xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision e8d89db01a97be04050fb2bc74ed6b6f01ed9169)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Edgeport USB Serial Converter driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
61da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Supports the following devices:
91da177e4SLinus Torvalds  *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * For questions or problems with this driver, contact Inside Out
121da177e4SLinus Torvalds  * Networks technical support, or Peter Berger <pberger@brimson.com>,
131da177e4SLinus Torvalds  * or Al Borchers <alborchers@steinerpoint.com>.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/jiffies.h>
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/tty.h>
211da177e4SLinus Torvalds #include <linux/tty_driver.h>
221da177e4SLinus Torvalds #include <linux/tty_flip.h>
231da177e4SLinus Torvalds #include <linux/module.h>
241da177e4SLinus Torvalds #include <linux/spinlock.h>
25241ca64fSMatthias Kaehlcke #include <linux/mutex.h>
261da177e4SLinus Torvalds #include <linux/serial.h>
275509076dSJohan Hovold #include <linux/swab.h>
28d733cec1SJohan Hovold #include <linux/kfifo.h>
291da177e4SLinus Torvalds #include <linux/ioctl.h>
30d12b219aSJaswinder Singh #include <linux/firmware.h>
312742fd88SAlan Cox #include <linux/uaccess.h>
321da177e4SLinus Torvalds #include <linux/usb.h>
33a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "io_16654.h"
361da177e4SLinus Torvalds #include "io_usbvend.h"
371da177e4SLinus Torvalds #include "io_ti.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
401da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define EPROM_PAGE_SIZE		64
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* different hardware types */
461da177e4SLinus Torvalds #define HARDWARE_TYPE_930	0
471da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP	1
481da177e4SLinus Torvalds 
492742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
502742fd88SAlan Cox #define	TI_MODE_CONFIGURING	0   /* Device has not entered start device */
512742fd88SAlan Cox #define	TI_MODE_BOOT		1   /* Staying in boot mode		   */
522742fd88SAlan Cox #define TI_MODE_DOWNLOAD	2   /* Made it to download mode		   */
5375899513SPeter E. Berger #define TI_MODE_TRANSITIONING	3   /*
5475899513SPeter E. Berger 				     * Currently in boot mode but
5575899513SPeter E. Berger 				     * transitioning to download mode
5675899513SPeter E. Berger 				     */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /* read urb state */
591da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
601da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
611da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Product information read from the Edgeport */
652742fd88SAlan Cox struct product_info {
662742fd88SAlan Cox 	int	TiMode;			/* Current TI Mode  */
672742fd88SAlan Cox 	__u8	hardware_type;		/* Type of hardware */
681da177e4SLinus Torvalds } __attribute__((packed));
691da177e4SLinus Torvalds 
70c0e34831SPeter E. Berger /*
71c0e34831SPeter E. Berger  * Edgeport firmware header
72c0e34831SPeter E. Berger  *
73c0e34831SPeter E. Berger  * "build_number" has been set to 0 in all three of the images I have
74c0e34831SPeter E. Berger  * seen, and Digi Tech Support suggests that it is safe to ignore it.
75c0e34831SPeter E. Berger  *
76c0e34831SPeter E. Berger  * "length" is the number of bytes of actual data following the header.
77c0e34831SPeter E. Berger  *
78c0e34831SPeter E. Berger  * "checksum" is the low order byte resulting from adding the values of
79c0e34831SPeter E. Berger  * all the data bytes.
80c0e34831SPeter E. Berger  */
81c0e34831SPeter E. Berger struct edgeport_fw_hdr {
82c0e34831SPeter E. Berger 	u8 major_version;
83c0e34831SPeter E. Berger 	u8 minor_version;
84c0e34831SPeter E. Berger 	__le16 build_number;
85c0e34831SPeter E. Berger 	__le16 length;
86c0e34831SPeter E. Berger 	u8 checksum;
87c0e34831SPeter E. Berger } __packed;
88c0e34831SPeter E. Berger 
891da177e4SLinus Torvalds struct edgeport_port {
901da177e4SLinus Torvalds 	__u16 uart_base;
911da177e4SLinus Torvalds 	__u16 dma_address;
921da177e4SLinus Torvalds 	__u8 shadow_msr;
931da177e4SLinus Torvalds 	__u8 shadow_mcr;
941da177e4SLinus Torvalds 	__u8 shadow_lsr;
951da177e4SLinus Torvalds 	__u8 lsr_mask;
9675899513SPeter E. Berger 	__u32 ump_read_timeout;		/*
9775899513SPeter E. Berger 					 * Number of milliseconds the UMP will
9875899513SPeter E. Berger 					 * wait without data before completing
9975899513SPeter E. Berger 					 * a read short
10075899513SPeter E. Berger 					 */
1011da177e4SLinus Torvalds 	int baud_rate;
1021da177e4SLinus Torvalds 	int close_pending;
1031da177e4SLinus Torvalds 	int lsr_event;
10448ee5801SJohan Hovold 
1051da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
1061da177e4SLinus Torvalds 	struct usb_serial_port	*port;
1071da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
1081da177e4SLinus Torvalds 	spinlock_t ep_lock;
1091da177e4SLinus Torvalds 	int ep_read_urb_state;
1101da177e4SLinus Torvalds 	int ep_write_urb_in_use;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds struct edgeport_serial {
1141da177e4SLinus Torvalds 	struct product_info product_info;
1152742fd88SAlan Cox 	u8 TI_I2C_Type;			/* Type of I2C in UMP */
11675899513SPeter E. Berger 	u8 TiReadI2C;			/*
11775899513SPeter E. Berger 					 * Set to TRUE if we have read the
11875899513SPeter E. Berger 					 * I2c in Boot Mode
11975899513SPeter E. Berger 					 */
120241ca64fSMatthias Kaehlcke 	struct mutex es_lock;
1211da177e4SLinus Torvalds 	int num_ports_open;
1221da177e4SLinus Torvalds 	struct usb_serial *serial;
12326c78daaSPeter E. Berger 	struct delayed_work heartbeat_work;
124c0e34831SPeter E. Berger 	int fw_version;
12526c78daaSPeter E. Berger 	bool use_heartbeat;
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /* Devices that this driver supports */
1307d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = {
1311da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1321da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1331da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1341da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1351da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1361da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1371da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1381da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1391da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1401da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1411da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1421da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1431da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1441da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1451da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1461da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1471da177e4SLinus Torvalds 	{ }
1481da177e4SLinus Torvalds };
1491da177e4SLinus Torvalds 
1507d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = {
1511da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1521da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1531da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1541da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1551da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1561da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1571da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1581da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1591da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1601da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1611da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1621da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
163fc4cbd75SMartin K. Petersen 	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1641da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
165fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
166fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
167fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
168fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1691da177e4SLinus Torvalds 	{ }
1701da177e4SLinus Torvalds };
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /* Devices that this driver supports */
1737d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
1741da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1751da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1761da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1781da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1791da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1801da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1811da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1821da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1831da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1841da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1851da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1861da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1911da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1921da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1931da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1941da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1951da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1961da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1971da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1981da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1991da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
2001da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
2011da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
2021da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
203fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
204fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
205fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
206fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
2071da177e4SLinus Torvalds 	{ }
2081da177e4SLinus Torvalds };
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
2111da177e4SLinus Torvalds 
21290ab5ee9SRusty Russell static bool ignore_cpu_rev;
2132742fd88SAlan Cox static int default_uart_mode;		/* RS232 */
2141da177e4SLinus Torvalds 
2152e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2162e124b4aSJiri Slaby 		int length);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2191da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2201da177e4SLinus Torvalds 
22195da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
22295da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
223ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
2241da177e4SLinus Torvalds 
225bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial,
226bebf1f18SPeter E. Berger 		const struct firmware *fw);
227bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial,
228bebf1f18SPeter E. Berger 		const struct firmware *fw);
229bebf1f18SPeter E. Berger 
230fc4cbd75SMartin K. Petersen /* sysfs attributes */
231fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
232fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
233fc4cbd75SMartin K. Petersen 
23426c78daaSPeter E. Berger /*
23526c78daaSPeter E. Berger  * Some release of Edgeport firmware "down3.bin" after version 4.80
23626c78daaSPeter E. Berger  * introduced code to automatically disconnect idle devices on some
23726c78daaSPeter E. Berger  * Edgeport models after periods of inactivity, typically ~60 seconds.
23826c78daaSPeter E. Berger  * This occurs without regard to whether ports on the device are open
23926c78daaSPeter E. Berger  * or not.  Digi International Tech Support suggested:
24026c78daaSPeter E. Berger  *
24126c78daaSPeter E. Berger  * 1.  Adding driver "heartbeat" code to reset the firmware timer by
24226c78daaSPeter E. Berger  *     requesting a descriptor record every 15 seconds, which should be
24326c78daaSPeter E. Berger  *     effective with newer firmware versions that require it, and benign
24426c78daaSPeter E. Berger  *     with older versions that do not. In practice 40 seconds seems often
24526c78daaSPeter E. Berger  *     enough.
24626c78daaSPeter E. Berger  * 2.  The heartbeat code is currently required only on Edgeport/416 models.
24726c78daaSPeter E. Berger  */
24826c78daaSPeter E. Berger #define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80)
24926c78daaSPeter E. Berger #define FW_HEARTBEAT_SECS 40
2501da177e4SLinus Torvalds 
251c3ece7e7SPeter E. Berger /* Timeouts in msecs: firmware downloads take longer */
252c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_DEFAULT 1000
253c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
254c3ece7e7SPeter E. Berger 
255*e8d89db0SJohan Hovold static int ti_vread_sync(struct usb_device *dev, u8 request, u16 value,
256*e8d89db0SJohan Hovold 		u16 index, void *data, int size)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	int status;
2591da177e4SLinus Torvalds 
2602742fd88SAlan Cox 	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
2612742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
2622742fd88SAlan Cox 			value, index, data, size, 1000);
2631da177e4SLinus Torvalds 	if (status < 0)
2641da177e4SLinus Torvalds 		return status;
2651da177e4SLinus Torvalds 	if (status != size) {
2660765590fSJohan Hovold 		dev_dbg(&dev->dev, "%s - wanted to read %d, but only read %d\n",
267441b62c1SHarvey Harrison 			__func__, size, status);
2681da177e4SLinus Torvalds 		return -ECOMM;
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
273c3ece7e7SPeter E. Berger static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
274*e8d89db0SJohan Hovold 		u16 index, void *data, int size, int timeout)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	int status;
2771da177e4SLinus Torvalds 
2782742fd88SAlan Cox 	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
2792742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
280c3ece7e7SPeter E. Berger 			value, index, data, size, timeout);
2811da177e4SLinus Torvalds 	if (status < 0)
2821da177e4SLinus Torvalds 		return status;
28366db9478SJohan Hovold 
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
287*e8d89db0SJohan Hovold static int send_cmd(struct usb_device *dev, u8 command, u8 moduleid,
288*e8d89db0SJohan Hovold 		u16 value, void *data, int size)
2891da177e4SLinus Torvalds {
290c3ece7e7SPeter E. Berger 	return ti_vsend_sync(dev, command, value, moduleid, data, size,
291c3ece7e7SPeter E. Berger 			TI_VSEND_TIMEOUT_DEFAULT);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
2952742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask)
2961da177e4SLinus Torvalds {
2971143832eSGreg Kroah-Hartman 	int port_number = port->port_number;
2981da177e4SLinus Torvalds 
29967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
3001da177e4SLinus Torvalds 
3012742fd88SAlan Cox 	return send_cmd(port->serial->dev,
3021da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
3031da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
3041da177e4SLinus Torvalds 					mask,
3051da177e4SLinus Torvalds 					NULL,
3061da177e4SLinus Torvalds 					0);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /**
3102742fd88SAlan Cox  * read_download_mem - Read edgeport memory from TI chip
3111da177e4SLinus Torvalds  * @dev: usb device pointer
3121da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3131da177e4SLinus Torvalds  * @length: Length of above data
3141da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3151da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3161da177e4SLinus Torvalds  */
3172742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address,
3181da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	int status = 0;
3211da177e4SLinus Torvalds 	__u8 read_length;
3225509076dSJohan Hovold 	u16 be_start_address;
3231da177e4SLinus Torvalds 
32467e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
3251da177e4SLinus Torvalds 
32675899513SPeter E. Berger 	/*
32775899513SPeter E. Berger 	 * Read in blocks of 64 bytes
3281da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3291da177e4SLinus Torvalds 	 */
3301da177e4SLinus Torvalds 	while (length) {
3311da177e4SLinus Torvalds 		if (length > 64)
3321da177e4SLinus Torvalds 			read_length = 64;
3331da177e4SLinus Torvalds 		else
3341da177e4SLinus Torvalds 			read_length = (__u8)length;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		if (read_length > 1) {
33767e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
3381da177e4SLinus Torvalds 		}
3395509076dSJohan Hovold 		/*
3405509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian
3415509076dSJohan Hovold 		 *       byte order regardless of host byte order.
3425509076dSJohan Hovold 		 */
3435509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
3442742fd88SAlan Cox 		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
3452742fd88SAlan Cox 					(__u16)address_type,
3465509076dSJohan Hovold 					be_start_address,
3472742fd88SAlan Cox 					buffer, read_length);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		if (status) {
35067e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
3511da177e4SLinus Torvalds 			return status;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 
3542742fd88SAlan Cox 		if (read_length > 1)
35559d33f2fSGreg Kroah-Hartman 			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		/* Update pointers/length */
3581da177e4SLinus Torvalds 		start_address += read_length;
3591da177e4SLinus Torvalds 		buffer += read_length;
3601da177e4SLinus Torvalds 		length -= read_length;
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	return status;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3662742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address,
3672742fd88SAlan Cox 						int length, __u8 *buffer)
3681da177e4SLinus Torvalds {
3692742fd88SAlan Cox 	return read_download_mem(dev, start_address, length,
3702742fd88SAlan Cox 					DTK_ADDR_SPACE_XDATA, buffer);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /* Read edgeport memory to a given block */
3742742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial,
3752742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	int status = 0;
3781da177e4SLinus Torvalds 	int i;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	for (i = 0; i < length; i++) {
3812742fd88SAlan Cox 		status = ti_vread_sync(serial->serial->dev,
3822742fd88SAlan Cox 				UMPC_MEMORY_READ, serial->TI_I2C_Type,
3832742fd88SAlan Cox 				(__u16)(start_address+i), &buffer[i], 0x01);
3841da177e4SLinus Torvalds 		if (status) {
38567e6da70SGreg Kroah-Hartman 			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
3861da177e4SLinus Torvalds 			return status;
3871da177e4SLinus Torvalds 		}
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
39067e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
3912742fd88SAlan Cox 		__func__, start_address, length);
39259d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	return status;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
4002742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial,
4012742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	int status = 0;
4041da177e4SLinus Torvalds 	int i;
405e9305d2fSJohan Hovold 	u8 *temp;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Must do a read before write */
4081da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
409e9305d2fSJohan Hovold 		temp = kmalloc(1, GFP_KERNEL);
41010c642d0SJohan Hovold 		if (!temp)
411e9305d2fSJohan Hovold 			return -ENOMEM;
41210c642d0SJohan Hovold 
413e9305d2fSJohan Hovold 		status = read_boot_mem(serial, 0, 1, temp);
414e9305d2fSJohan Hovold 		kfree(temp);
4151da177e4SLinus Torvalds 		if (status)
4161da177e4SLinus Torvalds 			return status;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	for (i = 0; i < length; ++i) {
420c3ece7e7SPeter E. Berger 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
421c3ece7e7SPeter E. Berger 				buffer[i], (u16)(i + start_address), NULL,
422c3ece7e7SPeter E. Berger 				0, TI_VSEND_TIMEOUT_DEFAULT);
4231da177e4SLinus Torvalds 		if (status)
4241da177e4SLinus Torvalds 			return status;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
42767e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
42859d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	return status;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4342742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial,
4352742fd88SAlan Cox 		int start_address, int length, __u8 address_type, __u8 *buffer)
4361da177e4SLinus Torvalds {
43767e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
4381da177e4SLinus Torvalds 	int status = 0;
4391da177e4SLinus Torvalds 	int write_length;
4405509076dSJohan Hovold 	u16 be_start_address;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4431da177e4SLinus Torvalds 
44425985edcSLucas De Marchi 	/* calculate the number of bytes left in the first page */
4452742fd88SAlan Cox 	write_length = EPROM_PAGE_SIZE -
4462742fd88SAlan Cox 				(start_address & (EPROM_PAGE_SIZE - 1));
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	if (write_length > length)
4491da177e4SLinus Torvalds 		write_length = length;
4501da177e4SLinus Torvalds 
45167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
4522742fd88SAlan Cox 		__func__, start_address, write_length);
45359d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, write_length, buffer);
4541da177e4SLinus Torvalds 
4555509076dSJohan Hovold 	/*
4565509076dSJohan Hovold 	 * Write first page.
4575509076dSJohan Hovold 	 *
4585509076dSJohan Hovold 	 * NOTE: Must use swab as wIndex is sent in little-endian byte order
4595509076dSJohan Hovold 	 *       regardless of host byte order.
4605509076dSJohan Hovold 	 */
4615509076dSJohan Hovold 	be_start_address = swab16((u16)start_address);
462c3ece7e7SPeter E. Berger 	status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
463c3ece7e7SPeter E. Berger 				(u16)address_type, be_start_address,
464c3ece7e7SPeter E. Berger 				buffer,	write_length, TI_VSEND_TIMEOUT_DEFAULT);
4651da177e4SLinus Torvalds 	if (status) {
46667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
4671da177e4SLinus Torvalds 		return status;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	length		-= write_length;
4711da177e4SLinus Torvalds 	start_address	+= write_length;
4721da177e4SLinus Torvalds 	buffer		+= write_length;
4731da177e4SLinus Torvalds 
47475899513SPeter E. Berger 	/*
47575899513SPeter E. Berger 	 * We should be aligned now -- can write max page size bytes at a
47675899513SPeter E. Berger 	 * time.
47775899513SPeter E. Berger 	 */
4781da177e4SLinus Torvalds 	while (length) {
4791da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
4801da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
4811da177e4SLinus Torvalds 		else
4821da177e4SLinus Torvalds 			write_length = length;
4831da177e4SLinus Torvalds 
48467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
4852742fd88SAlan Cox 			__func__, start_address, write_length);
48659d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, write_length, buffer);
4871da177e4SLinus Torvalds 
4885509076dSJohan Hovold 		/*
4895509076dSJohan Hovold 		 * Write next page.
4905509076dSJohan Hovold 		 *
4915509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian byte
4925509076dSJohan Hovold 		 *       order regardless of host byte order.
4935509076dSJohan Hovold 		 */
4945509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
4952742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
496c3ece7e7SPeter E. Berger 				(u16)address_type, be_start_address, buffer,
497c3ece7e7SPeter E. Berger 				write_length, TI_VSEND_TIMEOUT_DEFAULT);
4981da177e4SLinus Torvalds 		if (status) {
49967e6da70SGreg Kroah-Hartman 			dev_err(dev, "%s - ERROR %d\n", __func__, status);
5001da177e4SLinus Torvalds 			return status;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		length		-= write_length;
5041da177e4SLinus Torvalds 		start_address	+= write_length;
5051da177e4SLinus Torvalds 		buffer		+= write_length;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 	return status;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
51075899513SPeter E. Berger /*
51175899513SPeter E. Berger  * Examine the UMP DMA registers and LSR
5121da177e4SLinus Torvalds  *
5131da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5141da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5151da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5161da177e4SLinus Torvalds  */
5172742fd88SAlan Cox static int tx_active(struct edgeport_port *port)
5181da177e4SLinus Torvalds {
5191da177e4SLinus Torvalds 	int status;
5201da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
5211da177e4SLinus Torvalds 	__u8 *lsr;
5221da177e4SLinus Torvalds 	int bytes_left = 0;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
52510c642d0SJohan Hovold 	if (!oedb)
5261da177e4SLinus Torvalds 		return -ENOMEM;
5271da177e4SLinus Torvalds 
52875899513SPeter E. Berger 	/*
52975899513SPeter E. Berger 	 * Sigh, that's right, just one byte, as not all platforms can
53075899513SPeter E. Berger 	 * do DMA from stack
53175899513SPeter E. Berger 	 */
53275899513SPeter E. Berger 	lsr = kmalloc(1, GFP_KERNEL);
5331da177e4SLinus Torvalds 	if (!lsr) {
5341da177e4SLinus Torvalds 		kfree(oedb);
5351da177e4SLinus Torvalds 		return -ENOMEM;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5382742fd88SAlan Cox 	status = read_ram(port->port->serial->dev, port->dma_address,
5392742fd88SAlan Cox 						sizeof(*oedb), (void *)oedb);
5401da177e4SLinus Torvalds 	if (status)
5411da177e4SLinus Torvalds 		goto exit_is_tx_active;
5421da177e4SLinus Torvalds 
54367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - XByteCount    0x%X\n", __func__, oedb->XByteCount);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* and the LSR */
5462742fd88SAlan Cox 	status = read_ram(port->port->serial->dev,
5472742fd88SAlan Cox 			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	if (status)
5501da177e4SLinus Torvalds 		goto exit_is_tx_active;
55167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5541da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80) != 0)
5551da177e4SLinus Torvalds 		bytes_left += 64;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
5581da177e4SLinus Torvalds 		bytes_left += 1;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5611da177e4SLinus Torvalds exit_is_tx_active:
56267e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	kfree(lsr);
5651da177e4SLinus Torvalds 	kfree(oedb);
5661da177e4SLinus Torvalds 	return bytes_left;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5692742fd88SAlan Cox static int choose_config(struct usb_device *dev)
5701da177e4SLinus Torvalds {
5712742fd88SAlan Cox 	/*
5722742fd88SAlan Cox 	 * There may be multiple configurations on this device, in which case
5732742fd88SAlan Cox 	 * we would need to read and parse all of them to find out which one
5742742fd88SAlan Cox 	 * we want. However, we just support one config at this point,
5752742fd88SAlan Cox 	 * configuration # 1, which is Config Descriptor 0.
5762742fd88SAlan Cox 	 */
5771da177e4SLinus Torvalds 
57867e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
5792742fd88SAlan Cox 		__func__, dev->config->desc.bNumInterfaces);
58067e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - MAX Power            = %d\n",
5812742fd88SAlan Cox 		__func__, dev->config->desc.bMaxPower * 2);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
58467e6da70SGreg Kroah-Hartman 		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
5851da177e4SLinus Torvalds 		return -ENODEV;
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	return 0;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5912742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial,
5922742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	int status;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
5972742fd88SAlan Cox 		status = read_download_mem(serial->serial->dev,
5981da177e4SLinus Torvalds 					       start_address,
5991da177e4SLinus Torvalds 					       length,
6001da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6011da177e4SLinus Torvalds 					       buffer);
6021da177e4SLinus Torvalds 	} else {
6032742fd88SAlan Cox 		status = read_boot_mem(serial, start_address, length,
6041da177e4SLinus Torvalds 								buffer);
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 	return status;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6092742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address,
6102742fd88SAlan Cox 						int length, __u8 *buffer)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6132742fd88SAlan Cox 		return write_boot_mem(serial, start_address, length,
6141da177e4SLinus Torvalds 								buffer);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
6172742fd88SAlan Cox 		return write_i2c_mem(serial, start_address, length,
6182742fd88SAlan Cox 						serial->TI_I2C_Type, buffer);
6191da177e4SLinus Torvalds 	return -EINVAL;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
6232742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial,
6242742fd88SAlan Cox 				int desc_type, struct ti_i2c_desc *rom_desc)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	int start_address;
6271da177e4SLinus Torvalds 	int status;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
6301da177e4SLinus Torvalds 	start_address = 2;
6311da177e4SLinus Torvalds 	do {
6322742fd88SAlan Cox 		status = read_rom(serial,
6331da177e4SLinus Torvalds 				   start_address,
6341da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
6351da177e4SLinus Torvalds 				   (__u8 *)rom_desc);
6361da177e4SLinus Torvalds 		if (status)
6371da177e4SLinus Torvalds 			return 0;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
6401da177e4SLinus Torvalds 			return start_address;
6411da177e4SLinus Torvalds 
6425509076dSJohan Hovold 		start_address = start_address + sizeof(struct ti_i2c_desc) +
6435509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	return 0;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds /* Validate descriptor checksum */
6512742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	__u16 i;
6541da177e4SLinus Torvalds 	__u8 cs = 0;
6551da177e4SLinus Torvalds 
6565509076dSJohan Hovold 	for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
6571da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
6582742fd88SAlan Cox 
6591da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
66067e6da70SGreg Kroah-Hartman 		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
6611da177e4SLinus Torvalds 		return -EINVAL;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds /* Make sure that the I2C image is good */
6672742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
6701da177e4SLinus Torvalds 	int status = 0;
6711da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
6721da177e4SLinus Torvalds 	int start_address = 2;
6731da177e4SLinus Torvalds 	__u8 *buffer;
6741da177e4SLinus Torvalds 	__u16 ttype;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
67710c642d0SJohan Hovold 	if (!rom_desc)
6781da177e4SLinus Torvalds 		return -ENOMEM;
67910c642d0SJohan Hovold 
6801da177e4SLinus Torvalds 	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
6811da177e4SLinus Torvalds 	if (!buffer) {
6821da177e4SLinus Torvalds 		kfree(rom_desc);
6831da177e4SLinus Torvalds 		return -ENOMEM;
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 
6862742fd88SAlan Cox 	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
6872742fd88SAlan Cox 	status = read_rom(serial, 0, 1, buffer);
6881da177e4SLinus Torvalds 	if (status)
6892742fd88SAlan Cox 		goto out;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
692441b62c1SHarvey Harrison 		dev_err(dev, "%s - invalid buffer signature\n", __func__);
6931da177e4SLinus Torvalds 		status = -ENODEV;
6942742fd88SAlan Cox 		goto out;
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	do {
6982742fd88SAlan Cox 		/* Validate the I2C */
6992742fd88SAlan Cox 		status = read_rom(serial,
7001da177e4SLinus Torvalds 				start_address,
7011da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7021da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7031da177e4SLinus Torvalds 		if (status)
7041da177e4SLinus Torvalds 			break;
7051da177e4SLinus Torvalds 
7062742fd88SAlan Cox 		if ((start_address + sizeof(struct ti_i2c_desc) +
7075509076dSJohan Hovold 			le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
7081da177e4SLinus Torvalds 			status = -ENODEV;
70967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 		}
7121da177e4SLinus Torvalds 
71367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
7141da177e4SLinus Torvalds 
7152742fd88SAlan Cox 		/* Skip type 2 record */
7161da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
7171da177e4SLinus Torvalds 		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
7181da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
7192742fd88SAlan Cox 			/* Read the descriptor data */
7202742fd88SAlan Cox 			status = read_rom(serial, start_address +
7212742fd88SAlan Cox 						sizeof(struct ti_i2c_desc),
7225509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size),
7235509076dSJohan Hovold 						buffer);
7241da177e4SLinus Torvalds 			if (status)
7251da177e4SLinus Torvalds 				break;
7261da177e4SLinus Torvalds 
7272742fd88SAlan Cox 			status = valid_csum(rom_desc, buffer);
7281da177e4SLinus Torvalds 			if (status)
7291da177e4SLinus Torvalds 				break;
7301da177e4SLinus Torvalds 		}
7312742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc) +
7325509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
7331da177e4SLinus Torvalds 
7342742fd88SAlan Cox 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
7352742fd88SAlan Cox 				(start_address < TI_MAX_I2C_SIZE));
7361da177e4SLinus Torvalds 
7372742fd88SAlan Cox 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
7382742fd88SAlan Cox 				(start_address > TI_MAX_I2C_SIZE))
7391da177e4SLinus Torvalds 		status = -ENODEV;
7401da177e4SLinus Torvalds 
7412742fd88SAlan Cox out:
7421da177e4SLinus Torvalds 	kfree(buffer);
7431da177e4SLinus Torvalds 	kfree(rom_desc);
7441da177e4SLinus Torvalds 	return status;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7472742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	int status;
7501da177e4SLinus Torvalds 	int start_address;
7511da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7521da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
75367e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
75610c642d0SJohan Hovold 	if (!rom_desc)
7571da177e4SLinus Torvalds 		return -ENOMEM;
75810c642d0SJohan Hovold 
7592742fd88SAlan Cox 	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
7602742fd88SAlan Cox 								rom_desc);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if (!start_address) {
76367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
7641da177e4SLinus Torvalds 		status = -ENODEV;
7651da177e4SLinus Torvalds 		goto exit;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 
7682742fd88SAlan Cox 	/* Read the descriptor data */
7692742fd88SAlan Cox 	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
7705509076dSJohan Hovold 					le16_to_cpu(rom_desc->Size), buffer);
7711da177e4SLinus Torvalds 	if (status)
7721da177e4SLinus Torvalds 		goto exit;
7731da177e4SLinus Torvalds 
7742742fd88SAlan Cox 	status = valid_csum(rom_desc, buffer);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
77767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - IonConfig      0x%x\n", __func__, desc->IonConfig);
77867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Version          %d\n", __func__, desc->Version);
77967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Cpu/Board      0x%x\n", __func__, desc->CpuRev_BoardRev);
78067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumPorts         %d\n", __func__, desc->NumPorts);
78167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumVirtualPorts  %d\n", __func__, desc->NumVirtualPorts);
78267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - TotalPorts       %d\n", __func__, desc->TotalPorts);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds exit:
7851da177e4SLinus Torvalds 	kfree(rom_desc);
7861da177e4SLinus Torvalds 	return status;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds /* Build firmware header used for firmware update */
790277bf37bSPeter E. Berger static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	__u8 *buffer;
7931da177e4SLinus Torvalds 	int buffer_size;
7941da177e4SLinus Torvalds 	int i;
7951da177e4SLinus Torvalds 	__u8 cs = 0;
7961da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
7971da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
7981da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
799c0e34831SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
8001da177e4SLinus Torvalds 
80175899513SPeter E. Berger 	/*
80275899513SPeter E. Berger 	 * In order to update the I2C firmware we must change the type 2 record
8032742fd88SAlan Cox 	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
8042742fd88SAlan Cox 	 * Then while in boot mode, the driver will download the latest
8052742fd88SAlan Cox 	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
8062742fd88SAlan Cox 	 * device comes back up in download mode the driver will cause the new
8072742fd88SAlan Cox 	 * firmware to be copied from the UMP Ram to I2C and the firmware will
8082742fd88SAlan Cox 	 * update the record type from 0xf2 to 0x02.
8092742fd88SAlan Cox 	 */
8101da177e4SLinus Torvalds 
81175899513SPeter E. Berger 	/*
81275899513SPeter E. Berger 	 * Allocate a 15.5k buffer + 2 bytes for version number (Firmware
81375899513SPeter E. Berger 	 * Record)
81475899513SPeter E. Berger 	 */
8152742fd88SAlan Cox 	buffer_size = (((1024 * 16) - 512 ) +
8162742fd88SAlan Cox 			sizeof(struct ti_i2c_firmware_rec));
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	buffer = kmalloc(buffer_size, GFP_KERNEL);
81910c642d0SJohan Hovold 	if (!buffer)
8201da177e4SLinus Torvalds 		return -ENOMEM;
8211da177e4SLinus Torvalds 
82275899513SPeter E. Berger 	/* Set entire image of 0xffs */
8231da177e4SLinus Torvalds 	memset(buffer, 0xff, buffer_size);
8241da177e4SLinus Torvalds 
8252742fd88SAlan Cox 	/* Copy version number into firmware record */
8261da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
8271da177e4SLinus Torvalds 
828c0e34831SPeter E. Berger 	firmware_rec->Ver_Major	= fw_hdr->major_version;
829c0e34831SPeter E. Berger 	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
8301da177e4SLinus Torvalds 
8312742fd88SAlan Cox 	/* Pointer to fw_down memory image */
832d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
835d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
8361da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
8391da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	kfree(buffer);
8431da177e4SLinus Torvalds 
8442742fd88SAlan Cox 	/* Build new header */
8451da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
8461da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
849c03890ffSJohan Hovold 	i2c_header->Size	= cpu_to_le16(buffer_size);
8501da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
851c0e34831SPeter E. Berger 	firmware_rec->Ver_Major	= fw_hdr->major_version;
852c0e34831SPeter E. Berger 	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	return 0;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
8582742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial)
8591da177e4SLinus Torvalds {
86067e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
8611da177e4SLinus Torvalds 	int status;
862e9305d2fSJohan Hovold 	u8 *data;
863e9305d2fSJohan Hovold 
864e9305d2fSJohan Hovold 	data = kmalloc(1, GFP_KERNEL);
86510c642d0SJohan Hovold 	if (!data)
866e9305d2fSJohan Hovold 		return -ENOMEM;
8671da177e4SLinus Torvalds 
8682742fd88SAlan Cox 	/* Try to read type 2 */
8692742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
870e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
8711da177e4SLinus Torvalds 	if (status)
87267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
8731da177e4SLinus Torvalds 	else
87467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
875e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
87667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
8771da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
878e9305d2fSJohan Hovold 		goto out;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 
8812742fd88SAlan Cox 	/* Try to read type 3 */
8822742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
883e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
8841da177e4SLinus Torvalds 	if (status)
88567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
8861da177e4SLinus Torvalds 	else
88767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
888e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
88967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
8901da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
891e9305d2fSJohan Hovold 		goto out;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
89467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Unknown\n", __func__);
8951da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
896e9305d2fSJohan Hovold 	status = -ENODEV;
897e9305d2fSJohan Hovold out:
898e9305d2fSJohan Hovold 	kfree(data);
899e9305d2fSJohan Hovold 	return status;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9022742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer,
9032742fd88SAlan Cox 						int length, int *num_sent)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	int status;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	status = usb_bulk_msg(serial->dev,
9081da177e4SLinus Torvalds 			usb_sndbulkpipe(serial->dev,
9091da177e4SLinus Torvalds 				serial->port[0]->bulk_out_endpointAddress),
9102742fd88SAlan Cox 			buffer, length, num_sent, 1000);
9111da177e4SLinus Torvalds 	return status;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
9152742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image,
9162742fd88SAlan Cox 							int image_length)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	int status = 0;
9191da177e4SLinus Torvalds 	int pos;
9201da177e4SLinus Torvalds 	int transfer;
9211da177e4SLinus Torvalds 	int done;
9221da177e4SLinus Torvalds 
9232742fd88SAlan Cox 	/* Transfer firmware image */
9241da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
9252742fd88SAlan Cox 		/* Read the next buffer from file */
9261da177e4SLinus Torvalds 		transfer = image_length - pos;
9271da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
9281da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
9291da177e4SLinus Torvalds 
9302742fd88SAlan Cox 		/* Transfer data */
9312742fd88SAlan Cox 		status = bulk_xfer(serial->serial, &image[pos],
9322742fd88SAlan Cox 							transfer, &done);
9331da177e4SLinus Torvalds 		if (status)
9341da177e4SLinus Torvalds 			break;
9352742fd88SAlan Cox 		/* Advance buffer pointer */
9361da177e4SLinus Torvalds 		pos += done;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	return status;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9422742fd88SAlan Cox /* FIXME!!! */
9432742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9482742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
9492742fd88SAlan Cox {
9502742fd88SAlan Cox 	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
9512742fd88SAlan Cox }
9522742fd88SAlan Cox 
953dcb8e99dSPeter E. Berger static int check_fw_sanity(struct edgeport_serial *serial,
954dcb8e99dSPeter E. Berger 		const struct firmware *fw)
955dcb8e99dSPeter E. Berger {
956dcb8e99dSPeter E. Berger 	u16 length_total;
957dcb8e99dSPeter E. Berger 	u8 checksum = 0;
958dcb8e99dSPeter E. Berger 	int pos;
959dcb8e99dSPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
960dcb8e99dSPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
961dcb8e99dSPeter E. Berger 
962dcb8e99dSPeter E. Berger 	if (fw->size < sizeof(struct edgeport_fw_hdr)) {
963dcb8e99dSPeter E. Berger 		dev_err(dev, "incomplete fw header\n");
964dcb8e99dSPeter E. Berger 		return -EINVAL;
965dcb8e99dSPeter E. Berger 	}
966dcb8e99dSPeter E. Berger 
967dcb8e99dSPeter E. Berger 	length_total = le16_to_cpu(fw_hdr->length) +
968dcb8e99dSPeter E. Berger 			sizeof(struct edgeport_fw_hdr);
969dcb8e99dSPeter E. Berger 
970dcb8e99dSPeter E. Berger 	if (fw->size != length_total) {
971dcb8e99dSPeter E. Berger 		dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
972dcb8e99dSPeter E. Berger 				length_total, fw->size);
973dcb8e99dSPeter E. Berger 		return -EINVAL;
974dcb8e99dSPeter E. Berger 	}
975dcb8e99dSPeter E. Berger 
976dcb8e99dSPeter E. Berger 	for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
977dcb8e99dSPeter E. Berger 		checksum += fw->data[pos];
978dcb8e99dSPeter E. Berger 
979dcb8e99dSPeter E. Berger 	if (checksum != fw_hdr->checksum) {
980dcb8e99dSPeter E. Berger 		dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
981dcb8e99dSPeter E. Berger 				fw_hdr->checksum, checksum);
982dcb8e99dSPeter E. Berger 		return -EINVAL;
983dcb8e99dSPeter E. Berger 	}
984dcb8e99dSPeter E. Berger 
985dcb8e99dSPeter E. Berger 	return 0;
986dcb8e99dSPeter E. Berger }
987dcb8e99dSPeter E. Berger 
98875899513SPeter E. Berger /*
9891da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
9901da177e4SLinus Torvalds  *
9911da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
9921da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
9931da177e4SLinus Torvalds  */
994b4146162SPeter E. Berger static int download_fw(struct edgeport_serial *serial)
9951da177e4SLinus Torvalds {
996ef9324b2SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
9971da177e4SLinus Torvalds 	int status = 0;
9981da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
999b4146162SPeter E. Berger 	const struct firmware *fw;
1000b4146162SPeter E. Berger 	const char *fw_name = "edgeport/down3.bin";
1001b4146162SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr;
1002c0e34831SPeter E. Berger 
1003b4146162SPeter E. Berger 	status = request_firmware(&fw, fw_name, dev);
1004b4146162SPeter E. Berger 	if (status) {
1005b4146162SPeter E. Berger 		dev_err(dev, "Failed to load image \"%s\" err %d\n",
1006b4146162SPeter E. Berger 				fw_name, status);
1007b4146162SPeter E. Berger 		return status;
1008b4146162SPeter E. Berger 	}
1009c0e34831SPeter E. Berger 
1010b4146162SPeter E. Berger 	if (check_fw_sanity(serial, fw)) {
1011b4146162SPeter E. Berger 		status = -EINVAL;
1012b4146162SPeter E. Berger 		goto out;
1013b4146162SPeter E. Berger 	}
1014b4146162SPeter E. Berger 
1015b4146162SPeter E. Berger 	fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1016b4146162SPeter E. Berger 
1017b4146162SPeter E. Berger 	/* If on-board version is newer, "fw_version" will be updated later. */
1018c0e34831SPeter E. Berger 	serial->fw_version = (fw_hdr->major_version << 8) +
1019c0e34831SPeter E. Berger 			fw_hdr->minor_version;
10201da177e4SLinus Torvalds 
102175899513SPeter E. Berger 	/*
102275899513SPeter E. Berger 	 * This routine is entered by both the BOOT mode and the Download mode
10231da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10241da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10251da177e4SLinus Torvalds 	 */
10261da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10291da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10301da177e4SLinus Torvalds 
10312742fd88SAlan Cox 	status = choose_config(serial->serial->dev);
10321da177e4SLinus Torvalds 	if (status)
1033b4146162SPeter E. Berger 		goto out;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10361da177e4SLinus Torvalds 	if (!interface) {
1037441b62c1SHarvey Harrison 		dev_err(dev, "%s - no interface set, error!\n", __func__);
1038b4146162SPeter E. Berger 		status = -ENODEV;
1039b4146162SPeter E. Berger 		goto out;
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 
10422742fd88SAlan Cox 	/*
10432742fd88SAlan Cox 	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10442742fd88SAlan Cox 	 * if we have more than one endpoint we are definitely in download
10452742fd88SAlan Cox 	 * mode
10462742fd88SAlan Cox 	 */
1047bebf1f18SPeter E. Berger 	if (interface->bNumEndpoints > 1) {
10481da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1049b4146162SPeter E. Berger 		status = do_download_mode(serial, fw);
1050b4146162SPeter E. Berger 	} else {
10512742fd88SAlan Cox 		/* Otherwise we will remain in configuring mode */
10521da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
1053b4146162SPeter E. Berger 		status = do_boot_mode(serial, fw);
1054b4146162SPeter E. Berger 	}
10551da177e4SLinus Torvalds 
1056b4146162SPeter E. Berger out:
1057b4146162SPeter E. Berger 	release_firmware(fw);
1058b4146162SPeter E. Berger 	return status;
1059bebf1f18SPeter E. Berger }
1060bebf1f18SPeter E. Berger 
1061bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial,
1062bebf1f18SPeter E. Berger 		const struct firmware *fw)
1063bebf1f18SPeter E. Berger {
1064bebf1f18SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
1065bebf1f18SPeter E. Berger 	int status = 0;
1066bebf1f18SPeter E. Berger 	int start_address;
1067bebf1f18SPeter E. Berger 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
1068bebf1f18SPeter E. Berger 	int download_cur_ver;
1069bebf1f18SPeter E. Berger 	int download_new_ver;
1070bebf1f18SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
10711da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
10721da177e4SLinus Torvalds 
107367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
10741da177e4SLinus Torvalds 
10752742fd88SAlan Cox 	status = check_i2c_image(serial);
10761da177e4SLinus Torvalds 	if (status) {
107767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
10781da177e4SLinus Torvalds 		return status;
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 
108175899513SPeter E. Berger 	/*
108275899513SPeter E. Berger 	 * Validate Hardware version number
10831da177e4SLinus Torvalds 	 * Read Manufacturing Descriptor from TI Based Edgeport
10841da177e4SLinus Torvalds 	 */
10851da177e4SLinus Torvalds 	ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
108610c642d0SJohan Hovold 	if (!ti_manuf_desc)
10871da177e4SLinus Torvalds 		return -ENOMEM;
108810c642d0SJohan Hovold 
10892742fd88SAlan Cox 	status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
10901da177e4SLinus Torvalds 	if (status) {
10911da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
10921da177e4SLinus Torvalds 		return status;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10952742fd88SAlan Cox 	/* Check version number of ION descriptor */
10962742fd88SAlan Cox 	if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
109767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
10982742fd88SAlan Cox 			__func__, ti_cpu_rev(ti_manuf_desc));
10991da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
11001da177e4SLinus Torvalds 		return -EINVAL;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
11041da177e4SLinus Torvalds 	if (!rom_desc) {
11051da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
11061da177e4SLinus Torvalds 		return -ENOMEM;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 
11092742fd88SAlan Cox 	/* Search for type 2 record (firmware record) */
11102742fd88SAlan Cox 	start_address = get_descriptor_addr(serial,
11112742fd88SAlan Cox 			I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
11122742fd88SAlan Cox 	if (start_address != 0) {
11131da177e4SLinus Torvalds 		struct ti_i2c_firmware_rec *firmware_version;
1114e9305d2fSJohan Hovold 		u8 *record;
11151da177e4SLinus Torvalds 
1116bebf1f18SPeter E. Berger 		dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n",
1117bebf1f18SPeter E. Berger 				__func__);
11181da177e4SLinus Torvalds 
11192742fd88SAlan Cox 		firmware_version = kmalloc(sizeof(*firmware_version),
11202742fd88SAlan Cox 							GFP_KERNEL);
11211da177e4SLinus Torvalds 		if (!firmware_version) {
11221da177e4SLinus Torvalds 			kfree(rom_desc);
11231da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11241da177e4SLinus Torvalds 			return -ENOMEM;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds 
112775899513SPeter E. Berger 		/*
112875899513SPeter E. Berger 		 * Validate version number
11292742fd88SAlan Cox 		 * Read the descriptor data
11302742fd88SAlan Cox 		 */
11312742fd88SAlan Cox 		status = read_rom(serial, start_address +
11322742fd88SAlan Cox 				sizeof(struct ti_i2c_desc),
11331da177e4SLinus Torvalds 				sizeof(struct ti_i2c_firmware_rec),
11341da177e4SLinus Torvalds 				(__u8 *)firmware_version);
11351da177e4SLinus Torvalds 		if (status) {
11361da177e4SLinus Torvalds 			kfree(firmware_version);
11371da177e4SLinus Torvalds 			kfree(rom_desc);
11381da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11391da177e4SLinus Torvalds 			return status;
11401da177e4SLinus Torvalds 		}
11411da177e4SLinus Torvalds 
114275899513SPeter E. Berger 		/*
114375899513SPeter E. Berger 		 * Check version number of download with current
114475899513SPeter E. Berger 		 * version in I2c
114575899513SPeter E. Berger 		 */
11461da177e4SLinus Torvalds 		download_cur_ver = (firmware_version->Ver_Major << 8) +
11471da177e4SLinus Torvalds 				   (firmware_version->Ver_Minor);
1148c0e34831SPeter E. Berger 		download_new_ver = (fw_hdr->major_version << 8) +
1149c0e34831SPeter E. Berger 				   (fw_hdr->minor_version);
11501da177e4SLinus Torvalds 
115167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
115267e6da70SGreg Kroah-Hartman 			__func__, firmware_version->Ver_Major,
11531da177e4SLinus Torvalds 			firmware_version->Ver_Minor,
1154c0e34831SPeter E. Berger 			fw_hdr->major_version, fw_hdr->minor_version);
11551da177e4SLinus Torvalds 
115675899513SPeter E. Berger 		/*
115775899513SPeter E. Berger 		 * Check if we have an old version in the I2C and
115875899513SPeter E. Berger 		 * update if necessary
115975899513SPeter E. Berger 		 */
11600827a9ffSGreg Kroah-Hartman 		if (download_cur_ver < download_new_ver) {
116167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1162441b62c1SHarvey Harrison 				__func__,
11631da177e4SLinus Torvalds 				firmware_version->Ver_Major,
11641da177e4SLinus Torvalds 				firmware_version->Ver_Minor,
1165c0e34831SPeter E. Berger 				fw_hdr->major_version,
1166c0e34831SPeter E. Berger 				fw_hdr->minor_version);
11671da177e4SLinus Torvalds 
1168e9305d2fSJohan Hovold 			record = kmalloc(1, GFP_KERNEL);
1169e9305d2fSJohan Hovold 			if (!record) {
1170e9305d2fSJohan Hovold 				kfree(firmware_version);
1171e9305d2fSJohan Hovold 				kfree(rom_desc);
1172e9305d2fSJohan Hovold 				kfree(ti_manuf_desc);
1173e9305d2fSJohan Hovold 				return -ENOMEM;
1174e9305d2fSJohan Hovold 			}
1175bebf1f18SPeter E. Berger 			/*
1176bebf1f18SPeter E. Berger 			 * In order to update the I2C firmware we must
11772742fd88SAlan Cox 			 * change the type 2 record to type 0xF2. This
11782742fd88SAlan Cox 			 * will force the UMP to come up in Boot Mode.
11792742fd88SAlan Cox 			 * Then while in boot mode, the driver will
11802742fd88SAlan Cox 			 * download the latest firmware (padded to
11812742fd88SAlan Cox 			 * 15.5k) into the UMP ram. Finally when the
11822742fd88SAlan Cox 			 * device comes back up in download mode the
11832742fd88SAlan Cox 			 * driver will cause the new firmware to be
11842742fd88SAlan Cox 			 * copied from the UMP Ram to I2C and the
11852742fd88SAlan Cox 			 * firmware will update the record type from
11862742fd88SAlan Cox 			 * 0xf2 to 0x02.
11872742fd88SAlan Cox 			 */
1188e9305d2fSJohan Hovold 			*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11891da177e4SLinus Torvalds 
119075899513SPeter E. Berger 			/*
119175899513SPeter E. Berger 			 * Change the I2C Firmware record type to
119275899513SPeter E. Berger 			 * 0xf2 to trigger an update
119375899513SPeter E. Berger 			 */
11942742fd88SAlan Cox 			status = write_rom(serial, start_address,
1195e9305d2fSJohan Hovold 					sizeof(*record), record);
11961da177e4SLinus Torvalds 			if (status) {
1197e9305d2fSJohan Hovold 				kfree(record);
11981da177e4SLinus Torvalds 				kfree(firmware_version);
11991da177e4SLinus Torvalds 				kfree(rom_desc);
12001da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12011da177e4SLinus Torvalds 				return status;
12021da177e4SLinus Torvalds 			}
12031da177e4SLinus Torvalds 
120475899513SPeter E. Berger 			/*
120575899513SPeter E. Berger 			 * verify the write -- must do this in order
12062742fd88SAlan Cox 			 * for write to complete before we do the
12072742fd88SAlan Cox 			 * hardware reset
12082742fd88SAlan Cox 			 */
12092742fd88SAlan Cox 			status = read_rom(serial,
12101da177e4SLinus Torvalds 						start_address,
1211e9305d2fSJohan Hovold 						sizeof(*record),
1212e9305d2fSJohan Hovold 						record);
12131da177e4SLinus Torvalds 			if (status) {
1214e9305d2fSJohan Hovold 				kfree(record);
12151da177e4SLinus Torvalds 				kfree(firmware_version);
12161da177e4SLinus Torvalds 				kfree(rom_desc);
12171da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12181da177e4SLinus Torvalds 				return status;
12191da177e4SLinus Torvalds 			}
12201da177e4SLinus Torvalds 
1221e9305d2fSJohan Hovold 			if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1222bebf1f18SPeter E. Berger 				dev_err(dev, "%s - error resetting device\n",
1223bebf1f18SPeter E. Berger 						__func__);
1224e9305d2fSJohan Hovold 				kfree(record);
12251da177e4SLinus Torvalds 				kfree(firmware_version);
12261da177e4SLinus Torvalds 				kfree(rom_desc);
12271da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12281da177e4SLinus Torvalds 				return -ENODEV;
12291da177e4SLinus Torvalds 			}
12301da177e4SLinus Torvalds 
123167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
12321da177e4SLinus Torvalds 
12332742fd88SAlan Cox 			/* Reset UMP -- Back to BOOT MODE */
12342742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
12352742fd88SAlan Cox 					UMPC_HARDWARE_RESET,
1236c3ece7e7SPeter E. Berger 					0, 0, NULL, 0,
1237c3ece7e7SPeter E. Berger 					TI_VSEND_TIMEOUT_DEFAULT);
12381da177e4SLinus Torvalds 
1239bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - HARDWARE RESET return %d\n",
1240bebf1f18SPeter E. Berger 					__func__, status);
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 			/* return an error on purpose. */
1243e9305d2fSJohan Hovold 			kfree(record);
12441da177e4SLinus Torvalds 			kfree(firmware_version);
12451da177e4SLinus Torvalds 			kfree(rom_desc);
12461da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
12471da177e4SLinus Torvalds 			return -ENODEV;
1248bebf1f18SPeter E. Berger 		}
1249c0e34831SPeter E. Berger 		/* Same or newer fw version is already loaded */
1250c0e34831SPeter E. Berger 		serial->fw_version = download_cur_ver;
12511da177e4SLinus Torvalds 		kfree(firmware_version);
12521da177e4SLinus Torvalds 	}
12532742fd88SAlan Cox 	/* Search for type 0xF2 record (firmware blank record) */
1254bebf1f18SPeter E. Berger 	else {
1255bebf1f18SPeter E. Berger 		start_address = get_descriptor_addr(serial,
1256bebf1f18SPeter E. Berger 				I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc);
1257bebf1f18SPeter E. Berger 		if (start_address != 0) {
12582742fd88SAlan Cox #define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
12592742fd88SAlan Cox 				sizeof(struct ti_i2c_firmware_rec))
12601da177e4SLinus Torvalds 			__u8 *header;
12611da177e4SLinus Torvalds 			__u8 *vheader;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
12641da177e4SLinus Torvalds 			if (!header) {
12651da177e4SLinus Torvalds 				kfree(rom_desc);
12661da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12671da177e4SLinus Torvalds 				return -ENOMEM;
12681da177e4SLinus Torvalds 			}
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
12711da177e4SLinus Torvalds 			if (!vheader) {
12721da177e4SLinus Torvalds 				kfree(header);
12731da177e4SLinus Torvalds 				kfree(rom_desc);
12741da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12751da177e4SLinus Torvalds 				return -ENOMEM;
12761da177e4SLinus Torvalds 			}
12771da177e4SLinus Torvalds 
1278bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n",
1279bebf1f18SPeter E. Berger 					__func__);
12801da177e4SLinus Torvalds 
12812742fd88SAlan Cox 			/*
12822742fd88SAlan Cox 			 * In order to update the I2C firmware we must change
12832742fd88SAlan Cox 			 * the type 2 record to type 0xF2. This will force the
12842742fd88SAlan Cox 			 * UMP to come up in Boot Mode.  Then while in boot
12852742fd88SAlan Cox 			 * mode, the driver will download the latest firmware
12862742fd88SAlan Cox 			 * (padded to 15.5k) into the UMP ram. Finally when the
12872742fd88SAlan Cox 			 * device comes back up in download mode the driver
12882742fd88SAlan Cox 			 * will cause the new firmware to be copied from the
12892742fd88SAlan Cox 			 * UMP Ram to I2C and the firmware will update the
12902742fd88SAlan Cox 			 * record type from 0xf2 to 0x02.
12912742fd88SAlan Cox 			 */
1292277bf37bSPeter E. Berger 			status = build_i2c_fw_hdr(header, fw);
12931da177e4SLinus Torvalds 			if (status) {
12941da177e4SLinus Torvalds 				kfree(vheader);
12951da177e4SLinus Torvalds 				kfree(header);
12961da177e4SLinus Torvalds 				kfree(rom_desc);
12971da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
1298fd6e5bbbSRoel Kluin 				return -EINVAL;
12991da177e4SLinus Torvalds 			}
13001da177e4SLinus Torvalds 
130175899513SPeter E. Berger 			/*
130275899513SPeter E. Berger 			 * Update I2C with type 0xf2 record with correct
130375899513SPeter E. Berger 			 * size and checksum
130475899513SPeter E. Berger 			 */
13052742fd88SAlan Cox 			status = write_rom(serial,
13061da177e4SLinus Torvalds 						start_address,
13071da177e4SLinus Torvalds 						HEADER_SIZE,
13081da177e4SLinus Torvalds 						header);
13091da177e4SLinus Torvalds 			if (status) {
13101da177e4SLinus Torvalds 				kfree(vheader);
13111da177e4SLinus Torvalds 				kfree(header);
13121da177e4SLinus Torvalds 				kfree(rom_desc);
13131da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13149800eb33SRoel Kluin 				return -EINVAL;
13151da177e4SLinus Torvalds 			}
13161da177e4SLinus Torvalds 
131775899513SPeter E. Berger 			/*
131875899513SPeter E. Berger 			 * verify the write -- must do this in order for
131975899513SPeter E. Berger 			 * write to complete before we do the hardware reset
132075899513SPeter E. Berger 			 */
13212742fd88SAlan Cox 			status = read_rom(serial, start_address,
13222742fd88SAlan Cox 							HEADER_SIZE, vheader);
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 			if (status) {
1325bebf1f18SPeter E. Berger 				dev_dbg(dev, "%s - can't read header back\n",
1326bebf1f18SPeter E. Berger 						__func__);
13271da177e4SLinus Torvalds 				kfree(vheader);
13281da177e4SLinus Torvalds 				kfree(header);
13291da177e4SLinus Torvalds 				kfree(rom_desc);
13301da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13311da177e4SLinus Torvalds 				return status;
13321da177e4SLinus Torvalds 			}
13331da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
1334bebf1f18SPeter E. Berger 				dev_dbg(dev, "%s - write download record failed\n",
1335bebf1f18SPeter E. Berger 						__func__);
13361da177e4SLinus Torvalds 				kfree(vheader);
13371da177e4SLinus Torvalds 				kfree(header);
13381da177e4SLinus Torvalds 				kfree(rom_desc);
13391da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13401e29709eSRoel Kluin 				return -EINVAL;
13411da177e4SLinus Torvalds 			}
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 			kfree(vheader);
13441da177e4SLinus Torvalds 			kfree(header);
13451da177e4SLinus Torvalds 
134667e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Start firmware update\n", __func__);
13471da177e4SLinus Torvalds 
13482742fd88SAlan Cox 			/* Tell firmware to copy download image into I2C */
13492742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
1350c3ece7e7SPeter E. Berger 					UMPC_COPY_DNLD_TO_I2C,
1351c3ece7e7SPeter E. Berger 					0, 0, NULL, 0,
1352c3ece7e7SPeter E. Berger 					TI_VSEND_TIMEOUT_FW_DOWNLOAD);
13531da177e4SLinus Torvalds 
1354bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - Update complete 0x%x\n", __func__,
1355bebf1f18SPeter E. Berger 					status);
13561da177e4SLinus Torvalds 			if (status) {
13572742fd88SAlan Cox 				dev_err(dev,
13582742fd88SAlan Cox 					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
13592742fd88SAlan Cox 					__func__);
13601da177e4SLinus Torvalds 				kfree(rom_desc);
13611da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13621da177e4SLinus Torvalds 				return status;
13631da177e4SLinus Torvalds 			}
13641da177e4SLinus Torvalds 		}
1365bebf1f18SPeter E. Berger 	}
13661da177e4SLinus Torvalds 
1367bebf1f18SPeter E. Berger 	/* The device is running the download code */
13681da177e4SLinus Torvalds 	kfree(rom_desc);
13691da177e4SLinus Torvalds 	kfree(ti_manuf_desc);
13701da177e4SLinus Torvalds 	return 0;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
1373bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial,
1374bebf1f18SPeter E. Berger 		const struct firmware *fw)
1375bebf1f18SPeter E. Berger {
1376bebf1f18SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
1377bebf1f18SPeter E. Berger 	int status = 0;
1378bebf1f18SPeter E. Berger 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
1379bebf1f18SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1380bebf1f18SPeter E. Berger 
138167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
13821da177e4SLinus Torvalds 
13832742fd88SAlan Cox 	/* Configure the TI device so we can use the BULK pipes for download */
13842742fd88SAlan Cox 	status = config_boot_dev(serial->serial->dev);
13851da177e4SLinus Torvalds 	if (status)
13861da177e4SLinus Torvalds 		return status;
13871da177e4SLinus Torvalds 
13882742fd88SAlan Cox 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
13892742fd88SAlan Cox 							!= USB_VENDOR_ID_ION) {
139067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
13911da177e4SLinus Torvalds 			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13921da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13932742fd88SAlan Cox 		goto stayinbootmode;
13941da177e4SLinus Torvalds 	}
13951da177e4SLinus Torvalds 
139675899513SPeter E. Berger 	/*
139775899513SPeter E. Berger 	 * We have an ION device (I2c Must be programmed)
139875899513SPeter E. Berger 	 * Determine I2C image type
139975899513SPeter E. Berger 	 */
14002742fd88SAlan Cox 	if (i2c_type_bootmode(serial))
14012742fd88SAlan Cox 		goto stayinbootmode;
14021da177e4SLinus Torvalds 
14032742fd88SAlan Cox 	/* Check for ION Vendor ID and that the I2C is valid */
14042742fd88SAlan Cox 	if (!check_i2c_image(serial)) {
14051da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
14061da177e4SLinus Torvalds 		int i;
14071da177e4SLinus Torvalds 		__u8 cs = 0;
14081da177e4SLinus Torvalds 		__u8 *buffer;
14091da177e4SLinus Torvalds 		int buffer_size;
14101da177e4SLinus Torvalds 
141175899513SPeter E. Berger 		/*
141275899513SPeter E. Berger 		 * Validate Hardware version number
14131da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
14141da177e4SLinus Torvalds 		 */
14151da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
141610c642d0SJohan Hovold 		if (!ti_manuf_desc)
14171da177e4SLinus Torvalds 			return -ENOMEM;
141810c642d0SJohan Hovold 
14192742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
14201da177e4SLinus Torvalds 		if (status) {
14211da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14222742fd88SAlan Cox 			goto stayinbootmode;
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 
14252742fd88SAlan Cox 		/* Check for version 2 */
14262742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
142767e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
14282742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
14291da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14302742fd88SAlan Cox 			goto stayinbootmode;
14311da177e4SLinus Torvalds 		}
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 		/*
14362742fd88SAlan Cox 		 * In order to update the I2C firmware we must change the type
14372742fd88SAlan Cox 		 * 2 record to type 0xF2. This will force the UMP to come up
14382742fd88SAlan Cox 		 * in Boot Mode.  Then while in boot mode, the driver will
14392742fd88SAlan Cox 		 * download the latest firmware (padded to 15.5k) into the
14402742fd88SAlan Cox 		 * UMP ram. Finally when the device comes back up in download
14412742fd88SAlan Cox 		 * mode the driver will cause the new firmware to be copied
14422742fd88SAlan Cox 		 * from the UMP Ram to I2C and the firmware will update the
14432742fd88SAlan Cox 		 * record type from 0xf2 to 0x02.
14442742fd88SAlan Cox 		 *
14451da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14461da177e4SLinus Torvalds 		 * or could we do this in place!
14471da177e4SLinus Torvalds 		 */
14481da177e4SLinus Torvalds 
14492742fd88SAlan Cox 		/* Allocate a 15.5k buffer + 3 byte header */
14502742fd88SAlan Cox 		buffer_size = (((1024 * 16) - 512) +
14512742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header));
14521da177e4SLinus Torvalds 		buffer = kmalloc(buffer_size, GFP_KERNEL);
145310c642d0SJohan Hovold 		if (!buffer)
14541da177e4SLinus Torvalds 			return -ENOMEM;
14551da177e4SLinus Torvalds 
14562742fd88SAlan Cox 		/* Initialize the buffer to 0xff (pad the buffer) */
14571da177e4SLinus Torvalds 		memset(buffer, 0xff, buffer_size);
1458d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
14591da177e4SLinus Torvalds 
14602742fd88SAlan Cox 		for (i = sizeof(struct ti_i2c_image_header);
14612742fd88SAlan Cox 				i < buffer_size; i++) {
14621da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14661da177e4SLinus Torvalds 
14672742fd88SAlan Cox 		/* update length and checksum after padding */
14682742fd88SAlan Cox 		header->Length 	 = cpu_to_le16((__u16)(buffer_size -
14692742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header)));
14701da177e4SLinus Torvalds 		header->CheckSum = cs;
14711da177e4SLinus Torvalds 
14722742fd88SAlan Cox 		/* Download the operational code  */
1473c0e34831SPeter E. Berger 		dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1474c0e34831SPeter E. Berger 				__func__,
1475c0e34831SPeter E. Berger 				fw_hdr->major_version, fw_hdr->minor_version);
14762742fd88SAlan Cox 		status = download_code(serial, buffer, buffer_size);
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 		kfree(buffer);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 		if (status) {
148167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
14821da177e4SLinus Torvalds 			return status;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 
14852742fd88SAlan Cox 		/* Device will reboot */
14861da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14871da177e4SLinus Torvalds 
148867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
14891da177e4SLinus Torvalds 
1490e35d6d7cSJohan Hovold 		return 1;
14911da177e4SLinus Torvalds 	}
14921da177e4SLinus Torvalds 
14932742fd88SAlan Cox stayinbootmode:
14942742fd88SAlan Cox 	/* Eprom is invalid or blank stay in boot mode */
149567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
14961da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
14971da177e4SLinus Torvalds 
14984f9785ccSJohan Hovold 	return 1;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15012742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on)
15021da177e4SLinus Torvalds {
15031143832eSGreg Kroah-Hartman 	int port_number = port->port->port_number;
15041143832eSGreg Kroah-Hartman 
15052742fd88SAlan Cox 	on = !!on;	/* 1 or 0 not bitmask */
15062742fd88SAlan Cox 	return send_cmd(port->port->serial->dev,
15072742fd88SAlan Cox 			feature, (__u8)(UMPM_UART1_PORT + port_number),
15082742fd88SAlan Cox 			on, NULL, 0);
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15112742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	int status = 0;
15141da177e4SLinus Torvalds 
151567e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
15161da177e4SLinus Torvalds 
15172742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
15181da177e4SLinus Torvalds 	if (status)
15191da177e4SLinus Torvalds 		return status;
15202742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
15211da177e4SLinus Torvalds 	if (status)
15221da177e4SLinus Torvalds 		return status;
15232742fd88SAlan Cox 	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
15272742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	__u8 lsr = 0;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
15321da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
15331da177e4SLinus Torvalds 		lsr |= flagUart;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
15361da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
15371da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
15381da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
15392742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
15402742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds #undef MAP_FLAG
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	return lsr;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	struct async_icount *icount;
15501da177e4SLinus Torvalds 	struct tty_struct *tty;
15511da177e4SLinus Torvalds 
155267e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
15531da177e4SLinus Torvalds 
15542742fd88SAlan Cox 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
15552742fd88SAlan Cox 			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1556cf9a9d66SJohan Hovold 		icount = &edge_port->port->icount;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 		/* update input line counters */
15591da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
15601da177e4SLinus Torvalds 			icount->cts++;
15611da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
15621da177e4SLinus Torvalds 			icount->dsr++;
15631da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
15641da177e4SLinus Torvalds 			icount->dcd++;
15651da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
15661da177e4SLinus Torvalds 			icount->rng++;
156748ee5801SJohan Hovold 		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	/* Save the new modem status */
15711da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
15721da177e4SLinus Torvalds 
15734a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
15741da177e4SLinus Torvalds 	/* handle CTS flow control */
15751da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
1576d95e3caeSPeter Hurley 		if (msr & EDGEPORT_MSR_CTS)
15771da177e4SLinus Torvalds 			tty_wakeup(tty);
15781da177e4SLinus Torvalds 	}
15794a90f09bSAlan Cox 	tty_kref_put(tty);
15801da177e4SLinus Torvalds }
15811da177e4SLinus Torvalds 
15822742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
15832742fd88SAlan Cox 							__u8 lsr, __u8 data)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	struct async_icount *icount;
15862742fd88SAlan Cox 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
15872742fd88SAlan Cox 						LSR_FRM_ERR | LSR_BREAK));
15881da177e4SLinus Torvalds 
158967e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
15921da177e4SLinus Torvalds 
15932742fd88SAlan Cox 	if (new_lsr & LSR_BREAK)
15941da177e4SLinus Torvalds 		/*
15951da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
15961da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
15971da177e4SLinus Torvalds 		 */
15981da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
16012e124b4aSJiri Slaby 	if (lsr_data)
16022e124b4aSJiri Slaby 		edge_tty_recv(edge_port->port, &data, 1);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	/* update input line counters */
1605cf9a9d66SJohan Hovold 	icount = &edge_port->port->icount;
16061da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
16071da177e4SLinus Torvalds 		icount->brk++;
16081da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
16091da177e4SLinus Torvalds 		icount->overrun++;
16101da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
16111da177e4SLinus Torvalds 		icount->parity++;
16121da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
16131da177e4SLinus Torvalds 		icount->frame++;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
16167d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb)
16171da177e4SLinus Torvalds {
1618cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
16191da177e4SLinus Torvalds 	struct usb_serial_port *port;
16201da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
162167e6da70SGreg Kroah-Hartman 	struct device *dev;
16221da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
16231da177e4SLinus Torvalds 	int length = urb->actual_length;
16241da177e4SLinus Torvalds 	int port_number;
16251da177e4SLinus Torvalds 	int function;
1626ee337c21SGreg Kroah-Hartman 	int retval;
16271da177e4SLinus Torvalds 	__u8 lsr;
16281da177e4SLinus Torvalds 	__u8 msr;
1629ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16301da177e4SLinus Torvalds 
1631ee337c21SGreg Kroah-Hartman 	switch (status) {
16321da177e4SLinus Torvalds 	case 0:
16331da177e4SLinus Torvalds 		/* success */
16341da177e4SLinus Torvalds 		break;
16351da177e4SLinus Torvalds 	case -ECONNRESET:
16361da177e4SLinus Torvalds 	case -ENOENT:
16371da177e4SLinus Torvalds 	case -ESHUTDOWN:
16381da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
163967e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1640441b62c1SHarvey Harrison 		    __func__, status);
16411da177e4SLinus Torvalds 		return;
16421da177e4SLinus Torvalds 	default:
1643ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1644441b62c1SHarvey Harrison 			"%d\n", __func__, status);
16451da177e4SLinus Torvalds 		goto exit;
16461da177e4SLinus Torvalds 	}
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	if (!length) {
164967e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
16501da177e4SLinus Torvalds 		goto exit;
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds 
165367e6da70SGreg Kroah-Hartman 	dev = &edge_serial->serial->dev->dev;
165459d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, length, data);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	if (length != 2) {
165767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
16581da177e4SLinus Torvalds 		goto exit;
16591da177e4SLinus Torvalds 	}
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
16621da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
166367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
166467e6da70SGreg Kroah-Hartman 		port_number, function, data[1]);
16650b1d250aSJohan Hovold 
16660b1d250aSJohan Hovold 	if (port_number >= edge_serial->serial->num_ports) {
16670b1d250aSJohan Hovold 		dev_err(dev, "bad port number %d\n", port_number);
16680b1d250aSJohan Hovold 		goto exit;
16690b1d250aSJohan Hovold 	}
16700b1d250aSJohan Hovold 
16711da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
16721da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
16731da177e4SLinus Torvalds 	if (!edge_port) {
167467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - edge_port not found\n", __func__);
16751da177e4SLinus Torvalds 		return;
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds 	switch (function) {
16781da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
16792742fd88SAlan Cox 		lsr = map_line_status(data[1]);
16801da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
168175899513SPeter E. Berger 			/*
168275899513SPeter E. Berger 			 * Save the LSR event for bulk read completion routine
168375899513SPeter E. Berger 			 */
168467e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1685441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16861da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
16871da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
16881da177e4SLinus Torvalds 		} else {
168967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1690441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16911da177e4SLinus Torvalds 			handle_new_lsr(edge_port, 0, lsr, 0);
16921da177e4SLinus Torvalds 		}
16931da177e4SLinus Torvalds 		break;
16941da177e4SLinus Torvalds 
16952742fd88SAlan Cox 	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
16961da177e4SLinus Torvalds 		/* Copy MSR from UMP */
16971da177e4SLinus Torvalds 		msr = data[1];
169867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1699441b62c1SHarvey Harrison 			__func__, port_number, msr);
17001da177e4SLinus Torvalds 		handle_new_msr(edge_port, msr);
17011da177e4SLinus Torvalds 		break;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	default:
17042742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17052742fd88SAlan Cox 			"%s - Unknown Interrupt code from UMP %x\n",
1706441b62c1SHarvey Harrison 			__func__, data[1]);
17071da177e4SLinus Torvalds 		break;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	}
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds exit:
1712ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1713ee337c21SGreg Kroah-Hartman 	if (retval)
17142742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17152742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1716441b62c1SHarvey Harrison 			 __func__, retval);
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
17197d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb)
17201da177e4SLinus Torvalds {
1721cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
172267e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
17231da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17246778b0cbSJohn Ogness 	unsigned long flags;
1725ee337c21SGreg Kroah-Hartman 	int retval = 0;
17261da177e4SLinus Torvalds 	int port_number;
1727ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17281da177e4SLinus Torvalds 
1729ee337c21SGreg Kroah-Hartman 	switch (status) {
17301da177e4SLinus Torvalds 	case 0:
17311da177e4SLinus Torvalds 		/* success */
17321da177e4SLinus Torvalds 		break;
17331da177e4SLinus Torvalds 	case -ECONNRESET:
17341da177e4SLinus Torvalds 	case -ENOENT:
17351da177e4SLinus Torvalds 	case -ESHUTDOWN:
17361da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
173767e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
17381da177e4SLinus Torvalds 		return;
17391da177e4SLinus Torvalds 	default:
174067e6da70SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 
1743ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
17441da177e4SLinus Torvalds 		goto exit;
17451da177e4SLinus Torvalds 
1746ee337c21SGreg Kroah-Hartman 	if (status) {
1747441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
17481da177e4SLinus Torvalds 		return;
17491da177e4SLinus Torvalds 	}
17501da177e4SLinus Torvalds 
17511143832eSGreg Kroah-Hartman 	port_number = edge_port->port->port_number;
17521da177e4SLinus Torvalds 
1753654b404fSJohan Hovold 	if (urb->actual_length > 0 && edge_port->lsr_event) {
17541da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
175567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1756441b62c1SHarvey Harrison 			__func__, port_number, edge_port->lsr_mask, *data);
17571da177e4SLinus Torvalds 		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
17581da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
17591da177e4SLinus Torvalds 		--urb->actual_length;
17601da177e4SLinus Torvalds 		++data;
17611da177e4SLinus Torvalds 	}
17621da177e4SLinus Torvalds 
17632e124b4aSJiri Slaby 	if (urb->actual_length) {
176459d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
17652742fd88SAlan Cox 		if (edge_port->close_pending)
176667e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
17672742fd88SAlan Cox 								__func__);
17682742fd88SAlan Cox 		else
17692e124b4aSJiri Slaby 			edge_tty_recv(edge_port->port, data,
177005c7cd39SJiri Slaby 					urb->actual_length);
1771cf9a9d66SJohan Hovold 		edge_port->port->icount.rx += urb->actual_length;
17721da177e4SLinus Torvalds 	}
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds exit:
17751da177e4SLinus Torvalds 	/* continue read unless stopped */
17766778b0cbSJohn Ogness 	spin_lock_irqsave(&edge_port->ep_lock, flags);
17775833041fSJohan Hovold 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1778ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
17795833041fSJohan Hovold 	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
17801da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
17815833041fSJohan Hovold 
17826778b0cbSJohn Ogness 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1783ee337c21SGreg Kroah-Hartman 	if (retval)
178467e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17872e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
17882e124b4aSJiri Slaby 		int length)
17891da177e4SLinus Torvalds {
17902742fd88SAlan Cox 	int queued;
17911da177e4SLinus Torvalds 
179205c7cd39SJiri Slaby 	queued = tty_insert_flip_string(&port->port, data, length);
17932742fd88SAlan Cox 	if (queued < length)
179405c7cd39SJiri Slaby 		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
17952742fd88SAlan Cox 			__func__, length - queued);
17962e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17997d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb)
18001da177e4SLinus Torvalds {
1801cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
18021da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1803ee337c21SGreg Kroah-Hartman 	int status = urb->status;
18044a90f09bSAlan Cox 	struct tty_struct *tty;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
18071da177e4SLinus Torvalds 
1808ee337c21SGreg Kroah-Hartman 	switch (status) {
18091da177e4SLinus Torvalds 	case 0:
18101da177e4SLinus Torvalds 		/* success */
18111da177e4SLinus Torvalds 		break;
18121da177e4SLinus Torvalds 	case -ECONNRESET:
18131da177e4SLinus Torvalds 	case -ENOENT:
18141da177e4SLinus Torvalds 	case -ESHUTDOWN:
18151da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
181667e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1817441b62c1SHarvey Harrison 		    __func__, status);
18181da177e4SLinus Torvalds 		return;
18191da177e4SLinus Torvalds 	default:
182022a416c4SJohan Hovold 		dev_err_console(port, "%s - nonzero write bulk status "
1821441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	/* send any buffered data */
18254a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
1826ae3759c2SJiri Slaby 	edge_send(port, tty);
18274a90f09bSAlan Cox 	tty_kref_put(tty);
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
1830a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
18331da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
18341da177e4SLinus Torvalds 	struct usb_device *dev;
18351da177e4SLinus Torvalds 	struct urb *urb;
18361da177e4SLinus Torvalds 	int port_number;
18371da177e4SLinus Torvalds 	int status;
18381da177e4SLinus Torvalds 	u16 open_settings;
18391da177e4SLinus Torvalds 	u8 transaction_timeout;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (edge_port == NULL)
18421da177e4SLinus Torvalds 		return -ENODEV;
18431da177e4SLinus Torvalds 
18441143832eSGreg Kroah-Hartman 	port_number = port->port_number;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	dev = port->serial->dev;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	/* turn off loopback */
18492742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
18501da177e4SLinus Torvalds 	if (status) {
18512742fd88SAlan Cox 		dev_err(&port->dev,
18522742fd88SAlan Cox 				"%s - cannot send clear loopback command, %d\n",
1853441b62c1SHarvey Harrison 			__func__, status);
18541da177e4SLinus Torvalds 		return status;
18551da177e4SLinus Torvalds 	}
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	/* set up the port settings */
185895da310eSAlan Cox 	if (tty)
1859adc8d746SAlan Cox 		edge_set_termios(tty, port, &tty->termios);
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	/* open up the port */
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
18641da177e4SLinus Torvalds 	transaction_timeout = 2;
18651da177e4SLinus Torvalds 
18662742fd88SAlan Cox 	edge_port->ump_read_timeout =
18672742fd88SAlan Cox 				max(20, ((transaction_timeout * 3) / 2));
18681da177e4SLinus Torvalds 
18692742fd88SAlan Cox 	/* milliseconds to timeout for DMA transfer */
18701da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
18711da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
18721da177e4SLinus Torvalds 			     (transaction_timeout << 2));
18731da177e4SLinus Torvalds 
187467e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
18772742fd88SAlan Cox 	status = send_cmd(dev, UMPC_OPEN_PORT,
18782742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
18791da177e4SLinus Torvalds 	if (status) {
18802742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send open command, %d\n",
18812742fd88SAlan Cox 							__func__, status);
18821da177e4SLinus Torvalds 		return status;
18831da177e4SLinus Torvalds 	}
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	/* Start the DMA? */
18862742fd88SAlan Cox 	status = send_cmd(dev, UMPC_START_PORT,
18872742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
18881da177e4SLinus Torvalds 	if (status) {
18892742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
18902742fd88SAlan Cox 							__func__, status);
18911da177e4SLinus Torvalds 		return status;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
18952742fd88SAlan Cox 	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
18961da177e4SLinus Torvalds 	if (status) {
18972742fd88SAlan Cox 		dev_err(&port->dev,
18982742fd88SAlan Cox 			"%s - cannot send clear buffers command, %d\n",
18992742fd88SAlan Cox 			__func__, status);
19001da177e4SLinus Torvalds 		return status;
19011da177e4SLinus Torvalds 	}
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	/* Read Initial MSR */
19042742fd88SAlan Cox 	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
19052742fd88SAlan Cox 				(__u16)(UMPM_UART1_PORT + port_number),
19062742fd88SAlan Cox 				&edge_port->shadow_msr, 1);
19071da177e4SLinus Torvalds 	if (status) {
19082742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
19092742fd88SAlan Cox 							__func__, status);
19101da177e4SLinus Torvalds 		return status;
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 
191367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	/* Set Initial MCR */
19161da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
191767e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
1920241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
19211da177e4SLinus Torvalds 		return -ERESTARTSYS;
19221da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
19232742fd88SAlan Cox 		/* we are the first port to open, post the interrupt urb */
19241da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
19251da177e4SLinus Torvalds 		urb->context = edge_serial;
19261da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
19271da177e4SLinus Torvalds 		if (status) {
19282742fd88SAlan Cox 			dev_err(&port->dev,
19292742fd88SAlan Cox 				"%s - usb_submit_urb failed with value %d\n",
19302742fd88SAlan Cox 					__func__, status);
1931241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19321da177e4SLinus Torvalds 		}
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/*
19361da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
19371da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
19381da177e4SLinus Torvalds 	 */
19391da177e4SLinus Torvalds 	usb_clear_halt(dev, port->write_urb->pipe);
19401da177e4SLinus Torvalds 	usb_clear_halt(dev, port->read_urb->pipe);
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	/* start up our bulk read urb */
19431da177e4SLinus Torvalds 	urb = port->read_urb;
19441da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
19451da177e4SLinus Torvalds 	urb->context = edge_port;
19461da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_KERNEL);
19471da177e4SLinus Torvalds 	if (status) {
19482742fd88SAlan Cox 		dev_err(&port->dev,
19492742fd88SAlan Cox 			"%s - read bulk usb_submit_urb failed with value %d\n",
19502742fd88SAlan Cox 				__func__, status);
19511da177e4SLinus Torvalds 		goto unlink_int_urb;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
19551da177e4SLinus Torvalds 
1956241ca64fSMatthias Kaehlcke 	goto release_es_lock;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds unlink_int_urb:
19591da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
19601da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1961241ca64fSMatthias Kaehlcke release_es_lock:
1962241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
19631da177e4SLinus Torvalds 	return status;
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
1966335f8514SAlan Cox static void edge_close(struct usb_serial_port *port)
19671da177e4SLinus Torvalds {
19681da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19691da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
1970af581059SJohan Hovold 	struct usb_serial *serial = port->serial;
197177de2518SJohan Hovold 	unsigned long flags;
19721da177e4SLinus Torvalds 	int port_number;
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
19751da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
19762742fd88SAlan Cox 	if (edge_serial == NULL || edge_port == NULL)
19771da177e4SLinus Torvalds 		return;
19781da177e4SLinus Torvalds 
197975899513SPeter E. Berger 	/*
198075899513SPeter E. Berger 	 * The bulkreadcompletion routine will check
198175899513SPeter E. Berger 	 * this flag and dump add read data
198275899513SPeter E. Berger 	 */
19831da177e4SLinus Torvalds 	edge_port->close_pending = 1;
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
19861da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
19871da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
198877de2518SJohan Hovold 	spin_lock_irqsave(&edge_port->ep_lock, flags);
1989ddca16e4SJohan Hovold 	kfifo_reset_out(&port->write_fifo);
199077de2518SJohan Hovold 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19911da177e4SLinus Torvalds 
199267e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
19931143832eSGreg Kroah-Hartman 	port_number = port->port_number;
1994bca87e9eSJohan Hovold 	send_cmd(serial->dev, UMPC_CLOSE_PORT,
1995bca87e9eSJohan Hovold 		     (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1996af581059SJohan Hovold 
1997241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
19981da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
19991da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
20001da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
20011da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
20021da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
20031da177e4SLinus Torvalds 	}
2004241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
20051da177e4SLinus Torvalds 	edge_port->close_pending = 0;
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
200895da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
200995da310eSAlan Cox 				const unsigned char *data, int count)
20101da177e4SLinus Torvalds {
20111da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	if (count == 0) {
201467e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
20151da177e4SLinus Torvalds 		return 0;
20161da177e4SLinus Torvalds 	}
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	if (edge_port == NULL)
20191da177e4SLinus Torvalds 		return -ENODEV;
20201da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20211da177e4SLinus Torvalds 		return -ENODEV;
20221da177e4SLinus Torvalds 
2023ddca16e4SJohan Hovold 	count = kfifo_in_locked(&port->write_fifo, data, count,
2024d733cec1SJohan Hovold 							&edge_port->ep_lock);
2025ae3759c2SJiri Slaby 	edge_send(port, tty);
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	return count;
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
2030ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
20311da177e4SLinus Torvalds {
20321da177e4SLinus Torvalds 	int count, result;
20331da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20341da177e4SLinus Torvalds 	unsigned long flags;
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
20391da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20401da177e4SLinus Torvalds 		return;
20411da177e4SLinus Torvalds 	}
20421da177e4SLinus Torvalds 
2043ddca16e4SJohan Hovold 	count = kfifo_out(&port->write_fifo,
20441da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
20451da177e4SLinus Torvalds 				port->bulk_out_size);
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	if (count == 0) {
20481da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20491da177e4SLinus Torvalds 		return;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20551da177e4SLinus Torvalds 
205659d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	/* set up our urb */
2059fd11961aSJohan Hovold 	port->write_urb->transfer_buffer_length = count;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	/* send the data out the bulk port */
20621da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
20631da177e4SLinus Torvalds 	if (result) {
206422a416c4SJohan Hovold 		dev_err_console(port,
20652742fd88SAlan Cox 			"%s - failed submitting write urb, error %d\n",
20662742fd88SAlan Cox 				__func__, result);
20671da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
20682742fd88SAlan Cox 		/* TODO: reschedule edge_send */
20692742fd88SAlan Cox 	} else
2070cf9a9d66SJohan Hovold 		edge_port->port->icount.tx += count;
20711da177e4SLinus Torvalds 
207275899513SPeter E. Berger 	/*
207375899513SPeter E. Berger 	 * wakeup any process waiting for writes to complete
207475899513SPeter E. Berger 	 * there is now more room in the buffer for new writes
207575899513SPeter E. Berger 	 */
20762742fd88SAlan Cox 	if (tty)
20771da177e4SLinus Torvalds 		tty_wakeup(tty);
20781da177e4SLinus Torvalds }
20791da177e4SLinus Torvalds 
208095da310eSAlan Cox static int edge_write_room(struct tty_struct *tty)
20811da177e4SLinus Torvalds {
208295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20831da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20841da177e4SLinus Torvalds 	int room = 0;
20851da177e4SLinus Torvalds 	unsigned long flags;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	if (edge_port == NULL)
20882742fd88SAlan Cox 		return 0;
20891da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20902742fd88SAlan Cox 		return 0;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2093ddca16e4SJohan Hovold 	room = kfifo_avail(&port->write_fifo);
20941da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20951da177e4SLinus Torvalds 
209667e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
20971da177e4SLinus Torvalds 	return room;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
210095da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty)
21011da177e4SLinus Torvalds {
210295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21031da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21041da177e4SLinus Torvalds 	int chars = 0;
21051da177e4SLinus Torvalds 	unsigned long flags;
21061da177e4SLinus Torvalds 	if (edge_port == NULL)
21072742fd88SAlan Cox 		return 0;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2110ddca16e4SJohan Hovold 	chars = kfifo_len(&port->write_fifo);
21111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21121da177e4SLinus Torvalds 
211367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
21141da177e4SLinus Torvalds 	return chars;
21151da177e4SLinus Torvalds }
21161da177e4SLinus Torvalds 
2117b16634adSJohan Hovold static bool edge_tx_empty(struct usb_serial_port *port)
2118b16634adSJohan Hovold {
2119b16634adSJohan Hovold 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2120b16634adSJohan Hovold 	int ret;
2121b16634adSJohan Hovold 
2122b16634adSJohan Hovold 	ret = tx_active(edge_port);
2123b16634adSJohan Hovold 	if (ret > 0)
2124b16634adSJohan Hovold 		return false;
2125b16634adSJohan Hovold 
2126b16634adSJohan Hovold 	return true;
2127b16634adSJohan Hovold }
2128b16634adSJohan Hovold 
212995da310eSAlan Cox static void edge_throttle(struct tty_struct *tty)
21301da177e4SLinus Torvalds {
213195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21321da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21331da177e4SLinus Torvalds 	int status;
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 	if (edge_port == NULL)
21361da177e4SLinus Torvalds 		return;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
21391da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21401da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
214195da310eSAlan Cox 		status = edge_write(tty, port, &stop_char, 1);
214295da310eSAlan Cox 		if (status <= 0) {
214395da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
214495da310eSAlan Cox 		}
21451da177e4SLinus Torvalds 	}
21461da177e4SLinus Torvalds 
214775899513SPeter E. Berger 	/*
214875899513SPeter E. Berger 	 * if we are implementing RTS/CTS, stop reads
214975899513SPeter E. Berger 	 * and the Edgeport will clear the RTS line
215075899513SPeter E. Berger 	 */
21511da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
21521da177e4SLinus Torvalds 		stop_read(edge_port);
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds }
21551da177e4SLinus Torvalds 
215695da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty)
21571da177e4SLinus Torvalds {
215895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21591da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21601da177e4SLinus Torvalds 	int status;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	if (edge_port == NULL)
21631da177e4SLinus Torvalds 		return;
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
21661da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21671da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
216895da310eSAlan Cox 		status = edge_write(tty, port, &start_char, 1);
216995da310eSAlan Cox 		if (status <= 0) {
217095da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
217195da310eSAlan Cox 		}
21721da177e4SLinus Torvalds 	}
217375899513SPeter E. Berger 	/*
217475899513SPeter E. Berger 	 * if we are implementing RTS/CTS, restart reads
217575899513SPeter E. Berger 	 * are the Edgeport will assert the RTS line
217675899513SPeter E. Berger 	 */
21771da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
21781da177e4SLinus Torvalds 		status = restart_read(edge_port);
21791da177e4SLinus Torvalds 		if (status)
21802742fd88SAlan Cox 			dev_err(&port->dev,
21812742fd88SAlan Cox 				"%s - read bulk usb_submit_urb failed: %d\n",
21822742fd88SAlan Cox 							__func__, status);
21831da177e4SLinus Torvalds 	}
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
21881da177e4SLinus Torvalds {
21891da177e4SLinus Torvalds 	unsigned long flags;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
21941da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
21951da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
22011da177e4SLinus Torvalds {
22021da177e4SLinus Torvalds 	struct urb *urb;
22031da177e4SLinus Torvalds 	int status = 0;
22041da177e4SLinus Torvalds 	unsigned long flags;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
22091da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
2210efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
22111da177e4SLinus Torvalds 	}
22121da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
22131da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	return status;
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
222095da310eSAlan Cox static void change_port_settings(struct tty_struct *tty,
222195da310eSAlan Cox 		struct edgeport_port *edge_port, struct ktermios *old_termios)
22221da177e4SLinus Torvalds {
222367e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
22241da177e4SLinus Torvalds 	struct ump_uart_config *config;
22251da177e4SLinus Torvalds 	int baud;
22261da177e4SLinus Torvalds 	unsigned cflag;
22271da177e4SLinus Torvalds 	int status;
22281143832eSGreg Kroah-Hartman 	int port_number = edge_port->port->port_number;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
22311da177e4SLinus Torvalds 	if (!config) {
2232adc8d746SAlan Cox 		tty->termios = *old_termios;
22331da177e4SLinus Torvalds 		return;
22341da177e4SLinus Torvalds 	}
22351da177e4SLinus Torvalds 
2236adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	config->wFlags = 0;
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	/* These flags must be set */
22411da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
22421da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
22431da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
22461da177e4SLinus Torvalds 	case CS5:
22471da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR5BITS;
224867e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
22491da177e4SLinus Torvalds 		    break;
22501da177e4SLinus Torvalds 	case CS6:
22511da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR6BITS;
225267e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
22531da177e4SLinus Torvalds 		    break;
22541da177e4SLinus Torvalds 	case CS7:
22551da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR7BITS;
225667e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
22571da177e4SLinus Torvalds 		    break;
22581da177e4SLinus Torvalds 	default:
22591da177e4SLinus Torvalds 	case CS8:
22601da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR8BITS;
226167e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
22621da177e4SLinus Torvalds 			    break;
22631da177e4SLinus Torvalds 	}
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	if (cflag & PARENB) {
22661da177e4SLinus Torvalds 		if (cflag & PARODD) {
22671da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22681da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
226967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = odd\n", __func__);
22701da177e4SLinus Torvalds 		} else {
22711da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22721da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
227367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = even\n", __func__);
22741da177e4SLinus Torvalds 		}
22751da177e4SLinus Torvalds 	} else {
22761da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
227767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = none\n", __func__);
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
22811da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
228267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
22831da177e4SLinus Torvalds 	} else {
22841da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
228567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
22861da177e4SLinus Torvalds 	}
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	/* figure out the flow control settings */
22891da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
22901da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
22911da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
229267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
22931da177e4SLinus Torvalds 	} else {
229467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
22951da177e4SLinus Torvalds 		restart_read(edge_port);
22961da177e4SLinus Torvalds 	}
22971da177e4SLinus Torvalds 
229875899513SPeter E. Berger 	/*
229975899513SPeter E. Berger 	 * if we are implementing XON/XOFF, set the start and stop
230075899513SPeter E. Berger 	 * character in the device
230175899513SPeter E. Berger 	 */
23021da177e4SLinus Torvalds 	config->cXon  = START_CHAR(tty);
23031da177e4SLinus Torvalds 	config->cXoff = STOP_CHAR(tty);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	/* if we are implementing INBOUND XON/XOFF */
23061da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
23071da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
230867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2309441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
23102742fd88SAlan Cox 	} else
231167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	/* if we are implementing OUTBOUND XON/XOFF */
23141da177e4SLinus Torvalds 	if (I_IXON(tty)) {
23151da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
231667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2317441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
23182742fd88SAlan Cox 	} else
231967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
23201da177e4SLinus Torvalds 
2321adc8d746SAlan Cox 	tty->termios.c_cflag &= ~CMSPAR;
2322d5f5bcd4SAlan Cox 
23231da177e4SLinus Torvalds 	/* Round the baud rate */
23241da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
23251da177e4SLinus Torvalds 	if (!baud) {
23261da177e4SLinus Torvalds 		/* pick a default, any default... */
23271da177e4SLinus Torvalds 		baud = 9600;
23286aeb75e6SJohan Hovold 	} else {
23296aeb75e6SJohan Hovold 		/* Avoid a zero divisor. */
23306aeb75e6SJohan Hovold 		baud = min(baud, 461550);
2331d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
23326aeb75e6SJohan Hovold 	}
2333d5f5bcd4SAlan Cox 
23341da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
23351da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
23361da177e4SLinus Torvalds 
2337d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2338d5f5bcd4SAlan Cox 
233967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
23401da177e4SLinus Torvalds 
234167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate:   %d\n", (int)(461550L / config->wBaudRate));
234267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wFlags:    0x%x\n", config->wFlags);
234367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bDataBits:   %d\n", config->bDataBits);
234467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bParity:     %d\n", config->bParity);
234567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bStopBits:   %d\n", config->bStopBits);
234667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXon:        %d\n", config->cXon);
234767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXoff:       %d\n", config->cXoff);
234867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bUartMode:   %d\n", config->bUartMode);
23491da177e4SLinus Torvalds 
23501da177e4SLinus Torvalds 	/* move the word values into big endian mode */
23511da177e4SLinus Torvalds 	cpu_to_be16s(&config->wFlags);
23521da177e4SLinus Torvalds 	cpu_to_be16s(&config->wBaudRate);
23531da177e4SLinus Torvalds 
23542742fd88SAlan Cox 	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
23551da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
2356*e8d89db0SJohan Hovold 				0, config, sizeof(*config));
23572742fd88SAlan Cox 	if (status)
235867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2359441b62c1SHarvey Harrison 			__func__, status);
23601da177e4SLinus Torvalds 	kfree(config);
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
236395da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
236495da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	if (edge_port == NULL)
23691da177e4SLinus Torvalds 		return;
23701da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
237195da310eSAlan Cox 	change_port_settings(tty, edge_port, old_termios);
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
237420b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty,
23752742fd88SAlan Cox 					unsigned int set, unsigned int clear)
23761da177e4SLinus Torvalds {
237795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23781da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23791da177e4SLinus Torvalds 	unsigned int mcr;
23803d71fe0bSAlan Cox 	unsigned long flags;
23811da177e4SLinus Torvalds 
23823d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23831da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
23841da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
23851da177e4SLinus Torvalds 		mcr |= MCR_RTS;
23861da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
23871da177e4SLinus Torvalds 		mcr |= MCR_DTR;
23881da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
23891da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
23921da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
23931da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
23941da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
23951da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
23961da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
23993d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
24001da177e4SLinus Torvalds 
24012742fd88SAlan Cox 	restore_mcr(edge_port, mcr);
24021da177e4SLinus Torvalds 	return 0;
24031da177e4SLinus Torvalds }
24041da177e4SLinus Torvalds 
240560b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty)
24061da177e4SLinus Torvalds {
240795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24081da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24091da177e4SLinus Torvalds 	unsigned int result = 0;
24101da177e4SLinus Torvalds 	unsigned int msr;
24111da177e4SLinus Torvalds 	unsigned int mcr;
24123d71fe0bSAlan Cox 	unsigned long flags;
24131da177e4SLinus Torvalds 
24143d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
24153d71fe0bSAlan Cox 
24161da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
24171da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
24181da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
24191da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
24201da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
24211da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
24221da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
24231da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
24241da177e4SLinus Torvalds 
24251da177e4SLinus Torvalds 
242667e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
24273d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	return result;
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
243295da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state)
24331da177e4SLinus Torvalds {
243495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24351da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24361da177e4SLinus Torvalds 	int status;
24372742fd88SAlan Cox 	int bv = 0;	/* Off */
24381da177e4SLinus Torvalds 
243995da310eSAlan Cox 	if (break_state == -1)
24402742fd88SAlan Cox 		bv = 1;	/* On */
24412742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
24422742fd88SAlan Cox 	if (status)
244367e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2444441b62c1SHarvey Harrison 			__func__, status);
24451da177e4SLinus Torvalds }
24461da177e4SLinus Torvalds 
244726c78daaSPeter E. Berger static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial)
244826c78daaSPeter E. Berger {
244926c78daaSPeter E. Berger 	if (!edge_serial->use_heartbeat)
245026c78daaSPeter E. Berger 		return;
245126c78daaSPeter E. Berger 
245226c78daaSPeter E. Berger 	schedule_delayed_work(&edge_serial->heartbeat_work,
245326c78daaSPeter E. Berger 			FW_HEARTBEAT_SECS * HZ);
245426c78daaSPeter E. Berger }
245526c78daaSPeter E. Berger 
245626c78daaSPeter E. Berger static void edge_heartbeat_work(struct work_struct *work)
245726c78daaSPeter E. Berger {
245826c78daaSPeter E. Berger 	struct edgeport_serial *serial;
245926c78daaSPeter E. Berger 	struct ti_i2c_desc *rom_desc;
246026c78daaSPeter E. Berger 
246126c78daaSPeter E. Berger 	serial = container_of(work, struct edgeport_serial,
246226c78daaSPeter E. Berger 			heartbeat_work.work);
246326c78daaSPeter E. Berger 
246426c78daaSPeter E. Berger 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
246526c78daaSPeter E. Berger 
246626c78daaSPeter E. Berger 	/* Descriptor address request is enough to reset the firmware timer */
246726c78daaSPeter E. Berger 	if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
246826c78daaSPeter E. Berger 			rom_desc)) {
246926c78daaSPeter E. Berger 		dev_err(&serial->serial->interface->dev,
247026c78daaSPeter E. Berger 				"%s - Incomplete heartbeat\n", __func__);
247126c78daaSPeter E. Berger 	}
247226c78daaSPeter E. Berger 	kfree(rom_desc);
247326c78daaSPeter E. Berger 
247426c78daaSPeter E. Berger 	edge_heartbeat_schedule(serial);
247526c78daaSPeter E. Berger }
247626c78daaSPeter E. Berger 
247749f4ff2dSJohan Hovold static int edge_calc_num_ports(struct usb_serial *serial,
247849f4ff2dSJohan Hovold 				struct usb_serial_endpoints *epds)
247949f4ff2dSJohan Hovold {
248049f4ff2dSJohan Hovold 	struct device *dev = &serial->interface->dev;
248149f4ff2dSJohan Hovold 	unsigned char num_ports = serial->type->num_ports;
248249f4ff2dSJohan Hovold 
248349f4ff2dSJohan Hovold 	/* Make sure we have the required endpoints when in download mode. */
248449f4ff2dSJohan Hovold 	if (serial->interface->cur_altsetting->desc.bNumEndpoints > 1) {
248549f4ff2dSJohan Hovold 		if (epds->num_bulk_in < num_ports ||
24869c8299b4SJohan Hovold 				epds->num_bulk_out < num_ports ||
24879c8299b4SJohan Hovold 				epds->num_interrupt_in < 1) {
248849f4ff2dSJohan Hovold 			dev_err(dev, "required endpoints missing\n");
248949f4ff2dSJohan Hovold 			return -ENODEV;
249049f4ff2dSJohan Hovold 		}
249149f4ff2dSJohan Hovold 	}
249249f4ff2dSJohan Hovold 
249349f4ff2dSJohan Hovold 	return num_ports;
249449f4ff2dSJohan Hovold }
249549f4ff2dSJohan Hovold 
24961da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial)
24971da177e4SLinus Torvalds {
24981da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
24991da177e4SLinus Torvalds 	int status;
250026c78daaSPeter E. Berger 	u16 product_id;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	/* create our private serial structure */
250380b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
250410c642d0SJohan Hovold 	if (!edge_serial)
25051da177e4SLinus Torvalds 		return -ENOMEM;
250610c642d0SJohan Hovold 
2507241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
25081da177e4SLinus Torvalds 	edge_serial->serial = serial;
25094f9785ccSJohan Hovold 	INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work);
25101da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
25111da177e4SLinus Torvalds 
2512b4146162SPeter E. Berger 	status = download_fw(edge_serial);
25134f9785ccSJohan Hovold 	if (status < 0) {
25141da177e4SLinus Torvalds 		kfree(edge_serial);
25151da177e4SLinus Torvalds 		return status;
25161da177e4SLinus Torvalds 	}
25171da177e4SLinus Torvalds 
25184f9785ccSJohan Hovold 	if (status > 0)
25194f9785ccSJohan Hovold 		return 1;	/* bind but do not register any ports */
25204f9785ccSJohan Hovold 
252126c78daaSPeter E. Berger 	product_id = le16_to_cpu(
252226c78daaSPeter E. Berger 			edge_serial->serial->dev->descriptor.idProduct);
252326c78daaSPeter E. Berger 
252426c78daaSPeter E. Berger 	/* Currently only the EP/416 models require heartbeat support */
252526c78daaSPeter E. Berger 	if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) {
252626c78daaSPeter E. Berger 		if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 ||
252726c78daaSPeter E. Berger 			product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) {
252826c78daaSPeter E. Berger 			edge_serial->use_heartbeat = true;
252926c78daaSPeter E. Berger 		}
253026c78daaSPeter E. Berger 	}
253126c78daaSPeter E. Berger 
253226c78daaSPeter E. Berger 	edge_heartbeat_schedule(edge_serial);
253326c78daaSPeter E. Berger 
25341da177e4SLinus Torvalds 	return 0;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
2537f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial)
25381da177e4SLinus Torvalds {
25392330d0a8SJohan Hovold 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
25402330d0a8SJohan Hovold 
25412330d0a8SJohan Hovold 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2542f9c99bb8SAlan Stern }
2543f9c99bb8SAlan Stern 
2544f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial)
2545f9c99bb8SAlan Stern {
254626c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
254726c78daaSPeter E. Berger 
254826c78daaSPeter E. Berger 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
254926c78daaSPeter E. Berger 	kfree(edge_serial);
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
255200361530SJohan Hovold static int edge_port_probe(struct usb_serial_port *port)
255300361530SJohan Hovold {
255400361530SJohan Hovold 	struct edgeport_port *edge_port;
255500361530SJohan Hovold 	int ret;
255600361530SJohan Hovold 
255700361530SJohan Hovold 	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
255800361530SJohan Hovold 	if (!edge_port)
255900361530SJohan Hovold 		return -ENOMEM;
256000361530SJohan Hovold 
256100361530SJohan Hovold 	spin_lock_init(&edge_port->ep_lock);
256200361530SJohan Hovold 	edge_port->port = port;
256300361530SJohan Hovold 	edge_port->edge_serial = usb_get_serial_data(port->serial);
256400361530SJohan Hovold 	edge_port->bUartMode = default_uart_mode;
256500361530SJohan Hovold 
25660fce06daSJohan Hovold 	switch (port->port_number) {
25670fce06daSJohan Hovold 	case 0:
25680fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART1;
25690fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
25700fce06daSJohan Hovold 		break;
25710fce06daSJohan Hovold 	case 1:
25720fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART2;
25730fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
25740fce06daSJohan Hovold 		break;
25750fce06daSJohan Hovold 	default:
25760fce06daSJohan Hovold 		dev_err(&port->dev, "unknown port number\n");
25770fce06daSJohan Hovold 		ret = -ENODEV;
25780fce06daSJohan Hovold 		goto err;
25790fce06daSJohan Hovold 	}
25800fce06daSJohan Hovold 
25810fce06daSJohan Hovold 	dev_dbg(&port->dev,
25820fce06daSJohan Hovold 		"%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
25830fce06daSJohan Hovold 		__func__, port->port_number, edge_port->uart_base,
25840fce06daSJohan Hovold 		edge_port->dma_address);
25850fce06daSJohan Hovold 
258600361530SJohan Hovold 	usb_set_serial_port_data(port, edge_port);
258700361530SJohan Hovold 
25885d8c61bcSJohan Hovold 	ret = edge_create_sysfs_attrs(port);
25890fce06daSJohan Hovold 	if (ret)
25900fce06daSJohan Hovold 		goto err;
25915d8c61bcSJohan Hovold 
25924b8e0795SJohan Hovold 	/*
25934b8e0795SJohan Hovold 	 * The LSR does not tell when the transmitter shift register has
25944b8e0795SJohan Hovold 	 * emptied so add a one-character drain delay.
25954b8e0795SJohan Hovold 	 */
2596d7be6221SJohan Hovold 	port->port.drain_delay = 1;
2597f40d7815SJohan Hovold 
259800361530SJohan Hovold 	return 0;
25990fce06daSJohan Hovold err:
26000fce06daSJohan Hovold 	kfree(edge_port);
26010fce06daSJohan Hovold 
26020fce06daSJohan Hovold 	return ret;
260300361530SJohan Hovold }
260400361530SJohan Hovold 
2605c5d1448fSUwe Kleine-König static void edge_port_remove(struct usb_serial_port *port)
260600361530SJohan Hovold {
260700361530SJohan Hovold 	struct edgeport_port *edge_port;
260800361530SJohan Hovold 
260900361530SJohan Hovold 	edge_port = usb_get_serial_port_data(port);
261000361530SJohan Hovold 	edge_remove_sysfs_attrs(port);
261100361530SJohan Hovold 	kfree(edge_port);
261200361530SJohan Hovold }
26131da177e4SLinus Torvalds 
2614fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2615fc4cbd75SMartin K. Petersen 
2616154547c4SGreg Kroah-Hartman static ssize_t uart_mode_show(struct device *dev,
2617fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2618fc4cbd75SMartin K. Petersen {
2619fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2620fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2621fc4cbd75SMartin K. Petersen 
2622fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2623fc4cbd75SMartin K. Petersen }
2624fc4cbd75SMartin K. Petersen 
2625154547c4SGreg Kroah-Hartman static ssize_t uart_mode_store(struct device *dev,
2626fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2627fc4cbd75SMartin K. Petersen {
2628fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2629fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2630fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2631fc4cbd75SMartin K. Petersen 
263267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2633fc4cbd75SMartin K. Petersen 
2634fc4cbd75SMartin K. Petersen 	if (v < 256)
2635fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2636fc4cbd75SMartin K. Petersen 	else
2637441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2638fc4cbd75SMartin K. Petersen 
2639fc4cbd75SMartin K. Petersen 	return count;
2640fc4cbd75SMartin K. Petersen }
2641154547c4SGreg Kroah-Hartman static DEVICE_ATTR_RW(uart_mode);
2642fc4cbd75SMartin K. Petersen 
2643fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2644fc4cbd75SMartin K. Petersen {
2645fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2646fc4cbd75SMartin K. Petersen }
2647fc4cbd75SMartin K. Petersen 
2648fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2649fc4cbd75SMartin K. Petersen {
2650fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2651fc4cbd75SMartin K. Petersen 	return 0;
2652fc4cbd75SMartin K. Petersen }
2653fc4cbd75SMartin K. Petersen 
265426c78daaSPeter E. Berger #ifdef CONFIG_PM
265526c78daaSPeter E. Berger static int edge_suspend(struct usb_serial *serial, pm_message_t message)
265626c78daaSPeter E. Berger {
265726c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
265826c78daaSPeter E. Berger 
265926c78daaSPeter E. Berger 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
266026c78daaSPeter E. Berger 
266126c78daaSPeter E. Berger 	return 0;
266226c78daaSPeter E. Berger }
266326c78daaSPeter E. Berger 
266426c78daaSPeter E. Berger static int edge_resume(struct usb_serial *serial)
266526c78daaSPeter E. Berger {
266626c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
266726c78daaSPeter E. Berger 
266826c78daaSPeter E. Berger 	edge_heartbeat_schedule(edge_serial);
266926c78daaSPeter E. Berger 
267026c78daaSPeter E. Berger 	return 0;
267126c78daaSPeter E. Berger }
267226c78daaSPeter E. Berger #endif
2673fc4cbd75SMartin K. Petersen 
2674ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
267518fcac35SGreg Kroah-Hartman 	.driver = {
26761da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2677269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
267818fcac35SGreg Kroah-Hartman 	},
2679269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
26801da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
26811da177e4SLinus Torvalds 	.num_ports		= 1,
26828d9c4d9eSJohan Hovold 	.num_bulk_out		= 1,
26831da177e4SLinus Torvalds 	.open			= edge_open,
26841da177e4SLinus Torvalds 	.close			= edge_close,
26851da177e4SLinus Torvalds 	.throttle		= edge_throttle,
26861da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
26871da177e4SLinus Torvalds 	.attach			= edge_startup,
268849f4ff2dSJohan Hovold 	.calc_num_ports		= edge_calc_num_ports,
2689f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2690f9c99bb8SAlan Stern 	.release		= edge_release,
269100361530SJohan Hovold 	.port_probe		= edge_port_probe,
269200361530SJohan Hovold 	.port_remove		= edge_port_remove,
26931da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
26941da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
26951da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
269648ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2697cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
26981da177e4SLinus Torvalds 	.write			= edge_write,
26991da177e4SLinus Torvalds 	.write_room		= edge_write_room,
27001da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2701b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
27021da177e4SLinus Torvalds 	.break_ctl		= edge_break,
27031da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
27041da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
27051da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
270626c78daaSPeter E. Berger #ifdef CONFIG_PM
270726c78daaSPeter E. Berger 	.suspend		= edge_suspend,
270826c78daaSPeter E. Berger 	.resume			= edge_resume,
270926c78daaSPeter E. Berger #endif
27101da177e4SLinus Torvalds };
27111da177e4SLinus Torvalds 
2712ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
271318fcac35SGreg Kroah-Hartman 	.driver = {
27141da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2715269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
271618fcac35SGreg Kroah-Hartman 	},
2717269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
27181da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
27191da177e4SLinus Torvalds 	.num_ports		= 2,
27208d9c4d9eSJohan Hovold 	.num_bulk_out		= 1,
27211da177e4SLinus Torvalds 	.open			= edge_open,
27221da177e4SLinus Torvalds 	.close			= edge_close,
27231da177e4SLinus Torvalds 	.throttle		= edge_throttle,
27241da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
27251da177e4SLinus Torvalds 	.attach			= edge_startup,
272649f4ff2dSJohan Hovold 	.calc_num_ports		= edge_calc_num_ports,
2727f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2728f9c99bb8SAlan Stern 	.release		= edge_release,
272900361530SJohan Hovold 	.port_probe		= edge_port_probe,
273000361530SJohan Hovold 	.port_remove		= edge_port_remove,
27311da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
27321da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
27331da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
273448ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2735cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
27361da177e4SLinus Torvalds 	.write			= edge_write,
27371da177e4SLinus Torvalds 	.write_room		= edge_write_room,
27381da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2739b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
27401da177e4SLinus Torvalds 	.break_ctl		= edge_break,
27411da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
27421da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
27431da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
274426c78daaSPeter E. Berger #ifdef CONFIG_PM
274526c78daaSPeter E. Berger 	.suspend		= edge_suspend,
274626c78daaSPeter E. Berger 	.resume			= edge_resume,
274726c78daaSPeter E. Berger #endif
27481da177e4SLinus Torvalds };
27491da177e4SLinus Torvalds 
27507dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
27517dbe2460SAlan Stern 	&edgeport_1port_device, &edgeport_2port_device, NULL
27527dbe2460SAlan Stern };
27537dbe2460SAlan Stern 
275468e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
27571da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
27581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2759d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
27622742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev,
27632742fd88SAlan Cox 			"Ignore the cpu revision when connecting to a device");
27641da177e4SLinus Torvalds 
2765fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2766fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
2767