xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision a969888ce91673c7f4b86520d851a6f0d5a5fa7d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Edgeport USB Serial Converter driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
51da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *	the Free Software Foundation; either version 2 of the License, or
101da177e4SLinus Torvalds  *	(at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Supports the following devices:
131da177e4SLinus Torvalds  *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * For questions or problems with this driver, contact Inside Out
161da177e4SLinus Torvalds  * Networks technical support, or Peter Berger <pberger@brimson.com>,
171da177e4SLinus Torvalds  * or Al Borchers <alborchers@steinerpoint.com>.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Version history:
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	July 11, 2002 	Removed 4 port device structure since all TI UMP
221da177e4SLinus Torvalds  *			chips have only 2 ports
231da177e4SLinus Torvalds  *			David Iacovelli (davidi@ionetworks.com)
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/jiffies.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/tty.h>
331da177e4SLinus Torvalds #include <linux/tty_driver.h>
341da177e4SLinus Torvalds #include <linux/tty_flip.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/spinlock.h>
371da177e4SLinus Torvalds #include <linux/serial.h>
381da177e4SLinus Torvalds #include <linux/ioctl.h>
391da177e4SLinus Torvalds #include <asm/uaccess.h>
401da177e4SLinus Torvalds #include <asm/semaphore.h>
411da177e4SLinus Torvalds #include <linux/usb.h>
42*a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include "io_16654.h"
451da177e4SLinus Torvalds #include "io_usbvend.h"
461da177e4SLinus Torvalds #include "io_ti.h"
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Version Information
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds #define DRIVER_VERSION "v0.7"
521da177e4SLinus Torvalds #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
531da177e4SLinus Torvalds #define DRIVER_DESC "Edgeport USB Serial Driver"
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* firmware image code */
571da177e4SLinus Torvalds #define IMAGE_VERSION_NAME	PagableOperationalCodeImageVersion
581da177e4SLinus Torvalds #define IMAGE_ARRAY_NAME	PagableOperationalCodeImage
591da177e4SLinus Torvalds #define IMAGE_SIZE		PagableOperationalCodeSize
601da177e4SLinus Torvalds #include "io_fw_down3.h"	/* Define array OperationalCodeImage[] */
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define EPROM_PAGE_SIZE		64
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds struct edgeport_uart_buf_desc {
661da177e4SLinus Torvalds 	__u32 count;		// Number of bytes currently in buffer
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* different hardware types */
701da177e4SLinus Torvalds #define HARDWARE_TYPE_930	0
711da177e4SLinus Torvalds #define HARDWARE_TYPE_TIUMP	1
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds // IOCTL_PRIVATE_TI_GET_MODE Definitions
741da177e4SLinus Torvalds #define	TI_MODE_CONFIGURING	0   // Device has not entered start device
751da177e4SLinus Torvalds #define	TI_MODE_BOOT		1   // Staying in boot mode
761da177e4SLinus Torvalds #define TI_MODE_DOWNLOAD	2   // Made it to download mode
771da177e4SLinus Torvalds #define TI_MODE_TRANSITIONING	3   // Currently in boot mode but transitioning to download mode
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* read urb state */
801da177e4SLinus Torvalds #define EDGE_READ_URB_RUNNING	0
811da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPING	1
821da177e4SLinus Torvalds #define EDGE_READ_URB_STOPPED	2
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define EDGE_LOW_LATENCY	1
851da177e4SLinus Torvalds #define EDGE_CLOSING_WAIT	4000	/* in .01 sec */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define EDGE_OUT_BUF_SIZE	1024
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Product information read from the Edgeport */
911da177e4SLinus Torvalds struct product_info
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	int	TiMode;			// Current TI Mode
941da177e4SLinus Torvalds 	__u8	hardware_type;		// Type of hardware
951da177e4SLinus Torvalds } __attribute__((packed));
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* circular buffer */
981da177e4SLinus Torvalds struct edge_buf {
991da177e4SLinus Torvalds 	unsigned int	buf_size;
1001da177e4SLinus Torvalds 	char		*buf_buf;
1011da177e4SLinus Torvalds 	char		*buf_get;
1021da177e4SLinus Torvalds 	char		*buf_put;
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds struct edgeport_port {
1061da177e4SLinus Torvalds 	__u16 uart_base;
1071da177e4SLinus Torvalds 	__u16 dma_address;
1081da177e4SLinus Torvalds 	__u8 shadow_msr;
1091da177e4SLinus Torvalds 	__u8 shadow_mcr;
1101da177e4SLinus Torvalds 	__u8 shadow_lsr;
1111da177e4SLinus Torvalds 	__u8 lsr_mask;
1121da177e4SLinus Torvalds 	__u32 ump_read_timeout;		/* Number of miliseconds the UMP will
1131da177e4SLinus Torvalds 					   wait without data before completing
1141da177e4SLinus Torvalds 					   a read short */
1151da177e4SLinus Torvalds 	int baud_rate;
1161da177e4SLinus Torvalds 	int close_pending;
1171da177e4SLinus Torvalds 	int lsr_event;
1181da177e4SLinus Torvalds 	struct edgeport_uart_buf_desc tx;
1191da177e4SLinus Torvalds 	struct async_icount	icount;
1201da177e4SLinus Torvalds 	wait_queue_head_t	delta_msr_wait;	/* for handling sleeping while
1211da177e4SLinus Torvalds 						   waiting for msr change to
1221da177e4SLinus Torvalds 						   happen */
1231da177e4SLinus Torvalds 	struct edgeport_serial	*edge_serial;
1241da177e4SLinus Torvalds 	struct usb_serial_port	*port;
1251da177e4SLinus Torvalds 	__u8 bUartMode;		/* Port type, 0: RS232, etc. */
1261da177e4SLinus Torvalds 	spinlock_t ep_lock;
1271da177e4SLinus Torvalds 	int ep_read_urb_state;
1281da177e4SLinus Torvalds 	int ep_write_urb_in_use;
1291da177e4SLinus Torvalds 	struct edge_buf *ep_out_buf;
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds struct edgeport_serial {
1331da177e4SLinus Torvalds 	struct product_info product_info;
1341da177e4SLinus Torvalds 	u8 TI_I2C_Type;			// Type of I2C in UMP
1351da177e4SLinus Torvalds 	u8 TiReadI2C;			// Set to TRUE if we have read the I2c in Boot Mode
1361da177e4SLinus Torvalds 	struct semaphore es_sem;
1371da177e4SLinus Torvalds 	int num_ports_open;
1381da177e4SLinus Torvalds 	struct usb_serial *serial;
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* Devices that this driver supports */
1431da177e4SLinus Torvalds static struct usb_device_id edgeport_1port_id_table [] = {
1441da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1451da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1461da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1471da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1481da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1491da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1501da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1511da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1521da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1531da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1541da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1551da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1561da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1571da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1581da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1591da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1601da177e4SLinus Torvalds 	{ }
1611da177e4SLinus Torvalds };
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static struct usb_device_id edgeport_2port_id_table [] = {
1641da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
1651da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
1661da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
1671da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
1681da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
1691da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
1701da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
1711da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
1721da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
1731da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
1741da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
1751da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
1761da177e4SLinus Torvalds // The 4-port shows up as two 2-port devices
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
1781da177e4SLinus Torvalds 	{ }
1791da177e4SLinus Torvalds };
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /* Devices that this driver supports */
1821da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = {
1831da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1841da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1851da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1861da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1911da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1921da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1931da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1941da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1951da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
1961da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
1971da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
1981da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
1991da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
2001da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
2011da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
2021da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
2031da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
2041da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
2051da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
2061da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
2071da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
2081da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
2091da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
2101da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
2111da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
2121da177e4SLinus Torvalds 	{ }
2131da177e4SLinus Torvalds };
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table_combined);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static struct usb_driver io_driver = {
2181da177e4SLinus Torvalds 	.name =		"io_ti",
2191da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
2201da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
2211da177e4SLinus Torvalds 	.id_table =	id_table_combined,
222ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
2231da177e4SLinus Torvalds };
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static int debug;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds static int TIStayInBootMode = 0;
2311da177e4SLinus Torvalds static int low_latency = EDGE_LOW_LATENCY;
2321da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
2331da177e4SLinus Torvalds static int ignore_cpu_rev = 0;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2391da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
2421da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /* circular buffer */
2451da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size);
2461da177e4SLinus Torvalds static void edge_buf_free(struct edge_buf *eb);
2471da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb);
2481da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb);
2491da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb);
2501da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2511da177e4SLinus Torvalds 	unsigned int count);
2521da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2531da177e4SLinus Torvalds 	unsigned int count);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static int TIReadVendorRequestSync (struct usb_device *dev,
2571da177e4SLinus Torvalds 				__u8		request,
2581da177e4SLinus Torvalds 				__u16		value,
2591da177e4SLinus Torvalds 				__u16		index,
2601da177e4SLinus Torvalds 				u8 		*data,
2611da177e4SLinus Torvalds 				int		size)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	int status;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	status = usb_control_msg (dev,
2661da177e4SLinus Torvalds 				usb_rcvctrlpipe(dev, 0),
2671da177e4SLinus Torvalds 				request,
2681da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
2691da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
2701da177e4SLinus Torvalds 				 USB_DIR_IN),
2711da177e4SLinus Torvalds 				value,
2721da177e4SLinus Torvalds 				index,
2731da177e4SLinus Torvalds 				data,
2741da177e4SLinus Torvalds 				size,
2751da177e4SLinus Torvalds 				1000);
2761da177e4SLinus Torvalds 	if (status < 0)
2771da177e4SLinus Torvalds 		return status;
2781da177e4SLinus Torvalds 	if (status != size) {
2791da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
2801da177e4SLinus Torvalds 		     __FUNCTION__, size, status);
2811da177e4SLinus Torvalds 		return -ECOMM;
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static int TISendVendorRequestSync (struct usb_device *dev,
2871da177e4SLinus Torvalds 				__u8		request,
2881da177e4SLinus Torvalds 				__u16		value,
2891da177e4SLinus Torvalds 				__u16		index,
2901da177e4SLinus Torvalds 				u8 		*data,
2911da177e4SLinus Torvalds 				int		size)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	int status;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	status = usb_control_msg (dev,
2961da177e4SLinus Torvalds 				usb_sndctrlpipe(dev, 0),
2971da177e4SLinus Torvalds 				request,
2981da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
2991da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
3001da177e4SLinus Torvalds 				 USB_DIR_OUT),
3011da177e4SLinus Torvalds 				value,
3021da177e4SLinus Torvalds 				index,
3031da177e4SLinus Torvalds 				data,
3041da177e4SLinus Torvalds 				size,
3051da177e4SLinus Torvalds 				1000);
3061da177e4SLinus Torvalds 	if (status < 0)
3071da177e4SLinus Torvalds 		return status;
3081da177e4SLinus Torvalds 	if (status != size) {
3091da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
3101da177e4SLinus Torvalds 		     __FUNCTION__, size, status);
3111da177e4SLinus Torvalds 		return -ECOMM;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
3171da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
3181da177e4SLinus Torvalds 				int size)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	return TISendVendorRequestSync (dev,
3211da177e4SLinus Torvalds 					  command,	  		// Request
3221da177e4SLinus Torvalds 					  value,			// wValue
3231da177e4SLinus Torvalds 					  moduleid,			// wIndex
3241da177e4SLinus Torvalds 					  data,				// TransferBuffer
3251da177e4SLinus Torvalds 					  size);			// TransferBufferLength
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
3301da177e4SLinus Torvalds static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	int port_number = port->number - port->serial->minor;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return TIWriteCommandSync (port->serial->dev,
3371da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
3381da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
3391da177e4SLinus Torvalds 					mask,
3401da177e4SLinus Torvalds 					NULL,
3411da177e4SLinus Torvalds 					0);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /**
3451da177e4SLinus Torvalds  * TIReadDownloadMemory - Read edgeport memory from TI chip
3461da177e4SLinus Torvalds  * @dev: usb device pointer
3471da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3481da177e4SLinus Torvalds  * @length: Length of above data
3491da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3501da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
3531da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	int status = 0;
3561da177e4SLinus Torvalds 	__u8 read_length;
3571da177e4SLinus Torvalds 	__be16 be_start_address;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
3621da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3631da177e4SLinus Torvalds 	 */
3641da177e4SLinus Torvalds 	while (length) {
3651da177e4SLinus Torvalds 		if (length > 64)
3661da177e4SLinus Torvalds 			read_length= 64;
3671da177e4SLinus Torvalds 		else
3681da177e4SLinus Torvalds 			read_length = (__u8)length;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		if (read_length > 1) {
3711da177e4SLinus Torvalds 			dbg ("%s - @ %x for %d", __FUNCTION__,
3721da177e4SLinus Torvalds 			     start_address, read_length);
3731da177e4SLinus Torvalds 		}
3741da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
3751da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (dev,
3761da177e4SLinus Torvalds 						  UMPC_MEMORY_READ,	// Request
3771da177e4SLinus Torvalds 						  (__u16)address_type,	// wValue (Address type)
3781da177e4SLinus Torvalds 						  (__force __u16)be_start_address,	// wIndex (Address to read)
3791da177e4SLinus Torvalds 						  buffer,		// TransferBuffer
3801da177e4SLinus Torvalds 						  read_length);	// TransferBufferLength
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		if (status) {
3831da177e4SLinus Torvalds 			dbg ("%s - ERROR %x", __FUNCTION__, status);
3841da177e4SLinus Torvalds 			return status;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 		if (read_length > 1) {
3881da177e4SLinus Torvalds 			usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
3891da177e4SLinus Torvalds 					      read_length, buffer);
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 		/* Update pointers/length */
3931da177e4SLinus Torvalds 		start_address += read_length;
3941da177e4SLinus Torvalds 		buffer += read_length;
3951da177e4SLinus Torvalds 		length -= read_length;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	return status;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	return TIReadDownloadMemory (dev,
4041da177e4SLinus Torvalds 				     start_address,
4051da177e4SLinus Torvalds 				     length,
4061da177e4SLinus Torvalds 				     DTK_ADDR_SPACE_XDATA,
4071da177e4SLinus Torvalds 				     buffer);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /* Read edgeport memory to a given block */
4111da177e4SLinus Torvalds static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	int status = 0;
4141da177e4SLinus Torvalds 	int i;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	for (i=0; i< length; i++) {
4171da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (serial->serial->dev,
4181da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
4191da177e4SLinus Torvalds 					serial->TI_I2C_Type,		// wValue (Address type)
4201da177e4SLinus Torvalds 					(__u16)(start_address+i),	// wIndex
4211da177e4SLinus Torvalds 					&buffer[i],			// TransferBuffer
4221da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
4231da177e4SLinus Torvalds 		if (status) {
4241da177e4SLinus Torvalds 			dbg ("%s - ERROR %x", __FUNCTION__, status);
4251da177e4SLinus Torvalds 			return status;
4261da177e4SLinus Torvalds 		}
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
4301da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	return status;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
4381da177e4SLinus Torvalds static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	int status = 0;
4411da177e4SLinus Torvalds 	int i;
4421da177e4SLinus Torvalds 	__u8 temp;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	/* Must do a read before write */
4451da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
4461da177e4SLinus Torvalds 		status = TIReadBootMemory(serial, 0, 1, &temp);
4471da177e4SLinus Torvalds 		if (status)
4481da177e4SLinus Torvalds 			return status;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	for (i=0; i < length; ++i) {
4521da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
4531da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,		// Request
4541da177e4SLinus Torvalds 						buffer[i],			// wValue
4551da177e4SLinus Torvalds 						(__u16)(i+start_address),	// wIndex
4561da177e4SLinus Torvalds 						NULL,				// TransferBuffer
4571da177e4SLinus Torvalds 						0);				// TransferBufferLength
4581da177e4SLinus Torvalds 		if (status)
4591da177e4SLinus Torvalds 			return status;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds   	dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
4631da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	return status;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4701da177e4SLinus Torvalds static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	int status = 0;
4731da177e4SLinus Torvalds 	int write_length;
4741da177e4SLinus Torvalds 	__be16 be_start_address;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/* calulate the number of bytes left in the first page */
4791da177e4SLinus Torvalds 	write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	if (write_length > length)
4821da177e4SLinus Torvalds 		write_length = length;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
4851da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* Write first page */
4881da177e4SLinus Torvalds 	be_start_address = cpu_to_be16 (start_address);
4891da177e4SLinus Torvalds 	status = TISendVendorRequestSync (serial->serial->dev,
4901da177e4SLinus Torvalds 					UMPC_MEMORY_WRITE,	// Request
4911da177e4SLinus Torvalds 					(__u16)address_type,	// wValue
4921da177e4SLinus Torvalds 					(__force __u16)be_start_address,	// wIndex
4931da177e4SLinus Torvalds 					buffer,			// TransferBuffer
4941da177e4SLinus Torvalds 					write_length);
4951da177e4SLinus Torvalds 	if (status) {
4961da177e4SLinus Torvalds 		dbg ("%s - ERROR %d", __FUNCTION__, status);
4971da177e4SLinus Torvalds 		return status;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	length		-= write_length;
5011da177e4SLinus Torvalds 	start_address	+= write_length;
5021da177e4SLinus Torvalds 	buffer		+= write_length;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* We should be aligned now -- can write max page size bytes at a time */
5051da177e4SLinus Torvalds 	while (length) {
5061da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
5071da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
5081da177e4SLinus Torvalds 		else
5091da177e4SLinus Torvalds 			write_length = length;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
5121da177e4SLinus Torvalds 		usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		/* Write next page */
5151da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
5161da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
5171da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,	// Request
5181da177e4SLinus Torvalds 						(__u16)address_type,	// wValue
5191da177e4SLinus Torvalds 						(__force __u16)be_start_address,	// wIndex
5201da177e4SLinus Torvalds 						buffer,	  		// TransferBuffer
5211da177e4SLinus Torvalds 						write_length);		// TransferBufferLength
5221da177e4SLinus Torvalds 		if (status) {
5231da177e4SLinus Torvalds 			dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
5241da177e4SLinus Torvalds 			return status;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		length		-= write_length;
5281da177e4SLinus Torvalds 		start_address	+= write_length;
5291da177e4SLinus Torvalds 		buffer		+= write_length;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 	return status;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
5351da177e4SLinus Torvalds  *
5361da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5371da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5381da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5391da177e4SLinus Torvalds  */
5401da177e4SLinus Torvalds static int TIIsTxActive (struct edgeport_port *port)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	int status;
5431da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
5441da177e4SLinus Torvalds 	__u8 *lsr;
5451da177e4SLinus Torvalds 	int bytes_left = 0;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
5481da177e4SLinus Torvalds 	if (!oedb) {
5491da177e4SLinus Torvalds 		dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
5501da177e4SLinus Torvalds 		return -ENOMEM;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	lsr = kmalloc (1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
5541da177e4SLinus Torvalds 					   as not all platforms can do DMA
5551da177e4SLinus Torvalds 					   from stack */
5561da177e4SLinus Torvalds 	if (!lsr) {
5571da177e4SLinus Torvalds 		kfree(oedb);
5581da177e4SLinus Torvalds 		return -ENOMEM;
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5611da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5621da177e4SLinus Torvalds 			    port->dma_address,
5631da177e4SLinus Torvalds 			    sizeof( *oedb),
5641da177e4SLinus Torvalds 			    (void *)oedb);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	if (status)
5671da177e4SLinus Torvalds 		goto exit_is_tx_active;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	dbg ("%s - XByteCount    0x%X", __FUNCTION__, oedb->XByteCount);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/* and the LSR */
5721da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5731da177e4SLinus Torvalds 			    port->uart_base + UMPMEM_OFFS_UART_LSR,
5741da177e4SLinus Torvalds 			    1,
5751da177e4SLinus Torvalds 			    lsr);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	if (status)
5781da177e4SLinus Torvalds 		goto exit_is_tx_active;
5791da177e4SLinus Torvalds 	dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5821da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80 ) != 0 )
5831da177e4SLinus Torvalds 		bytes_left += 64;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
5861da177e4SLinus Torvalds 		bytes_left += 1;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
5891da177e4SLinus Torvalds exit_is_tx_active:
5901da177e4SLinus Torvalds 	dbg ("%s - return %d", __FUNCTION__, bytes_left );
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	kfree(lsr);
5931da177e4SLinus Torvalds 	kfree(oedb);
5941da177e4SLinus Torvalds 	return bytes_left;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	int baud_rate;
6001da177e4SLinus Torvalds 	struct tty_struct *tty = port->port->tty;
6011da177e4SLinus Torvalds 	wait_queue_t wait;
6021da177e4SLinus Torvalds 	unsigned long flags;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	if (!timeout)
6051da177e4SLinus Torvalds 		timeout = (HZ*EDGE_CLOSING_WAIT)/100;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* wait for data to drain from the buffer */
6081da177e4SLinus Torvalds 	spin_lock_irqsave(&port->ep_lock, flags);
6091da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
6101da177e4SLinus Torvalds 	add_wait_queue(&tty->write_wait, &wait);
6111da177e4SLinus Torvalds 	for (;;) {
6121da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
6131da177e4SLinus Torvalds 		if (edge_buf_data_avail(port->ep_out_buf) == 0
6141da177e4SLinus Torvalds 		|| timeout == 0 || signal_pending(current)
6151da177e4SLinus Torvalds 		|| !usb_get_intfdata(port->port->serial->interface))  /* disconnect */
6161da177e4SLinus Torvalds 			break;
6171da177e4SLinus Torvalds 		spin_unlock_irqrestore(&port->ep_lock, flags);
6181da177e4SLinus Torvalds 		timeout = schedule_timeout(timeout);
6191da177e4SLinus Torvalds 		spin_lock_irqsave(&port->ep_lock, flags);
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
6221da177e4SLinus Torvalds 	remove_wait_queue(&tty->write_wait, &wait);
6231da177e4SLinus Torvalds 	if (flush)
6241da177e4SLinus Torvalds 		edge_buf_clear(port->ep_out_buf);
6251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&port->ep_lock, flags);
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	/* wait for data to drain from the device */
6281da177e4SLinus Torvalds 	timeout += jiffies;
6291da177e4SLinus Torvalds 	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
6301da177e4SLinus Torvalds 	&& usb_get_intfdata(port->port->serial->interface)) {  /* not disconnected */
6311da177e4SLinus Torvalds 		if (!TIIsTxActive(port))
6321da177e4SLinus Torvalds 			break;
6331da177e4SLinus Torvalds 		msleep(10);
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/* disconnected */
6371da177e4SLinus Torvalds 	if (!usb_get_intfdata(port->port->serial->interface))
6381da177e4SLinus Torvalds 		return;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/* wait one more character time, based on baud rate */
6411da177e4SLinus Torvalds 	/* (TIIsTxActive doesn't seem to wait for the last byte) */
6421da177e4SLinus Torvalds 	if ((baud_rate=port->baud_rate) == 0)
6431da177e4SLinus Torvalds 		baud_rate = 50;
6441da177e4SLinus Torvalds 	msleep(max(1,(10000+baud_rate-1)/baud_rate));
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static int TIChooseConfiguration (struct usb_device *dev)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	// There may be multiple configurations on this device, in which case
6501da177e4SLinus Torvalds 	// we would need to read and parse all of them to find out which one
6511da177e4SLinus Torvalds 	// we want. However, we just support one config at this point,
6521da177e4SLinus Torvalds 	// configuration # 1, which is Config Descriptor 0.
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
6551da177e4SLinus Torvalds 	dbg ("%s - MAX Power            = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
6581da177e4SLinus Torvalds 		dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
6591da177e4SLinus Torvalds 		return -ENODEV;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	return 0;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	int status;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6701da177e4SLinus Torvalds 		status = TIReadDownloadMemory (serial->serial->dev,
6711da177e4SLinus Torvalds 					       start_address,
6721da177e4SLinus Torvalds 					       length,
6731da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6741da177e4SLinus Torvalds 					       buffer);
6751da177e4SLinus Torvalds 	} else {
6761da177e4SLinus Torvalds 		status = TIReadBootMemory (serial,
6771da177e4SLinus Torvalds 					   start_address,
6781da177e4SLinus Torvalds 					   length,
6791da177e4SLinus Torvalds 					   buffer);
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	return status;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
6881da177e4SLinus Torvalds 		return TIWriteBootMemory (serial,
6891da177e4SLinus Torvalds 					  start_address,
6901da177e4SLinus Torvalds 					  length,
6911da177e4SLinus Torvalds 					  buffer);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
6941da177e4SLinus Torvalds 		return TIWriteDownloadI2C (serial,
6951da177e4SLinus Torvalds 					   start_address,
6961da177e4SLinus Torvalds 					   length,
6971da177e4SLinus Torvalds 					   serial->TI_I2C_Type,
6981da177e4SLinus Torvalds 					   buffer);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	return -EINVAL;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
7061da177e4SLinus Torvalds static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	int start_address;
7091da177e4SLinus Torvalds 	int status;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
7121da177e4SLinus Torvalds 	start_address = 2;
7131da177e4SLinus Torvalds 	do {
7141da177e4SLinus Torvalds 		status = TIReadRom (serial,
7151da177e4SLinus Torvalds 				   start_address,
7161da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
7171da177e4SLinus Torvalds 				   (__u8 *)rom_desc );
7181da177e4SLinus Torvalds 		if (status)
7191da177e4SLinus Torvalds 			return 0;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
7221da177e4SLinus Torvalds 			return start_address;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	return 0;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /* Validate descriptor checksum */
7321da177e4SLinus Torvalds static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	__u16 i;
7351da177e4SLinus Torvalds 	__u8 cs = 0;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	for (i=0; i < rom_desc->Size; i++) {
7381da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
7411da177e4SLinus Torvalds 		dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
7421da177e4SLinus Torvalds 		return -EINVAL;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 	return 0;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /* Make sure that the I2C image is good */
7481da177e4SLinus Torvalds static int TiValidateI2cImage (struct edgeport_serial *serial)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
7511da177e4SLinus Torvalds 	int status = 0;
7521da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7531da177e4SLinus Torvalds 	int start_address = 2;
7541da177e4SLinus Torvalds 	__u8 *buffer;
7551da177e4SLinus Torvalds 	__u16 ttype;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
7581da177e4SLinus Torvalds 	if (!rom_desc) {
7591da177e4SLinus Torvalds 		dev_err (dev, "%s - out of memory\n", __FUNCTION__);
7601da177e4SLinus Torvalds 		return -ENOMEM;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
7631da177e4SLinus Torvalds 	if (!buffer) {
7641da177e4SLinus Torvalds 		dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
7651da177e4SLinus Torvalds 		kfree (rom_desc);
7661da177e4SLinus Torvalds 		return -ENOMEM;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	// Read the first byte (Signature0) must be 0x52 or 0x10
7701da177e4SLinus Torvalds 	status = TIReadRom (serial, 0, 1, buffer);
7711da177e4SLinus Torvalds 	if (status)
7721da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
7751da177e4SLinus Torvalds 		dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
7761da177e4SLinus Torvalds 		status = -ENODEV;
7771da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	do {
7811da177e4SLinus Torvalds 		// Validate the I2C
7821da177e4SLinus Torvalds 		status = TIReadRom (serial,
7831da177e4SLinus Torvalds 				start_address,
7841da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7851da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7861da177e4SLinus Torvalds 		if (status)
7871da177e4SLinus Torvalds 			break;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
7901da177e4SLinus Torvalds 			status = -ENODEV;
7911da177e4SLinus Torvalds 			dbg ("%s - structure too big, erroring out.", __FUNCTION__);
7921da177e4SLinus Torvalds 			break;
7931da177e4SLinus Torvalds 		}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 		// Skip type 2 record
7981da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
7991da177e4SLinus Torvalds 		if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
8001da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
8011da177e4SLinus Torvalds 			// Read the descriptor data
8021da177e4SLinus Torvalds 			status = TIReadRom(serial,
8031da177e4SLinus Torvalds 						start_address+sizeof(struct ti_i2c_desc),
8041da177e4SLinus Torvalds 						rom_desc->Size,
8051da177e4SLinus Torvalds 						buffer);
8061da177e4SLinus Torvalds 			if (status)
8071da177e4SLinus Torvalds 				break;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 			status = ValidChecksum(rom_desc, buffer);
8101da177e4SLinus Torvalds 			if (status)
8111da177e4SLinus Torvalds 				break;
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
8181da177e4SLinus Torvalds 		status = -ENODEV;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds ExitTiValidateI2cImage:
8211da177e4SLinus Torvalds 	kfree (buffer);
8221da177e4SLinus Torvalds 	kfree (rom_desc);
8231da177e4SLinus Torvalds 	return status;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	int status;
8291da177e4SLinus Torvalds 	int start_address;
8301da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
8311da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
8341da177e4SLinus Torvalds 	if (!rom_desc) {
8351da177e4SLinus Torvalds 		dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
8361da177e4SLinus Torvalds 		return -ENOMEM;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 	start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	if (!start_address) {
8411da177e4SLinus Torvalds 		dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
8421da177e4SLinus Torvalds 		status = -ENODEV;
8431da177e4SLinus Torvalds 		goto exit;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	// Read the descriptor data
8471da177e4SLinus Torvalds 	status = TIReadRom (serial,
8481da177e4SLinus Torvalds 				start_address+sizeof(struct ti_i2c_desc),
8491da177e4SLinus Torvalds 				rom_desc->Size,
8501da177e4SLinus Torvalds 				buffer);
8511da177e4SLinus Torvalds 	if (status)
8521da177e4SLinus Torvalds 		goto exit;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	status = ValidChecksum(rom_desc, buffer);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
8571da177e4SLinus Torvalds 	dbg ( "%s - IonConfig      0x%x", __FUNCTION__, desc->IonConfig 	);
8581da177e4SLinus Torvalds 	dbg ( "%s - Version          %d", __FUNCTION__, desc->Version	  	);
8591da177e4SLinus Torvalds 	dbg ( "%s - Cpu/Board      0x%x", __FUNCTION__, desc->CpuRev_BoardRev	);
8601da177e4SLinus Torvalds 	dbg ( "%s - NumPorts         %d", __FUNCTION__, desc->NumPorts  	);
8611da177e4SLinus Torvalds 	dbg ( "%s - NumVirtualPorts  %d", __FUNCTION__, desc->NumVirtualPorts	);
8621da177e4SLinus Torvalds 	dbg ( "%s - TotalPorts       %d", __FUNCTION__, desc->TotalPorts  	);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds exit:
8651da177e4SLinus Torvalds 	kfree (rom_desc);
8661da177e4SLinus Torvalds 	return status;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds /* Build firmware header used for firmware update */
8701da177e4SLinus Torvalds static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	__u8 *buffer;
8731da177e4SLinus Torvalds 	int buffer_size;
8741da177e4SLinus Torvalds 	int i;
8751da177e4SLinus Torvalds 	__u8 cs = 0;
8761da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8771da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8781da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
8811da177e4SLinus Torvalds 	// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
8821da177e4SLinus Torvalds 	// will download the latest firmware (padded to 15.5k) into the UMP ram.
8831da177e4SLinus Torvalds 	// And finally when the device comes back up in download mode the driver will cause
8841da177e4SLinus Torvalds 	// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
8851da177e4SLinus Torvalds 	// the record type from 0xf2 to 0x02.
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	// Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
8881da177e4SLinus Torvalds 	buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	buffer = kmalloc (buffer_size, GFP_KERNEL);
8911da177e4SLinus Torvalds 	if (!buffer) {
8921da177e4SLinus Torvalds 		dev_err (dev, "%s - out of memory\n", __FUNCTION__);
8931da177e4SLinus Torvalds 		return -ENOMEM;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	// Set entire image of 0xffs
8971da177e4SLinus Torvalds 	memset (buffer, 0xff, buffer_size);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	// Copy version number into firmware record
9001da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
9031da177e4SLinus Torvalds 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	// Pointer to fw_down memory image
9061da177e4SLinus Torvalds 	img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
9091da177e4SLinus Torvalds 		&PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
9101da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
9131da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	kfree (buffer);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	// Build new header
9191da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
9201da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
9231da177e4SLinus Torvalds 	i2c_header->Size	= (__u16)buffer_size;
9241da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
9251da177e4SLinus Torvalds 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
9261da177e4SLinus Torvalds 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	return 0;
9291da177e4SLinus Torvalds }
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
9321da177e4SLinus Torvalds static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	int status;
9351da177e4SLinus Torvalds 	__u8 data;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	// Try to read type 2
9381da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9391da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9401da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_II,	// wValue (Address type)
9411da177e4SLinus Torvalds 					0,		 		// wIndex
9421da177e4SLinus Torvalds 					&data,				// TransferBuffer
9431da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9441da177e4SLinus Torvalds 	if (status)
9451da177e4SLinus Torvalds 		dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
9461da177e4SLinus Torvalds 	else
9471da177e4SLinus Torvalds 		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
9481da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
9491da177e4SLinus Torvalds 		dbg ("%s - ROM_TYPE_II", __FUNCTION__);
9501da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9511da177e4SLinus Torvalds 		return 0;
9521da177e4SLinus Torvalds 	}
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	// Try to read type 3
9551da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9561da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9571da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_III,	// wValue (Address type)
9581da177e4SLinus Torvalds 					0,				// wIndex
9591da177e4SLinus Torvalds 					&data,				// TransferBuffer
9601da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9611da177e4SLinus Torvalds 	if (status)
9621da177e4SLinus Torvalds 		dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
9631da177e4SLinus Torvalds 	else
9641da177e4SLinus Torvalds 		dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
9651da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
9661da177e4SLinus Torvalds 		dbg ("%s - ROM_TYPE_III", __FUNCTION__);
9671da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
9681da177e4SLinus Torvalds 		return 0;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	dbg ("%s - Unknown", __FUNCTION__);
9721da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9731da177e4SLinus Torvalds 	return -ENODEV;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	int status;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	status = usb_bulk_msg (serial->dev,
9811da177e4SLinus Torvalds 				usb_sndbulkpipe(serial->dev,
9821da177e4SLinus Torvalds 						serial->port[0]->bulk_out_endpointAddress),
9831da177e4SLinus Torvalds 				buffer,
9841da177e4SLinus Torvalds 				length,
9851da177e4SLinus Torvalds 				num_sent,
9861da177e4SLinus Torvalds 				1000);
9871da177e4SLinus Torvalds 	return status;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
9911da177e4SLinus Torvalds static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	int status = 0;
9941da177e4SLinus Torvalds 	int pos;
9951da177e4SLinus Torvalds 	int transfer;
9961da177e4SLinus Torvalds 	int done;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	// Transfer firmware image
9991da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
10001da177e4SLinus Torvalds 		// Read the next buffer from file
10011da177e4SLinus Torvalds 		transfer = image_length - pos;
10021da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
10031da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 		// Transfer data
10061da177e4SLinus Torvalds 		status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
10071da177e4SLinus Torvalds 		if (status)
10081da177e4SLinus Torvalds 			break;
10091da177e4SLinus Torvalds 		// Advance buffer pointer
10101da177e4SLinus Torvalds 		pos += done;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	return status;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds // FIXME!!!
10171da177e4SLinus Torvalds static int TIConfigureBootDevice (struct usb_device *dev)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	return 0;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /**
10231da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
10241da177e4SLinus Torvalds  *
10251da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
10261da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
10271da177e4SLinus Torvalds  */
10281da177e4SLinus Torvalds static int TIDownloadFirmware (struct edgeport_serial *serial)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
10311da177e4SLinus Torvalds 	int status = 0;
10321da177e4SLinus Torvalds 	int start_address;
10331da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
10341da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
10351da177e4SLinus Torvalds 	int download_cur_ver;
10361da177e4SLinus Torvalds 	int download_new_ver;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
10391da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10401da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10411da177e4SLinus Torvalds 	 */
10421da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10451da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	status = TIChooseConfiguration (serial->serial->dev);
10481da177e4SLinus Torvalds 	if (status)
10491da177e4SLinus Torvalds 		return status;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10521da177e4SLinus Torvalds 	if (!interface) {
10531da177e4SLinus Torvalds 		dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
10541da177e4SLinus Torvalds 		return -ENODEV;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	// Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10581da177e4SLinus Torvalds 	// if we have more than one endpoint we are definitely in download mode
10591da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
10601da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
10611da177e4SLinus Torvalds 	else
10621da177e4SLinus Torvalds 		// Otherwise we will remain in configuring mode
10631da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	// Save Download Version Number
10661da177e4SLinus Torvalds 	OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
10671da177e4SLinus Torvalds 	OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
10681da177e4SLinus Torvalds 	OperationalCodeImageVersion.BuildNumber	 = PagableOperationalCodeImageVersion.BuildNumber;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	/********************************************************************/
10711da177e4SLinus Torvalds 	/* Download Mode */
10721da177e4SLinus Torvalds 	/********************************************************************/
10731da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
10741da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 		dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 		status = TiValidateI2cImage (serial);
10791da177e4SLinus Torvalds 		if (status) {
10801da177e4SLinus Torvalds 			dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
10811da177e4SLinus Torvalds 			     __FUNCTION__);
10821da177e4SLinus Torvalds 			return status;
10831da177e4SLinus Torvalds 		}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 		/* Validate Hardware version number
10861da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
10871da177e4SLinus Torvalds 		 */
10881da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
10891da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
10901da177e4SLinus Torvalds 			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
10911da177e4SLinus Torvalds 			return -ENOMEM;
10921da177e4SLinus Torvalds 		}
10931da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
10941da177e4SLinus Torvalds 		if (status) {
10951da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
10961da177e4SLinus Torvalds 			return status;
10971da177e4SLinus Torvalds 		}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		// Check version number of ION descriptor
11001da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
11011da177e4SLinus Torvalds 			dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
11021da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
11031da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11041da177e4SLinus Torvalds 		   	return -EINVAL;
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
11081da177e4SLinus Torvalds 		if (!rom_desc) {
11091da177e4SLinus Torvalds 			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
11101da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11111da177e4SLinus Torvalds 			return -ENOMEM;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 		// Search for type 2 record (firmware record)
11151da177e4SLinus Torvalds 		if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
11161da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
11171da177e4SLinus Torvalds 			__u8 record;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 			dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 			firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
11221da177e4SLinus Torvalds 			if (!firmware_version) {
11231da177e4SLinus Torvalds 				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
11241da177e4SLinus Torvalds 				kfree (rom_desc);
11251da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11261da177e4SLinus Torvalds 				return -ENOMEM;
11271da177e4SLinus Torvalds 			}
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 			// Validate version number
11301da177e4SLinus Torvalds 			// Read the descriptor data
11311da177e4SLinus Torvalds 			status = TIReadRom (serial,
11321da177e4SLinus Torvalds 					start_address+sizeof(struct ti_i2c_desc),
11331da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
11341da177e4SLinus Torvalds 					(__u8 *)firmware_version);
11351da177e4SLinus Torvalds 			if (status) {
11361da177e4SLinus Torvalds 				kfree (firmware_version);
11371da177e4SLinus Torvalds 				kfree (rom_desc);
11381da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11391da177e4SLinus Torvalds 				return status;
11401da177e4SLinus Torvalds 			}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 			// Check version number of download with current version in I2c
11431da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
11441da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
11451da177e4SLinus Torvalds 			download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
11461da177e4SLinus Torvalds 					   (OperationalCodeImageVersion.MinorVersion);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 			dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
11491da177e4SLinus Torvalds 			     __FUNCTION__,
11501da177e4SLinus Torvalds 			     firmware_version->Ver_Major,
11511da177e4SLinus Torvalds 			     firmware_version->Ver_Minor,
11521da177e4SLinus Torvalds 			     OperationalCodeImageVersion.MajorVersion,
11531da177e4SLinus Torvalds 			     OperationalCodeImageVersion.MinorVersion);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 			// Check if we have an old version in the I2C and update if necessary
11561da177e4SLinus Torvalds 			if (download_cur_ver != download_new_ver) {
11571da177e4SLinus Torvalds 				dbg ("%s - Update I2C Download from %d.%d to %d.%d",
11581da177e4SLinus Torvalds 				     __FUNCTION__,
11591da177e4SLinus Torvalds 				     firmware_version->Ver_Major,
11601da177e4SLinus Torvalds 				     firmware_version->Ver_Minor,
11611da177e4SLinus Torvalds 				     OperationalCodeImageVersion.MajorVersion,
11621da177e4SLinus Torvalds 				     OperationalCodeImageVersion.MinorVersion);
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 				// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
11651da177e4SLinus Torvalds 				// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
11661da177e4SLinus Torvalds 				// will download the latest firmware (padded to 15.5k) into the UMP ram.
11671da177e4SLinus Torvalds 				// And finally when the device comes back up in download mode the driver will cause
11681da177e4SLinus Torvalds 				// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
11691da177e4SLinus Torvalds 				// the record type from 0xf2 to 0x02.
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 				record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 				// Change the I2C Firmware record type to 0xf2 to trigger an update
11741da177e4SLinus Torvalds 				status = TIWriteRom (serial,
11751da177e4SLinus Torvalds 							start_address,
11761da177e4SLinus Torvalds 							sizeof(record),
11771da177e4SLinus Torvalds 							&record);
11781da177e4SLinus Torvalds 				if (status) {
11791da177e4SLinus Torvalds 					kfree (firmware_version);
11801da177e4SLinus Torvalds 					kfree (rom_desc);
11811da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
11821da177e4SLinus Torvalds 					return status;
11831da177e4SLinus Torvalds 				}
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 				// verify the write -- must do this in order for write to
11861da177e4SLinus Torvalds 				// complete before we do the hardware reset
11871da177e4SLinus Torvalds 				status = TIReadRom (serial,
11881da177e4SLinus Torvalds 							start_address,
11891da177e4SLinus Torvalds 							sizeof(record),
11901da177e4SLinus Torvalds 							&record);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 				if (status) {
11931da177e4SLinus Torvalds 					kfree (firmware_version);
11941da177e4SLinus Torvalds 					kfree (rom_desc);
11951da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
11961da177e4SLinus Torvalds 					return status;
11971da177e4SLinus Torvalds 				}
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
12001da177e4SLinus Torvalds 					dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
12011da177e4SLinus Torvalds 					kfree (firmware_version);
12021da177e4SLinus Torvalds 					kfree (rom_desc);
12031da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12041da177e4SLinus Torvalds 					return -ENODEV;
12051da177e4SLinus Torvalds 				}
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 				dbg ("%s - HARDWARE RESET", __FUNCTION__);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 				// Reset UMP -- Back to BOOT MODE
12101da177e4SLinus Torvalds 				status = TISendVendorRequestSync (serial->serial->dev,
12111da177e4SLinus Torvalds 								UMPC_HARDWARE_RESET,	// Request
12121da177e4SLinus Torvalds 								0,			// wValue
12131da177e4SLinus Torvalds 								0,			// wIndex
12141da177e4SLinus Torvalds 								NULL,			// TransferBuffer
12151da177e4SLinus Torvalds 								0);			// TransferBufferLength
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 				dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 				/* return an error on purpose. */
12201da177e4SLinus Torvalds 				kfree (firmware_version);
12211da177e4SLinus Torvalds 				kfree (rom_desc);
12221da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12231da177e4SLinus Torvalds 				return -ENODEV;
12241da177e4SLinus Torvalds 			}
12251da177e4SLinus Torvalds 			kfree (firmware_version);
12261da177e4SLinus Torvalds 		}
12271da177e4SLinus Torvalds 		// Search for type 0xF2 record (firmware blank record)
12281da177e4SLinus Torvalds 		else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
12291da177e4SLinus Torvalds 			#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
12301da177e4SLinus Torvalds 			__u8 *header;
12311da177e4SLinus Torvalds 			__u8 *vheader;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 			header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
12341da177e4SLinus Torvalds 			if (!header) {
12351da177e4SLinus Torvalds 				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
12361da177e4SLinus Torvalds 				kfree (rom_desc);
12371da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12381da177e4SLinus Torvalds 				return -ENOMEM;
12391da177e4SLinus Torvalds 			}
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 			vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
12421da177e4SLinus Torvalds 			if (!vheader) {
12431da177e4SLinus Torvalds 				dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
12441da177e4SLinus Torvalds 				kfree (header);
12451da177e4SLinus Torvalds 				kfree (rom_desc);
12461da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12471da177e4SLinus Torvalds 				return -ENOMEM;
12481da177e4SLinus Torvalds 			}
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 			dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 			// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
12531da177e4SLinus Torvalds 			// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
12541da177e4SLinus Torvalds 			// will download the latest firmware (padded to 15.5k) into the UMP ram.
12551da177e4SLinus Torvalds 			// And finally when the device comes back up in download mode the driver will cause
12561da177e4SLinus Torvalds 			// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
12571da177e4SLinus Torvalds 			// the record type from 0xf2 to 0x02.
12581da177e4SLinus Torvalds 			status = BuildI2CFirmwareHeader(header, dev);
12591da177e4SLinus Torvalds 			if (status) {
12601da177e4SLinus Torvalds 				kfree (vheader);
12611da177e4SLinus Torvalds 				kfree (header);
12621da177e4SLinus Torvalds 				kfree (rom_desc);
12631da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12641da177e4SLinus Torvalds 				return status;
12651da177e4SLinus Torvalds 			}
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 			// Update I2C with type 0xf2 record with correct size and checksum
12681da177e4SLinus Torvalds 			status = TIWriteRom (serial,
12691da177e4SLinus Torvalds 						start_address,
12701da177e4SLinus Torvalds 						HEADER_SIZE,
12711da177e4SLinus Torvalds 						header);
12721da177e4SLinus Torvalds 			if (status) {
12731da177e4SLinus Torvalds 				kfree (vheader);
12741da177e4SLinus Torvalds 				kfree (header);
12751da177e4SLinus Torvalds 				kfree (rom_desc);
12761da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12771da177e4SLinus Torvalds 				return status;
12781da177e4SLinus Torvalds 			}
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 			// verify the write -- must do this in order for write to
12811da177e4SLinus Torvalds 			// complete before we do the hardware reset
12821da177e4SLinus Torvalds 			status = TIReadRom (serial,
12831da177e4SLinus Torvalds 						start_address,
12841da177e4SLinus Torvalds 						HEADER_SIZE,
12851da177e4SLinus Torvalds 						vheader);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 			if (status) {
12881da177e4SLinus Torvalds 				dbg ("%s - can't read header back", __FUNCTION__);
12891da177e4SLinus Torvalds 				kfree (vheader);
12901da177e4SLinus Torvalds 				kfree (header);
12911da177e4SLinus Torvalds 				kfree (rom_desc);
12921da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12931da177e4SLinus Torvalds 				return status;
12941da177e4SLinus Torvalds 			}
12951da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
12961da177e4SLinus Torvalds 				dbg ("%s - write download record failed", __FUNCTION__);
12971da177e4SLinus Torvalds 				kfree (vheader);
12981da177e4SLinus Torvalds 				kfree (header);
12991da177e4SLinus Torvalds 				kfree (rom_desc);
13001da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13011da177e4SLinus Torvalds 				return status;
13021da177e4SLinus Torvalds 			}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 			kfree (vheader);
13051da177e4SLinus Torvalds 			kfree (header);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 			dbg ("%s - Start firmware update", __FUNCTION__);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 			// Tell firmware to copy download image into I2C
13101da177e4SLinus Torvalds 			status = TISendVendorRequestSync (serial->serial->dev,
13111da177e4SLinus Torvalds 						UMPC_COPY_DNLD_TO_I2C,	// Request
13121da177e4SLinus Torvalds 						0,			// wValue
13131da177e4SLinus Torvalds 						0,			// wIndex
13141da177e4SLinus Torvalds 						NULL,			// TransferBuffer
13151da177e4SLinus Torvalds 						0);			// TransferBufferLength
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		  	dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
13181da177e4SLinus Torvalds 			if (status) {
13191da177e4SLinus Torvalds 				dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
13201da177e4SLinus Torvalds 				kfree (rom_desc);
13211da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13221da177e4SLinus Torvalds 				return status;
13231da177e4SLinus Torvalds 			}
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		// The device is running the download code
13271da177e4SLinus Torvalds 		kfree (rom_desc);
13281da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
13291da177e4SLinus Torvalds 		return 0;
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	/********************************************************************/
13331da177e4SLinus Torvalds 	/* Boot Mode */
13341da177e4SLinus Torvalds 	/********************************************************************/
13351da177e4SLinus Torvalds 	dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
13361da177e4SLinus Torvalds 	     __FUNCTION__);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	// Configure the TI device so we can use the BULK pipes for download
13391da177e4SLinus Torvalds 	status = TIConfigureBootDevice (serial->serial->dev);
13401da177e4SLinus Torvalds 	if (status)
13411da177e4SLinus Torvalds 		return status;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
13441da177e4SLinus Torvalds 		dbg ("%s - VID = 0x%x", __FUNCTION__,
13451da177e4SLinus Torvalds 		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13461da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13471da177e4SLinus Torvalds 		goto StayInBootMode;
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	// We have an ION device (I2c Must be programmed)
13511da177e4SLinus Torvalds 	// Determine I2C image type
13521da177e4SLinus Torvalds 	if (TIGetI2cTypeInBootMode(serial)) {
13531da177e4SLinus Torvalds 		goto StayInBootMode;
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	// Registry variable set?
13571da177e4SLinus Torvalds 	if (TIStayInBootMode) {
13581da177e4SLinus Torvalds 		dbg ("%s - TIStayInBootMode", __FUNCTION__);
13591da177e4SLinus Torvalds 		goto StayInBootMode;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	// Check for ION Vendor ID and that the I2C is valid
13631da177e4SLinus Torvalds 	if (!TiValidateI2cImage(serial)) {
13641da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
13651da177e4SLinus Torvalds 		int i;
13661da177e4SLinus Torvalds 		__u8 cs = 0;
13671da177e4SLinus Torvalds 		__u8 *buffer;
13681da177e4SLinus Torvalds 		int buffer_size;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		/* Validate Hardware version number
13711da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
13721da177e4SLinus Torvalds 		 */
13731da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
13741da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
13751da177e4SLinus Torvalds 			dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
13761da177e4SLinus Torvalds 			return -ENOMEM;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
13791da177e4SLinus Torvalds 		if (status) {
13801da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
13811da177e4SLinus Torvalds 			goto StayInBootMode;
13821da177e4SLinus Torvalds 		}
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 		// Check for version 2
13851da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
13861da177e4SLinus Torvalds 			dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
13871da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
13881da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
13891da177e4SLinus Torvalds 			goto StayInBootMode;
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 		// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
13951da177e4SLinus Torvalds 		// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
13961da177e4SLinus Torvalds 		// will download the latest firmware (padded to 15.5k) into the UMP ram.
13971da177e4SLinus Torvalds 		// And finally when the device comes back up in download mode the driver will cause
13981da177e4SLinus Torvalds 		// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
13991da177e4SLinus Torvalds 		// the record type from 0xf2 to 0x02.
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		/*
14021da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14031da177e4SLinus Torvalds 		 * or could we do this in place!
14041da177e4SLinus Torvalds 		 */
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		// Allocate a 15.5k buffer + 3 byte header
14071da177e4SLinus Torvalds 		buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
14081da177e4SLinus Torvalds 		buffer = kmalloc (buffer_size, GFP_KERNEL);
14091da177e4SLinus Torvalds 		if (!buffer) {
14101da177e4SLinus Torvalds 			dev_err (dev, "%s - out of memory\n", __FUNCTION__);
14111da177e4SLinus Torvalds 			return -ENOMEM;
14121da177e4SLinus Torvalds 		}
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 		// Initialize the buffer to 0xff (pad the buffer)
14151da177e4SLinus Torvalds 		memset (buffer, 0xff, buffer_size);
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 		memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 		for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
14201da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14211da177e4SLinus Torvalds 		}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 		// update length and checksum after padding
14261da177e4SLinus Torvalds 		header->Length 	 = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
14271da177e4SLinus Torvalds 		header->CheckSum = cs;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 		// Download the operational code
14301da177e4SLinus Torvalds 		dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
14311da177e4SLinus Torvalds 		status = TIDownloadCodeImage (serial, buffer, buffer_size);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 		kfree (buffer);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 		if (status) {
14361da177e4SLinus Torvalds 	  		dbg ("%s - Error downloading operational code image", __FUNCTION__);
14371da177e4SLinus Torvalds 			return status;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 		// Device will reboot
14411da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds   		dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 		/* return an error on purpose */
14461da177e4SLinus Torvalds 		return -ENODEV;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds StayInBootMode:
14501da177e4SLinus Torvalds 	// Eprom is invalid or blank stay in boot mode
14511da177e4SLinus Torvalds 	dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
14521da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	return 0;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds static int TISetDtr (struct edgeport_port *port)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
14631da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_DTR;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14661da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
14671da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14681da177e4SLinus Torvalds 				1,	/* set */
14691da177e4SLinus Torvalds 				NULL,
14701da177e4SLinus Torvalds 				0);
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static int TIClearDtr (struct edgeport_port *port)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
14781da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_DTR;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14811da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
14821da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14831da177e4SLinus Torvalds 				0,	/* clear */
14841da177e4SLinus Torvalds 				NULL,
14851da177e4SLinus Torvalds 				0);
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds static int TISetRts (struct edgeport_port *port)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
14931da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_RTS;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14961da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
14971da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14981da177e4SLinus Torvalds 				1,	/* set */
14991da177e4SLinus Torvalds 				NULL,
15001da177e4SLinus Torvalds 				0);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds static int TIClearRts (struct edgeport_port *port)
15041da177e4SLinus Torvalds {
15051da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
15081da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_RTS;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15111da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
15121da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15131da177e4SLinus Torvalds 				0,	/* clear */
15141da177e4SLinus Torvalds 				NULL,
15151da177e4SLinus Torvalds 				0);
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds static int TISetLoopBack (struct edgeport_port *port)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15251da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15261da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15271da177e4SLinus Torvalds 				1,	/* set */
15281da177e4SLinus Torvalds 				NULL,
15291da177e4SLinus Torvalds 				0);
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds static int TIClearLoopBack (struct edgeport_port *port)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15391da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15401da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15411da177e4SLinus Torvalds 				0,	/* clear */
15421da177e4SLinus Torvalds 				NULL,
15431da177e4SLinus Torvalds 				0);
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds static int TISetBreak (struct edgeport_port *port)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15531da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15541da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15551da177e4SLinus Torvalds 				1,	/* set */
15561da177e4SLinus Torvalds 				NULL,
15571da177e4SLinus Torvalds 				0);
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds static int TIClearBreak (struct edgeport_port *port)
15611da177e4SLinus Torvalds {
15621da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15671da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15681da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15691da177e4SLinus Torvalds 				0,	/* clear */
15701da177e4SLinus Torvalds 				NULL,
15711da177e4SLinus Torvalds 				0);
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
15751da177e4SLinus Torvalds {
15761da177e4SLinus Torvalds 	int status = 0;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	dbg ("%s - %x", __FUNCTION__, mcr);
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	if (mcr & MCR_DTR)
15811da177e4SLinus Torvalds 		status = TISetDtr (port);
15821da177e4SLinus Torvalds 	else
15831da177e4SLinus Torvalds 		status = TIClearDtr (port);
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	if (status)
15861da177e4SLinus Torvalds 		return status;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	if (mcr & MCR_RTS)
15891da177e4SLinus Torvalds 		status = TISetRts (port);
15901da177e4SLinus Torvalds 	else
15911da177e4SLinus Torvalds 		status = TIClearRts (port);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	if (status)
15941da177e4SLinus Torvalds 		return status;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	if (mcr & MCR_LOOPBACK)
15971da177e4SLinus Torvalds 		status = TISetLoopBack (port);
15981da177e4SLinus Torvalds 	else
15991da177e4SLinus Torvalds 		status = TIClearLoopBack (port);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	return status;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
16071da177e4SLinus Torvalds static __u8 MapLineStatus (__u8 ti_lsr)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	__u8 lsr = 0;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
16121da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
16131da177e4SLinus Torvalds 		lsr |= flagUart;
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
16161da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
16171da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
16181da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
16191da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* receive data available */
16201da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* transmit holding register empty */
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds #undef MAP_FLAG
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	return lsr;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
16281da177e4SLinus Torvalds {
16291da177e4SLinus Torvalds 	struct async_icount *icount;
16301da177e4SLinus Torvalds 	struct tty_struct *tty;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	dbg ("%s - %02x", __FUNCTION__, msr);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
16351da177e4SLinus Torvalds 		icount = &edge_port->icount;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 		/* update input line counters */
16381da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
16391da177e4SLinus Torvalds 			icount->cts++;
16401da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
16411da177e4SLinus Torvalds 			icount->dsr++;
16421da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
16431da177e4SLinus Torvalds 			icount->dcd++;
16441da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
16451da177e4SLinus Torvalds 			icount->rng++;
16461da177e4SLinus Torvalds 		wake_up_interruptible (&edge_port->delta_msr_wait);
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	/* Save the new modem status */
16501da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	tty = edge_port->port->tty;
16531da177e4SLinus Torvalds 	/* handle CTS flow control */
16541da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
16551da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_CTS) {
16561da177e4SLinus Torvalds 			tty->hw_stopped = 0;
16571da177e4SLinus Torvalds 			tty_wakeup(tty);
16581da177e4SLinus Torvalds 		} else {
16591da177e4SLinus Torvalds 			tty->hw_stopped = 1;
16601da177e4SLinus Torvalds 		}
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	return;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	struct async_icount *icount;
16691da177e4SLinus Torvalds 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	dbg ("%s - %02x", __FUNCTION__, new_lsr);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK) {
16761da177e4SLinus Torvalds 		/*
16771da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
16781da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
16791da177e4SLinus Torvalds 		 */
16801da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
16841da177e4SLinus Torvalds 	if (lsr_data && edge_port->port->tty)
16851da177e4SLinus Torvalds 		edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	/* update input line counters */
16881da177e4SLinus Torvalds 	icount = &edge_port->icount;
16891da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
16901da177e4SLinus Torvalds 		icount->brk++;
16911da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
16921da177e4SLinus Torvalds 		icount->overrun++;
16931da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
16941da177e4SLinus Torvalds 		icount->parity++;
16951da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
16961da177e4SLinus Torvalds 		icount->frame++;
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
17011da177e4SLinus Torvalds {
17021da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
17031da177e4SLinus Torvalds 	struct usb_serial_port *port;
17041da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
17051da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17061da177e4SLinus Torvalds 	int length = urb->actual_length;
17071da177e4SLinus Torvalds 	int port_number;
17081da177e4SLinus Torvalds 	int function;
17091da177e4SLinus Torvalds 	int status;
17101da177e4SLinus Torvalds 	__u8 lsr;
17111da177e4SLinus Torvalds 	__u8 msr;
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 	switch (urb->status) {
17161da177e4SLinus Torvalds 	case 0:
17171da177e4SLinus Torvalds 		/* success */
17181da177e4SLinus Torvalds 		break;
17191da177e4SLinus Torvalds 	case -ECONNRESET:
17201da177e4SLinus Torvalds 	case -ENOENT:
17211da177e4SLinus Torvalds 	case -ESHUTDOWN:
17221da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
17231da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
17241da177e4SLinus Torvalds 		return;
17251da177e4SLinus Torvalds 	default:
17261da177e4SLinus Torvalds 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
17271da177e4SLinus Torvalds 		goto exit;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	if (!length) {
17311da177e4SLinus Torvalds 		dbg ("%s - no data in urb", __FUNCTION__);
17321da177e4SLinus Torvalds 		goto exit;
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	if (length != 2) {
17381da177e4SLinus Torvalds 		dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
17391da177e4SLinus Torvalds 		goto exit;
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
17431da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
17441da177e4SLinus Torvalds 	dbg ("%s - port_number %d, function %d, info 0x%x",
17451da177e4SLinus Torvalds 	     __FUNCTION__, port_number, function, data[1]);
17461da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
17471da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
17481da177e4SLinus Torvalds 	if (!edge_port) {
17491da177e4SLinus Torvalds 		dbg ("%s - edge_port not found", __FUNCTION__);
17501da177e4SLinus Torvalds 		return;
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 	switch (function) {
17531da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
17541da177e4SLinus Torvalds 		lsr = MapLineStatus(data[1]);
17551da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
17561da177e4SLinus Torvalds 			/* Save the LSR event for bulk read completion routine */
17571da177e4SLinus Torvalds 			dbg ("%s - LSR Event Port %u LSR Status = %02x",
17581da177e4SLinus Torvalds 			     __FUNCTION__, port_number, lsr);
17591da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
17601da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
17611da177e4SLinus Torvalds 		} else {
17621da177e4SLinus Torvalds 			dbg ("%s - ===== Port %d LSR Status = %02x ======",
17631da177e4SLinus Torvalds 			     __FUNCTION__, port_number, lsr);
17641da177e4SLinus Torvalds 			handle_new_lsr (edge_port, 0, lsr, 0);
17651da177e4SLinus Torvalds 		}
17661da177e4SLinus Torvalds 		break;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_MSR:	// MSR
17691da177e4SLinus Torvalds 		/* Copy MSR from UMP */
17701da177e4SLinus Torvalds 		msr = data[1];
17711da177e4SLinus Torvalds 		dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
17721da177e4SLinus Torvalds 		     __FUNCTION__, port_number, msr);
17731da177e4SLinus Torvalds 		handle_new_msr (edge_port, msr);
17741da177e4SLinus Torvalds 		break;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	default:
17771da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
17781da177e4SLinus Torvalds 			 __FUNCTION__, data[1]);
17791da177e4SLinus Torvalds 		break;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds exit:
17841da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_ATOMIC);
17851da177e4SLinus Torvalds 	if (status)
17861da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
17871da177e4SLinus Torvalds 			 __FUNCTION__, status);
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
17911da177e4SLinus Torvalds {
17921da177e4SLinus Torvalds 	struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
17931da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17941da177e4SLinus Torvalds 	struct tty_struct *tty;
17951da177e4SLinus Torvalds 	int status = 0;
17961da177e4SLinus Torvalds 	int port_number;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	switch (urb->status) {
18011da177e4SLinus Torvalds 	case 0:
18021da177e4SLinus Torvalds 		/* success */
18031da177e4SLinus Torvalds 		break;
18041da177e4SLinus Torvalds 	case -ECONNRESET:
18051da177e4SLinus Torvalds 	case -ENOENT:
18061da177e4SLinus Torvalds 	case -ESHUTDOWN:
18071da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
18081da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
18091da177e4SLinus Torvalds 		return;
18101da177e4SLinus Torvalds 	default:
18111da177e4SLinus Torvalds 		dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
18121da177e4SLinus Torvalds 		     __FUNCTION__, urb->status );
18131da177e4SLinus Torvalds 	}
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	if (urb->status == -EPIPE)
18161da177e4SLinus Torvalds 		goto exit;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	if (urb->status) {
18191da177e4SLinus Torvalds 		dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
18201da177e4SLinus Torvalds 		return;
18211da177e4SLinus Torvalds 	}
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	port_number = edge_port->port->number - edge_port->port->serial->minor;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
18261da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
18271da177e4SLinus Torvalds 		dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
18281da177e4SLinus Torvalds 		     __FUNCTION__, port_number, edge_port->lsr_mask, *data);
18291da177e4SLinus Torvalds 		handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
18301da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
18311da177e4SLinus Torvalds 		--urb->actual_length;
18321da177e4SLinus Torvalds 		++data;
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	tty = edge_port->port->tty;
18361da177e4SLinus Torvalds 	if (tty && urb->actual_length) {
18371da177e4SLinus Torvalds 		usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 		if (edge_port->close_pending) {
18401da177e4SLinus Torvalds 			dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
18411da177e4SLinus Torvalds 		} else {
18421da177e4SLinus Torvalds 			edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 		edge_port->icount.rx += urb->actual_length;
18451da177e4SLinus Torvalds 	}
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds exit:
18481da177e4SLinus Torvalds 	/* continue read unless stopped */
18491da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
18501da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
18511da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
18521da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_ATOMIC);
18531da177e4SLinus Torvalds 	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
18541da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
18551da177e4SLinus Torvalds 	}
18561da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
18571da177e4SLinus Torvalds 	if (status)
18581da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
18591da177e4SLinus Torvalds 			 __FUNCTION__, status);
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	int cnt;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	do {
186733f0f88fSAlan Cox 		cnt = tty_buffer_request_room(tty, length);
186833f0f88fSAlan Cox 		if (cnt < length) {
18691da177e4SLinus Torvalds 			dev_err(dev, "%s - dropping data, %d bytes lost\n",
187033f0f88fSAlan Cox 				__FUNCTION__, length - cnt);
187133f0f88fSAlan Cox 			if(cnt == 0)
187233f0f88fSAlan Cox 				break;
18731da177e4SLinus Torvalds 		}
187433f0f88fSAlan Cox 		tty_insert_flip_string(tty, data, cnt);
18751da177e4SLinus Torvalds 		data += cnt;
18761da177e4SLinus Torvalds 		length -= cnt;
18771da177e4SLinus Torvalds 	} while (length > 0);
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	tty_flip_buffer_push(tty);
18801da177e4SLinus Torvalds }
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds static void edge_bulk_out_callback (struct urb *urb, struct pt_regs *regs)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
18851da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 	dbg ("%s - port %d", __FUNCTION__, port->number);
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 	switch (urb->status) {
18921da177e4SLinus Torvalds 	case 0:
18931da177e4SLinus Torvalds 		/* success */
18941da177e4SLinus Torvalds 		break;
18951da177e4SLinus Torvalds 	case -ECONNRESET:
18961da177e4SLinus Torvalds 	case -ENOENT:
18971da177e4SLinus Torvalds 	case -ESHUTDOWN:
18981da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
18991da177e4SLinus Torvalds 		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
19001da177e4SLinus Torvalds 		return;
19011da177e4SLinus Torvalds 	default:
19021da177e4SLinus Torvalds 		dev_err (&urb->dev->dev,"%s - nonzero write bulk status received: %d\n",
19031da177e4SLinus Torvalds 		     __FUNCTION__, urb->status);
19041da177e4SLinus Torvalds 	}
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	/* send any buffered data */
19071da177e4SLinus Torvalds 	edge_send(port);
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds static int edge_open (struct usb_serial_port *port, struct file * filp)
19111da177e4SLinus Torvalds {
19121da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19131da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19141da177e4SLinus Torvalds 	struct usb_device *dev;
19151da177e4SLinus Torvalds 	struct urb *urb;
19161da177e4SLinus Torvalds 	int port_number;
19171da177e4SLinus Torvalds 	int status;
19181da177e4SLinus Torvalds 	u16 open_settings;
19191da177e4SLinus Torvalds 	u8 transaction_timeout;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	if (edge_port == NULL)
19241da177e4SLinus Torvalds 		return -ENODEV;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	if (port->tty)
19271da177e4SLinus Torvalds 		port->tty->low_latency = low_latency;
19281da177e4SLinus Torvalds 
19291da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
19301da177e4SLinus Torvalds 	switch (port_number) {
19311da177e4SLinus Torvalds 		case 0:
19321da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART1;
19331da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB1_ADDRESS;
19341da177e4SLinus Torvalds 			break;
19351da177e4SLinus Torvalds 		case 1:
19361da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART2;
19371da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB2_ADDRESS;
19381da177e4SLinus Torvalds 			break;
19391da177e4SLinus Torvalds 		default:
19401da177e4SLinus Torvalds 			dev_err (&port->dev, "Unknown port number!!!\n");
19411da177e4SLinus Torvalds 			return -ENODEV;
19421da177e4SLinus Torvalds 	}
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
19451da177e4SLinus Torvalds 	     __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	dev = port->serial->dev;
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
19501da177e4SLinus Torvalds 	init_waitqueue_head (&edge_port->delta_msr_wait);
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	/* turn off loopback */
19531da177e4SLinus Torvalds 	status = TIClearLoopBack (edge_port);
19541da177e4SLinus Torvalds 	if (status) {
19551da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
19561da177e4SLinus Torvalds 			__FUNCTION__, status);
19571da177e4SLinus Torvalds 		return status;
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	/* set up the port settings */
19611da177e4SLinus Torvalds 	edge_set_termios (port, NULL);
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	/* open up the port */
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
19661da177e4SLinus Torvalds 	transaction_timeout = 2;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	// milliseconds to timeout for DMA transfer
19711da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
19721da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
19731da177e4SLinus Torvalds 			     (transaction_timeout << 2));
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
19781da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
19791da177e4SLinus Torvalds 					UMPC_OPEN_PORT,
19801da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
19811da177e4SLinus Torvalds 					open_settings,
19821da177e4SLinus Torvalds 					NULL,
19831da177e4SLinus Torvalds 					0);
19841da177e4SLinus Torvalds 	if (status) {
19851da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
19861da177e4SLinus Torvalds 		return status;
19871da177e4SLinus Torvalds 	}
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	/* Start the DMA? */
19901da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
19911da177e4SLinus Torvalds 					UMPC_START_PORT,
19921da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
19931da177e4SLinus Torvalds 					0,
19941da177e4SLinus Torvalds 					NULL,
19951da177e4SLinus Torvalds 					0);
19961da177e4SLinus Torvalds 	if (status) {
19971da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
19981da177e4SLinus Torvalds 		return status;
19991da177e4SLinus Torvalds 	}
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
20021da177e4SLinus Torvalds 	status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
20031da177e4SLinus Torvalds 	if (status) {
20041da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
20051da177e4SLinus Torvalds 		return status;
20061da177e4SLinus Torvalds 	}
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	/* Read Initial MSR */
20091da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (dev,
20101da177e4SLinus Torvalds 					UMPC_READ_MSR,	// Request
20111da177e4SLinus Torvalds 					0,		// wValue
20121da177e4SLinus Torvalds 					(__u16)(UMPM_UART1_PORT + port_number),	// wIndex (Address)
20131da177e4SLinus Torvalds 					&edge_port->shadow_msr,			// TransferBuffer
20141da177e4SLinus Torvalds 					1);					// TransferBufferLength
20151da177e4SLinus Torvalds 	if (status) {
20161da177e4SLinus Torvalds  		dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
20171da177e4SLinus Torvalds 		return status;
20181da177e4SLinus Torvalds 	}
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	/* Set Initial MCR */
20231da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
20241da177e4SLinus Torvalds 	dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
20271da177e4SLinus Torvalds 	if (down_interruptible(&edge_serial->es_sem))
20281da177e4SLinus Torvalds 		return -ERESTARTSYS;
20291da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
20301da177e4SLinus Torvalds 		/* we are the first port to be opened, let's post the interrupt urb */
20311da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
20321da177e4SLinus Torvalds 		if (!urb) {
20331da177e4SLinus Torvalds 			dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
20341da177e4SLinus Torvalds 			status = -EINVAL;
20351da177e4SLinus Torvalds 			goto up_es_sem;
20361da177e4SLinus Torvalds 		}
20371da177e4SLinus Torvalds 		urb->complete = edge_interrupt_callback;
20381da177e4SLinus Torvalds 		urb->context = edge_serial;
20391da177e4SLinus Torvalds 		urb->dev = dev;
20401da177e4SLinus Torvalds 		status = usb_submit_urb (urb, GFP_KERNEL);
20411da177e4SLinus Torvalds 		if (status) {
20421da177e4SLinus Torvalds 			dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
20431da177e4SLinus Torvalds 			goto up_es_sem;
20441da177e4SLinus Torvalds 		}
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	/*
20481da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
20491da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
20501da177e4SLinus Torvalds 	 */
20511da177e4SLinus Torvalds 	usb_clear_halt (dev, port->write_urb->pipe);
20521da177e4SLinus Torvalds 	usb_clear_halt (dev, port->read_urb->pipe);
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	/* start up our bulk read urb */
20551da177e4SLinus Torvalds 	urb = port->read_urb;
20561da177e4SLinus Torvalds 	if (!urb) {
20571da177e4SLinus Torvalds 		dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
20581da177e4SLinus Torvalds 		status = -EINVAL;
20591da177e4SLinus Torvalds 		goto unlink_int_urb;
20601da177e4SLinus Torvalds 	}
20611da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
20621da177e4SLinus Torvalds 	urb->complete = edge_bulk_in_callback;
20631da177e4SLinus Torvalds 	urb->context = edge_port;
20641da177e4SLinus Torvalds 	urb->dev = dev;
20651da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_KERNEL);
20661da177e4SLinus Torvalds 	if (status) {
20671da177e4SLinus Torvalds 		dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
20681da177e4SLinus Torvalds 		goto unlink_int_urb;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	dbg("%s - exited", __FUNCTION__);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	goto up_es_sem;
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds unlink_int_urb:
20781da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
20791da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
20801da177e4SLinus Torvalds up_es_sem:
20811da177e4SLinus Torvalds 	up(&edge_serial->es_sem);
20821da177e4SLinus Torvalds 	return status;
20831da177e4SLinus Torvalds }
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds static void edge_close (struct usb_serial_port *port, struct file *filp)
20861da177e4SLinus Torvalds {
20871da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
20881da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
20891da177e4SLinus Torvalds 	int port_number;
20901da177e4SLinus Torvalds 	int status;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
20951da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
20961da177e4SLinus Torvalds 	if ((edge_serial == NULL) || (edge_port == NULL))
20971da177e4SLinus Torvalds 		return;
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
21001da177e4SLinus Torvalds 	 * this flag and dump add read data */
21011da177e4SLinus Torvalds 	edge_port->close_pending = 1;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	/* chase the port close and flush */
21041da177e4SLinus Torvalds 	TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
21071da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
21081da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	/* assuming we can still talk to the device,
21111da177e4SLinus Torvalds 	 * send a close port command to it */
21121da177e4SLinus Torvalds 	dbg("%s - send umpc_close_port", __FUNCTION__);
21131da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
21141da177e4SLinus Torvalds 	status = TIWriteCommandSync (port->serial->dev,
21151da177e4SLinus Torvalds 				     UMPC_CLOSE_PORT,
21161da177e4SLinus Torvalds 				     (__u8)(UMPM_UART1_PORT + port_number),
21171da177e4SLinus Torvalds 				     0,
21181da177e4SLinus Torvalds 				     NULL,
21191da177e4SLinus Torvalds 				     0);
21201da177e4SLinus Torvalds 	down(&edge_serial->es_sem);
21211da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
21221da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
21231da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
21241da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
21251da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 	up(&edge_serial->es_sem);
21281da177e4SLinus Torvalds 	edge_port->close_pending = 0;
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	dbg("%s - exited", __FUNCTION__);
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21361da177e4SLinus Torvalds 	unsigned long flags;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (count == 0) {
21411da177e4SLinus Torvalds 		dbg("%s - write request of 0 bytes", __FUNCTION__);
21421da177e4SLinus Torvalds 		return 0;
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	if (edge_port == NULL)
21461da177e4SLinus Torvalds 		return -ENODEV;
21471da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21481da177e4SLinus Torvalds 		return -ENODEV;
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21511da177e4SLinus Torvalds 	count = edge_buf_put(edge_port->ep_out_buf, data, count);
21521da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	edge_send(port);
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	return count;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port)
21601da177e4SLinus Torvalds {
21611da177e4SLinus Torvalds 	int count, result;
21621da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21631da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
21641da177e4SLinus Torvalds 	unsigned long flags;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
21721da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21731da177e4SLinus Torvalds 		return;
21741da177e4SLinus Torvalds 	}
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	count = edge_buf_get(edge_port->ep_out_buf,
21771da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
21781da177e4SLinus Torvalds 				port->bulk_out_size);
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	if (count == 0) {
21811da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21821da177e4SLinus Torvalds 		return;
21831da177e4SLinus Torvalds 	}
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* set up our urb */
21921da177e4SLinus Torvalds 	usb_fill_bulk_urb (port->write_urb, port->serial->dev,
21931da177e4SLinus Torvalds 			   usb_sndbulkpipe (port->serial->dev,
21941da177e4SLinus Torvalds 					    port->bulk_out_endpointAddress),
21951da177e4SLinus Torvalds 			   port->write_urb->transfer_buffer, count,
21961da177e4SLinus Torvalds 			   edge_bulk_out_callback,
21971da177e4SLinus Torvalds 			   port);
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	/* send the data out the bulk port */
22001da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
22011da177e4SLinus Torvalds 	if (result) {
22021da177e4SLinus Torvalds 		dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
22031da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
22041da177e4SLinus Torvalds 		// TODO: reschedule edge_send
22051da177e4SLinus Torvalds 	} else {
22061da177e4SLinus Torvalds 		edge_port->icount.tx += count;
22071da177e4SLinus Torvalds 	}
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
22101da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
22111da177e4SLinus Torvalds 	if (tty) {
22121da177e4SLinus Torvalds 		/* let the tty driver wakeup if it has a special write_wakeup function */
22131da177e4SLinus Torvalds 		tty_wakeup(tty);
22141da177e4SLinus Torvalds 	}
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds static int edge_write_room (struct usb_serial_port *port)
22181da177e4SLinus Torvalds {
22191da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22201da177e4SLinus Torvalds 	int room = 0;
22211da177e4SLinus Torvalds 	unsigned long flags;
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	if (edge_port == NULL)
22261da177e4SLinus Torvalds 		return -ENODEV;
22271da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22281da177e4SLinus Torvalds 		return -ENODEV;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22311da177e4SLinus Torvalds 	room = edge_buf_space_avail(edge_port->ep_out_buf);
22321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds 	dbg("%s - returns %d", __FUNCTION__, room);
22351da177e4SLinus Torvalds 	return room;
22361da177e4SLinus Torvalds }
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds static int edge_chars_in_buffer (struct usb_serial_port *port)
22391da177e4SLinus Torvalds {
22401da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22411da177e4SLinus Torvalds 	int chars = 0;
22421da177e4SLinus Torvalds 	unsigned long flags;
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	if (edge_port == NULL)
22471da177e4SLinus Torvalds 		return -ENODEV;
22481da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22491da177e4SLinus Torvalds 		return -ENODEV;
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22521da177e4SLinus Torvalds 	chars = edge_buf_data_avail(edge_port->ep_out_buf);
22531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	dbg ("%s - returns %d", __FUNCTION__, chars);
22561da177e4SLinus Torvalds 	return chars;
22571da177e4SLinus Torvalds }
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds static void edge_throttle (struct usb_serial_port *port)
22601da177e4SLinus Torvalds {
22611da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22621da177e4SLinus Torvalds 	struct tty_struct *tty;
22631da177e4SLinus Torvalds 	int status;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 	if (edge_port == NULL)
22681da177e4SLinus Torvalds 		return;
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 	tty = port->tty;
22711da177e4SLinus Torvalds 	if (!tty) {
22721da177e4SLinus Torvalds 		dbg ("%s - no tty available", __FUNCTION__);
22731da177e4SLinus Torvalds 		return;
22741da177e4SLinus Torvalds 	}
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
22771da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22781da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
22791da177e4SLinus Torvalds 		status = edge_write (port, &stop_char, 1);
22801da177e4SLinus Torvalds 		if (status <= 0) {
22811da177e4SLinus Torvalds 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
22821da177e4SLinus Torvalds 		}
22831da177e4SLinus Torvalds 	}
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
22861da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
22871da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
22881da177e4SLinus Torvalds 		stop_read(edge_port);
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds }
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds static void edge_unthrottle (struct usb_serial_port *port)
22931da177e4SLinus Torvalds {
22941da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22951da177e4SLinus Torvalds 	struct tty_struct *tty;
22961da177e4SLinus Torvalds 	int status;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	if (edge_port == NULL)
23011da177e4SLinus Torvalds 		return;
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 	tty = port->tty;
23041da177e4SLinus Torvalds 	if (!tty) {
23051da177e4SLinus Torvalds 		dbg ("%s - no tty available", __FUNCTION__);
23061da177e4SLinus Torvalds 		return;
23071da177e4SLinus Torvalds 	}
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
23101da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
23111da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
23121da177e4SLinus Torvalds 		status = edge_write (port, &start_char, 1);
23131da177e4SLinus Torvalds 		if (status <= 0) {
23141da177e4SLinus Torvalds 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
23151da177e4SLinus Torvalds 		}
23161da177e4SLinus Torvalds 	}
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
23191da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
23201da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
23211da177e4SLinus Torvalds 		status = restart_read(edge_port);
23221da177e4SLinus Torvalds 		if (status)
23231da177e4SLinus Torvalds 			dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
23241da177e4SLinus Torvalds 	}
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	unsigned long flags;
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
23351da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
23361da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
23421da177e4SLinus Torvalds {
23431da177e4SLinus Torvalds 	struct urb *urb;
23441da177e4SLinus Torvalds 	int status = 0;
23451da177e4SLinus Torvalds 	unsigned long flags;
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
23501da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
23511da177e4SLinus Torvalds 		urb->complete = edge_bulk_in_callback;
23521da177e4SLinus Torvalds 		urb->context = edge_port;
23531da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
23541da177e4SLinus Torvalds 		status = usb_submit_urb(urb, GFP_KERNEL);
23551da177e4SLinus Torvalds 	}
23561da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
23571da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds 	return status;
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct ump_uart_config *config;
23671da177e4SLinus Torvalds 	struct tty_struct *tty;
23681da177e4SLinus Torvalds 	int baud;
23691da177e4SLinus Torvalds 	unsigned cflag;
23701da177e4SLinus Torvalds 	int status;
23711da177e4SLinus Torvalds 	int port_number = edge_port->port->number - edge_port->port->serial->minor;
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	tty = edge_port->port->tty;
23761da177e4SLinus Torvalds 	if ((!tty) ||
23771da177e4SLinus Torvalds 	    (!tty->termios)) {
23781da177e4SLinus Torvalds 		dbg("%s - no tty structures", __FUNCTION__);
23791da177e4SLinus Torvalds 		return;
23801da177e4SLinus Torvalds 	}
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
23831da177e4SLinus Torvalds 	if (!config) {
23841da177e4SLinus Torvalds 		dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
23851da177e4SLinus Torvalds 		return;
23861da177e4SLinus Torvalds 	}
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
23891da177e4SLinus Torvalds 
23901da177e4SLinus Torvalds 	config->wFlags = 0;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	/* These flags must be set */
23931da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
23941da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
23951da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
23981da177e4SLinus Torvalds 		case CS5:
23991da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR5BITS;
24001da177e4SLinus Torvalds 			    dbg ("%s - data bits = 5", __FUNCTION__);
24011da177e4SLinus Torvalds 			    break;
24021da177e4SLinus Torvalds 		case CS6:
24031da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR6BITS;
24041da177e4SLinus Torvalds 			    dbg ("%s - data bits = 6", __FUNCTION__);
24051da177e4SLinus Torvalds 			    break;
24061da177e4SLinus Torvalds 		case CS7:
24071da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR7BITS;
24081da177e4SLinus Torvalds 			    dbg ("%s - data bits = 7", __FUNCTION__);
24091da177e4SLinus Torvalds 			    break;
24101da177e4SLinus Torvalds 		default:
24111da177e4SLinus Torvalds 		case CS8:
24121da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR8BITS;
24131da177e4SLinus Torvalds 			    dbg ("%s - data bits = 8", __FUNCTION__);
24141da177e4SLinus Torvalds 			    break;
24151da177e4SLinus Torvalds 	}
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	if (cflag & PARENB) {
24181da177e4SLinus Torvalds 		if (cflag & PARODD) {
24191da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24201da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
24211da177e4SLinus Torvalds 			dbg("%s - parity = odd", __FUNCTION__);
24221da177e4SLinus Torvalds 		} else {
24231da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24241da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
24251da177e4SLinus Torvalds 			dbg("%s - parity = even", __FUNCTION__);
24261da177e4SLinus Torvalds 		}
24271da177e4SLinus Torvalds 	} else {
24281da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
24291da177e4SLinus Torvalds 		dbg("%s - parity = none", __FUNCTION__);
24301da177e4SLinus Torvalds 	}
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
24331da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
24341da177e4SLinus Torvalds 		dbg("%s - stop bits = 2", __FUNCTION__);
24351da177e4SLinus Torvalds 	} else {
24361da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
24371da177e4SLinus Torvalds 		dbg("%s - stop bits = 1", __FUNCTION__);
24381da177e4SLinus Torvalds 	}
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	/* figure out the flow control settings */
24411da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
24421da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
24431da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
24441da177e4SLinus Torvalds 		dbg("%s - RTS/CTS is enabled", __FUNCTION__);
24451da177e4SLinus Torvalds 	} else {
24461da177e4SLinus Torvalds 		dbg("%s - RTS/CTS is disabled", __FUNCTION__);
24471da177e4SLinus Torvalds 		tty->hw_stopped = 0;
24481da177e4SLinus Torvalds 		restart_read(edge_port);
24491da177e4SLinus Torvalds 	}
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, set the start and stop character in the device */
24521da177e4SLinus Torvalds 	if (I_IXOFF(tty) || I_IXON(tty)) {
24531da177e4SLinus Torvalds 		config->cXon  = START_CHAR(tty);
24541da177e4SLinus Torvalds 		config->cXoff = STOP_CHAR(tty);
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 		/* if we are implementing INBOUND XON/XOFF */
24571da177e4SLinus Torvalds 		if (I_IXOFF(tty)) {
24581da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
24591da177e4SLinus Torvalds 			dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
24601da177e4SLinus Torvalds 			     __FUNCTION__, config->cXon, config->cXoff);
24611da177e4SLinus Torvalds 		} else {
24621da177e4SLinus Torvalds 			dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
24631da177e4SLinus Torvalds 		}
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 		/* if we are implementing OUTBOUND XON/XOFF */
24661da177e4SLinus Torvalds 		if (I_IXON(tty)) {
24671da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
24681da177e4SLinus Torvalds 			dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
24691da177e4SLinus Torvalds 			     __FUNCTION__, config->cXon, config->cXoff);
24701da177e4SLinus Torvalds 		} else {
24711da177e4SLinus Torvalds 			dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
24721da177e4SLinus Torvalds 		}
24731da177e4SLinus Torvalds 	}
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	/* Round the baud rate */
24761da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
24771da177e4SLinus Torvalds 	if (!baud) {
24781da177e4SLinus Torvalds 		/* pick a default, any default... */
24791da177e4SLinus Torvalds 		baud = 9600;
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
24821da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
24871da177e4SLinus Torvalds 	dbg ("wFlags:    0x%x", config->wFlags);
24881da177e4SLinus Torvalds 	dbg ("bDataBits:   %d", config->bDataBits);
24891da177e4SLinus Torvalds 	dbg ("bParity:     %d", config->bParity);
24901da177e4SLinus Torvalds 	dbg ("bStopBits:   %d", config->bStopBits);
24911da177e4SLinus Torvalds 	dbg ("cXon:        %d", config->cXon);
24921da177e4SLinus Torvalds 	dbg ("cXoff:       %d", config->cXoff);
24931da177e4SLinus Torvalds 	dbg ("bUartMode:   %d", config->bUartMode);
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	/* move the word values into big endian mode */
24961da177e4SLinus Torvalds 	cpu_to_be16s (&config->wFlags);
24971da177e4SLinus Torvalds 	cpu_to_be16s (&config->wBaudRate);
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	status = TIWriteCommandSync (edge_port->port->serial->dev,
25001da177e4SLinus Torvalds 				UMPC_SET_CONFIG,
25011da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
25021da177e4SLinus Torvalds 				0,
25031da177e4SLinus Torvalds 				(__u8 *)config,
25041da177e4SLinus Torvalds 				sizeof(*config));
25051da177e4SLinus Torvalds 	if (status) {
25061da177e4SLinus Torvalds 		dbg ("%s - error %d when trying to write config to device",
25071da177e4SLinus Torvalds 		     __FUNCTION__, status);
25081da177e4SLinus Torvalds 	}
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	kfree (config);
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds 	return;
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25181da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
25191da177e4SLinus Torvalds 	unsigned int cflag;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	if (!port->tty || !port->tty->termios) {
25221da177e4SLinus Torvalds 		dbg ("%s - no tty or termios", __FUNCTION__);
25231da177e4SLinus Torvalds 		return;
25241da177e4SLinus Torvalds 	}
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
25271da177e4SLinus Torvalds 	/* check that they really want us to change something */
25281da177e4SLinus Torvalds 	if (old_termios) {
25291da177e4SLinus Torvalds 		if (cflag == old_termios->c_cflag &&
25301da177e4SLinus Torvalds 		    tty->termios->c_iflag == old_termios->c_iflag) {
25311da177e4SLinus Torvalds 			dbg ("%s - nothing to change", __FUNCTION__);
25321da177e4SLinus Torvalds 			return;
25331da177e4SLinus Torvalds 		}
25341da177e4SLinus Torvalds 	}
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds 	dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
25371da177e4SLinus Torvalds 	    tty->termios->c_cflag, tty->termios->c_iflag);
25381da177e4SLinus Torvalds 	if (old_termios) {
25391da177e4SLinus Torvalds 		dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
25401da177e4SLinus Torvalds 		    old_termios->c_cflag, old_termios->c_iflag);
25411da177e4SLinus Torvalds 	}
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	if (edge_port == NULL)
25461da177e4SLinus Torvalds 		return;
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
25491da177e4SLinus Torvalds 	change_port_settings (edge_port, old_termios);
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	return;
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
25551da177e4SLinus Torvalds {
25561da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25571da177e4SLinus Torvalds 	unsigned int mcr;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
25621da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
25631da177e4SLinus Torvalds 		mcr |= MCR_RTS;
25641da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
25651da177e4SLinus Torvalds 		mcr |= MCR_DTR;
25661da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
25671da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
25701da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
25711da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
25721da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
25731da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
25741da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	TIRestoreMCR (edge_port, mcr);
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	return 0;
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
25841da177e4SLinus Torvalds {
25851da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25861da177e4SLinus Torvalds 	unsigned int result = 0;
25871da177e4SLinus Torvalds 	unsigned int msr;
25881da177e4SLinus Torvalds 	unsigned int mcr;
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
25931da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
25941da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
25951da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
25961da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
25971da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
25981da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
25991da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	dbg("%s -- %x", __FUNCTION__, result);
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	return result;
26051da177e4SLinus Torvalds }
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
26081da177e4SLinus Torvalds {
26091da177e4SLinus Torvalds 	struct serial_struct tmp;
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 	if (!retinfo)
26121da177e4SLinus Torvalds 		return -EFAULT;
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
26171da177e4SLinus Torvalds 	tmp.line		= edge_port->port->serial->minor;
26181da177e4SLinus Torvalds 	tmp.port		= edge_port->port->number;
26191da177e4SLinus Torvalds 	tmp.irq			= 0;
26201da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
26211da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
26221da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
26231da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
26241da177e4SLinus Torvalds 	tmp.closing_wait	= closing_wait;
26251da177e4SLinus Torvalds //	tmp.custom_divisor	= state->custom_divisor;
26261da177e4SLinus Torvalds //	tmp.hub6		= state->hub6;
26271da177e4SLinus Torvalds //	tmp.io_type		= state->io_type;
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
26311da177e4SLinus Torvalds 		return -EFAULT;
26321da177e4SLinus Torvalds 	return 0;
26331da177e4SLinus Torvalds }
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
26361da177e4SLinus Torvalds {
26371da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
26381da177e4SLinus Torvalds 	struct async_icount cnow;
26391da177e4SLinus Torvalds 	struct async_icount cprev;
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	switch (cmd) {
26441da177e4SLinus Torvalds 		case TIOCINQ:
26451da177e4SLinus Torvalds 			dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
26461da177e4SLinus Torvalds //			return get_number_bytes_avail(edge_port, (unsigned int *) arg);
26471da177e4SLinus Torvalds 			break;
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 		case TIOCSERGETLSR:
26501da177e4SLinus Torvalds 			dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
26511da177e4SLinus Torvalds //			return get_lsr_info(edge_port, (unsigned int *) arg);
26521da177e4SLinus Torvalds 			break;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 		case TIOCGSERIAL:
26551da177e4SLinus Torvalds 			dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
26561da177e4SLinus Torvalds 			return get_serial_info(edge_port, (struct serial_struct __user *) arg);
26571da177e4SLinus Torvalds 			break;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 		case TIOCSSERIAL:
26601da177e4SLinus Torvalds 			dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
26611da177e4SLinus Torvalds 			break;
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 		case TIOCMIWAIT:
26641da177e4SLinus Torvalds 			dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
26651da177e4SLinus Torvalds 			cprev = edge_port->icount;
26661da177e4SLinus Torvalds 			while (1) {
26671da177e4SLinus Torvalds 				interruptible_sleep_on(&edge_port->delta_msr_wait);
26681da177e4SLinus Torvalds 				/* see if a signal did it */
26691da177e4SLinus Torvalds 				if (signal_pending(current))
26701da177e4SLinus Torvalds 					return -ERESTARTSYS;
26711da177e4SLinus Torvalds 				cnow = edge_port->icount;
26721da177e4SLinus Torvalds 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
26731da177e4SLinus Torvalds 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
26741da177e4SLinus Torvalds 					return -EIO; /* no change => error */
26751da177e4SLinus Torvalds 				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
26761da177e4SLinus Torvalds 				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
26771da177e4SLinus Torvalds 				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
26781da177e4SLinus Torvalds 				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
26791da177e4SLinus Torvalds 					return 0;
26801da177e4SLinus Torvalds 				}
26811da177e4SLinus Torvalds 				cprev = cnow;
26821da177e4SLinus Torvalds 			}
26831da177e4SLinus Torvalds 			/* not reached */
26841da177e4SLinus Torvalds 			break;
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 		case TIOCGICOUNT:
26871da177e4SLinus Torvalds 			dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
26881da177e4SLinus Torvalds 			     port->number, edge_port->icount.rx, edge_port->icount.tx);
26891da177e4SLinus Torvalds 			if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
26901da177e4SLinus Torvalds 				return -EFAULT;
26911da177e4SLinus Torvalds 			return 0;
26921da177e4SLinus Torvalds 	}
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds static void edge_break (struct usb_serial_port *port, int break_state)
26981da177e4SLinus Torvalds {
26991da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
27001da177e4SLinus Torvalds 	int status;
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	dbg ("%s - state = %d", __FUNCTION__, break_state);
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	/* chase the port close */
27051da177e4SLinus Torvalds 	TIChasePort (edge_port, 0, 0);
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	if (break_state == -1) {
27081da177e4SLinus Torvalds 		status = TISetBreak (edge_port);
27091da177e4SLinus Torvalds 	} else {
27101da177e4SLinus Torvalds 		status = TIClearBreak (edge_port);
27111da177e4SLinus Torvalds 	}
27121da177e4SLinus Torvalds 	if (status) {
27131da177e4SLinus Torvalds 		dbg ("%s - error %d sending break set/clear command.",
27141da177e4SLinus Torvalds 		     __FUNCTION__, status);
27151da177e4SLinus Torvalds 	}
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds static int edge_startup (struct usb_serial *serial)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
27211da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
27221da177e4SLinus Torvalds 	struct usb_device *dev;
27231da177e4SLinus Torvalds 	int status;
27241da177e4SLinus Torvalds 	int i;
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 	dev = serial->dev;
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 	/* create our private serial structure */
272980b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
27301da177e4SLinus Torvalds 	if (edge_serial == NULL) {
27311da177e4SLinus Torvalds 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
27321da177e4SLinus Torvalds 		return -ENOMEM;
27331da177e4SLinus Torvalds 	}
27341da177e4SLinus Torvalds 	sema_init(&edge_serial->es_sem, 1);
27351da177e4SLinus Torvalds 	edge_serial->serial = serial;
27361da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	status = TIDownloadFirmware (edge_serial);
27391da177e4SLinus Torvalds 	if (status) {
27401da177e4SLinus Torvalds 		kfree (edge_serial);
27411da177e4SLinus Torvalds 		return status;
27421da177e4SLinus Torvalds 	}
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	/* set up our port private structures */
27451da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
274680b6ca48SEric Sesterhenn 		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
27471da177e4SLinus Torvalds 		if (edge_port == NULL) {
27481da177e4SLinus Torvalds 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
27491da177e4SLinus Torvalds 			goto cleanup;
27501da177e4SLinus Torvalds 		}
27511da177e4SLinus Torvalds 		spin_lock_init(&edge_port->ep_lock);
27521da177e4SLinus Torvalds 		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
27531da177e4SLinus Torvalds 		if (edge_port->ep_out_buf == NULL) {
27541da177e4SLinus Torvalds 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
27551da177e4SLinus Torvalds 			kfree(edge_port);
27561da177e4SLinus Torvalds 			goto cleanup;
27571da177e4SLinus Torvalds 		}
27581da177e4SLinus Torvalds 		edge_port->port = serial->port[i];
27591da177e4SLinus Torvalds 		edge_port->edge_serial = edge_serial;
27601da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], edge_port);
27611da177e4SLinus Torvalds 		edge_port->bUartMode = 0;	/* Default is RS232 */
27621da177e4SLinus Torvalds 	}
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds 	return 0;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds cleanup:
27671da177e4SLinus Torvalds 	for (--i; i>=0; --i) {
27681da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
27691da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
27701da177e4SLinus Torvalds 		kfree(edge_port);
27711da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
27721da177e4SLinus Torvalds 	}
27731da177e4SLinus Torvalds 	kfree (edge_serial);
27741da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
27751da177e4SLinus Torvalds 	return -ENOMEM;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds static void edge_shutdown (struct usb_serial *serial)
27791da177e4SLinus Torvalds {
27801da177e4SLinus Torvalds 	int i;
27811da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	dbg ("%s", __FUNCTION__);
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 	for (i=0; i < serial->num_ports; ++i) {
27861da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
27871da177e4SLinus Torvalds 		if (edge_port) {
27881da177e4SLinus Torvalds 			edge_buf_free(edge_port->ep_out_buf);
27891da177e4SLinus Torvalds 			kfree(edge_port);
27901da177e4SLinus Torvalds 		}
27911da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
27921da177e4SLinus Torvalds 	}
27931da177e4SLinus Torvalds 	kfree (usb_get_serial_data(serial));
27941da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
27951da177e4SLinus Torvalds }
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds /* Circular Buffer */
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds /*
28011da177e4SLinus Torvalds  * edge_buf_alloc
28021da177e4SLinus Torvalds  *
28031da177e4SLinus Torvalds  * Allocate a circular buffer and all associated memory.
28041da177e4SLinus Torvalds  */
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size)
28071da177e4SLinus Torvalds {
28081da177e4SLinus Torvalds 	struct edge_buf *eb;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds 	if (size == 0)
28121da177e4SLinus Torvalds 		return NULL;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	eb = (struct edge_buf *)kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
28151da177e4SLinus Torvalds 	if (eb == NULL)
28161da177e4SLinus Torvalds 		return NULL;
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	eb->buf_buf = kmalloc(size, GFP_KERNEL);
28191da177e4SLinus Torvalds 	if (eb->buf_buf == NULL) {
28201da177e4SLinus Torvalds 		kfree(eb);
28211da177e4SLinus Torvalds 		return NULL;
28221da177e4SLinus Torvalds 	}
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	eb->buf_size = size;
28251da177e4SLinus Torvalds 	eb->buf_get = eb->buf_put = eb->buf_buf;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 	return eb;
28281da177e4SLinus Torvalds }
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds /*
28321da177e4SLinus Torvalds  * edge_buf_free
28331da177e4SLinus Torvalds  *
28341da177e4SLinus Torvalds  * Free the buffer and all associated memory.
28351da177e4SLinus Torvalds  */
28361da177e4SLinus Torvalds 
28373d48586cSAdrian Bunk static void edge_buf_free(struct edge_buf *eb)
28381da177e4SLinus Torvalds {
28391bc3c9e1SJesper Juhl 	if (eb) {
28401da177e4SLinus Torvalds 		kfree(eb->buf_buf);
28411da177e4SLinus Torvalds 		kfree(eb);
28421da177e4SLinus Torvalds 	}
28431da177e4SLinus Torvalds }
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds /*
28471da177e4SLinus Torvalds  * edge_buf_clear
28481da177e4SLinus Torvalds  *
28491da177e4SLinus Torvalds  * Clear out all data in the circular buffer.
28501da177e4SLinus Torvalds  */
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb)
28531da177e4SLinus Torvalds {
28541da177e4SLinus Torvalds         if (eb != NULL)
28551da177e4SLinus Torvalds                 eb->buf_get = eb->buf_put;
28561da177e4SLinus Torvalds                 /* equivalent to a get of all data available */
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds /*
28611da177e4SLinus Torvalds  * edge_buf_data_avail
28621da177e4SLinus Torvalds  *
28631da177e4SLinus Torvalds  * Return the number of bytes of data available in the circular
28641da177e4SLinus Torvalds  * buffer.
28651da177e4SLinus Torvalds  */
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb)
28681da177e4SLinus Torvalds {
28691da177e4SLinus Torvalds 	if (eb != NULL)
28701da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
28711da177e4SLinus Torvalds 	else
28721da177e4SLinus Torvalds 		return 0;
28731da177e4SLinus Torvalds }
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 
28761da177e4SLinus Torvalds /*
28771da177e4SLinus Torvalds  * edge_buf_space_avail
28781da177e4SLinus Torvalds  *
28791da177e4SLinus Torvalds  * Return the number of bytes of space available in the circular
28801da177e4SLinus Torvalds  * buffer.
28811da177e4SLinus Torvalds  */
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb)
28841da177e4SLinus Torvalds {
28851da177e4SLinus Torvalds 	if (eb != NULL)
28861da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
28871da177e4SLinus Torvalds 	else
28881da177e4SLinus Torvalds 		return 0;
28891da177e4SLinus Torvalds }
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds /*
28931da177e4SLinus Torvalds  * edge_buf_put
28941da177e4SLinus Torvalds  *
28951da177e4SLinus Torvalds  * Copy data data from a user buffer and put it into the circular buffer.
28961da177e4SLinus Torvalds  * Restrict to the amount of space available.
28971da177e4SLinus Torvalds  *
28981da177e4SLinus Torvalds  * Return the number of bytes copied.
28991da177e4SLinus Torvalds  */
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
29021da177e4SLinus Torvalds 	unsigned int count)
29031da177e4SLinus Torvalds {
29041da177e4SLinus Torvalds 	unsigned int len;
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds 	if (eb == NULL)
29081da177e4SLinus Torvalds 		return 0;
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 	len  = edge_buf_space_avail(eb);
29111da177e4SLinus Torvalds 	if (count > len)
29121da177e4SLinus Torvalds 		count = len;
29131da177e4SLinus Torvalds 
29141da177e4SLinus Torvalds 	if (count == 0)
29151da177e4SLinus Torvalds 		return 0;
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_put;
29181da177e4SLinus Torvalds 	if (count > len) {
29191da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, len);
29201da177e4SLinus Torvalds 		memcpy(eb->buf_buf, buf+len, count - len);
29211da177e4SLinus Torvalds 		eb->buf_put = eb->buf_buf + count - len;
29221da177e4SLinus Torvalds 	} else {
29231da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, count);
29241da177e4SLinus Torvalds 		if (count < len)
29251da177e4SLinus Torvalds 			eb->buf_put += count;
29261da177e4SLinus Torvalds 		else /* count == len */
29271da177e4SLinus Torvalds 			eb->buf_put = eb->buf_buf;
29281da177e4SLinus Torvalds 	}
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds 	return count;
29311da177e4SLinus Torvalds }
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds /*
29351da177e4SLinus Torvalds  * edge_buf_get
29361da177e4SLinus Torvalds  *
29371da177e4SLinus Torvalds  * Get data from the circular buffer and copy to the given buffer.
29381da177e4SLinus Torvalds  * Restrict to the amount of data available.
29391da177e4SLinus Torvalds  *
29401da177e4SLinus Torvalds  * Return the number of bytes copied.
29411da177e4SLinus Torvalds  */
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
29441da177e4SLinus Torvalds 	unsigned int count)
29451da177e4SLinus Torvalds {
29461da177e4SLinus Torvalds 	unsigned int len;
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 	if (eb == NULL)
29501da177e4SLinus Torvalds 		return 0;
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds 	len = edge_buf_data_avail(eb);
29531da177e4SLinus Torvalds 	if (count > len)
29541da177e4SLinus Torvalds 		count = len;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	if (count == 0)
29571da177e4SLinus Torvalds 		return 0;
29581da177e4SLinus Torvalds 
29591da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_get;
29601da177e4SLinus Torvalds 	if (count > len) {
29611da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, len);
29621da177e4SLinus Torvalds 		memcpy(buf+len, eb->buf_buf, count - len);
29631da177e4SLinus Torvalds 		eb->buf_get = eb->buf_buf + count - len;
29641da177e4SLinus Torvalds 	} else {
29651da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, count);
29661da177e4SLinus Torvalds 		if (count < len)
29671da177e4SLinus Torvalds 			eb->buf_get += count;
29681da177e4SLinus Torvalds 		else /* count == len */
29691da177e4SLinus Torvalds 			eb->buf_get = eb->buf_buf;
29701da177e4SLinus Torvalds 	}
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 	return count;
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 
2976ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
297718fcac35SGreg Kroah-Hartman 	.driver = {
29781da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
2979269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
298018fcac35SGreg Kroah-Hartman 	},
2981269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
29821da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
29831da177e4SLinus Torvalds 	.num_interrupt_in	= 1,
29841da177e4SLinus Torvalds 	.num_bulk_in		= 1,
29851da177e4SLinus Torvalds 	.num_bulk_out		= 1,
29861da177e4SLinus Torvalds 	.num_ports		= 1,
29871da177e4SLinus Torvalds 	.open			= edge_open,
29881da177e4SLinus Torvalds 	.close			= edge_close,
29891da177e4SLinus Torvalds 	.throttle		= edge_throttle,
29901da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
29911da177e4SLinus Torvalds 	.attach			= edge_startup,
29921da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
29931da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
29941da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
29951da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
29961da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
29971da177e4SLinus Torvalds 	.write			= edge_write,
29981da177e4SLinus Torvalds 	.write_room		= edge_write_room,
29991da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30001da177e4SLinus Torvalds 	.break_ctl		= edge_break,
30011da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
30021da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30031da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30041da177e4SLinus Torvalds };
30051da177e4SLinus Torvalds 
3006ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
300718fcac35SGreg Kroah-Hartman 	.driver = {
30081da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
3009269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
301018fcac35SGreg Kroah-Hartman 	},
3011269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
30121da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
30131da177e4SLinus Torvalds 	.num_interrupt_in	= 1,
30141da177e4SLinus Torvalds 	.num_bulk_in		= 2,
30151da177e4SLinus Torvalds 	.num_bulk_out		= 2,
30161da177e4SLinus Torvalds 	.num_ports		= 2,
30171da177e4SLinus Torvalds 	.open			= edge_open,
30181da177e4SLinus Torvalds 	.close			= edge_close,
30191da177e4SLinus Torvalds 	.throttle		= edge_throttle,
30201da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
30211da177e4SLinus Torvalds 	.attach			= edge_startup,
30221da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
30231da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
30241da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
30251da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
30261da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
30271da177e4SLinus Torvalds 	.write			= edge_write,
30281da177e4SLinus Torvalds 	.write_room		= edge_write_room,
30291da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30301da177e4SLinus Torvalds 	.break_ctl		= edge_break,
30311da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
30321da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30331da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30341da177e4SLinus Torvalds };
30351da177e4SLinus Torvalds 
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds static int __init edgeport_init(void)
30381da177e4SLinus Torvalds {
30391da177e4SLinus Torvalds 	int retval;
30401da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_1port_device);
30411da177e4SLinus Torvalds 	if (retval)
30421da177e4SLinus Torvalds 		goto failed_1port_device_register;
30431da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_2port_device);
30441da177e4SLinus Torvalds 	if (retval)
30451da177e4SLinus Torvalds 		goto failed_2port_device_register;
30461da177e4SLinus Torvalds 	retval = usb_register(&io_driver);
30471da177e4SLinus Torvalds 	if (retval)
30481da177e4SLinus Torvalds 		goto failed_usb_register;
30491da177e4SLinus Torvalds 	info(DRIVER_DESC " " DRIVER_VERSION);
30501da177e4SLinus Torvalds 	return 0;
30511da177e4SLinus Torvalds failed_usb_register:
30521da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_2port_device);
30531da177e4SLinus Torvalds failed_2port_device_register:
30541da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_1port_device);
30551da177e4SLinus Torvalds failed_1port_device_register:
30561da177e4SLinus Torvalds 	return retval;
30571da177e4SLinus Torvalds }
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds static void __exit edgeport_exit (void)
30601da177e4SLinus Torvalds {
30611da177e4SLinus Torvalds 	usb_deregister (&io_driver);
30621da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_1port_device);
30631da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_2port_device);
30641da177e4SLinus Torvalds }
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds module_init(edgeport_init);
30671da177e4SLinus Torvalds module_exit(edgeport_exit);
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds /* Module information */
30701da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
30711da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
30721da177e4SLinus Torvalds MODULE_LICENSE("GPL");
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
30751da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds module_param(low_latency, bool, S_IRUGO | S_IWUSR);
30781da177e4SLinus Torvalds MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
30811da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
30841da177e4SLinus Torvalds MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
30851da177e4SLinus Torvalds 
3086