xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision d95e3caea2d1424dfdec30b46bd94087da94f928)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Edgeport USB Serial Converter driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
51da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *	the Free Software Foundation; either version 2 of the License, or
101da177e4SLinus Torvalds  *	(at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Supports the following devices:
131da177e4SLinus Torvalds  *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * For questions or problems with this driver, contact Inside Out
161da177e4SLinus Torvalds  * Networks technical support, or Peter Berger <pberger@brimson.com>,
171da177e4SLinus Torvalds  * or Al Borchers <alborchers@steinerpoint.com>.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/jiffies.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/tty.h>
251da177e4SLinus Torvalds #include <linux/tty_driver.h>
261da177e4SLinus Torvalds #include <linux/tty_flip.h>
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/spinlock.h>
29241ca64fSMatthias Kaehlcke #include <linux/mutex.h>
301da177e4SLinus Torvalds #include <linux/serial.h>
315509076dSJohan Hovold #include <linux/swab.h>
32d733cec1SJohan Hovold #include <linux/kfifo.h>
331da177e4SLinus Torvalds #include <linux/ioctl.h>
34d12b219aSJaswinder Singh #include <linux/firmware.h>
352742fd88SAlan Cox #include <linux/uaccess.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
37a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "io_16654.h"
401da177e4SLinus Torvalds #include "io_usbvend.h"
411da177e4SLinus Torvalds #include "io_ti.h"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
441da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define EPROM_PAGE_SIZE		64
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* different hardware types */
501da177e4SLinus Torvalds #define HARDWARE_TYPE_930	0
511da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP	1
521da177e4SLinus Torvalds 
532742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
542742fd88SAlan Cox #define	TI_MODE_CONFIGURING	0   /* Device has not entered start device */
552742fd88SAlan Cox #define	TI_MODE_BOOT		1   /* Staying in boot mode		   */
562742fd88SAlan Cox #define TI_MODE_DOWNLOAD	2   /* Made it to download mode		   */
572742fd88SAlan Cox #define TI_MODE_TRANSITIONING	3   /* Currently in boot mode but
582742fd88SAlan Cox 				       transitioning to download mode	   */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* read urb state */
611da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
621da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
631da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT	4000	/* in .01 sec */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Product information read from the Edgeport */
692742fd88SAlan Cox struct product_info {
702742fd88SAlan Cox 	int	TiMode;			/* Current TI Mode  */
712742fd88SAlan Cox 	__u8	hardware_type;		/* Type of hardware */
721da177e4SLinus Torvalds } __attribute__((packed));
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds struct edgeport_port {
751da177e4SLinus Torvalds 	__u16 uart_base;
761da177e4SLinus Torvalds 	__u16 dma_address;
771da177e4SLinus Torvalds 	__u8 shadow_msr;
781da177e4SLinus Torvalds 	__u8 shadow_mcr;
791da177e4SLinus Torvalds 	__u8 shadow_lsr;
801da177e4SLinus Torvalds 	__u8 lsr_mask;
8119af5cdbSMartin Olsson 	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
821da177e4SLinus Torvalds 					   wait without data before completing
831da177e4SLinus Torvalds 					   a read short */
841da177e4SLinus Torvalds 	int baud_rate;
851da177e4SLinus Torvalds 	int close_pending;
861da177e4SLinus Torvalds 	int lsr_event;
8748ee5801SJohan Hovold 
881da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
891da177e4SLinus Torvalds 	struct usb_serial_port	*port;
901da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
911da177e4SLinus Torvalds 	spinlock_t ep_lock;
921da177e4SLinus Torvalds 	int ep_read_urb_state;
931da177e4SLinus Torvalds 	int ep_write_urb_in_use;
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds struct edgeport_serial {
971da177e4SLinus Torvalds 	struct product_info product_info;
982742fd88SAlan Cox 	u8 TI_I2C_Type;			/* Type of I2C in UMP */
992742fd88SAlan Cox 	u8 TiReadI2C;			/* Set to TRUE if we have read the
1002742fd88SAlan Cox 					   I2c in Boot Mode */
101241ca64fSMatthias Kaehlcke 	struct mutex es_lock;
1021da177e4SLinus Torvalds 	int num_ports_open;
1031da177e4SLinus Torvalds 	struct usb_serial *serial;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /* Devices that this driver supports */
1087d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = {
1091da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1101da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1111da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1121da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1131da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1141da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1151da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1161da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1171da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1181da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1191da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1201da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1211da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1221da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1231da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1241da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1251da177e4SLinus Torvalds 	{ }
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1287d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = {
1291da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1301da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1311da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1321da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1331da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1341da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1351da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1361da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1371da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1381da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1391da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1401da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
141fc4cbd75SMartin K. Petersen 	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1421da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
143fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
144fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
145fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
146fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1471da177e4SLinus Torvalds 	{ }
1481da177e4SLinus Torvalds };
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* Devices that this driver supports */
1517d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
1521da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1531da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1541da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1551da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1561da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1571da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1581da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1591da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1601da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1611da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1621da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1631da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1641da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1651da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1661da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1671da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1681da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1691da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1701da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1711da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1721da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1731da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1741da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1751da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1761da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1781da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1791da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
1801da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
181fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
182fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
183fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
184fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1851da177e4SLinus Torvalds 	{ }
1861da177e4SLinus Torvalds };
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
1891da177e4SLinus Torvalds 
190d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion;
191d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion;
192d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
19590ab5ee9SRusty Russell static bool ignore_cpu_rev;
1962742fd88SAlan Cox static int default_uart_mode;		/* RS232 */
1971da177e4SLinus Torvalds 
1982e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1992e124b4aSJiri Slaby 		int length);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2021da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2031da177e4SLinus Torvalds 
20495da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
20595da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
206ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
2071da177e4SLinus Torvalds 
208fc4cbd75SMartin K. Petersen /* sysfs attributes */
209fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
210fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
211fc4cbd75SMartin K. Petersen 
2121da177e4SLinus Torvalds 
2132742fd88SAlan Cox static int ti_vread_sync(struct usb_device *dev, __u8 request,
2142742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	int status;
2171da177e4SLinus Torvalds 
2182742fd88SAlan Cox 	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
2192742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
2202742fd88SAlan Cox 			value, index, data, size, 1000);
2211da177e4SLinus Torvalds 	if (status < 0)
2221da177e4SLinus Torvalds 		return status;
2231da177e4SLinus Torvalds 	if (status != size) {
22467e6da70SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
225441b62c1SHarvey Harrison 			__func__, size, status);
2261da177e4SLinus Torvalds 		return -ECOMM;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 	return 0;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2312742fd88SAlan Cox static int ti_vsend_sync(struct usb_device *dev, __u8 request,
2322742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	int status;
2351da177e4SLinus Torvalds 
2362742fd88SAlan Cox 	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
2372742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
2382742fd88SAlan Cox 			value, index, data, size, 1000);
2391da177e4SLinus Torvalds 	if (status < 0)
2401da177e4SLinus Torvalds 		return status;
2411da177e4SLinus Torvalds 	if (status != size) {
24267e6da70SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
243441b62c1SHarvey Harrison 			__func__, size, status);
2441da177e4SLinus Torvalds 		return -ECOMM;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 	return 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2492742fd88SAlan Cox static int send_cmd(struct usb_device *dev, __u8 command,
2501da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
2511da177e4SLinus Torvalds 				int size)
2521da177e4SLinus Torvalds {
2532742fd88SAlan Cox 	return ti_vsend_sync(dev, command, value, moduleid, data, size);
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
2572742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask)
2581da177e4SLinus Torvalds {
2591143832eSGreg Kroah-Hartman 	int port_number = port->port_number;
2601da177e4SLinus Torvalds 
26167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
2621da177e4SLinus Torvalds 
2632742fd88SAlan Cox 	return send_cmd(port->serial->dev,
2641da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
2651da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
2661da177e4SLinus Torvalds 					mask,
2671da177e4SLinus Torvalds 					NULL,
2681da177e4SLinus Torvalds 					0);
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /**
2722742fd88SAlan Cox  * read_download_mem - Read edgeport memory from TI chip
2731da177e4SLinus Torvalds  * @dev: usb device pointer
2741da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
2751da177e4SLinus Torvalds  * @length: Length of above data
2761da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
2771da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
2781da177e4SLinus Torvalds  */
2792742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address,
2801da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int status = 0;
2831da177e4SLinus Torvalds 	__u8 read_length;
2845509076dSJohan Hovold 	u16 be_start_address;
2851da177e4SLinus Torvalds 
28667e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
2891da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
2901da177e4SLinus Torvalds 	 */
2911da177e4SLinus Torvalds 	while (length) {
2921da177e4SLinus Torvalds 		if (length > 64)
2931da177e4SLinus Torvalds 			read_length = 64;
2941da177e4SLinus Torvalds 		else
2951da177e4SLinus Torvalds 			read_length = (__u8)length;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 		if (read_length > 1) {
29867e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
2991da177e4SLinus Torvalds 		}
3005509076dSJohan Hovold 		/*
3015509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian
3025509076dSJohan Hovold 		 *       byte order regardless of host byte order.
3035509076dSJohan Hovold 		 */
3045509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
3052742fd88SAlan Cox 		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
3062742fd88SAlan Cox 					(__u16)address_type,
3075509076dSJohan Hovold 					be_start_address,
3082742fd88SAlan Cox 					buffer, read_length);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 		if (status) {
31167e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
3121da177e4SLinus Torvalds 			return status;
3131da177e4SLinus Torvalds 		}
3141da177e4SLinus Torvalds 
3152742fd88SAlan Cox 		if (read_length > 1)
31659d33f2fSGreg Kroah-Hartman 			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 		/* Update pointers/length */
3191da177e4SLinus Torvalds 		start_address += read_length;
3201da177e4SLinus Torvalds 		buffer += read_length;
3211da177e4SLinus Torvalds 		length -= read_length;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	return status;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3272742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address,
3282742fd88SAlan Cox 						int length, __u8 *buffer)
3291da177e4SLinus Torvalds {
3302742fd88SAlan Cox 	return read_download_mem(dev, start_address, length,
3312742fd88SAlan Cox 					DTK_ADDR_SPACE_XDATA, buffer);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /* Read edgeport memory to a given block */
3352742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial,
3362742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	int status = 0;
3391da177e4SLinus Torvalds 	int i;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	for (i = 0; i < length; i++) {
3422742fd88SAlan Cox 		status = ti_vread_sync(serial->serial->dev,
3432742fd88SAlan Cox 				UMPC_MEMORY_READ, serial->TI_I2C_Type,
3442742fd88SAlan Cox 				(__u16)(start_address+i), &buffer[i], 0x01);
3451da177e4SLinus Torvalds 		if (status) {
34667e6da70SGreg Kroah-Hartman 			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
3471da177e4SLinus Torvalds 			return status;
3481da177e4SLinus Torvalds 		}
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 
35167e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
3522742fd88SAlan Cox 		__func__, start_address, length);
35359d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	return status;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
3612742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial,
3622742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	int status = 0;
3651da177e4SLinus Torvalds 	int i;
366e9305d2fSJohan Hovold 	u8 *temp;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* Must do a read before write */
3691da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
370e9305d2fSJohan Hovold 		temp = kmalloc(1, GFP_KERNEL);
37110c642d0SJohan Hovold 		if (!temp)
372e9305d2fSJohan Hovold 			return -ENOMEM;
37310c642d0SJohan Hovold 
374e9305d2fSJohan Hovold 		status = read_boot_mem(serial, 0, 1, temp);
375e9305d2fSJohan Hovold 		kfree(temp);
3761da177e4SLinus Torvalds 		if (status)
3771da177e4SLinus Torvalds 			return status;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	for (i = 0; i < length; ++i) {
3812742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev,
3822742fd88SAlan Cox 				UMPC_MEMORY_WRITE, buffer[i],
3832742fd88SAlan Cox 				(__u16)(i + start_address), NULL, 0);
3841da177e4SLinus Torvalds 		if (status)
3851da177e4SLinus Torvalds 			return status;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
38867e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
38959d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	return status;
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
3962742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial,
3972742fd88SAlan Cox 		int start_address, int length, __u8 address_type, __u8 *buffer)
3981da177e4SLinus Torvalds {
39967e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
4001da177e4SLinus Torvalds 	int status = 0;
4011da177e4SLinus Torvalds 	int write_length;
4025509076dSJohan Hovold 	u16 be_start_address;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4051da177e4SLinus Torvalds 
40625985edcSLucas De Marchi 	/* calculate the number of bytes left in the first page */
4072742fd88SAlan Cox 	write_length = EPROM_PAGE_SIZE -
4082742fd88SAlan Cox 				(start_address & (EPROM_PAGE_SIZE - 1));
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (write_length > length)
4111da177e4SLinus Torvalds 		write_length = length;
4121da177e4SLinus Torvalds 
41367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
4142742fd88SAlan Cox 		__func__, start_address, write_length);
41559d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, write_length, buffer);
4161da177e4SLinus Torvalds 
4175509076dSJohan Hovold 	/*
4185509076dSJohan Hovold 	 * Write first page.
4195509076dSJohan Hovold 	 *
4205509076dSJohan Hovold 	 * NOTE: Must use swab as wIndex is sent in little-endian byte order
4215509076dSJohan Hovold 	 *       regardless of host byte order.
4225509076dSJohan Hovold 	 */
4235509076dSJohan Hovold 	be_start_address = swab16((u16)start_address);
4242742fd88SAlan Cox 	status = ti_vsend_sync(serial->serial->dev,
4252742fd88SAlan Cox 				UMPC_MEMORY_WRITE, (__u16)address_type,
4265509076dSJohan Hovold 				be_start_address,
4272742fd88SAlan Cox 				buffer,	write_length);
4281da177e4SLinus Torvalds 	if (status) {
42967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
4301da177e4SLinus Torvalds 		return status;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	length		-= write_length;
4341da177e4SLinus Torvalds 	start_address	+= write_length;
4351da177e4SLinus Torvalds 	buffer		+= write_length;
4361da177e4SLinus Torvalds 
4372742fd88SAlan Cox 	/* We should be aligned now -- can write
4382742fd88SAlan Cox 	   max page size bytes at a time */
4391da177e4SLinus Torvalds 	while (length) {
4401da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
4411da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
4421da177e4SLinus Torvalds 		else
4431da177e4SLinus Torvalds 			write_length = length;
4441da177e4SLinus Torvalds 
44567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
4462742fd88SAlan Cox 			__func__, start_address, write_length);
44759d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, write_length, buffer);
4481da177e4SLinus Torvalds 
4495509076dSJohan Hovold 		/*
4505509076dSJohan Hovold 		 * Write next page.
4515509076dSJohan Hovold 		 *
4525509076dSJohan Hovold 		 * NOTE: Must use swab as wIndex is sent in little-endian byte
4535509076dSJohan Hovold 		 *       order regardless of host byte order.
4545509076dSJohan Hovold 		 */
4555509076dSJohan Hovold 		be_start_address = swab16((u16)start_address);
4562742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
4572742fd88SAlan Cox 				(__u16)address_type,
4585509076dSJohan Hovold 				be_start_address,
4592742fd88SAlan Cox 				buffer, write_length);
4601da177e4SLinus Torvalds 		if (status) {
46167e6da70SGreg Kroah-Hartman 			dev_err(dev, "%s - ERROR %d\n", __func__, status);
4621da177e4SLinus Torvalds 			return status;
4631da177e4SLinus Torvalds 		}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 		length		-= write_length;
4661da177e4SLinus Torvalds 		start_address	+= write_length;
4671da177e4SLinus Torvalds 		buffer		+= write_length;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 	return status;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
4731da177e4SLinus Torvalds  *
4741da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
4751da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
4761da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
4771da177e4SLinus Torvalds  */
4782742fd88SAlan Cox static int tx_active(struct edgeport_port *port)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	int status;
4811da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
4821da177e4SLinus Torvalds 	__u8 *lsr;
4831da177e4SLinus Torvalds 	int bytes_left = 0;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
48610c642d0SJohan Hovold 	if (!oedb)
4871da177e4SLinus Torvalds 		return -ENOMEM;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
4901da177e4SLinus Torvalds 					   as not all platforms can do DMA
4911da177e4SLinus Torvalds 					   from stack */
4921da177e4SLinus Torvalds 	if (!lsr) {
4931da177e4SLinus Torvalds 		kfree(oedb);
4941da177e4SLinus Torvalds 		return -ENOMEM;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
4972742fd88SAlan Cox 	status = read_ram(port->port->serial->dev, port->dma_address,
4982742fd88SAlan Cox 						sizeof(*oedb), (void *)oedb);
4991da177e4SLinus Torvalds 	if (status)
5001da177e4SLinus Torvalds 		goto exit_is_tx_active;
5011da177e4SLinus Torvalds 
50267e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - XByteCount    0x%X\n", __func__, oedb->XByteCount);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* and the LSR */
5052742fd88SAlan Cox 	status = read_ram(port->port->serial->dev,
5062742fd88SAlan Cox 			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (status)
5091da177e4SLinus Torvalds 		goto exit_is_tx_active;
51067e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5131da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80) != 0)
5141da177e4SLinus Torvalds 		bytes_left += 64;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
5171da177e4SLinus Torvalds 		bytes_left += 1;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5201da177e4SLinus Torvalds exit_is_tx_active:
52167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	kfree(lsr);
5241da177e4SLinus Torvalds 	kfree(oedb);
5251da177e4SLinus Torvalds 	return bytes_left;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5282742fd88SAlan Cox static int choose_config(struct usb_device *dev)
5291da177e4SLinus Torvalds {
5302742fd88SAlan Cox 	/*
5312742fd88SAlan Cox 	 * There may be multiple configurations on this device, in which case
5322742fd88SAlan Cox 	 * we would need to read and parse all of them to find out which one
5332742fd88SAlan Cox 	 * we want. However, we just support one config at this point,
5342742fd88SAlan Cox 	 * configuration # 1, which is Config Descriptor 0.
5352742fd88SAlan Cox 	 */
5361da177e4SLinus Torvalds 
53767e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
5382742fd88SAlan Cox 		__func__, dev->config->desc.bNumInterfaces);
53967e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - MAX Power            = %d\n",
5402742fd88SAlan Cox 		__func__, dev->config->desc.bMaxPower * 2);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
54367e6da70SGreg Kroah-Hartman 		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
5441da177e4SLinus Torvalds 		return -ENODEV;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	return 0;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5502742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial,
5512742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	int status;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
5562742fd88SAlan Cox 		status = read_download_mem(serial->serial->dev,
5571da177e4SLinus Torvalds 					       start_address,
5581da177e4SLinus Torvalds 					       length,
5591da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
5601da177e4SLinus Torvalds 					       buffer);
5611da177e4SLinus Torvalds 	} else {
5622742fd88SAlan Cox 		status = read_boot_mem(serial, start_address, length,
5631da177e4SLinus Torvalds 								buffer);
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 	return status;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5682742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address,
5692742fd88SAlan Cox 						int length, __u8 *buffer)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
5722742fd88SAlan Cox 		return write_boot_mem(serial, start_address, length,
5731da177e4SLinus Torvalds 								buffer);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
5762742fd88SAlan Cox 		return write_i2c_mem(serial, start_address, length,
5772742fd88SAlan Cox 						serial->TI_I2C_Type, buffer);
5781da177e4SLinus Torvalds 	return -EINVAL;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
5842742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial,
5852742fd88SAlan Cox 				int desc_type, struct ti_i2c_desc *rom_desc)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	int start_address;
5881da177e4SLinus Torvalds 	int status;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
5911da177e4SLinus Torvalds 	start_address = 2;
5921da177e4SLinus Torvalds 	do {
5932742fd88SAlan Cox 		status = read_rom(serial,
5941da177e4SLinus Torvalds 				   start_address,
5951da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
5961da177e4SLinus Torvalds 				   (__u8 *)rom_desc);
5971da177e4SLinus Torvalds 		if (status)
5981da177e4SLinus Torvalds 			return 0;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
6011da177e4SLinus Torvalds 			return start_address;
6021da177e4SLinus Torvalds 
6035509076dSJohan Hovold 		start_address = start_address + sizeof(struct ti_i2c_desc) +
6045509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	return 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /* Validate descriptor checksum */
6122742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	__u16 i;
6151da177e4SLinus Torvalds 	__u8 cs = 0;
6161da177e4SLinus Torvalds 
6175509076dSJohan Hovold 	for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
6181da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
6192742fd88SAlan Cox 
6201da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
62167e6da70SGreg Kroah-Hartman 		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
6221da177e4SLinus Torvalds 		return -EINVAL;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 	return 0;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds /* Make sure that the I2C image is good */
6282742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
6311da177e4SLinus Torvalds 	int status = 0;
6321da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
6331da177e4SLinus Torvalds 	int start_address = 2;
6341da177e4SLinus Torvalds 	__u8 *buffer;
6351da177e4SLinus Torvalds 	__u16 ttype;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
63810c642d0SJohan Hovold 	if (!rom_desc)
6391da177e4SLinus Torvalds 		return -ENOMEM;
64010c642d0SJohan Hovold 
6411da177e4SLinus Torvalds 	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
6421da177e4SLinus Torvalds 	if (!buffer) {
6431da177e4SLinus Torvalds 		kfree(rom_desc);
6441da177e4SLinus Torvalds 		return -ENOMEM;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 
6472742fd88SAlan Cox 	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
6482742fd88SAlan Cox 	status = read_rom(serial, 0, 1, buffer);
6491da177e4SLinus Torvalds 	if (status)
6502742fd88SAlan Cox 		goto out;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
653441b62c1SHarvey Harrison 		dev_err(dev, "%s - invalid buffer signature\n", __func__);
6541da177e4SLinus Torvalds 		status = -ENODEV;
6552742fd88SAlan Cox 		goto out;
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	do {
6592742fd88SAlan Cox 		/* Validate the I2C */
6602742fd88SAlan Cox 		status = read_rom(serial,
6611da177e4SLinus Torvalds 				start_address,
6621da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
6631da177e4SLinus Torvalds 				(__u8 *)rom_desc);
6641da177e4SLinus Torvalds 		if (status)
6651da177e4SLinus Torvalds 			break;
6661da177e4SLinus Torvalds 
6672742fd88SAlan Cox 		if ((start_address + sizeof(struct ti_i2c_desc) +
6685509076dSJohan Hovold 			le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
6691da177e4SLinus Torvalds 			status = -ENODEV;
67067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
6711da177e4SLinus Torvalds 			break;
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 
67467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
6751da177e4SLinus Torvalds 
6762742fd88SAlan Cox 		/* Skip type 2 record */
6771da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
6781da177e4SLinus Torvalds 		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
6791da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
6802742fd88SAlan Cox 			/* Read the descriptor data */
6812742fd88SAlan Cox 			status = read_rom(serial, start_address +
6822742fd88SAlan Cox 						sizeof(struct ti_i2c_desc),
6835509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size),
6845509076dSJohan Hovold 						buffer);
6851da177e4SLinus Torvalds 			if (status)
6861da177e4SLinus Torvalds 				break;
6871da177e4SLinus Torvalds 
6882742fd88SAlan Cox 			status = valid_csum(rom_desc, buffer);
6891da177e4SLinus Torvalds 			if (status)
6901da177e4SLinus Torvalds 				break;
6911da177e4SLinus Torvalds 		}
6922742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc) +
6935509076dSJohan Hovold 						le16_to_cpu(rom_desc->Size);
6941da177e4SLinus Torvalds 
6952742fd88SAlan Cox 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
6962742fd88SAlan Cox 				(start_address < TI_MAX_I2C_SIZE));
6971da177e4SLinus Torvalds 
6982742fd88SAlan Cox 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
6992742fd88SAlan Cox 				(start_address > TI_MAX_I2C_SIZE))
7001da177e4SLinus Torvalds 		status = -ENODEV;
7011da177e4SLinus Torvalds 
7022742fd88SAlan Cox out:
7031da177e4SLinus Torvalds 	kfree(buffer);
7041da177e4SLinus Torvalds 	kfree(rom_desc);
7051da177e4SLinus Torvalds 	return status;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7082742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	int status;
7111da177e4SLinus Torvalds 	int start_address;
7121da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7131da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
71467e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
71710c642d0SJohan Hovold 	if (!rom_desc)
7181da177e4SLinus Torvalds 		return -ENOMEM;
71910c642d0SJohan Hovold 
7202742fd88SAlan Cox 	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
7212742fd88SAlan Cox 								rom_desc);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (!start_address) {
72467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
7251da177e4SLinus Torvalds 		status = -ENODEV;
7261da177e4SLinus Torvalds 		goto exit;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
7292742fd88SAlan Cox 	/* Read the descriptor data */
7302742fd88SAlan Cox 	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
7315509076dSJohan Hovold 					le16_to_cpu(rom_desc->Size), buffer);
7321da177e4SLinus Torvalds 	if (status)
7331da177e4SLinus Torvalds 		goto exit;
7341da177e4SLinus Torvalds 
7352742fd88SAlan Cox 	status = valid_csum(rom_desc, buffer);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
73867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - IonConfig      0x%x\n", __func__, desc->IonConfig);
73967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Version          %d\n", __func__, desc->Version);
74067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Cpu/Board      0x%x\n", __func__, desc->CpuRev_BoardRev);
74167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumPorts         %d\n", __func__, desc->NumPorts);
74267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumVirtualPorts  %d\n", __func__, desc->NumVirtualPorts);
74367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - TotalPorts       %d\n", __func__, desc->TotalPorts);
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds exit:
7461da177e4SLinus Torvalds 	kfree(rom_desc);
7471da177e4SLinus Torvalds 	return status;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds /* Build firmware header used for firmware update */
7512742fd88SAlan Cox static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	__u8 *buffer;
7541da177e4SLinus Torvalds 	int buffer_size;
7551da177e4SLinus Torvalds 	int i;
756d12b219aSJaswinder Singh 	int err;
7571da177e4SLinus Torvalds 	__u8 cs = 0;
7581da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
7591da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
7601da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
761d12b219aSJaswinder Singh 	const struct firmware *fw;
762d12b219aSJaswinder Singh 	const char *fw_name = "edgeport/down3.bin";
7631da177e4SLinus Torvalds 
7642742fd88SAlan Cox 	/* In order to update the I2C firmware we must change the type 2 record
7652742fd88SAlan Cox 	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
7662742fd88SAlan Cox 	 * Then while in boot mode, the driver will download the latest
7672742fd88SAlan Cox 	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
7682742fd88SAlan Cox 	 * device comes back up in download mode the driver will cause the new
7692742fd88SAlan Cox 	 * firmware to be copied from the UMP Ram to I2C and the firmware will
7702742fd88SAlan Cox 	 * update the record type from 0xf2 to 0x02.
7712742fd88SAlan Cox 	 */
7721da177e4SLinus Torvalds 
7732742fd88SAlan Cox 	/* Allocate a 15.5k buffer + 2 bytes for version number
7742742fd88SAlan Cox 	 * (Firmware Record) */
7752742fd88SAlan Cox 	buffer_size = (((1024 * 16) - 512 ) +
7762742fd88SAlan Cox 			sizeof(struct ti_i2c_firmware_rec));
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	buffer = kmalloc(buffer_size, GFP_KERNEL);
77910c642d0SJohan Hovold 	if (!buffer)
7801da177e4SLinus Torvalds 		return -ENOMEM;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	// Set entire image of 0xffs
7831da177e4SLinus Torvalds 	memset(buffer, 0xff, buffer_size);
7841da177e4SLinus Torvalds 
785d12b219aSJaswinder Singh 	err = request_firmware(&fw, fw_name, dev);
786d12b219aSJaswinder Singh 	if (err) {
787d9bb03dcSGreg Kroah-Hartman 		dev_err(dev, "Failed to load image \"%s\" err %d\n",
788d12b219aSJaswinder Singh 			fw_name, err);
789d12b219aSJaswinder Singh 		kfree(buffer);
790d12b219aSJaswinder Singh 		return err;
791d12b219aSJaswinder Singh 	}
792d12b219aSJaswinder Singh 
793d12b219aSJaswinder Singh 	/* Save Download Version Number */
794d12b219aSJaswinder Singh 	OperationalMajorVersion = fw->data[0];
795d12b219aSJaswinder Singh 	OperationalMinorVersion = fw->data[1];
796d12b219aSJaswinder Singh 	OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
797d12b219aSJaswinder Singh 
7982742fd88SAlan Cox 	/* Copy version number into firmware record */
7991da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
8001da177e4SLinus Torvalds 
801d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
802d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
8031da177e4SLinus Torvalds 
8042742fd88SAlan Cox 	/* Pointer to fw_down memory image */
805d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
808d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
8091da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
8101da177e4SLinus Torvalds 
811d12b219aSJaswinder Singh 	release_firmware(fw);
812d12b219aSJaswinder Singh 
8131da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
8141da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	kfree(buffer);
8181da177e4SLinus Torvalds 
8192742fd88SAlan Cox 	/* Build new header */
8201da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
8211da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
824c03890ffSJohan Hovold 	i2c_header->Size	= cpu_to_le16(buffer_size);
8251da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
826d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
827d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	return 0;
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
8332742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial)
8341da177e4SLinus Torvalds {
83567e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
8361da177e4SLinus Torvalds 	int status;
837e9305d2fSJohan Hovold 	u8 *data;
838e9305d2fSJohan Hovold 
839e9305d2fSJohan Hovold 	data = kmalloc(1, GFP_KERNEL);
84010c642d0SJohan Hovold 	if (!data)
841e9305d2fSJohan Hovold 		return -ENOMEM;
8421da177e4SLinus Torvalds 
8432742fd88SAlan Cox 	/* Try to read type 2 */
8442742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
845e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
8461da177e4SLinus Torvalds 	if (status)
84767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
8481da177e4SLinus Torvalds 	else
84967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
850e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
85167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
8521da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
853e9305d2fSJohan Hovold 		goto out;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
8562742fd88SAlan Cox 	/* Try to read type 3 */
8572742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
858e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
8591da177e4SLinus Torvalds 	if (status)
86067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
8611da177e4SLinus Torvalds 	else
86267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
863e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
86467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
8651da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
866e9305d2fSJohan Hovold 		goto out;
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
86967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Unknown\n", __func__);
8701da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
871e9305d2fSJohan Hovold 	status = -ENODEV;
872e9305d2fSJohan Hovold out:
873e9305d2fSJohan Hovold 	kfree(data);
874e9305d2fSJohan Hovold 	return status;
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8772742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer,
8782742fd88SAlan Cox 						int length, int *num_sent)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	int status;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	status = usb_bulk_msg(serial->dev,
8831da177e4SLinus Torvalds 			usb_sndbulkpipe(serial->dev,
8841da177e4SLinus Torvalds 				serial->port[0]->bulk_out_endpointAddress),
8852742fd88SAlan Cox 			buffer, length, num_sent, 1000);
8861da177e4SLinus Torvalds 	return status;
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
8902742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image,
8912742fd88SAlan Cox 							int image_length)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	int status = 0;
8941da177e4SLinus Torvalds 	int pos;
8951da177e4SLinus Torvalds 	int transfer;
8961da177e4SLinus Torvalds 	int done;
8971da177e4SLinus Torvalds 
8982742fd88SAlan Cox 	/* Transfer firmware image */
8991da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
9002742fd88SAlan Cox 		/* Read the next buffer from file */
9011da177e4SLinus Torvalds 		transfer = image_length - pos;
9021da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
9031da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
9041da177e4SLinus Torvalds 
9052742fd88SAlan Cox 		/* Transfer data */
9062742fd88SAlan Cox 		status = bulk_xfer(serial->serial, &image[pos],
9072742fd88SAlan Cox 							transfer, &done);
9081da177e4SLinus Torvalds 		if (status)
9091da177e4SLinus Torvalds 			break;
9102742fd88SAlan Cox 		/* Advance buffer pointer */
9111da177e4SLinus Torvalds 		pos += done;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	return status;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9172742fd88SAlan Cox /* FIXME!!! */
9182742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	return 0;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9232742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
9242742fd88SAlan Cox {
9252742fd88SAlan Cox 	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
9262742fd88SAlan Cox }
9272742fd88SAlan Cox 
9281da177e4SLinus Torvalds /**
9291da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
9301da177e4SLinus Torvalds  *
9311da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
9321da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
9331da177e4SLinus Torvalds  */
9342742fd88SAlan Cox static int download_fw(struct edgeport_serial *serial)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
9371da177e4SLinus Torvalds 	int status = 0;
9381da177e4SLinus Torvalds 	int start_address;
9391da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
9401da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
9411da177e4SLinus Torvalds 	int download_cur_ver;
9421da177e4SLinus Torvalds 	int download_new_ver;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
9451da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
9461da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
9471da177e4SLinus Torvalds 	 */
9481da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	/* Default to type 2 i2c */
9511da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9521da177e4SLinus Torvalds 
9532742fd88SAlan Cox 	status = choose_config(serial->serial->dev);
9541da177e4SLinus Torvalds 	if (status)
9551da177e4SLinus Torvalds 		return status;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
9581da177e4SLinus Torvalds 	if (!interface) {
959441b62c1SHarvey Harrison 		dev_err(dev, "%s - no interface set, error!\n", __func__);
9601da177e4SLinus Torvalds 		return -ENODEV;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9632742fd88SAlan Cox 	/*
9642742fd88SAlan Cox 	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
9652742fd88SAlan Cox 	 * if we have more than one endpoint we are definitely in download
9662742fd88SAlan Cox 	 * mode
9672742fd88SAlan Cox 	 */
9681da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
9691da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
9701da177e4SLinus Torvalds 	else
9712742fd88SAlan Cox 		/* Otherwise we will remain in configuring mode */
9721da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/********************************************************************/
9751da177e4SLinus Torvalds 	/* Download Mode */
9761da177e4SLinus Torvalds 	/********************************************************************/
9771da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
9781da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
9791da177e4SLinus Torvalds 
98067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
9811da177e4SLinus Torvalds 
9822742fd88SAlan Cox 		status = check_i2c_image(serial);
9831da177e4SLinus Torvalds 		if (status) {
98467e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
9851da177e4SLinus Torvalds 			return status;
9861da177e4SLinus Torvalds 		}
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 		/* Validate Hardware version number
9891da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
9901da177e4SLinus Torvalds 		 */
9911da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
99210c642d0SJohan Hovold 		if (!ti_manuf_desc)
9931da177e4SLinus Torvalds 			return -ENOMEM;
99410c642d0SJohan Hovold 
9952742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
9961da177e4SLinus Torvalds 		if (status) {
9971da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
9981da177e4SLinus Torvalds 			return status;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 
10012742fd88SAlan Cox 		/* Check version number of ION descriptor */
10022742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
100367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
10042742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
10051da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
10061da177e4SLinus Torvalds 			return -EINVAL;
10071da177e4SLinus Torvalds   		}
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
10101da177e4SLinus Torvalds 		if (!rom_desc) {
10111da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
10121da177e4SLinus Torvalds 			return -ENOMEM;
10131da177e4SLinus Torvalds 		}
10141da177e4SLinus Torvalds 
10152742fd88SAlan Cox 		/* Search for type 2 record (firmware record) */
10162742fd88SAlan Cox 		start_address = get_descriptor_addr(serial,
10172742fd88SAlan Cox 				I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
10182742fd88SAlan Cox 		if (start_address != 0) {
10191da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
1020e9305d2fSJohan Hovold 			u8 *record;
10211da177e4SLinus Torvalds 
102267e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
10231da177e4SLinus Torvalds 
10242742fd88SAlan Cox 			firmware_version = kmalloc(sizeof(*firmware_version),
10252742fd88SAlan Cox 								GFP_KERNEL);
10261da177e4SLinus Torvalds 			if (!firmware_version) {
10271da177e4SLinus Torvalds 				kfree(rom_desc);
10281da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
10291da177e4SLinus Torvalds 				return -ENOMEM;
10301da177e4SLinus Torvalds 			}
10311da177e4SLinus Torvalds 
10322742fd88SAlan Cox 			/* Validate version number
10332742fd88SAlan Cox 			 * Read the descriptor data
10342742fd88SAlan Cox 			 */
10352742fd88SAlan Cox 			status = read_rom(serial, start_address +
10362742fd88SAlan Cox 					sizeof(struct ti_i2c_desc),
10371da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
10381da177e4SLinus Torvalds 					(__u8 *)firmware_version);
10391da177e4SLinus Torvalds 			if (status) {
10401da177e4SLinus Torvalds 				kfree(firmware_version);
10411da177e4SLinus Torvalds 				kfree(rom_desc);
10421da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
10431da177e4SLinus Torvalds 				return status;
10441da177e4SLinus Torvalds 			}
10451da177e4SLinus Torvalds 
10462742fd88SAlan Cox 			/* Check version number of download with current
10472742fd88SAlan Cox 			   version in I2c */
10481da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
10491da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
1050d12b219aSJaswinder Singh 			download_new_ver = (OperationalMajorVersion << 8) +
1051d12b219aSJaswinder Singh 					   (OperationalMinorVersion);
10521da177e4SLinus Torvalds 
105367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
105467e6da70SGreg Kroah-Hartman 				__func__, firmware_version->Ver_Major,
10551da177e4SLinus Torvalds 				firmware_version->Ver_Minor,
1056d12b219aSJaswinder Singh 				OperationalMajorVersion,
1057d12b219aSJaswinder Singh 				OperationalMinorVersion);
10581da177e4SLinus Torvalds 
10592742fd88SAlan Cox 			/* Check if we have an old version in the I2C and
10602742fd88SAlan Cox 			   update if necessary */
10610827a9ffSGreg Kroah-Hartman 			if (download_cur_ver < download_new_ver) {
106267e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1063441b62c1SHarvey Harrison 					__func__,
10641da177e4SLinus Torvalds 					firmware_version->Ver_Major,
10651da177e4SLinus Torvalds 					firmware_version->Ver_Minor,
1066d12b219aSJaswinder Singh 					OperationalMajorVersion,
1067d12b219aSJaswinder Singh 					OperationalMinorVersion);
10681da177e4SLinus Torvalds 
1069e9305d2fSJohan Hovold 				record = kmalloc(1, GFP_KERNEL);
1070e9305d2fSJohan Hovold 				if (!record) {
1071e9305d2fSJohan Hovold 					kfree(firmware_version);
1072e9305d2fSJohan Hovold 					kfree(rom_desc);
1073e9305d2fSJohan Hovold 					kfree(ti_manuf_desc);
1074e9305d2fSJohan Hovold 					return -ENOMEM;
1075e9305d2fSJohan Hovold 				}
10762742fd88SAlan Cox 				/* In order to update the I2C firmware we must
10772742fd88SAlan Cox 				 * change the type 2 record to type 0xF2. This
10782742fd88SAlan Cox 				 * will force the UMP to come up in Boot Mode.
10792742fd88SAlan Cox 				 * Then while in boot mode, the driver will
10802742fd88SAlan Cox 				 * download the latest firmware (padded to
10812742fd88SAlan Cox 				 * 15.5k) into the UMP ram. Finally when the
10822742fd88SAlan Cox 				 * device comes back up in download mode the
10832742fd88SAlan Cox 				 * driver will cause the new firmware to be
10842742fd88SAlan Cox 				 * copied from the UMP Ram to I2C and the
10852742fd88SAlan Cox 				 * firmware will update the record type from
10862742fd88SAlan Cox 				 * 0xf2 to 0x02.
10872742fd88SAlan Cox 				 */
1088e9305d2fSJohan Hovold 				*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
10891da177e4SLinus Torvalds 
10902742fd88SAlan Cox 				/* Change the I2C Firmware record type to
10912742fd88SAlan Cox 				   0xf2 to trigger an update */
10922742fd88SAlan Cox 				status = write_rom(serial, start_address,
1093e9305d2fSJohan Hovold 						sizeof(*record), record);
10941da177e4SLinus Torvalds 				if (status) {
1095e9305d2fSJohan Hovold 					kfree(record);
10961da177e4SLinus Torvalds 					kfree(firmware_version);
10971da177e4SLinus Torvalds 					kfree(rom_desc);
10981da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
10991da177e4SLinus Torvalds 					return status;
11001da177e4SLinus Torvalds 				}
11011da177e4SLinus Torvalds 
11022742fd88SAlan Cox 				/* verify the write -- must do this in order
11032742fd88SAlan Cox 				 * for write to complete before we do the
11042742fd88SAlan Cox 				 * hardware reset
11052742fd88SAlan Cox 				 */
11062742fd88SAlan Cox 				status = read_rom(serial,
11071da177e4SLinus Torvalds 							start_address,
1108e9305d2fSJohan Hovold 							sizeof(*record),
1109e9305d2fSJohan Hovold 							record);
11101da177e4SLinus Torvalds 				if (status) {
1111e9305d2fSJohan Hovold 					kfree(record);
11121da177e4SLinus Torvalds 					kfree(firmware_version);
11131da177e4SLinus Torvalds 					kfree(rom_desc);
11141da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
11151da177e4SLinus Torvalds 					return status;
11161da177e4SLinus Torvalds 				}
11171da177e4SLinus Torvalds 
1118e9305d2fSJohan Hovold 				if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
111967e6da70SGreg Kroah-Hartman 					dev_err(dev, "%s - error resetting device\n", __func__);
1120e9305d2fSJohan Hovold 					kfree(record);
11211da177e4SLinus Torvalds 					kfree(firmware_version);
11221da177e4SLinus Torvalds 					kfree(rom_desc);
11231da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
11241da177e4SLinus Torvalds 					return -ENODEV;
11251da177e4SLinus Torvalds 				}
11261da177e4SLinus Torvalds 
112767e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
11281da177e4SLinus Torvalds 
11292742fd88SAlan Cox 				/* Reset UMP -- Back to BOOT MODE */
11302742fd88SAlan Cox 				status = ti_vsend_sync(serial->serial->dev,
11312742fd88SAlan Cox 						UMPC_HARDWARE_RESET,
11322742fd88SAlan Cox 						0, 0, NULL, 0);
11331da177e4SLinus Torvalds 
113467e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 				/* return an error on purpose. */
1137e9305d2fSJohan Hovold 				kfree(record);
11381da177e4SLinus Torvalds 				kfree(firmware_version);
11391da177e4SLinus Torvalds 				kfree(rom_desc);
11401da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11411da177e4SLinus Torvalds 				return -ENODEV;
11421da177e4SLinus Torvalds 			}
11431da177e4SLinus Torvalds 			kfree(firmware_version);
11441da177e4SLinus Torvalds 		}
11452742fd88SAlan Cox 		/* Search for type 0xF2 record (firmware blank record) */
11462742fd88SAlan Cox 		else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
11472742fd88SAlan Cox #define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
11482742fd88SAlan Cox 					sizeof(struct ti_i2c_firmware_rec))
11491da177e4SLinus Torvalds 			__u8 *header;
11501da177e4SLinus Torvalds 			__u8 *vheader;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
11531da177e4SLinus Torvalds 			if (!header) {
11541da177e4SLinus Torvalds 				kfree(rom_desc);
11551da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11561da177e4SLinus Torvalds 				return -ENOMEM;
11571da177e4SLinus Torvalds 			}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
11601da177e4SLinus Torvalds 			if (!vheader) {
11611da177e4SLinus Torvalds 				kfree(header);
11621da177e4SLinus Torvalds 				kfree(rom_desc);
11631da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11641da177e4SLinus Torvalds 				return -ENOMEM;
11651da177e4SLinus Torvalds 			}
11661da177e4SLinus Torvalds 
116767e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
11681da177e4SLinus Torvalds 
11692742fd88SAlan Cox 			/*
11702742fd88SAlan Cox 			 * In order to update the I2C firmware we must change
11712742fd88SAlan Cox 			 * the type 2 record to type 0xF2. This will force the
11722742fd88SAlan Cox 			 * UMP to come up in Boot Mode.  Then while in boot
11732742fd88SAlan Cox 			 * mode, the driver will download the latest firmware
11742742fd88SAlan Cox 			 * (padded to 15.5k) into the UMP ram. Finally when the
11752742fd88SAlan Cox 			 * device comes back up in download mode the driver
11762742fd88SAlan Cox 			 * will cause the new firmware to be copied from the
11772742fd88SAlan Cox 			 * UMP Ram to I2C and the firmware will update the
11782742fd88SAlan Cox 			 * record type from 0xf2 to 0x02.
11792742fd88SAlan Cox 			 */
11802742fd88SAlan Cox 			status = build_i2c_fw_hdr(header, dev);
11811da177e4SLinus Torvalds 			if (status) {
11821da177e4SLinus Torvalds 				kfree(vheader);
11831da177e4SLinus Torvalds 				kfree(header);
11841da177e4SLinus Torvalds 				kfree(rom_desc);
11851da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
1186fd6e5bbbSRoel Kluin 				return -EINVAL;
11871da177e4SLinus Torvalds 			}
11881da177e4SLinus Torvalds 
11892742fd88SAlan Cox 			/* Update I2C with type 0xf2 record with correct
11902742fd88SAlan Cox 			   size and checksum */
11912742fd88SAlan Cox 			status = write_rom(serial,
11921da177e4SLinus Torvalds 						start_address,
11931da177e4SLinus Torvalds 						HEADER_SIZE,
11941da177e4SLinus Torvalds 						header);
11951da177e4SLinus Torvalds 			if (status) {
11961da177e4SLinus Torvalds 				kfree(vheader);
11971da177e4SLinus Torvalds 				kfree(header);
11981da177e4SLinus Torvalds 				kfree(rom_desc);
11991da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12009800eb33SRoel Kluin 				return -EINVAL;
12011da177e4SLinus Torvalds 			}
12021da177e4SLinus Torvalds 
12032742fd88SAlan Cox 			/* verify the write -- must do this in order for
12042742fd88SAlan Cox 			   write to complete before we do the hardware reset */
12052742fd88SAlan Cox 			status = read_rom(serial, start_address,
12062742fd88SAlan Cox 							HEADER_SIZE, vheader);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 			if (status) {
120967e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - can't read header back\n", __func__);
12101da177e4SLinus Torvalds 				kfree(vheader);
12111da177e4SLinus Torvalds 				kfree(header);
12121da177e4SLinus Torvalds 				kfree(rom_desc);
12131da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12141da177e4SLinus Torvalds 				return status;
12151da177e4SLinus Torvalds 			}
12161da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
121767e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - write download record failed\n", __func__);
12181da177e4SLinus Torvalds 				kfree(vheader);
12191da177e4SLinus Torvalds 				kfree(header);
12201da177e4SLinus Torvalds 				kfree(rom_desc);
12211da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12221e29709eSRoel Kluin 				return -EINVAL;
12231da177e4SLinus Torvalds 			}
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 			kfree(vheader);
12261da177e4SLinus Torvalds 			kfree(header);
12271da177e4SLinus Torvalds 
122867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Start firmware update\n", __func__);
12291da177e4SLinus Torvalds 
12302742fd88SAlan Cox 			/* Tell firmware to copy download image into I2C */
12312742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
12322742fd88SAlan Cox 					UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
12331da177e4SLinus Torvalds 
123467e6da70SGreg Kroah-Hartman 		  	dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
12351da177e4SLinus Torvalds 			if (status) {
12362742fd88SAlan Cox 				dev_err(dev,
12372742fd88SAlan Cox 					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
12382742fd88SAlan Cox 								__func__);
12391da177e4SLinus Torvalds 				kfree(rom_desc);
12401da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12411da177e4SLinus Torvalds 				return status;
12421da177e4SLinus Torvalds 			}
12431da177e4SLinus Torvalds 		}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 		// The device is running the download code
12461da177e4SLinus Torvalds 		kfree(rom_desc);
12471da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
12481da177e4SLinus Torvalds 		return 0;
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	/********************************************************************/
12521da177e4SLinus Torvalds 	/* Boot Mode */
12531da177e4SLinus Torvalds 	/********************************************************************/
125467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
12551da177e4SLinus Torvalds 
12562742fd88SAlan Cox 	/* Configure the TI device so we can use the BULK pipes for download */
12572742fd88SAlan Cox 	status = config_boot_dev(serial->serial->dev);
12581da177e4SLinus Torvalds 	if (status)
12591da177e4SLinus Torvalds 		return status;
12601da177e4SLinus Torvalds 
12612742fd88SAlan Cox 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
12622742fd88SAlan Cox 							!= USB_VENDOR_ID_ION) {
126367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
12641da177e4SLinus Torvalds 			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
12651da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
12662742fd88SAlan Cox 		goto stayinbootmode;
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 
12692742fd88SAlan Cox 	/* We have an ION device (I2c Must be programmed)
12702742fd88SAlan Cox 	   Determine I2C image type */
12712742fd88SAlan Cox 	if (i2c_type_bootmode(serial))
12722742fd88SAlan Cox 		goto stayinbootmode;
12731da177e4SLinus Torvalds 
12742742fd88SAlan Cox 	/* Check for ION Vendor ID and that the I2C is valid */
12752742fd88SAlan Cox 	if (!check_i2c_image(serial)) {
12761da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
12771da177e4SLinus Torvalds 		int i;
12781da177e4SLinus Torvalds 		__u8 cs = 0;
12791da177e4SLinus Torvalds 		__u8 *buffer;
12801da177e4SLinus Torvalds 		int buffer_size;
1281d12b219aSJaswinder Singh 		int err;
1282d12b219aSJaswinder Singh 		const struct firmware *fw;
1283d12b219aSJaswinder Singh 		const char *fw_name = "edgeport/down3.bin";
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 		/* Validate Hardware version number
12861da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
12871da177e4SLinus Torvalds 		 */
12881da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
128910c642d0SJohan Hovold 		if (!ti_manuf_desc)
12901da177e4SLinus Torvalds 			return -ENOMEM;
129110c642d0SJohan Hovold 
12922742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
12931da177e4SLinus Torvalds 		if (status) {
12941da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
12952742fd88SAlan Cox 			goto stayinbootmode;
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 
12982742fd88SAlan Cox 		/* Check for version 2 */
12992742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
130067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
13012742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
13021da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
13032742fd88SAlan Cox 			goto stayinbootmode;
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 		/*
13092742fd88SAlan Cox 		 * In order to update the I2C firmware we must change the type
13102742fd88SAlan Cox 		 * 2 record to type 0xF2. This will force the UMP to come up
13112742fd88SAlan Cox 		 * in Boot Mode.  Then while in boot mode, the driver will
13122742fd88SAlan Cox 		 * download the latest firmware (padded to 15.5k) into the
13132742fd88SAlan Cox 		 * UMP ram. Finally when the device comes back up in download
13142742fd88SAlan Cox 		 * mode the driver will cause the new firmware to be copied
13152742fd88SAlan Cox 		 * from the UMP Ram to I2C and the firmware will update the
13162742fd88SAlan Cox 		 * record type from 0xf2 to 0x02.
13172742fd88SAlan Cox 		 *
13181da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
13191da177e4SLinus Torvalds 		 * or could we do this in place!
13201da177e4SLinus Torvalds 		 */
13211da177e4SLinus Torvalds 
13222742fd88SAlan Cox 		/* Allocate a 15.5k buffer + 3 byte header */
13232742fd88SAlan Cox 		buffer_size = (((1024 * 16) - 512) +
13242742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header));
13251da177e4SLinus Torvalds 		buffer = kmalloc(buffer_size, GFP_KERNEL);
132610c642d0SJohan Hovold 		if (!buffer)
13271da177e4SLinus Torvalds 			return -ENOMEM;
13281da177e4SLinus Torvalds 
13292742fd88SAlan Cox 		/* Initialize the buffer to 0xff (pad the buffer) */
13301da177e4SLinus Torvalds 		memset(buffer, 0xff, buffer_size);
13311da177e4SLinus Torvalds 
1332d12b219aSJaswinder Singh 		err = request_firmware(&fw, fw_name, dev);
1333d12b219aSJaswinder Singh 		if (err) {
1334d9bb03dcSGreg Kroah-Hartman 			dev_err(dev, "Failed to load image \"%s\" err %d\n",
1335d12b219aSJaswinder Singh 				fw_name, err);
1336d12b219aSJaswinder Singh 			kfree(buffer);
1337d12b219aSJaswinder Singh 			return err;
1338d12b219aSJaswinder Singh 		}
1339d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
1340d12b219aSJaswinder Singh 		release_firmware(fw);
13411da177e4SLinus Torvalds 
13422742fd88SAlan Cox 		for (i = sizeof(struct ti_i2c_image_header);
13432742fd88SAlan Cox 				i < buffer_size; i++) {
13441da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
13451da177e4SLinus Torvalds 		}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
13481da177e4SLinus Torvalds 
13492742fd88SAlan Cox 		/* update length and checksum after padding */
13502742fd88SAlan Cox 		header->Length 	 = cpu_to_le16((__u16)(buffer_size -
13512742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header)));
13521da177e4SLinus Torvalds 		header->CheckSum = cs;
13531da177e4SLinus Torvalds 
13542742fd88SAlan Cox 		/* Download the operational code  */
135567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
13562742fd88SAlan Cox 		status = download_code(serial, buffer, buffer_size);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		kfree(buffer);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		if (status) {
136167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
13621da177e4SLinus Torvalds 			return status;
13631da177e4SLinus Torvalds 		}
13641da177e4SLinus Torvalds 
13652742fd88SAlan Cox 		/* Device will reboot */
13661da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
13671da177e4SLinus Torvalds 
136867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		/* return an error on purpose */
13711da177e4SLinus Torvalds 		return -ENODEV;
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 
13742742fd88SAlan Cox stayinbootmode:
13752742fd88SAlan Cox 	/* Eprom is invalid or blank stay in boot mode */
137667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
13771da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	return 0;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 
13832742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on)
13841da177e4SLinus Torvalds {
13851143832eSGreg Kroah-Hartman 	int port_number = port->port->port_number;
13861143832eSGreg Kroah-Hartman 
13872742fd88SAlan Cox 	on = !!on;	/* 1 or 0 not bitmask */
13882742fd88SAlan Cox 	return send_cmd(port->port->serial->dev,
13892742fd88SAlan Cox 			feature, (__u8)(UMPM_UART1_PORT + port_number),
13902742fd88SAlan Cox 			on, NULL, 0);
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 
13942742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	int status = 0;
13971da177e4SLinus Torvalds 
139867e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
13991da177e4SLinus Torvalds 
14002742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
14011da177e4SLinus Torvalds 	if (status)
14021da177e4SLinus Torvalds 		return status;
14032742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
14041da177e4SLinus Torvalds 	if (status)
14051da177e4SLinus Torvalds 		return status;
14062742fd88SAlan Cox 	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
14102742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	__u8 lsr = 0;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
14151da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
14161da177e4SLinus Torvalds 		lsr |= flagUart;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
14191da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
14201da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
14211da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
14222742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
14232742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds #undef MAP_FLAG
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	return lsr;
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
14311da177e4SLinus Torvalds {
14321da177e4SLinus Torvalds 	struct async_icount *icount;
14331da177e4SLinus Torvalds 	struct tty_struct *tty;
14341da177e4SLinus Torvalds 
143567e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
14361da177e4SLinus Torvalds 
14372742fd88SAlan Cox 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
14382742fd88SAlan Cox 			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1439cf9a9d66SJohan Hovold 		icount = &edge_port->port->icount;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 		/* update input line counters */
14421da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
14431da177e4SLinus Torvalds 			icount->cts++;
14441da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
14451da177e4SLinus Torvalds 			icount->dsr++;
14461da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
14471da177e4SLinus Torvalds 			icount->dcd++;
14481da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
14491da177e4SLinus Torvalds 			icount->rng++;
145048ee5801SJohan Hovold 		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	/* Save the new modem status */
14541da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
14551da177e4SLinus Torvalds 
14564a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
14571da177e4SLinus Torvalds 	/* handle CTS flow control */
14581da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
1459*d95e3caeSPeter Hurley 		if (msr & EDGEPORT_MSR_CTS)
14601da177e4SLinus Torvalds 			tty_wakeup(tty);
14611da177e4SLinus Torvalds 	}
14624a90f09bSAlan Cox 	tty_kref_put(tty);
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
14652742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
14662742fd88SAlan Cox 							__u8 lsr, __u8 data)
14671da177e4SLinus Torvalds {
14681da177e4SLinus Torvalds 	struct async_icount *icount;
14692742fd88SAlan Cox 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
14702742fd88SAlan Cox 						LSR_FRM_ERR | LSR_BREAK));
14711da177e4SLinus Torvalds 
147267e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
14751da177e4SLinus Torvalds 
14762742fd88SAlan Cox 	if (new_lsr & LSR_BREAK)
14771da177e4SLinus Torvalds 		/*
14781da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
14791da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
14801da177e4SLinus Torvalds 		 */
14811da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
14842e124b4aSJiri Slaby 	if (lsr_data)
14852e124b4aSJiri Slaby 		edge_tty_recv(edge_port->port, &data, 1);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	/* update input line counters */
1488cf9a9d66SJohan Hovold 	icount = &edge_port->port->icount;
14891da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
14901da177e4SLinus Torvalds 		icount->brk++;
14911da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
14921da177e4SLinus Torvalds 		icount->overrun++;
14931da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
14941da177e4SLinus Torvalds 		icount->parity++;
14951da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
14961da177e4SLinus Torvalds 		icount->frame++;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 
15007d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb)
15011da177e4SLinus Torvalds {
1502cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
15031da177e4SLinus Torvalds 	struct usb_serial_port *port;
15041da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
150567e6da70SGreg Kroah-Hartman 	struct device *dev;
15061da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
15071da177e4SLinus Torvalds 	int length = urb->actual_length;
15081da177e4SLinus Torvalds 	int port_number;
15091da177e4SLinus Torvalds 	int function;
1510ee337c21SGreg Kroah-Hartman 	int retval;
15111da177e4SLinus Torvalds 	__u8 lsr;
15121da177e4SLinus Torvalds 	__u8 msr;
1513ee337c21SGreg Kroah-Hartman 	int status = urb->status;
15141da177e4SLinus Torvalds 
1515ee337c21SGreg Kroah-Hartman 	switch (status) {
15161da177e4SLinus Torvalds 	case 0:
15171da177e4SLinus Torvalds 		/* success */
15181da177e4SLinus Torvalds 		break;
15191da177e4SLinus Torvalds 	case -ECONNRESET:
15201da177e4SLinus Torvalds 	case -ENOENT:
15211da177e4SLinus Torvalds 	case -ESHUTDOWN:
15221da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
152367e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1524441b62c1SHarvey Harrison 		    __func__, status);
15251da177e4SLinus Torvalds 		return;
15261da177e4SLinus Torvalds 	default:
1527ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1528441b62c1SHarvey Harrison 			"%d\n", __func__, status);
15291da177e4SLinus Torvalds 		goto exit;
15301da177e4SLinus Torvalds 	}
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	if (!length) {
153367e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
15341da177e4SLinus Torvalds 		goto exit;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 
153767e6da70SGreg Kroah-Hartman 	dev = &edge_serial->serial->dev->dev;
153859d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, length, data);
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	if (length != 2) {
154167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
15421da177e4SLinus Torvalds 		goto exit;
15431da177e4SLinus Torvalds 	}
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
15461da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
154767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
154867e6da70SGreg Kroah-Hartman 		port_number, function, data[1]);
15491da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
15501da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
15511da177e4SLinus Torvalds 	if (!edge_port) {
155267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - edge_port not found\n", __func__);
15531da177e4SLinus Torvalds 		return;
15541da177e4SLinus Torvalds 	}
15551da177e4SLinus Torvalds 	switch (function) {
15561da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
15572742fd88SAlan Cox 		lsr = map_line_status(data[1]);
15581da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
15592742fd88SAlan Cox 			/* Save the LSR event for bulk read
15602742fd88SAlan Cox 			   completion routine */
156167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1562441b62c1SHarvey Harrison 				__func__, port_number, lsr);
15631da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
15641da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
15651da177e4SLinus Torvalds 		} else {
156667e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1567441b62c1SHarvey Harrison 				__func__, port_number, lsr);
15681da177e4SLinus Torvalds 			handle_new_lsr(edge_port, 0, lsr, 0);
15691da177e4SLinus Torvalds 		}
15701da177e4SLinus Torvalds 		break;
15711da177e4SLinus Torvalds 
15722742fd88SAlan Cox 	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
15731da177e4SLinus Torvalds 		/* Copy MSR from UMP */
15741da177e4SLinus Torvalds 		msr = data[1];
157567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1576441b62c1SHarvey Harrison 			__func__, port_number, msr);
15771da177e4SLinus Torvalds 		handle_new_msr(edge_port, msr);
15781da177e4SLinus Torvalds 		break;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	default:
15812742fd88SAlan Cox 		dev_err(&urb->dev->dev,
15822742fd88SAlan Cox 			"%s - Unknown Interrupt code from UMP %x\n",
1583441b62c1SHarvey Harrison 			__func__, data[1]);
15841da177e4SLinus Torvalds 		break;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds exit:
1589ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1590ee337c21SGreg Kroah-Hartman 	if (retval)
15912742fd88SAlan Cox 		dev_err(&urb->dev->dev,
15922742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1593441b62c1SHarvey Harrison 			 __func__, retval);
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
15967d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb)
15971da177e4SLinus Torvalds {
1598cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
159967e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
16001da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
1601ee337c21SGreg Kroah-Hartman 	int retval = 0;
16021da177e4SLinus Torvalds 	int port_number;
1603ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16041da177e4SLinus Torvalds 
1605ee337c21SGreg Kroah-Hartman 	switch (status) {
16061da177e4SLinus Torvalds 	case 0:
16071da177e4SLinus Torvalds 		/* success */
16081da177e4SLinus Torvalds 		break;
16091da177e4SLinus Torvalds 	case -ECONNRESET:
16101da177e4SLinus Torvalds 	case -ENOENT:
16111da177e4SLinus Torvalds 	case -ESHUTDOWN:
16121da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
161367e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
16141da177e4SLinus Torvalds 		return;
16151da177e4SLinus Torvalds 	default:
161667e6da70SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
16171da177e4SLinus Torvalds 	}
16181da177e4SLinus Torvalds 
1619ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
16201da177e4SLinus Torvalds 		goto exit;
16211da177e4SLinus Torvalds 
1622ee337c21SGreg Kroah-Hartman 	if (status) {
1623441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
16241da177e4SLinus Torvalds 		return;
16251da177e4SLinus Torvalds 	}
16261da177e4SLinus Torvalds 
16271143832eSGreg Kroah-Hartman 	port_number = edge_port->port->port_number;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
16301da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
163167e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1632441b62c1SHarvey Harrison 			__func__, port_number, edge_port->lsr_mask, *data);
16331da177e4SLinus Torvalds 		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
16341da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
16351da177e4SLinus Torvalds 		--urb->actual_length;
16361da177e4SLinus Torvalds 		++data;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
16392e124b4aSJiri Slaby 	if (urb->actual_length) {
164059d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
16412742fd88SAlan Cox 		if (edge_port->close_pending)
164267e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
16432742fd88SAlan Cox 								__func__);
16442742fd88SAlan Cox 		else
16452e124b4aSJiri Slaby 			edge_tty_recv(edge_port->port, data,
164605c7cd39SJiri Slaby 					urb->actual_length);
1647cf9a9d66SJohan Hovold 		edge_port->port->icount.rx += urb->actual_length;
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds exit:
16511da177e4SLinus Torvalds 	/* continue read unless stopped */
16521da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
16535833041fSJohan Hovold 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1654ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
16555833041fSJohan Hovold 	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
16561da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
16575833041fSJohan Hovold 
16581da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
1659ee337c21SGreg Kroah-Hartman 	if (retval)
166067e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16632e124b4aSJiri Slaby static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
16642e124b4aSJiri Slaby 		int length)
16651da177e4SLinus Torvalds {
16662742fd88SAlan Cox 	int queued;
16671da177e4SLinus Torvalds 
166805c7cd39SJiri Slaby 	queued = tty_insert_flip_string(&port->port, data, length);
16692742fd88SAlan Cox 	if (queued < length)
167005c7cd39SJiri Slaby 		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
16712742fd88SAlan Cox 			__func__, length - queued);
16722e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
16757d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb)
16761da177e4SLinus Torvalds {
1677cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
16781da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1679ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16804a90f09bSAlan Cox 	struct tty_struct *tty;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
16831da177e4SLinus Torvalds 
1684ee337c21SGreg Kroah-Hartman 	switch (status) {
16851da177e4SLinus Torvalds 	case 0:
16861da177e4SLinus Torvalds 		/* success */
16871da177e4SLinus Torvalds 		break;
16881da177e4SLinus Torvalds 	case -ECONNRESET:
16891da177e4SLinus Torvalds 	case -ENOENT:
16901da177e4SLinus Torvalds 	case -ESHUTDOWN:
16911da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
169267e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1693441b62c1SHarvey Harrison 		    __func__, status);
16941da177e4SLinus Torvalds 		return;
16951da177e4SLinus Torvalds 	default:
169622a416c4SJohan Hovold 		dev_err_console(port, "%s - nonzero write bulk status "
1697441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	/* send any buffered data */
17014a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
1702ae3759c2SJiri Slaby 	edge_send(port, tty);
17034a90f09bSAlan Cox 	tty_kref_put(tty);
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
1706a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
17071da177e4SLinus Torvalds {
17081da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
17091da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
17101da177e4SLinus Torvalds 	struct usb_device *dev;
17111da177e4SLinus Torvalds 	struct urb *urb;
17121da177e4SLinus Torvalds 	int port_number;
17131da177e4SLinus Torvalds 	int status;
17141da177e4SLinus Torvalds 	u16 open_settings;
17151da177e4SLinus Torvalds 	u8 transaction_timeout;
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	if (edge_port == NULL)
17181da177e4SLinus Torvalds 		return -ENODEV;
17191da177e4SLinus Torvalds 
17201143832eSGreg Kroah-Hartman 	port_number = port->port_number;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	dev = port->serial->dev;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	/* turn off loopback */
17252742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
17261da177e4SLinus Torvalds 	if (status) {
17272742fd88SAlan Cox 		dev_err(&port->dev,
17282742fd88SAlan Cox 				"%s - cannot send clear loopback command, %d\n",
1729441b62c1SHarvey Harrison 			__func__, status);
17301da177e4SLinus Torvalds 		return status;
17311da177e4SLinus Torvalds 	}
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	/* set up the port settings */
173495da310eSAlan Cox 	if (tty)
1735adc8d746SAlan Cox 		edge_set_termios(tty, port, &tty->termios);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	/* open up the port */
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
17401da177e4SLinus Torvalds 	transaction_timeout = 2;
17411da177e4SLinus Torvalds 
17422742fd88SAlan Cox 	edge_port->ump_read_timeout =
17432742fd88SAlan Cox 				max(20, ((transaction_timeout * 3) / 2));
17441da177e4SLinus Torvalds 
17452742fd88SAlan Cox 	/* milliseconds to timeout for DMA transfer */
17461da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
17471da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
17481da177e4SLinus Torvalds 			     (transaction_timeout << 2));
17491da177e4SLinus Torvalds 
175067e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
17532742fd88SAlan Cox 	status = send_cmd(dev, UMPC_OPEN_PORT,
17542742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
17551da177e4SLinus Torvalds 	if (status) {
17562742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send open command, %d\n",
17572742fd88SAlan Cox 							__func__, status);
17581da177e4SLinus Torvalds 		return status;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	/* Start the DMA? */
17622742fd88SAlan Cox 	status = send_cmd(dev, UMPC_START_PORT,
17632742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
17641da177e4SLinus Torvalds 	if (status) {
17652742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
17662742fd88SAlan Cox 							__func__, status);
17671da177e4SLinus Torvalds 		return status;
17681da177e4SLinus Torvalds 	}
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
17712742fd88SAlan Cox 	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
17721da177e4SLinus Torvalds 	if (status) {
17732742fd88SAlan Cox 		dev_err(&port->dev,
17742742fd88SAlan Cox 			"%s - cannot send clear buffers command, %d\n",
17752742fd88SAlan Cox 			__func__, status);
17761da177e4SLinus Torvalds 		return status;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/* Read Initial MSR */
17802742fd88SAlan Cox 	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
17812742fd88SAlan Cox 				(__u16)(UMPM_UART1_PORT + port_number),
17822742fd88SAlan Cox 				&edge_port->shadow_msr, 1);
17831da177e4SLinus Torvalds 	if (status) {
17842742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
17852742fd88SAlan Cox 							__func__, status);
17861da177e4SLinus Torvalds 		return status;
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds 
178967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	/* Set Initial MCR */
17921da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
179367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
1796241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
17971da177e4SLinus Torvalds 		return -ERESTARTSYS;
17981da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
17992742fd88SAlan Cox 		/* we are the first port to open, post the interrupt urb */
18001da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
18011da177e4SLinus Torvalds 		if (!urb) {
18022742fd88SAlan Cox 			dev_err(&port->dev,
18032742fd88SAlan Cox 				"%s - no interrupt urb present, exiting\n",
18042742fd88SAlan Cox 				__func__);
18051da177e4SLinus Torvalds 			status = -EINVAL;
1806241ca64fSMatthias Kaehlcke 			goto release_es_lock;
18071da177e4SLinus Torvalds 		}
18081da177e4SLinus Torvalds 		urb->context = edge_serial;
18091da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
18101da177e4SLinus Torvalds 		if (status) {
18112742fd88SAlan Cox 			dev_err(&port->dev,
18122742fd88SAlan Cox 				"%s - usb_submit_urb failed with value %d\n",
18132742fd88SAlan Cox 					__func__, status);
1814241ca64fSMatthias Kaehlcke 			goto release_es_lock;
18151da177e4SLinus Torvalds 		}
18161da177e4SLinus Torvalds 	}
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	/*
18191da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
18201da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
18211da177e4SLinus Torvalds 	 */
18221da177e4SLinus Torvalds 	usb_clear_halt(dev, port->write_urb->pipe);
18231da177e4SLinus Torvalds 	usb_clear_halt(dev, port->read_urb->pipe);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	/* start up our bulk read urb */
18261da177e4SLinus Torvalds 	urb = port->read_urb;
18271da177e4SLinus Torvalds 	if (!urb) {
18282742fd88SAlan Cox 		dev_err(&port->dev, "%s - no read urb present, exiting\n",
18292742fd88SAlan Cox 								__func__);
18301da177e4SLinus Torvalds 		status = -EINVAL;
18311da177e4SLinus Torvalds 		goto unlink_int_urb;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
18341da177e4SLinus Torvalds 	urb->context = edge_port;
18351da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_KERNEL);
18361da177e4SLinus Torvalds 	if (status) {
18372742fd88SAlan Cox 		dev_err(&port->dev,
18382742fd88SAlan Cox 			"%s - read bulk usb_submit_urb failed with value %d\n",
18392742fd88SAlan Cox 				__func__, status);
18401da177e4SLinus Torvalds 		goto unlink_int_urb;
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
18441da177e4SLinus Torvalds 
1845241ca64fSMatthias Kaehlcke 	goto release_es_lock;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds unlink_int_urb:
18481da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
18491da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1850241ca64fSMatthias Kaehlcke release_es_lock:
1851241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
18521da177e4SLinus Torvalds 	return status;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
1855335f8514SAlan Cox static void edge_close(struct usb_serial_port *port)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
18581da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
1859af581059SJohan Hovold 	struct usb_serial *serial = port->serial;
186077de2518SJohan Hovold 	unsigned long flags;
18611da177e4SLinus Torvalds 	int port_number;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
18641da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
18652742fd88SAlan Cox 	if (edge_serial == NULL || edge_port == NULL)
18661da177e4SLinus Torvalds 		return;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
18691da177e4SLinus Torvalds 	 * this flag and dump add read data */
18701da177e4SLinus Torvalds 	edge_port->close_pending = 1;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
18731da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
18741da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
187577de2518SJohan Hovold 	spin_lock_irqsave(&edge_port->ep_lock, flags);
1876ddca16e4SJohan Hovold 	kfifo_reset_out(&port->write_fifo);
187777de2518SJohan Hovold 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
18781da177e4SLinus Torvalds 
187967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
18801143832eSGreg Kroah-Hartman 	port_number = port->port_number;
1881bca87e9eSJohan Hovold 	send_cmd(serial->dev, UMPC_CLOSE_PORT,
1882bca87e9eSJohan Hovold 		     (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
1883af581059SJohan Hovold 
1884241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
18851da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
18861da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
18871da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
18881da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
18891da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
18901da177e4SLinus Torvalds 	}
1891241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
18921da177e4SLinus Torvalds 	edge_port->close_pending = 0;
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
189595da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
189695da310eSAlan Cox 				const unsigned char *data, int count)
18971da177e4SLinus Torvalds {
18981da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	if (count == 0) {
190167e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
19021da177e4SLinus Torvalds 		return 0;
19031da177e4SLinus Torvalds 	}
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	if (edge_port == NULL)
19061da177e4SLinus Torvalds 		return -ENODEV;
19071da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
19081da177e4SLinus Torvalds 		return -ENODEV;
19091da177e4SLinus Torvalds 
1910ddca16e4SJohan Hovold 	count = kfifo_in_locked(&port->write_fifo, data, count,
1911d733cec1SJohan Hovold 							&edge_port->ep_lock);
1912ae3759c2SJiri Slaby 	edge_send(port, tty);
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	return count;
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds 
1917ae3759c2SJiri Slaby static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
19181da177e4SLinus Torvalds {
19191da177e4SLinus Torvalds 	int count, result;
19201da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19211da177e4SLinus Torvalds 	unsigned long flags;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
19261da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19271da177e4SLinus Torvalds 		return;
19281da177e4SLinus Torvalds 	}
19291da177e4SLinus Torvalds 
1930ddca16e4SJohan Hovold 	count = kfifo_out(&port->write_fifo,
19311da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
19321da177e4SLinus Torvalds 				port->bulk_out_size);
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	if (count == 0) {
19351da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19361da177e4SLinus Torvalds 		return;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19421da177e4SLinus Torvalds 
194359d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 	/* set up our urb */
1946fd11961aSJohan Hovold 	port->write_urb->transfer_buffer_length = count;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	/* send the data out the bulk port */
19491da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
19501da177e4SLinus Torvalds 	if (result) {
195122a416c4SJohan Hovold 		dev_err_console(port,
19522742fd88SAlan Cox 			"%s - failed submitting write urb, error %d\n",
19532742fd88SAlan Cox 				__func__, result);
19541da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
19552742fd88SAlan Cox 		/* TODO: reschedule edge_send */
19562742fd88SAlan Cox 	} else
1957cf9a9d66SJohan Hovold 		edge_port->port->icount.tx += count;
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
19601da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
19612742fd88SAlan Cox 	if (tty)
19621da177e4SLinus Torvalds 		tty_wakeup(tty);
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
196595da310eSAlan Cox static int edge_write_room(struct tty_struct *tty)
19661da177e4SLinus Torvalds {
196795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
19681da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19691da177e4SLinus Torvalds 	int room = 0;
19701da177e4SLinus Torvalds 	unsigned long flags;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	if (edge_port == NULL)
19732742fd88SAlan Cox 		return 0;
19741da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
19752742fd88SAlan Cox 		return 0;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
1978ddca16e4SJohan Hovold 	room = kfifo_avail(&port->write_fifo);
19791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19801da177e4SLinus Torvalds 
198167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
19821da177e4SLinus Torvalds 	return room;
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
198595da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty)
19861da177e4SLinus Torvalds {
198795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
19881da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19891da177e4SLinus Torvalds 	int chars = 0;
19901da177e4SLinus Torvalds 	unsigned long flags;
19911da177e4SLinus Torvalds 	if (edge_port == NULL)
19922742fd88SAlan Cox 		return 0;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
1995ddca16e4SJohan Hovold 	chars = kfifo_len(&port->write_fifo);
19961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
19971da177e4SLinus Torvalds 
199867e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
19991da177e4SLinus Torvalds 	return chars;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
2002b16634adSJohan Hovold static bool edge_tx_empty(struct usb_serial_port *port)
2003b16634adSJohan Hovold {
2004b16634adSJohan Hovold 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2005b16634adSJohan Hovold 	int ret;
2006b16634adSJohan Hovold 
2007b16634adSJohan Hovold 	ret = tx_active(edge_port);
2008b16634adSJohan Hovold 	if (ret > 0)
2009b16634adSJohan Hovold 		return false;
2010b16634adSJohan Hovold 
2011b16634adSJohan Hovold 	return true;
2012b16634adSJohan Hovold }
2013b16634adSJohan Hovold 
201495da310eSAlan Cox static void edge_throttle(struct tty_struct *tty)
20151da177e4SLinus Torvalds {
201695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20171da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20181da177e4SLinus Torvalds 	int status;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	if (edge_port == NULL)
20211da177e4SLinus Torvalds 		return;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
20241da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
20251da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
202695da310eSAlan Cox 		status = edge_write(tty, port, &stop_char, 1);
202795da310eSAlan Cox 		if (status <= 0) {
202895da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
202995da310eSAlan Cox 		}
20301da177e4SLinus Torvalds 	}
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
20331da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
20341da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
20351da177e4SLinus Torvalds 		stop_read(edge_port);
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
203995da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty)
20401da177e4SLinus Torvalds {
204195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20421da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20431da177e4SLinus Torvalds 	int status;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	if (edge_port == NULL)
20461da177e4SLinus Torvalds 		return;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
20491da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
20501da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
205195da310eSAlan Cox 		status = edge_write(tty, port, &start_char, 1);
205295da310eSAlan Cox 		if (status <= 0) {
205395da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
205495da310eSAlan Cox 		}
20551da177e4SLinus Torvalds 	}
20561da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
20571da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
20581da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
20591da177e4SLinus Torvalds 		status = restart_read(edge_port);
20601da177e4SLinus Torvalds 		if (status)
20612742fd88SAlan Cox 			dev_err(&port->dev,
20622742fd88SAlan Cox 				"%s - read bulk usb_submit_urb failed: %d\n",
20632742fd88SAlan Cox 							__func__, status);
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	unsigned long flags;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
20751da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
20761da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
20821da177e4SLinus Torvalds {
20831da177e4SLinus Torvalds 	struct urb *urb;
20841da177e4SLinus Torvalds 	int status = 0;
20851da177e4SLinus Torvalds 	unsigned long flags;
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
20901da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
2091efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
20921da177e4SLinus Torvalds 	}
20931da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
20941da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	return status;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
210195da310eSAlan Cox static void change_port_settings(struct tty_struct *tty,
210295da310eSAlan Cox 		struct edgeport_port *edge_port, struct ktermios *old_termios)
21031da177e4SLinus Torvalds {
210467e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
21051da177e4SLinus Torvalds 	struct ump_uart_config *config;
21061da177e4SLinus Torvalds 	int baud;
21071da177e4SLinus Torvalds 	unsigned cflag;
21081da177e4SLinus Torvalds 	int status;
21091143832eSGreg Kroah-Hartman 	int port_number = edge_port->port->port_number;
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
21121da177e4SLinus Torvalds 	if (!config) {
2113adc8d746SAlan Cox 		tty->termios = *old_termios;
21141da177e4SLinus Torvalds 		return;
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 
2117adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	config->wFlags = 0;
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds 	/* These flags must be set */
21221da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
21231da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
21241da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
21271da177e4SLinus Torvalds 	case CS5:
21281da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR5BITS;
212967e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
21301da177e4SLinus Torvalds 		    break;
21311da177e4SLinus Torvalds 	case CS6:
21321da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR6BITS;
213367e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
21341da177e4SLinus Torvalds 		    break;
21351da177e4SLinus Torvalds 	case CS7:
21361da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR7BITS;
213767e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
21381da177e4SLinus Torvalds 		    break;
21391da177e4SLinus Torvalds 	default:
21401da177e4SLinus Torvalds 	case CS8:
21411da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR8BITS;
214267e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
21431da177e4SLinus Torvalds 			    break;
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	if (cflag & PARENB) {
21471da177e4SLinus Torvalds 		if (cflag & PARODD) {
21481da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
21491da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
215067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = odd\n", __func__);
21511da177e4SLinus Torvalds 		} else {
21521da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
21531da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
215467e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = even\n", __func__);
21551da177e4SLinus Torvalds 		}
21561da177e4SLinus Torvalds 	} else {
21571da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
215867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = none\n", __func__);
21591da177e4SLinus Torvalds 	}
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
21621da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
216367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
21641da177e4SLinus Torvalds 	} else {
21651da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
216667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
21671da177e4SLinus Torvalds 	}
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	/* figure out the flow control settings */
21701da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
21711da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
21721da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
217367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
21741da177e4SLinus Torvalds 	} else {
217567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
21761da177e4SLinus Torvalds 		restart_read(edge_port);
21771da177e4SLinus Torvalds 	}
21781da177e4SLinus Torvalds 
21792742fd88SAlan Cox 	/* if we are implementing XON/XOFF, set the start and stop
21802742fd88SAlan Cox 	   character in the device */
21811da177e4SLinus Torvalds 	config->cXon  = START_CHAR(tty);
21821da177e4SLinus Torvalds 	config->cXoff = STOP_CHAR(tty);
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	/* if we are implementing INBOUND XON/XOFF */
21851da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21861da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
218767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2188441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
21892742fd88SAlan Cox 	} else
219067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	/* if we are implementing OUTBOUND XON/XOFF */
21931da177e4SLinus Torvalds 	if (I_IXON(tty)) {
21941da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
219567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2196441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
21972742fd88SAlan Cox 	} else
219867e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
21991da177e4SLinus Torvalds 
2200adc8d746SAlan Cox 	tty->termios.c_cflag &= ~CMSPAR;
2201d5f5bcd4SAlan Cox 
22021da177e4SLinus Torvalds 	/* Round the baud rate */
22031da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
22041da177e4SLinus Torvalds 	if (!baud) {
22051da177e4SLinus Torvalds 		/* pick a default, any default... */
22061da177e4SLinus Torvalds 		baud = 9600;
2207d5f5bcd4SAlan Cox 	} else
2208d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
2209d5f5bcd4SAlan Cox 
22101da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
22111da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
22121da177e4SLinus Torvalds 
2213d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2214d5f5bcd4SAlan Cox 
221567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
22161da177e4SLinus Torvalds 
221767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate:   %d\n", (int)(461550L / config->wBaudRate));
221867e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wFlags:    0x%x\n", config->wFlags);
221967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bDataBits:   %d\n", config->bDataBits);
222067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bParity:     %d\n", config->bParity);
222167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bStopBits:   %d\n", config->bStopBits);
222267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXon:        %d\n", config->cXon);
222367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXoff:       %d\n", config->cXoff);
222467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bUartMode:   %d\n", config->bUartMode);
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	/* move the word values into big endian mode */
22271da177e4SLinus Torvalds 	cpu_to_be16s(&config->wFlags);
22281da177e4SLinus Torvalds 	cpu_to_be16s(&config->wBaudRate);
22291da177e4SLinus Torvalds 
22302742fd88SAlan Cox 	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
22311da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
22322742fd88SAlan Cox 				0, (__u8 *)config, sizeof(*config));
22332742fd88SAlan Cox 	if (status)
223467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2235441b62c1SHarvey Harrison 			__func__, status);
22361da177e4SLinus Torvalds 	kfree(config);
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds 
223995da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
224095da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
224395da310eSAlan Cox 	unsigned int cflag;
224495da310eSAlan Cox 
2245adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
22461da177e4SLinus Torvalds 
224767e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2248adc8d746SAlan Cox 		tty->termios.c_cflag, tty->termios.c_iflag);
224967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
22501da177e4SLinus Torvalds 		old_termios->c_cflag, old_termios->c_iflag);
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	if (edge_port == NULL)
22531da177e4SLinus Torvalds 		return;
22541da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
225595da310eSAlan Cox 	change_port_settings(tty, edge_port, old_termios);
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
225820b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty,
22592742fd88SAlan Cox 					unsigned int set, unsigned int clear)
22601da177e4SLinus Torvalds {
226195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22621da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22631da177e4SLinus Torvalds 	unsigned int mcr;
22643d71fe0bSAlan Cox 	unsigned long flags;
22651da177e4SLinus Torvalds 
22663d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22671da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
22681da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
22691da177e4SLinus Torvalds 		mcr |= MCR_RTS;
22701da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
22711da177e4SLinus Torvalds 		mcr |= MCR_DTR;
22721da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
22731da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
22761da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
22771da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
22781da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
22791da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
22801da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
22833d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22841da177e4SLinus Torvalds 
22852742fd88SAlan Cox 	restore_mcr(edge_port, mcr);
22861da177e4SLinus Torvalds 	return 0;
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
228960b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty)
22901da177e4SLinus Torvalds {
229195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22921da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22931da177e4SLinus Torvalds 	unsigned int result = 0;
22941da177e4SLinus Torvalds 	unsigned int msr;
22951da177e4SLinus Torvalds 	unsigned int mcr;
22963d71fe0bSAlan Cox 	unsigned long flags;
22971da177e4SLinus Torvalds 
22983d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22993d71fe0bSAlan Cox 
23001da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
23011da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
23021da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
23031da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
23041da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
23051da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
23061da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
23071da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 
231067e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
23113d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	return result;
23141da177e4SLinus Torvalds }
23151da177e4SLinus Torvalds 
23162742fd88SAlan Cox static int get_serial_info(struct edgeport_port *edge_port,
23172742fd88SAlan Cox 				struct serial_struct __user *retinfo)
23181da177e4SLinus Torvalds {
23191da177e4SLinus Torvalds 	struct serial_struct tmp;
2320f40d7815SJohan Hovold 	unsigned cwait;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	if (!retinfo)
23231da177e4SLinus Torvalds 		return -EFAULT;
23241da177e4SLinus Torvalds 
2325f40d7815SJohan Hovold 	cwait = edge_port->port->port.closing_wait;
2326f40d7815SJohan Hovold 	if (cwait != ASYNC_CLOSING_WAIT_NONE)
2327b6fd35eeSJohan Hovold 		cwait = jiffies_to_msecs(cwait) / 10;
2328f40d7815SJohan Hovold 
23291da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
2332e5b1e206SGreg Kroah-Hartman 	tmp.line		= edge_port->port->minor;
23331143832eSGreg Kroah-Hartman 	tmp.port		= edge_port->port->port_number;
23341da177e4SLinus Torvalds 	tmp.irq			= 0;
23351da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
23361da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
23371da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
23381da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
2339f40d7815SJohan Hovold 	tmp.closing_wait	= cwait;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
23421da177e4SLinus Torvalds 		return -EFAULT;
23431da177e4SLinus Torvalds 	return 0;
23441da177e4SLinus Torvalds }
23451da177e4SLinus Torvalds 
234600a0d0d6SAlan Cox static int edge_ioctl(struct tty_struct *tty,
23472742fd88SAlan Cox 					unsigned int cmd, unsigned long arg)
23481da177e4SLinus Torvalds {
234995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23501da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	switch (cmd) {
23531da177e4SLinus Torvalds 	case TIOCGSERIAL:
235467e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
23552742fd88SAlan Cox 		return get_serial_info(edge_port,
23562742fd88SAlan Cox 				(struct serial_struct __user *) arg);
23571da177e4SLinus Torvalds 	}
23581da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
23591da177e4SLinus Torvalds }
23601da177e4SLinus Torvalds 
236195da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state)
23621da177e4SLinus Torvalds {
236395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23641da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23651da177e4SLinus Torvalds 	int status;
23662742fd88SAlan Cox 	int bv = 0;	/* Off */
23671da177e4SLinus Torvalds 
236895da310eSAlan Cox 	if (break_state == -1)
23692742fd88SAlan Cox 		bv = 1;	/* On */
23702742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
23712742fd88SAlan Cox 	if (status)
237267e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2373441b62c1SHarvey Harrison 			__func__, status);
23741da177e4SLinus Torvalds }
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
23791da177e4SLinus Torvalds 	int status;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	/* create our private serial structure */
238280b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
238310c642d0SJohan Hovold 	if (!edge_serial)
23841da177e4SLinus Torvalds 		return -ENOMEM;
238510c642d0SJohan Hovold 
2386241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
23871da177e4SLinus Torvalds 	edge_serial->serial = serial;
23881da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
23891da177e4SLinus Torvalds 
23902742fd88SAlan Cox 	status = download_fw(edge_serial);
23911da177e4SLinus Torvalds 	if (status) {
23921da177e4SLinus Torvalds 		kfree(edge_serial);
23931da177e4SLinus Torvalds 		return status;
23941da177e4SLinus Torvalds 	}
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	return 0;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
2399f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial)
24001da177e4SLinus Torvalds {
2401f9c99bb8SAlan Stern }
2402f9c99bb8SAlan Stern 
2403f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial)
2404f9c99bb8SAlan Stern {
24051da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
240800361530SJohan Hovold static int edge_port_probe(struct usb_serial_port *port)
240900361530SJohan Hovold {
241000361530SJohan Hovold 	struct edgeport_port *edge_port;
241100361530SJohan Hovold 	int ret;
241200361530SJohan Hovold 
241300361530SJohan Hovold 	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
241400361530SJohan Hovold 	if (!edge_port)
241500361530SJohan Hovold 		return -ENOMEM;
241600361530SJohan Hovold 
241700361530SJohan Hovold 	spin_lock_init(&edge_port->ep_lock);
241800361530SJohan Hovold 	edge_port->port = port;
241900361530SJohan Hovold 	edge_port->edge_serial = usb_get_serial_data(port->serial);
242000361530SJohan Hovold 	edge_port->bUartMode = default_uart_mode;
242100361530SJohan Hovold 
24220fce06daSJohan Hovold 	switch (port->port_number) {
24230fce06daSJohan Hovold 	case 0:
24240fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART1;
24250fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
24260fce06daSJohan Hovold 		break;
24270fce06daSJohan Hovold 	case 1:
24280fce06daSJohan Hovold 		edge_port->uart_base = UMPMEM_BASE_UART2;
24290fce06daSJohan Hovold 		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
24300fce06daSJohan Hovold 		break;
24310fce06daSJohan Hovold 	default:
24320fce06daSJohan Hovold 		dev_err(&port->dev, "unknown port number\n");
24330fce06daSJohan Hovold 		ret = -ENODEV;
24340fce06daSJohan Hovold 		goto err;
24350fce06daSJohan Hovold 	}
24360fce06daSJohan Hovold 
24370fce06daSJohan Hovold 	dev_dbg(&port->dev,
24380fce06daSJohan Hovold 		"%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
24390fce06daSJohan Hovold 		__func__, port->port_number, edge_port->uart_base,
24400fce06daSJohan Hovold 		edge_port->dma_address);
24410fce06daSJohan Hovold 
244200361530SJohan Hovold 	usb_set_serial_port_data(port, edge_port);
244300361530SJohan Hovold 
24445d8c61bcSJohan Hovold 	ret = edge_create_sysfs_attrs(port);
24450fce06daSJohan Hovold 	if (ret)
24460fce06daSJohan Hovold 		goto err;
24475d8c61bcSJohan Hovold 
2448f40d7815SJohan Hovold 	port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
2449d7be6221SJohan Hovold 	port->port.drain_delay = 1;
2450f40d7815SJohan Hovold 
245100361530SJohan Hovold 	return 0;
24520fce06daSJohan Hovold err:
24530fce06daSJohan Hovold 	kfree(edge_port);
24540fce06daSJohan Hovold 
24550fce06daSJohan Hovold 	return ret;
245600361530SJohan Hovold }
245700361530SJohan Hovold 
245800361530SJohan Hovold static int edge_port_remove(struct usb_serial_port *port)
245900361530SJohan Hovold {
246000361530SJohan Hovold 	struct edgeport_port *edge_port;
246100361530SJohan Hovold 
246200361530SJohan Hovold 	edge_port = usb_get_serial_port_data(port);
246300361530SJohan Hovold 	edge_remove_sysfs_attrs(port);
246400361530SJohan Hovold 	kfree(edge_port);
246500361530SJohan Hovold 
246600361530SJohan Hovold 	return 0;
246700361530SJohan Hovold }
24681da177e4SLinus Torvalds 
2469fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2470fc4cbd75SMartin K. Petersen 
2471154547c4SGreg Kroah-Hartman static ssize_t uart_mode_show(struct device *dev,
2472fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2473fc4cbd75SMartin K. Petersen {
2474fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2475fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2476fc4cbd75SMartin K. Petersen 
2477fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2478fc4cbd75SMartin K. Petersen }
2479fc4cbd75SMartin K. Petersen 
2480154547c4SGreg Kroah-Hartman static ssize_t uart_mode_store(struct device *dev,
2481fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2482fc4cbd75SMartin K. Petersen {
2483fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2484fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2485fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2486fc4cbd75SMartin K. Petersen 
248767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2488fc4cbd75SMartin K. Petersen 
2489fc4cbd75SMartin K. Petersen 	if (v < 256)
2490fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2491fc4cbd75SMartin K. Petersen 	else
2492441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2493fc4cbd75SMartin K. Petersen 
2494fc4cbd75SMartin K. Petersen 	return count;
2495fc4cbd75SMartin K. Petersen }
2496154547c4SGreg Kroah-Hartman static DEVICE_ATTR_RW(uart_mode);
2497fc4cbd75SMartin K. Petersen 
2498fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2499fc4cbd75SMartin K. Petersen {
2500fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2501fc4cbd75SMartin K. Petersen }
2502fc4cbd75SMartin K. Petersen 
2503fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2504fc4cbd75SMartin K. Petersen {
2505fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2506fc4cbd75SMartin K. Petersen 	return 0;
2507fc4cbd75SMartin K. Petersen }
2508fc4cbd75SMartin K. Petersen 
2509fc4cbd75SMartin K. Petersen 
2510ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
251118fcac35SGreg Kroah-Hartman 	.driver = {
25121da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2513269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
251418fcac35SGreg Kroah-Hartman 	},
2515269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
25161da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
25171da177e4SLinus Torvalds 	.num_ports		= 1,
25181da177e4SLinus Torvalds 	.open			= edge_open,
25191da177e4SLinus Torvalds 	.close			= edge_close,
25201da177e4SLinus Torvalds 	.throttle		= edge_throttle,
25211da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
25221da177e4SLinus Torvalds 	.attach			= edge_startup,
2523f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2524f9c99bb8SAlan Stern 	.release		= edge_release,
252500361530SJohan Hovold 	.port_probe		= edge_port_probe,
252600361530SJohan Hovold 	.port_remove		= edge_port_remove,
25271da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
25281da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
25291da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
25301da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
253148ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2532cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
25331da177e4SLinus Torvalds 	.write			= edge_write,
25341da177e4SLinus Torvalds 	.write_room		= edge_write_room,
25351da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2536b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
25371da177e4SLinus Torvalds 	.break_ctl		= edge_break,
25381da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
25391da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
25401da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
25411da177e4SLinus Torvalds };
25421da177e4SLinus Torvalds 
2543ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
254418fcac35SGreg Kroah-Hartman 	.driver = {
25451da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2546269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
254718fcac35SGreg Kroah-Hartman 	},
2548269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
25491da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
25501da177e4SLinus Torvalds 	.num_ports		= 2,
25511da177e4SLinus Torvalds 	.open			= edge_open,
25521da177e4SLinus Torvalds 	.close			= edge_close,
25531da177e4SLinus Torvalds 	.throttle		= edge_throttle,
25541da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
25551da177e4SLinus Torvalds 	.attach			= edge_startup,
2556f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2557f9c99bb8SAlan Stern 	.release		= edge_release,
255800361530SJohan Hovold 	.port_probe		= edge_port_probe,
255900361530SJohan Hovold 	.port_remove		= edge_port_remove,
25601da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
25611da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
25621da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
25631da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
256448ee5801SJohan Hovold 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
2565cf9a9d66SJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
25661da177e4SLinus Torvalds 	.write			= edge_write,
25671da177e4SLinus Torvalds 	.write_room		= edge_write_room,
25681da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
2569b16634adSJohan Hovold 	.tx_empty		= edge_tx_empty,
25701da177e4SLinus Torvalds 	.break_ctl		= edge_break,
25711da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
25721da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
25731da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
25741da177e4SLinus Torvalds };
25751da177e4SLinus Torvalds 
25767dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
25777dbe2460SAlan Stern 	&edgeport_1port_device, &edgeport_2port_device, NULL
25787dbe2460SAlan Stern };
25797dbe2460SAlan Stern 
258068e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
25831da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
25841da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2585d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
25881da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
25912742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev,
25922742fd88SAlan Cox 			"Ignore the cpu revision when connecting to a device");
25931da177e4SLinus Torvalds 
2594fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2595fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
2596