xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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  */
6735aeb1b3SJohan Hovold 	u8	hardware_type;		/* Type of hardware */
6846388e86SJohan Hovold } __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 {
9035aeb1b3SJohan Hovold 	u16 uart_base;
9135aeb1b3SJohan Hovold 	u16 dma_address;
9235aeb1b3SJohan Hovold 	u8 shadow_msr;
9335aeb1b3SJohan Hovold 	u8 shadow_mcr;
9435aeb1b3SJohan Hovold 	u8 shadow_lsr;
9535aeb1b3SJohan Hovold 	u8 lsr_mask;
9635aeb1b3SJohan Hovold 	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;
10735aeb1b3SJohan Hovold 	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) },
169908e698fSRobert Eckelmann 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) },
1701da177e4SLinus Torvalds 	{ }
1711da177e4SLinus Torvalds };
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /* Devices that this driver supports */
1747d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
1751da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1761da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1781da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1791da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1801da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1811da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1821da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1831da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1841da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1851da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1861da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1911da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1921da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1931da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1941da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1951da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1961da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1971da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1981da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1991da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
2001da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
2011da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
2021da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
2031da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
204fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
205fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
206fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
207fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
208908e698fSRobert Eckelmann 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_E5805A) },
2091da177e4SLinus Torvalds 	{ }
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
2131da177e4SLinus Torvalds 
21490ab5ee9SRusty Russell static bool ignore_cpu_rev;
2152742fd88SAlan Cox static int default_uart_mode;		/* RS232 */
2161da177e4SLinus Torvalds 
2172e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2182e124b4aSJiri Slaby 		int length);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2211da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2221da177e4SLinus Torvalds 
22395da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
224f6d47fe5SIlpo Järvinen 			     struct usb_serial_port *port,
225f6d47fe5SIlpo Järvinen 			     const struct ktermios *old_termios);
226ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
2271da177e4SLinus Torvalds 
228bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial,
229bebf1f18SPeter E. Berger 		const struct firmware *fw);
230bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial,
231bebf1f18SPeter E. Berger 		const struct firmware *fw);
232bebf1f18SPeter E. Berger 
233fc4cbd75SMartin K. Petersen /* sysfs attributes */
234fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
235fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
236fc4cbd75SMartin K. Petersen 
23726c78daaSPeter E. Berger /*
23826c78daaSPeter E. Berger  * Some release of Edgeport firmware "down3.bin" after version 4.80
23926c78daaSPeter E. Berger  * introduced code to automatically disconnect idle devices on some
24026c78daaSPeter E. Berger  * Edgeport models after periods of inactivity, typically ~60 seconds.
24126c78daaSPeter E. Berger  * This occurs without regard to whether ports on the device are open
24226c78daaSPeter E. Berger  * or not.  Digi International Tech Support suggested:
24326c78daaSPeter E. Berger  *
24426c78daaSPeter E. Berger  * 1.  Adding driver "heartbeat" code to reset the firmware timer by
24526c78daaSPeter E. Berger  *     requesting a descriptor record every 15 seconds, which should be
24626c78daaSPeter E. Berger  *     effective with newer firmware versions that require it, and benign
24726c78daaSPeter E. Berger  *     with older versions that do not. In practice 40 seconds seems often
24826c78daaSPeter E. Berger  *     enough.
24926c78daaSPeter E. Berger  * 2.  The heartbeat code is currently required only on Edgeport/416 models.
25026c78daaSPeter E. Berger  */
25126c78daaSPeter E. Berger #define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80)
25226c78daaSPeter E. Berger #define FW_HEARTBEAT_SECS 40
2531da177e4SLinus Torvalds 
254c3ece7e7SPeter E. Berger /* Timeouts in msecs: firmware downloads take longer */
255c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_DEFAULT 1000
256c3ece7e7SPeter E. Berger #define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
257c3ece7e7SPeter E. Berger 
ti_vread_sync(struct usb_device * dev,u8 request,u16 value,u16 index,void * data,int size)258e8d89db0SJohan Hovold static int ti_vread_sync(struct usb_device *dev, u8 request, u16 value,
259e8d89db0SJohan Hovold 		u16 index, void *data, int size)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	int status;
2621da177e4SLinus Torvalds 
2632742fd88SAlan Cox 	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
2642742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
2652742fd88SAlan Cox 			value, index, data, size, 1000);
2661da177e4SLinus Torvalds 	if (status < 0)
2671da177e4SLinus Torvalds 		return status;
2681da177e4SLinus Torvalds 	if (status != size) {
2690765590fSJohan Hovold 		dev_dbg(&dev->dev, "%s - wanted to read %d, but only read %d\n",
270441b62c1SHarvey Harrison 			__func__, size, status);
2711da177e4SLinus Torvalds 		return -ECOMM;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 	return 0;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
ti_vsend_sync(struct usb_device * dev,u8 request,u16 value,u16 index,void * data,int size,int timeout)276c3ece7e7SPeter E. Berger static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
277e8d89db0SJohan Hovold 		u16 index, void *data, int size, int timeout)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	int status;
2801da177e4SLinus Torvalds 
2812742fd88SAlan Cox 	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
2822742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
283c3ece7e7SPeter E. Berger 			value, index, data, size, timeout);
2841da177e4SLinus Torvalds 	if (status < 0)
2851da177e4SLinus Torvalds 		return status;
28666db9478SJohan Hovold 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
read_port_cmd(struct usb_serial_port * port,u8 command,u16 value,void * data,int size)29013c61339SJohan Hovold static int read_port_cmd(struct usb_serial_port *port, u8 command, u16 value,
29113c61339SJohan Hovold 		void *data, int size)
29213c61339SJohan Hovold {
29313c61339SJohan Hovold 	return ti_vread_sync(port->serial->dev, command, value,
29413c61339SJohan Hovold 			UMPM_UART1_PORT + port->port_number,
29513c61339SJohan Hovold 			data, size);
29613c61339SJohan Hovold }
29713c61339SJohan Hovold 
send_port_cmd(struct usb_serial_port * port,u8 command,u16 value,void * data,int size)2987a14fac0SJohan Hovold static int send_port_cmd(struct usb_serial_port *port, u8 command, u16 value,
2997a14fac0SJohan Hovold 		void *data, int size)
3001da177e4SLinus Torvalds {
3017a14fac0SJohan Hovold 	return ti_vsend_sync(port->serial->dev, command, value,
3027a14fac0SJohan Hovold 			UMPM_UART1_PORT + port->port_number,
3037a14fac0SJohan Hovold 			data, size, TI_VSEND_TIMEOUT_DEFAULT);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
purge_port(struct usb_serial_port * port,u16 mask)30735aeb1b3SJohan Hovold static int purge_port(struct usb_serial_port *port, u16 mask)
3081da177e4SLinus Torvalds {
3091143832eSGreg Kroah-Hartman 	int port_number = port->port_number;
3101da177e4SLinus Torvalds 
31167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
3121da177e4SLinus Torvalds 
3137a14fac0SJohan Hovold 	return send_port_cmd(port, UMPC_PURGE_PORT, mask, NULL, 0);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds /**
3172742fd88SAlan Cox  * read_download_mem - Read edgeport memory from TI chip
3181da177e4SLinus Torvalds  * @dev: usb device pointer
3191da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3201da177e4SLinus Torvalds  * @length: Length of above data
3211da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3221da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3231da177e4SLinus Torvalds  */
read_download_mem(struct usb_device * dev,int start_address,int length,u8 address_type,u8 * buffer)3242742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address,
32535aeb1b3SJohan Hovold 				int length, u8 address_type, u8 *buffer)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	int status = 0;
32835aeb1b3SJohan Hovold 	u8 read_length;
3295509076dSJohan Hovold 	u16 be_start_address;
3301da177e4SLinus Torvalds 
33167e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
3321da177e4SLinus Torvalds 
33375899513SPeter E. Berger 	/*
33475899513SPeter E. Berger 	 * Read in blocks of 64 bytes
3351da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3361da177e4SLinus Torvalds 	 */
3371da177e4SLinus Torvalds 	while (length) {
3381da177e4SLinus Torvalds 		if (length > 64)
3391da177e4SLinus Torvalds 			read_length = 64;
3401da177e4SLinus Torvalds 		else
34135aeb1b3SJohan Hovold 			read_length = (u8)length;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		if (read_length > 1) {
34467e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
3451da177e4SLinus Torvalds 		}
3465509076dSJohan Hovold 		/*
3475509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian
3485509076dSJohan Hovold 		 *       byte order regardless of host byte order.
3495509076dSJohan Hovold 		 */
3505509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
3512742fd88SAlan Cox 		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
35235aeb1b3SJohan Hovold 					(u16)address_type,
3535509076dSJohan Hovold 					be_start_address,
3542742fd88SAlan Cox 					buffer, read_length);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		if (status) {
35767e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
3581da177e4SLinus Torvalds 			return status;
3591da177e4SLinus Torvalds 		}
3601da177e4SLinus Torvalds 
3612742fd88SAlan Cox 		if (read_length > 1)
36259d33f2fSGreg Kroah-Hartman 			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 		/* Update pointers/length */
3651da177e4SLinus Torvalds 		start_address += read_length;
3661da177e4SLinus Torvalds 		buffer += read_length;
3671da177e4SLinus Torvalds 		length -= read_length;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	return status;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
read_ram(struct usb_device * dev,int start_address,int length,u8 * buffer)3732742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address,
37435aeb1b3SJohan Hovold 						int length, u8 *buffer)
3751da177e4SLinus Torvalds {
3762742fd88SAlan Cox 	return read_download_mem(dev, start_address, length,
3772742fd88SAlan Cox 					DTK_ADDR_SPACE_XDATA, buffer);
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /* Read edgeport memory to a given block */
read_boot_mem(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)3812742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial,
38235aeb1b3SJohan Hovold 				int start_address, int length, u8 *buffer)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	int status = 0;
3851da177e4SLinus Torvalds 	int i;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	for (i = 0; i < length; i++) {
3882742fd88SAlan Cox 		status = ti_vread_sync(serial->serial->dev,
3892742fd88SAlan Cox 				UMPC_MEMORY_READ, serial->TI_I2C_Type,
39035aeb1b3SJohan Hovold 				(u16)(start_address+i), &buffer[i], 0x01);
3911da177e4SLinus Torvalds 		if (status) {
39267e6da70SGreg Kroah-Hartman 			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
3931da177e4SLinus Torvalds 			return status;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
39767e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
3982742fd88SAlan Cox 		__func__, start_address, length);
39959d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	return status;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
write_boot_mem(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)4072742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial,
40835aeb1b3SJohan Hovold 				int start_address, int length, u8 *buffer)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	int status = 0;
4111da177e4SLinus Torvalds 	int i;
412e9305d2fSJohan Hovold 	u8 *temp;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* Must do a read before write */
4151da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
416e9305d2fSJohan Hovold 		temp = kmalloc(1, GFP_KERNEL);
41710c642d0SJohan Hovold 		if (!temp)
418e9305d2fSJohan Hovold 			return -ENOMEM;
41910c642d0SJohan Hovold 
420e9305d2fSJohan Hovold 		status = read_boot_mem(serial, 0, 1, temp);
421e9305d2fSJohan Hovold 		kfree(temp);
4221da177e4SLinus Torvalds 		if (status)
4231da177e4SLinus Torvalds 			return status;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	for (i = 0; i < length; ++i) {
427c3ece7e7SPeter E. Berger 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
428c3ece7e7SPeter E. Berger 				buffer[i], (u16)(i + start_address), NULL,
429c3ece7e7SPeter E. Berger 				0, TI_VSEND_TIMEOUT_DEFAULT);
4301da177e4SLinus Torvalds 		if (status)
4311da177e4SLinus Torvalds 			return status;
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 
43467e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
43559d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	return status;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
write_i2c_mem(struct edgeport_serial * serial,int start_address,int length,u8 address_type,u8 * buffer)4412742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial,
44235aeb1b3SJohan Hovold 		int start_address, int length, u8 address_type, u8 *buffer)
4431da177e4SLinus Torvalds {
44467e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
4451da177e4SLinus Torvalds 	int status = 0;
4461da177e4SLinus Torvalds 	int write_length;
4475509076dSJohan Hovold 	u16 be_start_address;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4501da177e4SLinus Torvalds 
45125985edcSLucas De Marchi 	/* calculate the number of bytes left in the first page */
4522742fd88SAlan Cox 	write_length = EPROM_PAGE_SIZE -
4532742fd88SAlan Cox 				(start_address & (EPROM_PAGE_SIZE - 1));
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (write_length > length)
4561da177e4SLinus Torvalds 		write_length = length;
4571da177e4SLinus Torvalds 
45867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
4592742fd88SAlan Cox 		__func__, start_address, write_length);
46059d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, write_length, buffer);
4611da177e4SLinus Torvalds 
4625509076dSJohan Hovold 	/*
4635509076dSJohan Hovold 	 * Write first page.
4645509076dSJohan Hovold 	 *
4655509076dSJohan Hovold 	 * NOTE: Must use swab as wIndex is sent in little-endian byte order
4665509076dSJohan Hovold 	 *       regardless of host byte order.
4675509076dSJohan Hovold 	 */
4685509076dSJohan Hovold 	be_start_address = swab16((u16)start_address);
469c3ece7e7SPeter E. Berger 	status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
470c3ece7e7SPeter E. Berger 				(u16)address_type, be_start_address,
471c3ece7e7SPeter E. Berger 				buffer,	write_length, TI_VSEND_TIMEOUT_DEFAULT);
4721da177e4SLinus Torvalds 	if (status) {
47367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
4741da177e4SLinus Torvalds 		return status;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	length		-= write_length;
4781da177e4SLinus Torvalds 	start_address	+= write_length;
4791da177e4SLinus Torvalds 	buffer		+= write_length;
4801da177e4SLinus Torvalds 
48175899513SPeter E. Berger 	/*
48275899513SPeter E. Berger 	 * We should be aligned now -- can write max page size bytes at a
48375899513SPeter E. Berger 	 * time.
48475899513SPeter E. Berger 	 */
4851da177e4SLinus Torvalds 	while (length) {
4861da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
4871da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
4881da177e4SLinus Torvalds 		else
4891da177e4SLinus Torvalds 			write_length = length;
4901da177e4SLinus Torvalds 
49167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
4922742fd88SAlan Cox 			__func__, start_address, write_length);
49359d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, write_length, buffer);
4941da177e4SLinus Torvalds 
4955509076dSJohan Hovold 		/*
4965509076dSJohan Hovold 		 * Write next page.
4975509076dSJohan Hovold 		 *
4985509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian byte
4995509076dSJohan Hovold 		 *       order regardless of host byte order.
5005509076dSJohan Hovold 		 */
5015509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
5022742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
503c3ece7e7SPeter E. Berger 				(u16)address_type, be_start_address, buffer,
504c3ece7e7SPeter E. Berger 				write_length, TI_VSEND_TIMEOUT_DEFAULT);
5051da177e4SLinus Torvalds 		if (status) {
50667e6da70SGreg Kroah-Hartman 			dev_err(dev, "%s - ERROR %d\n", __func__, status);
5071da177e4SLinus Torvalds 			return status;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 		length		-= write_length;
5111da177e4SLinus Torvalds 		start_address	+= write_length;
5121da177e4SLinus Torvalds 		buffer		+= write_length;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	return status;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
51775899513SPeter E. Berger /*
51875899513SPeter E. Berger  * Examine the UMP DMA registers and LSR
5191da177e4SLinus Torvalds  *
5201da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5211da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5221da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5231da177e4SLinus Torvalds  */
tx_active(struct edgeport_port * port)5242742fd88SAlan Cox static int tx_active(struct edgeport_port *port)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	int status;
5271da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
52835aeb1b3SJohan Hovold 	u8 *lsr;
5291da177e4SLinus Torvalds 	int bytes_left = 0;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
53210c642d0SJohan Hovold 	if (!oedb)
5331da177e4SLinus Torvalds 		return -ENOMEM;
5341da177e4SLinus Torvalds 
53575899513SPeter E. Berger 	/*
53675899513SPeter E. Berger 	 * Sigh, that's right, just one byte, as not all platforms can
53775899513SPeter E. Berger 	 * do DMA from stack
53875899513SPeter E. Berger 	 */
53975899513SPeter E. Berger 	lsr = kmalloc(1, GFP_KERNEL);
5401da177e4SLinus Torvalds 	if (!lsr) {
5411da177e4SLinus Torvalds 		kfree(oedb);
5421da177e4SLinus Torvalds 		return -ENOMEM;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5452742fd88SAlan Cox 	status = read_ram(port->port->serial->dev, port->dma_address,
5462742fd88SAlan Cox 						sizeof(*oedb), (void *)oedb);
5471da177e4SLinus Torvalds 	if (status)
5481da177e4SLinus Torvalds 		goto exit_is_tx_active;
5491da177e4SLinus Torvalds 
55067e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - XByteCount    0x%X\n", __func__, oedb->XByteCount);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* and the LSR */
5532742fd88SAlan Cox 	status = read_ram(port->port->serial->dev,
5542742fd88SAlan Cox 			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (status)
5571da177e4SLinus Torvalds 		goto exit_is_tx_active;
55867e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5611da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80) != 0)
5621da177e4SLinus Torvalds 		bytes_left += 64;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
5651da177e4SLinus Torvalds 		bytes_left += 1;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5681da177e4SLinus Torvalds exit_is_tx_active:
56967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	kfree(lsr);
5721da177e4SLinus Torvalds 	kfree(oedb);
5731da177e4SLinus Torvalds 	return bytes_left;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
choose_config(struct usb_device * dev)5762742fd88SAlan Cox static int choose_config(struct usb_device *dev)
5771da177e4SLinus Torvalds {
5782742fd88SAlan Cox 	/*
5792742fd88SAlan Cox 	 * There may be multiple configurations on this device, in which case
5802742fd88SAlan Cox 	 * we would need to read and parse all of them to find out which one
5812742fd88SAlan Cox 	 * we want. However, we just support one config at this point,
5822742fd88SAlan Cox 	 * configuration # 1, which is Config Descriptor 0.
5832742fd88SAlan Cox 	 */
5841da177e4SLinus Torvalds 
58567e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
5862742fd88SAlan Cox 		__func__, dev->config->desc.bNumInterfaces);
58767e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - MAX Power            = %d\n",
5882742fd88SAlan Cox 		__func__, dev->config->desc.bMaxPower * 2);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
59167e6da70SGreg Kroah-Hartman 		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
5921da177e4SLinus Torvalds 		return -ENODEV;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	return 0;
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
read_rom(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)5982742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial,
59935aeb1b3SJohan Hovold 				int start_address, int length, u8 *buffer)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	int status;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6042742fd88SAlan Cox 		status = read_download_mem(serial->serial->dev,
6051da177e4SLinus Torvalds 					       start_address,
6061da177e4SLinus Torvalds 					       length,
6071da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6081da177e4SLinus Torvalds 					       buffer);
6091da177e4SLinus Torvalds 	} else {
6102742fd88SAlan Cox 		status = read_boot_mem(serial, start_address, length,
6111da177e4SLinus Torvalds 								buffer);
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 	return status;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
write_rom(struct edgeport_serial * serial,int start_address,int length,u8 * buffer)6162742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address,
61735aeb1b3SJohan Hovold 						int length, u8 *buffer)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6202742fd88SAlan Cox 		return write_boot_mem(serial, start_address, length,
6211da177e4SLinus Torvalds 								buffer);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
6242742fd88SAlan Cox 		return write_i2c_mem(serial, start_address, length,
6252742fd88SAlan Cox 						serial->TI_I2C_Type, buffer);
6261da177e4SLinus Torvalds 	return -EINVAL;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
get_descriptor_addr(struct edgeport_serial * serial,int desc_type,struct ti_i2c_desc * rom_desc)6302742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial,
6312742fd88SAlan Cox 				int desc_type, struct ti_i2c_desc *rom_desc)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	int start_address;
6341da177e4SLinus Torvalds 	int status;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
6371da177e4SLinus Torvalds 	start_address = 2;
6381da177e4SLinus Torvalds 	do {
6392742fd88SAlan Cox 		status = read_rom(serial,
6401da177e4SLinus Torvalds 				   start_address,
6411da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
64235aeb1b3SJohan Hovold 				   (u8 *)rom_desc);
6431da177e4SLinus Torvalds 		if (status)
6441da177e4SLinus Torvalds 			return 0;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
6471da177e4SLinus Torvalds 			return start_address;
6481da177e4SLinus Torvalds 
6495509076dSJohan Hovold 		start_address = start_address + sizeof(struct ti_i2c_desc) +
6505509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	return 0;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /* Validate descriptor checksum */
valid_csum(struct ti_i2c_desc * rom_desc,u8 * buffer)65835aeb1b3SJohan Hovold static int valid_csum(struct ti_i2c_desc *rom_desc, u8 *buffer)
6591da177e4SLinus Torvalds {
66035aeb1b3SJohan Hovold 	u16 i;
66135aeb1b3SJohan Hovold 	u8 cs = 0;
6621da177e4SLinus Torvalds 
6635509076dSJohan Hovold 	for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
66435aeb1b3SJohan Hovold 		cs = (u8)(cs + buffer[i]);
6652742fd88SAlan Cox 
6661da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
66767e6da70SGreg Kroah-Hartman 		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
6681da177e4SLinus Torvalds 		return -EINVAL;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	return 0;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /* Make sure that the I2C image is good */
check_i2c_image(struct edgeport_serial * serial)6742742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
6771da177e4SLinus Torvalds 	int status = 0;
6781da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
6791da177e4SLinus Torvalds 	int start_address = 2;
68035aeb1b3SJohan Hovold 	u8 *buffer;
68135aeb1b3SJohan Hovold 	u16 ttype;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
68410c642d0SJohan Hovold 	if (!rom_desc)
6851da177e4SLinus Torvalds 		return -ENOMEM;
68610c642d0SJohan Hovold 
6871da177e4SLinus Torvalds 	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
6881da177e4SLinus Torvalds 	if (!buffer) {
6891da177e4SLinus Torvalds 		kfree(rom_desc);
6901da177e4SLinus Torvalds 		return -ENOMEM;
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
6932742fd88SAlan Cox 	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
6942742fd88SAlan Cox 	status = read_rom(serial, 0, 1, buffer);
6951da177e4SLinus Torvalds 	if (status)
6962742fd88SAlan Cox 		goto out;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
699441b62c1SHarvey Harrison 		dev_err(dev, "%s - invalid buffer signature\n", __func__);
7001da177e4SLinus Torvalds 		status = -ENODEV;
7012742fd88SAlan Cox 		goto out;
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	do {
7052742fd88SAlan Cox 		/* Validate the I2C */
7062742fd88SAlan Cox 		status = read_rom(serial,
7071da177e4SLinus Torvalds 				start_address,
7081da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
70935aeb1b3SJohan Hovold 				(u8 *)rom_desc);
7101da177e4SLinus Torvalds 		if (status)
7111da177e4SLinus Torvalds 			break;
7121da177e4SLinus Torvalds 
7132742fd88SAlan Cox 		if ((start_address + sizeof(struct ti_i2c_desc) +
7145509076dSJohan Hovold 			le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
7151da177e4SLinus Torvalds 			status = -ENODEV;
71667e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
7171da177e4SLinus Torvalds 			break;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 
72067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
7211da177e4SLinus Torvalds 
7222742fd88SAlan Cox 		/* Skip type 2 record */
7231da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
7241da177e4SLinus Torvalds 		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
7251da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
7262742fd88SAlan Cox 			/* Read the descriptor data */
7272742fd88SAlan Cox 			status = read_rom(serial, start_address +
7282742fd88SAlan Cox 						sizeof(struct ti_i2c_desc),
7295509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size),
7305509076dSJohan Hovold 						buffer);
7311da177e4SLinus Torvalds 			if (status)
7321da177e4SLinus Torvalds 				break;
7331da177e4SLinus Torvalds 
7342742fd88SAlan Cox 			status = valid_csum(rom_desc, buffer);
7351da177e4SLinus Torvalds 			if (status)
7361da177e4SLinus Torvalds 				break;
7371da177e4SLinus Torvalds 		}
7382742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc) +
7395509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
7401da177e4SLinus Torvalds 
7412742fd88SAlan Cox 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
7422742fd88SAlan Cox 				(start_address < TI_MAX_I2C_SIZE));
7431da177e4SLinus Torvalds 
7442742fd88SAlan Cox 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
7452742fd88SAlan Cox 				(start_address > TI_MAX_I2C_SIZE))
7461da177e4SLinus Torvalds 		status = -ENODEV;
7471da177e4SLinus Torvalds 
7482742fd88SAlan Cox out:
7491da177e4SLinus Torvalds 	kfree(buffer);
7501da177e4SLinus Torvalds 	kfree(rom_desc);
7511da177e4SLinus Torvalds 	return status;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
get_manuf_info(struct edgeport_serial * serial,u8 * buffer)75435aeb1b3SJohan Hovold static int get_manuf_info(struct edgeport_serial *serial, u8 *buffer)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	int status;
7571da177e4SLinus Torvalds 	int start_address;
7581da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7591da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
76067e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
76310c642d0SJohan Hovold 	if (!rom_desc)
7641da177e4SLinus Torvalds 		return -ENOMEM;
76510c642d0SJohan Hovold 
7662742fd88SAlan Cox 	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
7672742fd88SAlan Cox 								rom_desc);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (!start_address) {
77067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
7711da177e4SLinus Torvalds 		status = -ENODEV;
7721da177e4SLinus Torvalds 		goto exit;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7752742fd88SAlan Cox 	/* Read the descriptor data */
7762742fd88SAlan Cox 	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
7775509076dSJohan Hovold 					le16_to_cpu(rom_desc->Size), buffer);
7781da177e4SLinus Torvalds 	if (status)
7791da177e4SLinus Torvalds 		goto exit;
7801da177e4SLinus Torvalds 
7812742fd88SAlan Cox 	status = valid_csum(rom_desc, buffer);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
78467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - IonConfig      0x%x\n", __func__, desc->IonConfig);
78567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Version          %d\n", __func__, desc->Version);
78667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Cpu/Board      0x%x\n", __func__, desc->CpuRev_BoardRev);
78767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumPorts         %d\n", __func__, desc->NumPorts);
78867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumVirtualPorts  %d\n", __func__, desc->NumVirtualPorts);
78967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - TotalPorts       %d\n", __func__, desc->TotalPorts);
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds exit:
7921da177e4SLinus Torvalds 	kfree(rom_desc);
7931da177e4SLinus Torvalds 	return status;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /* Build firmware header used for firmware update */
build_i2c_fw_hdr(u8 * header,const struct firmware * fw)797277bf37bSPeter E. Berger static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw)
7981da177e4SLinus Torvalds {
79935aeb1b3SJohan Hovold 	u8 *buffer;
8001da177e4SLinus Torvalds 	int buffer_size;
8011da177e4SLinus Torvalds 	int i;
80235aeb1b3SJohan Hovold 	u8 cs = 0;
8031da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8041da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8051da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
806c0e34831SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
8071da177e4SLinus Torvalds 
80875899513SPeter E. Berger 	/*
80975899513SPeter E. Berger 	 * In order to update the I2C firmware we must change the type 2 record
8102742fd88SAlan Cox 	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
8112742fd88SAlan Cox 	 * Then while in boot mode, the driver will download the latest
8122742fd88SAlan Cox 	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
8132742fd88SAlan Cox 	 * device comes back up in download mode the driver will cause the new
8142742fd88SAlan Cox 	 * firmware to be copied from the UMP Ram to I2C and the firmware will
8152742fd88SAlan Cox 	 * update the record type from 0xf2 to 0x02.
8162742fd88SAlan Cox 	 */
8171da177e4SLinus Torvalds 
81875899513SPeter E. Berger 	/*
81975899513SPeter E. Berger 	 * Allocate a 15.5k buffer + 2 bytes for version number (Firmware
82075899513SPeter E. Berger 	 * Record)
82175899513SPeter E. Berger 	 */
8222742fd88SAlan Cox 	buffer_size = (((1024 * 16) - 512 ) +
8232742fd88SAlan Cox 			sizeof(struct ti_i2c_firmware_rec));
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	buffer = kmalloc(buffer_size, GFP_KERNEL);
82610c642d0SJohan Hovold 	if (!buffer)
8271da177e4SLinus Torvalds 		return -ENOMEM;
8281da177e4SLinus Torvalds 
82975899513SPeter E. Berger 	/* Set entire image of 0xffs */
8301da177e4SLinus Torvalds 	memset(buffer, 0xff, buffer_size);
8311da177e4SLinus Torvalds 
8322742fd88SAlan Cox 	/* Copy version number into firmware record */
8331da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
8341da177e4SLinus Torvalds 
835c0e34831SPeter E. Berger 	firmware_rec->Ver_Major	= fw_hdr->major_version;
836c0e34831SPeter E. Berger 	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
8371da177e4SLinus Torvalds 
8382742fd88SAlan Cox 	/* Pointer to fw_down memory image */
839d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
842d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
8431da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
84635aeb1b3SJohan Hovold 		cs = (u8)(cs + buffer[i]);
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	kfree(buffer);
8501da177e4SLinus Torvalds 
8512742fd88SAlan Cox 	/* Build new header */
8521da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
8531da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
856c03890ffSJohan Hovold 	i2c_header->Size	= cpu_to_le16(buffer_size);
8571da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
858c0e34831SPeter E. Berger 	firmware_rec->Ver_Major	= fw_hdr->major_version;
859c0e34831SPeter E. Berger 	firmware_rec->Ver_Minor	= fw_hdr->minor_version;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	return 0;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
i2c_type_bootmode(struct edgeport_serial * serial)8652742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial)
8661da177e4SLinus Torvalds {
86767e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
8681da177e4SLinus Torvalds 	int status;
869e9305d2fSJohan Hovold 	u8 *data;
870e9305d2fSJohan Hovold 
871e9305d2fSJohan Hovold 	data = kmalloc(1, GFP_KERNEL);
87210c642d0SJohan Hovold 	if (!data)
873e9305d2fSJohan Hovold 		return -ENOMEM;
8741da177e4SLinus Torvalds 
8752742fd88SAlan Cox 	/* Try to read type 2 */
8762742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
877e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
8781da177e4SLinus Torvalds 	if (status)
87967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
8801da177e4SLinus Torvalds 	else
88167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
882e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
88367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
8841da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
885e9305d2fSJohan Hovold 		goto out;
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 
8882742fd88SAlan Cox 	/* Try to read type 3 */
8892742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
890e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
8911da177e4SLinus Torvalds 	if (status)
89267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
8931da177e4SLinus Torvalds 	else
89467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
895e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
89667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
8971da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
898e9305d2fSJohan Hovold 		goto out;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
90167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Unknown\n", __func__);
9021da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
903e9305d2fSJohan Hovold 	status = -ENODEV;
904e9305d2fSJohan Hovold out:
905e9305d2fSJohan Hovold 	kfree(data);
906e9305d2fSJohan Hovold 	return status;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
bulk_xfer(struct usb_serial * serial,void * buffer,int length,int * num_sent)9092742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer,
9102742fd88SAlan Cox 						int length, int *num_sent)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	int status;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	status = usb_bulk_msg(serial->dev,
9151da177e4SLinus Torvalds 			usb_sndbulkpipe(serial->dev,
9161da177e4SLinus Torvalds 				serial->port[0]->bulk_out_endpointAddress),
9172742fd88SAlan Cox 			buffer, length, num_sent, 1000);
9181da177e4SLinus Torvalds 	return status;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
download_code(struct edgeport_serial * serial,u8 * image,int image_length)92235aeb1b3SJohan Hovold static int download_code(struct edgeport_serial *serial, u8 *image,
9232742fd88SAlan Cox 							int image_length)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	int status = 0;
9261da177e4SLinus Torvalds 	int pos;
9271da177e4SLinus Torvalds 	int transfer;
9281da177e4SLinus Torvalds 	int done;
9291da177e4SLinus Torvalds 
9302742fd88SAlan Cox 	/* Transfer firmware image */
9311da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
9322742fd88SAlan Cox 		/* Read the next buffer from file */
9331da177e4SLinus Torvalds 		transfer = image_length - pos;
9341da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
9351da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
9361da177e4SLinus Torvalds 
9372742fd88SAlan Cox 		/* Transfer data */
9382742fd88SAlan Cox 		status = bulk_xfer(serial->serial, &image[pos],
9392742fd88SAlan Cox 							transfer, &done);
9401da177e4SLinus Torvalds 		if (status)
9411da177e4SLinus Torvalds 			break;
9422742fd88SAlan Cox 		/* Advance buffer pointer */
9431da177e4SLinus Torvalds 		pos += done;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	return status;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9492742fd88SAlan Cox /* FIXME!!! */
config_boot_dev(struct usb_device * dev)9502742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	return 0;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
ti_cpu_rev(struct edge_ti_manuf_descriptor * desc)9552742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
9562742fd88SAlan Cox {
9572742fd88SAlan Cox 	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
9582742fd88SAlan Cox }
9592742fd88SAlan Cox 
check_fw_sanity(struct edgeport_serial * serial,const struct firmware * fw)960dcb8e99dSPeter E. Berger static int check_fw_sanity(struct edgeport_serial *serial,
961dcb8e99dSPeter E. Berger 		const struct firmware *fw)
962dcb8e99dSPeter E. Berger {
963dcb8e99dSPeter E. Berger 	u16 length_total;
964dcb8e99dSPeter E. Berger 	u8 checksum = 0;
965dcb8e99dSPeter E. Berger 	int pos;
966dcb8e99dSPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
967dcb8e99dSPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
968dcb8e99dSPeter E. Berger 
969dcb8e99dSPeter E. Berger 	if (fw->size < sizeof(struct edgeport_fw_hdr)) {
970dcb8e99dSPeter E. Berger 		dev_err(dev, "incomplete fw header\n");
971dcb8e99dSPeter E. Berger 		return -EINVAL;
972dcb8e99dSPeter E. Berger 	}
973dcb8e99dSPeter E. Berger 
974dcb8e99dSPeter E. Berger 	length_total = le16_to_cpu(fw_hdr->length) +
975dcb8e99dSPeter E. Berger 			sizeof(struct edgeport_fw_hdr);
976dcb8e99dSPeter E. Berger 
977dcb8e99dSPeter E. Berger 	if (fw->size != length_total) {
978dcb8e99dSPeter E. Berger 		dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
979dcb8e99dSPeter E. Berger 				length_total, fw->size);
980dcb8e99dSPeter E. Berger 		return -EINVAL;
981dcb8e99dSPeter E. Berger 	}
982dcb8e99dSPeter E. Berger 
983dcb8e99dSPeter E. Berger 	for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
984dcb8e99dSPeter E. Berger 		checksum += fw->data[pos];
985dcb8e99dSPeter E. Berger 
986dcb8e99dSPeter E. Berger 	if (checksum != fw_hdr->checksum) {
987dcb8e99dSPeter E. Berger 		dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
988dcb8e99dSPeter E. Berger 				fw_hdr->checksum, checksum);
989dcb8e99dSPeter E. Berger 		return -EINVAL;
990dcb8e99dSPeter E. Berger 	}
991dcb8e99dSPeter E. Berger 
992dcb8e99dSPeter E. Berger 	return 0;
993dcb8e99dSPeter E. Berger }
994dcb8e99dSPeter E. Berger 
99575899513SPeter E. Berger /*
9961da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
9971da177e4SLinus Torvalds  *
9981da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
9991da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
10001da177e4SLinus Torvalds  */
download_fw(struct edgeport_serial * serial)1001b4146162SPeter E. Berger static int download_fw(struct edgeport_serial *serial)
10021da177e4SLinus Torvalds {
1003ef9324b2SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
10041da177e4SLinus Torvalds 	int status = 0;
10051da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
1006b4146162SPeter E. Berger 	const struct firmware *fw;
1007b4146162SPeter E. Berger 	const char *fw_name = "edgeport/down3.bin";
1008b4146162SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr;
1009c0e34831SPeter E. Berger 
1010b4146162SPeter E. Berger 	status = request_firmware(&fw, fw_name, dev);
1011b4146162SPeter E. Berger 	if (status) {
1012b4146162SPeter E. Berger 		dev_err(dev, "Failed to load image \"%s\" err %d\n",
1013b4146162SPeter E. Berger 				fw_name, status);
1014b4146162SPeter E. Berger 		return status;
1015b4146162SPeter E. Berger 	}
1016c0e34831SPeter E. Berger 
1017b4146162SPeter E. Berger 	if (check_fw_sanity(serial, fw)) {
1018b4146162SPeter E. Berger 		status = -EINVAL;
1019b4146162SPeter E. Berger 		goto out;
1020b4146162SPeter E. Berger 	}
1021b4146162SPeter E. Berger 
1022b4146162SPeter E. Berger 	fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1023b4146162SPeter E. Berger 
1024b4146162SPeter E. Berger 	/* If on-board version is newer, "fw_version" will be updated later. */
1025c0e34831SPeter E. Berger 	serial->fw_version = (fw_hdr->major_version << 8) +
1026c0e34831SPeter E. Berger 			fw_hdr->minor_version;
10271da177e4SLinus Torvalds 
102875899513SPeter E. Berger 	/*
102975899513SPeter E. Berger 	 * This routine is entered by both the BOOT mode and the Download mode
10301da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10311da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10321da177e4SLinus Torvalds 	 */
10331da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10361da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10371da177e4SLinus Torvalds 
10382742fd88SAlan Cox 	status = choose_config(serial->serial->dev);
10391da177e4SLinus Torvalds 	if (status)
1040b4146162SPeter E. Berger 		goto out;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10431da177e4SLinus Torvalds 	if (!interface) {
1044441b62c1SHarvey Harrison 		dev_err(dev, "%s - no interface set, error!\n", __func__);
1045b4146162SPeter E. Berger 		status = -ENODEV;
1046b4146162SPeter E. Berger 		goto out;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 
10492742fd88SAlan Cox 	/*
10502742fd88SAlan Cox 	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10512742fd88SAlan Cox 	 * if we have more than one endpoint we are definitely in download
10522742fd88SAlan Cox 	 * mode
10532742fd88SAlan Cox 	 */
1054bebf1f18SPeter E. Berger 	if (interface->bNumEndpoints > 1) {
10551da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1056b4146162SPeter E. Berger 		status = do_download_mode(serial, fw);
1057b4146162SPeter E. Berger 	} else {
10582742fd88SAlan Cox 		/* Otherwise we will remain in configuring mode */
10591da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
1060b4146162SPeter E. Berger 		status = do_boot_mode(serial, fw);
1061b4146162SPeter E. Berger 	}
10621da177e4SLinus Torvalds 
1063b4146162SPeter E. Berger out:
1064b4146162SPeter E. Berger 	release_firmware(fw);
1065b4146162SPeter E. Berger 	return status;
1066bebf1f18SPeter E. Berger }
1067bebf1f18SPeter E. Berger 
do_download_mode(struct edgeport_serial * serial,const struct firmware * fw)1068bebf1f18SPeter E. Berger static int do_download_mode(struct edgeport_serial *serial,
1069bebf1f18SPeter E. Berger 		const struct firmware *fw)
1070bebf1f18SPeter E. Berger {
1071bebf1f18SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
1072bebf1f18SPeter E. Berger 	int status = 0;
1073bebf1f18SPeter E. Berger 	int start_address;
1074bebf1f18SPeter E. Berger 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
1075bebf1f18SPeter E. Berger 	int download_cur_ver;
1076bebf1f18SPeter E. Berger 	int download_new_ver;
1077bebf1f18SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
10781da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
10791da177e4SLinus Torvalds 
108067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
10811da177e4SLinus Torvalds 
10822742fd88SAlan Cox 	status = check_i2c_image(serial);
10831da177e4SLinus Torvalds 	if (status) {
108467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
10851da177e4SLinus Torvalds 		return status;
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 
108875899513SPeter E. Berger 	/*
108975899513SPeter E. Berger 	 * Validate Hardware version number
10901da177e4SLinus Torvalds 	 * Read Manufacturing Descriptor from TI Based Edgeport
10911da177e4SLinus Torvalds 	 */
10921da177e4SLinus Torvalds 	ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
109310c642d0SJohan Hovold 	if (!ti_manuf_desc)
10941da177e4SLinus Torvalds 		return -ENOMEM;
109510c642d0SJohan Hovold 
109635aeb1b3SJohan Hovold 	status = get_manuf_info(serial, (u8 *)ti_manuf_desc);
10971da177e4SLinus Torvalds 	if (status) {
10981da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
10991da177e4SLinus Torvalds 		return status;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
11022742fd88SAlan Cox 	/* Check version number of ION descriptor */
11032742fd88SAlan Cox 	if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
110467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
11052742fd88SAlan Cox 			__func__, ti_cpu_rev(ti_manuf_desc));
11061da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
11071da177e4SLinus Torvalds 		return -EINVAL;
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
11111da177e4SLinus Torvalds 	if (!rom_desc) {
11121da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
11131da177e4SLinus Torvalds 		return -ENOMEM;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 
11162742fd88SAlan Cox 	/* Search for type 2 record (firmware record) */
11172742fd88SAlan Cox 	start_address = get_descriptor_addr(serial,
11182742fd88SAlan Cox 			I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
11192742fd88SAlan Cox 	if (start_address != 0) {
11201da177e4SLinus Torvalds 		struct ti_i2c_firmware_rec *firmware_version;
1121e9305d2fSJohan Hovold 		u8 *record;
11221da177e4SLinus Torvalds 
1123bebf1f18SPeter E. Berger 		dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n",
1124bebf1f18SPeter E. Berger 				__func__);
11251da177e4SLinus Torvalds 
11262742fd88SAlan Cox 		firmware_version = kmalloc(sizeof(*firmware_version),
11272742fd88SAlan Cox 							GFP_KERNEL);
11281da177e4SLinus Torvalds 		if (!firmware_version) {
11291da177e4SLinus Torvalds 			kfree(rom_desc);
11301da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11311da177e4SLinus Torvalds 			return -ENOMEM;
11321da177e4SLinus Torvalds 		}
11331da177e4SLinus Torvalds 
113475899513SPeter E. Berger 		/*
113575899513SPeter E. Berger 		 * Validate version number
11362742fd88SAlan Cox 		 * Read the descriptor data
11372742fd88SAlan Cox 		 */
11382742fd88SAlan Cox 		status = read_rom(serial, start_address +
11392742fd88SAlan Cox 				sizeof(struct ti_i2c_desc),
11401da177e4SLinus Torvalds 				sizeof(struct ti_i2c_firmware_rec),
114135aeb1b3SJohan Hovold 				(u8 *)firmware_version);
11421da177e4SLinus Torvalds 		if (status) {
11431da177e4SLinus Torvalds 			kfree(firmware_version);
11441da177e4SLinus Torvalds 			kfree(rom_desc);
11451da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11461da177e4SLinus Torvalds 			return status;
11471da177e4SLinus Torvalds 		}
11481da177e4SLinus Torvalds 
114975899513SPeter E. Berger 		/*
115075899513SPeter E. Berger 		 * Check version number of download with current
115175899513SPeter E. Berger 		 * version in I2c
115275899513SPeter E. Berger 		 */
11531da177e4SLinus Torvalds 		download_cur_ver = (firmware_version->Ver_Major << 8) +
11541da177e4SLinus Torvalds 				   (firmware_version->Ver_Minor);
1155c0e34831SPeter E. Berger 		download_new_ver = (fw_hdr->major_version << 8) +
1156c0e34831SPeter E. Berger 				   (fw_hdr->minor_version);
11571da177e4SLinus Torvalds 
115867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
115967e6da70SGreg Kroah-Hartman 			__func__, firmware_version->Ver_Major,
11601da177e4SLinus Torvalds 			firmware_version->Ver_Minor,
1161c0e34831SPeter E. Berger 			fw_hdr->major_version, fw_hdr->minor_version);
11621da177e4SLinus Torvalds 
116375899513SPeter E. Berger 		/*
116475899513SPeter E. Berger 		 * Check if we have an old version in the I2C and
116575899513SPeter E. Berger 		 * update if necessary
116675899513SPeter E. Berger 		 */
11670827a9ffSGreg Kroah-Hartman 		if (download_cur_ver < download_new_ver) {
116867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1169441b62c1SHarvey Harrison 				__func__,
11701da177e4SLinus Torvalds 				firmware_version->Ver_Major,
11711da177e4SLinus Torvalds 				firmware_version->Ver_Minor,
1172c0e34831SPeter E. Berger 				fw_hdr->major_version,
1173c0e34831SPeter E. Berger 				fw_hdr->minor_version);
11741da177e4SLinus Torvalds 
1175e9305d2fSJohan Hovold 			record = kmalloc(1, GFP_KERNEL);
1176e9305d2fSJohan Hovold 			if (!record) {
1177e9305d2fSJohan Hovold 				kfree(firmware_version);
1178e9305d2fSJohan Hovold 				kfree(rom_desc);
1179e9305d2fSJohan Hovold 				kfree(ti_manuf_desc);
1180e9305d2fSJohan Hovold 				return -ENOMEM;
1181e9305d2fSJohan Hovold 			}
1182bebf1f18SPeter E. Berger 			/*
1183bebf1f18SPeter E. Berger 			 * In order to update the I2C firmware we must
11842742fd88SAlan Cox 			 * change the type 2 record to type 0xF2. This
11852742fd88SAlan Cox 			 * will force the UMP to come up in Boot Mode.
11862742fd88SAlan Cox 			 * Then while in boot mode, the driver will
11872742fd88SAlan Cox 			 * download the latest firmware (padded to
11882742fd88SAlan Cox 			 * 15.5k) into the UMP ram. Finally when the
11892742fd88SAlan Cox 			 * device comes back up in download mode the
11902742fd88SAlan Cox 			 * driver will cause the new firmware to be
11912742fd88SAlan Cox 			 * copied from the UMP Ram to I2C and the
11922742fd88SAlan Cox 			 * firmware will update the record type from
11932742fd88SAlan Cox 			 * 0xf2 to 0x02.
11942742fd88SAlan Cox 			 */
1195e9305d2fSJohan Hovold 			*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11961da177e4SLinus Torvalds 
119775899513SPeter E. Berger 			/*
119875899513SPeter E. Berger 			 * Change the I2C Firmware record type to
119975899513SPeter E. Berger 			 * 0xf2 to trigger an update
120075899513SPeter E. Berger 			 */
12012742fd88SAlan Cox 			status = write_rom(serial, start_address,
1202e9305d2fSJohan Hovold 					sizeof(*record), record);
12031da177e4SLinus Torvalds 			if (status) {
1204e9305d2fSJohan Hovold 				kfree(record);
12051da177e4SLinus Torvalds 				kfree(firmware_version);
12061da177e4SLinus Torvalds 				kfree(rom_desc);
12071da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12081da177e4SLinus Torvalds 				return status;
12091da177e4SLinus Torvalds 			}
12101da177e4SLinus Torvalds 
121175899513SPeter E. Berger 			/*
121275899513SPeter E. Berger 			 * verify the write -- must do this in order
12132742fd88SAlan Cox 			 * for write to complete before we do the
12142742fd88SAlan Cox 			 * hardware reset
12152742fd88SAlan Cox 			 */
12162742fd88SAlan Cox 			status = read_rom(serial,
12171da177e4SLinus Torvalds 						start_address,
1218e9305d2fSJohan Hovold 						sizeof(*record),
1219e9305d2fSJohan Hovold 						record);
12201da177e4SLinus Torvalds 			if (status) {
1221e9305d2fSJohan Hovold 				kfree(record);
12221da177e4SLinus Torvalds 				kfree(firmware_version);
12231da177e4SLinus Torvalds 				kfree(rom_desc);
12241da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12251da177e4SLinus Torvalds 				return status;
12261da177e4SLinus Torvalds 			}
12271da177e4SLinus Torvalds 
1228e9305d2fSJohan Hovold 			if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1229bebf1f18SPeter E. Berger 				dev_err(dev, "%s - error resetting device\n",
1230bebf1f18SPeter E. Berger 						__func__);
1231e9305d2fSJohan Hovold 				kfree(record);
12321da177e4SLinus Torvalds 				kfree(firmware_version);
12331da177e4SLinus Torvalds 				kfree(rom_desc);
12341da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12351da177e4SLinus Torvalds 				return -ENODEV;
12361da177e4SLinus Torvalds 			}
12371da177e4SLinus Torvalds 
123867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
12391da177e4SLinus Torvalds 
12402742fd88SAlan Cox 			/* Reset UMP -- Back to BOOT MODE */
12412742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
12422742fd88SAlan Cox 					UMPC_HARDWARE_RESET,
1243c3ece7e7SPeter E. Berger 					0, 0, NULL, 0,
1244c3ece7e7SPeter E. Berger 					TI_VSEND_TIMEOUT_DEFAULT);
12451da177e4SLinus Torvalds 
1246bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - HARDWARE RESET return %d\n",
1247bebf1f18SPeter E. Berger 					__func__, status);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 			/* return an error on purpose. */
1250e9305d2fSJohan Hovold 			kfree(record);
12511da177e4SLinus Torvalds 			kfree(firmware_version);
12521da177e4SLinus Torvalds 			kfree(rom_desc);
12531da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
12541da177e4SLinus Torvalds 			return -ENODEV;
1255bebf1f18SPeter E. Berger 		}
1256c0e34831SPeter E. Berger 		/* Same or newer fw version is already loaded */
1257c0e34831SPeter E. Berger 		serial->fw_version = download_cur_ver;
12581da177e4SLinus Torvalds 		kfree(firmware_version);
12591da177e4SLinus Torvalds 	}
12602742fd88SAlan Cox 	/* Search for type 0xF2 record (firmware blank record) */
1261bebf1f18SPeter E. Berger 	else {
1262bebf1f18SPeter E. Berger 		start_address = get_descriptor_addr(serial,
1263bebf1f18SPeter E. Berger 				I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc);
1264bebf1f18SPeter E. Berger 		if (start_address != 0) {
12652742fd88SAlan Cox #define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
12662742fd88SAlan Cox 				sizeof(struct ti_i2c_firmware_rec))
126735aeb1b3SJohan Hovold 			u8 *header;
126835aeb1b3SJohan Hovold 			u8 *vheader;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
12711da177e4SLinus Torvalds 			if (!header) {
12721da177e4SLinus Torvalds 				kfree(rom_desc);
12731da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12741da177e4SLinus Torvalds 				return -ENOMEM;
12751da177e4SLinus Torvalds 			}
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
12781da177e4SLinus Torvalds 			if (!vheader) {
12791da177e4SLinus Torvalds 				kfree(header);
12801da177e4SLinus Torvalds 				kfree(rom_desc);
12811da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12821da177e4SLinus Torvalds 				return -ENOMEM;
12831da177e4SLinus Torvalds 			}
12841da177e4SLinus Torvalds 
1285bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n",
1286bebf1f18SPeter E. Berger 					__func__);
12871da177e4SLinus Torvalds 
12882742fd88SAlan Cox 			/*
12892742fd88SAlan Cox 			 * In order to update the I2C firmware we must change
12902742fd88SAlan Cox 			 * the type 2 record to type 0xF2. This will force the
12912742fd88SAlan Cox 			 * UMP to come up in Boot Mode.  Then while in boot
12922742fd88SAlan Cox 			 * mode, the driver will download the latest firmware
12932742fd88SAlan Cox 			 * (padded to 15.5k) into the UMP ram. Finally when the
12942742fd88SAlan Cox 			 * device comes back up in download mode the driver
12952742fd88SAlan Cox 			 * will cause the new firmware to be copied from the
12962742fd88SAlan Cox 			 * UMP Ram to I2C and the firmware will update the
12972742fd88SAlan Cox 			 * record type from 0xf2 to 0x02.
12982742fd88SAlan Cox 			 */
1299277bf37bSPeter E. Berger 			status = build_i2c_fw_hdr(header, fw);
13001da177e4SLinus Torvalds 			if (status) {
13011da177e4SLinus Torvalds 				kfree(vheader);
13021da177e4SLinus Torvalds 				kfree(header);
13031da177e4SLinus Torvalds 				kfree(rom_desc);
13041da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
1305fd6e5bbbSRoel Kluin 				return -EINVAL;
13061da177e4SLinus Torvalds 			}
13071da177e4SLinus Torvalds 
130875899513SPeter E. Berger 			/*
130975899513SPeter E. Berger 			 * Update I2C with type 0xf2 record with correct
131075899513SPeter E. Berger 			 * size and checksum
131175899513SPeter E. Berger 			 */
13122742fd88SAlan Cox 			status = write_rom(serial,
13131da177e4SLinus Torvalds 						start_address,
13141da177e4SLinus Torvalds 						HEADER_SIZE,
13151da177e4SLinus Torvalds 						header);
13161da177e4SLinus Torvalds 			if (status) {
13171da177e4SLinus Torvalds 				kfree(vheader);
13181da177e4SLinus Torvalds 				kfree(header);
13191da177e4SLinus Torvalds 				kfree(rom_desc);
13201da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13219800eb33SRoel Kluin 				return -EINVAL;
13221da177e4SLinus Torvalds 			}
13231da177e4SLinus Torvalds 
132475899513SPeter E. Berger 			/*
132575899513SPeter E. Berger 			 * verify the write -- must do this in order for
132675899513SPeter E. Berger 			 * write to complete before we do the hardware reset
132775899513SPeter E. Berger 			 */
13282742fd88SAlan Cox 			status = read_rom(serial, start_address,
13292742fd88SAlan Cox 							HEADER_SIZE, vheader);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 			if (status) {
1332bebf1f18SPeter E. Berger 				dev_dbg(dev, "%s - can't read header back\n",
1333bebf1f18SPeter E. Berger 						__func__);
13341da177e4SLinus Torvalds 				kfree(vheader);
13351da177e4SLinus Torvalds 				kfree(header);
13361da177e4SLinus Torvalds 				kfree(rom_desc);
13371da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13381da177e4SLinus Torvalds 				return status;
13391da177e4SLinus Torvalds 			}
13401da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
1341bebf1f18SPeter E. Berger 				dev_dbg(dev, "%s - write download record failed\n",
1342bebf1f18SPeter E. Berger 						__func__);
13431da177e4SLinus Torvalds 				kfree(vheader);
13441da177e4SLinus Torvalds 				kfree(header);
13451da177e4SLinus Torvalds 				kfree(rom_desc);
13461da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13471e29709eSRoel Kluin 				return -EINVAL;
13481da177e4SLinus Torvalds 			}
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 			kfree(vheader);
13511da177e4SLinus Torvalds 			kfree(header);
13521da177e4SLinus Torvalds 
135367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Start firmware update\n", __func__);
13541da177e4SLinus Torvalds 
13552742fd88SAlan Cox 			/* Tell firmware to copy download image into I2C */
13562742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
1357c3ece7e7SPeter E. Berger 					UMPC_COPY_DNLD_TO_I2C,
1358c3ece7e7SPeter E. Berger 					0, 0, NULL, 0,
1359c3ece7e7SPeter E. Berger 					TI_VSEND_TIMEOUT_FW_DOWNLOAD);
13601da177e4SLinus Torvalds 
1361bebf1f18SPeter E. Berger 			dev_dbg(dev, "%s - Update complete 0x%x\n", __func__,
1362bebf1f18SPeter E. Berger 					status);
13631da177e4SLinus Torvalds 			if (status) {
13642742fd88SAlan Cox 				dev_err(dev,
13652742fd88SAlan Cox 					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
13662742fd88SAlan Cox 					__func__);
13671da177e4SLinus Torvalds 				kfree(rom_desc);
13681da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13691da177e4SLinus Torvalds 				return status;
13701da177e4SLinus Torvalds 			}
13711da177e4SLinus Torvalds 		}
1372bebf1f18SPeter E. Berger 	}
13731da177e4SLinus Torvalds 
1374bebf1f18SPeter E. Berger 	/* The device is running the download code */
13751da177e4SLinus Torvalds 	kfree(rom_desc);
13761da177e4SLinus Torvalds 	kfree(ti_manuf_desc);
13771da177e4SLinus Torvalds 	return 0;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
do_boot_mode(struct edgeport_serial * serial,const struct firmware * fw)1380bebf1f18SPeter E. Berger static int do_boot_mode(struct edgeport_serial *serial,
1381bebf1f18SPeter E. Berger 		const struct firmware *fw)
1382bebf1f18SPeter E. Berger {
1383bebf1f18SPeter E. Berger 	struct device *dev = &serial->serial->interface->dev;
1384bebf1f18SPeter E. Berger 	int status = 0;
1385bebf1f18SPeter E. Berger 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
1386bebf1f18SPeter E. Berger 	struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1387bebf1f18SPeter E. Berger 
138867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
13891da177e4SLinus Torvalds 
13902742fd88SAlan Cox 	/* Configure the TI device so we can use the BULK pipes for download */
13912742fd88SAlan Cox 	status = config_boot_dev(serial->serial->dev);
13921da177e4SLinus Torvalds 	if (status)
13931da177e4SLinus Torvalds 		return status;
13941da177e4SLinus Torvalds 
13952742fd88SAlan Cox 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
13962742fd88SAlan Cox 							!= USB_VENDOR_ID_ION) {
139767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
13981da177e4SLinus Torvalds 			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13991da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
14002742fd88SAlan Cox 		goto stayinbootmode;
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 
140375899513SPeter E. Berger 	/*
140475899513SPeter E. Berger 	 * We have an ION device (I2c Must be programmed)
140575899513SPeter E. Berger 	 * Determine I2C image type
140675899513SPeter E. Berger 	 */
14072742fd88SAlan Cox 	if (i2c_type_bootmode(serial))
14082742fd88SAlan Cox 		goto stayinbootmode;
14091da177e4SLinus Torvalds 
14102742fd88SAlan Cox 	/* Check for ION Vendor ID and that the I2C is valid */
14112742fd88SAlan Cox 	if (!check_i2c_image(serial)) {
14121da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
14131da177e4SLinus Torvalds 		int i;
141435aeb1b3SJohan Hovold 		u8 cs = 0;
141535aeb1b3SJohan Hovold 		u8 *buffer;
14161da177e4SLinus Torvalds 		int buffer_size;
14171da177e4SLinus Torvalds 
141875899513SPeter E. Berger 		/*
141975899513SPeter E. Berger 		 * Validate Hardware version number
14201da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
14211da177e4SLinus Torvalds 		 */
14221da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
142310c642d0SJohan Hovold 		if (!ti_manuf_desc)
14241da177e4SLinus Torvalds 			return -ENOMEM;
142510c642d0SJohan Hovold 
142635aeb1b3SJohan Hovold 		status = get_manuf_info(serial, (u8 *)ti_manuf_desc);
14271da177e4SLinus Torvalds 		if (status) {
14281da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14292742fd88SAlan Cox 			goto stayinbootmode;
14301da177e4SLinus Torvalds 		}
14311da177e4SLinus Torvalds 
14322742fd88SAlan Cox 		/* Check for version 2 */
14332742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
143467e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
14352742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
14361da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14372742fd88SAlan Cox 			goto stayinbootmode;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 		/*
14432742fd88SAlan Cox 		 * In order to update the I2C firmware we must change the type
14442742fd88SAlan Cox 		 * 2 record to type 0xF2. This will force the UMP to come up
14452742fd88SAlan Cox 		 * in Boot Mode.  Then while in boot mode, the driver will
14462742fd88SAlan Cox 		 * download the latest firmware (padded to 15.5k) into the
14472742fd88SAlan Cox 		 * UMP ram. Finally when the device comes back up in download
14482742fd88SAlan Cox 		 * mode the driver will cause the new firmware to be copied
14492742fd88SAlan Cox 		 * from the UMP Ram to I2C and the firmware will update the
14502742fd88SAlan Cox 		 * record type from 0xf2 to 0x02.
14512742fd88SAlan Cox 		 *
14521da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14531da177e4SLinus Torvalds 		 * or could we do this in place!
14541da177e4SLinus Torvalds 		 */
14551da177e4SLinus Torvalds 
14562742fd88SAlan Cox 		/* Allocate a 15.5k buffer + 3 byte header */
14572742fd88SAlan Cox 		buffer_size = (((1024 * 16) - 512) +
14582742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header));
14591da177e4SLinus Torvalds 		buffer = kmalloc(buffer_size, GFP_KERNEL);
146010c642d0SJohan Hovold 		if (!buffer)
14611da177e4SLinus Torvalds 			return -ENOMEM;
14621da177e4SLinus Torvalds 
14632742fd88SAlan Cox 		/* Initialize the buffer to 0xff (pad the buffer) */
14641da177e4SLinus Torvalds 		memset(buffer, 0xff, buffer_size);
1465d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
14661da177e4SLinus Torvalds 
14672742fd88SAlan Cox 		for (i = sizeof(struct ti_i2c_image_header);
14682742fd88SAlan Cox 				i < buffer_size; i++) {
146935aeb1b3SJohan Hovold 			cs = (u8)(cs + buffer[i]);
14701da177e4SLinus Torvalds 		}
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14731da177e4SLinus Torvalds 
14742742fd88SAlan Cox 		/* update length and checksum after padding */
147535aeb1b3SJohan Hovold 		header->Length = cpu_to_le16((u16)(buffer_size -
14762742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header)));
14771da177e4SLinus Torvalds 		header->CheckSum = cs;
14781da177e4SLinus Torvalds 
14792742fd88SAlan Cox 		/* Download the operational code  */
1480c0e34831SPeter E. Berger 		dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1481c0e34831SPeter E. Berger 				__func__,
1482c0e34831SPeter E. Berger 				fw_hdr->major_version, fw_hdr->minor_version);
14832742fd88SAlan Cox 		status = download_code(serial, buffer, buffer_size);
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		kfree(buffer);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 		if (status) {
148867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
14891da177e4SLinus Torvalds 			return status;
14901da177e4SLinus Torvalds 		}
14911da177e4SLinus Torvalds 
14922742fd88SAlan Cox 		/* Device will reboot */
14931da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14941da177e4SLinus Torvalds 
149567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
14961da177e4SLinus Torvalds 
1497e35d6d7cSJohan Hovold 		return 1;
14981da177e4SLinus Torvalds 	}
14991da177e4SLinus Torvalds 
15002742fd88SAlan Cox stayinbootmode:
15012742fd88SAlan Cox 	/* Eprom is invalid or blank stay in boot mode */
150267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
15031da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
15041da177e4SLinus Torvalds 
15054f9785ccSJohan Hovold 	return 1;
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
ti_do_config(struct edgeport_port * port,int feature,int on)15082742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on)
15091da177e4SLinus Torvalds {
15102742fd88SAlan Cox 	on = !!on;	/* 1 or 0 not bitmask */
15117a14fac0SJohan Hovold 
15127a14fac0SJohan Hovold 	return send_port_cmd(port->port, feature, on, NULL, 0);
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
restore_mcr(struct edgeport_port * port,u8 mcr)151535aeb1b3SJohan Hovold static int restore_mcr(struct edgeport_port *port, u8 mcr)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	int status = 0;
15181da177e4SLinus Torvalds 
151967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
15201da177e4SLinus Torvalds 
15212742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
15221da177e4SLinus Torvalds 	if (status)
15231da177e4SLinus Torvalds 		return status;
15242742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
15251da177e4SLinus Torvalds 	if (status)
15261da177e4SLinus Torvalds 		return status;
15272742fd88SAlan Cox 	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
map_line_status(u8 ti_lsr)153135aeb1b3SJohan Hovold static u8 map_line_status(u8 ti_lsr)
15321da177e4SLinus Torvalds {
153335aeb1b3SJohan Hovold 	u8 lsr = 0;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
15361da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
15371da177e4SLinus Torvalds 		lsr |= flagUart;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
15401da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
15411da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
15421da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
15432742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
15442742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds #undef MAP_FLAG
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	return lsr;
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
handle_new_msr(struct edgeport_port * edge_port,u8 msr)155135aeb1b3SJohan Hovold static void handle_new_msr(struct edgeport_port *edge_port, u8 msr)
15521da177e4SLinus Torvalds {
15531da177e4SLinus Torvalds 	struct async_icount *icount;
15541da177e4SLinus Torvalds 	struct tty_struct *tty;
15551da177e4SLinus Torvalds 
155667e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
15571da177e4SLinus Torvalds 
15582742fd88SAlan Cox 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
15592742fd88SAlan Cox 			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1560cf9a9d66SJohan Hovold 		icount = &edge_port->port->icount;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 		/* update input line counters */
15631da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
15641da177e4SLinus Torvalds 			icount->cts++;
15651da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
15661da177e4SLinus Torvalds 			icount->dsr++;
15671da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
15681da177e4SLinus Torvalds 			icount->dcd++;
15691da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
15701da177e4SLinus Torvalds 			icount->rng++;
157148ee5801SJohan Hovold 		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	/* Save the new modem status */
15751da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
15761da177e4SLinus Torvalds 
15774a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
15781da177e4SLinus Torvalds 	/* handle CTS flow control */
15791da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
1580d95e3caeSPeter Hurley 		if (msr & EDGEPORT_MSR_CTS)
15811da177e4SLinus Torvalds 			tty_wakeup(tty);
15821da177e4SLinus Torvalds 	}
15834a90f09bSAlan Cox 	tty_kref_put(tty);
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
handle_new_lsr(struct edgeport_port * edge_port,int lsr_data,u8 lsr,u8 data)15862742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
158735aeb1b3SJohan Hovold 							u8 lsr, u8 data)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct async_icount *icount;
159035aeb1b3SJohan Hovold 	u8 new_lsr = (u8)(lsr & (u8)(LSR_OVER_ERR | LSR_PAR_ERR |
15912742fd88SAlan Cox 						LSR_FRM_ERR | LSR_BREAK));
15921da177e4SLinus Torvalds 
159367e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
15961da177e4SLinus Torvalds 
15972742fd88SAlan Cox 	if (new_lsr & LSR_BREAK)
15981da177e4SLinus Torvalds 		/*
15991da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
16001da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
16011da177e4SLinus Torvalds 		 */
160235aeb1b3SJohan Hovold 		new_lsr &= (u8)(LSR_OVER_ERR | LSR_BREAK);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
16052e124b4aSJiri Slaby 	if (lsr_data)
16062e124b4aSJiri Slaby 		edge_tty_recv(edge_port->port, &data, 1);
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	/* update input line counters */
1609cf9a9d66SJohan Hovold 	icount = &edge_port->port->icount;
16101da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
16111da177e4SLinus Torvalds 		icount->brk++;
16121da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
16131da177e4SLinus Torvalds 		icount->overrun++;
16141da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
16151da177e4SLinus Torvalds 		icount->parity++;
16161da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
16171da177e4SLinus Torvalds 		icount->frame++;
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
edge_interrupt_callback(struct urb * urb)16207d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb)
16211da177e4SLinus Torvalds {
1622cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
16231da177e4SLinus Torvalds 	struct usb_serial_port *port;
16241da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
162567e6da70SGreg Kroah-Hartman 	struct device *dev;
16261da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
16271da177e4SLinus Torvalds 	int length = urb->actual_length;
16281da177e4SLinus Torvalds 	int port_number;
16291da177e4SLinus Torvalds 	int function;
1630ee337c21SGreg Kroah-Hartman 	int retval;
163135aeb1b3SJohan Hovold 	u8 lsr;
163235aeb1b3SJohan Hovold 	u8 msr;
1633ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16341da177e4SLinus Torvalds 
1635ee337c21SGreg Kroah-Hartman 	switch (status) {
16361da177e4SLinus Torvalds 	case 0:
16371da177e4SLinus Torvalds 		/* success */
16381da177e4SLinus Torvalds 		break;
16391da177e4SLinus Torvalds 	case -ECONNRESET:
16401da177e4SLinus Torvalds 	case -ENOENT:
16411da177e4SLinus Torvalds 	case -ESHUTDOWN:
16421da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
164367e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1644441b62c1SHarvey Harrison 		    __func__, status);
16451da177e4SLinus Torvalds 		return;
16461da177e4SLinus Torvalds 	default:
1647ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1648441b62c1SHarvey Harrison 			"%d\n", __func__, status);
16491da177e4SLinus Torvalds 		goto exit;
16501da177e4SLinus Torvalds 	}
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	if (!length) {
165367e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
16541da177e4SLinus Torvalds 		goto exit;
16551da177e4SLinus Torvalds 	}
16561da177e4SLinus Torvalds 
165767e6da70SGreg Kroah-Hartman 	dev = &edge_serial->serial->dev->dev;
165859d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, length, data);
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	if (length != 2) {
166167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
16621da177e4SLinus Torvalds 		goto exit;
16631da177e4SLinus Torvalds 	}
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
16661da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
166767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
166867e6da70SGreg Kroah-Hartman 		port_number, function, data[1]);
16690b1d250aSJohan Hovold 
16700b1d250aSJohan Hovold 	if (port_number >= edge_serial->serial->num_ports) {
16710b1d250aSJohan Hovold 		dev_err(dev, "bad port number %d\n", port_number);
16720b1d250aSJohan Hovold 		goto exit;
16730b1d250aSJohan Hovold 	}
16740b1d250aSJohan Hovold 
16751da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
16761da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
16771da177e4SLinus Torvalds 	if (!edge_port) {
167867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - edge_port not found\n", __func__);
16791da177e4SLinus Torvalds 		return;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds 	switch (function) {
16821da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
16832742fd88SAlan Cox 		lsr = map_line_status(data[1]);
16841da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
168575899513SPeter E. Berger 			/*
168675899513SPeter E. Berger 			 * Save the LSR event for bulk read completion routine
168775899513SPeter E. Berger 			 */
168867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1689441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16901da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
16911da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
16921da177e4SLinus Torvalds 		} else {
169367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1694441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16951da177e4SLinus Torvalds 			handle_new_lsr(edge_port, 0, lsr, 0);
16961da177e4SLinus Torvalds 		}
16971da177e4SLinus Torvalds 		break;
16981da177e4SLinus Torvalds 
16992742fd88SAlan Cox 	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
17001da177e4SLinus Torvalds 		/* Copy MSR from UMP */
17011da177e4SLinus Torvalds 		msr = data[1];
170267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1703441b62c1SHarvey Harrison 			__func__, port_number, msr);
17041da177e4SLinus Torvalds 		handle_new_msr(edge_port, msr);
17051da177e4SLinus Torvalds 		break;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	default:
17082742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17092742fd88SAlan Cox 			"%s - Unknown Interrupt code from UMP %x\n",
1710441b62c1SHarvey Harrison 			__func__, data[1]);
17111da177e4SLinus Torvalds 		break;
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	}
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds exit:
1716ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1717ee337c21SGreg Kroah-Hartman 	if (retval)
17182742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17192742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1720441b62c1SHarvey Harrison 			 __func__, retval);
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
edge_bulk_in_callback(struct urb * urb)17237d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb)
17241da177e4SLinus Torvalds {
1725cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
172667e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
17271da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17286778b0cbSJohn Ogness 	unsigned long flags;
1729ee337c21SGreg Kroah-Hartman 	int retval = 0;
17301da177e4SLinus Torvalds 	int port_number;
1731ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17321da177e4SLinus Torvalds 
1733ee337c21SGreg Kroah-Hartman 	switch (status) {
17341da177e4SLinus Torvalds 	case 0:
17351da177e4SLinus Torvalds 		/* success */
17361da177e4SLinus Torvalds 		break;
17371da177e4SLinus Torvalds 	case -ECONNRESET:
17381da177e4SLinus Torvalds 	case -ENOENT:
17391da177e4SLinus Torvalds 	case -ESHUTDOWN:
17401da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
174167e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
17421da177e4SLinus Torvalds 		return;
17431da177e4SLinus Torvalds 	default:
174467e6da70SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 
1747ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
17481da177e4SLinus Torvalds 		goto exit;
17491da177e4SLinus Torvalds 
1750ee337c21SGreg Kroah-Hartman 	if (status) {
1751441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
17521da177e4SLinus Torvalds 		return;
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 
17551143832eSGreg Kroah-Hartman 	port_number = edge_port->port->port_number;
17561da177e4SLinus Torvalds 
1757654b404fSJohan Hovold 	if (urb->actual_length > 0 && edge_port->lsr_event) {
17581da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
175967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1760441b62c1SHarvey Harrison 			__func__, port_number, edge_port->lsr_mask, *data);
17611da177e4SLinus Torvalds 		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
17621da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
17631da177e4SLinus Torvalds 		--urb->actual_length;
17641da177e4SLinus Torvalds 		++data;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 
17672e124b4aSJiri Slaby 	if (urb->actual_length) {
176859d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
17692742fd88SAlan Cox 		if (edge_port->close_pending)
177067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
17712742fd88SAlan Cox 								__func__);
17722742fd88SAlan Cox 		else
17732e124b4aSJiri Slaby 			edge_tty_recv(edge_port->port, data,
177405c7cd39SJiri Slaby 					urb->actual_length);
1775cf9a9d66SJohan Hovold 		edge_port->port->icount.rx += urb->actual_length;
17761da177e4SLinus Torvalds 	}
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds exit:
17791da177e4SLinus Torvalds 	/* continue read unless stopped */
17806778b0cbSJohn Ogness 	spin_lock_irqsave(&edge_port->ep_lock, flags);
17815833041fSJohan Hovold 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1782ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
17835833041fSJohan Hovold 	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
17841da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
17855833041fSJohan Hovold 
17866778b0cbSJohn Ogness 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1787ee337c21SGreg Kroah-Hartman 	if (retval)
178867e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
edge_tty_recv(struct usb_serial_port * port,unsigned char * data,int length)17912e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
17922e124b4aSJiri Slaby 		int length)
17931da177e4SLinus Torvalds {
17942742fd88SAlan Cox 	int queued;
17951da177e4SLinus Torvalds 
179605c7cd39SJiri Slaby 	queued = tty_insert_flip_string(&port->port, data, length);
17972742fd88SAlan Cox 	if (queued < length)
179805c7cd39SJiri Slaby 		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
17992742fd88SAlan Cox 			__func__, length - queued);
18002e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
edge_bulk_out_callback(struct urb * urb)18037d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb)
18041da177e4SLinus Torvalds {
1805cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
18061da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1807ee337c21SGreg Kroah-Hartman 	int status = urb->status;
18084a90f09bSAlan Cox 	struct tty_struct *tty;
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
18111da177e4SLinus Torvalds 
1812ee337c21SGreg Kroah-Hartman 	switch (status) {
18131da177e4SLinus Torvalds 	case 0:
18141da177e4SLinus Torvalds 		/* success */
18151da177e4SLinus Torvalds 		break;
18161da177e4SLinus Torvalds 	case -ECONNRESET:
18171da177e4SLinus Torvalds 	case -ENOENT:
18181da177e4SLinus Torvalds 	case -ESHUTDOWN:
18191da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
182067e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1821441b62c1SHarvey Harrison 		    __func__, status);
18221da177e4SLinus Torvalds 		return;
18231da177e4SLinus Torvalds 	default:
182422a416c4SJohan Hovold 		dev_err_console(port, "%s - nonzero write bulk status "
1825441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
18261da177e4SLinus Torvalds 	}
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	/* send any buffered data */
18294a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
1830ae3759c2SJiri Slaby 	edge_send(port, tty);
18314a90f09bSAlan Cox 	tty_kref_put(tty);
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
edge_open(struct tty_struct * tty,struct usb_serial_port * port)1834a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
18351da177e4SLinus Torvalds {
18361da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
18371da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
18381da177e4SLinus Torvalds 	struct usb_device *dev;
18391da177e4SLinus Torvalds 	struct urb *urb;
18401da177e4SLinus Torvalds 	int status;
18411da177e4SLinus Torvalds 	u16 open_settings;
18421da177e4SLinus Torvalds 	u8 transaction_timeout;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	if (edge_port == NULL)
18451da177e4SLinus Torvalds 		return -ENODEV;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	dev = port->serial->dev;
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	/* turn off loopback */
18502742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
18511da177e4SLinus Torvalds 	if (status) {
18522742fd88SAlan Cox 		dev_err(&port->dev,
18532742fd88SAlan Cox 				"%s - cannot send clear loopback command, %d\n",
1854441b62c1SHarvey Harrison 			__func__, status);
18551da177e4SLinus Torvalds 		return status;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	/* set up the port settings */
185995da310eSAlan Cox 	if (tty)
1860adc8d746SAlan Cox 		edge_set_termios(tty, port, &tty->termios);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	/* open up the port */
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
18651da177e4SLinus Torvalds 	transaction_timeout = 2;
18661da177e4SLinus Torvalds 
18672742fd88SAlan Cox 	edge_port->ump_read_timeout =
18682742fd88SAlan Cox 				max(20, ((transaction_timeout * 3) / 2));
18691da177e4SLinus Torvalds 
18702742fd88SAlan Cox 	/* milliseconds to timeout for DMA transfer */
18711da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
18721da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
18731da177e4SLinus Torvalds 			     (transaction_timeout << 2));
18741da177e4SLinus Torvalds 
187567e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
18787a14fac0SJohan Hovold 	status = send_port_cmd(port, UMPC_OPEN_PORT, 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? */
18867a14fac0SJohan Hovold 	status = send_port_cmd(port, UMPC_START_PORT, 0, NULL, 0);
18871da177e4SLinus Torvalds 	if (status) {
18882742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
18892742fd88SAlan Cox 							__func__, status);
18901da177e4SLinus Torvalds 		return status;
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
18942742fd88SAlan Cox 	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
18951da177e4SLinus Torvalds 	if (status) {
18962742fd88SAlan Cox 		dev_err(&port->dev,
18972742fd88SAlan Cox 			"%s - cannot send clear buffers command, %d\n",
18982742fd88SAlan Cox 			__func__, status);
18991da177e4SLinus Torvalds 		return status;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	/* Read Initial MSR */
190313c61339SJohan Hovold 	status = read_port_cmd(port, UMPC_READ_MSR, 0, &edge_port->shadow_msr, 1);
19041da177e4SLinus Torvalds 	if (status) {
19052742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
19062742fd88SAlan Cox 							__func__, status);
19071da177e4SLinus Torvalds 		return status;
19081da177e4SLinus Torvalds 	}
19091da177e4SLinus Torvalds 
191067e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	/* Set Initial MCR */
19131da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
191467e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
1917241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
19181da177e4SLinus Torvalds 		return -ERESTARTSYS;
19191da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
19202742fd88SAlan Cox 		/* we are the first port to open, post the interrupt urb */
19211da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
19221da177e4SLinus Torvalds 		urb->context = edge_serial;
19231da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
19241da177e4SLinus Torvalds 		if (status) {
19252742fd88SAlan Cox 			dev_err(&port->dev,
19262742fd88SAlan Cox 				"%s - usb_submit_urb failed with value %d\n",
19272742fd88SAlan Cox 					__func__, status);
1928241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19291da177e4SLinus Torvalds 		}
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	/*
19331da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
19341da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
19351da177e4SLinus Torvalds 	 */
19361da177e4SLinus Torvalds 	usb_clear_halt(dev, port->write_urb->pipe);
19371da177e4SLinus Torvalds 	usb_clear_halt(dev, port->read_urb->pipe);
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	/* start up our bulk read urb */
19401da177e4SLinus Torvalds 	urb = port->read_urb;
19411da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
19421da177e4SLinus Torvalds 	urb->context = edge_port;
19431da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_KERNEL);
19441da177e4SLinus Torvalds 	if (status) {
19452742fd88SAlan Cox 		dev_err(&port->dev,
19462742fd88SAlan Cox 			"%s - read bulk usb_submit_urb failed with value %d\n",
19472742fd88SAlan Cox 				__func__, status);
19481da177e4SLinus Torvalds 		goto unlink_int_urb;
19491da177e4SLinus Torvalds 	}
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
19521da177e4SLinus Torvalds 
1953241ca64fSMatthias Kaehlcke 	goto release_es_lock;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds unlink_int_urb:
19561da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
19571da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1958241ca64fSMatthias Kaehlcke release_es_lock:
1959241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
19601da177e4SLinus Torvalds 	return status;
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
edge_close(struct usb_serial_port * port)1963335f8514SAlan Cox static void edge_close(struct usb_serial_port *port)
19641da177e4SLinus Torvalds {
19651da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19661da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
196777de2518SJohan Hovold 	unsigned long flags;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
19701da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
19712742fd88SAlan Cox 	if (edge_serial == NULL || edge_port == NULL)
19721da177e4SLinus Torvalds 		return;
19731da177e4SLinus Torvalds 
197475899513SPeter E. Berger 	/*
197575899513SPeter E. Berger 	 * The bulkreadcompletion routine will check
197675899513SPeter E. Berger 	 * this flag and dump add read data
197775899513SPeter E. Berger 	 */
19781da177e4SLinus Torvalds 	edge_port->close_pending = 1;
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
19811da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
19821da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
198377de2518SJohan Hovold 	spin_lock_irqsave(&edge_port->ep_lock, flags);
1984ddca16e4SJohan Hovold 	kfifo_reset_out(&port->write_fifo);
198577de2518SJohan Hovold 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19861da177e4SLinus Torvalds 
198767e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
19887a14fac0SJohan Hovold 	send_port_cmd(port, UMPC_CLOSE_PORT, 0, NULL, 0);
1989af581059SJohan Hovold 
1990241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
19911da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
19921da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
19931da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
19941da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
19951da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
19961da177e4SLinus Torvalds 	}
1997241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
19981da177e4SLinus Torvalds 	edge_port->close_pending = 0;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds 
edge_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * data,int count)200195da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
200295da310eSAlan Cox 				const unsigned char *data, int count)
20031da177e4SLinus Torvalds {
20041da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	if (count == 0) {
200767e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
20081da177e4SLinus Torvalds 		return 0;
20091da177e4SLinus Torvalds 	}
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	if (edge_port == NULL)
20121da177e4SLinus Torvalds 		return -ENODEV;
20131da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20141da177e4SLinus Torvalds 		return -ENODEV;
20151da177e4SLinus Torvalds 
2016ddca16e4SJohan Hovold 	count = kfifo_in_locked(&port->write_fifo, data, count,
2017d733cec1SJohan Hovold 							&edge_port->ep_lock);
2018ae3759c2SJiri Slaby 	edge_send(port, tty);
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	return count;
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds 
edge_send(struct usb_serial_port * port,struct tty_struct * tty)2023ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
20241da177e4SLinus Torvalds {
20251da177e4SLinus Torvalds 	int count, result;
20261da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20271da177e4SLinus Torvalds 	unsigned long flags;
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
20321da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20331da177e4SLinus Torvalds 		return;
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds 
2036ddca16e4SJohan Hovold 	count = kfifo_out(&port->write_fifo,
20371da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
20381da177e4SLinus Torvalds 				port->bulk_out_size);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	if (count == 0) {
20411da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20421da177e4SLinus Torvalds 		return;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20481da177e4SLinus Torvalds 
204959d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	/* set up our urb */
2052fd11961aSJohan Hovold 	port->write_urb->transfer_buffer_length = count;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	/* send the data out the bulk port */
20551da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
20561da177e4SLinus Torvalds 	if (result) {
205722a416c4SJohan Hovold 		dev_err_console(port,
20582742fd88SAlan Cox 			"%s - failed submitting write urb, error %d\n",
20592742fd88SAlan Cox 				__func__, result);
20601da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
20612742fd88SAlan Cox 		/* TODO: reschedule edge_send */
20622742fd88SAlan Cox 	} else
2063cf9a9d66SJohan Hovold 		edge_port->port->icount.tx += count;
20641da177e4SLinus Torvalds 
206575899513SPeter E. Berger 	/*
206675899513SPeter E. Berger 	 * wakeup any process waiting for writes to complete
206775899513SPeter E. Berger 	 * there is now more room in the buffer for new writes
206875899513SPeter E. Berger 	 */
20692742fd88SAlan Cox 	if (tty)
20701da177e4SLinus Torvalds 		tty_wakeup(tty);
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
edge_write_room(struct tty_struct * tty)207394cc7aeaSJiri Slaby static unsigned int edge_write_room(struct tty_struct *tty)
20741da177e4SLinus Torvalds {
207595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20761da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
207794cc7aeaSJiri Slaby 	unsigned int room;
20781da177e4SLinus Torvalds 	unsigned long flags;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	if (edge_port == NULL)
20812742fd88SAlan Cox 		return 0;
20821da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20832742fd88SAlan Cox 		return 0;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2086ddca16e4SJohan Hovold 	room = kfifo_avail(&port->write_fifo);
20871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20881da177e4SLinus Torvalds 
208994cc7aeaSJiri Slaby 	dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
20901da177e4SLinus Torvalds 	return room;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
edge_chars_in_buffer(struct tty_struct * tty)2093155591d3SJiri Slaby static unsigned int edge_chars_in_buffer(struct tty_struct *tty)
20941da177e4SLinus Torvalds {
209595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20961da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2097155591d3SJiri Slaby 	unsigned int chars;
20981da177e4SLinus Torvalds 	unsigned long flags;
20991da177e4SLinus Torvalds 	if (edge_port == NULL)
21002742fd88SAlan Cox 		return 0;
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2103ddca16e4SJohan Hovold 	chars = kfifo_len(&port->write_fifo);
21041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21051da177e4SLinus Torvalds 
2106155591d3SJiri Slaby 	dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
21071da177e4SLinus Torvalds 	return chars;
21081da177e4SLinus Torvalds }
21091da177e4SLinus Torvalds 
edge_tx_empty(struct usb_serial_port * port)2110b16634adSJohan Hovold static bool edge_tx_empty(struct usb_serial_port *port)
2111b16634adSJohan Hovold {
2112b16634adSJohan Hovold 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2113b16634adSJohan Hovold 	int ret;
2114b16634adSJohan Hovold 
2115b16634adSJohan Hovold 	ret = tx_active(edge_port);
2116b16634adSJohan Hovold 	if (ret > 0)
2117b16634adSJohan Hovold 		return false;
2118b16634adSJohan Hovold 
2119b16634adSJohan Hovold 	return true;
2120b16634adSJohan Hovold }
2121b16634adSJohan Hovold 
edge_throttle(struct tty_struct * tty)212295da310eSAlan Cox static void edge_throttle(struct tty_struct *tty)
21231da177e4SLinus Torvalds {
212495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21251da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21261da177e4SLinus Torvalds 	int status;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	if (edge_port == NULL)
21291da177e4SLinus Torvalds 		return;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
21321da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21331da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
213495da310eSAlan Cox 		status = edge_write(tty, port, &stop_char, 1);
213595da310eSAlan Cox 		if (status <= 0) {
213695da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
213795da310eSAlan Cox 		}
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds 
214075899513SPeter E. Berger 	/*
214175899513SPeter E. Berger 	 * if we are implementing RTS/CTS, stop reads
214275899513SPeter E. Berger 	 * and the Edgeport will clear the RTS line
214375899513SPeter E. Berger 	 */
21441da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
21451da177e4SLinus Torvalds 		stop_read(edge_port);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
edge_unthrottle(struct tty_struct * tty)214995da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty)
21501da177e4SLinus Torvalds {
215195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21521da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21531da177e4SLinus Torvalds 	int status;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	if (edge_port == NULL)
21561da177e4SLinus Torvalds 		return;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
21591da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21601da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
216195da310eSAlan Cox 		status = edge_write(tty, port, &start_char, 1);
216295da310eSAlan Cox 		if (status <= 0) {
216395da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
216495da310eSAlan Cox 		}
21651da177e4SLinus Torvalds 	}
216675899513SPeter E. Berger 	/*
216775899513SPeter E. Berger 	 * if we are implementing RTS/CTS, restart reads
216875899513SPeter E. Berger 	 * are the Edgeport will assert the RTS line
216975899513SPeter E. Berger 	 */
21701da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
21711da177e4SLinus Torvalds 		status = restart_read(edge_port);
21721da177e4SLinus Torvalds 		if (status)
21732742fd88SAlan Cox 			dev_err(&port->dev,
21742742fd88SAlan Cox 				"%s - read bulk usb_submit_urb failed: %d\n",
21752742fd88SAlan Cox 							__func__, status);
21761da177e4SLinus Torvalds 	}
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
stop_read(struct edgeport_port * edge_port)21801da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
21811da177e4SLinus Torvalds {
21821da177e4SLinus Torvalds 	unsigned long flags;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
21871da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
21881da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
restart_read(struct edgeport_port * edge_port)21931da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
21941da177e4SLinus Torvalds {
21951da177e4SLinus Torvalds 	struct urb *urb;
21961da177e4SLinus Torvalds 	int status = 0;
21971da177e4SLinus Torvalds 	unsigned long flags;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
22021da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
2203efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
22041da177e4SLinus Torvalds 	}
22051da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
22061da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	return status;
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds 
change_port_settings(struct tty_struct * tty,struct edgeport_port * edge_port,const struct ktermios * old_termios)221395da310eSAlan Cox static void change_port_settings(struct tty_struct *tty,
2214f6d47fe5SIlpo Järvinen 		struct edgeport_port *edge_port, const struct ktermios *old_termios)
22151da177e4SLinus Torvalds {
221667e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
22171da177e4SLinus Torvalds 	struct ump_uart_config *config;
22181da177e4SLinus Torvalds 	int baud;
22191da177e4SLinus Torvalds 	unsigned cflag;
22201da177e4SLinus Torvalds 	int status;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
22231da177e4SLinus Torvalds 	if (!config) {
2224adc8d746SAlan Cox 		tty->termios = *old_termios;
22251da177e4SLinus Torvalds 		return;
22261da177e4SLinus Torvalds 	}
22271da177e4SLinus Torvalds 
2228adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	config->wFlags = 0;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/* These flags must be set */
22331da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
22341da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
223535aeb1b3SJohan Hovold 	config->bUartMode = (u8)(edge_port->bUartMode);
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
22381da177e4SLinus Torvalds 	case CS5:
22391da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR5BITS;
224067e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
22411da177e4SLinus Torvalds 		    break;
22421da177e4SLinus Torvalds 	case CS6:
22431da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR6BITS;
224467e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
22451da177e4SLinus Torvalds 		    break;
22461da177e4SLinus Torvalds 	case CS7:
22471da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR7BITS;
224867e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
22491da177e4SLinus Torvalds 		    break;
22501da177e4SLinus Torvalds 	default:
22511da177e4SLinus Torvalds 	case CS8:
22521da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR8BITS;
225367e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
22541da177e4SLinus Torvalds 			    break;
22551da177e4SLinus Torvalds 	}
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	if (cflag & PARENB) {
22581da177e4SLinus Torvalds 		if (cflag & PARODD) {
22591da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22601da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
226167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = odd\n", __func__);
22621da177e4SLinus Torvalds 		} else {
22631da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22641da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
226567e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = even\n", __func__);
22661da177e4SLinus Torvalds 		}
22671da177e4SLinus Torvalds 	} else {
22681da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
226967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = none\n", __func__);
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
22731da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
227467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
22751da177e4SLinus Torvalds 	} else {
22761da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
227767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	/* figure out the flow control settings */
22811da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
22821da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
22831da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
228467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
22851da177e4SLinus Torvalds 	} else {
228667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
22871da177e4SLinus Torvalds 		restart_read(edge_port);
22881da177e4SLinus Torvalds 	}
22891da177e4SLinus Torvalds 
229075899513SPeter E. Berger 	/*
229175899513SPeter E. Berger 	 * if we are implementing XON/XOFF, set the start and stop
229275899513SPeter E. Berger 	 * character in the device
229375899513SPeter E. Berger 	 */
22941da177e4SLinus Torvalds 	config->cXon  = START_CHAR(tty);
22951da177e4SLinus Torvalds 	config->cXoff = STOP_CHAR(tty);
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 	/* if we are implementing INBOUND XON/XOFF */
22981da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22991da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
230067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2301441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
23022742fd88SAlan Cox 	} else
230367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	/* if we are implementing OUTBOUND XON/XOFF */
23061da177e4SLinus Torvalds 	if (I_IXON(tty)) {
23071da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
230867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND 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 - OUTBOUND XON/XOFF is disabled\n", __func__);
23121da177e4SLinus Torvalds 
2313adc8d746SAlan Cox 	tty->termios.c_cflag &= ~CMSPAR;
2314d5f5bcd4SAlan Cox 
23151da177e4SLinus Torvalds 	/* Round the baud rate */
23161da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
23171da177e4SLinus Torvalds 	if (!baud) {
23181da177e4SLinus Torvalds 		/* pick a default, any default... */
23191da177e4SLinus Torvalds 		baud = 9600;
23206aeb75e6SJohan Hovold 	} else {
23216aeb75e6SJohan Hovold 		/* Avoid a zero divisor. */
23226aeb75e6SJohan Hovold 		baud = min(baud, 461550);
2323d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
23246aeb75e6SJohan Hovold 	}
2325d5f5bcd4SAlan Cox 
23261da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
232735aeb1b3SJohan Hovold 	config->wBaudRate = (u16)((461550L + baud/2) / baud);
23281da177e4SLinus Torvalds 
2329d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2330d5f5bcd4SAlan Cox 
233167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
23321da177e4SLinus Torvalds 
233367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate:   %d\n", (int)(461550L / config->wBaudRate));
233467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wFlags:    0x%x\n", config->wFlags);
233567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bDataBits:   %d\n", config->bDataBits);
233667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bParity:     %d\n", config->bParity);
233767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bStopBits:   %d\n", config->bStopBits);
233867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXon:        %d\n", config->cXon);
233967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXoff:       %d\n", config->cXoff);
234067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bUartMode:   %d\n", config->bUartMode);
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	/* move the word values into big endian mode */
23431da177e4SLinus Torvalds 	cpu_to_be16s(&config->wFlags);
23441da177e4SLinus Torvalds 	cpu_to_be16s(&config->wBaudRate);
23451da177e4SLinus Torvalds 
23467a14fac0SJohan Hovold 	status = send_port_cmd(edge_port->port, UMPC_SET_CONFIG, 0, config,
23477a14fac0SJohan Hovold 			sizeof(*config));
23482742fd88SAlan Cox 	if (status)
234967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2350441b62c1SHarvey Harrison 			__func__, status);
23511da177e4SLinus Torvalds 	kfree(config);
23521da177e4SLinus Torvalds }
23531da177e4SLinus Torvalds 
edge_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)235495da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
2355f6d47fe5SIlpo Järvinen 			     struct usb_serial_port *port,
2356f6d47fe5SIlpo Järvinen 			     const struct ktermios *old_termios)
23571da177e4SLinus Torvalds {
23581da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 	if (edge_port == NULL)
23611da177e4SLinus Torvalds 		return;
23621da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
236395da310eSAlan Cox 	change_port_settings(tty, edge_port, old_termios);
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds 
edge_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)236620b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty,
23672742fd88SAlan Cox 					unsigned int set, unsigned int clear)
23681da177e4SLinus Torvalds {
236995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23701da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23711da177e4SLinus Torvalds 	unsigned int mcr;
23723d71fe0bSAlan Cox 	unsigned long flags;
23731da177e4SLinus Torvalds 
23743d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23751da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
23761da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
23771da177e4SLinus Torvalds 		mcr |= MCR_RTS;
23781da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
23791da177e4SLinus Torvalds 		mcr |= MCR_DTR;
23801da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
23811da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
23841da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
23851da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
23861da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
23871da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
23881da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
23913d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23921da177e4SLinus Torvalds 
23932742fd88SAlan Cox 	restore_mcr(edge_port, mcr);
23941da177e4SLinus Torvalds 	return 0;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
edge_tiocmget(struct tty_struct * tty)239760b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty)
23981da177e4SLinus Torvalds {
239995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24001da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24011da177e4SLinus Torvalds 	unsigned int result = 0;
24021da177e4SLinus Torvalds 	unsigned int msr;
24031da177e4SLinus Torvalds 	unsigned int mcr;
24043d71fe0bSAlan Cox 	unsigned long flags;
24051da177e4SLinus Torvalds 
24063d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
24073d71fe0bSAlan Cox 
24081da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
24091da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
24101da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
24111da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
24121da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
24131da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
24141da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
24151da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 
241867e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
24193d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	return result;
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
edge_break(struct tty_struct * tty,int break_state)2424*6ff58ae1SJohan Hovold static int edge_break(struct tty_struct *tty, int break_state)
24251da177e4SLinus Torvalds {
242695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24271da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24281da177e4SLinus Torvalds 	int status;
24292742fd88SAlan Cox 	int bv = 0;	/* Off */
24301da177e4SLinus Torvalds 
243195da310eSAlan Cox 	if (break_state == -1)
24322742fd88SAlan Cox 		bv = 1;	/* On */
2433*6ff58ae1SJohan Hovold 
24342742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2435*6ff58ae1SJohan Hovold 	if (status) {
243667e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2437441b62c1SHarvey Harrison 			__func__, status);
2438*6ff58ae1SJohan Hovold 		return status;
2439*6ff58ae1SJohan Hovold 	}
2440*6ff58ae1SJohan Hovold 
2441*6ff58ae1SJohan Hovold 	return 0;
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
edge_heartbeat_schedule(struct edgeport_serial * edge_serial)244426c78daaSPeter E. Berger static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial)
244526c78daaSPeter E. Berger {
244626c78daaSPeter E. Berger 	if (!edge_serial->use_heartbeat)
244726c78daaSPeter E. Berger 		return;
244826c78daaSPeter E. Berger 
244926c78daaSPeter E. Berger 	schedule_delayed_work(&edge_serial->heartbeat_work,
245026c78daaSPeter E. Berger 			FW_HEARTBEAT_SECS * HZ);
245126c78daaSPeter E. Berger }
245226c78daaSPeter E. Berger 
edge_heartbeat_work(struct work_struct * work)245326c78daaSPeter E. Berger static void edge_heartbeat_work(struct work_struct *work)
245426c78daaSPeter E. Berger {
245526c78daaSPeter E. Berger 	struct edgeport_serial *serial;
245626c78daaSPeter E. Berger 	struct ti_i2c_desc *rom_desc;
245726c78daaSPeter E. Berger 
245826c78daaSPeter E. Berger 	serial = container_of(work, struct edgeport_serial,
245926c78daaSPeter E. Berger 			heartbeat_work.work);
246026c78daaSPeter E. Berger 
246126c78daaSPeter E. Berger 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
246226c78daaSPeter E. Berger 
246326c78daaSPeter E. Berger 	/* Descriptor address request is enough to reset the firmware timer */
246426c78daaSPeter E. Berger 	if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
246526c78daaSPeter E. Berger 			rom_desc)) {
246626c78daaSPeter E. Berger 		dev_err(&serial->serial->interface->dev,
246726c78daaSPeter E. Berger 				"%s - Incomplete heartbeat\n", __func__);
246826c78daaSPeter E. Berger 	}
246926c78daaSPeter E. Berger 	kfree(rom_desc);
247026c78daaSPeter E. Berger 
247126c78daaSPeter E. Berger 	edge_heartbeat_schedule(serial);
247226c78daaSPeter E. Berger }
247326c78daaSPeter E. Berger 
edge_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)247449f4ff2dSJohan Hovold static int edge_calc_num_ports(struct usb_serial *serial,
247549f4ff2dSJohan Hovold 				struct usb_serial_endpoints *epds)
247649f4ff2dSJohan Hovold {
247749f4ff2dSJohan Hovold 	struct device *dev = &serial->interface->dev;
247849f4ff2dSJohan Hovold 	unsigned char num_ports = serial->type->num_ports;
247949f4ff2dSJohan Hovold 
248049f4ff2dSJohan Hovold 	/* Make sure we have the required endpoints when in download mode. */
248149f4ff2dSJohan Hovold 	if (serial->interface->cur_altsetting->desc.bNumEndpoints > 1) {
248249f4ff2dSJohan Hovold 		if (epds->num_bulk_in < num_ports ||
24839c8299b4SJohan Hovold 				epds->num_bulk_out < num_ports ||
24849c8299b4SJohan Hovold 				epds->num_interrupt_in < 1) {
248549f4ff2dSJohan Hovold 			dev_err(dev, "required endpoints missing\n");
248649f4ff2dSJohan Hovold 			return -ENODEV;
248749f4ff2dSJohan Hovold 		}
248849f4ff2dSJohan Hovold 	}
248949f4ff2dSJohan Hovold 
249049f4ff2dSJohan Hovold 	return num_ports;
249149f4ff2dSJohan Hovold }
249249f4ff2dSJohan Hovold 
edge_startup(struct usb_serial * serial)24931da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial)
24941da177e4SLinus Torvalds {
24951da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
24961da177e4SLinus Torvalds 	int status;
249726c78daaSPeter E. Berger 	u16 product_id;
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	/* create our private serial structure */
250080b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
250110c642d0SJohan Hovold 	if (!edge_serial)
25021da177e4SLinus Torvalds 		return -ENOMEM;
250310c642d0SJohan Hovold 
2504241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
25051da177e4SLinus Torvalds 	edge_serial->serial = serial;
25064f9785ccSJohan Hovold 	INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work);
25071da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
25081da177e4SLinus Torvalds 
2509b4146162SPeter E. Berger 	status = download_fw(edge_serial);
25104f9785ccSJohan Hovold 	if (status < 0) {
25111da177e4SLinus Torvalds 		kfree(edge_serial);
25121da177e4SLinus Torvalds 		return status;
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 
25154f9785ccSJohan Hovold 	if (status > 0)
25164f9785ccSJohan Hovold 		return 1;	/* bind but do not register any ports */
25174f9785ccSJohan Hovold 
251826c78daaSPeter E. Berger 	product_id = le16_to_cpu(
251926c78daaSPeter E. Berger 			edge_serial->serial->dev->descriptor.idProduct);
252026c78daaSPeter E. Berger 
252126c78daaSPeter E. Berger 	/* Currently only the EP/416 models require heartbeat support */
252226c78daaSPeter E. Berger 	if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) {
252326c78daaSPeter E. Berger 		if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 ||
252426c78daaSPeter E. Berger 			product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) {
252526c78daaSPeter E. Berger 			edge_serial->use_heartbeat = true;
252626c78daaSPeter E. Berger 		}
252726c78daaSPeter E. Berger 	}
252826c78daaSPeter E. Berger 
252926c78daaSPeter E. Berger 	edge_heartbeat_schedule(edge_serial);
253026c78daaSPeter E. Berger 
25311da177e4SLinus Torvalds 	return 0;
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
edge_disconnect(struct usb_serial * serial)2534f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial)
25351da177e4SLinus Torvalds {
25362330d0a8SJohan Hovold 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
25372330d0a8SJohan Hovold 
25382330d0a8SJohan Hovold 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2539f9c99bb8SAlan Stern }
2540f9c99bb8SAlan Stern 
edge_release(struct usb_serial * serial)2541f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial)
2542f9c99bb8SAlan Stern {
254326c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
254426c78daaSPeter E. Berger 
254526c78daaSPeter E. Berger 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
254626c78daaSPeter E. Berger 	kfree(edge_serial);
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
edge_port_probe(struct usb_serial_port * port)254900361530SJohan Hovold static int edge_port_probe(struct usb_serial_port *port)
255000361530SJohan Hovold {
255100361530SJohan Hovold 	struct edgeport_port *edge_port;
255200361530SJohan Hovold 	int ret;
255300361530SJohan Hovold 
255400361530SJohan Hovold 	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
255500361530SJohan Hovold 	if (!edge_port)
255600361530SJohan Hovold 		return -ENOMEM;
255700361530SJohan Hovold 
255800361530SJohan Hovold 	spin_lock_init(&edge_port->ep_lock);
255900361530SJohan Hovold 	edge_port->port = port;
256000361530SJohan Hovold 	edge_port->edge_serial = usb_get_serial_data(port->serial);
256100361530SJohan Hovold 	edge_port->bUartMode = default_uart_mode;
256200361530SJohan Hovold 
25630fce06daSJohan Hovold 	switch (port->port_number) {
25640fce06daSJohan Hovold 	case 0:
25650fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART1;
25660fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
25670fce06daSJohan Hovold 		break;
25680fce06daSJohan Hovold 	case 1:
25690fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART2;
25700fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
25710fce06daSJohan Hovold 		break;
25720fce06daSJohan Hovold 	default:
25730fce06daSJohan Hovold 		dev_err(&port->dev, "unknown port number\n");
25740fce06daSJohan Hovold 		ret = -ENODEV;
25750fce06daSJohan Hovold 		goto err;
25760fce06daSJohan Hovold 	}
25770fce06daSJohan Hovold 
25780fce06daSJohan Hovold 	dev_dbg(&port->dev,
25790fce06daSJohan Hovold 		"%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
25800fce06daSJohan Hovold 		__func__, port->port_number, edge_port->uart_base,
25810fce06daSJohan Hovold 		edge_port->dma_address);
25820fce06daSJohan Hovold 
258300361530SJohan Hovold 	usb_set_serial_port_data(port, edge_port);
258400361530SJohan Hovold 
25855d8c61bcSJohan Hovold 	ret = edge_create_sysfs_attrs(port);
25860fce06daSJohan Hovold 	if (ret)
25870fce06daSJohan Hovold 		goto err;
25885d8c61bcSJohan Hovold 
25894b8e0795SJohan Hovold 	/*
25904b8e0795SJohan Hovold 	 * The LSR does not tell when the transmitter shift register has
25914b8e0795SJohan Hovold 	 * emptied so add a one-character drain delay.
25924b8e0795SJohan Hovold 	 */
2593d7be6221SJohan Hovold 	port->port.drain_delay = 1;
2594f40d7815SJohan Hovold 
259500361530SJohan Hovold 	return 0;
25960fce06daSJohan Hovold err:
25970fce06daSJohan Hovold 	kfree(edge_port);
25980fce06daSJohan Hovold 
25990fce06daSJohan Hovold 	return ret;
260000361530SJohan Hovold }
260100361530SJohan Hovold 
edge_port_remove(struct usb_serial_port * port)2602c5d1448fSUwe Kleine-König static void edge_port_remove(struct usb_serial_port *port)
260300361530SJohan Hovold {
260400361530SJohan Hovold 	struct edgeport_port *edge_port;
260500361530SJohan Hovold 
260600361530SJohan Hovold 	edge_port = usb_get_serial_port_data(port);
260700361530SJohan Hovold 	edge_remove_sysfs_attrs(port);
260800361530SJohan Hovold 	kfree(edge_port);
260900361530SJohan Hovold }
26101da177e4SLinus Torvalds 
2611fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2612fc4cbd75SMartin K. Petersen 
uart_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2613154547c4SGreg Kroah-Hartman static ssize_t uart_mode_show(struct device *dev,
2614fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2615fc4cbd75SMartin K. Petersen {
2616fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2617fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2618fc4cbd75SMartin K. Petersen 
2619fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2620fc4cbd75SMartin K. Petersen }
2621fc4cbd75SMartin K. Petersen 
uart_mode_store(struct device * dev,struct device_attribute * attr,const char * valbuf,size_t count)2622154547c4SGreg Kroah-Hartman static ssize_t uart_mode_store(struct device *dev,
2623fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2624fc4cbd75SMartin K. Petersen {
2625fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2626fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2627fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2628fc4cbd75SMartin K. Petersen 
262967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2630fc4cbd75SMartin K. Petersen 
2631fc4cbd75SMartin K. Petersen 	if (v < 256)
2632fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2633fc4cbd75SMartin K. Petersen 	else
2634441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2635fc4cbd75SMartin K. Petersen 
2636fc4cbd75SMartin K. Petersen 	return count;
2637fc4cbd75SMartin K. Petersen }
2638154547c4SGreg Kroah-Hartman static DEVICE_ATTR_RW(uart_mode);
2639fc4cbd75SMartin K. Petersen 
edge_create_sysfs_attrs(struct usb_serial_port * port)2640fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2641fc4cbd75SMartin K. Petersen {
2642fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2643fc4cbd75SMartin K. Petersen }
2644fc4cbd75SMartin K. Petersen 
edge_remove_sysfs_attrs(struct usb_serial_port * port)2645fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2646fc4cbd75SMartin K. Petersen {
2647fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2648fc4cbd75SMartin K. Petersen 	return 0;
2649fc4cbd75SMartin K. Petersen }
2650fc4cbd75SMartin K. Petersen 
265126c78daaSPeter E. Berger #ifdef CONFIG_PM
edge_suspend(struct usb_serial * serial,pm_message_t message)265226c78daaSPeter E. Berger static int edge_suspend(struct usb_serial *serial, pm_message_t message)
265326c78daaSPeter E. Berger {
265426c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
265526c78daaSPeter E. Berger 
265626c78daaSPeter E. Berger 	cancel_delayed_work_sync(&edge_serial->heartbeat_work);
265726c78daaSPeter E. Berger 
265826c78daaSPeter E. Berger 	return 0;
265926c78daaSPeter E. Berger }
266026c78daaSPeter E. Berger 
edge_resume(struct usb_serial * serial)266126c78daaSPeter E. Berger static int edge_resume(struct usb_serial *serial)
266226c78daaSPeter E. Berger {
266326c78daaSPeter E. Berger 	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
266426c78daaSPeter E. Berger 
266526c78daaSPeter E. Berger 	edge_heartbeat_schedule(edge_serial);
266626c78daaSPeter E. Berger 
266726c78daaSPeter E. Berger 	return 0;
266826c78daaSPeter E. Berger }
266926c78daaSPeter E. Berger #endif
2670fc4cbd75SMartin K. Petersen 
2671ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
267218fcac35SGreg Kroah-Hartman 	.driver = {
26731da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2674269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
267518fcac35SGreg Kroah-Hartman 	},
2676269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
26771da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
26781da177e4SLinus Torvalds 	.num_ports		= 1,
26798d9c4d9eSJohan Hovold 	.num_bulk_out		= 1,
26801da177e4SLinus Torvalds 	.open			= edge_open,
26811da177e4SLinus Torvalds 	.close			= edge_close,
26821da177e4SLinus Torvalds 	.throttle		= edge_throttle,
26831da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
26841da177e4SLinus Torvalds 	.attach			= edge_startup,
268549f4ff2dSJohan Hovold 	.calc_num_ports		= edge_calc_num_ports,
2686f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2687f9c99bb8SAlan Stern 	.release		= edge_release,
268800361530SJohan Hovold 	.port_probe		= edge_port_probe,
268900361530SJohan Hovold 	.port_remove		= edge_port_remove,
26901da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
26911da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
26921da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
269348ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2694cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
26951da177e4SLinus Torvalds 	.write			= edge_write,
26961da177e4SLinus Torvalds 	.write_room		= edge_write_room,
26971da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2698b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
26991da177e4SLinus Torvalds 	.break_ctl		= edge_break,
27001da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
27011da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
27021da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
270326c78daaSPeter E. Berger #ifdef CONFIG_PM
270426c78daaSPeter E. Berger 	.suspend		= edge_suspend,
270526c78daaSPeter E. Berger 	.resume			= edge_resume,
270626c78daaSPeter E. Berger #endif
27071da177e4SLinus Torvalds };
27081da177e4SLinus Torvalds 
2709ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
271018fcac35SGreg Kroah-Hartman 	.driver = {
27111da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2712269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
271318fcac35SGreg Kroah-Hartman 	},
2714269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
27151da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
27161da177e4SLinus Torvalds 	.num_ports		= 2,
27178d9c4d9eSJohan Hovold 	.num_bulk_out		= 1,
27181da177e4SLinus Torvalds 	.open			= edge_open,
27191da177e4SLinus Torvalds 	.close			= edge_close,
27201da177e4SLinus Torvalds 	.throttle		= edge_throttle,
27211da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
27221da177e4SLinus Torvalds 	.attach			= edge_startup,
272349f4ff2dSJohan Hovold 	.calc_num_ports		= edge_calc_num_ports,
2724f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2725f9c99bb8SAlan Stern 	.release		= edge_release,
272600361530SJohan Hovold 	.port_probe		= edge_port_probe,
272700361530SJohan Hovold 	.port_remove		= edge_port_remove,
27281da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
27291da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
27301da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
273148ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2732cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
27331da177e4SLinus Torvalds 	.write			= edge_write,
27341da177e4SLinus Torvalds 	.write_room		= edge_write_room,
27351da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2736b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
27371da177e4SLinus Torvalds 	.break_ctl		= edge_break,
27381da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
27391da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
27401da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
274126c78daaSPeter E. Berger #ifdef CONFIG_PM
274226c78daaSPeter E. Berger 	.suspend		= edge_suspend,
274326c78daaSPeter E. Berger 	.resume			= edge_resume,
274426c78daaSPeter E. Berger #endif
27451da177e4SLinus Torvalds };
27461da177e4SLinus Torvalds 
27477dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
27487dbe2460SAlan Stern 	&edgeport_1port_device, &edgeport_2port_device, NULL
27497dbe2460SAlan Stern };
27507dbe2460SAlan Stern 
275168e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
27541da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
27551da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2756d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
27571da177e4SLinus Torvalds 
2758a65ab973SUtkarsh Verma module_param(ignore_cpu_rev, bool, 0644);
27592742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev,
27602742fd88SAlan Cox 			"Ignore the cpu revision when connecting to a device");
27611da177e4SLinus Torvalds 
2762a65ab973SUtkarsh Verma module_param(default_uart_mode, int, 0644);
2763fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
2764