xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision d12b219a228efe92f0778ed3af21305e65fbb052)
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  * Version history:
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	July 11, 2002 	Removed 4 port device structure since all TI UMP
221da177e4SLinus Torvalds  *			chips have only 2 ports
231da177e4SLinus Torvalds  *			David Iacovelli (davidi@ionetworks.com)
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/jiffies.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/tty.h>
331da177e4SLinus Torvalds #include <linux/tty_driver.h>
341da177e4SLinus Torvalds #include <linux/tty_flip.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/spinlock.h>
37241ca64fSMatthias Kaehlcke #include <linux/mutex.h>
381da177e4SLinus Torvalds #include <linux/serial.h>
391da177e4SLinus Torvalds #include <linux/ioctl.h>
40*d12b219aSJaswinder Singh #include <linux/firmware.h>
411da177e4SLinus Torvalds #include <asm/uaccess.h>
421da177e4SLinus Torvalds #include <linux/usb.h>
43a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include "io_16654.h"
461da177e4SLinus Torvalds #include "io_usbvend.h"
471da177e4SLinus Torvalds #include "io_ti.h"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * Version Information
511da177e4SLinus Torvalds  */
52fc4cbd75SMartin K. Petersen #define DRIVER_VERSION "v0.7mode043006"
531da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
541da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define EPROM_PAGE_SIZE		64
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct edgeport_uart_buf_desc {
601da177e4SLinus Torvalds 	__u32 count;		// Number of bytes currently in buffer
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* different hardware types */
641da177e4SLinus Torvalds #define HARDWARE_TYPE_930	0
651da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP	1
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds // IOCTL_PRIVATE_TI_GET_MODE Definitions
681da177e4SLinus Torvalds #define	TI_MODE_CONFIGURING	0   // Device has not entered start device
691da177e4SLinus Torvalds #define	TI_MODE_BOOT		1   // Staying in boot mode
701da177e4SLinus Torvalds #define TI_MODE_DOWNLOAD	2   // Made it to download mode
711da177e4SLinus Torvalds #define TI_MODE_TRANSITIONING	3   // Currently in boot mode but transitioning to download mode
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* read urb state */
741da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
751da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
761da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define EDGE_LOW_LATENCY	1
791da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT	4000	/* in .01 sec */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define EDGE_OUT_BUF_SIZE	1024
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* Product information read from the Edgeport */
851da177e4SLinus Torvalds struct product_info
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	int	TiMode;			// Current TI Mode
881da177e4SLinus Torvalds 	__u8	hardware_type;		// Type of hardware
891da177e4SLinus Torvalds } __attribute__((packed));
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* circular buffer */
921da177e4SLinus Torvalds struct edge_buf {
931da177e4SLinus Torvalds 	unsigned int	buf_size;
941da177e4SLinus Torvalds 	char		*buf_buf;
951da177e4SLinus Torvalds 	char		*buf_get;
961da177e4SLinus Torvalds 	char		*buf_put;
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds struct edgeport_port {
1001da177e4SLinus Torvalds 	__u16 uart_base;
1011da177e4SLinus Torvalds 	__u16 dma_address;
1021da177e4SLinus Torvalds 	__u8 shadow_msr;
1031da177e4SLinus Torvalds 	__u8 shadow_mcr;
1041da177e4SLinus Torvalds 	__u8 shadow_lsr;
1051da177e4SLinus Torvalds 	__u8 lsr_mask;
1061da177e4SLinus Torvalds 	__u32 ump_read_timeout;		/* Number of miliseconds the UMP will
1071da177e4SLinus Torvalds 					   wait without data before completing
1081da177e4SLinus Torvalds 					   a read short */
1091da177e4SLinus Torvalds 	int baud_rate;
1101da177e4SLinus Torvalds 	int close_pending;
1111da177e4SLinus Torvalds 	int lsr_event;
1121da177e4SLinus Torvalds 	struct edgeport_uart_buf_desc tx;
1131da177e4SLinus Torvalds 	struct async_icount	icount;
1141da177e4SLinus Torvalds 	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
1151da177e4SLinus Torvalds 						   waiting for msr change to
1161da177e4SLinus Torvalds 						   happen */
1171da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
1181da177e4SLinus Torvalds 	struct usb_serial_port	*port;
1191da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
1201da177e4SLinus Torvalds 	spinlock_t ep_lock;
1211da177e4SLinus Torvalds 	int ep_read_urb_state;
1221da177e4SLinus Torvalds 	int ep_write_urb_in_use;
1231da177e4SLinus Torvalds 	struct edge_buf *ep_out_buf;
1241da177e4SLinus Torvalds };
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds struct edgeport_serial {
1271da177e4SLinus Torvalds 	struct product_info product_info;
1281da177e4SLinus Torvalds 	u8 TI_I2C_Type;			// Type of I2C in UMP
1291da177e4SLinus Torvalds 	u8 TiReadI2C;			// Set to TRUE if we have read the I2c in Boot Mode
130241ca64fSMatthias Kaehlcke 	struct mutex es_lock;
1311da177e4SLinus Torvalds 	int num_ports_open;
1321da177e4SLinus Torvalds 	struct usb_serial *serial;
1331da177e4SLinus Torvalds };
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /* Devices that this driver supports */
1371da177e4SLinus Torvalds static struct usb_device_id edgeport_1port_id_table [] = {
1381da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1391da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1401da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1411da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1421da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1431da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1441da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1451da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1461da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1471da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1481da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1491da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1501da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1511da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1521da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1531da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1541da177e4SLinus Torvalds 	{ }
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static struct usb_device_id edgeport_2port_id_table [] = {
1581da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1591da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1601da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1611da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1621da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1631da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1641da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1651da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1661da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1671da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1681da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1691da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
170fc4cbd75SMartin K. Petersen 	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1711da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
172fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
173fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
174fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
175fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1761da177e4SLinus Torvalds 	{ }
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /* Devices that this driver supports */
1801da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = {
1811da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1821da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1831da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1841da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1851da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1861da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1911da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1921da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1931da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1941da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1951da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1961da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1971da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1981da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1991da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
2001da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
2011da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
2021da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
2031da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
2041da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
2051da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
2061da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
2071da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
2081da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
2091da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
210fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
211fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
212fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
213fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
2141da177e4SLinus Torvalds 	{ }
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table_combined);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static struct usb_driver io_driver = {
2201da177e4SLinus Torvalds 	.name =		"io_ti",
2211da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
2221da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
2231da177e4SLinus Torvalds 	.id_table =	id_table_combined,
224ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
2251da177e4SLinus Torvalds };
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 
228*d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion;
229*d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion;
230*d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static int debug;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int TIStayInBootMode = 0;
2351da177e4SLinus Torvalds static int low_latency = EDGE_LOW_LATENCY;
2361da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
2371da177e4SLinus Torvalds static int ignore_cpu_rev = 0;
238fc4cbd75SMartin K. Petersen static int default_uart_mode = 0;	/* RS232 */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2441da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2451da177e4SLinus Torvalds 
246606d099cSAlan Cox static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
2471da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port);
2481da177e4SLinus Torvalds 
249fc4cbd75SMartin K. Petersen /* sysfs attributes */
250fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
251fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
252fc4cbd75SMartin K. Petersen 
2531da177e4SLinus Torvalds /* circular buffer */
2541da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size);
2551da177e4SLinus Torvalds static void edge_buf_free(struct edge_buf *eb);
2561da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb);
2571da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb);
2581da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb);
2591da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2601da177e4SLinus Torvalds 	unsigned int count);
2611da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2621da177e4SLinus Torvalds 	unsigned int count);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds static int TIReadVendorRequestSync (struct usb_device *dev,
2661da177e4SLinus Torvalds 				__u8		request,
2671da177e4SLinus Torvalds 				__u16		value,
2681da177e4SLinus Torvalds 				__u16		index,
2691da177e4SLinus Torvalds 				u8 		*data,
2701da177e4SLinus Torvalds 				int		size)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	int status;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	status = usb_control_msg (dev,
2751da177e4SLinus Torvalds 				usb_rcvctrlpipe(dev, 0),
2761da177e4SLinus Torvalds 				request,
2771da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
2781da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
2791da177e4SLinus Torvalds 				 USB_DIR_IN),
2801da177e4SLinus Torvalds 				value,
2811da177e4SLinus Torvalds 				index,
2821da177e4SLinus Torvalds 				data,
2831da177e4SLinus Torvalds 				size,
2841da177e4SLinus Torvalds 				1000);
2851da177e4SLinus Torvalds 	if (status < 0)
2861da177e4SLinus Torvalds 		return status;
2871da177e4SLinus Torvalds 	if (status != size) {
2881da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
289441b62c1SHarvey Harrison 		     __func__, size, status);
2901da177e4SLinus Torvalds 		return -ECOMM;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int TISendVendorRequestSync (struct usb_device *dev,
2961da177e4SLinus Torvalds 				__u8		request,
2971da177e4SLinus Torvalds 				__u16		value,
2981da177e4SLinus Torvalds 				__u16		index,
2991da177e4SLinus Torvalds 				u8 		*data,
3001da177e4SLinus Torvalds 				int		size)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	int status;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	status = usb_control_msg (dev,
3051da177e4SLinus Torvalds 				usb_sndctrlpipe(dev, 0),
3061da177e4SLinus Torvalds 				request,
3071da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
3081da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
3091da177e4SLinus Torvalds 				 USB_DIR_OUT),
3101da177e4SLinus Torvalds 				value,
3111da177e4SLinus Torvalds 				index,
3121da177e4SLinus Torvalds 				data,
3131da177e4SLinus Torvalds 				size,
3141da177e4SLinus Torvalds 				1000);
3151da177e4SLinus Torvalds 	if (status < 0)
3161da177e4SLinus Torvalds 		return status;
3171da177e4SLinus Torvalds 	if (status != size) {
3181da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
319441b62c1SHarvey Harrison 		     __func__, size, status);
3201da177e4SLinus Torvalds 		return -ECOMM;
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 	return 0;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
3261da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
3271da177e4SLinus Torvalds 				int size)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	return TISendVendorRequestSync (dev,
3301da177e4SLinus Torvalds 					  command,	  		// Request
3311da177e4SLinus Torvalds 					  value,			// wValue
3321da177e4SLinus Torvalds 					  moduleid,			// wIndex
3331da177e4SLinus Torvalds 					  data,				// TransferBuffer
3341da177e4SLinus Torvalds 					  size);			// TransferBufferLength
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
3391da177e4SLinus Torvalds static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	int port_number = port->number - port->serial->minor;
3421da177e4SLinus Torvalds 
343441b62c1SHarvey Harrison 	dbg ("%s - port %d, mask %x", __func__, port_number, mask);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	return TIWriteCommandSync (port->serial->dev,
3461da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
3471da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
3481da177e4SLinus Torvalds 					mask,
3491da177e4SLinus Torvalds 					NULL,
3501da177e4SLinus Torvalds 					0);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /**
3541da177e4SLinus Torvalds  * TIReadDownloadMemory - Read edgeport memory from TI chip
3551da177e4SLinus Torvalds  * @dev: usb device pointer
3561da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3571da177e4SLinus Torvalds  * @length: Length of above data
3581da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3591da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3601da177e4SLinus Torvalds  */
3611da177e4SLinus Torvalds static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
3621da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	int status = 0;
3651da177e4SLinus Torvalds 	__u8 read_length;
3661da177e4SLinus Torvalds 	__be16 be_start_address;
3671da177e4SLinus Torvalds 
368441b62c1SHarvey Harrison 	dbg ("%s - @ %x for %d", __func__, start_address, length);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
3711da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3721da177e4SLinus Torvalds 	 */
3731da177e4SLinus Torvalds 	while (length) {
3741da177e4SLinus Torvalds 		if (length > 64)
3751da177e4SLinus Torvalds 			read_length= 64;
3761da177e4SLinus Torvalds 		else
3771da177e4SLinus Torvalds 			read_length = (__u8)length;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		if (read_length > 1) {
380441b62c1SHarvey Harrison 			dbg ("%s - @ %x for %d", __func__,
3811da177e4SLinus Torvalds 			     start_address, read_length);
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
3841da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (dev,
3851da177e4SLinus Torvalds 						  UMPC_MEMORY_READ,	// Request
3861da177e4SLinus Torvalds 						  (__u16)address_type,	// wValue (Address type)
3871da177e4SLinus Torvalds 						  (__force __u16)be_start_address,	// wIndex (Address to read)
3881da177e4SLinus Torvalds 						  buffer,		// TransferBuffer
3891da177e4SLinus Torvalds 						  read_length);	// TransferBufferLength
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		if (status) {
392441b62c1SHarvey Harrison 			dbg ("%s - ERROR %x", __func__, status);
3931da177e4SLinus Torvalds 			return status;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		if (read_length > 1) {
397441b62c1SHarvey Harrison 			usb_serial_debug_data(debug, &dev->dev, __func__,
3981da177e4SLinus Torvalds 					      read_length, buffer);
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		/* Update pointers/length */
4021da177e4SLinus Torvalds 		start_address += read_length;
4031da177e4SLinus Torvalds 		buffer += read_length;
4041da177e4SLinus Torvalds 		length -= read_length;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	return status;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	return TIReadDownloadMemory (dev,
4131da177e4SLinus Torvalds 				     start_address,
4141da177e4SLinus Torvalds 				     length,
4151da177e4SLinus Torvalds 				     DTK_ADDR_SPACE_XDATA,
4161da177e4SLinus Torvalds 				     buffer);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds /* Read edgeport memory to a given block */
4201da177e4SLinus Torvalds static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	int status = 0;
4231da177e4SLinus Torvalds 	int i;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	for (i=0; i< length; i++) {
4261da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (serial->serial->dev,
4271da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
4281da177e4SLinus Torvalds 					serial->TI_I2C_Type,		// wValue (Address type)
4291da177e4SLinus Torvalds 					(__u16)(start_address+i),	// wIndex
4301da177e4SLinus Torvalds 					&buffer[i],			// TransferBuffer
4311da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
4321da177e4SLinus Torvalds 		if (status) {
433441b62c1SHarvey Harrison 			dbg ("%s - ERROR %x", __func__, status);
4341da177e4SLinus Torvalds 			return status;
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
438441b62c1SHarvey Harrison 	dbg ("%s - start_address = %x, length = %d", __func__, start_address, length);
439441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	return status;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
4471da177e4SLinus Torvalds static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	int status = 0;
4501da177e4SLinus Torvalds 	int i;
4511da177e4SLinus Torvalds 	__u8 temp;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* Must do a read before write */
4541da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
4551da177e4SLinus Torvalds 		status = TIReadBootMemory(serial, 0, 1, &temp);
4561da177e4SLinus Torvalds 		if (status)
4571da177e4SLinus Torvalds 			return status;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	for (i=0; i < length; ++i) {
4611da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
4621da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,		// Request
4631da177e4SLinus Torvalds 						buffer[i],			// wValue
4641da177e4SLinus Torvalds 						(__u16)(i+start_address),	// wIndex
4651da177e4SLinus Torvalds 						NULL,				// TransferBuffer
4661da177e4SLinus Torvalds 						0);				// TransferBufferLength
4671da177e4SLinus Torvalds 		if (status)
4681da177e4SLinus Torvalds 			return status;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
471441b62c1SHarvey Harrison   	dbg ("%s - start_sddr = %x, length = %d", __func__, start_address, length);
472441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return status;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4791da177e4SLinus Torvalds static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	int status = 0;
4821da177e4SLinus Torvalds 	int write_length;
4831da177e4SLinus Torvalds 	__be16 be_start_address;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* calulate the number of bytes left in the first page */
4881da177e4SLinus Torvalds 	write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	if (write_length > length)
4911da177e4SLinus Torvalds 		write_length = length;
4921da177e4SLinus Torvalds 
493441b62c1SHarvey Harrison 	dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __func__, start_address, write_length);
494441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/* Write first page */
4971da177e4SLinus Torvalds 	be_start_address = cpu_to_be16 (start_address);
4981da177e4SLinus Torvalds 	status = TISendVendorRequestSync (serial->serial->dev,
4991da177e4SLinus Torvalds 					UMPC_MEMORY_WRITE,	// Request
5001da177e4SLinus Torvalds 					(__u16)address_type,	// wValue
5011da177e4SLinus Torvalds 					(__force __u16)be_start_address,	// wIndex
5021da177e4SLinus Torvalds 					buffer,			// TransferBuffer
5031da177e4SLinus Torvalds 					write_length);
5041da177e4SLinus Torvalds 	if (status) {
505441b62c1SHarvey Harrison 		dbg ("%s - ERROR %d", __func__, status);
5061da177e4SLinus Torvalds 		return status;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	length		-= write_length;
5101da177e4SLinus Torvalds 	start_address	+= write_length;
5111da177e4SLinus Torvalds 	buffer		+= write_length;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	/* We should be aligned now -- can write max page size bytes at a time */
5141da177e4SLinus Torvalds 	while (length) {
5151da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
5161da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
5171da177e4SLinus Torvalds 		else
5181da177e4SLinus Torvalds 			write_length = length;
5191da177e4SLinus Torvalds 
520441b62c1SHarvey Harrison 		dbg ("%s - Page Write Addr = %x, length = %d", __func__, start_address, write_length);
521441b62c1SHarvey Harrison 		usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 		/* Write next page */
5241da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
5251da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
5261da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,	// Request
5271da177e4SLinus Torvalds 						(__u16)address_type,	// wValue
5281da177e4SLinus Torvalds 						(__force __u16)be_start_address,	// wIndex
5291da177e4SLinus Torvalds 						buffer,	  		// TransferBuffer
5301da177e4SLinus Torvalds 						write_length);		// TransferBufferLength
5311da177e4SLinus Torvalds 		if (status) {
532441b62c1SHarvey Harrison 			dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __func__, status);
5331da177e4SLinus Torvalds 			return status;
5341da177e4SLinus Torvalds 		}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 		length		-= write_length;
5371da177e4SLinus Torvalds 		start_address	+= write_length;
5381da177e4SLinus Torvalds 		buffer		+= write_length;
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 	return status;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
5441da177e4SLinus Torvalds  *
5451da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5461da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5471da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5481da177e4SLinus Torvalds  */
5491da177e4SLinus Torvalds static int TIIsTxActive (struct edgeport_port *port)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	int status;
5521da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
5531da177e4SLinus Torvalds 	__u8 *lsr;
5541da177e4SLinus Torvalds 	int bytes_left = 0;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
5571da177e4SLinus Torvalds 	if (!oedb) {
558441b62c1SHarvey Harrison 		dev_err (&port->port->dev, "%s - out of memory\n", __func__);
5591da177e4SLinus Torvalds 		return -ENOMEM;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	lsr = kmalloc (1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
5631da177e4SLinus Torvalds 					   as not all platforms can do DMA
5641da177e4SLinus Torvalds 					   from stack */
5651da177e4SLinus Torvalds 	if (!lsr) {
5661da177e4SLinus Torvalds 		kfree(oedb);
5671da177e4SLinus Torvalds 		return -ENOMEM;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5701da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5711da177e4SLinus Torvalds 			    port->dma_address,
5721da177e4SLinus Torvalds 			    sizeof( *oedb),
5731da177e4SLinus Torvalds 			    (void *)oedb);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (status)
5761da177e4SLinus Torvalds 		goto exit_is_tx_active;
5771da177e4SLinus Torvalds 
578441b62c1SHarvey Harrison 	dbg ("%s - XByteCount    0x%X", __func__, oedb->XByteCount);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* and the LSR */
5811da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5821da177e4SLinus Torvalds 			    port->uart_base + UMPMEM_OFFS_UART_LSR,
5831da177e4SLinus Torvalds 			    1,
5841da177e4SLinus Torvalds 			    lsr);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (status)
5871da177e4SLinus Torvalds 		goto exit_is_tx_active;
588441b62c1SHarvey Harrison 	dbg ("%s - LSR = 0x%X", __func__, *lsr);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5911da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80 ) != 0 )
5921da177e4SLinus Torvalds 		bytes_left += 64;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
5951da177e4SLinus Torvalds 		bytes_left += 1;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5981da177e4SLinus Torvalds exit_is_tx_active:
599441b62c1SHarvey Harrison 	dbg ("%s - return %d", __func__, bytes_left );
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	kfree(lsr);
6021da177e4SLinus Torvalds 	kfree(oedb);
6031da177e4SLinus Torvalds 	return bytes_left;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	int baud_rate;
6091da177e4SLinus Torvalds 	struct tty_struct *tty = port->port->tty;
6101da177e4SLinus Torvalds 	wait_queue_t wait;
6111da177e4SLinus Torvalds 	unsigned long flags;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	if (!timeout)
6141da177e4SLinus Torvalds 		timeout = (HZ*EDGE_CLOSING_WAIT)/100;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* wait for data to drain from the buffer */
6171da177e4SLinus Torvalds 	spin_lock_irqsave(&port->ep_lock, flags);
6181da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
6191da177e4SLinus Torvalds 	add_wait_queue(&tty->write_wait, &wait);
6201da177e4SLinus Torvalds 	for (;;) {
6211da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
6221da177e4SLinus Torvalds 		if (edge_buf_data_avail(port->ep_out_buf) == 0
6231da177e4SLinus Torvalds 		|| timeout == 0 || signal_pending(current)
6241da177e4SLinus Torvalds 		|| !usb_get_intfdata(port->port->serial->interface))  /* disconnect */
6251da177e4SLinus Torvalds 			break;
6261da177e4SLinus Torvalds 		spin_unlock_irqrestore(&port->ep_lock, flags);
6271da177e4SLinus Torvalds 		timeout = schedule_timeout(timeout);
6281da177e4SLinus Torvalds 		spin_lock_irqsave(&port->ep_lock, flags);
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
6311da177e4SLinus Torvalds 	remove_wait_queue(&tty->write_wait, &wait);
6321da177e4SLinus Torvalds 	if (flush)
6331da177e4SLinus Torvalds 		edge_buf_clear(port->ep_out_buf);
6341da177e4SLinus Torvalds 	spin_unlock_irqrestore(&port->ep_lock, flags);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/* wait for data to drain from the device */
6371da177e4SLinus Torvalds 	timeout += jiffies;
6381da177e4SLinus Torvalds 	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
6391da177e4SLinus Torvalds 	&& usb_get_intfdata(port->port->serial->interface)) {  /* not disconnected */
6401da177e4SLinus Torvalds 		if (!TIIsTxActive(port))
6411da177e4SLinus Torvalds 			break;
6421da177e4SLinus Torvalds 		msleep(10);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/* disconnected */
6461da177e4SLinus Torvalds 	if (!usb_get_intfdata(port->port->serial->interface))
6471da177e4SLinus Torvalds 		return;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* wait one more character time, based on baud rate */
6501da177e4SLinus Torvalds 	/* (TIIsTxActive doesn't seem to wait for the last byte) */
6511da177e4SLinus Torvalds 	if ((baud_rate=port->baud_rate) == 0)
6521da177e4SLinus Torvalds 		baud_rate = 50;
653dfa5ec79SJulia Lawall 	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds static int TIChooseConfiguration (struct usb_device *dev)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	// There may be multiple configurations on this device, in which case
6591da177e4SLinus Torvalds 	// we would need to read and parse all of them to find out which one
6601da177e4SLinus Torvalds 	// we want. However, we just support one config at this point,
6611da177e4SLinus Torvalds 	// configuration # 1, which is Config Descriptor 0.
6621da177e4SLinus Torvalds 
663441b62c1SHarvey Harrison 	dbg ("%s - Number of Interfaces = %d", __func__, dev->config->desc.bNumInterfaces);
664441b62c1SHarvey Harrison 	dbg ("%s - MAX Power            = %d", __func__, dev->config->desc.bMaxPower*2);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
667441b62c1SHarvey Harrison 		dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
6681da177e4SLinus Torvalds 		return -ENODEV;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	return 0;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	int status;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6791da177e4SLinus Torvalds 		status = TIReadDownloadMemory (serial->serial->dev,
6801da177e4SLinus Torvalds 					       start_address,
6811da177e4SLinus Torvalds 					       length,
6821da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6831da177e4SLinus Torvalds 					       buffer);
6841da177e4SLinus Torvalds 	} else {
6851da177e4SLinus Torvalds 		status = TIReadBootMemory (serial,
6861da177e4SLinus Torvalds 					   start_address,
6871da177e4SLinus Torvalds 					   length,
6881da177e4SLinus Torvalds 					   buffer);
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	return status;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6971da177e4SLinus Torvalds 		return TIWriteBootMemory (serial,
6981da177e4SLinus Torvalds 					  start_address,
6991da177e4SLinus Torvalds 					  length,
7001da177e4SLinus Torvalds 					  buffer);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
7031da177e4SLinus Torvalds 		return TIWriteDownloadI2C (serial,
7041da177e4SLinus Torvalds 					   start_address,
7051da177e4SLinus Torvalds 					   length,
7061da177e4SLinus Torvalds 					   serial->TI_I2C_Type,
7071da177e4SLinus Torvalds 					   buffer);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	return -EINVAL;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
7151da177e4SLinus Torvalds static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	int start_address;
7181da177e4SLinus Torvalds 	int status;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
7211da177e4SLinus Torvalds 	start_address = 2;
7221da177e4SLinus Torvalds 	do {
7231da177e4SLinus Torvalds 		status = TIReadRom (serial,
7241da177e4SLinus Torvalds 				   start_address,
7251da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
7261da177e4SLinus Torvalds 				   (__u8 *)rom_desc );
7271da177e4SLinus Torvalds 		if (status)
7281da177e4SLinus Torvalds 			return 0;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
7311da177e4SLinus Torvalds 			return start_address;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	return 0;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Validate descriptor checksum */
7411da177e4SLinus Torvalds static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	__u16 i;
7441da177e4SLinus Torvalds 	__u8 cs = 0;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	for (i=0; i < rom_desc->Size; i++) {
7471da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
750441b62c1SHarvey Harrison 		dbg ("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
7511da177e4SLinus Torvalds 		return -EINVAL;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 	return 0;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds /* Make sure that the I2C image is good */
7571da177e4SLinus Torvalds static int TiValidateI2cImage (struct edgeport_serial *serial)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
7601da177e4SLinus Torvalds 	int status = 0;
7611da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7621da177e4SLinus Torvalds 	int start_address = 2;
7631da177e4SLinus Torvalds 	__u8 *buffer;
7641da177e4SLinus Torvalds 	__u16 ttype;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
7671da177e4SLinus Torvalds 	if (!rom_desc) {
768441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory\n", __func__);
7691da177e4SLinus Torvalds 		return -ENOMEM;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 	buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
7721da177e4SLinus Torvalds 	if (!buffer) {
773441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory when allocating buffer\n", __func__);
7741da177e4SLinus Torvalds 		kfree (rom_desc);
7751da177e4SLinus Torvalds 		return -ENOMEM;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	// Read the first byte (Signature0) must be 0x52 or 0x10
7791da177e4SLinus Torvalds 	status = TIReadRom (serial, 0, 1, buffer);
7801da177e4SLinus Torvalds 	if (status)
7811da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
784441b62c1SHarvey Harrison 		dev_err (dev, "%s - invalid buffer signature\n", __func__);
7851da177e4SLinus Torvalds 		status = -ENODEV;
7861da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	do {
7901da177e4SLinus Torvalds 		// Validate the I2C
7911da177e4SLinus Torvalds 		status = TIReadRom (serial,
7921da177e4SLinus Torvalds 				start_address,
7931da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7941da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7951da177e4SLinus Torvalds 		if (status)
7961da177e4SLinus Torvalds 			break;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 		if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
7991da177e4SLinus Torvalds 			status = -ENODEV;
800441b62c1SHarvey Harrison 			dbg ("%s - structure too big, erroring out.", __func__);
8011da177e4SLinus Torvalds 			break;
8021da177e4SLinus Torvalds 		}
8031da177e4SLinus Torvalds 
804441b62c1SHarvey Harrison 		dbg ("%s Type = 0x%x", __func__, rom_desc->Type);
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		// Skip type 2 record
8071da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
8081da177e4SLinus Torvalds 		if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
8091da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
8101da177e4SLinus Torvalds 			// Read the descriptor data
8111da177e4SLinus Torvalds 			status = TIReadRom(serial,
8121da177e4SLinus Torvalds 						start_address+sizeof(struct ti_i2c_desc),
8131da177e4SLinus Torvalds 						rom_desc->Size,
8141da177e4SLinus Torvalds 						buffer);
8151da177e4SLinus Torvalds 			if (status)
8161da177e4SLinus Torvalds 				break;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 			status = ValidChecksum(rom_desc, buffer);
8191da177e4SLinus Torvalds 			if (status)
8201da177e4SLinus Torvalds 				break;
8211da177e4SLinus Torvalds 		}
8221da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
8271da177e4SLinus Torvalds 		status = -ENODEV;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds ExitTiValidateI2cImage:
8301da177e4SLinus Torvalds 	kfree (buffer);
8311da177e4SLinus Torvalds 	kfree (rom_desc);
8321da177e4SLinus Torvalds 	return status;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	int status;
8381da177e4SLinus Torvalds 	int start_address;
8391da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
8401da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
8431da177e4SLinus Torvalds 	if (!rom_desc) {
844441b62c1SHarvey Harrison 		dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __func__);
8451da177e4SLinus Torvalds 		return -ENOMEM;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 	start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	if (!start_address) {
850441b62c1SHarvey Harrison 		dbg ("%s - Edge Descriptor not found in I2C", __func__);
8511da177e4SLinus Torvalds 		status = -ENODEV;
8521da177e4SLinus Torvalds 		goto exit;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	// Read the descriptor data
8561da177e4SLinus Torvalds 	status = TIReadRom (serial,
8571da177e4SLinus Torvalds 				start_address+sizeof(struct ti_i2c_desc),
8581da177e4SLinus Torvalds 				rom_desc->Size,
8591da177e4SLinus Torvalds 				buffer);
8601da177e4SLinus Torvalds 	if (status)
8611da177e4SLinus Torvalds 		goto exit;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	status = ValidChecksum(rom_desc, buffer);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
866441b62c1SHarvey Harrison 	dbg ( "%s - IonConfig      0x%x", __func__, desc->IonConfig 	);
867441b62c1SHarvey Harrison 	dbg ( "%s - Version          %d", __func__, desc->Version	  	);
868441b62c1SHarvey Harrison 	dbg ( "%s - Cpu/Board      0x%x", __func__, desc->CpuRev_BoardRev	);
869441b62c1SHarvey Harrison 	dbg ( "%s - NumPorts         %d", __func__, desc->NumPorts  	);
870441b62c1SHarvey Harrison 	dbg ( "%s - NumVirtualPorts  %d", __func__, desc->NumVirtualPorts	);
871441b62c1SHarvey Harrison 	dbg ( "%s - TotalPorts       %d", __func__, desc->TotalPorts  	);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds exit:
8741da177e4SLinus Torvalds 	kfree (rom_desc);
8751da177e4SLinus Torvalds 	return status;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /* Build firmware header used for firmware update */
8791da177e4SLinus Torvalds static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	__u8 *buffer;
8821da177e4SLinus Torvalds 	int buffer_size;
8831da177e4SLinus Torvalds 	int i;
884*d12b219aSJaswinder Singh 	int err;
8851da177e4SLinus Torvalds 	__u8 cs = 0;
8861da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8871da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8881da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
889*d12b219aSJaswinder Singh 	const struct firmware *fw;
890*d12b219aSJaswinder Singh 	const char *fw_name = "edgeport/down3.bin";
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
8931da177e4SLinus Torvalds 	// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
8941da177e4SLinus Torvalds 	// will download the latest firmware (padded to 15.5k) into the UMP ram.
8951da177e4SLinus Torvalds 	// And finally when the device comes back up in download mode the driver will cause
8961da177e4SLinus Torvalds 	// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
8971da177e4SLinus Torvalds 	// the record type from 0xf2 to 0x02.
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	// Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
9001da177e4SLinus Torvalds 	buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	buffer = kmalloc (buffer_size, GFP_KERNEL);
9031da177e4SLinus Torvalds 	if (!buffer) {
904441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory\n", __func__);
9051da177e4SLinus Torvalds 		return -ENOMEM;
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	// Set entire image of 0xffs
9091da177e4SLinus Torvalds 	memset (buffer, 0xff, buffer_size);
9101da177e4SLinus Torvalds 
911*d12b219aSJaswinder Singh 	err = request_firmware(&fw, fw_name, dev);
912*d12b219aSJaswinder Singh 	if (err) {
913*d12b219aSJaswinder Singh 		printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
914*d12b219aSJaswinder Singh 		       fw_name, err);
915*d12b219aSJaswinder Singh 		kfree(buffer);
916*d12b219aSJaswinder Singh 		return err;
917*d12b219aSJaswinder Singh 	}
918*d12b219aSJaswinder Singh 
919*d12b219aSJaswinder Singh 	/* Save Download Version Number */
920*d12b219aSJaswinder Singh 	OperationalMajorVersion = fw->data[0];
921*d12b219aSJaswinder Singh 	OperationalMinorVersion = fw->data[1];
922*d12b219aSJaswinder Singh 	OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
923*d12b219aSJaswinder Singh 
9241da177e4SLinus Torvalds 	// Copy version number into firmware record
9251da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
9261da177e4SLinus Torvalds 
927*d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
928*d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	// Pointer to fw_down memory image
931*d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
934*d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
9351da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
9361da177e4SLinus Torvalds 
937*d12b219aSJaswinder Singh 	release_firmware(fw);
938*d12b219aSJaswinder Singh 
9391da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
9401da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	kfree (buffer);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	// Build new header
9461da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
9471da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
9501da177e4SLinus Torvalds 	i2c_header->Size	= (__u16)buffer_size;
9511da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
952*d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
953*d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	return 0;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
9591da177e4SLinus Torvalds static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	int status;
9621da177e4SLinus Torvalds 	__u8 data;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	// Try to read type 2
9651da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9661da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9671da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_II,	// wValue (Address type)
9681da177e4SLinus Torvalds 					0,		 		// wIndex
9691da177e4SLinus Torvalds 					&data,				// TransferBuffer
9701da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9711da177e4SLinus Torvalds 	if (status)
972441b62c1SHarvey Harrison 		dbg ("%s - read 2 status error = %d", __func__, status);
9731da177e4SLinus Torvalds 	else
974441b62c1SHarvey Harrison 		dbg ("%s - read 2 data = 0x%x", __func__, data);
9751da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
976441b62c1SHarvey Harrison 		dbg ("%s - ROM_TYPE_II", __func__);
9771da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9781da177e4SLinus Torvalds 		return 0;
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	// Try to read type 3
9821da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9831da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9841da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_III,	// wValue (Address type)
9851da177e4SLinus Torvalds 					0,				// wIndex
9861da177e4SLinus Torvalds 					&data,				// TransferBuffer
9871da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9881da177e4SLinus Torvalds 	if (status)
989441b62c1SHarvey Harrison 		dbg ("%s - read 3 status error = %d", __func__, status);
9901da177e4SLinus Torvalds 	else
991441b62c1SHarvey Harrison 		dbg ("%s - read 2 data = 0x%x", __func__, data);
9921da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
993441b62c1SHarvey Harrison 		dbg ("%s - ROM_TYPE_III", __func__);
9941da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
9951da177e4SLinus Torvalds 		return 0;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
998441b62c1SHarvey Harrison 	dbg ("%s - Unknown", __func__);
9991da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10001da177e4SLinus Torvalds 	return -ENODEV;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
10041da177e4SLinus Torvalds {
10051da177e4SLinus Torvalds 	int status;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	status = usb_bulk_msg (serial->dev,
10081da177e4SLinus Torvalds 				usb_sndbulkpipe(serial->dev,
10091da177e4SLinus Torvalds 						serial->port[0]->bulk_out_endpointAddress),
10101da177e4SLinus Torvalds 				buffer,
10111da177e4SLinus Torvalds 				length,
10121da177e4SLinus Torvalds 				num_sent,
10131da177e4SLinus Torvalds 				1000);
10141da177e4SLinus Torvalds 	return status;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
10181da177e4SLinus Torvalds static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	int status = 0;
10211da177e4SLinus Torvalds 	int pos;
10221da177e4SLinus Torvalds 	int transfer;
10231da177e4SLinus Torvalds 	int done;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	// Transfer firmware image
10261da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
10271da177e4SLinus Torvalds 		// Read the next buffer from file
10281da177e4SLinus Torvalds 		transfer = image_length - pos;
10291da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
10301da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		// Transfer data
10331da177e4SLinus Torvalds 		status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
10341da177e4SLinus Torvalds 		if (status)
10351da177e4SLinus Torvalds 			break;
10361da177e4SLinus Torvalds 		// Advance buffer pointer
10371da177e4SLinus Torvalds 		pos += done;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	return status;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds // FIXME!!!
10441da177e4SLinus Torvalds static int TIConfigureBootDevice (struct usb_device *dev)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	return 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /**
10501da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
10511da177e4SLinus Torvalds  *
10521da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
10531da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
10541da177e4SLinus Torvalds  */
10551da177e4SLinus Torvalds static int TIDownloadFirmware (struct edgeport_serial *serial)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
10581da177e4SLinus Torvalds 	int status = 0;
10591da177e4SLinus Torvalds 	int start_address;
10601da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
10611da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
10621da177e4SLinus Torvalds 	int download_cur_ver;
10631da177e4SLinus Torvalds 	int download_new_ver;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
10661da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10671da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10681da177e4SLinus Torvalds 	 */
10691da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10721da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	status = TIChooseConfiguration (serial->serial->dev);
10751da177e4SLinus Torvalds 	if (status)
10761da177e4SLinus Torvalds 		return status;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10791da177e4SLinus Torvalds 	if (!interface) {
1080441b62c1SHarvey Harrison 		dev_err (dev, "%s - no interface set, error!\n", __func__);
10811da177e4SLinus Torvalds 		return -ENODEV;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	// Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10851da177e4SLinus Torvalds 	// if we have more than one endpoint we are definitely in download mode
10861da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
10871da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
10881da177e4SLinus Torvalds 	else
10891da177e4SLinus Torvalds 		// Otherwise we will remain in configuring mode
10901da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	/********************************************************************/
10931da177e4SLinus Torvalds 	/* Download Mode */
10941da177e4SLinus Torvalds 	/********************************************************************/
10951da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
10961da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
10971da177e4SLinus Torvalds 
10989544e833SAndrew Morton 		dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 		status = TiValidateI2cImage (serial);
11011da177e4SLinus Torvalds 		if (status) {
11029544e833SAndrew Morton 			dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
11031da177e4SLinus Torvalds 			return status;
11041da177e4SLinus Torvalds 		}
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 		/* Validate Hardware version number
11071da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
11081da177e4SLinus Torvalds 		 */
11091da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
11101da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1111441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
11121da177e4SLinus Torvalds 			return -ENOMEM;
11131da177e4SLinus Torvalds 		}
11141da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
11151da177e4SLinus Torvalds 		if (status) {
11161da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11171da177e4SLinus Torvalds 			return status;
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 		// Check version number of ION descriptor
11211da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1122441b62c1SHarvey Harrison 			dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __func__,
11231da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
11241da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11251da177e4SLinus Torvalds 		   	return -EINVAL;
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 		rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
11291da177e4SLinus Torvalds 		if (!rom_desc) {
1130441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
11311da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11321da177e4SLinus Torvalds 			return -ENOMEM;
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		// Search for type 2 record (firmware record)
11361da177e4SLinus Torvalds 		if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
11371da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
11381da177e4SLinus Torvalds 			__u8 record;
11391da177e4SLinus Torvalds 
1140441b62c1SHarvey Harrison 			dbg ("%s - Found Type FIRMWARE (Type 2) record", __func__);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 			firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
11431da177e4SLinus Torvalds 			if (!firmware_version) {
1144441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
11451da177e4SLinus Torvalds 				kfree (rom_desc);
11461da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11471da177e4SLinus Torvalds 				return -ENOMEM;
11481da177e4SLinus Torvalds 			}
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 			// Validate version number
11511da177e4SLinus Torvalds 			// Read the descriptor data
11521da177e4SLinus Torvalds 			status = TIReadRom (serial,
11531da177e4SLinus Torvalds 					start_address+sizeof(struct ti_i2c_desc),
11541da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
11551da177e4SLinus Torvalds 					(__u8 *)firmware_version);
11561da177e4SLinus Torvalds 			if (status) {
11571da177e4SLinus Torvalds 				kfree (firmware_version);
11581da177e4SLinus Torvalds 				kfree (rom_desc);
11591da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11601da177e4SLinus Torvalds 				return status;
11611da177e4SLinus Torvalds 			}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 			// Check version number of download with current version in I2c
11641da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
11651da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
1166*d12b219aSJaswinder Singh 			download_new_ver = (OperationalMajorVersion << 8) +
1167*d12b219aSJaswinder Singh 					   (OperationalMinorVersion);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 			dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
1170441b62c1SHarvey Harrison 			     __func__,
11711da177e4SLinus Torvalds 			     firmware_version->Ver_Major,
11721da177e4SLinus Torvalds 			     firmware_version->Ver_Minor,
1173*d12b219aSJaswinder Singh 			     OperationalMajorVersion,
1174*d12b219aSJaswinder Singh 			     OperationalMinorVersion);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 			// Check if we have an old version in the I2C and update if necessary
11771da177e4SLinus Torvalds 			if (download_cur_ver != download_new_ver) {
11781da177e4SLinus Torvalds 				dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1179441b62c1SHarvey Harrison 				     __func__,
11801da177e4SLinus Torvalds 				     firmware_version->Ver_Major,
11811da177e4SLinus Torvalds 				     firmware_version->Ver_Minor,
1182*d12b219aSJaswinder Singh 				     OperationalMajorVersion,
1183*d12b219aSJaswinder Singh 				     OperationalMinorVersion);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 				// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
11861da177e4SLinus Torvalds 				// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
11871da177e4SLinus Torvalds 				// will download the latest firmware (padded to 15.5k) into the UMP ram.
11881da177e4SLinus Torvalds 				// And finally when the device comes back up in download mode the driver will cause
11891da177e4SLinus Torvalds 				// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
11901da177e4SLinus Torvalds 				// the record type from 0xf2 to 0x02.
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 				record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 				// Change the I2C Firmware record type to 0xf2 to trigger an update
11951da177e4SLinus Torvalds 				status = TIWriteRom (serial,
11961da177e4SLinus Torvalds 							start_address,
11971da177e4SLinus Torvalds 							sizeof(record),
11981da177e4SLinus Torvalds 							&record);
11991da177e4SLinus Torvalds 				if (status) {
12001da177e4SLinus Torvalds 					kfree (firmware_version);
12011da177e4SLinus Torvalds 					kfree (rom_desc);
12021da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12031da177e4SLinus Torvalds 					return status;
12041da177e4SLinus Torvalds 				}
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 				// verify the write -- must do this in order for write to
12071da177e4SLinus Torvalds 				// complete before we do the hardware reset
12081da177e4SLinus Torvalds 				status = TIReadRom (serial,
12091da177e4SLinus Torvalds 							start_address,
12101da177e4SLinus Torvalds 							sizeof(record),
12111da177e4SLinus Torvalds 							&record);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 				if (status) {
12141da177e4SLinus Torvalds 					kfree (firmware_version);
12151da177e4SLinus Torvalds 					kfree (rom_desc);
12161da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12171da177e4SLinus Torvalds 					return status;
12181da177e4SLinus Torvalds 				}
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1221441b62c1SHarvey Harrison 					dev_err (dev, "%s - error resetting device\n", __func__);
12221da177e4SLinus Torvalds 					kfree (firmware_version);
12231da177e4SLinus Torvalds 					kfree (rom_desc);
12241da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12251da177e4SLinus Torvalds 					return -ENODEV;
12261da177e4SLinus Torvalds 				}
12271da177e4SLinus Torvalds 
1228441b62c1SHarvey Harrison 				dbg ("%s - HARDWARE RESET", __func__);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 				// Reset UMP -- Back to BOOT MODE
12311da177e4SLinus Torvalds 				status = TISendVendorRequestSync (serial->serial->dev,
12321da177e4SLinus Torvalds 								UMPC_HARDWARE_RESET,	// Request
12331da177e4SLinus Torvalds 								0,			// wValue
12341da177e4SLinus Torvalds 								0,			// wIndex
12351da177e4SLinus Torvalds 								NULL,			// TransferBuffer
12361da177e4SLinus Torvalds 								0);			// TransferBufferLength
12371da177e4SLinus Torvalds 
1238441b62c1SHarvey Harrison 				dbg ( "%s - HARDWARE RESET return %d", __func__, status);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 				/* return an error on purpose. */
12411da177e4SLinus Torvalds 				kfree (firmware_version);
12421da177e4SLinus Torvalds 				kfree (rom_desc);
12431da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12441da177e4SLinus Torvalds 				return -ENODEV;
12451da177e4SLinus Torvalds 			}
12461da177e4SLinus Torvalds 			kfree (firmware_version);
12471da177e4SLinus Torvalds 		}
12481da177e4SLinus Torvalds 		// Search for type 0xF2 record (firmware blank record)
12491da177e4SLinus Torvalds 		else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
12501da177e4SLinus Torvalds 			#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
12511da177e4SLinus Torvalds 			__u8 *header;
12521da177e4SLinus Torvalds 			__u8 *vheader;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 			header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
12551da177e4SLinus Torvalds 			if (!header) {
1256441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
12571da177e4SLinus Torvalds 				kfree (rom_desc);
12581da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12591da177e4SLinus Torvalds 				return -ENOMEM;
12601da177e4SLinus Torvalds 			}
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 			vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
12631da177e4SLinus Torvalds 			if (!vheader) {
1264441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
12651da177e4SLinus Torvalds 				kfree (header);
12661da177e4SLinus Torvalds 				kfree (rom_desc);
12671da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12681da177e4SLinus Torvalds 				return -ENOMEM;
12691da177e4SLinus Torvalds 			}
12701da177e4SLinus Torvalds 
1271441b62c1SHarvey Harrison 			dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __func__);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 			// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
12741da177e4SLinus Torvalds 			// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
12751da177e4SLinus Torvalds 			// will download the latest firmware (padded to 15.5k) into the UMP ram.
12761da177e4SLinus Torvalds 			// And finally when the device comes back up in download mode the driver will cause
12771da177e4SLinus Torvalds 			// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
12781da177e4SLinus Torvalds 			// the record type from 0xf2 to 0x02.
12791da177e4SLinus Torvalds 			status = BuildI2CFirmwareHeader(header, dev);
12801da177e4SLinus Torvalds 			if (status) {
12811da177e4SLinus Torvalds 				kfree (vheader);
12821da177e4SLinus Torvalds 				kfree (header);
12831da177e4SLinus Torvalds 				kfree (rom_desc);
12841da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12851da177e4SLinus Torvalds 				return status;
12861da177e4SLinus Torvalds 			}
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 			// Update I2C with type 0xf2 record with correct size and checksum
12891da177e4SLinus Torvalds 			status = TIWriteRom (serial,
12901da177e4SLinus Torvalds 						start_address,
12911da177e4SLinus Torvalds 						HEADER_SIZE,
12921da177e4SLinus Torvalds 						header);
12931da177e4SLinus Torvalds 			if (status) {
12941da177e4SLinus Torvalds 				kfree (vheader);
12951da177e4SLinus Torvalds 				kfree (header);
12961da177e4SLinus Torvalds 				kfree (rom_desc);
12971da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12981da177e4SLinus Torvalds 				return status;
12991da177e4SLinus Torvalds 			}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 			// verify the write -- must do this in order for write to
13021da177e4SLinus Torvalds 			// complete before we do the hardware reset
13031da177e4SLinus Torvalds 			status = TIReadRom (serial,
13041da177e4SLinus Torvalds 						start_address,
13051da177e4SLinus Torvalds 						HEADER_SIZE,
13061da177e4SLinus Torvalds 						vheader);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 			if (status) {
1309441b62c1SHarvey Harrison 				dbg ("%s - can't read header back", __func__);
13101da177e4SLinus Torvalds 				kfree (vheader);
13111da177e4SLinus Torvalds 				kfree (header);
13121da177e4SLinus Torvalds 				kfree (rom_desc);
13131da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13141da177e4SLinus Torvalds 				return status;
13151da177e4SLinus Torvalds 			}
13161da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
1317441b62c1SHarvey Harrison 				dbg ("%s - write download record failed", __func__);
13181da177e4SLinus Torvalds 				kfree (vheader);
13191da177e4SLinus Torvalds 				kfree (header);
13201da177e4SLinus Torvalds 				kfree (rom_desc);
13211da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13221da177e4SLinus Torvalds 				return status;
13231da177e4SLinus Torvalds 			}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 			kfree (vheader);
13261da177e4SLinus Torvalds 			kfree (header);
13271da177e4SLinus Torvalds 
1328441b62c1SHarvey Harrison 			dbg ("%s - Start firmware update", __func__);
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 			// Tell firmware to copy download image into I2C
13311da177e4SLinus Torvalds 			status = TISendVendorRequestSync (serial->serial->dev,
13321da177e4SLinus Torvalds 						UMPC_COPY_DNLD_TO_I2C,	// Request
13331da177e4SLinus Torvalds 						0,			// wValue
13341da177e4SLinus Torvalds 						0,			// wIndex
13351da177e4SLinus Torvalds 						NULL,			// TransferBuffer
13361da177e4SLinus Torvalds 						0);			// TransferBufferLength
13371da177e4SLinus Torvalds 
1338441b62c1SHarvey Harrison 		  	dbg ("%s - Update complete 0x%x", __func__, status);
13391da177e4SLinus Torvalds 			if (status) {
1340441b62c1SHarvey Harrison 				dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __func__);
13411da177e4SLinus Torvalds 				kfree (rom_desc);
13421da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13431da177e4SLinus Torvalds 				return status;
13441da177e4SLinus Torvalds 			}
13451da177e4SLinus Torvalds 		}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 		// The device is running the download code
13481da177e4SLinus Torvalds 		kfree (rom_desc);
13491da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
13501da177e4SLinus Torvalds 		return 0;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	/********************************************************************/
13541da177e4SLinus Torvalds 	/* Boot Mode */
13551da177e4SLinus Torvalds 	/********************************************************************/
13569544e833SAndrew Morton 	dbg("%s - RUNNING IN BOOT MODE", __func__);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	// Configure the TI device so we can use the BULK pipes for download
13591da177e4SLinus Torvalds 	status = TIConfigureBootDevice (serial->serial->dev);
13601da177e4SLinus Torvalds 	if (status)
13611da177e4SLinus Torvalds 		return status;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1364441b62c1SHarvey Harrison 		dbg ("%s - VID = 0x%x", __func__,
13651da177e4SLinus Torvalds 		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13661da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13671da177e4SLinus Torvalds 		goto StayInBootMode;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	// We have an ION device (I2c Must be programmed)
13711da177e4SLinus Torvalds 	// Determine I2C image type
13721da177e4SLinus Torvalds 	if (TIGetI2cTypeInBootMode(serial)) {
13731da177e4SLinus Torvalds 		goto StayInBootMode;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	// Registry variable set?
13771da177e4SLinus Torvalds 	if (TIStayInBootMode) {
1378441b62c1SHarvey Harrison 		dbg ("%s - TIStayInBootMode", __func__);
13791da177e4SLinus Torvalds 		goto StayInBootMode;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	// Check for ION Vendor ID and that the I2C is valid
13831da177e4SLinus Torvalds 	if (!TiValidateI2cImage(serial)) {
13841da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
13851da177e4SLinus Torvalds 		int i;
13861da177e4SLinus Torvalds 		__u8 cs = 0;
13871da177e4SLinus Torvalds 		__u8 *buffer;
13881da177e4SLinus Torvalds 		int buffer_size;
1389*d12b219aSJaswinder Singh 		int err;
1390*d12b219aSJaswinder Singh 		const struct firmware *fw;
1391*d12b219aSJaswinder Singh 		const char *fw_name = "edgeport/down3.bin";
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 		/* Validate Hardware version number
13941da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
13951da177e4SLinus Torvalds 		 */
13961da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
13971da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1398441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
13991da177e4SLinus Torvalds 			return -ENOMEM;
14001da177e4SLinus Torvalds 		}
14011da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
14021da177e4SLinus Torvalds 		if (status) {
14031da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
14041da177e4SLinus Torvalds 			goto StayInBootMode;
14051da177e4SLinus Torvalds 		}
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 		// Check for version 2
14081da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1409441b62c1SHarvey Harrison 			dbg ("%s - Wrong CPU Rev %d (Must be 2)", __func__,
14101da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
14111da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
14121da177e4SLinus Torvalds 			goto StayInBootMode;
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 		// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
14181da177e4SLinus Torvalds 		// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
14191da177e4SLinus Torvalds 		// will download the latest firmware (padded to 15.5k) into the UMP ram.
14201da177e4SLinus Torvalds 		// And finally when the device comes back up in download mode the driver will cause
14211da177e4SLinus Torvalds 		// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
14221da177e4SLinus Torvalds 		// the record type from 0xf2 to 0x02.
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 		/*
14251da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14261da177e4SLinus Torvalds 		 * or could we do this in place!
14271da177e4SLinus Torvalds 		 */
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 		// Allocate a 15.5k buffer + 3 byte header
14301da177e4SLinus Torvalds 		buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
14311da177e4SLinus Torvalds 		buffer = kmalloc (buffer_size, GFP_KERNEL);
14321da177e4SLinus Torvalds 		if (!buffer) {
1433441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory\n", __func__);
14341da177e4SLinus Torvalds 			return -ENOMEM;
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 		// Initialize the buffer to 0xff (pad the buffer)
14381da177e4SLinus Torvalds 		memset (buffer, 0xff, buffer_size);
14391da177e4SLinus Torvalds 
1440*d12b219aSJaswinder Singh 		err = request_firmware(&fw, fw_name, dev);
1441*d12b219aSJaswinder Singh 		if (err) {
1442*d12b219aSJaswinder Singh 			printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1443*d12b219aSJaswinder Singh 			       fw_name, err);
1444*d12b219aSJaswinder Singh 			kfree(buffer);
1445*d12b219aSJaswinder Singh 			return err;
1446*d12b219aSJaswinder Singh 		}
1447*d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
1448*d12b219aSJaswinder Singh 		release_firmware(fw);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 		for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
14511da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14521da177e4SLinus Torvalds 		}
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 		// update length and checksum after padding
14571da177e4SLinus Torvalds 		header->Length 	 = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
14581da177e4SLinus Torvalds 		header->CheckSum = cs;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 		// Download the operational code
1461441b62c1SHarvey Harrison 		dbg ("%s - Downloading operational code image (TI UMP)", __func__);
14621da177e4SLinus Torvalds 		status = TIDownloadCodeImage (serial, buffer, buffer_size);
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 		kfree (buffer);
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 		if (status) {
1467441b62c1SHarvey Harrison 	  		dbg ("%s - Error downloading operational code image", __func__);
14681da177e4SLinus Torvalds 			return status;
14691da177e4SLinus Torvalds 		}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 		// Device will reboot
14721da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14731da177e4SLinus Torvalds 
1474441b62c1SHarvey Harrison   		dbg ("%s - Download successful -- Device rebooting...", __func__);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 		/* return an error on purpose */
14771da177e4SLinus Torvalds 		return -ENODEV;
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds StayInBootMode:
14811da177e4SLinus Torvalds 	// Eprom is invalid or blank stay in boot mode
14829544e833SAndrew Morton 	dbg("%s - STAYING IN BOOT MODE", __func__);
14831da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	return 0;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds static int TISetDtr (struct edgeport_port *port)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14921da177e4SLinus Torvalds 
1493441b62c1SHarvey Harrison 	dbg ("%s", __func__);
14941da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_DTR;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14971da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
14981da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14991da177e4SLinus Torvalds 				1,	/* set */
15001da177e4SLinus Torvalds 				NULL,
15011da177e4SLinus Torvalds 				0);
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds static int TIClearDtr (struct edgeport_port *port)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15071da177e4SLinus Torvalds 
1508441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15091da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_DTR;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15121da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
15131da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15141da177e4SLinus Torvalds 				0,	/* clear */
15151da177e4SLinus Torvalds 				NULL,
15161da177e4SLinus Torvalds 				0);
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds static int TISetRts (struct edgeport_port *port)
15201da177e4SLinus Torvalds {
15211da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15221da177e4SLinus Torvalds 
1523441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15241da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_RTS;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15271da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
15281da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15291da177e4SLinus Torvalds 				1,	/* set */
15301da177e4SLinus Torvalds 				NULL,
15311da177e4SLinus Torvalds 				0);
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds static int TIClearRts (struct edgeport_port *port)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15371da177e4SLinus Torvalds 
1538441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15391da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_RTS;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15421da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
15431da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15441da177e4SLinus Torvalds 				0,	/* clear */
15451da177e4SLinus Torvalds 				NULL,
15461da177e4SLinus Torvalds 				0);
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds static int TISetLoopBack (struct edgeport_port *port)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15521da177e4SLinus Torvalds 
1553441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15561da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15571da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15581da177e4SLinus Torvalds 				1,	/* set */
15591da177e4SLinus Torvalds 				NULL,
15601da177e4SLinus Torvalds 				0);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds static int TIClearLoopBack (struct edgeport_port *port)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15661da177e4SLinus Torvalds 
1567441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15701da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15711da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15721da177e4SLinus Torvalds 				0,	/* clear */
15731da177e4SLinus Torvalds 				NULL,
15741da177e4SLinus Torvalds 				0);
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds static int TISetBreak (struct edgeport_port *port)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15801da177e4SLinus Torvalds 
1581441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15841da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15851da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15861da177e4SLinus Torvalds 				1,	/* set */
15871da177e4SLinus Torvalds 				NULL,
15881da177e4SLinus Torvalds 				0);
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds static int TIClearBreak (struct edgeport_port *port)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15941da177e4SLinus Torvalds 
1595441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15981da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15991da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
16001da177e4SLinus Torvalds 				0,	/* clear */
16011da177e4SLinus Torvalds 				NULL,
16021da177e4SLinus Torvalds 				0);
16031da177e4SLinus Torvalds }
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	int status = 0;
16081da177e4SLinus Torvalds 
1609441b62c1SHarvey Harrison 	dbg ("%s - %x", __func__, mcr);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	if (mcr & MCR_DTR)
16121da177e4SLinus Torvalds 		status = TISetDtr (port);
16131da177e4SLinus Torvalds 	else
16141da177e4SLinus Torvalds 		status = TIClearDtr (port);
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (status)
16171da177e4SLinus Torvalds 		return status;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 	if (mcr & MCR_RTS)
16201da177e4SLinus Torvalds 		status = TISetRts (port);
16211da177e4SLinus Torvalds 	else
16221da177e4SLinus Torvalds 		status = TIClearRts (port);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (status)
16251da177e4SLinus Torvalds 		return status;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	if (mcr & MCR_LOOPBACK)
16281da177e4SLinus Torvalds 		status = TISetLoopBack (port);
16291da177e4SLinus Torvalds 	else
16301da177e4SLinus Torvalds 		status = TIClearLoopBack (port);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	return status;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
16381da177e4SLinus Torvalds static __u8 MapLineStatus (__u8 ti_lsr)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	__u8 lsr = 0;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
16431da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
16441da177e4SLinus Torvalds 		lsr |= flagUart;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
16471da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
16481da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
16491da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
16501da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* receive data available */
16511da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* transmit holding register empty */
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds #undef MAP_FLAG
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	return lsr;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	struct async_icount *icount;
16611da177e4SLinus Torvalds 	struct tty_struct *tty;
16621da177e4SLinus Torvalds 
1663441b62c1SHarvey Harrison 	dbg ("%s - %02x", __func__, msr);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
16661da177e4SLinus Torvalds 		icount = &edge_port->icount;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 		/* update input line counters */
16691da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
16701da177e4SLinus Torvalds 			icount->cts++;
16711da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
16721da177e4SLinus Torvalds 			icount->dsr++;
16731da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
16741da177e4SLinus Torvalds 			icount->dcd++;
16751da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
16761da177e4SLinus Torvalds 			icount->rng++;
16771da177e4SLinus Torvalds 		wake_up_interruptible (&edge_port->delta_msr_wait);
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	/* Save the new modem status */
16811da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	tty = edge_port->port->tty;
16841da177e4SLinus Torvalds 	/* handle CTS flow control */
16851da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
16861da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_CTS) {
16871da177e4SLinus Torvalds 			tty->hw_stopped = 0;
16881da177e4SLinus Torvalds 			tty_wakeup(tty);
16891da177e4SLinus Torvalds 		} else {
16901da177e4SLinus Torvalds 			tty->hw_stopped = 1;
16911da177e4SLinus Torvalds 		}
16921da177e4SLinus Torvalds 	}
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	return;
16951da177e4SLinus Torvalds }
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
16981da177e4SLinus Torvalds {
16991da177e4SLinus Torvalds 	struct async_icount *icount;
17001da177e4SLinus Torvalds 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
17011da177e4SLinus Torvalds 
1702441b62c1SHarvey Harrison 	dbg ("%s - %02x", __func__, new_lsr);
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK) {
17071da177e4SLinus Torvalds 		/*
17081da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
17091da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
17101da177e4SLinus Torvalds 		 */
17111da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
17151da177e4SLinus Torvalds 	if (lsr_data && edge_port->port->tty)
17161da177e4SLinus Torvalds 		edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	/* update input line counters */
17191da177e4SLinus Torvalds 	icount = &edge_port->icount;
17201da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
17211da177e4SLinus Torvalds 		icount->brk++;
17221da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
17231da177e4SLinus Torvalds 		icount->overrun++;
17241da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
17251da177e4SLinus Torvalds 		icount->parity++;
17261da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
17271da177e4SLinus Torvalds 		icount->frame++;
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 
17317d12e780SDavid Howells static void edge_interrupt_callback (struct urb *urb)
17321da177e4SLinus Torvalds {
1733cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
17341da177e4SLinus Torvalds 	struct usb_serial_port *port;
17351da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
17361da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17371da177e4SLinus Torvalds 	int length = urb->actual_length;
17381da177e4SLinus Torvalds 	int port_number;
17391da177e4SLinus Torvalds 	int function;
1740ee337c21SGreg Kroah-Hartman 	int retval;
17411da177e4SLinus Torvalds 	__u8 lsr;
17421da177e4SLinus Torvalds 	__u8 msr;
1743ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17441da177e4SLinus Torvalds 
1745441b62c1SHarvey Harrison 	dbg("%s", __func__);
17461da177e4SLinus Torvalds 
1747ee337c21SGreg Kroah-Hartman 	switch (status) {
17481da177e4SLinus Torvalds 	case 0:
17491da177e4SLinus Torvalds 		/* success */
17501da177e4SLinus Torvalds 		break;
17511da177e4SLinus Torvalds 	case -ECONNRESET:
17521da177e4SLinus Torvalds 	case -ENOENT:
17531da177e4SLinus Torvalds 	case -ESHUTDOWN:
17541da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1755ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1756441b62c1SHarvey Harrison 		    __func__, status);
17571da177e4SLinus Torvalds 		return;
17581da177e4SLinus Torvalds 	default:
1759ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1760441b62c1SHarvey Harrison 			"%d\n", __func__, status);
17611da177e4SLinus Torvalds 		goto exit;
17621da177e4SLinus Torvalds 	}
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	if (!length) {
1765441b62c1SHarvey Harrison 		dbg ("%s - no data in urb", __func__);
17661da177e4SLinus Torvalds 		goto exit;
17671da177e4SLinus Torvalds 	}
17681da177e4SLinus Torvalds 
1769441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if (length != 2) {
1772441b62c1SHarvey Harrison 		dbg ("%s - expecting packet of size 2, got %d", __func__, length);
17731da177e4SLinus Torvalds 		goto exit;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
17771da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
17781da177e4SLinus Torvalds 	dbg ("%s - port_number %d, function %d, info 0x%x",
1779441b62c1SHarvey Harrison 	     __func__, port_number, function, data[1]);
17801da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
17811da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
17821da177e4SLinus Torvalds 	if (!edge_port) {
1783441b62c1SHarvey Harrison 		dbg ("%s - edge_port not found", __func__);
17841da177e4SLinus Torvalds 		return;
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 	switch (function) {
17871da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
17881da177e4SLinus Torvalds 		lsr = MapLineStatus(data[1]);
17891da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
17901da177e4SLinus Torvalds 			/* Save the LSR event for bulk read completion routine */
17911da177e4SLinus Torvalds 			dbg ("%s - LSR Event Port %u LSR Status = %02x",
1792441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17931da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
17941da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
17951da177e4SLinus Torvalds 		} else {
17961da177e4SLinus Torvalds 			dbg ("%s - ===== Port %d LSR Status = %02x ======",
1797441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17981da177e4SLinus Torvalds 			handle_new_lsr (edge_port, 0, lsr, 0);
17991da177e4SLinus Torvalds 		}
18001da177e4SLinus Torvalds 		break;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_MSR:	// MSR
18031da177e4SLinus Torvalds 		/* Copy MSR from UMP */
18041da177e4SLinus Torvalds 		msr = data[1];
18051da177e4SLinus Torvalds 		dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1806441b62c1SHarvey Harrison 		     __func__, port_number, msr);
18071da177e4SLinus Torvalds 		handle_new_msr (edge_port, msr);
18081da177e4SLinus Torvalds 		break;
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	default:
18111da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1812441b62c1SHarvey Harrison 			 __func__, data[1]);
18131da177e4SLinus Torvalds 		break;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	}
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds exit:
1818ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb (urb, GFP_ATOMIC);
1819ee337c21SGreg Kroah-Hartman 	if (retval)
18201da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1821441b62c1SHarvey Harrison 			 __func__, retval);
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18247d12e780SDavid Howells static void edge_bulk_in_callback (struct urb *urb)
18251da177e4SLinus Torvalds {
1826cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
18271da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
18281da177e4SLinus Torvalds 	struct tty_struct *tty;
1829ee337c21SGreg Kroah-Hartman 	int retval = 0;
18301da177e4SLinus Torvalds 	int port_number;
1831ee337c21SGreg Kroah-Hartman 	int status = urb->status;
18321da177e4SLinus Torvalds 
1833441b62c1SHarvey Harrison 	dbg("%s", __func__);
18341da177e4SLinus Torvalds 
1835ee337c21SGreg Kroah-Hartman 	switch (status) {
18361da177e4SLinus Torvalds 	case 0:
18371da177e4SLinus Torvalds 		/* success */
18381da177e4SLinus Torvalds 		break;
18391da177e4SLinus Torvalds 	case -ECONNRESET:
18401da177e4SLinus Torvalds 	case -ENOENT:
18411da177e4SLinus Torvalds 	case -ESHUTDOWN:
18421da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1843ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1844441b62c1SHarvey Harrison 		    __func__, status);
18451da177e4SLinus Torvalds 		return;
18461da177e4SLinus Torvalds 	default:
18471da177e4SLinus Torvalds 		dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1848441b62c1SHarvey Harrison 		     __func__, status);
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 
1851ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
18521da177e4SLinus Torvalds 		goto exit;
18531da177e4SLinus Torvalds 
1854ee337c21SGreg Kroah-Hartman 	if (status) {
1855441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev,"%s - stopping read!\n", __func__);
18561da177e4SLinus Torvalds 		return;
18571da177e4SLinus Torvalds 	}
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	port_number = edge_port->port->number - edge_port->port->serial->minor;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
18621da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
18631da177e4SLinus Torvalds 		dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1864441b62c1SHarvey Harrison 		     __func__, port_number, edge_port->lsr_mask, *data);
18651da177e4SLinus Torvalds 		handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
18661da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
18671da177e4SLinus Torvalds 		--urb->actual_length;
18681da177e4SLinus Torvalds 		++data;
18691da177e4SLinus Torvalds 	}
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	tty = edge_port->port->tty;
18721da177e4SLinus Torvalds 	if (tty && urb->actual_length) {
1873441b62c1SHarvey Harrison 		usb_serial_debug_data(debug, &edge_port->port->dev, __func__, urb->actual_length, data);
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 		if (edge_port->close_pending) {
1876441b62c1SHarvey Harrison 			dbg ("%s - close is pending, dropping data on the floor.", __func__);
18771da177e4SLinus Torvalds 		} else {
18781da177e4SLinus Torvalds 			edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
18791da177e4SLinus Torvalds 		}
18801da177e4SLinus Torvalds 		edge_port->icount.rx += urb->actual_length;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds exit:
18841da177e4SLinus Torvalds 	/* continue read unless stopped */
18851da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
18861da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
18871da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
1888ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
18891da177e4SLinus Torvalds 	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
18901da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
1893ee337c21SGreg Kroah-Hartman 	if (retval)
18941da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1895441b62c1SHarvey Harrison 			 __func__, retval);
18961da177e4SLinus Torvalds }
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
18991da177e4SLinus Torvalds {
19001da177e4SLinus Torvalds 	int cnt;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	do {
190333f0f88fSAlan Cox 		cnt = tty_buffer_request_room(tty, length);
190433f0f88fSAlan Cox 		if (cnt < length) {
19051da177e4SLinus Torvalds 			dev_err(dev, "%s - dropping data, %d bytes lost\n",
1906441b62c1SHarvey Harrison 				__func__, length - cnt);
190733f0f88fSAlan Cox 			if(cnt == 0)
190833f0f88fSAlan Cox 				break;
19091da177e4SLinus Torvalds 		}
191033f0f88fSAlan Cox 		tty_insert_flip_string(tty, data, cnt);
19111da177e4SLinus Torvalds 		data += cnt;
19121da177e4SLinus Torvalds 		length -= cnt;
19131da177e4SLinus Torvalds 	} while (length > 0);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	tty_flip_buffer_push(tty);
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19187d12e780SDavid Howells static void edge_bulk_out_callback (struct urb *urb)
19191da177e4SLinus Torvalds {
1920cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
19211da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1922ee337c21SGreg Kroah-Hartman 	int status = urb->status;
19231da177e4SLinus Torvalds 
1924441b62c1SHarvey Harrison 	dbg ("%s - port %d", __func__, port->number);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
19271da177e4SLinus Torvalds 
1928ee337c21SGreg Kroah-Hartman 	switch (status) {
19291da177e4SLinus Torvalds 	case 0:
19301da177e4SLinus Torvalds 		/* success */
19311da177e4SLinus Torvalds 		break;
19321da177e4SLinus Torvalds 	case -ECONNRESET:
19331da177e4SLinus Torvalds 	case -ENOENT:
19341da177e4SLinus Torvalds 	case -ESHUTDOWN:
19351da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1936ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1937441b62c1SHarvey Harrison 		    __func__, status);
19381da177e4SLinus Torvalds 		return;
19391da177e4SLinus Torvalds 	default:
1940ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1941441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
19421da177e4SLinus Torvalds 	}
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	/* send any buffered data */
19451da177e4SLinus Torvalds 	edge_send(port);
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds static int edge_open (struct usb_serial_port *port, struct file * filp)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19511da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19521da177e4SLinus Torvalds 	struct usb_device *dev;
19531da177e4SLinus Torvalds 	struct urb *urb;
19541da177e4SLinus Torvalds 	int port_number;
19551da177e4SLinus Torvalds 	int status;
19561da177e4SLinus Torvalds 	u16 open_settings;
19571da177e4SLinus Torvalds 	u8 transaction_timeout;
19581da177e4SLinus Torvalds 
1959441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	if (edge_port == NULL)
19621da177e4SLinus Torvalds 		return -ENODEV;
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	port->tty->low_latency = low_latency;
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
19671da177e4SLinus Torvalds 	switch (port_number) {
19681da177e4SLinus Torvalds 		case 0:
19691da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART1;
19701da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB1_ADDRESS;
19711da177e4SLinus Torvalds 			break;
19721da177e4SLinus Torvalds 		case 1:
19731da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART2;
19741da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB2_ADDRESS;
19751da177e4SLinus Torvalds 			break;
19761da177e4SLinus Torvalds 		default:
19771da177e4SLinus Torvalds 			dev_err (&port->dev, "Unknown port number!!!\n");
19781da177e4SLinus Torvalds 			return -ENODEV;
19791da177e4SLinus Torvalds 	}
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1982441b62c1SHarvey Harrison 	     __func__, port_number, edge_port->uart_base, edge_port->dma_address);
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	dev = port->serial->dev;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 	memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
19871da177e4SLinus Torvalds 	init_waitqueue_head (&edge_port->delta_msr_wait);
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	/* turn off loopback */
19901da177e4SLinus Torvalds 	status = TIClearLoopBack (edge_port);
19911da177e4SLinus Torvalds 	if (status) {
19921da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1993441b62c1SHarvey Harrison 			__func__, status);
19941da177e4SLinus Torvalds 		return status;
19951da177e4SLinus Torvalds 	}
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	/* set up the port settings */
1998d5f5bcd4SAlan Cox 	edge_set_termios (port, port->tty->termios);
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/* open up the port */
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
20031da177e4SLinus Torvalds 	transaction_timeout = 2;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	// milliseconds to timeout for DMA transfer
20081da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
20091da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
20101da177e4SLinus Torvalds 			     (transaction_timeout << 2));
20111da177e4SLinus Torvalds 
2012441b62c1SHarvey Harrison 	dbg ("%s - Sending UMPC_OPEN_PORT", __func__);
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
20151da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
20161da177e4SLinus Torvalds 					UMPC_OPEN_PORT,
20171da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
20181da177e4SLinus Torvalds 					open_settings,
20191da177e4SLinus Torvalds 					NULL,
20201da177e4SLinus Torvalds 					0);
20211da177e4SLinus Torvalds 	if (status) {
2022441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send open command, %d\n", __func__, status);
20231da177e4SLinus Torvalds 		return status;
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	/* Start the DMA? */
20271da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
20281da177e4SLinus Torvalds 					UMPC_START_PORT,
20291da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
20301da177e4SLinus Torvalds 					0,
20311da177e4SLinus Torvalds 					NULL,
20321da177e4SLinus Torvalds 					0);
20331da177e4SLinus Torvalds 	if (status) {
2034441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __func__, status);
20351da177e4SLinus Torvalds 		return status;
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
20391da177e4SLinus Torvalds 	status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
20401da177e4SLinus Torvalds 	if (status) {
2041441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __func__, status);
20421da177e4SLinus Torvalds 		return status;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	/* Read Initial MSR */
20461da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (dev,
20471da177e4SLinus Torvalds 					UMPC_READ_MSR,	// Request
20481da177e4SLinus Torvalds 					0,		// wValue
20491da177e4SLinus Torvalds 					(__u16)(UMPM_UART1_PORT + port_number),	// wIndex (Address)
20501da177e4SLinus Torvalds 					&edge_port->shadow_msr,			// TransferBuffer
20511da177e4SLinus Torvalds 					1);					// TransferBufferLength
20521da177e4SLinus Torvalds 	if (status) {
2053441b62c1SHarvey Harrison  		dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __func__, status);
20541da177e4SLinus Torvalds 		return status;
20551da177e4SLinus Torvalds 	}
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	/* Set Initial MCR */
20601da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
20611da177e4SLinus Torvalds 	dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
2064241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
20651da177e4SLinus Torvalds 		return -ERESTARTSYS;
20661da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
20671da177e4SLinus Torvalds 		/* we are the first port to be opened, let's post the interrupt urb */
20681da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
20691da177e4SLinus Torvalds 		if (!urb) {
2070441b62c1SHarvey Harrison 			dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __func__);
20711da177e4SLinus Torvalds 			status = -EINVAL;
2072241ca64fSMatthias Kaehlcke 			goto release_es_lock;
20731da177e4SLinus Torvalds 		}
20741da177e4SLinus Torvalds 		urb->complete = edge_interrupt_callback;
20751da177e4SLinus Torvalds 		urb->context = edge_serial;
20761da177e4SLinus Torvalds 		urb->dev = dev;
20771da177e4SLinus Torvalds 		status = usb_submit_urb (urb, GFP_KERNEL);
20781da177e4SLinus Torvalds 		if (status) {
2079441b62c1SHarvey Harrison 			dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __func__, status);
2080241ca64fSMatthias Kaehlcke 			goto release_es_lock;
20811da177e4SLinus Torvalds 		}
20821da177e4SLinus Torvalds 	}
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	/*
20851da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
20861da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
20871da177e4SLinus Torvalds 	 */
20881da177e4SLinus Torvalds 	usb_clear_halt (dev, port->write_urb->pipe);
20891da177e4SLinus Torvalds 	usb_clear_halt (dev, port->read_urb->pipe);
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/* start up our bulk read urb */
20921da177e4SLinus Torvalds 	urb = port->read_urb;
20931da177e4SLinus Torvalds 	if (!urb) {
2094441b62c1SHarvey Harrison 		dev_err (&port->dev, "%s - no read urb present, exiting\n", __func__);
20951da177e4SLinus Torvalds 		status = -EINVAL;
20961da177e4SLinus Torvalds 		goto unlink_int_urb;
20971da177e4SLinus Torvalds 	}
20981da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
20991da177e4SLinus Torvalds 	urb->complete = edge_bulk_in_callback;
21001da177e4SLinus Torvalds 	urb->context = edge_port;
21011da177e4SLinus Torvalds 	urb->dev = dev;
21021da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_KERNEL);
21031da177e4SLinus Torvalds 	if (status) {
2104441b62c1SHarvey Harrison 		dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
21051da177e4SLinus Torvalds 		goto unlink_int_urb;
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
21091da177e4SLinus Torvalds 
2110441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
21111da177e4SLinus Torvalds 
2112241ca64fSMatthias Kaehlcke 	goto release_es_lock;
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds unlink_int_urb:
21151da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
21161da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2117241ca64fSMatthias Kaehlcke release_es_lock:
2118241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
21191da177e4SLinus Torvalds 	return status;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds static void edge_close (struct usb_serial_port *port, struct file *filp)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
21251da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
21261da177e4SLinus Torvalds 	int port_number;
21271da177e4SLinus Torvalds 	int status;
21281da177e4SLinus Torvalds 
2129441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
21321da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
21331da177e4SLinus Torvalds 	if ((edge_serial == NULL) || (edge_port == NULL))
21341da177e4SLinus Torvalds 		return;
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
21371da177e4SLinus Torvalds 	 * this flag and dump add read data */
21381da177e4SLinus Torvalds 	edge_port->close_pending = 1;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	/* chase the port close and flush */
21411da177e4SLinus Torvalds 	TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
21441da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
21451da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	/* assuming we can still talk to the device,
21481da177e4SLinus Torvalds 	 * send a close port command to it */
2149441b62c1SHarvey Harrison 	dbg("%s - send umpc_close_port", __func__);
21501da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
21511da177e4SLinus Torvalds 	status = TIWriteCommandSync (port->serial->dev,
21521da177e4SLinus Torvalds 				     UMPC_CLOSE_PORT,
21531da177e4SLinus Torvalds 				     (__u8)(UMPM_UART1_PORT + port_number),
21541da177e4SLinus Torvalds 				     0,
21551da177e4SLinus Torvalds 				     NULL,
21561da177e4SLinus Torvalds 				     0);
2157241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
21581da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
21591da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
21601da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
21611da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
21621da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
21631da177e4SLinus Torvalds 	}
2164241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
21651da177e4SLinus Torvalds 	edge_port->close_pending = 0;
21661da177e4SLinus Torvalds 
2167441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21731da177e4SLinus Torvalds 	unsigned long flags;
21741da177e4SLinus Torvalds 
2175441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 	if (count == 0) {
2178441b62c1SHarvey Harrison 		dbg("%s - write request of 0 bytes", __func__);
21791da177e4SLinus Torvalds 		return 0;
21801da177e4SLinus Torvalds 	}
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	if (edge_port == NULL)
21831da177e4SLinus Torvalds 		return -ENODEV;
21841da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21851da177e4SLinus Torvalds 		return -ENODEV;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21881da177e4SLinus Torvalds 	count = edge_buf_put(edge_port->ep_out_buf, data, count);
21891da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	edge_send(port);
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	return count;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port)
21971da177e4SLinus Torvalds {
21981da177e4SLinus Torvalds 	int count, result;
21991da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22001da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
22011da177e4SLinus Torvalds 	unsigned long flags;
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds 
2204441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
22091da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22101da177e4SLinus Torvalds 		return;
22111da177e4SLinus Torvalds 	}
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	count = edge_buf_get(edge_port->ep_out_buf,
22141da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
22151da177e4SLinus Torvalds 				port->bulk_out_size);
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	if (count == 0) {
22181da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22191da177e4SLinus Torvalds 		return;
22201da177e4SLinus Torvalds 	}
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22251da177e4SLinus Torvalds 
2226441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	/* set up our urb */
22291da177e4SLinus Torvalds 	usb_fill_bulk_urb (port->write_urb, port->serial->dev,
22301da177e4SLinus Torvalds 			   usb_sndbulkpipe (port->serial->dev,
22311da177e4SLinus Torvalds 					    port->bulk_out_endpointAddress),
22321da177e4SLinus Torvalds 			   port->write_urb->transfer_buffer, count,
22331da177e4SLinus Torvalds 			   edge_bulk_out_callback,
22341da177e4SLinus Torvalds 			   port);
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	/* send the data out the bulk port */
22371da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
22381da177e4SLinus Torvalds 	if (result) {
2239441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
22401da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
22411da177e4SLinus Torvalds 		// TODO: reschedule edge_send
22421da177e4SLinus Torvalds 	} else {
22431da177e4SLinus Torvalds 		edge_port->icount.tx += count;
22441da177e4SLinus Torvalds 	}
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
22471da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
22481da177e4SLinus Torvalds 	if (tty) {
22491da177e4SLinus Torvalds 		/* let the tty driver wakeup if it has a special write_wakeup function */
22501da177e4SLinus Torvalds 		tty_wakeup(tty);
22511da177e4SLinus Torvalds 	}
22521da177e4SLinus Torvalds }
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds static int edge_write_room (struct usb_serial_port *port)
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22571da177e4SLinus Torvalds 	int room = 0;
22581da177e4SLinus Torvalds 	unsigned long flags;
22591da177e4SLinus Torvalds 
2260441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	if (edge_port == NULL)
22631da177e4SLinus Torvalds 		return -ENODEV;
22641da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22651da177e4SLinus Torvalds 		return -ENODEV;
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22681da177e4SLinus Torvalds 	room = edge_buf_space_avail(edge_port->ep_out_buf);
22691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22701da177e4SLinus Torvalds 
2271441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
22721da177e4SLinus Torvalds 	return room;
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds static int edge_chars_in_buffer (struct usb_serial_port *port)
22761da177e4SLinus Torvalds {
22771da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22781da177e4SLinus Torvalds 	int chars = 0;
22791da177e4SLinus Torvalds 	unsigned long flags;
22801da177e4SLinus Torvalds 
2281441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	if (edge_port == NULL)
22841da177e4SLinus Torvalds 		return -ENODEV;
22851da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22861da177e4SLinus Torvalds 		return -ENODEV;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22891da177e4SLinus Torvalds 	chars = edge_buf_data_avail(edge_port->ep_out_buf);
22901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22911da177e4SLinus Torvalds 
2292441b62c1SHarvey Harrison 	dbg ("%s - returns %d", __func__, chars);
22931da177e4SLinus Torvalds 	return chars;
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds static void edge_throttle (struct usb_serial_port *port)
22971da177e4SLinus Torvalds {
22981da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22991da177e4SLinus Torvalds 	struct tty_struct *tty;
23001da177e4SLinus Torvalds 	int status;
23011da177e4SLinus Torvalds 
2302441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	if (edge_port == NULL)
23051da177e4SLinus Torvalds 		return;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	tty = port->tty;
23081da177e4SLinus Torvalds 	if (!tty) {
2309441b62c1SHarvey Harrison 		dbg ("%s - no tty available", __func__);
23101da177e4SLinus Torvalds 		return;
23111da177e4SLinus Torvalds 	}
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
23141da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
23151da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
23161da177e4SLinus Torvalds 		status = edge_write (port, &stop_char, 1);
23171da177e4SLinus Torvalds 		if (status <= 0) {
2318441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
23191da177e4SLinus Torvalds 		}
23201da177e4SLinus Torvalds 	}
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
23231da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
23241da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
23251da177e4SLinus Torvalds 		stop_read(edge_port);
23261da177e4SLinus Torvalds 
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds static void edge_unthrottle (struct usb_serial_port *port)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23321da177e4SLinus Torvalds 	struct tty_struct *tty;
23331da177e4SLinus Torvalds 	int status;
23341da177e4SLinus Torvalds 
2335441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	if (edge_port == NULL)
23381da177e4SLinus Torvalds 		return;
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	tty = port->tty;
23411da177e4SLinus Torvalds 	if (!tty) {
2342441b62c1SHarvey Harrison 		dbg ("%s - no tty available", __func__);
23431da177e4SLinus Torvalds 		return;
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
23471da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
23481da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
23491da177e4SLinus Torvalds 		status = edge_write (port, &start_char, 1);
23501da177e4SLinus Torvalds 		if (status <= 0) {
2351441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
23521da177e4SLinus Torvalds 		}
23531da177e4SLinus Torvalds 	}
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
23561da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
23571da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
23581da177e4SLinus Torvalds 		status = restart_read(edge_port);
23591da177e4SLinus Torvalds 		if (status)
2360441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
23611da177e4SLinus Torvalds 	}
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
23661da177e4SLinus Torvalds {
23671da177e4SLinus Torvalds 	unsigned long flags;
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
23721da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
23731da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23761da177e4SLinus Torvalds }
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
23791da177e4SLinus Torvalds {
23801da177e4SLinus Torvalds 	struct urb *urb;
23811da177e4SLinus Torvalds 	int status = 0;
23821da177e4SLinus Torvalds 	unsigned long flags;
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
23871da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
23881da177e4SLinus Torvalds 		urb->complete = edge_bulk_in_callback;
23891da177e4SLinus Torvalds 		urb->context = edge_port;
23901da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
2391efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
23941da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	return status;
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
2401606d099cSAlan Cox static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	struct ump_uart_config *config;
24041da177e4SLinus Torvalds 	struct tty_struct *tty;
24051da177e4SLinus Torvalds 	int baud;
24061da177e4SLinus Torvalds 	unsigned cflag;
24071da177e4SLinus Torvalds 	int status;
24081da177e4SLinus Torvalds 	int port_number = edge_port->port->number - edge_port->port->serial->minor;
24091da177e4SLinus Torvalds 
2410441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, edge_port->port->number);
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds 	tty = edge_port->port->tty;
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
24151da177e4SLinus Torvalds 	if (!config) {
2416441b62c1SHarvey Harrison 		dev_err (&edge_port->port->dev, "%s - out of memory\n", __func__);
24171da177e4SLinus Torvalds 		return;
24181da177e4SLinus Torvalds 	}
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	config->wFlags = 0;
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	/* These flags must be set */
24251da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
24261da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
24271da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
24301da177e4SLinus Torvalds 		case CS5:
24311da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR5BITS;
2432441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 5", __func__);
24331da177e4SLinus Torvalds 			    break;
24341da177e4SLinus Torvalds 		case CS6:
24351da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR6BITS;
2436441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 6", __func__);
24371da177e4SLinus Torvalds 			    break;
24381da177e4SLinus Torvalds 		case CS7:
24391da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR7BITS;
2440441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 7", __func__);
24411da177e4SLinus Torvalds 			    break;
24421da177e4SLinus Torvalds 		default:
24431da177e4SLinus Torvalds 		case CS8:
24441da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR8BITS;
2445441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 8", __func__);
24461da177e4SLinus Torvalds 			    break;
24471da177e4SLinus Torvalds 	}
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 	if (cflag & PARENB) {
24501da177e4SLinus Torvalds 		if (cflag & PARODD) {
24511da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24521da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
2453441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
24541da177e4SLinus Torvalds 		} else {
24551da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24561da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
2457441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
24581da177e4SLinus Torvalds 		}
24591da177e4SLinus Torvalds 	} else {
24601da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
2461441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
24651da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
2466441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
24671da177e4SLinus Torvalds 	} else {
24681da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
2469441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	/* figure out the flow control settings */
24731da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
24741da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
24751da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2476441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is enabled", __func__);
24771da177e4SLinus Torvalds 	} else {
2478441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is disabled", __func__);
24791da177e4SLinus Torvalds 		tty->hw_stopped = 0;
24801da177e4SLinus Torvalds 		restart_read(edge_port);
24811da177e4SLinus Torvalds 	}
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, set the start and stop character in the device */
24841da177e4SLinus Torvalds 	if (I_IXOFF(tty) || I_IXON(tty)) {
24851da177e4SLinus Torvalds 		config->cXon  = START_CHAR(tty);
24861da177e4SLinus Torvalds 		config->cXoff = STOP_CHAR(tty);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 		/* if we are implementing INBOUND XON/XOFF */
24891da177e4SLinus Torvalds 		if (I_IXOFF(tty)) {
24901da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
24911da177e4SLinus Torvalds 			dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2492441b62c1SHarvey Harrison 			     __func__, config->cXon, config->cXoff);
24931da177e4SLinus Torvalds 		} else {
2494441b62c1SHarvey Harrison 			dbg ("%s - INBOUND XON/XOFF is disabled", __func__);
24951da177e4SLinus Torvalds 		}
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 		/* if we are implementing OUTBOUND XON/XOFF */
24981da177e4SLinus Torvalds 		if (I_IXON(tty)) {
24991da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
25001da177e4SLinus Torvalds 			dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2501441b62c1SHarvey Harrison 			     __func__, config->cXon, config->cXoff);
25021da177e4SLinus Torvalds 		} else {
2503441b62c1SHarvey Harrison 			dbg ("%s - OUTBOUND XON/XOFF is disabled", __func__);
25041da177e4SLinus Torvalds 		}
25051da177e4SLinus Torvalds 	}
25061da177e4SLinus Torvalds 
2507d5f5bcd4SAlan Cox 	tty->termios->c_cflag &= ~CMSPAR;
2508d5f5bcd4SAlan Cox 
25091da177e4SLinus Torvalds 	/* Round the baud rate */
25101da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
25111da177e4SLinus Torvalds 	if (!baud) {
25121da177e4SLinus Torvalds 		/* pick a default, any default... */
25131da177e4SLinus Torvalds 		baud = 9600;
2514d5f5bcd4SAlan Cox 	} else
2515d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
2516d5f5bcd4SAlan Cox 
25171da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
25181da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
25191da177e4SLinus Torvalds 
2520d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2521d5f5bcd4SAlan Cox 
2522441b62c1SHarvey Harrison 	dbg ("%s - baud rate = %d, wBaudRate = %d", __func__, baud, config->wBaudRate);
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
25251da177e4SLinus Torvalds 	dbg ("wFlags:    0x%x", config->wFlags);
25261da177e4SLinus Torvalds 	dbg ("bDataBits:   %d", config->bDataBits);
25271da177e4SLinus Torvalds 	dbg ("bParity:     %d", config->bParity);
25281da177e4SLinus Torvalds 	dbg ("bStopBits:   %d", config->bStopBits);
25291da177e4SLinus Torvalds 	dbg ("cXon:        %d", config->cXon);
25301da177e4SLinus Torvalds 	dbg ("cXoff:       %d", config->cXoff);
25311da177e4SLinus Torvalds 	dbg ("bUartMode:   %d", config->bUartMode);
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	/* move the word values into big endian mode */
25341da177e4SLinus Torvalds 	cpu_to_be16s (&config->wFlags);
25351da177e4SLinus Torvalds 	cpu_to_be16s (&config->wBaudRate);
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	status = TIWriteCommandSync (edge_port->port->serial->dev,
25381da177e4SLinus Torvalds 				UMPC_SET_CONFIG,
25391da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
25401da177e4SLinus Torvalds 				0,
25411da177e4SLinus Torvalds 				(__u8 *)config,
25421da177e4SLinus Torvalds 				sizeof(*config));
25431da177e4SLinus Torvalds 	if (status) {
25441da177e4SLinus Torvalds 		dbg ("%s - error %d when trying to write config to device",
2545441b62c1SHarvey Harrison 		     __func__, status);
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	kfree (config);
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 	return;
25511da177e4SLinus Torvalds }
25521da177e4SLinus Torvalds 
2553606d099cSAlan Cox static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25561da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
25571da177e4SLinus Torvalds 	unsigned int cflag;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
25601da177e4SLinus Torvalds 
2561441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
25621da177e4SLinus Torvalds 	    tty->termios->c_cflag, tty->termios->c_iflag);
2563441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
25641da177e4SLinus Torvalds 	    old_termios->c_cflag, old_termios->c_iflag);
25651da177e4SLinus Torvalds 
2566441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	if (edge_port == NULL)
25691da177e4SLinus Torvalds 		return;
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
25721da177e4SLinus Torvalds 	change_port_settings (edge_port, old_termios);
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	return;
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
25781da177e4SLinus Torvalds {
25791da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25801da177e4SLinus Torvalds 	unsigned int mcr;
25813d71fe0bSAlan Cox 	unsigned long flags;
25821da177e4SLinus Torvalds 
2583441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25841da177e4SLinus Torvalds 
25853d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
25861da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
25871da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
25881da177e4SLinus Torvalds 		mcr |= MCR_RTS;
25891da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
25901da177e4SLinus Torvalds 		mcr |= MCR_DTR;
25911da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
25921da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
25951da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
25961da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
25971da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
25981da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
25991da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
26023d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	TIRestoreMCR (edge_port, mcr);
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	return 0;
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
26101da177e4SLinus Torvalds {
26111da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
26121da177e4SLinus Torvalds 	unsigned int result = 0;
26131da177e4SLinus Torvalds 	unsigned int msr;
26141da177e4SLinus Torvalds 	unsigned int mcr;
26153d71fe0bSAlan Cox 	unsigned long flags;
26161da177e4SLinus Torvalds 
2617441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
26181da177e4SLinus Torvalds 
26193d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
26203d71fe0bSAlan Cox 
26211da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
26221da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
26231da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
26241da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
26251da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
26261da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
26271da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
26281da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 
2631441b62c1SHarvey Harrison 	dbg("%s -- %x", __func__, result);
26323d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	return result;
26351da177e4SLinus Torvalds }
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
26381da177e4SLinus Torvalds {
26391da177e4SLinus Torvalds 	struct serial_struct tmp;
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	if (!retinfo)
26421da177e4SLinus Torvalds 		return -EFAULT;
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
26471da177e4SLinus Torvalds 	tmp.line		= edge_port->port->serial->minor;
26481da177e4SLinus Torvalds 	tmp.port		= edge_port->port->number;
26491da177e4SLinus Torvalds 	tmp.irq			= 0;
26501da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
26511da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
26521da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
26531da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
26541da177e4SLinus Torvalds 	tmp.closing_wait	= closing_wait;
26551da177e4SLinus Torvalds //	tmp.custom_divisor	= state->custom_divisor;
26561da177e4SLinus Torvalds //	tmp.hub6		= state->hub6;
26571da177e4SLinus Torvalds //	tmp.io_type		= state->io_type;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
26611da177e4SLinus Torvalds 		return -EFAULT;
26621da177e4SLinus Torvalds 	return 0;
26631da177e4SLinus Torvalds }
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
26661da177e4SLinus Torvalds {
26671da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
26681da177e4SLinus Torvalds 	struct async_icount cnow;
26691da177e4SLinus Torvalds 	struct async_icount cprev;
26701da177e4SLinus Torvalds 
2671441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 	switch (cmd) {
26741da177e4SLinus Torvalds 		case TIOCINQ:
2675441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCINQ", __func__, port->number);
26761da177e4SLinus Torvalds //			return get_number_bytes_avail(edge_port, (unsigned int *) arg);
26771da177e4SLinus Torvalds 			break;
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 		case TIOCSERGETLSR:
2680441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCSERGETLSR", __func__, port->number);
26811da177e4SLinus Torvalds //			return get_lsr_info(edge_port, (unsigned int *) arg);
26821da177e4SLinus Torvalds 			break;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 		case TIOCGSERIAL:
2685441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
26861da177e4SLinus Torvalds 			return get_serial_info(edge_port, (struct serial_struct __user *) arg);
26871da177e4SLinus Torvalds 			break;
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 		case TIOCSSERIAL:
2690441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
26911da177e4SLinus Torvalds 			break;
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 		case TIOCMIWAIT:
2694441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
26951da177e4SLinus Torvalds 			cprev = edge_port->icount;
26961da177e4SLinus Torvalds 			while (1) {
26971da177e4SLinus Torvalds 				interruptible_sleep_on(&edge_port->delta_msr_wait);
26981da177e4SLinus Torvalds 				/* see if a signal did it */
26991da177e4SLinus Torvalds 				if (signal_pending(current))
27001da177e4SLinus Torvalds 					return -ERESTARTSYS;
27011da177e4SLinus Torvalds 				cnow = edge_port->icount;
27021da177e4SLinus Torvalds 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
27031da177e4SLinus Torvalds 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
27041da177e4SLinus Torvalds 					return -EIO; /* no change => error */
27051da177e4SLinus Torvalds 				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
27061da177e4SLinus Torvalds 				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
27071da177e4SLinus Torvalds 				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
27081da177e4SLinus Torvalds 				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
27091da177e4SLinus Torvalds 					return 0;
27101da177e4SLinus Torvalds 				}
27111da177e4SLinus Torvalds 				cprev = cnow;
27121da177e4SLinus Torvalds 			}
27131da177e4SLinus Torvalds 			/* not reached */
27141da177e4SLinus Torvalds 			break;
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 		case TIOCGICOUNT:
2717441b62c1SHarvey Harrison 			dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
27181da177e4SLinus Torvalds 			     port->number, edge_port->icount.rx, edge_port->icount.tx);
27191da177e4SLinus Torvalds 			if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
27201da177e4SLinus Torvalds 				return -EFAULT;
27211da177e4SLinus Torvalds 			return 0;
27221da177e4SLinus Torvalds 	}
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds static void edge_break (struct usb_serial_port *port, int break_state)
27281da177e4SLinus Torvalds {
27291da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
27301da177e4SLinus Torvalds 	int status;
27311da177e4SLinus Torvalds 
2732441b62c1SHarvey Harrison 	dbg ("%s - state = %d", __func__, break_state);
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	/* chase the port close */
27351da177e4SLinus Torvalds 	TIChasePort (edge_port, 0, 0);
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 	if (break_state == -1) {
27381da177e4SLinus Torvalds 		status = TISetBreak (edge_port);
27391da177e4SLinus Torvalds 	} else {
27401da177e4SLinus Torvalds 		status = TIClearBreak (edge_port);
27411da177e4SLinus Torvalds 	}
27421da177e4SLinus Torvalds 	if (status) {
27431da177e4SLinus Torvalds 		dbg ("%s - error %d sending break set/clear command.",
2744441b62c1SHarvey Harrison 		     __func__, status);
27451da177e4SLinus Torvalds 	}
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds static int edge_startup (struct usb_serial *serial)
27491da177e4SLinus Torvalds {
27501da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
27511da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
27521da177e4SLinus Torvalds 	struct usb_device *dev;
27531da177e4SLinus Torvalds 	int status;
27541da177e4SLinus Torvalds 	int i;
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 	dev = serial->dev;
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	/* create our private serial structure */
275980b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
27601da177e4SLinus Torvalds 	if (edge_serial == NULL) {
2761441b62c1SHarvey Harrison 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27621da177e4SLinus Torvalds 		return -ENOMEM;
27631da177e4SLinus Torvalds 	}
2764241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
27651da177e4SLinus Torvalds 	edge_serial->serial = serial;
27661da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	status = TIDownloadFirmware (edge_serial);
27691da177e4SLinus Torvalds 	if (status) {
27701da177e4SLinus Torvalds 		kfree (edge_serial);
27711da177e4SLinus Torvalds 		return status;
27721da177e4SLinus Torvalds 	}
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	/* set up our port private structures */
27751da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
277680b6ca48SEric Sesterhenn 		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
27771da177e4SLinus Torvalds 		if (edge_port == NULL) {
2778441b62c1SHarvey Harrison 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27791da177e4SLinus Torvalds 			goto cleanup;
27801da177e4SLinus Torvalds 		}
27811da177e4SLinus Torvalds 		spin_lock_init(&edge_port->ep_lock);
27821da177e4SLinus Torvalds 		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
27831da177e4SLinus Torvalds 		if (edge_port->ep_out_buf == NULL) {
2784441b62c1SHarvey Harrison 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27851da177e4SLinus Torvalds 			kfree(edge_port);
27861da177e4SLinus Torvalds 			goto cleanup;
27871da177e4SLinus Torvalds 		}
27881da177e4SLinus Torvalds 		edge_port->port = serial->port[i];
27891da177e4SLinus Torvalds 		edge_port->edge_serial = edge_serial;
27901da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], edge_port);
2791fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = default_uart_mode;
27921da177e4SLinus Torvalds 	}
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds 	return 0;
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds cleanup:
27971da177e4SLinus Torvalds 	for (--i; i>=0; --i) {
27981da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
27991da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
28001da177e4SLinus Torvalds 		kfree(edge_port);
28011da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
28021da177e4SLinus Torvalds 	}
28031da177e4SLinus Torvalds 	kfree (edge_serial);
28041da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
28051da177e4SLinus Torvalds 	return -ENOMEM;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds static void edge_shutdown (struct usb_serial *serial)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	int i;
28111da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
28121da177e4SLinus Torvalds 
2813441b62c1SHarvey Harrison 	dbg ("%s", __func__);
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
28161da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
2817fc4cbd75SMartin K. Petersen 		edge_remove_sysfs_attrs(edge_port->port);
28181da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
28191da177e4SLinus Torvalds 		kfree(edge_port);
28201da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
28211da177e4SLinus Torvalds 	}
28221da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
28231da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds 
2827fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2828fc4cbd75SMartin K. Petersen 
2829fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev,
2830fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2831fc4cbd75SMartin K. Petersen {
2832fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2833fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2834fc4cbd75SMartin K. Petersen 
2835fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2836fc4cbd75SMartin K. Petersen }
2837fc4cbd75SMartin K. Petersen 
2838fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev,
2839fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2840fc4cbd75SMartin K. Petersen {
2841fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2842fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2843fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2844fc4cbd75SMartin K. Petersen 
2845441b62c1SHarvey Harrison 	dbg("%s: setting uart_mode = %d", __func__, v);
2846fc4cbd75SMartin K. Petersen 
2847fc4cbd75SMartin K. Petersen 	if (v < 256)
2848fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2849fc4cbd75SMartin K. Petersen 	else
2850441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2851fc4cbd75SMartin K. Petersen 
2852fc4cbd75SMartin K. Petersen 	return count;
2853fc4cbd75SMartin K. Petersen }
2854fc4cbd75SMartin K. Petersen 
2855fc4cbd75SMartin K. Petersen static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode);
2856fc4cbd75SMartin K. Petersen 
2857fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2858fc4cbd75SMartin K. Petersen {
2859fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2860fc4cbd75SMartin K. Petersen }
2861fc4cbd75SMartin K. Petersen 
2862fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2863fc4cbd75SMartin K. Petersen {
2864fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2865fc4cbd75SMartin K. Petersen 	return 0;
2866fc4cbd75SMartin K. Petersen }
2867fc4cbd75SMartin K. Petersen 
2868fc4cbd75SMartin K. Petersen 
28691da177e4SLinus Torvalds /* Circular Buffer */
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds /*
28721da177e4SLinus Torvalds  * edge_buf_alloc
28731da177e4SLinus Torvalds  *
28741da177e4SLinus Torvalds  * Allocate a circular buffer and all associated memory.
28751da177e4SLinus Torvalds  */
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size)
28781da177e4SLinus Torvalds {
28791da177e4SLinus Torvalds 	struct edge_buf *eb;
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	if (size == 0)
28831da177e4SLinus Torvalds 		return NULL;
28841da177e4SLinus Torvalds 
28855cbded58SRobert P. J. Day 	eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
28861da177e4SLinus Torvalds 	if (eb == NULL)
28871da177e4SLinus Torvalds 		return NULL;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	eb->buf_buf = kmalloc(size, GFP_KERNEL);
28901da177e4SLinus Torvalds 	if (eb->buf_buf == NULL) {
28911da177e4SLinus Torvalds 		kfree(eb);
28921da177e4SLinus Torvalds 		return NULL;
28931da177e4SLinus Torvalds 	}
28941da177e4SLinus Torvalds 
28951da177e4SLinus Torvalds 	eb->buf_size = size;
28961da177e4SLinus Torvalds 	eb->buf_get = eb->buf_put = eb->buf_buf;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	return eb;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds /*
29031da177e4SLinus Torvalds  * edge_buf_free
29041da177e4SLinus Torvalds  *
29051da177e4SLinus Torvalds  * Free the buffer and all associated memory.
29061da177e4SLinus Torvalds  */
29071da177e4SLinus Torvalds 
29083d48586cSAdrian Bunk static void edge_buf_free(struct edge_buf *eb)
29091da177e4SLinus Torvalds {
29101bc3c9e1SJesper Juhl 	if (eb) {
29111da177e4SLinus Torvalds 		kfree(eb->buf_buf);
29121da177e4SLinus Torvalds 		kfree(eb);
29131da177e4SLinus Torvalds 	}
29141da177e4SLinus Torvalds }
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds /*
29181da177e4SLinus Torvalds  * edge_buf_clear
29191da177e4SLinus Torvalds  *
29201da177e4SLinus Torvalds  * Clear out all data in the circular buffer.
29211da177e4SLinus Torvalds  */
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb)
29241da177e4SLinus Torvalds {
29251da177e4SLinus Torvalds         if (eb != NULL)
29261da177e4SLinus Torvalds                 eb->buf_get = eb->buf_put;
29271da177e4SLinus Torvalds                 /* equivalent to a get of all data available */
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds 
29311da177e4SLinus Torvalds /*
29321da177e4SLinus Torvalds  * edge_buf_data_avail
29331da177e4SLinus Torvalds  *
29341da177e4SLinus Torvalds  * Return the number of bytes of data available in the circular
29351da177e4SLinus Torvalds  * buffer.
29361da177e4SLinus Torvalds  */
29371da177e4SLinus Torvalds 
29381da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb)
29391da177e4SLinus Torvalds {
29401da177e4SLinus Torvalds 	if (eb != NULL)
29411da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
29421da177e4SLinus Torvalds 	else
29431da177e4SLinus Torvalds 		return 0;
29441da177e4SLinus Torvalds }
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds /*
29481da177e4SLinus Torvalds  * edge_buf_space_avail
29491da177e4SLinus Torvalds  *
29501da177e4SLinus Torvalds  * Return the number of bytes of space available in the circular
29511da177e4SLinus Torvalds  * buffer.
29521da177e4SLinus Torvalds  */
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb)
29551da177e4SLinus Torvalds {
29561da177e4SLinus Torvalds 	if (eb != NULL)
29571da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
29581da177e4SLinus Torvalds 	else
29591da177e4SLinus Torvalds 		return 0;
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds /*
29641da177e4SLinus Torvalds  * edge_buf_put
29651da177e4SLinus Torvalds  *
29661da177e4SLinus Torvalds  * Copy data data from a user buffer and put it into the circular buffer.
29671da177e4SLinus Torvalds  * Restrict to the amount of space available.
29681da177e4SLinus Torvalds  *
29691da177e4SLinus Torvalds  * Return the number of bytes copied.
29701da177e4SLinus Torvalds  */
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
29731da177e4SLinus Torvalds 	unsigned int count)
29741da177e4SLinus Torvalds {
29751da177e4SLinus Torvalds 	unsigned int len;
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	if (eb == NULL)
29791da177e4SLinus Torvalds 		return 0;
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	len  = edge_buf_space_avail(eb);
29821da177e4SLinus Torvalds 	if (count > len)
29831da177e4SLinus Torvalds 		count = len;
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 	if (count == 0)
29861da177e4SLinus Torvalds 		return 0;
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_put;
29891da177e4SLinus Torvalds 	if (count > len) {
29901da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, len);
29911da177e4SLinus Torvalds 		memcpy(eb->buf_buf, buf+len, count - len);
29921da177e4SLinus Torvalds 		eb->buf_put = eb->buf_buf + count - len;
29931da177e4SLinus Torvalds 	} else {
29941da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, count);
29951da177e4SLinus Torvalds 		if (count < len)
29961da177e4SLinus Torvalds 			eb->buf_put += count;
29971da177e4SLinus Torvalds 		else /* count == len */
29981da177e4SLinus Torvalds 			eb->buf_put = eb->buf_buf;
29991da177e4SLinus Torvalds 	}
30001da177e4SLinus Torvalds 
30011da177e4SLinus Torvalds 	return count;
30021da177e4SLinus Torvalds }
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds /*
30061da177e4SLinus Torvalds  * edge_buf_get
30071da177e4SLinus Torvalds  *
30081da177e4SLinus Torvalds  * Get data from the circular buffer and copy to the given buffer.
30091da177e4SLinus Torvalds  * Restrict to the amount of data available.
30101da177e4SLinus Torvalds  *
30111da177e4SLinus Torvalds  * Return the number of bytes copied.
30121da177e4SLinus Torvalds  */
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
30151da177e4SLinus Torvalds 	unsigned int count)
30161da177e4SLinus Torvalds {
30171da177e4SLinus Torvalds 	unsigned int len;
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 	if (eb == NULL)
30211da177e4SLinus Torvalds 		return 0;
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	len = edge_buf_data_avail(eb);
30241da177e4SLinus Torvalds 	if (count > len)
30251da177e4SLinus Torvalds 		count = len;
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 	if (count == 0)
30281da177e4SLinus Torvalds 		return 0;
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_get;
30311da177e4SLinus Torvalds 	if (count > len) {
30321da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, len);
30331da177e4SLinus Torvalds 		memcpy(buf+len, eb->buf_buf, count - len);
30341da177e4SLinus Torvalds 		eb->buf_get = eb->buf_buf + count - len;
30351da177e4SLinus Torvalds 	} else {
30361da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, count);
30371da177e4SLinus Torvalds 		if (count < len)
30381da177e4SLinus Torvalds 			eb->buf_get += count;
30391da177e4SLinus Torvalds 		else /* count == len */
30401da177e4SLinus Torvalds 			eb->buf_get = eb->buf_buf;
30411da177e4SLinus Torvalds 	}
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	return count;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds 
3047ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
304818fcac35SGreg Kroah-Hartman 	.driver = {
30491da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
3050269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
305118fcac35SGreg Kroah-Hartman 	},
3052269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
3053d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
30541da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
30551da177e4SLinus Torvalds 	.num_ports		= 1,
30561da177e4SLinus Torvalds 	.open			= edge_open,
30571da177e4SLinus Torvalds 	.close			= edge_close,
30581da177e4SLinus Torvalds 	.throttle		= edge_throttle,
30591da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
30601da177e4SLinus Torvalds 	.attach			= edge_startup,
30611da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
3062fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
30631da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
30641da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
30651da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
30661da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
30671da177e4SLinus Torvalds 	.write			= edge_write,
30681da177e4SLinus Torvalds 	.write_room		= edge_write_room,
30691da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30701da177e4SLinus Torvalds 	.break_ctl		= edge_break,
30711da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
30721da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30731da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30741da177e4SLinus Torvalds };
30751da177e4SLinus Torvalds 
3076ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
307718fcac35SGreg Kroah-Hartman 	.driver = {
30781da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
3079269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
308018fcac35SGreg Kroah-Hartman 	},
3081269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
3082d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
30831da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
30841da177e4SLinus Torvalds 	.num_ports		= 2,
30851da177e4SLinus Torvalds 	.open			= edge_open,
30861da177e4SLinus Torvalds 	.close			= edge_close,
30871da177e4SLinus Torvalds 	.throttle		= edge_throttle,
30881da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
30891da177e4SLinus Torvalds 	.attach			= edge_startup,
30901da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
3091fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
30921da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
30931da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
30941da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
30951da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
30961da177e4SLinus Torvalds 	.write			= edge_write,
30971da177e4SLinus Torvalds 	.write_room		= edge_write_room,
30981da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30991da177e4SLinus Torvalds 	.break_ctl		= edge_break,
31001da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
31011da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
31021da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
31031da177e4SLinus Torvalds };
31041da177e4SLinus Torvalds 
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds static int __init edgeport_init(void)
31071da177e4SLinus Torvalds {
31081da177e4SLinus Torvalds 	int retval;
31091da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_1port_device);
31101da177e4SLinus Torvalds 	if (retval)
31111da177e4SLinus Torvalds 		goto failed_1port_device_register;
31121da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_2port_device);
31131da177e4SLinus Torvalds 	if (retval)
31141da177e4SLinus Torvalds 		goto failed_2port_device_register;
31151da177e4SLinus Torvalds 	retval = usb_register(&io_driver);
31161da177e4SLinus Torvalds 	if (retval)
31171da177e4SLinus Torvalds 		goto failed_usb_register;
31181da177e4SLinus Torvalds 	info(DRIVER_DESC " " DRIVER_VERSION);
31191da177e4SLinus Torvalds 	return 0;
31201da177e4SLinus Torvalds failed_usb_register:
31211da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_2port_device);
31221da177e4SLinus Torvalds failed_2port_device_register:
31231da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_1port_device);
31241da177e4SLinus Torvalds failed_1port_device_register:
31251da177e4SLinus Torvalds 	return retval;
31261da177e4SLinus Torvalds }
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds static void __exit edgeport_exit (void)
31291da177e4SLinus Torvalds {
31301da177e4SLinus Torvalds 	usb_deregister (&io_driver);
31311da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_1port_device);
31321da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_2port_device);
31331da177e4SLinus Torvalds }
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds module_init(edgeport_init);
31361da177e4SLinus Torvalds module_exit(edgeport_exit);
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds /* Module information */
31391da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
31401da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
31411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3142*d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
31451da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds module_param(low_latency, bool, S_IRUGO | S_IWUSR);
31481da177e4SLinus Torvalds MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
31511da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
31541da177e4SLinus Torvalds MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
31551da177e4SLinus Torvalds 
3156fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3157fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
3158fc4cbd75SMartin K. Petersen 
3159