xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision 7d40d7e85a25e01948bcb4dc3eda1355af318337)
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>
40d12b219aSJaswinder Singh #include <linux/firmware.h>
412742fd88SAlan Cox #include <linux/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 {
602742fd88SAlan Cox 	__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 
672742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
682742fd88SAlan Cox #define	TI_MODE_CONFIGURING	0   /* Device has not entered start device */
692742fd88SAlan Cox #define	TI_MODE_BOOT		1   /* Staying in boot mode		   */
702742fd88SAlan Cox #define TI_MODE_DOWNLOAD	2   /* Made it to download mode		   */
712742fd88SAlan Cox #define TI_MODE_TRANSITIONING	3   /* Currently in boot mode but
722742fd88SAlan Cox 				       transitioning to download mode	   */
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* read urb state */
751da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
761da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
771da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
781da177e4SLinus Torvalds 
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 */
852742fd88SAlan Cox struct product_info {
862742fd88SAlan Cox 	int	TiMode;			/* Current TI Mode  */
872742fd88SAlan Cox 	__u8	hardware_type;		/* Type of hardware */
881da177e4SLinus Torvalds } __attribute__((packed));
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* circular buffer */
911da177e4SLinus Torvalds struct edge_buf {
921da177e4SLinus Torvalds 	unsigned int	buf_size;
931da177e4SLinus Torvalds 	char		*buf_buf;
941da177e4SLinus Torvalds 	char		*buf_get;
951da177e4SLinus Torvalds 	char		*buf_put;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds struct edgeport_port {
991da177e4SLinus Torvalds 	__u16 uart_base;
1001da177e4SLinus Torvalds 	__u16 dma_address;
1011da177e4SLinus Torvalds 	__u8 shadow_msr;
1021da177e4SLinus Torvalds 	__u8 shadow_mcr;
1031da177e4SLinus Torvalds 	__u8 shadow_lsr;
1041da177e4SLinus Torvalds 	__u8 lsr_mask;
10519af5cdbSMartin Olsson 	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
1061da177e4SLinus Torvalds 					   wait without data before completing
1071da177e4SLinus Torvalds 					   a read short */
1081da177e4SLinus Torvalds 	int baud_rate;
1091da177e4SLinus Torvalds 	int close_pending;
1101da177e4SLinus Torvalds 	int lsr_event;
1111da177e4SLinus Torvalds 	struct edgeport_uart_buf_desc tx;
1121da177e4SLinus Torvalds 	struct async_icount	icount;
1131da177e4SLinus Torvalds 	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
1141da177e4SLinus Torvalds 						   waiting for msr change to
1151da177e4SLinus Torvalds 						   happen */
1161da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
1171da177e4SLinus Torvalds 	struct usb_serial_port	*port;
1181da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
1191da177e4SLinus Torvalds 	spinlock_t ep_lock;
1201da177e4SLinus Torvalds 	int ep_read_urb_state;
1211da177e4SLinus Torvalds 	int ep_write_urb_in_use;
1221da177e4SLinus Torvalds 	struct edge_buf *ep_out_buf;
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds struct edgeport_serial {
1261da177e4SLinus Torvalds 	struct product_info product_info;
1272742fd88SAlan Cox 	u8 TI_I2C_Type;			/* Type of I2C in UMP */
1282742fd88SAlan Cox 	u8 TiReadI2C;			/* Set to TRUE if we have read the
1292742fd88SAlan Cox 					   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 */
137*7d40d7e8SNémeth Márton static const 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 
157*7d40d7e8SNémeth Márton static const 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 */
180*7d40d7e8SNémeth Márton static const 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 
228d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion;
229d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion;
230d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static int debug;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
2352742fd88SAlan Cox static int ignore_cpu_rev;
2362742fd88SAlan Cox static int default_uart_mode;		/* RS232 */
2371da177e4SLinus Torvalds 
2382742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2392742fd88SAlan Cox 			  unsigned char *data, int length);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2421da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2431da177e4SLinus Torvalds 
24495da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
24595da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
24695da310eSAlan Cox static void edge_send(struct tty_struct *tty);
2471da177e4SLinus Torvalds 
248fc4cbd75SMartin K. Petersen /* sysfs attributes */
249fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
250fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
251fc4cbd75SMartin K. Petersen 
2521da177e4SLinus Torvalds /* circular buffer */
2531da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size);
2541da177e4SLinus Torvalds static void edge_buf_free(struct edge_buf *eb);
2551da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb);
2561da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb);
2571da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb);
2581da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2591da177e4SLinus Torvalds 	unsigned int count);
2601da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2611da177e4SLinus Torvalds 	unsigned int count);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 
2642742fd88SAlan Cox static int ti_vread_sync(struct usb_device *dev, __u8 request,
2652742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	int status;
2681da177e4SLinus Torvalds 
2692742fd88SAlan Cox 	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
2702742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
2712742fd88SAlan Cox 			value, index, data, size, 1000);
2721da177e4SLinus Torvalds 	if (status < 0)
2731da177e4SLinus Torvalds 		return status;
2741da177e4SLinus Torvalds 	if (status != size) {
2751da177e4SLinus Torvalds 		dbg("%s - wanted to write %d, but only wrote %d",
276441b62c1SHarvey Harrison 					     __func__, size, status);
2771da177e4SLinus Torvalds 		return -ECOMM;
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 	return 0;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2822742fd88SAlan Cox static int ti_vsend_sync(struct usb_device *dev, __u8 request,
2832742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	int status;
2861da177e4SLinus Torvalds 
2872742fd88SAlan Cox 	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
2882742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
2892742fd88SAlan Cox 			value, index, data, size, 1000);
2901da177e4SLinus Torvalds 	if (status < 0)
2911da177e4SLinus Torvalds 		return status;
2921da177e4SLinus Torvalds 	if (status != size) {
2931da177e4SLinus Torvalds 		dbg("%s - wanted to write %d, but only wrote %d",
294441b62c1SHarvey Harrison 		     __func__, size, status);
2951da177e4SLinus Torvalds 		return -ECOMM;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	return 0;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3002742fd88SAlan Cox static int send_cmd(struct usb_device *dev, __u8 command,
3011da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
3021da177e4SLinus Torvalds 				int size)
3031da177e4SLinus Torvalds {
3042742fd88SAlan Cox 	return ti_vsend_sync(dev, command, value, moduleid, data, size);
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
3082742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	int port_number = port->number - port->serial->minor;
3111da177e4SLinus Torvalds 
312441b62c1SHarvey Harrison 	dbg("%s - port %d, mask %x", __func__, port_number, mask);
3131da177e4SLinus Torvalds 
3142742fd88SAlan Cox 	return send_cmd(port->serial->dev,
3151da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
3161da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
3171da177e4SLinus Torvalds 					mask,
3181da177e4SLinus Torvalds 					NULL,
3191da177e4SLinus Torvalds 					0);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /**
3232742fd88SAlan Cox  * read_download_mem - Read edgeport memory from TI chip
3241da177e4SLinus Torvalds  * @dev: usb device pointer
3251da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3261da177e4SLinus Torvalds  * @length: Length of above data
3271da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3281da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3291da177e4SLinus Torvalds  */
3302742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address,
3311da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	int status = 0;
3341da177e4SLinus Torvalds 	__u8 read_length;
3351da177e4SLinus Torvalds 	__be16 be_start_address;
3361da177e4SLinus Torvalds 
337441b62c1SHarvey Harrison 	dbg("%s - @ %x for %d", __func__, start_address, length);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
3401da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3411da177e4SLinus Torvalds 	 */
3421da177e4SLinus Torvalds 	while (length) {
3431da177e4SLinus Torvalds 		if (length > 64)
3441da177e4SLinus Torvalds 			read_length = 64;
3451da177e4SLinus Torvalds 		else
3461da177e4SLinus Torvalds 			read_length = (__u8)length;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		if (read_length > 1) {
349441b62c1SHarvey Harrison 			dbg("%s - @ %x for %d", __func__,
3501da177e4SLinus Torvalds 			     start_address, read_length);
3511da177e4SLinus Torvalds 		}
3521da177e4SLinus Torvalds 		be_start_address = cpu_to_be16(start_address);
3532742fd88SAlan Cox 		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
3542742fd88SAlan Cox 					(__u16)address_type,
3552742fd88SAlan Cox 					(__force __u16)be_start_address,
3562742fd88SAlan Cox 					buffer, read_length);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 		if (status) {
359441b62c1SHarvey Harrison 			dbg("%s - ERROR %x", __func__, status);
3601da177e4SLinus Torvalds 			return status;
3611da177e4SLinus Torvalds 		}
3621da177e4SLinus Torvalds 
3632742fd88SAlan Cox 		if (read_length > 1)
364441b62c1SHarvey Harrison 			usb_serial_debug_data(debug, &dev->dev, __func__,
3651da177e4SLinus Torvalds 					      read_length, buffer);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		/* Update pointers/length */
3681da177e4SLinus Torvalds 		start_address += read_length;
3691da177e4SLinus Torvalds 		buffer += read_length;
3701da177e4SLinus Torvalds 		length -= read_length;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	return status;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3762742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address,
3772742fd88SAlan Cox 						int length, __u8 *buffer)
3781da177e4SLinus Torvalds {
3792742fd88SAlan Cox 	return read_download_mem(dev, start_address, length,
3802742fd88SAlan Cox 					DTK_ADDR_SPACE_XDATA, buffer);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* Read edgeport memory to a given block */
3842742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial,
3852742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	int status = 0;
3881da177e4SLinus Torvalds 	int i;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	for (i = 0; i < length; i++) {
3912742fd88SAlan Cox 		status = ti_vread_sync(serial->serial->dev,
3922742fd88SAlan Cox 				UMPC_MEMORY_READ, serial->TI_I2C_Type,
3932742fd88SAlan Cox 				(__u16)(start_address+i), &buffer[i], 0x01);
3941da177e4SLinus Torvalds 		if (status) {
395441b62c1SHarvey Harrison 			dbg("%s - ERROR %x", __func__, status);
3961da177e4SLinus Torvalds 			return status;
3971da177e4SLinus Torvalds 		}
3981da177e4SLinus Torvalds 	}
3991da177e4SLinus Torvalds 
4002742fd88SAlan Cox 	dbg("%s - start_address = %x, length = %d",
4012742fd88SAlan Cox 					__func__, start_address, length);
4022742fd88SAlan Cox 	usb_serial_debug_data(debug, &serial->serial->dev->dev,
4032742fd88SAlan Cox 					__func__, length, buffer);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	return status;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
4112742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial,
4122742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	int status = 0;
4151da177e4SLinus Torvalds 	int i;
416e9305d2fSJohan Hovold 	u8 *temp;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/* Must do a read before write */
4191da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
420e9305d2fSJohan Hovold 		temp = kmalloc(1, GFP_KERNEL);
421e9305d2fSJohan Hovold 		if (!temp) {
422e9305d2fSJohan Hovold 			dev_err(&serial->serial->dev->dev,
423e9305d2fSJohan Hovold 					"%s - out of memory\n", __func__);
424e9305d2fSJohan Hovold 			return -ENOMEM;
425e9305d2fSJohan Hovold 		}
426e9305d2fSJohan Hovold 		status = read_boot_mem(serial, 0, 1, temp);
427e9305d2fSJohan Hovold 		kfree(temp);
4281da177e4SLinus Torvalds 		if (status)
4291da177e4SLinus Torvalds 			return status;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	for (i = 0; i < length; ++i) {
4332742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev,
4342742fd88SAlan Cox 				UMPC_MEMORY_WRITE, buffer[i],
4352742fd88SAlan Cox 				(__u16)(i + start_address), NULL, 0);
4361da177e4SLinus Torvalds 		if (status)
4371da177e4SLinus Torvalds 			return status;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4402742fd88SAlan Cox 	dbg("%s - start_sddr = %x, length = %d",
4412742fd88SAlan Cox 					__func__, start_address, length);
4422742fd88SAlan Cox 	usb_serial_debug_data(debug, &serial->serial->dev->dev,
4432742fd88SAlan Cox 					__func__, length, buffer);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	return status;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4502742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial,
4512742fd88SAlan Cox 		int start_address, int length, __u8 address_type, __u8 *buffer)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	int status = 0;
4541da177e4SLinus Torvalds 	int write_length;
4551da177e4SLinus Torvalds 	__be16 be_start_address;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/* calulate the number of bytes left in the first page */
4602742fd88SAlan Cox 	write_length = EPROM_PAGE_SIZE -
4612742fd88SAlan Cox 				(start_address & (EPROM_PAGE_SIZE - 1));
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if (write_length > length)
4641da177e4SLinus Torvalds 		write_length = length;
4651da177e4SLinus Torvalds 
4662742fd88SAlan Cox 	dbg("%s - BytesInFirstPage Addr = %x, length = %d",
4672742fd88SAlan Cox 					__func__, start_address, write_length);
4682742fd88SAlan Cox 	usb_serial_debug_data(debug, &serial->serial->dev->dev,
4692742fd88SAlan Cox 						__func__, write_length, buffer);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/* Write first page */
4721da177e4SLinus Torvalds 	be_start_address = cpu_to_be16(start_address);
4732742fd88SAlan Cox 	status = ti_vsend_sync(serial->serial->dev,
4742742fd88SAlan Cox 				UMPC_MEMORY_WRITE, (__u16)address_type,
4752742fd88SAlan Cox 				(__force __u16)be_start_address,
4762742fd88SAlan Cox 				buffer,	write_length);
4771da177e4SLinus Torvalds 	if (status) {
478441b62c1SHarvey Harrison 		dbg("%s - ERROR %d", __func__, status);
4791da177e4SLinus Torvalds 		return status;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	length		-= write_length;
4831da177e4SLinus Torvalds 	start_address	+= write_length;
4841da177e4SLinus Torvalds 	buffer		+= write_length;
4851da177e4SLinus Torvalds 
4862742fd88SAlan Cox 	/* We should be aligned now -- can write
4872742fd88SAlan Cox 	   max page size bytes at a time */
4881da177e4SLinus Torvalds 	while (length) {
4891da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
4901da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
4911da177e4SLinus Torvalds 		else
4921da177e4SLinus Torvalds 			write_length = length;
4931da177e4SLinus Torvalds 
4942742fd88SAlan Cox 		dbg("%s - Page Write Addr = %x, length = %d",
4952742fd88SAlan Cox 					__func__, start_address, write_length);
4962742fd88SAlan Cox 		usb_serial_debug_data(debug, &serial->serial->dev->dev,
4972742fd88SAlan Cox 					__func__, write_length, buffer);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 		/* Write next page */
5001da177e4SLinus Torvalds 		be_start_address = cpu_to_be16(start_address);
5012742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
5022742fd88SAlan Cox 				(__u16)address_type,
5032742fd88SAlan Cox 				(__force __u16)be_start_address,
5042742fd88SAlan Cox 				buffer, write_length);
5051da177e4SLinus Torvalds 		if (status) {
5062742fd88SAlan Cox 			dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
5072742fd88SAlan Cox 					__func__, status);
5081da177e4SLinus Torvalds 			return status;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		length		-= write_length;
5121da177e4SLinus Torvalds 		start_address	+= write_length;
5131da177e4SLinus Torvalds 		buffer		+= write_length;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 	return status;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
5191da177e4SLinus Torvalds  *
5201da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5211da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5221da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5231da177e4SLinus Torvalds  */
5242742fd88SAlan Cox static int tx_active(struct edgeport_port *port)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	int status;
5271da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
5281da177e4SLinus Torvalds 	__u8 *lsr;
5291da177e4SLinus Torvalds 	int bytes_left = 0;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
5321da177e4SLinus Torvalds 	if (!oedb) {
533441b62c1SHarvey Harrison 		dev_err(&port->port->dev, "%s - out of memory\n", __func__);
5341da177e4SLinus Torvalds 		return -ENOMEM;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
5381da177e4SLinus Torvalds 					   as not all platforms can do DMA
5391da177e4SLinus Torvalds 					   from stack */
5401da177e4SLinus Torvalds 	if (!lsr) {
5411da177e4SLinus Torvalds 		kfree(oedb);
5421da177e4SLinus Torvalds 		return -ENOMEM;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5452742fd88SAlan Cox 	status = read_ram(port->port->serial->dev, port->dma_address,
5462742fd88SAlan Cox 						sizeof(*oedb), (void *)oedb);
5471da177e4SLinus Torvalds 	if (status)
5481da177e4SLinus Torvalds 		goto exit_is_tx_active;
5491da177e4SLinus Torvalds 
550441b62c1SHarvey Harrison 	dbg("%s - XByteCount    0x%X", __func__, oedb->XByteCount);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* and the LSR */
5532742fd88SAlan Cox 	status = read_ram(port->port->serial->dev,
5542742fd88SAlan Cox 			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (status)
5571da177e4SLinus Torvalds 		goto exit_is_tx_active;
558441b62c1SHarvey Harrison 	dbg("%s - LSR = 0x%X", __func__, *lsr);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5611da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80) != 0)
5621da177e4SLinus Torvalds 		bytes_left += 64;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
5651da177e4SLinus Torvalds 		bytes_left += 1;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5681da177e4SLinus Torvalds exit_is_tx_active:
569441b62c1SHarvey Harrison 	dbg("%s - return %d", __func__, bytes_left);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	kfree(lsr);
5721da177e4SLinus Torvalds 	kfree(oedb);
5731da177e4SLinus Torvalds 	return bytes_left;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5762742fd88SAlan Cox static void chase_port(struct edgeport_port *port, unsigned long timeout,
5772742fd88SAlan Cox 								int flush)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	int baud_rate;
5804a90f09bSAlan Cox 	struct tty_struct *tty = tty_port_tty_get(&port->port->port);
5811da177e4SLinus Torvalds 	wait_queue_t wait;
5821da177e4SLinus Torvalds 	unsigned long flags;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	if (!timeout)
5851da177e4SLinus Torvalds 		timeout = (HZ * EDGE_CLOSING_WAIT)/100;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* wait for data to drain from the buffer */
5881da177e4SLinus Torvalds 	spin_lock_irqsave(&port->ep_lock, flags);
5891da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
5901da177e4SLinus Torvalds 	add_wait_queue(&tty->write_wait, &wait);
5911da177e4SLinus Torvalds 	for (;;) {
5921da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
5931da177e4SLinus Torvalds 		if (edge_buf_data_avail(port->ep_out_buf) == 0
5941da177e4SLinus Torvalds 		|| timeout == 0 || signal_pending(current)
5952742fd88SAlan Cox 		|| !usb_get_intfdata(port->port->serial->interface))
5962742fd88SAlan Cox 			/* disconnect */
5971da177e4SLinus Torvalds 			break;
5981da177e4SLinus Torvalds 		spin_unlock_irqrestore(&port->ep_lock, flags);
5991da177e4SLinus Torvalds 		timeout = schedule_timeout(timeout);
6001da177e4SLinus Torvalds 		spin_lock_irqsave(&port->ep_lock, flags);
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
6031da177e4SLinus Torvalds 	remove_wait_queue(&tty->write_wait, &wait);
6041da177e4SLinus Torvalds 	if (flush)
6051da177e4SLinus Torvalds 		edge_buf_clear(port->ep_out_buf);
6061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&port->ep_lock, flags);
6074a90f09bSAlan Cox 	tty_kref_put(tty);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/* wait for data to drain from the device */
6101da177e4SLinus Torvalds 	timeout += jiffies;
6111da177e4SLinus Torvalds 	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
6122742fd88SAlan Cox 	&& usb_get_intfdata(port->port->serial->interface)) {
6132742fd88SAlan Cox 		/* not disconnected */
6142742fd88SAlan Cox 		if (!tx_active(port))
6151da177e4SLinus Torvalds 			break;
6161da177e4SLinus Torvalds 		msleep(10);
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/* disconnected */
6201da177e4SLinus Torvalds 	if (!usb_get_intfdata(port->port->serial->interface))
6211da177e4SLinus Torvalds 		return;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* wait one more character time, based on baud rate */
6242742fd88SAlan Cox 	/* (tx_active doesn't seem to wait for the last byte) */
6252742fd88SAlan Cox 	baud_rate = port->baud_rate;
6262742fd88SAlan Cox 	if (baud_rate == 0)
6271da177e4SLinus Torvalds 		baud_rate = 50;
628dfa5ec79SJulia Lawall 	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6312742fd88SAlan Cox static int choose_config(struct usb_device *dev)
6321da177e4SLinus Torvalds {
6332742fd88SAlan Cox 	/*
6342742fd88SAlan Cox 	 * There may be multiple configurations on this device, in which case
6352742fd88SAlan Cox 	 * we would need to read and parse all of them to find out which one
6362742fd88SAlan Cox 	 * we want. However, we just support one config at this point,
6372742fd88SAlan Cox 	 * configuration # 1, which is Config Descriptor 0.
6382742fd88SAlan Cox 	 */
6391da177e4SLinus Torvalds 
6402742fd88SAlan Cox 	dbg("%s - Number of Interfaces = %d",
6412742fd88SAlan Cox 				__func__, dev->config->desc.bNumInterfaces);
6422742fd88SAlan Cox 	dbg("%s - MAX Power            = %d",
6432742fd88SAlan Cox 				__func__, dev->config->desc.bMaxPower * 2);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
6462742fd88SAlan Cox 		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
6472742fd88SAlan Cox 								__func__);
6481da177e4SLinus Torvalds 		return -ENODEV;
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	return 0;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6542742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial,
6552742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	int status;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6602742fd88SAlan Cox 		status = read_download_mem(serial->serial->dev,
6611da177e4SLinus Torvalds 					       start_address,
6621da177e4SLinus Torvalds 					       length,
6631da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6641da177e4SLinus Torvalds 					       buffer);
6651da177e4SLinus Torvalds 	} else {
6662742fd88SAlan Cox 		status = read_boot_mem(serial, start_address, length,
6671da177e4SLinus Torvalds 								buffer);
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	return status;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
6722742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address,
6732742fd88SAlan Cox 						int length, __u8 *buffer)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6762742fd88SAlan Cox 		return write_boot_mem(serial, start_address, length,
6771da177e4SLinus Torvalds 								buffer);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
6802742fd88SAlan Cox 		return write_i2c_mem(serial, start_address, length,
6812742fd88SAlan Cox 						serial->TI_I2C_Type, buffer);
6821da177e4SLinus Torvalds 	return -EINVAL;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
6882742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial,
6892742fd88SAlan Cox 				int desc_type, struct ti_i2c_desc *rom_desc)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	int start_address;
6921da177e4SLinus Torvalds 	int status;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
6951da177e4SLinus Torvalds 	start_address = 2;
6961da177e4SLinus Torvalds 	do {
6972742fd88SAlan Cox 		status = read_rom(serial,
6981da177e4SLinus Torvalds 				   start_address,
6991da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
7001da177e4SLinus Torvalds 				   (__u8 *)rom_desc);
7011da177e4SLinus Torvalds 		if (status)
7021da177e4SLinus Torvalds 			return 0;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
7051da177e4SLinus Torvalds 			return start_address;
7061da177e4SLinus Torvalds 
7072742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc)
7082742fd88SAlan Cox 							+ rom_desc->Size;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	return 0;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /* Validate descriptor checksum */
7162742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	__u16 i;
7191da177e4SLinus Torvalds 	__u8 cs = 0;
7201da177e4SLinus Torvalds 
7212742fd88SAlan Cox 	for (i = 0; i < rom_desc->Size; i++)
7221da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
7232742fd88SAlan Cox 
7241da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
725441b62c1SHarvey Harrison 		dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
7261da177e4SLinus Torvalds 		return -EINVAL;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 	return 0;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /* Make sure that the I2C image is good */
7322742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
7351da177e4SLinus Torvalds 	int status = 0;
7361da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7371da177e4SLinus Torvalds 	int start_address = 2;
7381da177e4SLinus Torvalds 	__u8 *buffer;
7391da177e4SLinus Torvalds 	__u16 ttype;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
7421da177e4SLinus Torvalds 	if (!rom_desc) {
743441b62c1SHarvey Harrison 		dev_err(dev, "%s - out of memory\n", __func__);
7441da177e4SLinus Torvalds 		return -ENOMEM;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
7471da177e4SLinus Torvalds 	if (!buffer) {
7482742fd88SAlan Cox 		dev_err(dev, "%s - out of memory when allocating buffer\n",
7492742fd88SAlan Cox 								__func__);
7501da177e4SLinus Torvalds 		kfree(rom_desc);
7511da177e4SLinus Torvalds 		return -ENOMEM;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7542742fd88SAlan Cox 	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
7552742fd88SAlan Cox 	status = read_rom(serial, 0, 1, buffer);
7561da177e4SLinus Torvalds 	if (status)
7572742fd88SAlan Cox 		goto out;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
760441b62c1SHarvey Harrison 		dev_err(dev, "%s - invalid buffer signature\n", __func__);
7611da177e4SLinus Torvalds 		status = -ENODEV;
7622742fd88SAlan Cox 		goto out;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	do {
7662742fd88SAlan Cox 		/* Validate the I2C */
7672742fd88SAlan Cox 		status = read_rom(serial,
7681da177e4SLinus Torvalds 				start_address,
7691da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7701da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7711da177e4SLinus Torvalds 		if (status)
7721da177e4SLinus Torvalds 			break;
7731da177e4SLinus Torvalds 
7742742fd88SAlan Cox 		if ((start_address + sizeof(struct ti_i2c_desc) +
7752742fd88SAlan Cox 					rom_desc->Size) > TI_MAX_I2C_SIZE) {
7761da177e4SLinus Torvalds 			status = -ENODEV;
777441b62c1SHarvey Harrison 			dbg("%s - structure too big, erroring out.", __func__);
7781da177e4SLinus Torvalds 			break;
7791da177e4SLinus Torvalds 		}
7801da177e4SLinus Torvalds 
781441b62c1SHarvey Harrison 		dbg("%s Type = 0x%x", __func__, rom_desc->Type);
7821da177e4SLinus Torvalds 
7832742fd88SAlan Cox 		/* Skip type 2 record */
7841da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
7851da177e4SLinus Torvalds 		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
7861da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
7872742fd88SAlan Cox 			/* Read the descriptor data */
7882742fd88SAlan Cox 			status = read_rom(serial, start_address +
7892742fd88SAlan Cox 						sizeof(struct ti_i2c_desc),
7902742fd88SAlan Cox 						rom_desc->Size, buffer);
7911da177e4SLinus Torvalds 			if (status)
7921da177e4SLinus Torvalds 				break;
7931da177e4SLinus Torvalds 
7942742fd88SAlan Cox 			status = valid_csum(rom_desc, buffer);
7951da177e4SLinus Torvalds 			if (status)
7961da177e4SLinus Torvalds 				break;
7971da177e4SLinus Torvalds 		}
7982742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc) +
7992742fd88SAlan Cox 								rom_desc->Size;
8001da177e4SLinus Torvalds 
8012742fd88SAlan Cox 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
8022742fd88SAlan Cox 				(start_address < TI_MAX_I2C_SIZE));
8031da177e4SLinus Torvalds 
8042742fd88SAlan Cox 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
8052742fd88SAlan Cox 				(start_address > TI_MAX_I2C_SIZE))
8061da177e4SLinus Torvalds 		status = -ENODEV;
8071da177e4SLinus Torvalds 
8082742fd88SAlan Cox out:
8091da177e4SLinus Torvalds 	kfree(buffer);
8101da177e4SLinus Torvalds 	kfree(rom_desc);
8111da177e4SLinus Torvalds 	return status;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8142742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	int status;
8171da177e4SLinus Torvalds 	int start_address;
8181da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
8191da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
8221da177e4SLinus Torvalds 	if (!rom_desc) {
8232742fd88SAlan Cox 		dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
8242742fd88SAlan Cox 								__func__);
8251da177e4SLinus Torvalds 		return -ENOMEM;
8261da177e4SLinus Torvalds 	}
8272742fd88SAlan Cox 	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
8282742fd88SAlan Cox 								rom_desc);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	if (!start_address) {
831441b62c1SHarvey Harrison 		dbg("%s - Edge Descriptor not found in I2C", __func__);
8321da177e4SLinus Torvalds 		status = -ENODEV;
8331da177e4SLinus Torvalds 		goto exit;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8362742fd88SAlan Cox 	/* Read the descriptor data */
8372742fd88SAlan Cox 	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
8382742fd88SAlan Cox 						rom_desc->Size, buffer);
8391da177e4SLinus Torvalds 	if (status)
8401da177e4SLinus Torvalds 		goto exit;
8411da177e4SLinus Torvalds 
8422742fd88SAlan Cox 	status = valid_csum(rom_desc, buffer);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
845441b62c1SHarvey Harrison 	dbg("%s - IonConfig      0x%x", __func__, desc->IonConfig);
846441b62c1SHarvey Harrison 	dbg("%s - Version          %d", __func__, desc->Version);
847441b62c1SHarvey Harrison 	dbg("%s - Cpu/Board      0x%x", __func__, desc->CpuRev_BoardRev);
848441b62c1SHarvey Harrison 	dbg("%s - NumPorts         %d", __func__, desc->NumPorts);
849441b62c1SHarvey Harrison 	dbg("%s - NumVirtualPorts  %d", __func__, desc->NumVirtualPorts);
850441b62c1SHarvey Harrison 	dbg("%s - TotalPorts       %d", __func__, desc->TotalPorts);
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds exit:
8531da177e4SLinus Torvalds 	kfree(rom_desc);
8541da177e4SLinus Torvalds 	return status;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds /* Build firmware header used for firmware update */
8582742fd88SAlan Cox static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	__u8 *buffer;
8611da177e4SLinus Torvalds 	int buffer_size;
8621da177e4SLinus Torvalds 	int i;
863d12b219aSJaswinder Singh 	int err;
8641da177e4SLinus Torvalds 	__u8 cs = 0;
8651da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8661da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8671da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
868d12b219aSJaswinder Singh 	const struct firmware *fw;
869d12b219aSJaswinder Singh 	const char *fw_name = "edgeport/down3.bin";
8701da177e4SLinus Torvalds 
8712742fd88SAlan Cox 	/* In order to update the I2C firmware we must change the type 2 record
8722742fd88SAlan Cox 	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
8732742fd88SAlan Cox 	 * Then while in boot mode, the driver will download the latest
8742742fd88SAlan Cox 	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
8752742fd88SAlan Cox 	 * device comes back up in download mode the driver will cause the new
8762742fd88SAlan Cox 	 * firmware to be copied from the UMP Ram to I2C and the firmware will
8772742fd88SAlan Cox 	 * update the record type from 0xf2 to 0x02.
8782742fd88SAlan Cox 	 */
8791da177e4SLinus Torvalds 
8802742fd88SAlan Cox 	/* Allocate a 15.5k buffer + 2 bytes for version number
8812742fd88SAlan Cox 	 * (Firmware Record) */
8822742fd88SAlan Cox 	buffer_size = (((1024 * 16) - 512 ) +
8832742fd88SAlan Cox 			sizeof(struct ti_i2c_firmware_rec));
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	buffer = kmalloc(buffer_size, GFP_KERNEL);
8861da177e4SLinus Torvalds 	if (!buffer) {
887441b62c1SHarvey Harrison 		dev_err(dev, "%s - out of memory\n", __func__);
8881da177e4SLinus Torvalds 		return -ENOMEM;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	// Set entire image of 0xffs
8921da177e4SLinus Torvalds 	memset(buffer, 0xff, buffer_size);
8931da177e4SLinus Torvalds 
894d12b219aSJaswinder Singh 	err = request_firmware(&fw, fw_name, dev);
895d12b219aSJaswinder Singh 	if (err) {
896d12b219aSJaswinder Singh 		printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
897d12b219aSJaswinder Singh 		       fw_name, err);
898d12b219aSJaswinder Singh 		kfree(buffer);
899d12b219aSJaswinder Singh 		return err;
900d12b219aSJaswinder Singh 	}
901d12b219aSJaswinder Singh 
902d12b219aSJaswinder Singh 	/* Save Download Version Number */
903d12b219aSJaswinder Singh 	OperationalMajorVersion = fw->data[0];
904d12b219aSJaswinder Singh 	OperationalMinorVersion = fw->data[1];
905d12b219aSJaswinder Singh 	OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
906d12b219aSJaswinder Singh 
9072742fd88SAlan Cox 	/* Copy version number into firmware record */
9081da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
9091da177e4SLinus Torvalds 
910d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
911d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
9121da177e4SLinus Torvalds 
9132742fd88SAlan Cox 	/* Pointer to fw_down memory image */
914d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
917d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
9181da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
9191da177e4SLinus Torvalds 
920d12b219aSJaswinder Singh 	release_firmware(fw);
921d12b219aSJaswinder Singh 
9221da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
9231da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	kfree(buffer);
9271da177e4SLinus Torvalds 
9282742fd88SAlan Cox 	/* Build new header */
9291da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
9301da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
9331da177e4SLinus Torvalds 	i2c_header->Size	= (__u16)buffer_size;
9341da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
935d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
936d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	return 0;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
9422742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	int status;
945e9305d2fSJohan Hovold 	u8 *data;
946e9305d2fSJohan Hovold 
947e9305d2fSJohan Hovold 	data = kmalloc(1, GFP_KERNEL);
948e9305d2fSJohan Hovold 	if (!data) {
949e9305d2fSJohan Hovold 		dev_err(&serial->serial->dev->dev,
950e9305d2fSJohan Hovold 				"%s - out of memory\n", __func__);
951e9305d2fSJohan Hovold 		return -ENOMEM;
952e9305d2fSJohan Hovold 	}
9531da177e4SLinus Torvalds 
9542742fd88SAlan Cox 	/* Try to read type 2 */
9552742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
956e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
9571da177e4SLinus Torvalds 	if (status)
958441b62c1SHarvey Harrison 		dbg("%s - read 2 status error = %d", __func__, status);
9591da177e4SLinus Torvalds 	else
960e9305d2fSJohan Hovold 		dbg("%s - read 2 data = 0x%x", __func__, *data);
961e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
962441b62c1SHarvey Harrison 		dbg("%s - ROM_TYPE_II", __func__);
9631da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
964e9305d2fSJohan Hovold 		goto out;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 
9672742fd88SAlan Cox 	/* Try to read type 3 */
9682742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
969e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
9701da177e4SLinus Torvalds 	if (status)
971441b62c1SHarvey Harrison 		dbg("%s - read 3 status error = %d", __func__, status);
9721da177e4SLinus Torvalds 	else
973e9305d2fSJohan Hovold 		dbg("%s - read 2 data = 0x%x", __func__, *data);
974e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
975441b62c1SHarvey Harrison 		dbg("%s - ROM_TYPE_III", __func__);
9761da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
977e9305d2fSJohan Hovold 		goto out;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
980441b62c1SHarvey Harrison 	dbg("%s - Unknown", __func__);
9811da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
982e9305d2fSJohan Hovold 	status = -ENODEV;
983e9305d2fSJohan Hovold out:
984e9305d2fSJohan Hovold 	kfree(data);
985e9305d2fSJohan Hovold 	return status;
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9882742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer,
9892742fd88SAlan Cox 						int length, int *num_sent)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	int status;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	status = usb_bulk_msg(serial->dev,
9941da177e4SLinus Torvalds 			usb_sndbulkpipe(serial->dev,
9951da177e4SLinus Torvalds 				serial->port[0]->bulk_out_endpointAddress),
9962742fd88SAlan Cox 			buffer, length, num_sent, 1000);
9971da177e4SLinus Torvalds 	return status;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
10012742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image,
10022742fd88SAlan Cox 							int image_length)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds 	int status = 0;
10051da177e4SLinus Torvalds 	int pos;
10061da177e4SLinus Torvalds 	int transfer;
10071da177e4SLinus Torvalds 	int done;
10081da177e4SLinus Torvalds 
10092742fd88SAlan Cox 	/* Transfer firmware image */
10101da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
10112742fd88SAlan Cox 		/* Read the next buffer from file */
10121da177e4SLinus Torvalds 		transfer = image_length - pos;
10131da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
10141da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
10151da177e4SLinus Torvalds 
10162742fd88SAlan Cox 		/* Transfer data */
10172742fd88SAlan Cox 		status = bulk_xfer(serial->serial, &image[pos],
10182742fd88SAlan Cox 							transfer, &done);
10191da177e4SLinus Torvalds 		if (status)
10201da177e4SLinus Torvalds 			break;
10212742fd88SAlan Cox 		/* Advance buffer pointer */
10221da177e4SLinus Torvalds 		pos += done;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	return status;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10282742fd88SAlan Cox /* FIXME!!! */
10292742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	return 0;
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
10342742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
10352742fd88SAlan Cox {
10362742fd88SAlan Cox 	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
10372742fd88SAlan Cox }
10382742fd88SAlan Cox 
10391da177e4SLinus Torvalds /**
10401da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
10411da177e4SLinus Torvalds  *
10421da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
10431da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
10441da177e4SLinus Torvalds  */
10452742fd88SAlan Cox static int download_fw(struct edgeport_serial *serial)
10461da177e4SLinus Torvalds {
10471da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
10481da177e4SLinus Torvalds 	int status = 0;
10491da177e4SLinus Torvalds 	int start_address;
10501da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
10511da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
10521da177e4SLinus Torvalds 	int download_cur_ver;
10531da177e4SLinus Torvalds 	int download_new_ver;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
10561da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10571da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10581da177e4SLinus Torvalds 	 */
10591da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10621da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10631da177e4SLinus Torvalds 
10642742fd88SAlan Cox 	status = choose_config(serial->serial->dev);
10651da177e4SLinus Torvalds 	if (status)
10661da177e4SLinus Torvalds 		return status;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10691da177e4SLinus Torvalds 	if (!interface) {
1070441b62c1SHarvey Harrison 		dev_err(dev, "%s - no interface set, error!\n", __func__);
10711da177e4SLinus Torvalds 		return -ENODEV;
10721da177e4SLinus Torvalds 	}
10731da177e4SLinus Torvalds 
10742742fd88SAlan Cox 	/*
10752742fd88SAlan Cox 	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10762742fd88SAlan Cox 	 * if we have more than one endpoint we are definitely in download
10772742fd88SAlan Cox 	 * mode
10782742fd88SAlan Cox 	 */
10791da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
10801da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
10811da177e4SLinus Torvalds 	else
10822742fd88SAlan Cox 		/* Otherwise we will remain in configuring mode */
10831da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/********************************************************************/
10861da177e4SLinus Torvalds 	/* Download Mode */
10871da177e4SLinus Torvalds 	/********************************************************************/
10881da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
10891da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
10901da177e4SLinus Torvalds 
10919544e833SAndrew Morton 		dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
10921da177e4SLinus Torvalds 
10932742fd88SAlan Cox 		status = check_i2c_image(serial);
10941da177e4SLinus Torvalds 		if (status) {
10959544e833SAndrew Morton 			dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
10961da177e4SLinus Torvalds 			return status;
10971da177e4SLinus Torvalds 		}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		/* Validate Hardware version number
11001da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
11011da177e4SLinus Torvalds 		 */
11021da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
11031da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1104441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
11051da177e4SLinus Torvalds 			return -ENOMEM;
11061da177e4SLinus Torvalds 		}
11072742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
11081da177e4SLinus Torvalds 		if (status) {
11091da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11101da177e4SLinus Torvalds 			return status;
11111da177e4SLinus Torvalds 		}
11121da177e4SLinus Torvalds 
11132742fd88SAlan Cox 		/* Check version number of ION descriptor */
11142742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
11152742fd88SAlan Cox 			dbg("%s - Wrong CPU Rev %d (Must be 2)",
11162742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
11171da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11181da177e4SLinus Torvalds 			return -EINVAL;
11191da177e4SLinus Torvalds   		}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
11221da177e4SLinus Torvalds 		if (!rom_desc) {
1123441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
11241da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
11251da177e4SLinus Torvalds 			return -ENOMEM;
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 
11282742fd88SAlan Cox 		/* Search for type 2 record (firmware record) */
11292742fd88SAlan Cox 		start_address = get_descriptor_addr(serial,
11302742fd88SAlan Cox 				I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
11312742fd88SAlan Cox 		if (start_address != 0) {
11321da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
1133e9305d2fSJohan Hovold 			u8 *record;
11341da177e4SLinus Torvalds 
11352742fd88SAlan Cox 			dbg("%s - Found Type FIRMWARE (Type 2) record",
11362742fd88SAlan Cox 								__func__);
11371da177e4SLinus Torvalds 
11382742fd88SAlan Cox 			firmware_version = kmalloc(sizeof(*firmware_version),
11392742fd88SAlan Cox 								GFP_KERNEL);
11401da177e4SLinus Torvalds 			if (!firmware_version) {
1141441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
11421da177e4SLinus Torvalds 				kfree(rom_desc);
11431da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11441da177e4SLinus Torvalds 				return -ENOMEM;
11451da177e4SLinus Torvalds 			}
11461da177e4SLinus Torvalds 
11472742fd88SAlan Cox 			/* Validate version number
11482742fd88SAlan Cox 			 * Read the descriptor data
11492742fd88SAlan Cox 			 */
11502742fd88SAlan Cox 			status = read_rom(serial, start_address +
11512742fd88SAlan Cox 					sizeof(struct ti_i2c_desc),
11521da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
11531da177e4SLinus Torvalds 					(__u8 *)firmware_version);
11541da177e4SLinus Torvalds 			if (status) {
11551da177e4SLinus Torvalds 				kfree(firmware_version);
11561da177e4SLinus Torvalds 				kfree(rom_desc);
11571da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11581da177e4SLinus Torvalds 				return status;
11591da177e4SLinus Torvalds 			}
11601da177e4SLinus Torvalds 
11612742fd88SAlan Cox 			/* Check version number of download with current
11622742fd88SAlan Cox 			   version in I2c */
11631da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
11641da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
1165d12b219aSJaswinder Singh 			download_new_ver = (OperationalMajorVersion << 8) +
1166d12b219aSJaswinder Singh 					   (OperationalMinorVersion);
11671da177e4SLinus Torvalds 
11682742fd88SAlan Cox 			dbg("%s - >> FW Versions Device %d.%d  Driver %d.%d",
1169441b62c1SHarvey Harrison 			    __func__,
11701da177e4SLinus Torvalds 			    firmware_version->Ver_Major,
11711da177e4SLinus Torvalds 			    firmware_version->Ver_Minor,
1172d12b219aSJaswinder Singh 			    OperationalMajorVersion,
1173d12b219aSJaswinder Singh 			    OperationalMinorVersion);
11741da177e4SLinus Torvalds 
11752742fd88SAlan Cox 			/* Check if we have an old version in the I2C and
11762742fd88SAlan Cox 			   update if necessary */
11771da177e4SLinus Torvalds 			if (download_cur_ver != download_new_ver) {
11782742fd88SAlan Cox 				dbg("%s - Update I2C dld from %d.%d to %d.%d",
1179441b62c1SHarvey Harrison 				    __func__,
11801da177e4SLinus Torvalds 				    firmware_version->Ver_Major,
11811da177e4SLinus Torvalds 				    firmware_version->Ver_Minor,
1182d12b219aSJaswinder Singh 				    OperationalMajorVersion,
1183d12b219aSJaswinder Singh 				    OperationalMinorVersion);
11841da177e4SLinus Torvalds 
1185e9305d2fSJohan Hovold 				record = kmalloc(1, GFP_KERNEL);
1186e9305d2fSJohan Hovold 				if (!record) {
1187e9305d2fSJohan Hovold 					dev_err(dev, "%s - out of memory.\n",
1188e9305d2fSJohan Hovold 							__func__);
1189e9305d2fSJohan Hovold 					kfree(firmware_version);
1190e9305d2fSJohan Hovold 					kfree(rom_desc);
1191e9305d2fSJohan Hovold 					kfree(ti_manuf_desc);
1192e9305d2fSJohan Hovold 					return -ENOMEM;
1193e9305d2fSJohan Hovold 				}
11942742fd88SAlan Cox 				/* In order to update the I2C firmware we must
11952742fd88SAlan Cox 				 * change the type 2 record to type 0xF2. This
11962742fd88SAlan Cox 				 * will force the UMP to come up in Boot Mode.
11972742fd88SAlan Cox 				 * Then while in boot mode, the driver will
11982742fd88SAlan Cox 				 * download the latest firmware (padded to
11992742fd88SAlan Cox 				 * 15.5k) into the UMP ram. Finally when the
12002742fd88SAlan Cox 				 * device comes back up in download mode the
12012742fd88SAlan Cox 				 * driver will cause the new firmware to be
12022742fd88SAlan Cox 				 * copied from the UMP Ram to I2C and the
12032742fd88SAlan Cox 				 * firmware will update the record type from
12042742fd88SAlan Cox 				 * 0xf2 to 0x02.
12052742fd88SAlan Cox 				 */
1206e9305d2fSJohan Hovold 				*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
12071da177e4SLinus Torvalds 
12082742fd88SAlan Cox 				/* Change the I2C Firmware record type to
12092742fd88SAlan Cox 				   0xf2 to trigger an update */
12102742fd88SAlan Cox 				status = write_rom(serial, start_address,
1211e9305d2fSJohan Hovold 						sizeof(*record), record);
12121da177e4SLinus Torvalds 				if (status) {
1213e9305d2fSJohan Hovold 					kfree(record);
12141da177e4SLinus Torvalds 					kfree(firmware_version);
12151da177e4SLinus Torvalds 					kfree(rom_desc);
12161da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
12171da177e4SLinus Torvalds 					return status;
12181da177e4SLinus Torvalds 				}
12191da177e4SLinus Torvalds 
12202742fd88SAlan Cox 				/* verify the write -- must do this in order
12212742fd88SAlan Cox 				 * for write to complete before we do the
12222742fd88SAlan Cox 				 * hardware reset
12232742fd88SAlan Cox 				 */
12242742fd88SAlan Cox 				status = read_rom(serial,
12251da177e4SLinus Torvalds 							start_address,
1226e9305d2fSJohan Hovold 							sizeof(*record),
1227e9305d2fSJohan Hovold 							record);
12281da177e4SLinus Torvalds 				if (status) {
1229e9305d2fSJohan Hovold 					kfree(record);
12301da177e4SLinus Torvalds 					kfree(firmware_version);
12311da177e4SLinus Torvalds 					kfree(rom_desc);
12321da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
12331da177e4SLinus Torvalds 					return status;
12341da177e4SLinus Torvalds 				}
12351da177e4SLinus Torvalds 
1236e9305d2fSJohan Hovold 				if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
12372742fd88SAlan Cox 					dev_err(dev,
12382742fd88SAlan Cox 						"%s - error resetting device\n",
12392742fd88SAlan Cox 						__func__);
1240e9305d2fSJohan Hovold 					kfree(record);
12411da177e4SLinus Torvalds 					kfree(firmware_version);
12421da177e4SLinus Torvalds 					kfree(rom_desc);
12431da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
12441da177e4SLinus Torvalds 					return -ENODEV;
12451da177e4SLinus Torvalds 				}
12461da177e4SLinus Torvalds 
1247441b62c1SHarvey Harrison 				dbg("%s - HARDWARE RESET", __func__);
12481da177e4SLinus Torvalds 
12492742fd88SAlan Cox 				/* Reset UMP -- Back to BOOT MODE */
12502742fd88SAlan Cox 				status = ti_vsend_sync(serial->serial->dev,
12512742fd88SAlan Cox 						UMPC_HARDWARE_RESET,
12522742fd88SAlan Cox 						0, 0, NULL, 0);
12531da177e4SLinus Torvalds 
12542742fd88SAlan Cox 				dbg("%s - HARDWARE RESET return %d",
12552742fd88SAlan Cox 						__func__, status);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 				/* return an error on purpose. */
1258e9305d2fSJohan Hovold 				kfree(record);
12591da177e4SLinus Torvalds 				kfree(firmware_version);
12601da177e4SLinus Torvalds 				kfree(rom_desc);
12611da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12621da177e4SLinus Torvalds 				return -ENODEV;
12631da177e4SLinus Torvalds 			}
12641da177e4SLinus Torvalds 			kfree(firmware_version);
12651da177e4SLinus Torvalds 		}
12662742fd88SAlan Cox 		/* Search for type 0xF2 record (firmware blank record) */
12672742fd88SAlan Cox 		else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
12682742fd88SAlan Cox #define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
12692742fd88SAlan Cox 					sizeof(struct ti_i2c_firmware_rec))
12701da177e4SLinus Torvalds 			__u8 *header;
12711da177e4SLinus Torvalds 			__u8 *vheader;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
12741da177e4SLinus Torvalds 			if (!header) {
1275441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
12761da177e4SLinus Torvalds 				kfree(rom_desc);
12771da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12781da177e4SLinus Torvalds 				return -ENOMEM;
12791da177e4SLinus Torvalds 			}
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
12821da177e4SLinus Torvalds 			if (!vheader) {
1283441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
12841da177e4SLinus Torvalds 				kfree(header);
12851da177e4SLinus Torvalds 				kfree(rom_desc);
12861da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12871da177e4SLinus Torvalds 				return -ENOMEM;
12881da177e4SLinus Torvalds 			}
12891da177e4SLinus Torvalds 
12902742fd88SAlan Cox 			dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
12912742fd88SAlan Cox 								__func__);
12921da177e4SLinus Torvalds 
12932742fd88SAlan Cox 			/*
12942742fd88SAlan Cox 			 * In order to update the I2C firmware we must change
12952742fd88SAlan Cox 			 * the type 2 record to type 0xF2. This will force the
12962742fd88SAlan Cox 			 * UMP to come up in Boot Mode.  Then while in boot
12972742fd88SAlan Cox 			 * mode, the driver will download the latest firmware
12982742fd88SAlan Cox 			 * (padded to 15.5k) into the UMP ram. Finally when the
12992742fd88SAlan Cox 			 * device comes back up in download mode the driver
13002742fd88SAlan Cox 			 * will cause the new firmware to be copied from the
13012742fd88SAlan Cox 			 * UMP Ram to I2C and the firmware will update the
13022742fd88SAlan Cox 			 * record type from 0xf2 to 0x02.
13032742fd88SAlan Cox 			 */
13042742fd88SAlan Cox 			status = build_i2c_fw_hdr(header, dev);
13051da177e4SLinus Torvalds 			if (status) {
13061da177e4SLinus Torvalds 				kfree(vheader);
13071da177e4SLinus Torvalds 				kfree(header);
13081da177e4SLinus Torvalds 				kfree(rom_desc);
13091da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13101da177e4SLinus Torvalds 				return status;
13111da177e4SLinus Torvalds 			}
13121da177e4SLinus Torvalds 
13132742fd88SAlan Cox 			/* Update I2C with type 0xf2 record with correct
13142742fd88SAlan Cox 			   size and checksum */
13152742fd88SAlan Cox 			status = write_rom(serial,
13161da177e4SLinus Torvalds 						start_address,
13171da177e4SLinus Torvalds 						HEADER_SIZE,
13181da177e4SLinus Torvalds 						header);
13191da177e4SLinus Torvalds 			if (status) {
13201da177e4SLinus Torvalds 				kfree(vheader);
13211da177e4SLinus Torvalds 				kfree(header);
13221da177e4SLinus Torvalds 				kfree(rom_desc);
13231da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13241da177e4SLinus Torvalds 				return status;
13251da177e4SLinus Torvalds 			}
13261da177e4SLinus Torvalds 
13272742fd88SAlan Cox 			/* verify the write -- must do this in order for
13282742fd88SAlan Cox 			   write to complete before we do the hardware reset */
13292742fd88SAlan Cox 			status = read_rom(serial, start_address,
13302742fd88SAlan Cox 							HEADER_SIZE, vheader);
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 			if (status) {
1333441b62c1SHarvey Harrison 				dbg("%s - can't read header back", __func__);
13341da177e4SLinus Torvalds 				kfree(vheader);
13351da177e4SLinus Torvalds 				kfree(header);
13361da177e4SLinus Torvalds 				kfree(rom_desc);
13371da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13381da177e4SLinus Torvalds 				return status;
13391da177e4SLinus Torvalds 			}
13401da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
13412742fd88SAlan Cox 				dbg("%s - write download record failed",
13422742fd88SAlan Cox 					__func__);
13431da177e4SLinus Torvalds 				kfree(vheader);
13441da177e4SLinus Torvalds 				kfree(header);
13451da177e4SLinus Torvalds 				kfree(rom_desc);
13461da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13471da177e4SLinus Torvalds 				return status;
13481da177e4SLinus Torvalds 			}
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 			kfree(vheader);
13511da177e4SLinus Torvalds 			kfree(header);
13521da177e4SLinus Torvalds 
1353441b62c1SHarvey Harrison 			dbg("%s - Start firmware update", __func__);
13541da177e4SLinus Torvalds 
13552742fd88SAlan Cox 			/* Tell firmware to copy download image into I2C */
13562742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
13572742fd88SAlan Cox 					UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
13581da177e4SLinus Torvalds 
1359441b62c1SHarvey Harrison 		  	dbg("%s - Update complete 0x%x", __func__, status);
13601da177e4SLinus Torvalds 			if (status) {
13612742fd88SAlan Cox 				dev_err(dev,
13622742fd88SAlan Cox 					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
13632742fd88SAlan Cox 								__func__);
13641da177e4SLinus Torvalds 				kfree(rom_desc);
13651da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13661da177e4SLinus Torvalds 				return status;
13671da177e4SLinus Torvalds 			}
13681da177e4SLinus Torvalds 		}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		// The device is running the download code
13711da177e4SLinus Torvalds 		kfree(rom_desc);
13721da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
13731da177e4SLinus Torvalds 		return 0;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/********************************************************************/
13771da177e4SLinus Torvalds 	/* Boot Mode */
13781da177e4SLinus Torvalds 	/********************************************************************/
13799544e833SAndrew Morton 	dbg("%s - RUNNING IN BOOT MODE", __func__);
13801da177e4SLinus Torvalds 
13812742fd88SAlan Cox 	/* Configure the TI device so we can use the BULK pipes for download */
13822742fd88SAlan Cox 	status = config_boot_dev(serial->serial->dev);
13831da177e4SLinus Torvalds 	if (status)
13841da177e4SLinus Torvalds 		return status;
13851da177e4SLinus Torvalds 
13862742fd88SAlan Cox 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
13872742fd88SAlan Cox 							!= USB_VENDOR_ID_ION) {
1388441b62c1SHarvey Harrison 		dbg("%s - VID = 0x%x", __func__,
13891da177e4SLinus Torvalds 		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13901da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13912742fd88SAlan Cox 		goto stayinbootmode;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 
13942742fd88SAlan Cox 	/* We have an ION device (I2c Must be programmed)
13952742fd88SAlan Cox 	   Determine I2C image type */
13962742fd88SAlan Cox 	if (i2c_type_bootmode(serial))
13972742fd88SAlan Cox 		goto stayinbootmode;
13981da177e4SLinus Torvalds 
13992742fd88SAlan Cox 	/* Check for ION Vendor ID and that the I2C is valid */
14002742fd88SAlan Cox 	if (!check_i2c_image(serial)) {
14011da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
14021da177e4SLinus Torvalds 		int i;
14031da177e4SLinus Torvalds 		__u8 cs = 0;
14041da177e4SLinus Torvalds 		__u8 *buffer;
14051da177e4SLinus Torvalds 		int buffer_size;
1406d12b219aSJaswinder Singh 		int err;
1407d12b219aSJaswinder Singh 		const struct firmware *fw;
1408d12b219aSJaswinder Singh 		const char *fw_name = "edgeport/down3.bin";
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 		/* Validate Hardware version number
14111da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
14121da177e4SLinus Torvalds 		 */
14131da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
14141da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1415441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
14161da177e4SLinus Torvalds 			return -ENOMEM;
14171da177e4SLinus Torvalds 		}
14182742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
14191da177e4SLinus Torvalds 		if (status) {
14201da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14212742fd88SAlan Cox 			goto stayinbootmode;
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 
14242742fd88SAlan Cox 		/* Check for version 2 */
14252742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
14262742fd88SAlan Cox 			dbg("%s - Wrong CPU Rev %d (Must be 2)",
14272742fd88SAlan Cox 					__func__, ti_cpu_rev(ti_manuf_desc));
14281da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
14292742fd88SAlan Cox 			goto stayinbootmode;
14301da177e4SLinus Torvalds 		}
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 		/*
14352742fd88SAlan Cox 		 * In order to update the I2C firmware we must change the type
14362742fd88SAlan Cox 		 * 2 record to type 0xF2. This will force the UMP to come up
14372742fd88SAlan Cox 		 * in Boot Mode.  Then while in boot mode, the driver will
14382742fd88SAlan Cox 		 * download the latest firmware (padded to 15.5k) into the
14392742fd88SAlan Cox 		 * UMP ram. Finally when the device comes back up in download
14402742fd88SAlan Cox 		 * mode the driver will cause the new firmware to be copied
14412742fd88SAlan Cox 		 * from the UMP Ram to I2C and the firmware will update the
14422742fd88SAlan Cox 		 * record type from 0xf2 to 0x02.
14432742fd88SAlan Cox 		 *
14441da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14451da177e4SLinus Torvalds 		 * or could we do this in place!
14461da177e4SLinus Torvalds 		 */
14471da177e4SLinus Torvalds 
14482742fd88SAlan Cox 		/* Allocate a 15.5k buffer + 3 byte header */
14492742fd88SAlan Cox 		buffer_size = (((1024 * 16) - 512) +
14502742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header));
14511da177e4SLinus Torvalds 		buffer = kmalloc(buffer_size, GFP_KERNEL);
14521da177e4SLinus Torvalds 		if (!buffer) {
1453441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory\n", __func__);
14541da177e4SLinus Torvalds 			return -ENOMEM;
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 
14572742fd88SAlan Cox 		/* Initialize the buffer to 0xff (pad the buffer) */
14581da177e4SLinus Torvalds 		memset(buffer, 0xff, buffer_size);
14591da177e4SLinus Torvalds 
1460d12b219aSJaswinder Singh 		err = request_firmware(&fw, fw_name, dev);
1461d12b219aSJaswinder Singh 		if (err) {
1462d12b219aSJaswinder Singh 			printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1463d12b219aSJaswinder Singh 			       fw_name, err);
1464d12b219aSJaswinder Singh 			kfree(buffer);
1465d12b219aSJaswinder Singh 			return err;
1466d12b219aSJaswinder Singh 		}
1467d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
1468d12b219aSJaswinder Singh 		release_firmware(fw);
14691da177e4SLinus Torvalds 
14702742fd88SAlan Cox 		for (i = sizeof(struct ti_i2c_image_header);
14712742fd88SAlan Cox 				i < buffer_size; i++) {
14721da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14731da177e4SLinus Torvalds 		}
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14761da177e4SLinus Torvalds 
14772742fd88SAlan Cox 		/* update length and checksum after padding */
14782742fd88SAlan Cox 		header->Length 	 = cpu_to_le16((__u16)(buffer_size -
14792742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header)));
14801da177e4SLinus Torvalds 		header->CheckSum = cs;
14811da177e4SLinus Torvalds 
14822742fd88SAlan Cox 		/* Download the operational code  */
14832742fd88SAlan Cox 		dbg("%s - Downloading operational code image (TI UMP)",
14842742fd88SAlan Cox 								__func__);
14852742fd88SAlan Cox 		status = download_code(serial, buffer, buffer_size);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 		kfree(buffer);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 		if (status) {
14902742fd88SAlan Cox 			dbg("%s - Error downloading operational code image",
14912742fd88SAlan Cox 								__func__);
14921da177e4SLinus Torvalds 			return status;
14931da177e4SLinus Torvalds 		}
14941da177e4SLinus Torvalds 
14952742fd88SAlan Cox 		/* Device will reboot */
14961da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14971da177e4SLinus Torvalds 
14982742fd88SAlan Cox 		dbg("%s - Download successful -- Device rebooting...",
14992742fd88SAlan Cox 								__func__);
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 		/* return an error on purpose */
15021da177e4SLinus Torvalds 		return -ENODEV;
15031da177e4SLinus Torvalds 	}
15041da177e4SLinus Torvalds 
15052742fd88SAlan Cox stayinbootmode:
15062742fd88SAlan Cox 	/* Eprom is invalid or blank stay in boot mode */
15079544e833SAndrew Morton 	dbg("%s - STAYING IN BOOT MODE", __func__);
15081da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	return 0;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 
15142742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15172742fd88SAlan Cox 	on = !!on;	/* 1 or 0 not bitmask */
15182742fd88SAlan Cox 	return send_cmd(port->port->serial->dev,
15192742fd88SAlan Cox 			feature, (__u8)(UMPM_UART1_PORT + port_number),
15202742fd88SAlan Cox 			on, NULL, 0);
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 
15242742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	int status = 0;
15271da177e4SLinus Torvalds 
1528441b62c1SHarvey Harrison 	dbg("%s - %x", __func__, mcr);
15291da177e4SLinus Torvalds 
15302742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
15311da177e4SLinus Torvalds 	if (status)
15321da177e4SLinus Torvalds 		return status;
15332742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
15341da177e4SLinus Torvalds 	if (status)
15351da177e4SLinus Torvalds 		return status;
15362742fd88SAlan Cox 	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
15402742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	__u8 lsr = 0;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
15451da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
15461da177e4SLinus Torvalds 		lsr |= flagUart;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
15491da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
15501da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
15511da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
15522742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
15532742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds #undef MAP_FLAG
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	return lsr;
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
15611da177e4SLinus Torvalds {
15621da177e4SLinus Torvalds 	struct async_icount *icount;
15631da177e4SLinus Torvalds 	struct tty_struct *tty;
15641da177e4SLinus Torvalds 
1565441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, msr);
15661da177e4SLinus Torvalds 
15672742fd88SAlan Cox 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
15682742fd88SAlan Cox 			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
15691da177e4SLinus Torvalds 		icount = &edge_port->icount;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		/* update input line counters */
15721da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
15731da177e4SLinus Torvalds 			icount->cts++;
15741da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
15751da177e4SLinus Torvalds 			icount->dsr++;
15761da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
15771da177e4SLinus Torvalds 			icount->dcd++;
15781da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
15791da177e4SLinus Torvalds 			icount->rng++;
15801da177e4SLinus Torvalds 		wake_up_interruptible(&edge_port->delta_msr_wait);
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	/* Save the new modem status */
15841da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
15851da177e4SLinus Torvalds 
15864a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
15871da177e4SLinus Torvalds 	/* handle CTS flow control */
15881da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
15891da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_CTS) {
15901da177e4SLinus Torvalds 			tty->hw_stopped = 0;
15911da177e4SLinus Torvalds 			tty_wakeup(tty);
15921da177e4SLinus Torvalds 		} else {
15931da177e4SLinus Torvalds 			tty->hw_stopped = 1;
15941da177e4SLinus Torvalds 		}
15951da177e4SLinus Torvalds 	}
15964a90f09bSAlan Cox 	tty_kref_put(tty);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	return;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16012742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
16022742fd88SAlan Cox 							__u8 lsr, __u8 data)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	struct async_icount *icount;
16052742fd88SAlan Cox 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
16062742fd88SAlan Cox 						LSR_FRM_ERR | LSR_BREAK));
16074a90f09bSAlan Cox 	struct tty_struct *tty;
16081da177e4SLinus Torvalds 
1609441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, new_lsr);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
16121da177e4SLinus Torvalds 
16132742fd88SAlan Cox 	if (new_lsr & LSR_BREAK)
16141da177e4SLinus Torvalds 		/*
16151da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
16161da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
16171da177e4SLinus Torvalds 		 */
16181da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
16214a90f09bSAlan Cox 	if (lsr_data) {
16224a90f09bSAlan Cox 		tty = tty_port_tty_get(&edge_port->port->port);
16234a90f09bSAlan Cox 		if (tty) {
16244a90f09bSAlan Cox 			edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
16254a90f09bSAlan Cox 			tty_kref_put(tty);
16264a90f09bSAlan Cox 		}
16274a90f09bSAlan Cox 	}
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	/* update input line counters */
16301da177e4SLinus Torvalds 	icount = &edge_port->icount;
16311da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
16321da177e4SLinus Torvalds 		icount->brk++;
16331da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
16341da177e4SLinus Torvalds 		icount->overrun++;
16351da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
16361da177e4SLinus Torvalds 		icount->parity++;
16371da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
16381da177e4SLinus Torvalds 		icount->frame++;
16391da177e4SLinus Torvalds }
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 
16427d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb)
16431da177e4SLinus Torvalds {
1644cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
16451da177e4SLinus Torvalds 	struct usb_serial_port *port;
16461da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
16471da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
16481da177e4SLinus Torvalds 	int length = urb->actual_length;
16491da177e4SLinus Torvalds 	int port_number;
16501da177e4SLinus Torvalds 	int function;
1651ee337c21SGreg Kroah-Hartman 	int retval;
16521da177e4SLinus Torvalds 	__u8 lsr;
16531da177e4SLinus Torvalds 	__u8 msr;
1654ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16551da177e4SLinus Torvalds 
1656441b62c1SHarvey Harrison 	dbg("%s", __func__);
16571da177e4SLinus Torvalds 
1658ee337c21SGreg Kroah-Hartman 	switch (status) {
16591da177e4SLinus Torvalds 	case 0:
16601da177e4SLinus Torvalds 		/* success */
16611da177e4SLinus Torvalds 		break;
16621da177e4SLinus Torvalds 	case -ECONNRESET:
16631da177e4SLinus Torvalds 	case -ENOENT:
16641da177e4SLinus Torvalds 	case -ESHUTDOWN:
16651da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1666ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1667441b62c1SHarvey Harrison 		    __func__, status);
16681da177e4SLinus Torvalds 		return;
16691da177e4SLinus Torvalds 	default:
1670ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1671441b62c1SHarvey Harrison 			"%d\n", __func__, status);
16721da177e4SLinus Torvalds 		goto exit;
16731da177e4SLinus Torvalds 	}
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	if (!length) {
1676441b62c1SHarvey Harrison 		dbg("%s - no data in urb", __func__);
16771da177e4SLinus Torvalds 		goto exit;
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16802742fd88SAlan Cox 	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
16812742fd88SAlan Cox 						__func__, length, data);
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	if (length != 2) {
16842742fd88SAlan Cox 		dbg("%s - expecting packet of size 2, got %d",
16852742fd88SAlan Cox 							__func__, length);
16861da177e4SLinus Torvalds 		goto exit;
16871da177e4SLinus Torvalds 	}
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
16901da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
16911da177e4SLinus Torvalds 	dbg("%s - port_number %d, function %d, info 0x%x",
1692441b62c1SHarvey Harrison 	     __func__, port_number, function, data[1]);
16931da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
16941da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
16951da177e4SLinus Torvalds 	if (!edge_port) {
1696441b62c1SHarvey Harrison 		dbg("%s - edge_port not found", __func__);
16971da177e4SLinus Torvalds 		return;
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds 	switch (function) {
17001da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
17012742fd88SAlan Cox 		lsr = map_line_status(data[1]);
17021da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
17032742fd88SAlan Cox 			/* Save the LSR event for bulk read
17042742fd88SAlan Cox 			   completion routine */
17051da177e4SLinus Torvalds 			dbg("%s - LSR Event Port %u LSR Status = %02x",
1706441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17071da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
17081da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
17091da177e4SLinus Torvalds 		} else {
17101da177e4SLinus Torvalds 			dbg("%s - ===== Port %d LSR Status = %02x ======",
1711441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17121da177e4SLinus Torvalds 			handle_new_lsr(edge_port, 0, lsr, 0);
17131da177e4SLinus Torvalds 		}
17141da177e4SLinus Torvalds 		break;
17151da177e4SLinus Torvalds 
17162742fd88SAlan Cox 	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
17171da177e4SLinus Torvalds 		/* Copy MSR from UMP */
17181da177e4SLinus Torvalds 		msr = data[1];
17191da177e4SLinus Torvalds 		dbg("%s - ===== Port %u MSR Status = %02x ======\n",
1720441b62c1SHarvey Harrison 		     __func__, port_number, msr);
17211da177e4SLinus Torvalds 		handle_new_msr(edge_port, msr);
17221da177e4SLinus Torvalds 		break;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	default:
17252742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17262742fd88SAlan Cox 			"%s - Unknown Interrupt code from UMP %x\n",
1727441b62c1SHarvey Harrison 			__func__, data[1]);
17281da177e4SLinus Torvalds 		break;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds exit:
1733ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1734ee337c21SGreg Kroah-Hartman 	if (retval)
17352742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17362742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1737441b62c1SHarvey Harrison 			 __func__, retval);
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17407d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb)
17411da177e4SLinus Torvalds {
1742cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
17431da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17441da177e4SLinus Torvalds 	struct tty_struct *tty;
1745ee337c21SGreg Kroah-Hartman 	int retval = 0;
17461da177e4SLinus Torvalds 	int port_number;
1747ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17481da177e4SLinus Torvalds 
1749441b62c1SHarvey Harrison 	dbg("%s", __func__);
17501da177e4SLinus Torvalds 
1751ee337c21SGreg Kroah-Hartman 	switch (status) {
17521da177e4SLinus Torvalds 	case 0:
17531da177e4SLinus Torvalds 		/* success */
17541da177e4SLinus Torvalds 		break;
17551da177e4SLinus Torvalds 	case -ECONNRESET:
17561da177e4SLinus Torvalds 	case -ENOENT:
17571da177e4SLinus Torvalds 	case -ESHUTDOWN:
17581da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1759ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1760441b62c1SHarvey Harrison 		    __func__, status);
17611da177e4SLinus Torvalds 		return;
17621da177e4SLinus Torvalds 	default:
17632742fd88SAlan Cox 		dev_err(&urb->dev->dev,
17642742fd88SAlan Cox 			"%s - nonzero read bulk status received: %d\n",
1765441b62c1SHarvey Harrison 			     __func__, status);
17661da177e4SLinus Torvalds 	}
17671da177e4SLinus Torvalds 
1768ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
17691da177e4SLinus Torvalds 		goto exit;
17701da177e4SLinus Torvalds 
1771ee337c21SGreg Kroah-Hartman 	if (status) {
1772441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
17731da177e4SLinus Torvalds 		return;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	port_number = edge_port->port->number - edge_port->port->serial->minor;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
17791da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
17801da177e4SLinus Torvalds 		dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1781441b62c1SHarvey Harrison 		     __func__, port_number, edge_port->lsr_mask, *data);
17821da177e4SLinus Torvalds 		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
17831da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
17841da177e4SLinus Torvalds 		--urb->actual_length;
17851da177e4SLinus Torvalds 		++data;
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds 
17884a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
17891da177e4SLinus Torvalds 	if (tty && urb->actual_length) {
17902742fd88SAlan Cox 		usb_serial_debug_data(debug, &edge_port->port->dev,
17912742fd88SAlan Cox 					__func__, urb->actual_length, data);
17922742fd88SAlan Cox 		if (edge_port->close_pending)
17932742fd88SAlan Cox 			dbg("%s - close pending, dropping data on the floor",
17942742fd88SAlan Cox 								__func__);
17952742fd88SAlan Cox 		else
17962742fd88SAlan Cox 			edge_tty_recv(&edge_port->port->dev, tty, data,
17972742fd88SAlan Cox 							urb->actual_length);
17981da177e4SLinus Torvalds 		edge_port->icount.rx += urb->actual_length;
17991da177e4SLinus Torvalds 	}
18004a90f09bSAlan Cox 	tty_kref_put(tty);
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds exit:
18031da177e4SLinus Torvalds 	/* continue read unless stopped */
18041da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
18051da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
18061da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
1807ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
18081da177e4SLinus Torvalds 	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
18091da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
1812ee337c21SGreg Kroah-Hartman 	if (retval)
18132742fd88SAlan Cox 		dev_err(&urb->dev->dev,
18142742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1815441b62c1SHarvey Harrison 			 __func__, retval);
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18182742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
18192742fd88SAlan Cox 					unsigned char *data, int length)
18201da177e4SLinus Torvalds {
18212742fd88SAlan Cox 	int queued;
18221da177e4SLinus Torvalds 
18232742fd88SAlan Cox 	tty_buffer_request_room(tty, length);
18242742fd88SAlan Cox 	queued = tty_insert_flip_string(tty, data, length);
18252742fd88SAlan Cox 	if (queued < length)
18261da177e4SLinus Torvalds 		dev_err(dev, "%s - dropping data, %d bytes lost\n",
18272742fd88SAlan Cox 			__func__, length - queued);
18281da177e4SLinus Torvalds 	tty_flip_buffer_push(tty);
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
18317d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb)
18321da177e4SLinus Torvalds {
1833cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
18341da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1835ee337c21SGreg Kroah-Hartman 	int status = urb->status;
18364a90f09bSAlan Cox 	struct tty_struct *tty;
18371da177e4SLinus Torvalds 
1838441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
18411da177e4SLinus Torvalds 
1842ee337c21SGreg Kroah-Hartman 	switch (status) {
18431da177e4SLinus Torvalds 	case 0:
18441da177e4SLinus Torvalds 		/* success */
18451da177e4SLinus Torvalds 		break;
18461da177e4SLinus Torvalds 	case -ECONNRESET:
18471da177e4SLinus Torvalds 	case -ENOENT:
18481da177e4SLinus Torvalds 	case -ESHUTDOWN:
18491da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1850ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1851441b62c1SHarvey Harrison 		    __func__, status);
18521da177e4SLinus Torvalds 		return;
18531da177e4SLinus Torvalds 	default:
1854ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1855441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	/* send any buffered data */
18594a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
18604a90f09bSAlan Cox 	edge_send(tty);
18614a90f09bSAlan Cox 	tty_kref_put(tty);
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
1864a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
18651da177e4SLinus Torvalds {
18661da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
18671da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
18681da177e4SLinus Torvalds 	struct usb_device *dev;
18691da177e4SLinus Torvalds 	struct urb *urb;
18701da177e4SLinus Torvalds 	int port_number;
18711da177e4SLinus Torvalds 	int status;
18721da177e4SLinus Torvalds 	u16 open_settings;
18731da177e4SLinus Torvalds 	u8 transaction_timeout;
18741da177e4SLinus Torvalds 
1875441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	if (edge_port == NULL)
18781da177e4SLinus Torvalds 		return -ENODEV;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
18811da177e4SLinus Torvalds 	switch (port_number) {
18821da177e4SLinus Torvalds 	case 0:
18831da177e4SLinus Torvalds 		edge_port->uart_base = UMPMEM_BASE_UART1;
18841da177e4SLinus Torvalds 		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
18851da177e4SLinus Torvalds 		break;
18861da177e4SLinus Torvalds 	case 1:
18871da177e4SLinus Torvalds 		edge_port->uart_base = UMPMEM_BASE_UART2;
18881da177e4SLinus Torvalds 		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
18891da177e4SLinus Torvalds 		break;
18901da177e4SLinus Torvalds 	default:
18911da177e4SLinus Torvalds 		dev_err(&port->dev, "Unknown port number!!!\n");
18921da177e4SLinus Torvalds 		return -ENODEV;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
18962742fd88SAlan Cox 				__func__, port_number, edge_port->uart_base,
18972742fd88SAlan Cox 				edge_port->dma_address);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	dev = port->serial->dev;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
19021da177e4SLinus Torvalds 	init_waitqueue_head(&edge_port->delta_msr_wait);
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	/* turn off loopback */
19052742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
19061da177e4SLinus Torvalds 	if (status) {
19072742fd88SAlan Cox 		dev_err(&port->dev,
19082742fd88SAlan Cox 				"%s - cannot send clear loopback command, %d\n",
1909441b62c1SHarvey Harrison 			__func__, status);
19101da177e4SLinus Torvalds 		return status;
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	/* set up the port settings */
191495da310eSAlan Cox 	if (tty)
19154a90f09bSAlan Cox 		edge_set_termios(tty, port, tty->termios);
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	/* open up the port */
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
19201da177e4SLinus Torvalds 	transaction_timeout = 2;
19211da177e4SLinus Torvalds 
19222742fd88SAlan Cox 	edge_port->ump_read_timeout =
19232742fd88SAlan Cox 				max(20, ((transaction_timeout * 3) / 2));
19241da177e4SLinus Torvalds 
19252742fd88SAlan Cox 	/* milliseconds to timeout for DMA transfer */
19261da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
19271da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
19281da177e4SLinus Torvalds 			     (transaction_timeout << 2));
19291da177e4SLinus Torvalds 
1930441b62c1SHarvey Harrison 	dbg("%s - Sending UMPC_OPEN_PORT", __func__);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
19332742fd88SAlan Cox 	status = send_cmd(dev, UMPC_OPEN_PORT,
19342742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
19351da177e4SLinus Torvalds 	if (status) {
19362742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send open command, %d\n",
19372742fd88SAlan Cox 							__func__, status);
19381da177e4SLinus Torvalds 		return status;
19391da177e4SLinus Torvalds 	}
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	/* Start the DMA? */
19422742fd88SAlan Cox 	status = send_cmd(dev, UMPC_START_PORT,
19432742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
19441da177e4SLinus Torvalds 	if (status) {
19452742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
19462742fd88SAlan Cox 							__func__, status);
19471da177e4SLinus Torvalds 		return status;
19481da177e4SLinus Torvalds 	}
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
19512742fd88SAlan Cox 	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
19521da177e4SLinus Torvalds 	if (status) {
19532742fd88SAlan Cox 		dev_err(&port->dev,
19542742fd88SAlan Cox 			"%s - cannot send clear buffers command, %d\n",
19552742fd88SAlan Cox 			__func__, status);
19561da177e4SLinus Torvalds 		return status;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	/* Read Initial MSR */
19602742fd88SAlan Cox 	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
19612742fd88SAlan Cox 				(__u16)(UMPM_UART1_PORT + port_number),
19622742fd88SAlan Cox 				&edge_port->shadow_msr, 1);
19631da177e4SLinus Torvalds 	if (status) {
19642742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
19652742fd88SAlan Cox 							__func__, status);
19661da177e4SLinus Torvalds 		return status;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	/* Set Initial MCR */
19721da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
19731da177e4SLinus Torvalds 	dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
1976241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
19771da177e4SLinus Torvalds 		return -ERESTARTSYS;
19781da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
19792742fd88SAlan Cox 		/* we are the first port to open, post the interrupt urb */
19801da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
19811da177e4SLinus Torvalds 		if (!urb) {
19822742fd88SAlan Cox 			dev_err(&port->dev,
19832742fd88SAlan Cox 				"%s - no interrupt urb present, exiting\n",
19842742fd88SAlan Cox 				__func__);
19851da177e4SLinus Torvalds 			status = -EINVAL;
1986241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19871da177e4SLinus Torvalds 		}
19881da177e4SLinus Torvalds 		urb->complete = edge_interrupt_callback;
19891da177e4SLinus Torvalds 		urb->context = edge_serial;
19901da177e4SLinus Torvalds 		urb->dev = dev;
19911da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
19921da177e4SLinus Torvalds 		if (status) {
19932742fd88SAlan Cox 			dev_err(&port->dev,
19942742fd88SAlan Cox 				"%s - usb_submit_urb failed with value %d\n",
19952742fd88SAlan Cox 					__func__, status);
1996241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19971da177e4SLinus Torvalds 		}
19981da177e4SLinus Torvalds 	}
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/*
20011da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
20021da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
20031da177e4SLinus Torvalds 	 */
20041da177e4SLinus Torvalds 	usb_clear_halt(dev, port->write_urb->pipe);
20051da177e4SLinus Torvalds 	usb_clear_halt(dev, port->read_urb->pipe);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	/* start up our bulk read urb */
20081da177e4SLinus Torvalds 	urb = port->read_urb;
20091da177e4SLinus Torvalds 	if (!urb) {
20102742fd88SAlan Cox 		dev_err(&port->dev, "%s - no read urb present, exiting\n",
20112742fd88SAlan Cox 								__func__);
20121da177e4SLinus Torvalds 		status = -EINVAL;
20131da177e4SLinus Torvalds 		goto unlink_int_urb;
20141da177e4SLinus Torvalds 	}
20151da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
20161da177e4SLinus Torvalds 	urb->complete = edge_bulk_in_callback;
20171da177e4SLinus Torvalds 	urb->context = edge_port;
20181da177e4SLinus Torvalds 	urb->dev = dev;
20191da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_KERNEL);
20201da177e4SLinus Torvalds 	if (status) {
20212742fd88SAlan Cox 		dev_err(&port->dev,
20222742fd88SAlan Cox 			"%s - read bulk usb_submit_urb failed with value %d\n",
20232742fd88SAlan Cox 				__func__, status);
20241da177e4SLinus Torvalds 		goto unlink_int_urb;
20251da177e4SLinus Torvalds 	}
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
20281da177e4SLinus Torvalds 
2029441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
20301da177e4SLinus Torvalds 
2031241ca64fSMatthias Kaehlcke 	goto release_es_lock;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds unlink_int_urb:
20341da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
20351da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2036241ca64fSMatthias Kaehlcke release_es_lock:
2037241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
20381da177e4SLinus Torvalds 	return status;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
2041335f8514SAlan Cox static void edge_close(struct usb_serial_port *port)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
20441da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
20451da177e4SLinus Torvalds 	int port_number;
20461da177e4SLinus Torvalds 	int status;
20471da177e4SLinus Torvalds 
2048441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
20511da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
20522742fd88SAlan Cox 	if (edge_serial == NULL || edge_port == NULL)
20531da177e4SLinus Torvalds 		return;
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
20561da177e4SLinus Torvalds 	 * this flag and dump add read data */
20571da177e4SLinus Torvalds 	edge_port->close_pending = 1;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	/* chase the port close and flush */
20602742fd88SAlan Cox 	chase_port(edge_port, (HZ * closing_wait) / 100, 1);
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
20631da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
20641da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	/* assuming we can still talk to the device,
20671da177e4SLinus Torvalds 	 * send a close port command to it */
2068441b62c1SHarvey Harrison 	dbg("%s - send umpc_close_port", __func__);
20691da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
20702742fd88SAlan Cox 	status = send_cmd(port->serial->dev,
20711da177e4SLinus Torvalds 				     UMPC_CLOSE_PORT,
20721da177e4SLinus Torvalds 				     (__u8)(UMPM_UART1_PORT + port_number),
20731da177e4SLinus Torvalds 				     0,
20741da177e4SLinus Torvalds 				     NULL,
20751da177e4SLinus Torvalds 				     0);
2076241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
20771da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
20781da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
20791da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
20801da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
20811da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
20821da177e4SLinus Torvalds 	}
2083241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
20841da177e4SLinus Torvalds 	edge_port->close_pending = 0;
20851da177e4SLinus Torvalds 
2086441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds 
208995da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
209095da310eSAlan Cox 				const unsigned char *data, int count)
20911da177e4SLinus Torvalds {
20921da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20931da177e4SLinus Torvalds 	unsigned long flags;
20941da177e4SLinus Torvalds 
2095441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	if (count == 0) {
2098441b62c1SHarvey Harrison 		dbg("%s - write request of 0 bytes", __func__);
20991da177e4SLinus Torvalds 		return 0;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	if (edge_port == NULL)
21031da177e4SLinus Torvalds 		return -ENODEV;
21041da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21051da177e4SLinus Torvalds 		return -ENODEV;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21081da177e4SLinus Torvalds 	count = edge_buf_put(edge_port->ep_out_buf, data, count);
21091da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21101da177e4SLinus Torvalds 
211195da310eSAlan Cox 	edge_send(tty);
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 	return count;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds 
211695da310eSAlan Cox static void edge_send(struct tty_struct *tty)
21171da177e4SLinus Torvalds {
211895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21191da177e4SLinus Torvalds 	int count, result;
21201da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21211da177e4SLinus Torvalds 	unsigned long flags;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 
2124441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
21291da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21301da177e4SLinus Torvalds 		return;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	count = edge_buf_get(edge_port->ep_out_buf,
21341da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
21351da177e4SLinus Torvalds 				port->bulk_out_size);
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (count == 0) {
21381da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21391da177e4SLinus Torvalds 		return;
21401da177e4SLinus Torvalds 	}
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21451da177e4SLinus Torvalds 
21462742fd88SAlan Cox 	usb_serial_debug_data(debug, &port->dev, __func__, count,
21472742fd88SAlan Cox 				port->write_urb->transfer_buffer);
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	/* set up our urb */
21501da177e4SLinus Torvalds 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
21511da177e4SLinus Torvalds 			   usb_sndbulkpipe(port->serial->dev,
21521da177e4SLinus Torvalds 					    port->bulk_out_endpointAddress),
21531da177e4SLinus Torvalds 			   port->write_urb->transfer_buffer, count,
21541da177e4SLinus Torvalds 			   edge_bulk_out_callback,
21551da177e4SLinus Torvalds 			   port);
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	/* send the data out the bulk port */
21581da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
21591da177e4SLinus Torvalds 	if (result) {
21602742fd88SAlan Cox 		dev_err(&port->dev,
21612742fd88SAlan Cox 			"%s - failed submitting write urb, error %d\n",
21622742fd88SAlan Cox 				__func__, result);
21631da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
21642742fd88SAlan Cox 		/* TODO: reschedule edge_send */
21652742fd88SAlan Cox 	} else
21661da177e4SLinus Torvalds 		edge_port->icount.tx += count;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
21691da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
21702742fd88SAlan Cox 	if (tty)
21711da177e4SLinus Torvalds 		tty_wakeup(tty);
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
217495da310eSAlan Cox static int edge_write_room(struct tty_struct *tty)
21751da177e4SLinus Torvalds {
217695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21771da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21781da177e4SLinus Torvalds 	int room = 0;
21791da177e4SLinus Torvalds 	unsigned long flags;
21801da177e4SLinus Torvalds 
2181441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	if (edge_port == NULL)
21842742fd88SAlan Cox 		return 0;
21851da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21862742fd88SAlan Cox 		return 0;
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21891da177e4SLinus Torvalds 	room = edge_buf_space_avail(edge_port->ep_out_buf);
21901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21911da177e4SLinus Torvalds 
2192441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
21931da177e4SLinus Torvalds 	return room;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
219695da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty)
21971da177e4SLinus Torvalds {
219895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21991da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22001da177e4SLinus Torvalds 	int chars = 0;
22011da177e4SLinus Torvalds 	unsigned long flags;
22021da177e4SLinus Torvalds 
2203441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	if (edge_port == NULL)
22062742fd88SAlan Cox 		return 0;
22071da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22082742fd88SAlan Cox 		return 0;
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22111da177e4SLinus Torvalds 	chars = edge_buf_data_avail(edge_port->ep_out_buf);
22121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22131da177e4SLinus Torvalds 
2214441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
22151da177e4SLinus Torvalds 	return chars;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
221895da310eSAlan Cox static void edge_throttle(struct tty_struct *tty)
22191da177e4SLinus Torvalds {
222095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22211da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22221da177e4SLinus Torvalds 	int status;
22231da177e4SLinus Torvalds 
2224441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	if (edge_port == NULL)
22271da177e4SLinus Torvalds 		return;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
22301da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22311da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
223295da310eSAlan Cox 		status = edge_write(tty, port, &stop_char, 1);
223395da310eSAlan Cox 		if (status <= 0) {
223495da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
223595da310eSAlan Cox 		}
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
22391da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
22401da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
22411da177e4SLinus Torvalds 		stop_read(edge_port);
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
224595da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty)
22461da177e4SLinus Torvalds {
224795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22481da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22491da177e4SLinus Torvalds 	int status;
22501da177e4SLinus Torvalds 
2251441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	if (edge_port == NULL)
22541da177e4SLinus Torvalds 		return;
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
22571da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22581da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
225995da310eSAlan Cox 		status = edge_write(tty, port, &start_char, 1);
226095da310eSAlan Cox 		if (status <= 0) {
226195da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
226295da310eSAlan Cox 		}
22631da177e4SLinus Torvalds 	}
22641da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
22651da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
22661da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
22671da177e4SLinus Torvalds 		status = restart_read(edge_port);
22681da177e4SLinus Torvalds 		if (status)
22692742fd88SAlan Cox 			dev_err(&port->dev,
22702742fd88SAlan Cox 				"%s - read bulk usb_submit_urb failed: %d\n",
22712742fd88SAlan Cox 							__func__, status);
22721da177e4SLinus Torvalds 	}
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
22771da177e4SLinus Torvalds {
22781da177e4SLinus Torvalds 	unsigned long flags;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
22831da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
22841da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
22901da177e4SLinus Torvalds {
22911da177e4SLinus Torvalds 	struct urb *urb;
22921da177e4SLinus Torvalds 	int status = 0;
22931da177e4SLinus Torvalds 	unsigned long flags;
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
22981da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
22991da177e4SLinus Torvalds 		urb->complete = edge_bulk_in_callback;
23001da177e4SLinus Torvalds 		urb->context = edge_port;
23011da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
2302efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
23031da177e4SLinus Torvalds 	}
23041da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
23051da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	return status;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
231295da310eSAlan Cox static void change_port_settings(struct tty_struct *tty,
231395da310eSAlan Cox 		struct edgeport_port *edge_port, struct ktermios *old_termios)
23141da177e4SLinus Torvalds {
23151da177e4SLinus Torvalds 	struct ump_uart_config *config;
23161da177e4SLinus Torvalds 	int baud;
23171da177e4SLinus Torvalds 	unsigned cflag;
23181da177e4SLinus Torvalds 	int status;
23192742fd88SAlan Cox 	int port_number = edge_port->port->number -
23202742fd88SAlan Cox 					edge_port->port->serial->minor;
23211da177e4SLinus Torvalds 
2322441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, edge_port->port->number);
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
23251da177e4SLinus Torvalds 	if (!config) {
23262742fd88SAlan Cox 		*tty->termios = *old_termios;
23272742fd88SAlan Cox 		dev_err(&edge_port->port->dev, "%s - out of memory\n",
23282742fd88SAlan Cox 								__func__);
23291da177e4SLinus Torvalds 		return;
23301da177e4SLinus Torvalds 	}
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	config->wFlags = 0;
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	/* These flags must be set */
23371da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
23381da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
23391da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
23421da177e4SLinus Torvalds 	case CS5:
23431da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR5BITS;
2344441b62c1SHarvey Harrison 		    dbg("%s - data bits = 5", __func__);
23451da177e4SLinus Torvalds 		    break;
23461da177e4SLinus Torvalds 	case CS6:
23471da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR6BITS;
2348441b62c1SHarvey Harrison 		    dbg("%s - data bits = 6", __func__);
23491da177e4SLinus Torvalds 		    break;
23501da177e4SLinus Torvalds 	case CS7:
23511da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR7BITS;
2352441b62c1SHarvey Harrison 		    dbg("%s - data bits = 7", __func__);
23531da177e4SLinus Torvalds 		    break;
23541da177e4SLinus Torvalds 	default:
23551da177e4SLinus Torvalds 	case CS8:
23561da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR8BITS;
2357441b62c1SHarvey Harrison 		    dbg("%s - data bits = 8", __func__);
23581da177e4SLinus Torvalds 			    break;
23591da177e4SLinus Torvalds 	}
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds 	if (cflag & PARENB) {
23621da177e4SLinus Torvalds 		if (cflag & PARODD) {
23631da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
23641da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
2365441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
23661da177e4SLinus Torvalds 		} else {
23671da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
23681da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
2369441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
23701da177e4SLinus Torvalds 		}
23711da177e4SLinus Torvalds 	} else {
23721da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
2373441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
23741da177e4SLinus Torvalds 	}
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
23771da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
2378441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
23791da177e4SLinus Torvalds 	} else {
23801da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
2381441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
23821da177e4SLinus Torvalds 	}
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	/* figure out the flow control settings */
23851da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
23861da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
23871da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2388441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is enabled", __func__);
23891da177e4SLinus Torvalds 	} else {
2390441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is disabled", __func__);
23911da177e4SLinus Torvalds 		tty->hw_stopped = 0;
23921da177e4SLinus Torvalds 		restart_read(edge_port);
23931da177e4SLinus Torvalds 	}
23941da177e4SLinus Torvalds 
23952742fd88SAlan Cox 	/* if we are implementing XON/XOFF, set the start and stop
23962742fd88SAlan Cox 	   character in the device */
23971da177e4SLinus Torvalds 	config->cXon  = START_CHAR(tty);
23981da177e4SLinus Torvalds 	config->cXoff = STOP_CHAR(tty);
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	/* if we are implementing INBOUND XON/XOFF */
24011da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
24021da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
24031da177e4SLinus Torvalds 		dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2404441b62c1SHarvey Harrison 		     __func__, config->cXon, config->cXoff);
24052742fd88SAlan Cox 	} else
2406441b62c1SHarvey Harrison 		dbg("%s - INBOUND XON/XOFF is disabled", __func__);
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	/* if we are implementing OUTBOUND XON/XOFF */
24091da177e4SLinus Torvalds 	if (I_IXON(tty)) {
24101da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
24111da177e4SLinus Torvalds 		dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2412441b62c1SHarvey Harrison 		     __func__, config->cXon, config->cXoff);
24132742fd88SAlan Cox 	} else
2414441b62c1SHarvey Harrison 		dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
24151da177e4SLinus Torvalds 
2416d5f5bcd4SAlan Cox 	tty->termios->c_cflag &= ~CMSPAR;
2417d5f5bcd4SAlan Cox 
24181da177e4SLinus Torvalds 	/* Round the baud rate */
24191da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
24201da177e4SLinus Torvalds 	if (!baud) {
24211da177e4SLinus Torvalds 		/* pick a default, any default... */
24221da177e4SLinus Torvalds 		baud = 9600;
2423d5f5bcd4SAlan Cox 	} else
2424d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
2425d5f5bcd4SAlan Cox 
24261da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
24271da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
24281da177e4SLinus Torvalds 
2429d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2430d5f5bcd4SAlan Cox 
24312742fd88SAlan Cox 	dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
24322742fd88SAlan Cox 							config->wBaudRate);
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	dbg("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
24351da177e4SLinus Torvalds 	dbg("wFlags:    0x%x", config->wFlags);
24361da177e4SLinus Torvalds 	dbg("bDataBits:   %d", config->bDataBits);
24371da177e4SLinus Torvalds 	dbg("bParity:     %d", config->bParity);
24381da177e4SLinus Torvalds 	dbg("bStopBits:   %d", config->bStopBits);
24391da177e4SLinus Torvalds 	dbg("cXon:        %d", config->cXon);
24401da177e4SLinus Torvalds 	dbg("cXoff:       %d", config->cXoff);
24411da177e4SLinus Torvalds 	dbg("bUartMode:   %d", config->bUartMode);
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	/* move the word values into big endian mode */
24441da177e4SLinus Torvalds 	cpu_to_be16s(&config->wFlags);
24451da177e4SLinus Torvalds 	cpu_to_be16s(&config->wBaudRate);
24461da177e4SLinus Torvalds 
24472742fd88SAlan Cox 	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
24481da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
24492742fd88SAlan Cox 				0, (__u8 *)config, sizeof(*config));
24502742fd88SAlan Cox 	if (status)
24511da177e4SLinus Torvalds 		dbg("%s - error %d when trying to write config to device",
2452441b62c1SHarvey Harrison 		     __func__, status);
24531da177e4SLinus Torvalds 	kfree(config);
24541da177e4SLinus Torvalds 	return;
24551da177e4SLinus Torvalds }
24561da177e4SLinus Torvalds 
245795da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
245895da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
24591da177e4SLinus Torvalds {
24601da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
246195da310eSAlan Cox 	unsigned int cflag;
246295da310eSAlan Cox 
246395da310eSAlan Cox 	cflag = tty->termios->c_cflag;
24641da177e4SLinus Torvalds 
2465441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
24661da177e4SLinus Torvalds 	    tty->termios->c_cflag, tty->termios->c_iflag);
2467441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
24681da177e4SLinus Torvalds 	    old_termios->c_cflag, old_termios->c_iflag);
2469441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	if (edge_port == NULL)
24721da177e4SLinus Torvalds 		return;
24731da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
247495da310eSAlan Cox 	change_port_settings(tty, edge_port, old_termios);
247595da310eSAlan Cox 	return;
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
247895da310eSAlan Cox static int edge_tiocmset(struct tty_struct *tty, struct file *file,
24792742fd88SAlan Cox 					unsigned int set, unsigned int clear)
24801da177e4SLinus Torvalds {
248195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24821da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24831da177e4SLinus Torvalds 	unsigned int mcr;
24843d71fe0bSAlan Cox 	unsigned long flags;
24851da177e4SLinus Torvalds 
2486441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
24871da177e4SLinus Torvalds 
24883d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
24891da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
24901da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
24911da177e4SLinus Torvalds 		mcr |= MCR_RTS;
24921da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
24931da177e4SLinus Torvalds 		mcr |= MCR_DTR;
24941da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
24951da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
24981da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
24991da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
25001da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
25011da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
25021da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
25053d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
25061da177e4SLinus Torvalds 
25072742fd88SAlan Cox 	restore_mcr(edge_port, mcr);
25081da177e4SLinus Torvalds 	return 0;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
251195da310eSAlan Cox static int edge_tiocmget(struct tty_struct *tty, struct file *file)
25121da177e4SLinus Torvalds {
251395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
25141da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25151da177e4SLinus Torvalds 	unsigned int result = 0;
25161da177e4SLinus Torvalds 	unsigned int msr;
25171da177e4SLinus Torvalds 	unsigned int mcr;
25183d71fe0bSAlan Cox 	unsigned long flags;
25191da177e4SLinus Torvalds 
2520441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25211da177e4SLinus Torvalds 
25223d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
25233d71fe0bSAlan Cox 
25241da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
25251da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
25261da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
25271da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
25281da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
25291da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
25301da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
25311da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 
2534441b62c1SHarvey Harrison 	dbg("%s -- %x", __func__, result);
25353d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	return result;
25381da177e4SLinus Torvalds }
25391da177e4SLinus Torvalds 
25402742fd88SAlan Cox static int get_serial_info(struct edgeport_port *edge_port,
25412742fd88SAlan Cox 				struct serial_struct __user *retinfo)
25421da177e4SLinus Torvalds {
25431da177e4SLinus Torvalds 	struct serial_struct tmp;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	if (!retinfo)
25461da177e4SLinus Torvalds 		return -EFAULT;
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
25511da177e4SLinus Torvalds 	tmp.line		= edge_port->port->serial->minor;
25521da177e4SLinus Torvalds 	tmp.port		= edge_port->port->number;
25531da177e4SLinus Torvalds 	tmp.irq			= 0;
25541da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
25551da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
25561da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
25571da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
25581da177e4SLinus Torvalds 	tmp.closing_wait	= closing_wait;
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
25611da177e4SLinus Torvalds 		return -EFAULT;
25621da177e4SLinus Torvalds 	return 0;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds 
256595da310eSAlan Cox static int edge_ioctl(struct tty_struct *tty, struct file *file,
25662742fd88SAlan Cox 					unsigned int cmd, unsigned long arg)
25671da177e4SLinus Torvalds {
256895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
25691da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25701da177e4SLinus Torvalds 	struct async_icount cnow;
25711da177e4SLinus Torvalds 	struct async_icount cprev;
25721da177e4SLinus Torvalds 
2573441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	switch (cmd) {
25761da177e4SLinus Torvalds 	case TIOCGSERIAL:
2577441b62c1SHarvey Harrison 		dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
25782742fd88SAlan Cox 		return get_serial_info(edge_port,
25792742fd88SAlan Cox 				(struct serial_struct __user *) arg);
25801da177e4SLinus Torvalds 	case TIOCMIWAIT:
2581441b62c1SHarvey Harrison 		dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
25821da177e4SLinus Torvalds 		cprev = edge_port->icount;
25831da177e4SLinus Torvalds 		while (1) {
25841da177e4SLinus Torvalds 			interruptible_sleep_on(&edge_port->delta_msr_wait);
25851da177e4SLinus Torvalds 			/* see if a signal did it */
25861da177e4SLinus Torvalds 			if (signal_pending(current))
25871da177e4SLinus Torvalds 				return -ERESTARTSYS;
25881da177e4SLinus Torvalds 			cnow = edge_port->icount;
25891da177e4SLinus Torvalds 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
25901da177e4SLinus Torvalds 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
25911da177e4SLinus Torvalds 				return -EIO; /* no change => error */
25921da177e4SLinus Torvalds 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
25931da177e4SLinus Torvalds 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
25941da177e4SLinus Torvalds 			    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
25951da177e4SLinus Torvalds 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
25961da177e4SLinus Torvalds 				return 0;
25971da177e4SLinus Torvalds 			}
25981da177e4SLinus Torvalds 			cprev = cnow;
25991da177e4SLinus Torvalds 		}
26001da177e4SLinus Torvalds 		/* not reached */
26011da177e4SLinus Torvalds 		break;
26021da177e4SLinus Torvalds 	case TIOCGICOUNT:
2603441b62c1SHarvey Harrison 		dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
26041da177e4SLinus Torvalds 		     port->number, edge_port->icount.rx, edge_port->icount.tx);
26052742fd88SAlan Cox 		if (copy_to_user((void __user *)arg, &edge_port->icount,
26062742fd88SAlan Cox 				sizeof(edge_port->icount)))
26071da177e4SLinus Torvalds 			return -EFAULT;
26081da177e4SLinus Torvalds 		return 0;
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
261395da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state)
26141da177e4SLinus Torvalds {
261595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
26161da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
26171da177e4SLinus Torvalds 	int status;
26182742fd88SAlan Cox 	int bv = 0;	/* Off */
26191da177e4SLinus Torvalds 
262095da310eSAlan Cox 	dbg("%s - state = %d", __func__, break_state);
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds 	/* chase the port close */
26232742fd88SAlan Cox 	chase_port(edge_port, 0, 0);
26241da177e4SLinus Torvalds 
262595da310eSAlan Cox 	if (break_state == -1)
26262742fd88SAlan Cox 		bv = 1;	/* On */
26272742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
26282742fd88SAlan Cox 	if (status)
26291da177e4SLinus Torvalds 		dbg("%s - error %d sending break set/clear command.",
2630441b62c1SHarvey Harrison 		     __func__, status);
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial)
26341da177e4SLinus Torvalds {
26351da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
26361da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
26371da177e4SLinus Torvalds 	struct usb_device *dev;
26381da177e4SLinus Torvalds 	int status;
26391da177e4SLinus Torvalds 	int i;
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	dev = serial->dev;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	/* create our private serial structure */
264480b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
26451da177e4SLinus Torvalds 	if (edge_serial == NULL) {
2646441b62c1SHarvey Harrison 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
26471da177e4SLinus Torvalds 		return -ENOMEM;
26481da177e4SLinus Torvalds 	}
2649241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
26501da177e4SLinus Torvalds 	edge_serial->serial = serial;
26511da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
26521da177e4SLinus Torvalds 
26532742fd88SAlan Cox 	status = download_fw(edge_serial);
26541da177e4SLinus Torvalds 	if (status) {
26551da177e4SLinus Torvalds 		kfree(edge_serial);
26561da177e4SLinus Torvalds 		return status;
26571da177e4SLinus Torvalds 	}
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	/* set up our port private structures */
26601da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
266180b6ca48SEric Sesterhenn 		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
26621da177e4SLinus Torvalds 		if (edge_port == NULL) {
26632742fd88SAlan Cox 			dev_err(&serial->dev->dev, "%s - Out of memory\n",
26642742fd88SAlan Cox 								__func__);
26651da177e4SLinus Torvalds 			goto cleanup;
26661da177e4SLinus Torvalds 		}
26671da177e4SLinus Torvalds 		spin_lock_init(&edge_port->ep_lock);
26681da177e4SLinus Torvalds 		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
26691da177e4SLinus Torvalds 		if (edge_port->ep_out_buf == NULL) {
26702742fd88SAlan Cox 			dev_err(&serial->dev->dev, "%s - Out of memory\n",
26712742fd88SAlan Cox 								__func__);
26721da177e4SLinus Torvalds 			kfree(edge_port);
26731da177e4SLinus Torvalds 			goto cleanup;
26741da177e4SLinus Torvalds 		}
26751da177e4SLinus Torvalds 		edge_port->port = serial->port[i];
26761da177e4SLinus Torvalds 		edge_port->edge_serial = edge_serial;
26771da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], edge_port);
2678fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = default_uart_mode;
26791da177e4SLinus Torvalds 	}
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	return 0;
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds cleanup:
26841da177e4SLinus Torvalds 	for (--i; i >= 0; --i) {
26851da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
26861da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
26871da177e4SLinus Torvalds 		kfree(edge_port);
26881da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
26891da177e4SLinus Torvalds 	}
26901da177e4SLinus Torvalds 	kfree(edge_serial);
26911da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
26921da177e4SLinus Torvalds 	return -ENOMEM;
26931da177e4SLinus Torvalds }
26941da177e4SLinus Torvalds 
2695f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	int i;
26981da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
26991da177e4SLinus Torvalds 
2700441b62c1SHarvey Harrison 	dbg("%s", __func__);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
27031da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
2704fc4cbd75SMartin K. Petersen 		edge_remove_sysfs_attrs(edge_port->port);
2705f9c99bb8SAlan Stern 	}
2706f9c99bb8SAlan Stern }
2707f9c99bb8SAlan Stern 
2708f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial)
2709f9c99bb8SAlan Stern {
2710f9c99bb8SAlan Stern 	int i;
2711f9c99bb8SAlan Stern 	struct edgeport_port *edge_port;
2712f9c99bb8SAlan Stern 
2713f9c99bb8SAlan Stern 	dbg("%s", __func__);
2714f9c99bb8SAlan Stern 
2715f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2716f9c99bb8SAlan Stern 		edge_port = usb_get_serial_port_data(serial->port[i]);
27171da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
27181da177e4SLinus Torvalds 		kfree(edge_port);
27191da177e4SLinus Torvalds 	}
27201da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 
2724fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2725fc4cbd75SMartin K. Petersen 
2726fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev,
2727fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2728fc4cbd75SMartin K. Petersen {
2729fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2730fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2731fc4cbd75SMartin K. Petersen 
2732fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2733fc4cbd75SMartin K. Petersen }
2734fc4cbd75SMartin K. Petersen 
2735fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev,
2736fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2737fc4cbd75SMartin K. Petersen {
2738fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2739fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2740fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2741fc4cbd75SMartin K. Petersen 
2742441b62c1SHarvey Harrison 	dbg("%s: setting uart_mode = %d", __func__, v);
2743fc4cbd75SMartin K. Petersen 
2744fc4cbd75SMartin K. Petersen 	if (v < 256)
2745fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2746fc4cbd75SMartin K. Petersen 	else
2747441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2748fc4cbd75SMartin K. Petersen 
2749fc4cbd75SMartin K. Petersen 	return count;
2750fc4cbd75SMartin K. Petersen }
2751fc4cbd75SMartin K. Petersen 
27522742fd88SAlan Cox static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
27532742fd88SAlan Cox 							store_uart_mode);
2754fc4cbd75SMartin K. Petersen 
2755fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2756fc4cbd75SMartin K. Petersen {
2757fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2758fc4cbd75SMartin K. Petersen }
2759fc4cbd75SMartin K. Petersen 
2760fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2761fc4cbd75SMartin K. Petersen {
2762fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2763fc4cbd75SMartin K. Petersen 	return 0;
2764fc4cbd75SMartin K. Petersen }
2765fc4cbd75SMartin K. Petersen 
2766fc4cbd75SMartin K. Petersen 
27671da177e4SLinus Torvalds /* Circular Buffer */
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds /*
27701da177e4SLinus Torvalds  * edge_buf_alloc
27711da177e4SLinus Torvalds  *
27721da177e4SLinus Torvalds  * Allocate a circular buffer and all associated memory.
27731da177e4SLinus Torvalds  */
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size)
27761da177e4SLinus Torvalds {
27771da177e4SLinus Torvalds 	struct edge_buf *eb;
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 	if (size == 0)
27811da177e4SLinus Torvalds 		return NULL;
27821da177e4SLinus Torvalds 
27835cbded58SRobert P. J. Day 	eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
27841da177e4SLinus Torvalds 	if (eb == NULL)
27851da177e4SLinus Torvalds 		return NULL;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	eb->buf_buf = kmalloc(size, GFP_KERNEL);
27881da177e4SLinus Torvalds 	if (eb->buf_buf == NULL) {
27891da177e4SLinus Torvalds 		kfree(eb);
27901da177e4SLinus Torvalds 		return NULL;
27911da177e4SLinus Torvalds 	}
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 	eb->buf_size = size;
27941da177e4SLinus Torvalds 	eb->buf_get = eb->buf_put = eb->buf_buf;
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 	return eb;
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds /*
28011da177e4SLinus Torvalds  * edge_buf_free
28021da177e4SLinus Torvalds  *
28031da177e4SLinus Torvalds  * Free the buffer and all associated memory.
28041da177e4SLinus Torvalds  */
28051da177e4SLinus Torvalds 
28063d48586cSAdrian Bunk static void edge_buf_free(struct edge_buf *eb)
28071da177e4SLinus Torvalds {
28081bc3c9e1SJesper Juhl 	if (eb) {
28091da177e4SLinus Torvalds 		kfree(eb->buf_buf);
28101da177e4SLinus Torvalds 		kfree(eb);
28111da177e4SLinus Torvalds 	}
28121da177e4SLinus Torvalds }
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds /*
28161da177e4SLinus Torvalds  * edge_buf_clear
28171da177e4SLinus Torvalds  *
28181da177e4SLinus Torvalds  * Clear out all data in the circular buffer.
28191da177e4SLinus Torvalds  */
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb)
28221da177e4SLinus Torvalds {
28231da177e4SLinus Torvalds 	if (eb != NULL)
28241da177e4SLinus Torvalds 		eb->buf_get = eb->buf_put;
28251da177e4SLinus Torvalds 	/* equivalent to a get of all data available */
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds /*
28301da177e4SLinus Torvalds  * edge_buf_data_avail
28311da177e4SLinus Torvalds  *
28321da177e4SLinus Torvalds  * Return the number of bytes of data available in the circular
28331da177e4SLinus Torvalds  * buffer.
28341da177e4SLinus Torvalds  */
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb)
28371da177e4SLinus Torvalds {
28382742fd88SAlan Cox 	if (eb == NULL)
28391da177e4SLinus Torvalds 		return 0;
28402742fd88SAlan Cox 	return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
28411da177e4SLinus Torvalds }
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds /*
28451da177e4SLinus Torvalds  * edge_buf_space_avail
28461da177e4SLinus Torvalds  *
28471da177e4SLinus Torvalds  * Return the number of bytes of space available in the circular
28481da177e4SLinus Torvalds  * buffer.
28491da177e4SLinus Torvalds  */
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb)
28521da177e4SLinus Torvalds {
28532742fd88SAlan Cox 	if (eb == NULL)
28541da177e4SLinus Torvalds 		return 0;
28552742fd88SAlan Cox 	return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds /*
28601da177e4SLinus Torvalds  * edge_buf_put
28611da177e4SLinus Torvalds  *
28621da177e4SLinus Torvalds  * Copy data data from a user buffer and put it into the circular buffer.
28631da177e4SLinus Torvalds  * Restrict to the amount of space available.
28641da177e4SLinus Torvalds  *
28651da177e4SLinus Torvalds  * Return the number of bytes copied.
28661da177e4SLinus Torvalds  */
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
28691da177e4SLinus Torvalds 	unsigned int count)
28701da177e4SLinus Torvalds {
28711da177e4SLinus Torvalds 	unsigned int len;
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 	if (eb == NULL)
28751da177e4SLinus Torvalds 		return 0;
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds 	len  = edge_buf_space_avail(eb);
28781da177e4SLinus Torvalds 	if (count > len)
28791da177e4SLinus Torvalds 		count = len;
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds 	if (count == 0)
28821da177e4SLinus Torvalds 		return 0;
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_put;
28851da177e4SLinus Torvalds 	if (count > len) {
28861da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, len);
28871da177e4SLinus Torvalds 		memcpy(eb->buf_buf, buf+len, count - len);
28881da177e4SLinus Torvalds 		eb->buf_put = eb->buf_buf + count - len;
28891da177e4SLinus Torvalds 	} else {
28901da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, count);
28911da177e4SLinus Torvalds 		if (count < len)
28921da177e4SLinus Torvalds 			eb->buf_put += count;
28931da177e4SLinus Torvalds 		else /* count == len */
28941da177e4SLinus Torvalds 			eb->buf_put = eb->buf_buf;
28951da177e4SLinus Torvalds 	}
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds 	return count;
28981da177e4SLinus Torvalds }
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds /*
29021da177e4SLinus Torvalds  * edge_buf_get
29031da177e4SLinus Torvalds  *
29041da177e4SLinus Torvalds  * Get data from the circular buffer and copy to the given buffer.
29051da177e4SLinus Torvalds  * Restrict to the amount of data available.
29061da177e4SLinus Torvalds  *
29071da177e4SLinus Torvalds  * Return the number of bytes copied.
29081da177e4SLinus Torvalds  */
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
29111da177e4SLinus Torvalds 	unsigned int count)
29121da177e4SLinus Torvalds {
29131da177e4SLinus Torvalds 	unsigned int len;
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 	if (eb == NULL)
29171da177e4SLinus Torvalds 		return 0;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	len = edge_buf_data_avail(eb);
29201da177e4SLinus Torvalds 	if (count > len)
29211da177e4SLinus Torvalds 		count = len;
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	if (count == 0)
29241da177e4SLinus Torvalds 		return 0;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_get;
29271da177e4SLinus Torvalds 	if (count > len) {
29281da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, len);
29291da177e4SLinus Torvalds 		memcpy(buf+len, eb->buf_buf, count - len);
29301da177e4SLinus Torvalds 		eb->buf_get = eb->buf_buf + count - len;
29311da177e4SLinus Torvalds 	} else {
29321da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, count);
29331da177e4SLinus Torvalds 		if (count < len)
29341da177e4SLinus Torvalds 			eb->buf_get += count;
29351da177e4SLinus Torvalds 		else /* count == len */
29361da177e4SLinus Torvalds 			eb->buf_get = eb->buf_buf;
29371da177e4SLinus Torvalds 	}
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	return count;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 
2943ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
294418fcac35SGreg Kroah-Hartman 	.driver = {
29451da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2946269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
294718fcac35SGreg Kroah-Hartman 	},
2948269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
2949d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
29501da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
29511da177e4SLinus Torvalds 	.num_ports		= 1,
29521da177e4SLinus Torvalds 	.open			= edge_open,
29531da177e4SLinus Torvalds 	.close			= edge_close,
29541da177e4SLinus Torvalds 	.throttle		= edge_throttle,
29551da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
29561da177e4SLinus Torvalds 	.attach			= edge_startup,
2957f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2958f9c99bb8SAlan Stern 	.release		= edge_release,
2959fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
29601da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
29611da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
29621da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
29631da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
29641da177e4SLinus Torvalds 	.write			= edge_write,
29651da177e4SLinus Torvalds 	.write_room		= edge_write_room,
29661da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
29671da177e4SLinus Torvalds 	.break_ctl		= edge_break,
29681da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
29691da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
29701da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
29711da177e4SLinus Torvalds };
29721da177e4SLinus Torvalds 
2973ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
297418fcac35SGreg Kroah-Hartman 	.driver = {
29751da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2976269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
297718fcac35SGreg Kroah-Hartman 	},
2978269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
2979d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
29801da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
29811da177e4SLinus Torvalds 	.num_ports		= 2,
29821da177e4SLinus Torvalds 	.open			= edge_open,
29831da177e4SLinus Torvalds 	.close			= edge_close,
29841da177e4SLinus Torvalds 	.throttle		= edge_throttle,
29851da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
29861da177e4SLinus Torvalds 	.attach			= edge_startup,
2987f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2988f9c99bb8SAlan Stern 	.release		= edge_release,
2989fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
29901da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
29911da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
29921da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
29931da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
29941da177e4SLinus Torvalds 	.write			= edge_write,
29951da177e4SLinus Torvalds 	.write_room		= edge_write_room,
29961da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
29971da177e4SLinus Torvalds 	.break_ctl		= edge_break,
29981da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
29991da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30001da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30011da177e4SLinus Torvalds };
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds static int __init edgeport_init(void)
30051da177e4SLinus Torvalds {
30061da177e4SLinus Torvalds 	int retval;
30071da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_1port_device);
30081da177e4SLinus Torvalds 	if (retval)
30091da177e4SLinus Torvalds 		goto failed_1port_device_register;
30101da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_2port_device);
30111da177e4SLinus Torvalds 	if (retval)
30121da177e4SLinus Torvalds 		goto failed_2port_device_register;
30131da177e4SLinus Torvalds 	retval = usb_register(&io_driver);
30141da177e4SLinus Torvalds 	if (retval)
30151da177e4SLinus Torvalds 		goto failed_usb_register;
3016c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3017c197a8dbSGreg Kroah-Hartman 	       DRIVER_DESC "\n");
30181da177e4SLinus Torvalds 	return 0;
30191da177e4SLinus Torvalds failed_usb_register:
30201da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_2port_device);
30211da177e4SLinus Torvalds failed_2port_device_register:
30221da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_1port_device);
30231da177e4SLinus Torvalds failed_1port_device_register:
30241da177e4SLinus Torvalds 	return retval;
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds static void __exit edgeport_exit(void)
30281da177e4SLinus Torvalds {
30291da177e4SLinus Torvalds 	usb_deregister(&io_driver);
30301da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_1port_device);
30311da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_2port_device);
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds module_init(edgeport_init);
30351da177e4SLinus Torvalds module_exit(edgeport_exit);
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds /* Module information */
30381da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
30391da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
30401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3041d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
30441da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
30471da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
30502742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev,
30512742fd88SAlan Cox 			"Ignore the cpu revision when connecting to a device");
30521da177e4SLinus Torvalds 
3053fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3054fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
3055