xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision 59d33f2fc2d63796296b1b76143e039d6e7cf532)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Edgeport USB Serial Converter driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
51da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *	the Free Software Foundation; either version 2 of the License, or
101da177e4SLinus Torvalds  *	(at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Supports the following devices:
131da177e4SLinus Torvalds  *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * For questions or problems with this driver, contact Inside Out
161da177e4SLinus Torvalds  * Networks technical support, or Peter Berger <pberger@brimson.com>,
171da177e4SLinus Torvalds  * or Al Borchers <alborchers@steinerpoint.com>.
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/jiffies.h>
221da177e4SLinus Torvalds #include <linux/errno.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/slab.h>
251da177e4SLinus Torvalds #include <linux/tty.h>
261da177e4SLinus Torvalds #include <linux/tty_driver.h>
271da177e4SLinus Torvalds #include <linux/tty_flip.h>
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/spinlock.h>
30241ca64fSMatthias Kaehlcke #include <linux/mutex.h>
311da177e4SLinus Torvalds #include <linux/serial.h>
32d733cec1SJohan Hovold #include <linux/kfifo.h>
331da177e4SLinus Torvalds #include <linux/ioctl.h>
34d12b219aSJaswinder Singh #include <linux/firmware.h>
352742fd88SAlan Cox #include <linux/uaccess.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
37a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include "io_16654.h"
401da177e4SLinus Torvalds #include "io_usbvend.h"
411da177e4SLinus Torvalds #include "io_ti.h"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * Version Information
451da177e4SLinus Torvalds  */
46fc4cbd75SMartin K. Petersen #define DRIVER_VERSION "v0.7mode043006"
471da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
481da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver"
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define EPROM_PAGE_SIZE		64
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* different hardware types */
541da177e4SLinus Torvalds #define HARDWARE_TYPE_930	0
551da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP	1
561da177e4SLinus Torvalds 
572742fd88SAlan Cox /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
582742fd88SAlan Cox #define	TI_MODE_CONFIGURING	0   /* Device has not entered start device */
592742fd88SAlan Cox #define	TI_MODE_BOOT		1   /* Staying in boot mode		   */
602742fd88SAlan Cox #define TI_MODE_DOWNLOAD	2   /* Made it to download mode		   */
612742fd88SAlan Cox #define TI_MODE_TRANSITIONING	3   /* Currently in boot mode but
622742fd88SAlan Cox 				       transitioning to download mode	   */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* read urb state */
651da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
661da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
671da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT	4000	/* in .01 sec */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #define EDGE_OUT_BUF_SIZE	1024
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Product information read from the Edgeport */
752742fd88SAlan Cox struct product_info {
762742fd88SAlan Cox 	int	TiMode;			/* Current TI Mode  */
772742fd88SAlan Cox 	__u8	hardware_type;		/* Type of hardware */
781da177e4SLinus Torvalds } __attribute__((packed));
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds struct edgeport_port {
811da177e4SLinus Torvalds 	__u16 uart_base;
821da177e4SLinus Torvalds 	__u16 dma_address;
831da177e4SLinus Torvalds 	__u8 shadow_msr;
841da177e4SLinus Torvalds 	__u8 shadow_mcr;
851da177e4SLinus Torvalds 	__u8 shadow_lsr;
861da177e4SLinus Torvalds 	__u8 lsr_mask;
8719af5cdbSMartin Olsson 	__u32 ump_read_timeout;		/* Number of milliseconds the UMP will
881da177e4SLinus Torvalds 					   wait without data before completing
891da177e4SLinus Torvalds 					   a read short */
901da177e4SLinus Torvalds 	int baud_rate;
911da177e4SLinus Torvalds 	int close_pending;
921da177e4SLinus Torvalds 	int lsr_event;
931da177e4SLinus Torvalds 	struct async_icount	icount;
941da177e4SLinus Torvalds 	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
951da177e4SLinus Torvalds 						   waiting for msr change to
961da177e4SLinus Torvalds 						   happen */
971da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
981da177e4SLinus Torvalds 	struct usb_serial_port	*port;
991da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
1001da177e4SLinus Torvalds 	spinlock_t ep_lock;
1011da177e4SLinus Torvalds 	int ep_read_urb_state;
1021da177e4SLinus Torvalds 	int ep_write_urb_in_use;
103d733cec1SJohan Hovold 	struct kfifo write_fifo;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds struct edgeport_serial {
1071da177e4SLinus Torvalds 	struct product_info product_info;
1082742fd88SAlan Cox 	u8 TI_I2C_Type;			/* Type of I2C in UMP */
1092742fd88SAlan Cox 	u8 TiReadI2C;			/* Set to TRUE if we have read the
1102742fd88SAlan Cox 					   I2c in Boot Mode */
111241ca64fSMatthias Kaehlcke 	struct mutex es_lock;
1121da177e4SLinus Torvalds 	int num_ports_open;
1131da177e4SLinus Torvalds 	struct usb_serial *serial;
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Devices that this driver supports */
1187d40d7e8SNémeth Márton static const struct usb_device_id edgeport_1port_id_table[] = {
1191da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1201da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1211da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1221da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1231da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1241da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1251da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1261da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1271da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1281da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1291da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1301da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1311da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1321da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1331da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1341da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1351da177e4SLinus Torvalds 	{ }
1361da177e4SLinus Torvalds };
1371da177e4SLinus Torvalds 
1387d40d7e8SNémeth Márton static const struct usb_device_id edgeport_2port_id_table[] = {
1391da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1401da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1411da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1421da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1431da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1441da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1451da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1461da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1471da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1481da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1491da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1501da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
151fc4cbd75SMartin K. Petersen 	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1521da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
153fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
154fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
155fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
156fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1571da177e4SLinus Torvalds 	{ }
1581da177e4SLinus Torvalds };
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* Devices that this driver supports */
1617d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
1621da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1631da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1641da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1651da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1661da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1671da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1681da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1691da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1701da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1711da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1721da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1731da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1741da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1751da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1761da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1781da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1791da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1801da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1811da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1821da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1831da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1841da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1851da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1861da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
191fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
192fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
193fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
194fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1951da177e4SLinus Torvalds 	{ }
1961da177e4SLinus Torvalds };
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
1991da177e4SLinus Torvalds 
200d12b219aSJaswinder Singh static unsigned char OperationalMajorVersion;
201d12b219aSJaswinder Singh static unsigned char OperationalMinorVersion;
202d12b219aSJaswinder Singh static unsigned short OperationalBuildNumber;
2031da177e4SLinus Torvalds 
20490ab5ee9SRusty Russell static bool debug;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
20790ab5ee9SRusty Russell static bool ignore_cpu_rev;
2082742fd88SAlan Cox static int default_uart_mode;		/* RS232 */
2091da177e4SLinus Torvalds 
2102742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2112742fd88SAlan Cox 			  unsigned char *data, int length);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2141da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2151da177e4SLinus Torvalds 
21695da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
21795da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
21895da310eSAlan Cox static void edge_send(struct tty_struct *tty);
2191da177e4SLinus Torvalds 
220fc4cbd75SMartin K. Petersen /* sysfs attributes */
221fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
222fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
223fc4cbd75SMartin K. Petersen 
2241da177e4SLinus Torvalds 
2252742fd88SAlan Cox static int ti_vread_sync(struct usb_device *dev, __u8 request,
2262742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	int status;
2291da177e4SLinus Torvalds 
2302742fd88SAlan Cox 	status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
2312742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
2322742fd88SAlan Cox 			value, index, data, size, 1000);
2331da177e4SLinus Torvalds 	if (status < 0)
2341da177e4SLinus Torvalds 		return status;
2351da177e4SLinus Torvalds 	if (status != size) {
23667e6da70SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
237441b62c1SHarvey Harrison 			__func__, size, status);
2381da177e4SLinus Torvalds 		return -ECOMM;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	return 0;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2432742fd88SAlan Cox static int ti_vsend_sync(struct usb_device *dev, __u8 request,
2442742fd88SAlan Cox 				__u16 value, __u16 index, u8 *data, int size)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	int status;
2471da177e4SLinus Torvalds 
2482742fd88SAlan Cox 	status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
2492742fd88SAlan Cox 			(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
2502742fd88SAlan Cox 			value, index, data, size, 1000);
2511da177e4SLinus Torvalds 	if (status < 0)
2521da177e4SLinus Torvalds 		return status;
2531da177e4SLinus Torvalds 	if (status != size) {
25467e6da70SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
255441b62c1SHarvey Harrison 			__func__, size, status);
2561da177e4SLinus Torvalds 		return -ECOMM;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 	return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2612742fd88SAlan Cox static int send_cmd(struct usb_device *dev, __u8 command,
2621da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
2631da177e4SLinus Torvalds 				int size)
2641da177e4SLinus Torvalds {
2652742fd88SAlan Cox 	return ti_vsend_sync(dev, command, value, moduleid, data, size);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
2692742fd88SAlan Cox static int purge_port(struct usb_serial_port *port, __u16 mask)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	int port_number = port->number - port->serial->minor;
2721da177e4SLinus Torvalds 
27367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
2741da177e4SLinus Torvalds 
2752742fd88SAlan Cox 	return send_cmd(port->serial->dev,
2761da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
2771da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
2781da177e4SLinus Torvalds 					mask,
2791da177e4SLinus Torvalds 					NULL,
2801da177e4SLinus Torvalds 					0);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /**
2842742fd88SAlan Cox  * read_download_mem - Read edgeport memory from TI chip
2851da177e4SLinus Torvalds  * @dev: usb device pointer
2861da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
2871da177e4SLinus Torvalds  * @length: Length of above data
2881da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
2891da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
2901da177e4SLinus Torvalds  */
2912742fd88SAlan Cox static int read_download_mem(struct usb_device *dev, int start_address,
2921da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	int status = 0;
2951da177e4SLinus Torvalds 	__u8 read_length;
2961da177e4SLinus Torvalds 	__be16 be_start_address;
2971da177e4SLinus Torvalds 
29867e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
3011da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3021da177e4SLinus Torvalds 	 */
3031da177e4SLinus Torvalds 	while (length) {
3041da177e4SLinus Torvalds 		if (length > 64)
3051da177e4SLinus Torvalds 			read_length = 64;
3061da177e4SLinus Torvalds 		else
3071da177e4SLinus Torvalds 			read_length = (__u8)length;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 		if (read_length > 1) {
31067e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 		be_start_address = cpu_to_be16(start_address);
3132742fd88SAlan Cox 		status = ti_vread_sync(dev, UMPC_MEMORY_READ,
3142742fd88SAlan Cox 					(__u16)address_type,
3152742fd88SAlan Cox 					(__force __u16)be_start_address,
3162742fd88SAlan Cox 					buffer, read_length);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 		if (status) {
31967e6da70SGreg Kroah-Hartman 			dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
3201da177e4SLinus Torvalds 			return status;
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 
3232742fd88SAlan Cox 		if (read_length > 1)
324*59d33f2fSGreg Kroah-Hartman 			usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		/* Update pointers/length */
3271da177e4SLinus Torvalds 		start_address += read_length;
3281da177e4SLinus Torvalds 		buffer += read_length;
3291da177e4SLinus Torvalds 		length -= read_length;
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	return status;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3352742fd88SAlan Cox static int read_ram(struct usb_device *dev, int start_address,
3362742fd88SAlan Cox 						int length, __u8 *buffer)
3371da177e4SLinus Torvalds {
3382742fd88SAlan Cox 	return read_download_mem(dev, start_address, length,
3392742fd88SAlan Cox 					DTK_ADDR_SPACE_XDATA, buffer);
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /* Read edgeport memory to a given block */
3432742fd88SAlan Cox static int read_boot_mem(struct edgeport_serial *serial,
3442742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	int status = 0;
3471da177e4SLinus Torvalds 	int i;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	for (i = 0; i < length; i++) {
3502742fd88SAlan Cox 		status = ti_vread_sync(serial->serial->dev,
3512742fd88SAlan Cox 				UMPC_MEMORY_READ, serial->TI_I2C_Type,
3522742fd88SAlan Cox 				(__u16)(start_address+i), &buffer[i], 0x01);
3531da177e4SLinus Torvalds 		if (status) {
35467e6da70SGreg Kroah-Hartman 			dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
3551da177e4SLinus Torvalds 			return status;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
35967e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
3602742fd88SAlan Cox 		__func__, start_address, length);
361*59d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	return status;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
3692742fd88SAlan Cox static int write_boot_mem(struct edgeport_serial *serial,
3702742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	int status = 0;
3731da177e4SLinus Torvalds 	int i;
374e9305d2fSJohan Hovold 	u8 *temp;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	/* Must do a read before write */
3771da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
378e9305d2fSJohan Hovold 		temp = kmalloc(1, GFP_KERNEL);
379e9305d2fSJohan Hovold 		if (!temp) {
380e9305d2fSJohan Hovold 			dev_err(&serial->serial->dev->dev,
381e9305d2fSJohan Hovold 					"%s - out of memory\n", __func__);
382e9305d2fSJohan Hovold 			return -ENOMEM;
383e9305d2fSJohan Hovold 		}
384e9305d2fSJohan Hovold 		status = read_boot_mem(serial, 0, 1, temp);
385e9305d2fSJohan Hovold 		kfree(temp);
3861da177e4SLinus Torvalds 		if (status)
3871da177e4SLinus Torvalds 			return status;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	for (i = 0; i < length; ++i) {
3912742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev,
3922742fd88SAlan Cox 				UMPC_MEMORY_WRITE, buffer[i],
3932742fd88SAlan Cox 				(__u16)(i + start_address), NULL, 0);
3941da177e4SLinus Torvalds 		if (status)
3951da177e4SLinus Torvalds 			return status;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
39867e6da70SGreg Kroah-Hartman 	dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
399*59d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	return status;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4062742fd88SAlan Cox static int write_i2c_mem(struct edgeport_serial *serial,
4072742fd88SAlan Cox 		int start_address, int length, __u8 address_type, __u8 *buffer)
4081da177e4SLinus Torvalds {
40967e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
4101da177e4SLinus Torvalds 	int status = 0;
4111da177e4SLinus Torvalds 	int write_length;
4121da177e4SLinus Torvalds 	__be16 be_start_address;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4151da177e4SLinus Torvalds 
41625985edcSLucas De Marchi 	/* calculate the number of bytes left in the first page */
4172742fd88SAlan Cox 	write_length = EPROM_PAGE_SIZE -
4182742fd88SAlan Cox 				(start_address & (EPROM_PAGE_SIZE - 1));
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (write_length > length)
4211da177e4SLinus Torvalds 		write_length = length;
4221da177e4SLinus Torvalds 
42367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
4242742fd88SAlan Cox 		__func__, start_address, write_length);
425*59d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, write_length, buffer);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* Write first page */
4281da177e4SLinus Torvalds 	be_start_address = cpu_to_be16(start_address);
4292742fd88SAlan Cox 	status = ti_vsend_sync(serial->serial->dev,
4302742fd88SAlan Cox 				UMPC_MEMORY_WRITE, (__u16)address_type,
4312742fd88SAlan Cox 				(__force __u16)be_start_address,
4322742fd88SAlan Cox 				buffer,	write_length);
4331da177e4SLinus Torvalds 	if (status) {
43467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
4351da177e4SLinus Torvalds 		return status;
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	length		-= write_length;
4391da177e4SLinus Torvalds 	start_address	+= write_length;
4401da177e4SLinus Torvalds 	buffer		+= write_length;
4411da177e4SLinus Torvalds 
4422742fd88SAlan Cox 	/* We should be aligned now -- can write
4432742fd88SAlan Cox 	   max page size bytes at a time */
4441da177e4SLinus Torvalds 	while (length) {
4451da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
4461da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
4471da177e4SLinus Torvalds 		else
4481da177e4SLinus Torvalds 			write_length = length;
4491da177e4SLinus Torvalds 
45067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
4512742fd88SAlan Cox 			__func__, start_address, write_length);
452*59d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, write_length, buffer);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 		/* Write next page */
4551da177e4SLinus Torvalds 		be_start_address = cpu_to_be16(start_address);
4562742fd88SAlan Cox 		status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
4572742fd88SAlan Cox 				(__u16)address_type,
4582742fd88SAlan Cox 				(__force __u16)be_start_address,
4592742fd88SAlan Cox 				buffer, write_length);
4601da177e4SLinus Torvalds 		if (status) {
46167e6da70SGreg Kroah-Hartman 			dev_err(dev, "%s - ERROR %d\n", __func__, status);
4621da177e4SLinus Torvalds 			return status;
4631da177e4SLinus Torvalds 		}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 		length		-= write_length;
4661da177e4SLinus Torvalds 		start_address	+= write_length;
4671da177e4SLinus Torvalds 		buffer		+= write_length;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 	return status;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
4731da177e4SLinus Torvalds  *
4741da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
4751da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
4761da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
4771da177e4SLinus Torvalds  */
4782742fd88SAlan Cox static int tx_active(struct edgeport_port *port)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	int status;
4811da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
4821da177e4SLinus Torvalds 	__u8 *lsr;
4831da177e4SLinus Torvalds 	int bytes_left = 0;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
4861da177e4SLinus Torvalds 	if (!oedb) {
487441b62c1SHarvey Harrison 		dev_err(&port->port->dev, "%s - out of memory\n", __func__);
4881da177e4SLinus Torvalds 		return -ENOMEM;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	lsr = kmalloc(1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
4921da177e4SLinus Torvalds 					   as not all platforms can do DMA
4931da177e4SLinus Torvalds 					   from stack */
4941da177e4SLinus Torvalds 	if (!lsr) {
4951da177e4SLinus Torvalds 		kfree(oedb);
4961da177e4SLinus Torvalds 		return -ENOMEM;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
4992742fd88SAlan Cox 	status = read_ram(port->port->serial->dev, port->dma_address,
5002742fd88SAlan Cox 						sizeof(*oedb), (void *)oedb);
5011da177e4SLinus Torvalds 	if (status)
5021da177e4SLinus Torvalds 		goto exit_is_tx_active;
5031da177e4SLinus Torvalds 
50467e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - XByteCount    0x%X\n", __func__, oedb->XByteCount);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	/* and the LSR */
5072742fd88SAlan Cox 	status = read_ram(port->port->serial->dev,
5082742fd88SAlan Cox 			port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (status)
5111da177e4SLinus Torvalds 		goto exit_is_tx_active;
51267e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5151da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80) != 0)
5161da177e4SLinus Torvalds 		bytes_left += 64;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
5191da177e4SLinus Torvalds 		bytes_left += 1;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5221da177e4SLinus Torvalds exit_is_tx_active:
52367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	kfree(lsr);
5261da177e4SLinus Torvalds 	kfree(oedb);
5271da177e4SLinus Torvalds 	return bytes_left;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5302742fd88SAlan Cox static void chase_port(struct edgeport_port *port, unsigned long timeout,
5312742fd88SAlan Cox 								int flush)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	int baud_rate;
5344a90f09bSAlan Cox 	struct tty_struct *tty = tty_port_tty_get(&port->port->port);
535af581059SJohan Hovold 	struct usb_serial *serial = port->port->serial;
5361da177e4SLinus Torvalds 	wait_queue_t wait;
5371da177e4SLinus Torvalds 	unsigned long flags;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (!timeout)
5401da177e4SLinus Torvalds 		timeout = (HZ * EDGE_CLOSING_WAIT)/100;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/* wait for data to drain from the buffer */
5431da177e4SLinus Torvalds 	spin_lock_irqsave(&port->ep_lock, flags);
5441da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
5451da177e4SLinus Torvalds 	add_wait_queue(&tty->write_wait, &wait);
5461da177e4SLinus Torvalds 	for (;;) {
5471da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
548d733cec1SJohan Hovold 		if (kfifo_len(&port->write_fifo) == 0
5491da177e4SLinus Torvalds 		|| timeout == 0 || signal_pending(current)
550af581059SJohan Hovold 		|| serial->disconnected)
5512742fd88SAlan Cox 			/* disconnect */
5521da177e4SLinus Torvalds 			break;
5531da177e4SLinus Torvalds 		spin_unlock_irqrestore(&port->ep_lock, flags);
5541da177e4SLinus Torvalds 		timeout = schedule_timeout(timeout);
5551da177e4SLinus Torvalds 		spin_lock_irqsave(&port->ep_lock, flags);
5561da177e4SLinus Torvalds 	}
5571da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
5581da177e4SLinus Torvalds 	remove_wait_queue(&tty->write_wait, &wait);
5591da177e4SLinus Torvalds 	if (flush)
560d733cec1SJohan Hovold 		kfifo_reset_out(&port->write_fifo);
5611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&port->ep_lock, flags);
5624a90f09bSAlan Cox 	tty_kref_put(tty);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* wait for data to drain from the device */
5651da177e4SLinus Torvalds 	timeout += jiffies;
5661da177e4SLinus Torvalds 	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
567af581059SJohan Hovold 						&& !serial->disconnected) {
5682742fd88SAlan Cox 		/* not disconnected */
5692742fd88SAlan Cox 		if (!tx_active(port))
5701da177e4SLinus Torvalds 			break;
5711da177e4SLinus Torvalds 		msleep(10);
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* disconnected */
575af581059SJohan Hovold 	if (serial->disconnected)
5761da177e4SLinus Torvalds 		return;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/* wait one more character time, based on baud rate */
5792742fd88SAlan Cox 	/* (tx_active doesn't seem to wait for the last byte) */
5802742fd88SAlan Cox 	baud_rate = port->baud_rate;
5812742fd88SAlan Cox 	if (baud_rate == 0)
5821da177e4SLinus Torvalds 		baud_rate = 50;
583dfa5ec79SJulia Lawall 	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5862742fd88SAlan Cox static int choose_config(struct usb_device *dev)
5871da177e4SLinus Torvalds {
5882742fd88SAlan Cox 	/*
5892742fd88SAlan Cox 	 * There may be multiple configurations on this device, in which case
5902742fd88SAlan Cox 	 * we would need to read and parse all of them to find out which one
5912742fd88SAlan Cox 	 * we want. However, we just support one config at this point,
5922742fd88SAlan Cox 	 * configuration # 1, which is Config Descriptor 0.
5932742fd88SAlan Cox 	 */
5941da177e4SLinus Torvalds 
59567e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
5962742fd88SAlan Cox 		__func__, dev->config->desc.bNumInterfaces);
59767e6da70SGreg Kroah-Hartman 	dev_dbg(&dev->dev, "%s - MAX Power            = %d\n",
5982742fd88SAlan Cox 		__func__, dev->config->desc.bMaxPower * 2);
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
60167e6da70SGreg Kroah-Hartman 		dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
6021da177e4SLinus Torvalds 		return -ENODEV;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return 0;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6082742fd88SAlan Cox static int read_rom(struct edgeport_serial *serial,
6092742fd88SAlan Cox 				int start_address, int length, __u8 *buffer)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	int status;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6142742fd88SAlan Cox 		status = read_download_mem(serial->serial->dev,
6151da177e4SLinus Torvalds 					       start_address,
6161da177e4SLinus Torvalds 					       length,
6171da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6181da177e4SLinus Torvalds 					       buffer);
6191da177e4SLinus Torvalds 	} else {
6202742fd88SAlan Cox 		status = read_boot_mem(serial, start_address, length,
6211da177e4SLinus Torvalds 								buffer);
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 	return status;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
6262742fd88SAlan Cox static int write_rom(struct edgeport_serial *serial, int start_address,
6272742fd88SAlan Cox 						int length, __u8 *buffer)
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6302742fd88SAlan Cox 		return write_boot_mem(serial, start_address, length,
6311da177e4SLinus Torvalds 								buffer);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
6342742fd88SAlan Cox 		return write_i2c_mem(serial, start_address, length,
6352742fd88SAlan Cox 						serial->TI_I2C_Type, buffer);
6361da177e4SLinus Torvalds 	return -EINVAL;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
6422742fd88SAlan Cox static int get_descriptor_addr(struct edgeport_serial *serial,
6432742fd88SAlan Cox 				int desc_type, struct ti_i2c_desc *rom_desc)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	int start_address;
6461da177e4SLinus Torvalds 	int status;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
6491da177e4SLinus Torvalds 	start_address = 2;
6501da177e4SLinus Torvalds 	do {
6512742fd88SAlan Cox 		status = read_rom(serial,
6521da177e4SLinus Torvalds 				   start_address,
6531da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
6541da177e4SLinus Torvalds 				   (__u8 *)rom_desc);
6551da177e4SLinus Torvalds 		if (status)
6561da177e4SLinus Torvalds 			return 0;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
6591da177e4SLinus Torvalds 			return start_address;
6601da177e4SLinus Torvalds 
6612742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc)
6622742fd88SAlan Cox 							+ rom_desc->Size;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	return 0;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /* Validate descriptor checksum */
6702742fd88SAlan Cox static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	__u16 i;
6731da177e4SLinus Torvalds 	__u8 cs = 0;
6741da177e4SLinus Torvalds 
6752742fd88SAlan Cox 	for (i = 0; i < rom_desc->Size; i++)
6761da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
6772742fd88SAlan Cox 
6781da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
67967e6da70SGreg Kroah-Hartman 		pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
6801da177e4SLinus Torvalds 		return -EINVAL;
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds 	return 0;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /* Make sure that the I2C image is good */
6862742fd88SAlan Cox static int check_i2c_image(struct edgeport_serial *serial)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
6891da177e4SLinus Torvalds 	int status = 0;
6901da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
6911da177e4SLinus Torvalds 	int start_address = 2;
6921da177e4SLinus Torvalds 	__u8 *buffer;
6931da177e4SLinus Torvalds 	__u16 ttype;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
6961da177e4SLinus Torvalds 	if (!rom_desc) {
697441b62c1SHarvey Harrison 		dev_err(dev, "%s - out of memory\n", __func__);
6981da177e4SLinus Torvalds 		return -ENOMEM;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
7011da177e4SLinus Torvalds 	if (!buffer) {
7022742fd88SAlan Cox 		dev_err(dev, "%s - out of memory when allocating buffer\n",
7032742fd88SAlan Cox 								__func__);
7041da177e4SLinus Torvalds 		kfree(rom_desc);
7051da177e4SLinus Torvalds 		return -ENOMEM;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7082742fd88SAlan Cox 	/* Read the first byte (Signature0) must be 0x52 or 0x10 */
7092742fd88SAlan Cox 	status = read_rom(serial, 0, 1, buffer);
7101da177e4SLinus Torvalds 	if (status)
7112742fd88SAlan Cox 		goto out;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
714441b62c1SHarvey Harrison 		dev_err(dev, "%s - invalid buffer signature\n", __func__);
7151da177e4SLinus Torvalds 		status = -ENODEV;
7162742fd88SAlan Cox 		goto out;
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	do {
7202742fd88SAlan Cox 		/* Validate the I2C */
7212742fd88SAlan Cox 		status = read_rom(serial,
7221da177e4SLinus Torvalds 				start_address,
7231da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7241da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7251da177e4SLinus Torvalds 		if (status)
7261da177e4SLinus Torvalds 			break;
7271da177e4SLinus Torvalds 
7282742fd88SAlan Cox 		if ((start_address + sizeof(struct ti_i2c_desc) +
7292742fd88SAlan Cox 					rom_desc->Size) > TI_MAX_I2C_SIZE) {
7301da177e4SLinus Torvalds 			status = -ENODEV;
73167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
7321da177e4SLinus Torvalds 			break;
7331da177e4SLinus Torvalds 		}
7341da177e4SLinus Torvalds 
73567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
7361da177e4SLinus Torvalds 
7372742fd88SAlan Cox 		/* Skip type 2 record */
7381da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
7391da177e4SLinus Torvalds 		if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
7401da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
7412742fd88SAlan Cox 			/* Read the descriptor data */
7422742fd88SAlan Cox 			status = read_rom(serial, start_address +
7432742fd88SAlan Cox 						sizeof(struct ti_i2c_desc),
7442742fd88SAlan Cox 						rom_desc->Size, buffer);
7451da177e4SLinus Torvalds 			if (status)
7461da177e4SLinus Torvalds 				break;
7471da177e4SLinus Torvalds 
7482742fd88SAlan Cox 			status = valid_csum(rom_desc, buffer);
7491da177e4SLinus Torvalds 			if (status)
7501da177e4SLinus Torvalds 				break;
7511da177e4SLinus Torvalds 		}
7522742fd88SAlan Cox 		start_address = start_address + sizeof(struct ti_i2c_desc) +
7532742fd88SAlan Cox 								rom_desc->Size;
7541da177e4SLinus Torvalds 
7552742fd88SAlan Cox 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
7562742fd88SAlan Cox 				(start_address < TI_MAX_I2C_SIZE));
7571da177e4SLinus Torvalds 
7582742fd88SAlan Cox 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
7592742fd88SAlan Cox 				(start_address > TI_MAX_I2C_SIZE))
7601da177e4SLinus Torvalds 		status = -ENODEV;
7611da177e4SLinus Torvalds 
7622742fd88SAlan Cox out:
7631da177e4SLinus Torvalds 	kfree(buffer);
7641da177e4SLinus Torvalds 	kfree(rom_desc);
7651da177e4SLinus Torvalds 	return status;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7682742fd88SAlan Cox static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	int status;
7711da177e4SLinus Torvalds 	int start_address;
7721da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7731da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
77467e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
7771da177e4SLinus Torvalds 	if (!rom_desc) {
77867e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - out of memory\n", __func__);
7791da177e4SLinus Torvalds 		return -ENOMEM;
7801da177e4SLinus Torvalds 	}
7812742fd88SAlan Cox 	start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
7822742fd88SAlan Cox 								rom_desc);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	if (!start_address) {
78567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
7861da177e4SLinus Torvalds 		status = -ENODEV;
7871da177e4SLinus Torvalds 		goto exit;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
7902742fd88SAlan Cox 	/* Read the descriptor data */
7912742fd88SAlan Cox 	status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
7922742fd88SAlan Cox 						rom_desc->Size, buffer);
7931da177e4SLinus Torvalds 	if (status)
7941da177e4SLinus Torvalds 		goto exit;
7951da177e4SLinus Torvalds 
7962742fd88SAlan Cox 	status = valid_csum(rom_desc, buffer);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
79967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - IonConfig      0x%x\n", __func__, desc->IonConfig);
80067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Version          %d\n", __func__, desc->Version);
80167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Cpu/Board      0x%x\n", __func__, desc->CpuRev_BoardRev);
80267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumPorts         %d\n", __func__, desc->NumPorts);
80367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - NumVirtualPorts  %d\n", __func__, desc->NumVirtualPorts);
80467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - TotalPorts       %d\n", __func__, desc->TotalPorts);
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds exit:
8071da177e4SLinus Torvalds 	kfree(rom_desc);
8081da177e4SLinus Torvalds 	return status;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds /* Build firmware header used for firmware update */
8122742fd88SAlan Cox static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	__u8 *buffer;
8151da177e4SLinus Torvalds 	int buffer_size;
8161da177e4SLinus Torvalds 	int i;
817d12b219aSJaswinder Singh 	int err;
8181da177e4SLinus Torvalds 	__u8 cs = 0;
8191da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8201da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8211da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
822d12b219aSJaswinder Singh 	const struct firmware *fw;
823d12b219aSJaswinder Singh 	const char *fw_name = "edgeport/down3.bin";
8241da177e4SLinus Torvalds 
8252742fd88SAlan Cox 	/* In order to update the I2C firmware we must change the type 2 record
8262742fd88SAlan Cox 	 * to type 0xF2.  This will force the UMP to come up in Boot Mode.
8272742fd88SAlan Cox 	 * Then while in boot mode, the driver will download the latest
8282742fd88SAlan Cox 	 * firmware (padded to 15.5k) into the UMP ram.  And finally when the
8292742fd88SAlan Cox 	 * device comes back up in download mode the driver will cause the new
8302742fd88SAlan Cox 	 * firmware to be copied from the UMP Ram to I2C and the firmware will
8312742fd88SAlan Cox 	 * update the record type from 0xf2 to 0x02.
8322742fd88SAlan Cox 	 */
8331da177e4SLinus Torvalds 
8342742fd88SAlan Cox 	/* Allocate a 15.5k buffer + 2 bytes for version number
8352742fd88SAlan Cox 	 * (Firmware Record) */
8362742fd88SAlan Cox 	buffer_size = (((1024 * 16) - 512 ) +
8372742fd88SAlan Cox 			sizeof(struct ti_i2c_firmware_rec));
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	buffer = kmalloc(buffer_size, GFP_KERNEL);
8401da177e4SLinus Torvalds 	if (!buffer) {
841441b62c1SHarvey Harrison 		dev_err(dev, "%s - out of memory\n", __func__);
8421da177e4SLinus Torvalds 		return -ENOMEM;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	// Set entire image of 0xffs
8461da177e4SLinus Torvalds 	memset(buffer, 0xff, buffer_size);
8471da177e4SLinus Torvalds 
848d12b219aSJaswinder Singh 	err = request_firmware(&fw, fw_name, dev);
849d12b219aSJaswinder Singh 	if (err) {
850d12b219aSJaswinder Singh 		printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
851d12b219aSJaswinder Singh 		       fw_name, err);
852d12b219aSJaswinder Singh 		kfree(buffer);
853d12b219aSJaswinder Singh 		return err;
854d12b219aSJaswinder Singh 	}
855d12b219aSJaswinder Singh 
856d12b219aSJaswinder Singh 	/* Save Download Version Number */
857d12b219aSJaswinder Singh 	OperationalMajorVersion = fw->data[0];
858d12b219aSJaswinder Singh 	OperationalMinorVersion = fw->data[1];
859d12b219aSJaswinder Singh 	OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
860d12b219aSJaswinder Singh 
8612742fd88SAlan Cox 	/* Copy version number into firmware record */
8621da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
8631da177e4SLinus Torvalds 
864d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
865d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
8661da177e4SLinus Torvalds 
8672742fd88SAlan Cox 	/* Pointer to fw_down memory image */
868d12b219aSJaswinder Singh 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
871d12b219aSJaswinder Singh 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
8721da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
8731da177e4SLinus Torvalds 
874d12b219aSJaswinder Singh 	release_firmware(fw);
875d12b219aSJaswinder Singh 
8761da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
8771da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	kfree(buffer);
8811da177e4SLinus Torvalds 
8822742fd88SAlan Cox 	/* Build new header */
8831da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
8841da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
8871da177e4SLinus Torvalds 	i2c_header->Size	= (__u16)buffer_size;
8881da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
889d12b219aSJaswinder Singh 	firmware_rec->Ver_Major	= OperationalMajorVersion;
890d12b219aSJaswinder Singh 	firmware_rec->Ver_Minor	= OperationalMinorVersion;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	return 0;
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
8962742fd88SAlan Cox static int i2c_type_bootmode(struct edgeport_serial *serial)
8971da177e4SLinus Torvalds {
89867e6da70SGreg Kroah-Hartman 	struct device *dev = &serial->serial->dev->dev;
8991da177e4SLinus Torvalds 	int status;
900e9305d2fSJohan Hovold 	u8 *data;
901e9305d2fSJohan Hovold 
902e9305d2fSJohan Hovold 	data = kmalloc(1, GFP_KERNEL);
903e9305d2fSJohan Hovold 	if (!data) {
90467e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - out of memory\n", __func__);
905e9305d2fSJohan Hovold 		return -ENOMEM;
906e9305d2fSJohan Hovold 	}
9071da177e4SLinus Torvalds 
9082742fd88SAlan Cox 	/* Try to read type 2 */
9092742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
910e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
9111da177e4SLinus Torvalds 	if (status)
91267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
9131da177e4SLinus Torvalds 	else
91467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
915e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
91667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
9171da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
918e9305d2fSJohan Hovold 		goto out;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9212742fd88SAlan Cox 	/* Try to read type 3 */
9222742fd88SAlan Cox 	status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
923e9305d2fSJohan Hovold 				DTK_ADDR_SPACE_I2C_TYPE_III, 0,	data, 0x01);
9241da177e4SLinus Torvalds 	if (status)
92567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
9261da177e4SLinus Torvalds 	else
92767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
928e9305d2fSJohan Hovold 	if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
92967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
9301da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
931e9305d2fSJohan Hovold 		goto out;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 
93467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - Unknown\n", __func__);
9351da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
936e9305d2fSJohan Hovold 	status = -ENODEV;
937e9305d2fSJohan Hovold out:
938e9305d2fSJohan Hovold 	kfree(data);
939e9305d2fSJohan Hovold 	return status;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9422742fd88SAlan Cox static int bulk_xfer(struct usb_serial *serial, void *buffer,
9432742fd88SAlan Cox 						int length, int *num_sent)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	int status;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	status = usb_bulk_msg(serial->dev,
9481da177e4SLinus Torvalds 			usb_sndbulkpipe(serial->dev,
9491da177e4SLinus Torvalds 				serial->port[0]->bulk_out_endpointAddress),
9502742fd88SAlan Cox 			buffer, length, num_sent, 1000);
9511da177e4SLinus Torvalds 	return status;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
9552742fd88SAlan Cox static int download_code(struct edgeport_serial *serial, __u8 *image,
9562742fd88SAlan Cox 							int image_length)
9571da177e4SLinus Torvalds {
9581da177e4SLinus Torvalds 	int status = 0;
9591da177e4SLinus Torvalds 	int pos;
9601da177e4SLinus Torvalds 	int transfer;
9611da177e4SLinus Torvalds 	int done;
9621da177e4SLinus Torvalds 
9632742fd88SAlan Cox 	/* Transfer firmware image */
9641da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
9652742fd88SAlan Cox 		/* Read the next buffer from file */
9661da177e4SLinus Torvalds 		transfer = image_length - pos;
9671da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
9681da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
9691da177e4SLinus Torvalds 
9702742fd88SAlan Cox 		/* Transfer data */
9712742fd88SAlan Cox 		status = bulk_xfer(serial->serial, &image[pos],
9722742fd88SAlan Cox 							transfer, &done);
9731da177e4SLinus Torvalds 		if (status)
9741da177e4SLinus Torvalds 			break;
9752742fd88SAlan Cox 		/* Advance buffer pointer */
9761da177e4SLinus Torvalds 		pos += done;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	return status;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9822742fd88SAlan Cox /* FIXME!!! */
9832742fd88SAlan Cox static int config_boot_dev(struct usb_device *dev)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	return 0;
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9882742fd88SAlan Cox static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
9892742fd88SAlan Cox {
9902742fd88SAlan Cox 	return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
9912742fd88SAlan Cox }
9922742fd88SAlan Cox 
9931da177e4SLinus Torvalds /**
9941da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
9951da177e4SLinus Torvalds  *
9961da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
9971da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
9981da177e4SLinus Torvalds  */
9992742fd88SAlan Cox static int download_fw(struct edgeport_serial *serial)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
10021da177e4SLinus Torvalds 	int status = 0;
10031da177e4SLinus Torvalds 	int start_address;
10041da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
10051da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
10061da177e4SLinus Torvalds 	int download_cur_ver;
10071da177e4SLinus Torvalds 	int download_new_ver;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
10101da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10111da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10121da177e4SLinus Torvalds 	 */
10131da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10161da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10171da177e4SLinus Torvalds 
10182742fd88SAlan Cox 	status = choose_config(serial->serial->dev);
10191da177e4SLinus Torvalds 	if (status)
10201da177e4SLinus Torvalds 		return status;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10231da177e4SLinus Torvalds 	if (!interface) {
1024441b62c1SHarvey Harrison 		dev_err(dev, "%s - no interface set, error!\n", __func__);
10251da177e4SLinus Torvalds 		return -ENODEV;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 
10282742fd88SAlan Cox 	/*
10292742fd88SAlan Cox 	 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10302742fd88SAlan Cox 	 * if we have more than one endpoint we are definitely in download
10312742fd88SAlan Cox 	 * mode
10322742fd88SAlan Cox 	 */
10331da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
10341da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
10351da177e4SLinus Torvalds 	else
10362742fd88SAlan Cox 		/* Otherwise we will remain in configuring mode */
10371da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/********************************************************************/
10401da177e4SLinus Torvalds 	/* Download Mode */
10411da177e4SLinus Torvalds 	/********************************************************************/
10421da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
10431da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
10441da177e4SLinus Torvalds 
104567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
10461da177e4SLinus Torvalds 
10472742fd88SAlan Cox 		status = check_i2c_image(serial);
10481da177e4SLinus Torvalds 		if (status) {
104967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
10501da177e4SLinus Torvalds 			return status;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		/* Validate Hardware version number
10541da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
10551da177e4SLinus Torvalds 		 */
10561da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
10571da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1058441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
10591da177e4SLinus Torvalds 			return -ENOMEM;
10601da177e4SLinus Torvalds 		}
10612742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
10621da177e4SLinus Torvalds 		if (status) {
10631da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
10641da177e4SLinus Torvalds 			return status;
10651da177e4SLinus Torvalds 		}
10661da177e4SLinus Torvalds 
10672742fd88SAlan Cox 		/* Check version number of ION descriptor */
10682742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
106967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
10702742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
10711da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
10721da177e4SLinus Torvalds 			return -EINVAL;
10731da177e4SLinus Torvalds   		}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 		rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
10761da177e4SLinus Torvalds 		if (!rom_desc) {
1077441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
10781da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
10791da177e4SLinus Torvalds 			return -ENOMEM;
10801da177e4SLinus Torvalds 		}
10811da177e4SLinus Torvalds 
10822742fd88SAlan Cox 		/* Search for type 2 record (firmware record) */
10832742fd88SAlan Cox 		start_address = get_descriptor_addr(serial,
10842742fd88SAlan Cox 				I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
10852742fd88SAlan Cox 		if (start_address != 0) {
10861da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
1087e9305d2fSJohan Hovold 			u8 *record;
10881da177e4SLinus Torvalds 
108967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
10901da177e4SLinus Torvalds 
10912742fd88SAlan Cox 			firmware_version = kmalloc(sizeof(*firmware_version),
10922742fd88SAlan Cox 								GFP_KERNEL);
10931da177e4SLinus Torvalds 			if (!firmware_version) {
1094441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
10951da177e4SLinus Torvalds 				kfree(rom_desc);
10961da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
10971da177e4SLinus Torvalds 				return -ENOMEM;
10981da177e4SLinus Torvalds 			}
10991da177e4SLinus Torvalds 
11002742fd88SAlan Cox 			/* Validate version number
11012742fd88SAlan Cox 			 * Read the descriptor data
11022742fd88SAlan Cox 			 */
11032742fd88SAlan Cox 			status = read_rom(serial, start_address +
11042742fd88SAlan Cox 					sizeof(struct ti_i2c_desc),
11051da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
11061da177e4SLinus Torvalds 					(__u8 *)firmware_version);
11071da177e4SLinus Torvalds 			if (status) {
11081da177e4SLinus Torvalds 				kfree(firmware_version);
11091da177e4SLinus Torvalds 				kfree(rom_desc);
11101da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
11111da177e4SLinus Torvalds 				return status;
11121da177e4SLinus Torvalds 			}
11131da177e4SLinus Torvalds 
11142742fd88SAlan Cox 			/* Check version number of download with current
11152742fd88SAlan Cox 			   version in I2c */
11161da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
11171da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
1118d12b219aSJaswinder Singh 			download_new_ver = (OperationalMajorVersion << 8) +
1119d12b219aSJaswinder Singh 					   (OperationalMinorVersion);
11201da177e4SLinus Torvalds 
112167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - >> FW Versions Device %d.%d  Driver %d.%d\n",
112267e6da70SGreg Kroah-Hartman 				__func__, firmware_version->Ver_Major,
11231da177e4SLinus Torvalds 				firmware_version->Ver_Minor,
1124d12b219aSJaswinder Singh 				OperationalMajorVersion,
1125d12b219aSJaswinder Singh 				OperationalMinorVersion);
11261da177e4SLinus Torvalds 
11272742fd88SAlan Cox 			/* Check if we have an old version in the I2C and
11282742fd88SAlan Cox 			   update if necessary */
11290827a9ffSGreg Kroah-Hartman 			if (download_cur_ver < download_new_ver) {
113067e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1131441b62c1SHarvey Harrison 					__func__,
11321da177e4SLinus Torvalds 					firmware_version->Ver_Major,
11331da177e4SLinus Torvalds 					firmware_version->Ver_Minor,
1134d12b219aSJaswinder Singh 					OperationalMajorVersion,
1135d12b219aSJaswinder Singh 					OperationalMinorVersion);
11361da177e4SLinus Torvalds 
1137e9305d2fSJohan Hovold 				record = kmalloc(1, GFP_KERNEL);
1138e9305d2fSJohan Hovold 				if (!record) {
1139e9305d2fSJohan Hovold 					dev_err(dev, "%s - out of memory.\n",
1140e9305d2fSJohan Hovold 							__func__);
1141e9305d2fSJohan Hovold 					kfree(firmware_version);
1142e9305d2fSJohan Hovold 					kfree(rom_desc);
1143e9305d2fSJohan Hovold 					kfree(ti_manuf_desc);
1144e9305d2fSJohan Hovold 					return -ENOMEM;
1145e9305d2fSJohan Hovold 				}
11462742fd88SAlan Cox 				/* In order to update the I2C firmware we must
11472742fd88SAlan Cox 				 * change the type 2 record to type 0xF2. This
11482742fd88SAlan Cox 				 * will force the UMP to come up in Boot Mode.
11492742fd88SAlan Cox 				 * Then while in boot mode, the driver will
11502742fd88SAlan Cox 				 * download the latest firmware (padded to
11512742fd88SAlan Cox 				 * 15.5k) into the UMP ram. Finally when the
11522742fd88SAlan Cox 				 * device comes back up in download mode the
11532742fd88SAlan Cox 				 * driver will cause the new firmware to be
11542742fd88SAlan Cox 				 * copied from the UMP Ram to I2C and the
11552742fd88SAlan Cox 				 * firmware will update the record type from
11562742fd88SAlan Cox 				 * 0xf2 to 0x02.
11572742fd88SAlan Cox 				 */
1158e9305d2fSJohan Hovold 				*record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11591da177e4SLinus Torvalds 
11602742fd88SAlan Cox 				/* Change the I2C Firmware record type to
11612742fd88SAlan Cox 				   0xf2 to trigger an update */
11622742fd88SAlan Cox 				status = write_rom(serial, start_address,
1163e9305d2fSJohan Hovold 						sizeof(*record), record);
11641da177e4SLinus Torvalds 				if (status) {
1165e9305d2fSJohan Hovold 					kfree(record);
11661da177e4SLinus Torvalds 					kfree(firmware_version);
11671da177e4SLinus Torvalds 					kfree(rom_desc);
11681da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
11691da177e4SLinus Torvalds 					return status;
11701da177e4SLinus Torvalds 				}
11711da177e4SLinus Torvalds 
11722742fd88SAlan Cox 				/* verify the write -- must do this in order
11732742fd88SAlan Cox 				 * for write to complete before we do the
11742742fd88SAlan Cox 				 * hardware reset
11752742fd88SAlan Cox 				 */
11762742fd88SAlan Cox 				status = read_rom(serial,
11771da177e4SLinus Torvalds 							start_address,
1178e9305d2fSJohan Hovold 							sizeof(*record),
1179e9305d2fSJohan Hovold 							record);
11801da177e4SLinus Torvalds 				if (status) {
1181e9305d2fSJohan Hovold 					kfree(record);
11821da177e4SLinus Torvalds 					kfree(firmware_version);
11831da177e4SLinus Torvalds 					kfree(rom_desc);
11841da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
11851da177e4SLinus Torvalds 					return status;
11861da177e4SLinus Torvalds 				}
11871da177e4SLinus Torvalds 
1188e9305d2fSJohan Hovold 				if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
118967e6da70SGreg Kroah-Hartman 					dev_err(dev, "%s - error resetting device\n", __func__);
1190e9305d2fSJohan Hovold 					kfree(record);
11911da177e4SLinus Torvalds 					kfree(firmware_version);
11921da177e4SLinus Torvalds 					kfree(rom_desc);
11931da177e4SLinus Torvalds 					kfree(ti_manuf_desc);
11941da177e4SLinus Torvalds 					return -ENODEV;
11951da177e4SLinus Torvalds 				}
11961da177e4SLinus Torvalds 
119767e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
11981da177e4SLinus Torvalds 
11992742fd88SAlan Cox 				/* Reset UMP -- Back to BOOT MODE */
12002742fd88SAlan Cox 				status = ti_vsend_sync(serial->serial->dev,
12012742fd88SAlan Cox 						UMPC_HARDWARE_RESET,
12022742fd88SAlan Cox 						0, 0, NULL, 0);
12031da177e4SLinus Torvalds 
120467e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 				/* return an error on purpose. */
1207e9305d2fSJohan Hovold 				kfree(record);
12081da177e4SLinus Torvalds 				kfree(firmware_version);
12091da177e4SLinus Torvalds 				kfree(rom_desc);
12101da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12111da177e4SLinus Torvalds 				return -ENODEV;
12121da177e4SLinus Torvalds 			}
12131da177e4SLinus Torvalds 			kfree(firmware_version);
12141da177e4SLinus Torvalds 		}
12152742fd88SAlan Cox 		/* Search for type 0xF2 record (firmware blank record) */
12162742fd88SAlan Cox 		else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
12172742fd88SAlan Cox #define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + \
12182742fd88SAlan Cox 					sizeof(struct ti_i2c_firmware_rec))
12191da177e4SLinus Torvalds 			__u8 *header;
12201da177e4SLinus Torvalds 			__u8 *vheader;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 			header = kmalloc(HEADER_SIZE, GFP_KERNEL);
12231da177e4SLinus Torvalds 			if (!header) {
1224441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
12251da177e4SLinus Torvalds 				kfree(rom_desc);
12261da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12271da177e4SLinus Torvalds 				return -ENOMEM;
12281da177e4SLinus Torvalds 			}
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 			vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
12311da177e4SLinus Torvalds 			if (!vheader) {
1232441b62c1SHarvey Harrison 				dev_err(dev, "%s - out of memory.\n", __func__);
12331da177e4SLinus Torvalds 				kfree(header);
12341da177e4SLinus Torvalds 				kfree(rom_desc);
12351da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12361da177e4SLinus Torvalds 				return -ENOMEM;
12371da177e4SLinus Torvalds 			}
12381da177e4SLinus Torvalds 
123967e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
12401da177e4SLinus Torvalds 
12412742fd88SAlan Cox 			/*
12422742fd88SAlan Cox 			 * In order to update the I2C firmware we must change
12432742fd88SAlan Cox 			 * the type 2 record to type 0xF2. This will force the
12442742fd88SAlan Cox 			 * UMP to come up in Boot Mode.  Then while in boot
12452742fd88SAlan Cox 			 * mode, the driver will download the latest firmware
12462742fd88SAlan Cox 			 * (padded to 15.5k) into the UMP ram. Finally when the
12472742fd88SAlan Cox 			 * device comes back up in download mode the driver
12482742fd88SAlan Cox 			 * will cause the new firmware to be copied from the
12492742fd88SAlan Cox 			 * UMP Ram to I2C and the firmware will update the
12502742fd88SAlan Cox 			 * record type from 0xf2 to 0x02.
12512742fd88SAlan Cox 			 */
12522742fd88SAlan Cox 			status = build_i2c_fw_hdr(header, dev);
12531da177e4SLinus Torvalds 			if (status) {
12541da177e4SLinus Torvalds 				kfree(vheader);
12551da177e4SLinus Torvalds 				kfree(header);
12561da177e4SLinus Torvalds 				kfree(rom_desc);
12571da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
1258fd6e5bbbSRoel Kluin 				return -EINVAL;
12591da177e4SLinus Torvalds 			}
12601da177e4SLinus Torvalds 
12612742fd88SAlan Cox 			/* Update I2C with type 0xf2 record with correct
12622742fd88SAlan Cox 			   size and checksum */
12632742fd88SAlan Cox 			status = write_rom(serial,
12641da177e4SLinus Torvalds 						start_address,
12651da177e4SLinus Torvalds 						HEADER_SIZE,
12661da177e4SLinus Torvalds 						header);
12671da177e4SLinus Torvalds 			if (status) {
12681da177e4SLinus Torvalds 				kfree(vheader);
12691da177e4SLinus Torvalds 				kfree(header);
12701da177e4SLinus Torvalds 				kfree(rom_desc);
12711da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12729800eb33SRoel Kluin 				return -EINVAL;
12731da177e4SLinus Torvalds 			}
12741da177e4SLinus Torvalds 
12752742fd88SAlan Cox 			/* verify the write -- must do this in order for
12762742fd88SAlan Cox 			   write to complete before we do the hardware reset */
12772742fd88SAlan Cox 			status = read_rom(serial, start_address,
12782742fd88SAlan Cox 							HEADER_SIZE, vheader);
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 			if (status) {
128167e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - can't read header back\n", __func__);
12821da177e4SLinus Torvalds 				kfree(vheader);
12831da177e4SLinus Torvalds 				kfree(header);
12841da177e4SLinus Torvalds 				kfree(rom_desc);
12851da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12861da177e4SLinus Torvalds 				return status;
12871da177e4SLinus Torvalds 			}
12881da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
128967e6da70SGreg Kroah-Hartman 				dev_dbg(dev, "%s - write download record failed\n", __func__);
12901da177e4SLinus Torvalds 				kfree(vheader);
12911da177e4SLinus Torvalds 				kfree(header);
12921da177e4SLinus Torvalds 				kfree(rom_desc);
12931da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
12941e29709eSRoel Kluin 				return -EINVAL;
12951da177e4SLinus Torvalds 			}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 			kfree(vheader);
12981da177e4SLinus Torvalds 			kfree(header);
12991da177e4SLinus Torvalds 
130067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Start firmware update\n", __func__);
13011da177e4SLinus Torvalds 
13022742fd88SAlan Cox 			/* Tell firmware to copy download image into I2C */
13032742fd88SAlan Cox 			status = ti_vsend_sync(serial->serial->dev,
13042742fd88SAlan Cox 					UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
13051da177e4SLinus Torvalds 
130667e6da70SGreg Kroah-Hartman 		  	dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
13071da177e4SLinus Torvalds 			if (status) {
13082742fd88SAlan Cox 				dev_err(dev,
13092742fd88SAlan Cox 					"%s - UMPC_COPY_DNLD_TO_I2C failed\n",
13102742fd88SAlan Cox 								__func__);
13111da177e4SLinus Torvalds 				kfree(rom_desc);
13121da177e4SLinus Torvalds 				kfree(ti_manuf_desc);
13131da177e4SLinus Torvalds 				return status;
13141da177e4SLinus Torvalds 			}
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		// The device is running the download code
13181da177e4SLinus Torvalds 		kfree(rom_desc);
13191da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
13201da177e4SLinus Torvalds 		return 0;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	/********************************************************************/
13241da177e4SLinus Torvalds 	/* Boot Mode */
13251da177e4SLinus Torvalds 	/********************************************************************/
132667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
13271da177e4SLinus Torvalds 
13282742fd88SAlan Cox 	/* Configure the TI device so we can use the BULK pipes for download */
13292742fd88SAlan Cox 	status = config_boot_dev(serial->serial->dev);
13301da177e4SLinus Torvalds 	if (status)
13311da177e4SLinus Torvalds 		return status;
13321da177e4SLinus Torvalds 
13332742fd88SAlan Cox 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
13342742fd88SAlan Cox 							!= USB_VENDOR_ID_ION) {
133567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
13361da177e4SLinus Torvalds 			le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13371da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13382742fd88SAlan Cox 		goto stayinbootmode;
13391da177e4SLinus Torvalds 	}
13401da177e4SLinus Torvalds 
13412742fd88SAlan Cox 	/* We have an ION device (I2c Must be programmed)
13422742fd88SAlan Cox 	   Determine I2C image type */
13432742fd88SAlan Cox 	if (i2c_type_bootmode(serial))
13442742fd88SAlan Cox 		goto stayinbootmode;
13451da177e4SLinus Torvalds 
13462742fd88SAlan Cox 	/* Check for ION Vendor ID and that the I2C is valid */
13472742fd88SAlan Cox 	if (!check_i2c_image(serial)) {
13481da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
13491da177e4SLinus Torvalds 		int i;
13501da177e4SLinus Torvalds 		__u8 cs = 0;
13511da177e4SLinus Torvalds 		__u8 *buffer;
13521da177e4SLinus Torvalds 		int buffer_size;
1353d12b219aSJaswinder Singh 		int err;
1354d12b219aSJaswinder Singh 		const struct firmware *fw;
1355d12b219aSJaswinder Singh 		const char *fw_name = "edgeport/down3.bin";
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 		/* Validate Hardware version number
13581da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
13591da177e4SLinus Torvalds 		 */
13601da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
13611da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1362441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory.\n", __func__);
13631da177e4SLinus Torvalds 			return -ENOMEM;
13641da177e4SLinus Torvalds 		}
13652742fd88SAlan Cox 		status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
13661da177e4SLinus Torvalds 		if (status) {
13671da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
13682742fd88SAlan Cox 			goto stayinbootmode;
13691da177e4SLinus Torvalds 		}
13701da177e4SLinus Torvalds 
13712742fd88SAlan Cox 		/* Check for version 2 */
13722742fd88SAlan Cox 		if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
137367e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
13742742fd88SAlan Cox 				__func__, ti_cpu_rev(ti_manuf_desc));
13751da177e4SLinus Torvalds 			kfree(ti_manuf_desc);
13762742fd88SAlan Cox 			goto stayinbootmode;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 		kfree(ti_manuf_desc);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		/*
13822742fd88SAlan Cox 		 * In order to update the I2C firmware we must change the type
13832742fd88SAlan Cox 		 * 2 record to type 0xF2. This will force the UMP to come up
13842742fd88SAlan Cox 		 * in Boot Mode.  Then while in boot mode, the driver will
13852742fd88SAlan Cox 		 * download the latest firmware (padded to 15.5k) into the
13862742fd88SAlan Cox 		 * UMP ram. Finally when the device comes back up in download
13872742fd88SAlan Cox 		 * mode the driver will cause the new firmware to be copied
13882742fd88SAlan Cox 		 * from the UMP Ram to I2C and the firmware will update the
13892742fd88SAlan Cox 		 * record type from 0xf2 to 0x02.
13902742fd88SAlan Cox 		 *
13911da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
13921da177e4SLinus Torvalds 		 * or could we do this in place!
13931da177e4SLinus Torvalds 		 */
13941da177e4SLinus Torvalds 
13952742fd88SAlan Cox 		/* Allocate a 15.5k buffer + 3 byte header */
13962742fd88SAlan Cox 		buffer_size = (((1024 * 16) - 512) +
13972742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header));
13981da177e4SLinus Torvalds 		buffer = kmalloc(buffer_size, GFP_KERNEL);
13991da177e4SLinus Torvalds 		if (!buffer) {
1400441b62c1SHarvey Harrison 			dev_err(dev, "%s - out of memory\n", __func__);
14011da177e4SLinus Torvalds 			return -ENOMEM;
14021da177e4SLinus Torvalds 		}
14031da177e4SLinus Torvalds 
14042742fd88SAlan Cox 		/* Initialize the buffer to 0xff (pad the buffer) */
14051da177e4SLinus Torvalds 		memset(buffer, 0xff, buffer_size);
14061da177e4SLinus Torvalds 
1407d12b219aSJaswinder Singh 		err = request_firmware(&fw, fw_name, dev);
1408d12b219aSJaswinder Singh 		if (err) {
1409d12b219aSJaswinder Singh 			printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1410d12b219aSJaswinder Singh 			       fw_name, err);
1411d12b219aSJaswinder Singh 			kfree(buffer);
1412d12b219aSJaswinder Singh 			return err;
1413d12b219aSJaswinder Singh 		}
1414d12b219aSJaswinder Singh 		memcpy(buffer, &fw->data[4], fw->size - 4);
1415d12b219aSJaswinder Singh 		release_firmware(fw);
14161da177e4SLinus Torvalds 
14172742fd88SAlan Cox 		for (i = sizeof(struct ti_i2c_image_header);
14182742fd88SAlan Cox 				i < buffer_size; i++) {
14191da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14201da177e4SLinus Torvalds 		}
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14231da177e4SLinus Torvalds 
14242742fd88SAlan Cox 		/* update length and checksum after padding */
14252742fd88SAlan Cox 		header->Length 	 = cpu_to_le16((__u16)(buffer_size -
14262742fd88SAlan Cox 					sizeof(struct ti_i2c_image_header)));
14271da177e4SLinus Torvalds 		header->CheckSum = cs;
14281da177e4SLinus Torvalds 
14292742fd88SAlan Cox 		/* Download the operational code  */
143067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
14312742fd88SAlan Cox 		status = download_code(serial, buffer, buffer_size);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 		kfree(buffer);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 		if (status) {
143667e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
14371da177e4SLinus Torvalds 			return status;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 
14402742fd88SAlan Cox 		/* Device will reboot */
14411da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14421da177e4SLinus Torvalds 
144367e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 		/* return an error on purpose */
14461da177e4SLinus Torvalds 		return -ENODEV;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14492742fd88SAlan Cox stayinbootmode:
14502742fd88SAlan Cox 	/* Eprom is invalid or blank stay in boot mode */
145167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
14521da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	return 0;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 
14582742fd88SAlan Cox static int ti_do_config(struct edgeport_port *port, int feature, int on)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14612742fd88SAlan Cox 	on = !!on;	/* 1 or 0 not bitmask */
14622742fd88SAlan Cox 	return send_cmd(port->port->serial->dev,
14632742fd88SAlan Cox 			feature, (__u8)(UMPM_UART1_PORT + port_number),
14642742fd88SAlan Cox 			on, NULL, 0);
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 
14682742fd88SAlan Cox static int restore_mcr(struct edgeport_port *port, __u8 mcr)
14691da177e4SLinus Torvalds {
14701da177e4SLinus Torvalds 	int status = 0;
14711da177e4SLinus Torvalds 
147267e6da70SGreg Kroah-Hartman 	dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
14731da177e4SLinus Torvalds 
14742742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
14751da177e4SLinus Torvalds 	if (status)
14761da177e4SLinus Torvalds 		return status;
14772742fd88SAlan Cox 	status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
14781da177e4SLinus Torvalds 	if (status)
14791da177e4SLinus Torvalds 		return status;
14802742fd88SAlan Cox 	return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
14842742fd88SAlan Cox static __u8 map_line_status(__u8 ti_lsr)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	__u8 lsr = 0;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
14891da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
14901da177e4SLinus Torvalds 		lsr |= flagUart;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
14931da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
14941da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
14951da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
14962742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* rx data available */
14972742fd88SAlan Cox 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* tx hold reg empty */
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds #undef MAP_FLAG
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	return lsr;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	struct async_icount *icount;
15071da177e4SLinus Torvalds 	struct tty_struct *tty;
15081da177e4SLinus Torvalds 
150967e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
15101da177e4SLinus Torvalds 
15112742fd88SAlan Cox 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
15122742fd88SAlan Cox 			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
15131da177e4SLinus Torvalds 		icount = &edge_port->icount;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 		/* update input line counters */
15161da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
15171da177e4SLinus Torvalds 			icount->cts++;
15181da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
15191da177e4SLinus Torvalds 			icount->dsr++;
15201da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
15211da177e4SLinus Torvalds 			icount->dcd++;
15221da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
15231da177e4SLinus Torvalds 			icount->rng++;
15241da177e4SLinus Torvalds 		wake_up_interruptible(&edge_port->delta_msr_wait);
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	/* Save the new modem status */
15281da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
15291da177e4SLinus Torvalds 
15304a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
15311da177e4SLinus Torvalds 	/* handle CTS flow control */
15321da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
15331da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_CTS) {
15341da177e4SLinus Torvalds 			tty->hw_stopped = 0;
15351da177e4SLinus Torvalds 			tty_wakeup(tty);
15361da177e4SLinus Torvalds 		} else {
15371da177e4SLinus Torvalds 			tty->hw_stopped = 1;
15381da177e4SLinus Torvalds 		}
15391da177e4SLinus Torvalds 	}
15404a90f09bSAlan Cox 	tty_kref_put(tty);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15432742fd88SAlan Cox static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
15442742fd88SAlan Cox 							__u8 lsr, __u8 data)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	struct async_icount *icount;
15472742fd88SAlan Cox 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
15482742fd88SAlan Cox 						LSR_FRM_ERR | LSR_BREAK));
15494a90f09bSAlan Cox 	struct tty_struct *tty;
15501da177e4SLinus Torvalds 
155167e6da70SGreg Kroah-Hartman 	dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
15541da177e4SLinus Torvalds 
15552742fd88SAlan Cox 	if (new_lsr & LSR_BREAK)
15561da177e4SLinus Torvalds 		/*
15571da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
15581da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
15591da177e4SLinus Torvalds 		 */
15601da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
15634a90f09bSAlan Cox 	if (lsr_data) {
15644a90f09bSAlan Cox 		tty = tty_port_tty_get(&edge_port->port->port);
15654a90f09bSAlan Cox 		if (tty) {
15664a90f09bSAlan Cox 			edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
15674a90f09bSAlan Cox 			tty_kref_put(tty);
15684a90f09bSAlan Cox 		}
15694a90f09bSAlan Cox 	}
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	/* update input line counters */
15721da177e4SLinus Torvalds 	icount = &edge_port->icount;
15731da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
15741da177e4SLinus Torvalds 		icount->brk++;
15751da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
15761da177e4SLinus Torvalds 		icount->overrun++;
15771da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
15781da177e4SLinus Torvalds 		icount->parity++;
15791da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
15801da177e4SLinus Torvalds 		icount->frame++;
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 
15847d12e780SDavid Howells static void edge_interrupt_callback(struct urb *urb)
15851da177e4SLinus Torvalds {
1586cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
15871da177e4SLinus Torvalds 	struct usb_serial_port *port;
15881da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
158967e6da70SGreg Kroah-Hartman 	struct device *dev;
15901da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
15911da177e4SLinus Torvalds 	int length = urb->actual_length;
15921da177e4SLinus Torvalds 	int port_number;
15931da177e4SLinus Torvalds 	int function;
1594ee337c21SGreg Kroah-Hartman 	int retval;
15951da177e4SLinus Torvalds 	__u8 lsr;
15961da177e4SLinus Torvalds 	__u8 msr;
1597ee337c21SGreg Kroah-Hartman 	int status = urb->status;
15981da177e4SLinus Torvalds 
1599ee337c21SGreg Kroah-Hartman 	switch (status) {
16001da177e4SLinus Torvalds 	case 0:
16011da177e4SLinus Torvalds 		/* success */
16021da177e4SLinus Torvalds 		break;
16031da177e4SLinus Torvalds 	case -ECONNRESET:
16041da177e4SLinus Torvalds 	case -ENOENT:
16051da177e4SLinus Torvalds 	case -ESHUTDOWN:
16061da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
160767e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1608441b62c1SHarvey Harrison 		    __func__, status);
16091da177e4SLinus Torvalds 		return;
16101da177e4SLinus Torvalds 	default:
1611ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1612441b62c1SHarvey Harrison 			"%d\n", __func__, status);
16131da177e4SLinus Torvalds 		goto exit;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (!length) {
161767e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
16181da177e4SLinus Torvalds 		goto exit;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 
162167e6da70SGreg Kroah-Hartman 	dev = &edge_serial->serial->dev->dev;
1622*59d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, length, data);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	if (length != 2) {
162567e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
16261da177e4SLinus Torvalds 		goto exit;
16271da177e4SLinus Torvalds 	}
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
16301da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE(data[0]);
163167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
163267e6da70SGreg Kroah-Hartman 		port_number, function, data[1]);
16331da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
16341da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
16351da177e4SLinus Torvalds 	if (!edge_port) {
163667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - edge_port not found\n", __func__);
16371da177e4SLinus Torvalds 		return;
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 	switch (function) {
16401da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
16412742fd88SAlan Cox 		lsr = map_line_status(data[1]);
16421da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
16432742fd88SAlan Cox 			/* Save the LSR event for bulk read
16442742fd88SAlan Cox 			   completion routine */
164567e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1646441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16471da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
16481da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
16491da177e4SLinus Torvalds 		} else {
165067e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1651441b62c1SHarvey Harrison 				__func__, port_number, lsr);
16521da177e4SLinus Torvalds 			handle_new_lsr(edge_port, 0, lsr, 0);
16531da177e4SLinus Torvalds 		}
16541da177e4SLinus Torvalds 		break;
16551da177e4SLinus Torvalds 
16562742fd88SAlan Cox 	case TIUMP_INTERRUPT_CODE_MSR:	/* MSR */
16571da177e4SLinus Torvalds 		/* Copy MSR from UMP */
16581da177e4SLinus Torvalds 		msr = data[1];
165967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1660441b62c1SHarvey Harrison 			__func__, port_number, msr);
16611da177e4SLinus Torvalds 		handle_new_msr(edge_port, msr);
16621da177e4SLinus Torvalds 		break;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	default:
16652742fd88SAlan Cox 		dev_err(&urb->dev->dev,
16662742fd88SAlan Cox 			"%s - Unknown Interrupt code from UMP %x\n",
1667441b62c1SHarvey Harrison 			__func__, data[1]);
16681da177e4SLinus Torvalds 		break;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	}
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds exit:
1673ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1674ee337c21SGreg Kroah-Hartman 	if (retval)
16752742fd88SAlan Cox 		dev_err(&urb->dev->dev,
16762742fd88SAlan Cox 			"%s - usb_submit_urb failed with result %d\n",
1677441b62c1SHarvey Harrison 			 __func__, retval);
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16807d12e780SDavid Howells static void edge_bulk_in_callback(struct urb *urb)
16811da177e4SLinus Torvalds {
1682cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
168367e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
16841da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
16851da177e4SLinus Torvalds 	struct tty_struct *tty;
1686ee337c21SGreg Kroah-Hartman 	int retval = 0;
16871da177e4SLinus Torvalds 	int port_number;
1688ee337c21SGreg Kroah-Hartman 	int status = urb->status;
16891da177e4SLinus Torvalds 
1690ee337c21SGreg Kroah-Hartman 	switch (status) {
16911da177e4SLinus Torvalds 	case 0:
16921da177e4SLinus Torvalds 		/* success */
16931da177e4SLinus Torvalds 		break;
16941da177e4SLinus Torvalds 	case -ECONNRESET:
16951da177e4SLinus Torvalds 	case -ENOENT:
16961da177e4SLinus Torvalds 	case -ESHUTDOWN:
16971da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
169867e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
16991da177e4SLinus Torvalds 		return;
17001da177e4SLinus Torvalds 	default:
170167e6da70SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
1704ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
17051da177e4SLinus Torvalds 		goto exit;
17061da177e4SLinus Torvalds 
1707ee337c21SGreg Kroah-Hartman 	if (status) {
1708441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
17091da177e4SLinus Torvalds 		return;
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	port_number = edge_port->port->number - edge_port->port->serial->minor;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
17151da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
171667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1717441b62c1SHarvey Harrison 			__func__, port_number, edge_port->lsr_mask, *data);
17181da177e4SLinus Torvalds 		handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
17191da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
17201da177e4SLinus Torvalds 		--urb->actual_length;
17211da177e4SLinus Torvalds 		++data;
17221da177e4SLinus Torvalds 	}
17231da177e4SLinus Torvalds 
17244a90f09bSAlan Cox 	tty = tty_port_tty_get(&edge_port->port->port);
17251da177e4SLinus Torvalds 	if (tty && urb->actual_length) {
1726*59d33f2fSGreg Kroah-Hartman 		usb_serial_debug_data(dev, __func__, urb->actual_length, data);
17272742fd88SAlan Cox 		if (edge_port->close_pending)
172867e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
17292742fd88SAlan Cox 								__func__);
17302742fd88SAlan Cox 		else
173167e6da70SGreg Kroah-Hartman 			edge_tty_recv(dev, tty, data, urb->actual_length);
17321da177e4SLinus Torvalds 		edge_port->icount.rx += urb->actual_length;
17331da177e4SLinus Torvalds 	}
17344a90f09bSAlan Cox 	tty_kref_put(tty);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds exit:
17371da177e4SLinus Torvalds 	/* continue read unless stopped */
17381da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
17395833041fSJohan Hovold 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
1740ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
17415833041fSJohan Hovold 	else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
17421da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
17435833041fSJohan Hovold 
17441da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
1745ee337c21SGreg Kroah-Hartman 	if (retval)
174667e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
17471da177e4SLinus Torvalds }
17481da177e4SLinus Torvalds 
17492742fd88SAlan Cox static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
17502742fd88SAlan Cox 					unsigned char *data, int length)
17511da177e4SLinus Torvalds {
17522742fd88SAlan Cox 	int queued;
17531da177e4SLinus Torvalds 
17542742fd88SAlan Cox 	queued = tty_insert_flip_string(tty, data, length);
17552742fd88SAlan Cox 	if (queued < length)
17561da177e4SLinus Torvalds 		dev_err(dev, "%s - dropping data, %d bytes lost\n",
17572742fd88SAlan Cox 			__func__, length - queued);
17581da177e4SLinus Torvalds 	tty_flip_buffer_push(tty);
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17617d12e780SDavid Howells static void edge_bulk_out_callback(struct urb *urb)
17621da177e4SLinus Torvalds {
1763cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
17641da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1765ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17664a90f09bSAlan Cox 	struct tty_struct *tty;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
17691da177e4SLinus Torvalds 
1770ee337c21SGreg Kroah-Hartman 	switch (status) {
17711da177e4SLinus Torvalds 	case 0:
17721da177e4SLinus Torvalds 		/* success */
17731da177e4SLinus Torvalds 		break;
17741da177e4SLinus Torvalds 	case -ECONNRESET:
17751da177e4SLinus Torvalds 	case -ENOENT:
17761da177e4SLinus Torvalds 	case -ESHUTDOWN:
17771da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
177867e6da70SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1779441b62c1SHarvey Harrison 		    __func__, status);
17801da177e4SLinus Torvalds 		return;
17811da177e4SLinus Torvalds 	default:
178222a416c4SJohan Hovold 		dev_err_console(port, "%s - nonzero write bulk status "
1783441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
17841da177e4SLinus Torvalds 	}
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	/* send any buffered data */
17874a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
17884a90f09bSAlan Cox 	edge_send(tty);
17894a90f09bSAlan Cox 	tty_kref_put(tty);
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
1792a509a7e4SAlan Cox static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
17951da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
17961da177e4SLinus Torvalds 	struct usb_device *dev;
17971da177e4SLinus Torvalds 	struct urb *urb;
17981da177e4SLinus Torvalds 	int port_number;
17991da177e4SLinus Torvalds 	int status;
18001da177e4SLinus Torvalds 	u16 open_settings;
18011da177e4SLinus Torvalds 	u8 transaction_timeout;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	if (edge_port == NULL)
18041da177e4SLinus Torvalds 		return -ENODEV;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
18071da177e4SLinus Torvalds 	switch (port_number) {
18081da177e4SLinus Torvalds 	case 0:
18091da177e4SLinus Torvalds 		edge_port->uart_base = UMPMEM_BASE_UART1;
18101da177e4SLinus Torvalds 		edge_port->dma_address = UMPD_OEDB1_ADDRESS;
18111da177e4SLinus Torvalds 		break;
18121da177e4SLinus Torvalds 	case 1:
18131da177e4SLinus Torvalds 		edge_port->uart_base = UMPMEM_BASE_UART2;
18141da177e4SLinus Torvalds 		edge_port->dma_address = UMPD_OEDB2_ADDRESS;
18151da177e4SLinus Torvalds 		break;
18161da177e4SLinus Torvalds 	default:
18171da177e4SLinus Torvalds 		dev_err(&port->dev, "Unknown port number!!!\n");
18181da177e4SLinus Torvalds 		return -ENODEV;
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 
182167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
182267e6da70SGreg Kroah-Hartman 		__func__, port_number, edge_port->uart_base, edge_port->dma_address);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	dev = port->serial->dev;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
18271da177e4SLinus Torvalds 	init_waitqueue_head(&edge_port->delta_msr_wait);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	/* turn off loopback */
18302742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
18311da177e4SLinus Torvalds 	if (status) {
18322742fd88SAlan Cox 		dev_err(&port->dev,
18332742fd88SAlan Cox 				"%s - cannot send clear loopback command, %d\n",
1834441b62c1SHarvey Harrison 			__func__, status);
18351da177e4SLinus Torvalds 		return status;
18361da177e4SLinus Torvalds 	}
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	/* set up the port settings */
183995da310eSAlan Cox 	if (tty)
18404a90f09bSAlan Cox 		edge_set_termios(tty, port, tty->termios);
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	/* open up the port */
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
18451da177e4SLinus Torvalds 	transaction_timeout = 2;
18461da177e4SLinus Torvalds 
18472742fd88SAlan Cox 	edge_port->ump_read_timeout =
18482742fd88SAlan Cox 				max(20, ((transaction_timeout * 3) / 2));
18491da177e4SLinus Torvalds 
18502742fd88SAlan Cox 	/* milliseconds to timeout for DMA transfer */
18511da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
18521da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
18531da177e4SLinus Torvalds 			     (transaction_timeout << 2));
18541da177e4SLinus Torvalds 
185567e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
18582742fd88SAlan Cox 	status = send_cmd(dev, UMPC_OPEN_PORT,
18592742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
18601da177e4SLinus Torvalds 	if (status) {
18612742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send open command, %d\n",
18622742fd88SAlan Cox 							__func__, status);
18631da177e4SLinus Torvalds 		return status;
18641da177e4SLinus Torvalds 	}
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	/* Start the DMA? */
18672742fd88SAlan Cox 	status = send_cmd(dev, UMPC_START_PORT,
18682742fd88SAlan Cox 		(u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
18691da177e4SLinus Torvalds 	if (status) {
18702742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
18712742fd88SAlan Cox 							__func__, status);
18721da177e4SLinus Torvalds 		return status;
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
18762742fd88SAlan Cox 	status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
18771da177e4SLinus Torvalds 	if (status) {
18782742fd88SAlan Cox 		dev_err(&port->dev,
18792742fd88SAlan Cox 			"%s - cannot send clear buffers command, %d\n",
18802742fd88SAlan Cox 			__func__, status);
18811da177e4SLinus Torvalds 		return status;
18821da177e4SLinus Torvalds 	}
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	/* Read Initial MSR */
18852742fd88SAlan Cox 	status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
18862742fd88SAlan Cox 				(__u16)(UMPM_UART1_PORT + port_number),
18872742fd88SAlan Cox 				&edge_port->shadow_msr, 1);
18881da177e4SLinus Torvalds 	if (status) {
18892742fd88SAlan Cox 		dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
18902742fd88SAlan Cox 							__func__, status);
18911da177e4SLinus Torvalds 		return status;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 
189467e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	/* Set Initial MCR */
18971da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
189867e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
1901241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
19021da177e4SLinus Torvalds 		return -ERESTARTSYS;
19031da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
19042742fd88SAlan Cox 		/* we are the first port to open, post the interrupt urb */
19051da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
19061da177e4SLinus Torvalds 		if (!urb) {
19072742fd88SAlan Cox 			dev_err(&port->dev,
19082742fd88SAlan Cox 				"%s - no interrupt urb present, exiting\n",
19092742fd88SAlan Cox 				__func__);
19101da177e4SLinus Torvalds 			status = -EINVAL;
1911241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19121da177e4SLinus Torvalds 		}
19131da177e4SLinus Torvalds 		urb->context = edge_serial;
19141da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
19151da177e4SLinus Torvalds 		if (status) {
19162742fd88SAlan Cox 			dev_err(&port->dev,
19172742fd88SAlan Cox 				"%s - usb_submit_urb failed with value %d\n",
19182742fd88SAlan Cox 					__func__, status);
1919241ca64fSMatthias Kaehlcke 			goto release_es_lock;
19201da177e4SLinus Torvalds 		}
19211da177e4SLinus Torvalds 	}
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	/*
19241da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
19251da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
19261da177e4SLinus Torvalds 	 */
19271da177e4SLinus Torvalds 	usb_clear_halt(dev, port->write_urb->pipe);
19281da177e4SLinus Torvalds 	usb_clear_halt(dev, port->read_urb->pipe);
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	/* start up our bulk read urb */
19311da177e4SLinus Torvalds 	urb = port->read_urb;
19321da177e4SLinus Torvalds 	if (!urb) {
19332742fd88SAlan Cox 		dev_err(&port->dev, "%s - no read urb present, exiting\n",
19342742fd88SAlan Cox 								__func__);
19351da177e4SLinus Torvalds 		status = -EINVAL;
19361da177e4SLinus Torvalds 		goto unlink_int_urb;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
19391da177e4SLinus Torvalds 	urb->context = edge_port;
19401da177e4SLinus Torvalds 	status = usb_submit_urb(urb, GFP_KERNEL);
19411da177e4SLinus Torvalds 	if (status) {
19422742fd88SAlan Cox 		dev_err(&port->dev,
19432742fd88SAlan Cox 			"%s - read bulk usb_submit_urb failed with value %d\n",
19442742fd88SAlan Cox 				__func__, status);
19451da177e4SLinus Torvalds 		goto unlink_int_urb;
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
19491da177e4SLinus Torvalds 
1950241ca64fSMatthias Kaehlcke 	goto release_es_lock;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds unlink_int_urb:
19531da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
19541da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1955241ca64fSMatthias Kaehlcke release_es_lock:
1956241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
19571da177e4SLinus Torvalds 	return status;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
1960335f8514SAlan Cox static void edge_close(struct usb_serial_port *port)
19611da177e4SLinus Torvalds {
19621da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19631da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
1964af581059SJohan Hovold 	struct usb_serial *serial = port->serial;
19651da177e4SLinus Torvalds 	int port_number;
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
19681da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
19692742fd88SAlan Cox 	if (edge_serial == NULL || edge_port == NULL)
19701da177e4SLinus Torvalds 		return;
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
19731da177e4SLinus Torvalds 	 * this flag and dump add read data */
19741da177e4SLinus Torvalds 	edge_port->close_pending = 1;
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	/* chase the port close and flush */
19772742fd88SAlan Cox 	chase_port(edge_port, (HZ * closing_wait) / 100, 1);
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
19801da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
19811da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	/* assuming we can still talk to the device,
19841da177e4SLinus Torvalds 	 * send a close port command to it */
198567e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
19861da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
1987af581059SJohan Hovold 
1988af581059SJohan Hovold 	mutex_lock(&serial->disc_mutex);
1989af581059SJohan Hovold 	if (!serial->disconnected) {
1990af581059SJohan Hovold 		send_cmd(serial->dev,
19911da177e4SLinus Torvalds 				     UMPC_CLOSE_PORT,
19921da177e4SLinus Torvalds 				     (__u8)(UMPM_UART1_PORT + port_number),
19931da177e4SLinus Torvalds 				     0,
19941da177e4SLinus Torvalds 				     NULL,
19951da177e4SLinus Torvalds 				     0);
1996af581059SJohan Hovold 	}
1997af581059SJohan Hovold 	mutex_unlock(&serial->disc_mutex);
1998af581059SJohan Hovold 
1999241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
20001da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
20011da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
20021da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
20031da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
20041da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
20051da177e4SLinus Torvalds 	}
2006241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
20071da177e4SLinus Torvalds 	edge_port->close_pending = 0;
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
201095da310eSAlan Cox static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
201195da310eSAlan Cox 				const unsigned char *data, int count)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	if (count == 0) {
201667e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
20171da177e4SLinus Torvalds 		return 0;
20181da177e4SLinus Torvalds 	}
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	if (edge_port == NULL)
20211da177e4SLinus Torvalds 		return -ENODEV;
20221da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20231da177e4SLinus Torvalds 		return -ENODEV;
20241da177e4SLinus Torvalds 
2025d733cec1SJohan Hovold 	count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2026d733cec1SJohan Hovold 							&edge_port->ep_lock);
202795da310eSAlan Cox 	edge_send(tty);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	return count;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
203295da310eSAlan Cox static void edge_send(struct tty_struct *tty)
20331da177e4SLinus Torvalds {
203495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20351da177e4SLinus Torvalds 	int count, result;
20361da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20371da177e4SLinus Torvalds 	unsigned long flags;
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
20421da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20431da177e4SLinus Torvalds 		return;
20441da177e4SLinus Torvalds 	}
20451da177e4SLinus Torvalds 
2046d733cec1SJohan Hovold 	count = kfifo_out(&edge_port->write_fifo,
20471da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
20481da177e4SLinus Torvalds 				port->bulk_out_size);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	if (count == 0) {
20511da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20521da177e4SLinus Torvalds 		return;
20531da177e4SLinus Torvalds 	}
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20581da177e4SLinus Torvalds 
2059*59d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	/* set up our urb */
2062fd11961aSJohan Hovold 	port->write_urb->transfer_buffer_length = count;
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	/* send the data out the bulk port */
20651da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
20661da177e4SLinus Torvalds 	if (result) {
206722a416c4SJohan Hovold 		dev_err_console(port,
20682742fd88SAlan Cox 			"%s - failed submitting write urb, error %d\n",
20692742fd88SAlan Cox 				__func__, result);
20701da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
20712742fd88SAlan Cox 		/* TODO: reschedule edge_send */
20722742fd88SAlan Cox 	} else
20731da177e4SLinus Torvalds 		edge_port->icount.tx += count;
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
20761da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
20772742fd88SAlan Cox 	if (tty)
20781da177e4SLinus Torvalds 		tty_wakeup(tty);
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
208195da310eSAlan Cox static int edge_write_room(struct tty_struct *tty)
20821da177e4SLinus Torvalds {
208395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
20841da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
20851da177e4SLinus Torvalds 	int room = 0;
20861da177e4SLinus Torvalds 	unsigned long flags;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	if (edge_port == NULL)
20892742fd88SAlan Cox 		return 0;
20901da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
20912742fd88SAlan Cox 		return 0;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2094d733cec1SJohan Hovold 	room = kfifo_avail(&edge_port->write_fifo);
20951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
20961da177e4SLinus Torvalds 
209767e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
20981da177e4SLinus Torvalds 	return room;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
210195da310eSAlan Cox static int edge_chars_in_buffer(struct tty_struct *tty)
21021da177e4SLinus Torvalds {
210395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21041da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21051da177e4SLinus Torvalds 	int chars = 0;
21061da177e4SLinus Torvalds 	unsigned long flags;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	if (edge_port == NULL)
21092742fd88SAlan Cox 		return 0;
21101da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21112742fd88SAlan Cox 		return 0;
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
2114d733cec1SJohan Hovold 	chars = kfifo_len(&edge_port->write_fifo);
21151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21161da177e4SLinus Torvalds 
211767e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
21181da177e4SLinus Torvalds 	return chars;
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds 
212195da310eSAlan Cox static void edge_throttle(struct tty_struct *tty)
21221da177e4SLinus Torvalds {
212395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21241da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21251da177e4SLinus Torvalds 	int status;
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	if (edge_port == NULL)
21281da177e4SLinus Torvalds 		return;
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
21311da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21321da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
213395da310eSAlan Cox 		status = edge_write(tty, port, &stop_char, 1);
213495da310eSAlan Cox 		if (status <= 0) {
213595da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
213695da310eSAlan Cox 		}
21371da177e4SLinus Torvalds 	}
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
21401da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
21411da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
21421da177e4SLinus Torvalds 		stop_read(edge_port);
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
214695da310eSAlan Cox static void edge_unthrottle(struct tty_struct *tty)
21471da177e4SLinus Torvalds {
214895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21491da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21501da177e4SLinus Torvalds 	int status;
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	if (edge_port == NULL)
21531da177e4SLinus Torvalds 		return;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
21561da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
21571da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
215895da310eSAlan Cox 		status = edge_write(tty, port, &start_char, 1);
215995da310eSAlan Cox 		if (status <= 0) {
216095da310eSAlan Cox 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
216195da310eSAlan Cox 		}
21621da177e4SLinus Torvalds 	}
21631da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
21641da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
21651da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
21661da177e4SLinus Torvalds 		status = restart_read(edge_port);
21671da177e4SLinus Torvalds 		if (status)
21682742fd88SAlan Cox 			dev_err(&port->dev,
21692742fd88SAlan Cox 				"%s - read bulk usb_submit_urb failed: %d\n",
21702742fd88SAlan Cox 							__func__, status);
21711da177e4SLinus Torvalds 	}
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
21761da177e4SLinus Torvalds {
21771da177e4SLinus Torvalds 	unsigned long flags;
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
21821da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
21831da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
21891da177e4SLinus Torvalds {
21901da177e4SLinus Torvalds 	struct urb *urb;
21911da177e4SLinus Torvalds 	int status = 0;
21921da177e4SLinus Torvalds 	unsigned long flags;
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
21971da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
2198efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
21991da177e4SLinus Torvalds 	}
22001da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
22011da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	return status;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
220895da310eSAlan Cox static void change_port_settings(struct tty_struct *tty,
220995da310eSAlan Cox 		struct edgeport_port *edge_port, struct ktermios *old_termios)
22101da177e4SLinus Torvalds {
221167e6da70SGreg Kroah-Hartman 	struct device *dev = &edge_port->port->dev;
22121da177e4SLinus Torvalds 	struct ump_uart_config *config;
22131da177e4SLinus Torvalds 	int baud;
22141da177e4SLinus Torvalds 	unsigned cflag;
22151da177e4SLinus Torvalds 	int status;
22162742fd88SAlan Cox 	int port_number = edge_port->port->number -
22172742fd88SAlan Cox 					edge_port->port->serial->minor;
22181da177e4SLinus Torvalds 
221967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
22221da177e4SLinus Torvalds 	if (!config) {
22232742fd88SAlan Cox 		*tty->termios = *old_termios;
222467e6da70SGreg Kroah-Hartman 		dev_err(dev, "%s - out of memory\n", __func__);
22251da177e4SLinus Torvalds 		return;
22261da177e4SLinus Torvalds 	}
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	config->wFlags = 0;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/* These flags must be set */
22331da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
22341da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
22351da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
22361da177e4SLinus Torvalds 
22371da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
22381da177e4SLinus Torvalds 	case CS5:
22391da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR5BITS;
224067e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 5\n", __func__);
22411da177e4SLinus Torvalds 		    break;
22421da177e4SLinus Torvalds 	case CS6:
22431da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR6BITS;
224467e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 6\n", __func__);
22451da177e4SLinus Torvalds 		    break;
22461da177e4SLinus Torvalds 	case CS7:
22471da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR7BITS;
224867e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 7\n", __func__);
22491da177e4SLinus Torvalds 		    break;
22501da177e4SLinus Torvalds 	default:
22511da177e4SLinus Torvalds 	case CS8:
22521da177e4SLinus Torvalds 		    config->bDataBits = UMP_UART_CHAR8BITS;
225367e6da70SGreg Kroah-Hartman 		    dev_dbg(dev, "%s - data bits = 8\n", __func__);
22541da177e4SLinus Torvalds 			    break;
22551da177e4SLinus Torvalds 	}
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	if (cflag & PARENB) {
22581da177e4SLinus Torvalds 		if (cflag & PARODD) {
22591da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22601da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
226167e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = odd\n", __func__);
22621da177e4SLinus Torvalds 		} else {
22631da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
22641da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
226567e6da70SGreg Kroah-Hartman 			dev_dbg(dev, "%s - parity = even\n", __func__);
22661da177e4SLinus Torvalds 		}
22671da177e4SLinus Torvalds 	} else {
22681da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
226967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = none\n", __func__);
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
22731da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
227467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
22751da177e4SLinus Torvalds 	} else {
22761da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
227767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 	/* figure out the flow control settings */
22811da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
22821da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
22831da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
228467e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
22851da177e4SLinus Torvalds 	} else {
228667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
22871da177e4SLinus Torvalds 		tty->hw_stopped = 0;
22881da177e4SLinus Torvalds 		restart_read(edge_port);
22891da177e4SLinus Torvalds 	}
22901da177e4SLinus Torvalds 
22912742fd88SAlan Cox 	/* if we are implementing XON/XOFF, set the start and stop
22922742fd88SAlan Cox 	   character in the device */
22931da177e4SLinus Torvalds 	config->cXon  = START_CHAR(tty);
22941da177e4SLinus Torvalds 	config->cXoff = STOP_CHAR(tty);
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds 	/* if we are implementing INBOUND XON/XOFF */
22971da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22981da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
229967e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2300441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
23012742fd88SAlan Cox 	} else
230267e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	/* if we are implementing OUTBOUND XON/XOFF */
23051da177e4SLinus Torvalds 	if (I_IXON(tty)) {
23061da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
230767e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2308441b62c1SHarvey Harrison 			__func__, config->cXon, config->cXoff);
23092742fd88SAlan Cox 	} else
231067e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
23111da177e4SLinus Torvalds 
2312d5f5bcd4SAlan Cox 	tty->termios->c_cflag &= ~CMSPAR;
2313d5f5bcd4SAlan Cox 
23141da177e4SLinus Torvalds 	/* Round the baud rate */
23151da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
23161da177e4SLinus Torvalds 	if (!baud) {
23171da177e4SLinus Torvalds 		/* pick a default, any default... */
23181da177e4SLinus Torvalds 		baud = 9600;
2319d5f5bcd4SAlan Cox 	} else
2320d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
2321d5f5bcd4SAlan Cox 
23221da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
23231da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
23241da177e4SLinus Torvalds 
2325d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2326d5f5bcd4SAlan Cox 
232767e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
23281da177e4SLinus Torvalds 
232967e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wBaudRate:   %d\n", (int)(461550L / config->wBaudRate));
233067e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "wFlags:    0x%x\n", config->wFlags);
233167e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bDataBits:   %d\n", config->bDataBits);
233267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bParity:     %d\n", config->bParity);
233367e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bStopBits:   %d\n", config->bStopBits);
233467e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXon:        %d\n", config->cXon);
233567e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "cXoff:       %d\n", config->cXoff);
233667e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "bUartMode:   %d\n", config->bUartMode);
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	/* move the word values into big endian mode */
23391da177e4SLinus Torvalds 	cpu_to_be16s(&config->wFlags);
23401da177e4SLinus Torvalds 	cpu_to_be16s(&config->wBaudRate);
23411da177e4SLinus Torvalds 
23422742fd88SAlan Cox 	status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
23431da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
23442742fd88SAlan Cox 				0, (__u8 *)config, sizeof(*config));
23452742fd88SAlan Cox 	if (status)
234667e6da70SGreg Kroah-Hartman 		dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2347441b62c1SHarvey Harrison 			__func__, status);
23481da177e4SLinus Torvalds 	kfree(config);
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
235195da310eSAlan Cox static void edge_set_termios(struct tty_struct *tty,
235295da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
23531da177e4SLinus Torvalds {
23541da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
235595da310eSAlan Cox 	unsigned int cflag;
235695da310eSAlan Cox 
235795da310eSAlan Cox 	cflag = tty->termios->c_cflag;
23581da177e4SLinus Torvalds 
235967e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
23601da177e4SLinus Torvalds 		tty->termios->c_cflag, tty->termios->c_iflag);
236167e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
23621da177e4SLinus Torvalds 		old_termios->c_cflag, old_termios->c_iflag);
236367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (edge_port == NULL)
23661da177e4SLinus Torvalds 		return;
23671da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
236895da310eSAlan Cox 	change_port_settings(tty, edge_port, old_termios);
23691da177e4SLinus Torvalds }
23701da177e4SLinus Torvalds 
237120b9d177SAlan Cox static int edge_tiocmset(struct tty_struct *tty,
23722742fd88SAlan Cox 					unsigned int set, unsigned int clear)
23731da177e4SLinus Torvalds {
237495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23751da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23761da177e4SLinus Torvalds 	unsigned int mcr;
23773d71fe0bSAlan Cox 	unsigned long flags;
23781da177e4SLinus Torvalds 
23793d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23801da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
23811da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
23821da177e4SLinus Torvalds 		mcr |= MCR_RTS;
23831da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
23841da177e4SLinus Torvalds 		mcr |= MCR_DTR;
23851da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
23861da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
23891da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
23901da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
23911da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
23921da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
23931da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
23963d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23971da177e4SLinus Torvalds 
23982742fd88SAlan Cox 	restore_mcr(edge_port, mcr);
23991da177e4SLinus Torvalds 	return 0;
24001da177e4SLinus Torvalds }
24011da177e4SLinus Torvalds 
240260b33c13SAlan Cox static int edge_tiocmget(struct tty_struct *tty)
24031da177e4SLinus Torvalds {
240495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24051da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24061da177e4SLinus Torvalds 	unsigned int result = 0;
24071da177e4SLinus Torvalds 	unsigned int msr;
24081da177e4SLinus Torvalds 	unsigned int mcr;
24093d71fe0bSAlan Cox 	unsigned long flags;
24101da177e4SLinus Torvalds 
24113d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
24123d71fe0bSAlan Cox 
24131da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
24141da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
24151da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
24161da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
24171da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
24181da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
24191da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
24201da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 
242367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
24243d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	return result;
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
24290bca1b91SAlan Cox static int edge_get_icount(struct tty_struct *tty,
24300bca1b91SAlan Cox 				struct serial_icounter_struct *icount)
24310bca1b91SAlan Cox {
24320bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24330bca1b91SAlan Cox 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24340bca1b91SAlan Cox 	struct async_icount *ic = &edge_port->icount;
24350bca1b91SAlan Cox 
24360bca1b91SAlan Cox 	icount->cts = ic->cts;
24370bca1b91SAlan Cox 	icount->dsr = ic->dsr;
24380bca1b91SAlan Cox 	icount->rng = ic->rng;
24390bca1b91SAlan Cox 	icount->dcd = ic->dcd;
24400bca1b91SAlan Cox 	icount->tx = ic->tx;
24410bca1b91SAlan Cox         icount->rx = ic->rx;
24420bca1b91SAlan Cox         icount->frame = ic->frame;
24430bca1b91SAlan Cox         icount->parity = ic->parity;
24440bca1b91SAlan Cox         icount->overrun = ic->overrun;
24450bca1b91SAlan Cox         icount->brk = ic->brk;
24460bca1b91SAlan Cox         icount->buf_overrun = ic->buf_overrun;
24470bca1b91SAlan Cox 	return 0;
24480bca1b91SAlan Cox }
24490bca1b91SAlan Cox 
24502742fd88SAlan Cox static int get_serial_info(struct edgeport_port *edge_port,
24512742fd88SAlan Cox 				struct serial_struct __user *retinfo)
24521da177e4SLinus Torvalds {
24531da177e4SLinus Torvalds 	struct serial_struct tmp;
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 	if (!retinfo)
24561da177e4SLinus Torvalds 		return -EFAULT;
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
24611da177e4SLinus Torvalds 	tmp.line		= edge_port->port->serial->minor;
24621da177e4SLinus Torvalds 	tmp.port		= edge_port->port->number;
24631da177e4SLinus Torvalds 	tmp.irq			= 0;
24641da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
24651da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
24661da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
24671da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
24681da177e4SLinus Torvalds 	tmp.closing_wait	= closing_wait;
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
24711da177e4SLinus Torvalds 		return -EFAULT;
24721da177e4SLinus Torvalds 	return 0;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
247500a0d0d6SAlan Cox static int edge_ioctl(struct tty_struct *tty,
24762742fd88SAlan Cox 					unsigned int cmd, unsigned long arg)
24771da177e4SLinus Torvalds {
247895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
24791da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
24801da177e4SLinus Torvalds 	struct async_icount cnow;
24811da177e4SLinus Torvalds 	struct async_icount cprev;
24821da177e4SLinus Torvalds 
248367e6da70SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 	switch (cmd) {
24861da177e4SLinus Torvalds 	case TIOCGSERIAL:
248767e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
24882742fd88SAlan Cox 		return get_serial_info(edge_port,
24892742fd88SAlan Cox 				(struct serial_struct __user *) arg);
24901da177e4SLinus Torvalds 	case TIOCMIWAIT:
249167e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
24921da177e4SLinus Torvalds 		cprev = edge_port->icount;
24931da177e4SLinus Torvalds 		while (1) {
24941da177e4SLinus Torvalds 			interruptible_sleep_on(&edge_port->delta_msr_wait);
24951da177e4SLinus Torvalds 			/* see if a signal did it */
24961da177e4SLinus Torvalds 			if (signal_pending(current))
24971da177e4SLinus Torvalds 				return -ERESTARTSYS;
24981da177e4SLinus Torvalds 			cnow = edge_port->icount;
24991da177e4SLinus Torvalds 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
25001da177e4SLinus Torvalds 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
25011da177e4SLinus Torvalds 				return -EIO; /* no change => error */
25021da177e4SLinus Torvalds 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
25031da177e4SLinus Torvalds 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
25041da177e4SLinus Torvalds 			    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
25051da177e4SLinus Torvalds 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
25061da177e4SLinus Torvalds 				return 0;
25071da177e4SLinus Torvalds 			}
25081da177e4SLinus Torvalds 			cprev = cnow;
25091da177e4SLinus Torvalds 		}
25101da177e4SLinus Torvalds 		/* not reached */
25111da177e4SLinus Torvalds 		break;
25121da177e4SLinus Torvalds 	}
25131da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
25141da177e4SLinus Torvalds }
25151da177e4SLinus Torvalds 
251695da310eSAlan Cox static void edge_break(struct tty_struct *tty, int break_state)
25171da177e4SLinus Torvalds {
251895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
25191da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25201da177e4SLinus Torvalds 	int status;
25212742fd88SAlan Cox 	int bv = 0;	/* Off */
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	/* chase the port close */
25242742fd88SAlan Cox 	chase_port(edge_port, 0, 0);
25251da177e4SLinus Torvalds 
252695da310eSAlan Cox 	if (break_state == -1)
25272742fd88SAlan Cox 		bv = 1;	/* On */
25282742fd88SAlan Cox 	status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
25292742fd88SAlan Cox 	if (status)
253067e6da70SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2531441b62c1SHarvey Harrison 			__func__, status);
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds static int edge_startup(struct usb_serial *serial)
25351da177e4SLinus Torvalds {
25361da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
25371da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
25381da177e4SLinus Torvalds 	struct usb_device *dev;
25391da177e4SLinus Torvalds 	int status;
25401da177e4SLinus Torvalds 	int i;
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	dev = serial->dev;
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds 	/* create our private serial structure */
254580b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
25461da177e4SLinus Torvalds 	if (edge_serial == NULL) {
2547441b62c1SHarvey Harrison 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
25481da177e4SLinus Torvalds 		return -ENOMEM;
25491da177e4SLinus Torvalds 	}
2550241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
25511da177e4SLinus Torvalds 	edge_serial->serial = serial;
25521da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
25531da177e4SLinus Torvalds 
25542742fd88SAlan Cox 	status = download_fw(edge_serial);
25551da177e4SLinus Torvalds 	if (status) {
25561da177e4SLinus Torvalds 		kfree(edge_serial);
25571da177e4SLinus Torvalds 		return status;
25581da177e4SLinus Torvalds 	}
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	/* set up our port private structures */
25611da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
256280b6ca48SEric Sesterhenn 		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
25631da177e4SLinus Torvalds 		if (edge_port == NULL) {
25642742fd88SAlan Cox 			dev_err(&serial->dev->dev, "%s - Out of memory\n",
25652742fd88SAlan Cox 								__func__);
25661da177e4SLinus Torvalds 			goto cleanup;
25671da177e4SLinus Torvalds 		}
25681da177e4SLinus Torvalds 		spin_lock_init(&edge_port->ep_lock);
2569d733cec1SJohan Hovold 		if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2570d733cec1SJohan Hovold 								GFP_KERNEL)) {
25712742fd88SAlan Cox 			dev_err(&serial->dev->dev, "%s - Out of memory\n",
25722742fd88SAlan Cox 								__func__);
25731da177e4SLinus Torvalds 			kfree(edge_port);
25741da177e4SLinus Torvalds 			goto cleanup;
25751da177e4SLinus Torvalds 		}
25761da177e4SLinus Torvalds 		edge_port->port = serial->port[i];
25771da177e4SLinus Torvalds 		edge_port->edge_serial = edge_serial;
25781da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], edge_port);
2579fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = default_uart_mode;
25801da177e4SLinus Torvalds 	}
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	return 0;
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds cleanup:
25851da177e4SLinus Torvalds 	for (--i; i >= 0; --i) {
25861da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
2587d733cec1SJohan Hovold 		kfifo_free(&edge_port->write_fifo);
25881da177e4SLinus Torvalds 		kfree(edge_port);
25891da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
25901da177e4SLinus Torvalds 	}
25911da177e4SLinus Torvalds 	kfree(edge_serial);
25921da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
25931da177e4SLinus Torvalds 	return -ENOMEM;
25941da177e4SLinus Torvalds }
25951da177e4SLinus Torvalds 
2596f9c99bb8SAlan Stern static void edge_disconnect(struct usb_serial *serial)
25971da177e4SLinus Torvalds {
2598f9c99bb8SAlan Stern }
2599f9c99bb8SAlan Stern 
2600f9c99bb8SAlan Stern static void edge_release(struct usb_serial *serial)
2601f9c99bb8SAlan Stern {
2602f9c99bb8SAlan Stern 	int i;
2603f9c99bb8SAlan Stern 	struct edgeport_port *edge_port;
2604f9c99bb8SAlan Stern 
2605f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2606f9c99bb8SAlan Stern 		edge_port = usb_get_serial_port_data(serial->port[i]);
2607d733cec1SJohan Hovold 		kfifo_free(&edge_port->write_fifo);
26081da177e4SLinus Torvalds 		kfree(edge_port);
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds 
2614fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2615fc4cbd75SMartin K. Petersen 
2616fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev,
2617fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2618fc4cbd75SMartin K. Petersen {
2619fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2620fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2621fc4cbd75SMartin K. Petersen 
2622fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2623fc4cbd75SMartin K. Petersen }
2624fc4cbd75SMartin K. Petersen 
2625fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev,
2626fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2627fc4cbd75SMartin K. Petersen {
2628fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2629fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2630fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2631fc4cbd75SMartin K. Petersen 
263267e6da70SGreg Kroah-Hartman 	dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
2633fc4cbd75SMartin K. Petersen 
2634fc4cbd75SMartin K. Petersen 	if (v < 256)
2635fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2636fc4cbd75SMartin K. Petersen 	else
2637441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2638fc4cbd75SMartin K. Petersen 
2639fc4cbd75SMartin K. Petersen 	return count;
2640fc4cbd75SMartin K. Petersen }
2641fc4cbd75SMartin K. Petersen 
26422742fd88SAlan Cox static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
26432742fd88SAlan Cox 							store_uart_mode);
2644fc4cbd75SMartin K. Petersen 
2645fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2646fc4cbd75SMartin K. Petersen {
2647fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2648fc4cbd75SMartin K. Petersen }
2649fc4cbd75SMartin K. Petersen 
2650fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2651fc4cbd75SMartin K. Petersen {
2652fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2653fc4cbd75SMartin K. Petersen 	return 0;
2654fc4cbd75SMartin K. Petersen }
2655fc4cbd75SMartin K. Petersen 
2656fc4cbd75SMartin K. Petersen 
2657ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
265818fcac35SGreg Kroah-Hartman 	.driver = {
26591da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2660269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
266118fcac35SGreg Kroah-Hartman 	},
2662269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
26631da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
26641da177e4SLinus Torvalds 	.num_ports		= 1,
26651da177e4SLinus Torvalds 	.open			= edge_open,
26661da177e4SLinus Torvalds 	.close			= edge_close,
26671da177e4SLinus Torvalds 	.throttle		= edge_throttle,
26681da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
26691da177e4SLinus Torvalds 	.attach			= edge_startup,
2670f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2671f9c99bb8SAlan Stern 	.release		= edge_release,
2672fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
26736d443d84SEric W. Biederman 	.port_remove		= edge_remove_sysfs_attrs,
26741da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
26751da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
26761da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
26771da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
26780bca1b91SAlan Cox 	.get_icount		= edge_get_icount,
26791da177e4SLinus Torvalds 	.write			= edge_write,
26801da177e4SLinus Torvalds 	.write_room		= edge_write_room,
26811da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
26821da177e4SLinus Torvalds 	.break_ctl		= edge_break,
26831da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
26841da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
26851da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
26861da177e4SLinus Torvalds };
26871da177e4SLinus Torvalds 
2688ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
268918fcac35SGreg Kroah-Hartman 	.driver = {
26901da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2691269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
269218fcac35SGreg Kroah-Hartman 	},
2693269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
26941da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
26951da177e4SLinus Torvalds 	.num_ports		= 2,
26961da177e4SLinus Torvalds 	.open			= edge_open,
26971da177e4SLinus Torvalds 	.close			= edge_close,
26981da177e4SLinus Torvalds 	.throttle		= edge_throttle,
26991da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
27001da177e4SLinus Torvalds 	.attach			= edge_startup,
2701f9c99bb8SAlan Stern 	.disconnect		= edge_disconnect,
2702f9c99bb8SAlan Stern 	.release		= edge_release,
2703fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
27046d443d84SEric W. Biederman 	.port_remove		= edge_remove_sysfs_attrs,
27051da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
27061da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
27071da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
27081da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
27091da177e4SLinus Torvalds 	.write			= edge_write,
27101da177e4SLinus Torvalds 	.write_room		= edge_write_room,
27111da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
27121da177e4SLinus Torvalds 	.break_ctl		= edge_break,
27131da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
27141da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
27151da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
27161da177e4SLinus Torvalds };
27171da177e4SLinus Torvalds 
27187dbe2460SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
27197dbe2460SAlan Stern 	&edgeport_1port_device, &edgeport_2port_device, NULL
27207dbe2460SAlan Stern };
27217dbe2460SAlan Stern 
272268e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
27251da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
27261da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2727d12b219aSJaswinder Singh MODULE_FIRMWARE("edgeport/down3.bin");
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
27301da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
27331da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
27362742fd88SAlan Cox MODULE_PARM_DESC(ignore_cpu_rev,
27372742fd88SAlan Cox 			"Ignore the cpu revision when connecting to a device");
27381da177e4SLinus Torvalds 
2739fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2740fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
2741