xref: /openbmc/linux/drivers/usb/serial/io_ti.c (revision cdc97792289179974af6dda781c855696358d307)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Edgeport USB Serial Converter driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
51da177e4SLinus Torvalds  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *	the Free Software Foundation; either version 2 of the License, or
101da177e4SLinus Torvalds  *	(at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Supports the following devices:
131da177e4SLinus Torvalds  *	EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * For questions or problems with this driver, contact Inside Out
161da177e4SLinus Torvalds  * Networks technical support, or Peter Berger <pberger@brimson.com>,
171da177e4SLinus Torvalds  * or Al Borchers <alborchers@steinerpoint.com>.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Version history:
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	July 11, 2002 	Removed 4 port device structure since all TI UMP
221da177e4SLinus Torvalds  *			chips have only 2 ports
231da177e4SLinus Torvalds  *			David Iacovelli (davidi@ionetworks.com)
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/jiffies.h>
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/tty.h>
331da177e4SLinus Torvalds #include <linux/tty_driver.h>
341da177e4SLinus Torvalds #include <linux/tty_flip.h>
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/spinlock.h>
37241ca64fSMatthias Kaehlcke #include <linux/mutex.h>
381da177e4SLinus Torvalds #include <linux/serial.h>
391da177e4SLinus Torvalds #include <linux/ioctl.h>
401da177e4SLinus Torvalds #include <asm/uaccess.h>
411da177e4SLinus Torvalds #include <linux/usb.h>
42a969888cSGreg 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  */
51fc4cbd75SMartin K. Petersen #define DRIVER_VERSION "v0.7mode043006"
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
136241ca64fSMatthias Kaehlcke 	struct mutex es_lock;
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) },
176fc4cbd75SMartin K. Petersen 	/* The 4, 8 and 16 port devices show up as multiple 2 port devices */
1771da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
178fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
179fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
180fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
181fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
1821da177e4SLinus Torvalds 	{ }
1831da177e4SLinus Torvalds };
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /* Devices that this driver supports */
1861da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = {
1871da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
1881da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
1891da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
1901da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
1911da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
1921da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
1931da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
1941da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
1951da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
1961da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
1971da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
1981da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
1991da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
2001da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
2011da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
2021da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
2031da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
2041da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
2051da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
2061da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
2071da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
2081da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
2091da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
2101da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
2111da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
2121da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
2131da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
2141da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
2151da177e4SLinus Torvalds 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
216fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
217fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
218fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
219fc4cbd75SMartin K. Petersen 	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
2201da177e4SLinus Torvalds 	{ }
2211da177e4SLinus Torvalds };
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table_combined);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static struct usb_driver io_driver = {
2261da177e4SLinus Torvalds 	.name =		"io_ti",
2271da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
2281da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
2291da177e4SLinus Torvalds 	.id_table =	id_table_combined,
230ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
2311da177e4SLinus Torvalds };
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static int debug;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static int TIStayInBootMode = 0;
2391da177e4SLinus Torvalds static int low_latency = EDGE_LOW_LATENCY;
2401da177e4SLinus Torvalds static int closing_wait = EDGE_CLOSING_WAIT;
2411da177e4SLinus Torvalds static int ignore_cpu_rev = 0;
242fc4cbd75SMartin K. Petersen static int default_uart_mode = 0;	/* RS232 */
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port);
2481da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port);
2491da177e4SLinus Torvalds 
250606d099cSAlan Cox static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
2511da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port);
2521da177e4SLinus Torvalds 
253fc4cbd75SMartin K. Petersen /* sysfs attributes */
254fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port);
255fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
256fc4cbd75SMartin K. Petersen 
2571da177e4SLinus Torvalds /* circular buffer */
2581da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size);
2591da177e4SLinus Torvalds static void edge_buf_free(struct edge_buf *eb);
2601da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb);
2611da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb);
2621da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb);
2631da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2641da177e4SLinus Torvalds 	unsigned int count);
2651da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2661da177e4SLinus Torvalds 	unsigned int count);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static int TIReadVendorRequestSync (struct usb_device *dev,
2701da177e4SLinus Torvalds 				__u8		request,
2711da177e4SLinus Torvalds 				__u16		value,
2721da177e4SLinus Torvalds 				__u16		index,
2731da177e4SLinus Torvalds 				u8 		*data,
2741da177e4SLinus Torvalds 				int		size)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	int status;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	status = usb_control_msg (dev,
2791da177e4SLinus Torvalds 				usb_rcvctrlpipe(dev, 0),
2801da177e4SLinus Torvalds 				request,
2811da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
2821da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
2831da177e4SLinus Torvalds 				 USB_DIR_IN),
2841da177e4SLinus Torvalds 				value,
2851da177e4SLinus Torvalds 				index,
2861da177e4SLinus Torvalds 				data,
2871da177e4SLinus Torvalds 				size,
2881da177e4SLinus Torvalds 				1000);
2891da177e4SLinus Torvalds 	if (status < 0)
2901da177e4SLinus Torvalds 		return status;
2911da177e4SLinus Torvalds 	if (status != size) {
2921da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
293441b62c1SHarvey Harrison 		     __func__, size, status);
2941da177e4SLinus Torvalds 		return -ECOMM;
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static int TISendVendorRequestSync (struct usb_device *dev,
3001da177e4SLinus Torvalds 				__u8		request,
3011da177e4SLinus Torvalds 				__u16		value,
3021da177e4SLinus Torvalds 				__u16		index,
3031da177e4SLinus Torvalds 				u8 		*data,
3041da177e4SLinus Torvalds 				int		size)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	int status;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	status = usb_control_msg (dev,
3091da177e4SLinus Torvalds 				usb_sndctrlpipe(dev, 0),
3101da177e4SLinus Torvalds 				request,
3111da177e4SLinus Torvalds 				(USB_TYPE_VENDOR |
3121da177e4SLinus Torvalds 				 USB_RECIP_DEVICE |
3131da177e4SLinus Torvalds 				 USB_DIR_OUT),
3141da177e4SLinus Torvalds 				value,
3151da177e4SLinus Torvalds 				index,
3161da177e4SLinus Torvalds 				data,
3171da177e4SLinus Torvalds 				size,
3181da177e4SLinus Torvalds 				1000);
3191da177e4SLinus Torvalds 	if (status < 0)
3201da177e4SLinus Torvalds 		return status;
3211da177e4SLinus Torvalds 	if (status != size) {
3221da177e4SLinus Torvalds 		dbg ("%s - wanted to write %d, but only wrote %d",
323441b62c1SHarvey Harrison 		     __func__, size, status);
3241da177e4SLinus Torvalds 		return -ECOMM;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	return 0;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
3301da177e4SLinus Torvalds 				__u8 moduleid, __u16 value, u8 *data,
3311da177e4SLinus Torvalds 				int size)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	return TISendVendorRequestSync (dev,
3341da177e4SLinus Torvalds 					  command,	  		// Request
3351da177e4SLinus Torvalds 					  value,			// wValue
3361da177e4SLinus Torvalds 					  moduleid,			// wIndex
3371da177e4SLinus Torvalds 					  data,				// TransferBuffer
3381da177e4SLinus Torvalds 					  size);			// TransferBufferLength
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /* clear tx/rx buffers and fifo in TI UMP */
3431da177e4SLinus Torvalds static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	int port_number = port->number - port->serial->minor;
3461da177e4SLinus Torvalds 
347441b62c1SHarvey Harrison 	dbg ("%s - port %d, mask %x", __func__, port_number, mask);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	return TIWriteCommandSync (port->serial->dev,
3501da177e4SLinus Torvalds 					UMPC_PURGE_PORT,
3511da177e4SLinus Torvalds 					(__u8)(UMPM_UART1_PORT + port_number),
3521da177e4SLinus Torvalds 					mask,
3531da177e4SLinus Torvalds 					NULL,
3541da177e4SLinus Torvalds 					0);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds /**
3581da177e4SLinus Torvalds  * TIReadDownloadMemory - Read edgeport memory from TI chip
3591da177e4SLinus Torvalds  * @dev: usb device pointer
3601da177e4SLinus Torvalds  * @start_address: Device CPU address at which to read
3611da177e4SLinus Torvalds  * @length: Length of above data
3621da177e4SLinus Torvalds  * @address_type: Can read both XDATA and I2C
3631da177e4SLinus Torvalds  * @buffer: pointer to input data buffer
3641da177e4SLinus Torvalds  */
3651da177e4SLinus Torvalds static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
3661da177e4SLinus Torvalds 				int length, __u8 address_type, __u8 *buffer)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	int status = 0;
3691da177e4SLinus Torvalds 	__u8 read_length;
3701da177e4SLinus Torvalds 	__be16 be_start_address;
3711da177e4SLinus Torvalds 
372441b62c1SHarvey Harrison 	dbg ("%s - @ %x for %d", __func__, start_address, length);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Read in blocks of 64 bytes
3751da177e4SLinus Torvalds 	 * (TI firmware can't handle more than 64 byte reads)
3761da177e4SLinus Torvalds 	 */
3771da177e4SLinus Torvalds 	while (length) {
3781da177e4SLinus Torvalds 		if (length > 64)
3791da177e4SLinus Torvalds 			read_length= 64;
3801da177e4SLinus Torvalds 		else
3811da177e4SLinus Torvalds 			read_length = (__u8)length;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		if (read_length > 1) {
384441b62c1SHarvey Harrison 			dbg ("%s - @ %x for %d", __func__,
3851da177e4SLinus Torvalds 			     start_address, read_length);
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
3881da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (dev,
3891da177e4SLinus Torvalds 						  UMPC_MEMORY_READ,	// Request
3901da177e4SLinus Torvalds 						  (__u16)address_type,	// wValue (Address type)
3911da177e4SLinus Torvalds 						  (__force __u16)be_start_address,	// wIndex (Address to read)
3921da177e4SLinus Torvalds 						  buffer,		// TransferBuffer
3931da177e4SLinus Torvalds 						  read_length);	// TransferBufferLength
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 		if (status) {
396441b62c1SHarvey Harrison 			dbg ("%s - ERROR %x", __func__, status);
3971da177e4SLinus Torvalds 			return status;
3981da177e4SLinus Torvalds 		}
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		if (read_length > 1) {
401441b62c1SHarvey Harrison 			usb_serial_debug_data(debug, &dev->dev, __func__,
4021da177e4SLinus Torvalds 					      read_length, buffer);
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 		/* Update pointers/length */
4061da177e4SLinus Torvalds 		start_address += read_length;
4071da177e4SLinus Torvalds 		buffer += read_length;
4081da177e4SLinus Torvalds 		length -= read_length;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	return status;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	return TIReadDownloadMemory (dev,
4171da177e4SLinus Torvalds 				     start_address,
4181da177e4SLinus Torvalds 				     length,
4191da177e4SLinus Torvalds 				     DTK_ADDR_SPACE_XDATA,
4201da177e4SLinus Torvalds 				     buffer);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /* Read edgeport memory to a given block */
4241da177e4SLinus Torvalds static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	int status = 0;
4271da177e4SLinus Torvalds 	int i;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	for (i=0; i< length; i++) {
4301da177e4SLinus Torvalds 		status = TIReadVendorRequestSync (serial->serial->dev,
4311da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
4321da177e4SLinus Torvalds 					serial->TI_I2C_Type,		// wValue (Address type)
4331da177e4SLinus Torvalds 					(__u16)(start_address+i),	// wIndex
4341da177e4SLinus Torvalds 					&buffer[i],			// TransferBuffer
4351da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
4361da177e4SLinus Torvalds 		if (status) {
437441b62c1SHarvey Harrison 			dbg ("%s - ERROR %x", __func__, status);
4381da177e4SLinus Torvalds 			return status;
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
442441b62c1SHarvey Harrison 	dbg ("%s - start_address = %x, length = %d", __func__, start_address, length);
443441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	serial->TiReadI2C = 1;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	return status;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /* Write given block to TI EPROM memory */
4511da177e4SLinus Torvalds static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	int status = 0;
4541da177e4SLinus Torvalds 	int i;
4551da177e4SLinus Torvalds 	__u8 temp;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* Must do a read before write */
4581da177e4SLinus Torvalds 	if (!serial->TiReadI2C) {
4591da177e4SLinus Torvalds 		status = TIReadBootMemory(serial, 0, 1, &temp);
4601da177e4SLinus Torvalds 		if (status)
4611da177e4SLinus Torvalds 			return status;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	for (i=0; i < length; ++i) {
4651da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
4661da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,		// Request
4671da177e4SLinus Torvalds 						buffer[i],			// wValue
4681da177e4SLinus Torvalds 						(__u16)(i+start_address),	// wIndex
4691da177e4SLinus Torvalds 						NULL,				// TransferBuffer
4701da177e4SLinus Torvalds 						0);				// TransferBufferLength
4711da177e4SLinus Torvalds 		if (status)
4721da177e4SLinus Torvalds 			return status;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
475441b62c1SHarvey Harrison   	dbg ("%s - start_sddr = %x, length = %d", __func__, start_address, length);
476441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	return status;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /* Write edgeport I2C memory to TI chip	*/
4831da177e4SLinus Torvalds static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	int status = 0;
4861da177e4SLinus Torvalds 	int write_length;
4871da177e4SLinus Torvalds 	__be16 be_start_address;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	/* We can only send a maximum of 1 aligned byte page at a time */
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* calulate the number of bytes left in the first page */
4921da177e4SLinus Torvalds 	write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (write_length > length)
4951da177e4SLinus Torvalds 		write_length = length;
4961da177e4SLinus Torvalds 
497441b62c1SHarvey Harrison 	dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __func__, start_address, write_length);
498441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/* Write first page */
5011da177e4SLinus Torvalds 	be_start_address = cpu_to_be16 (start_address);
5021da177e4SLinus Torvalds 	status = TISendVendorRequestSync (serial->serial->dev,
5031da177e4SLinus Torvalds 					UMPC_MEMORY_WRITE,	// Request
5041da177e4SLinus Torvalds 					(__u16)address_type,	// wValue
5051da177e4SLinus Torvalds 					(__force __u16)be_start_address,	// wIndex
5061da177e4SLinus Torvalds 					buffer,			// TransferBuffer
5071da177e4SLinus Torvalds 					write_length);
5081da177e4SLinus Torvalds 	if (status) {
509441b62c1SHarvey Harrison 		dbg ("%s - ERROR %d", __func__, status);
5101da177e4SLinus Torvalds 		return status;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	length		-= write_length;
5141da177e4SLinus Torvalds 	start_address	+= write_length;
5151da177e4SLinus Torvalds 	buffer		+= write_length;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/* We should be aligned now -- can write max page size bytes at a time */
5181da177e4SLinus Torvalds 	while (length) {
5191da177e4SLinus Torvalds 		if (length > EPROM_PAGE_SIZE)
5201da177e4SLinus Torvalds 			write_length = EPROM_PAGE_SIZE;
5211da177e4SLinus Torvalds 		else
5221da177e4SLinus Torvalds 			write_length = length;
5231da177e4SLinus Torvalds 
524441b62c1SHarvey Harrison 		dbg ("%s - Page Write Addr = %x, length = %d", __func__, start_address, write_length);
525441b62c1SHarvey Harrison 		usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		/* Write next page */
5281da177e4SLinus Torvalds 		be_start_address = cpu_to_be16 (start_address);
5291da177e4SLinus Torvalds 		status = TISendVendorRequestSync (serial->serial->dev,
5301da177e4SLinus Torvalds 						UMPC_MEMORY_WRITE,	// Request
5311da177e4SLinus Torvalds 						(__u16)address_type,	// wValue
5321da177e4SLinus Torvalds 						(__force __u16)be_start_address,	// wIndex
5331da177e4SLinus Torvalds 						buffer,	  		// TransferBuffer
5341da177e4SLinus Torvalds 						write_length);		// TransferBufferLength
5351da177e4SLinus Torvalds 		if (status) {
536441b62c1SHarvey Harrison 			dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __func__, status);
5371da177e4SLinus Torvalds 			return status;
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		length		-= write_length;
5411da177e4SLinus Torvalds 		start_address	+= write_length;
5421da177e4SLinus Torvalds 		buffer		+= write_length;
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds 	return status;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* Examine the UMP DMA registers and LSR
5481da177e4SLinus Torvalds  *
5491da177e4SLinus Torvalds  * Check the MSBit of the X and Y DMA byte count registers.
5501da177e4SLinus Torvalds  * A zero in this bit indicates that the TX DMA buffers are empty
5511da177e4SLinus Torvalds  * then check the TX Empty bit in the UART.
5521da177e4SLinus Torvalds  */
5531da177e4SLinus Torvalds static int TIIsTxActive (struct edgeport_port *port)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	int status;
5561da177e4SLinus Torvalds 	struct out_endpoint_desc_block *oedb;
5571da177e4SLinus Torvalds 	__u8 *lsr;
5581da177e4SLinus Torvalds 	int bytes_left = 0;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
5611da177e4SLinus Torvalds 	if (!oedb) {
562441b62c1SHarvey Harrison 		dev_err (&port->port->dev, "%s - out of memory\n", __func__);
5631da177e4SLinus Torvalds 		return -ENOMEM;
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	lsr = kmalloc (1, GFP_KERNEL);	/* Sigh, that's right, just one byte,
5671da177e4SLinus Torvalds 					   as not all platforms can do DMA
5681da177e4SLinus Torvalds 					   from stack */
5691da177e4SLinus Torvalds 	if (!lsr) {
5701da177e4SLinus Torvalds 		kfree(oedb);
5711da177e4SLinus Torvalds 		return -ENOMEM;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	/* Read the DMA Count Registers */
5741da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5751da177e4SLinus Torvalds 			    port->dma_address,
5761da177e4SLinus Torvalds 			    sizeof( *oedb),
5771da177e4SLinus Torvalds 			    (void *)oedb);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (status)
5801da177e4SLinus Torvalds 		goto exit_is_tx_active;
5811da177e4SLinus Torvalds 
582441b62c1SHarvey Harrison 	dbg ("%s - XByteCount    0x%X", __func__, oedb->XByteCount);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* and the LSR */
5851da177e4SLinus Torvalds 	status = TIReadRam (port->port->serial->dev,
5861da177e4SLinus Torvalds 			    port->uart_base + UMPMEM_OFFS_UART_LSR,
5871da177e4SLinus Torvalds 			    1,
5881da177e4SLinus Torvalds 			    lsr);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (status)
5911da177e4SLinus Torvalds 		goto exit_is_tx_active;
592441b62c1SHarvey Harrison 	dbg ("%s - LSR = 0x%X", __func__, *lsr);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* If either buffer has data or we are transmitting then return TRUE */
5951da177e4SLinus Torvalds 	if ((oedb->XByteCount & 0x80 ) != 0 )
5961da177e4SLinus Torvalds 		bytes_left += 64;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
5991da177e4SLinus Torvalds 		bytes_left += 1;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	/* We return Not Active if we get any kind of error */
6021da177e4SLinus Torvalds exit_is_tx_active:
603441b62c1SHarvey Harrison 	dbg ("%s - return %d", __func__, bytes_left );
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	kfree(lsr);
6061da177e4SLinus Torvalds 	kfree(oedb);
6071da177e4SLinus Torvalds 	return bytes_left;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	int baud_rate;
6131da177e4SLinus Torvalds 	struct tty_struct *tty = port->port->tty;
6141da177e4SLinus Torvalds 	wait_queue_t wait;
6151da177e4SLinus Torvalds 	unsigned long flags;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (!timeout)
6181da177e4SLinus Torvalds 		timeout = (HZ*EDGE_CLOSING_WAIT)/100;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	/* wait for data to drain from the buffer */
6211da177e4SLinus Torvalds 	spin_lock_irqsave(&port->ep_lock, flags);
6221da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
6231da177e4SLinus Torvalds 	add_wait_queue(&tty->write_wait, &wait);
6241da177e4SLinus Torvalds 	for (;;) {
6251da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
6261da177e4SLinus Torvalds 		if (edge_buf_data_avail(port->ep_out_buf) == 0
6271da177e4SLinus Torvalds 		|| timeout == 0 || signal_pending(current)
6281da177e4SLinus Torvalds 		|| !usb_get_intfdata(port->port->serial->interface))  /* disconnect */
6291da177e4SLinus Torvalds 			break;
6301da177e4SLinus Torvalds 		spin_unlock_irqrestore(&port->ep_lock, flags);
6311da177e4SLinus Torvalds 		timeout = schedule_timeout(timeout);
6321da177e4SLinus Torvalds 		spin_lock_irqsave(&port->ep_lock, flags);
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
6351da177e4SLinus Torvalds 	remove_wait_queue(&tty->write_wait, &wait);
6361da177e4SLinus Torvalds 	if (flush)
6371da177e4SLinus Torvalds 		edge_buf_clear(port->ep_out_buf);
6381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&port->ep_lock, flags);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/* wait for data to drain from the device */
6411da177e4SLinus Torvalds 	timeout += jiffies;
6421da177e4SLinus Torvalds 	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
6431da177e4SLinus Torvalds 	&& usb_get_intfdata(port->port->serial->interface)) {  /* not disconnected */
6441da177e4SLinus Torvalds 		if (!TIIsTxActive(port))
6451da177e4SLinus Torvalds 			break;
6461da177e4SLinus Torvalds 		msleep(10);
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* disconnected */
6501da177e4SLinus Torvalds 	if (!usb_get_intfdata(port->port->serial->interface))
6511da177e4SLinus Torvalds 		return;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* wait one more character time, based on baud rate */
6541da177e4SLinus Torvalds 	/* (TIIsTxActive doesn't seem to wait for the last byte) */
6551da177e4SLinus Torvalds 	if ((baud_rate=port->baud_rate) == 0)
6561da177e4SLinus Torvalds 		baud_rate = 50;
657dfa5ec79SJulia Lawall 	msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds static int TIChooseConfiguration (struct usb_device *dev)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	// There may be multiple configurations on this device, in which case
6631da177e4SLinus Torvalds 	// we would need to read and parse all of them to find out which one
6641da177e4SLinus Torvalds 	// we want. However, we just support one config at this point,
6651da177e4SLinus Torvalds 	// configuration # 1, which is Config Descriptor 0.
6661da177e4SLinus Torvalds 
667441b62c1SHarvey Harrison 	dbg ("%s - Number of Interfaces = %d", __func__, dev->config->desc.bNumInterfaces);
668441b62c1SHarvey Harrison 	dbg ("%s - MAX Power            = %d", __func__, dev->config->desc.bMaxPower*2);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (dev->config->desc.bNumInterfaces != 1) {
671441b62c1SHarvey Harrison 		dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
6721da177e4SLinus Torvalds 		return -ENODEV;
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	return 0;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	int status;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
6831da177e4SLinus Torvalds 		status = TIReadDownloadMemory (serial->serial->dev,
6841da177e4SLinus Torvalds 					       start_address,
6851da177e4SLinus Torvalds 					       length,
6861da177e4SLinus Torvalds 					       serial->TI_I2C_Type,
6871da177e4SLinus Torvalds 					       buffer);
6881da177e4SLinus Torvalds 	} else {
6891da177e4SLinus Torvalds 		status = TIReadBootMemory (serial,
6901da177e4SLinus Torvalds 					   start_address,
6911da177e4SLinus Torvalds 					   length,
6921da177e4SLinus Torvalds 					   buffer);
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	return status;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_BOOT)
7011da177e4SLinus Torvalds 		return TIWriteBootMemory (serial,
7021da177e4SLinus Torvalds 					  start_address,
7031da177e4SLinus Torvalds 					  length,
7041da177e4SLinus Torvalds 					  buffer);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
7071da177e4SLinus Torvalds 		return TIWriteDownloadI2C (serial,
7081da177e4SLinus Torvalds 					   start_address,
7091da177e4SLinus Torvalds 					   length,
7101da177e4SLinus Torvalds 					   serial->TI_I2C_Type,
7111da177e4SLinus Torvalds 					   buffer);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	return -EINVAL;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /* Read a descriptor header from I2C based on type */
7191da177e4SLinus Torvalds static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	int start_address;
7221da177e4SLinus Torvalds 	int status;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	/* Search for requested descriptor in I2C */
7251da177e4SLinus Torvalds 	start_address = 2;
7261da177e4SLinus Torvalds 	do {
7271da177e4SLinus Torvalds 		status = TIReadRom (serial,
7281da177e4SLinus Torvalds 				   start_address,
7291da177e4SLinus Torvalds 				   sizeof(struct ti_i2c_desc),
7301da177e4SLinus Torvalds 				   (__u8 *)rom_desc );
7311da177e4SLinus Torvalds 		if (status)
7321da177e4SLinus Torvalds 			return 0;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		if (rom_desc->Type == desc_type)
7351da177e4SLinus Torvalds 			return start_address;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) +  rom_desc->Size;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	} while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	return 0;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* Validate descriptor checksum */
7451da177e4SLinus Torvalds static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	__u16 i;
7481da177e4SLinus Torvalds 	__u8 cs = 0;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	for (i=0; i < rom_desc->Size; i++) {
7511da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 	if (cs != rom_desc->CheckSum) {
754441b62c1SHarvey Harrison 		dbg ("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
7551da177e4SLinus Torvalds 		return -EINVAL;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	return 0;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds /* Make sure that the I2C image is good */
7611da177e4SLinus Torvalds static int TiValidateI2cImage (struct edgeport_serial *serial)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
7641da177e4SLinus Torvalds 	int status = 0;
7651da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
7661da177e4SLinus Torvalds 	int start_address = 2;
7671da177e4SLinus Torvalds 	__u8 *buffer;
7681da177e4SLinus Torvalds 	__u16 ttype;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
7711da177e4SLinus Torvalds 	if (!rom_desc) {
772441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory\n", __func__);
7731da177e4SLinus Torvalds 		return -ENOMEM;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
7761da177e4SLinus Torvalds 	if (!buffer) {
777441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory when allocating buffer\n", __func__);
7781da177e4SLinus Torvalds 		kfree (rom_desc);
7791da177e4SLinus Torvalds 		return -ENOMEM;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	// Read the first byte (Signature0) must be 0x52 or 0x10
7831da177e4SLinus Torvalds 	status = TIReadRom (serial, 0, 1, buffer);
7841da177e4SLinus Torvalds 	if (status)
7851da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if (*buffer != UMP5152 && *buffer != UMP3410) {
788441b62c1SHarvey Harrison 		dev_err (dev, "%s - invalid buffer signature\n", __func__);
7891da177e4SLinus Torvalds 		status = -ENODEV;
7901da177e4SLinus Torvalds 		goto ExitTiValidateI2cImage;
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	do {
7941da177e4SLinus Torvalds 		// Validate the I2C
7951da177e4SLinus Torvalds 		status = TIReadRom (serial,
7961da177e4SLinus Torvalds 				start_address,
7971da177e4SLinus Torvalds 				sizeof(struct ti_i2c_desc),
7981da177e4SLinus Torvalds 				(__u8 *)rom_desc);
7991da177e4SLinus Torvalds 		if (status)
8001da177e4SLinus Torvalds 			break;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
8031da177e4SLinus Torvalds 			status = -ENODEV;
804441b62c1SHarvey Harrison 			dbg ("%s - structure too big, erroring out.", __func__);
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 		}
8071da177e4SLinus Torvalds 
808441b62c1SHarvey Harrison 		dbg ("%s Type = 0x%x", __func__, rom_desc->Type);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 		// Skip type 2 record
8111da177e4SLinus Torvalds 		ttype = rom_desc->Type & 0x0f;
8121da177e4SLinus Torvalds 		if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
8131da177e4SLinus Torvalds 			&& ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
8141da177e4SLinus Torvalds 			// Read the descriptor data
8151da177e4SLinus Torvalds 			status = TIReadRom(serial,
8161da177e4SLinus Torvalds 						start_address+sizeof(struct ti_i2c_desc),
8171da177e4SLinus Torvalds 						rom_desc->Size,
8181da177e4SLinus Torvalds 						buffer);
8191da177e4SLinus Torvalds 			if (status)
8201da177e4SLinus Torvalds 				break;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 			status = ValidChecksum(rom_desc, buffer);
8231da177e4SLinus Torvalds 			if (status)
8241da177e4SLinus Torvalds 				break;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 		start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	} while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
8311da177e4SLinus Torvalds 		status = -ENODEV;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds ExitTiValidateI2cImage:
8341da177e4SLinus Torvalds 	kfree (buffer);
8351da177e4SLinus Torvalds 	kfree (rom_desc);
8361da177e4SLinus Torvalds 	return status;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	int status;
8421da177e4SLinus Torvalds 	int start_address;
8431da177e4SLinus Torvalds 	struct ti_i2c_desc *rom_desc;
8441da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *desc;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
8471da177e4SLinus Torvalds 	if (!rom_desc) {
848441b62c1SHarvey Harrison 		dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __func__);
8491da177e4SLinus Torvalds 		return -ENOMEM;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (!start_address) {
854441b62c1SHarvey Harrison 		dbg ("%s - Edge Descriptor not found in I2C", __func__);
8551da177e4SLinus Torvalds 		status = -ENODEV;
8561da177e4SLinus Torvalds 		goto exit;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	// Read the descriptor data
8601da177e4SLinus Torvalds 	status = TIReadRom (serial,
8611da177e4SLinus Torvalds 				start_address+sizeof(struct ti_i2c_desc),
8621da177e4SLinus Torvalds 				rom_desc->Size,
8631da177e4SLinus Torvalds 				buffer);
8641da177e4SLinus Torvalds 	if (status)
8651da177e4SLinus Torvalds 		goto exit;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	status = ValidChecksum(rom_desc, buffer);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	desc = (struct edge_ti_manuf_descriptor *)buffer;
870441b62c1SHarvey Harrison 	dbg ( "%s - IonConfig      0x%x", __func__, desc->IonConfig 	);
871441b62c1SHarvey Harrison 	dbg ( "%s - Version          %d", __func__, desc->Version	  	);
872441b62c1SHarvey Harrison 	dbg ( "%s - Cpu/Board      0x%x", __func__, desc->CpuRev_BoardRev	);
873441b62c1SHarvey Harrison 	dbg ( "%s - NumPorts         %d", __func__, desc->NumPorts  	);
874441b62c1SHarvey Harrison 	dbg ( "%s - NumVirtualPorts  %d", __func__, desc->NumVirtualPorts	);
875441b62c1SHarvey Harrison 	dbg ( "%s - TotalPorts       %d", __func__, desc->TotalPorts  	);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds exit:
8781da177e4SLinus Torvalds 	kfree (rom_desc);
8791da177e4SLinus Torvalds 	return status;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds /* Build firmware header used for firmware update */
8831da177e4SLinus Torvalds static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
8841da177e4SLinus Torvalds {
8851da177e4SLinus Torvalds 	__u8 *buffer;
8861da177e4SLinus Torvalds 	int buffer_size;
8871da177e4SLinus Torvalds 	int i;
8881da177e4SLinus Torvalds 	__u8 cs = 0;
8891da177e4SLinus Torvalds 	struct ti_i2c_desc *i2c_header;
8901da177e4SLinus Torvalds 	struct ti_i2c_image_header *img_header;
8911da177e4SLinus Torvalds 	struct ti_i2c_firmware_rec *firmware_rec;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
8941da177e4SLinus Torvalds 	// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
8951da177e4SLinus Torvalds 	// will download the latest firmware (padded to 15.5k) into the UMP ram.
8961da177e4SLinus Torvalds 	// And finally when the device comes back up in download mode the driver will cause
8971da177e4SLinus Torvalds 	// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
8981da177e4SLinus Torvalds 	// the record type from 0xf2 to 0x02.
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	// Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
9011da177e4SLinus Torvalds 	buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	buffer = kmalloc (buffer_size, GFP_KERNEL);
9041da177e4SLinus Torvalds 	if (!buffer) {
905441b62c1SHarvey Harrison 		dev_err (dev, "%s - out of memory\n", __func__);
9061da177e4SLinus Torvalds 		return -ENOMEM;
9071da177e4SLinus Torvalds 	}
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	// Set entire image of 0xffs
9101da177e4SLinus Torvalds 	memset (buffer, 0xff, buffer_size);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	// Copy version number into firmware record
9131da177e4SLinus Torvalds 	firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
9161da177e4SLinus Torvalds 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	// Pointer to fw_down memory image
9191da177e4SLinus Torvalds 	img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
9221da177e4SLinus Torvalds 		&PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
9231da177e4SLinus Torvalds 		le16_to_cpu(img_header->Length));
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	for (i=0; i < buffer_size; i++) {
9261da177e4SLinus Torvalds 		cs = (__u8)(cs + buffer[i]);
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	kfree (buffer);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	// Build new header
9321da177e4SLinus Torvalds 	i2c_header =  (struct ti_i2c_desc *)header;
9331da177e4SLinus Torvalds 	firmware_rec =  (struct ti_i2c_firmware_rec*)i2c_header->Data;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	i2c_header->Type	= I2C_DESC_TYPE_FIRMWARE_BLANK;
9361da177e4SLinus Torvalds 	i2c_header->Size	= (__u16)buffer_size;
9371da177e4SLinus Torvalds 	i2c_header->CheckSum	= cs;
9381da177e4SLinus Torvalds 	firmware_rec->Ver_Major	= OperationalCodeImageVersion.MajorVersion;
9391da177e4SLinus Torvalds 	firmware_rec->Ver_Minor	= OperationalCodeImageVersion.MinorVersion;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	return 0;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds /* Try to figure out what type of I2c we have */
9451da177e4SLinus Torvalds static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	int status;
9481da177e4SLinus Torvalds 	__u8 data;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	// Try to read type 2
9511da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9521da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9531da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_II,	// wValue (Address type)
9541da177e4SLinus Torvalds 					0,		 		// wIndex
9551da177e4SLinus Torvalds 					&data,				// TransferBuffer
9561da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9571da177e4SLinus Torvalds 	if (status)
958441b62c1SHarvey Harrison 		dbg ("%s - read 2 status error = %d", __func__, status);
9591da177e4SLinus Torvalds 	else
960441b62c1SHarvey Harrison 		dbg ("%s - read 2 data = 0x%x", __func__, data);
9611da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
962441b62c1SHarvey Harrison 		dbg ("%s - ROM_TYPE_II", __func__);
9631da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9641da177e4SLinus Torvalds 		return 0;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	// Try to read type 3
9681da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (serial->serial->dev,
9691da177e4SLinus Torvalds 					UMPC_MEMORY_READ,		// Request
9701da177e4SLinus Torvalds 					DTK_ADDR_SPACE_I2C_TYPE_III,	// wValue (Address type)
9711da177e4SLinus Torvalds 					0,				// wIndex
9721da177e4SLinus Torvalds 					&data,				// TransferBuffer
9731da177e4SLinus Torvalds 					0x01);				// TransferBufferLength
9741da177e4SLinus Torvalds 	if (status)
975441b62c1SHarvey Harrison 		dbg ("%s - read 3 status error = %d", __func__, status);
9761da177e4SLinus Torvalds 	else
977441b62c1SHarvey Harrison 		dbg ("%s - read 2 data = 0x%x", __func__, data);
9781da177e4SLinus Torvalds 	if ((!status) && (data == UMP5152 || data == UMP3410)) {
979441b62c1SHarvey Harrison 		dbg ("%s - ROM_TYPE_III", __func__);
9801da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
9811da177e4SLinus Torvalds 		return 0;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
984441b62c1SHarvey Harrison 	dbg ("%s - Unknown", __func__);
9851da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
9861da177e4SLinus Torvalds 	return -ENODEV;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	int status;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	status = usb_bulk_msg (serial->dev,
9941da177e4SLinus Torvalds 				usb_sndbulkpipe(serial->dev,
9951da177e4SLinus Torvalds 						serial->port[0]->bulk_out_endpointAddress),
9961da177e4SLinus Torvalds 				buffer,
9971da177e4SLinus Torvalds 				length,
9981da177e4SLinus Torvalds 				num_sent,
9991da177e4SLinus Torvalds 				1000);
10001da177e4SLinus Torvalds 	return status;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds /* Download given firmware image to the device (IN BOOT MODE) */
10041da177e4SLinus Torvalds static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	int status = 0;
10071da177e4SLinus Torvalds 	int pos;
10081da177e4SLinus Torvalds 	int transfer;
10091da177e4SLinus Torvalds 	int done;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	// Transfer firmware image
10121da177e4SLinus Torvalds 	for (pos = 0; pos < image_length; ) {
10131da177e4SLinus Torvalds 		// Read the next buffer from file
10141da177e4SLinus Torvalds 		transfer = image_length - pos;
10151da177e4SLinus Torvalds 		if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
10161da177e4SLinus Torvalds 			transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 		// Transfer data
10191da177e4SLinus Torvalds 		status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
10201da177e4SLinus Torvalds 		if (status)
10211da177e4SLinus Torvalds 			break;
10221da177e4SLinus Torvalds 		// Advance buffer pointer
10231da177e4SLinus Torvalds 		pos += done;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	return status;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds // FIXME!!!
10301da177e4SLinus Torvalds static int TIConfigureBootDevice (struct usb_device *dev)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	return 0;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds /**
10361da177e4SLinus Torvalds  * DownloadTIFirmware - Download run-time operating firmware to the TI5052
10371da177e4SLinus Torvalds  *
10381da177e4SLinus Torvalds  * This routine downloads the main operating code into the TI5052, using the
10391da177e4SLinus Torvalds  * boot code already burned into E2PROM or ROM.
10401da177e4SLinus Torvalds  */
10411da177e4SLinus Torvalds static int TIDownloadFirmware (struct edgeport_serial *serial)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct device *dev = &serial->serial->dev->dev;
10441da177e4SLinus Torvalds 	int status = 0;
10451da177e4SLinus Torvalds 	int start_address;
10461da177e4SLinus Torvalds 	struct edge_ti_manuf_descriptor *ti_manuf_desc;
10471da177e4SLinus Torvalds 	struct usb_interface_descriptor *interface;
10481da177e4SLinus Torvalds 	int download_cur_ver;
10491da177e4SLinus Torvalds 	int download_new_ver;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/* This routine is entered by both the BOOT mode and the Download mode
10521da177e4SLinus Torvalds 	 * We can determine which code is running by the reading the config
10531da177e4SLinus Torvalds 	 * descriptor and if we have only one bulk pipe it is in boot mode
10541da177e4SLinus Torvalds 	 */
10551da177e4SLinus Torvalds 	serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	/* Default to type 2 i2c */
10581da177e4SLinus Torvalds 	serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	status = TIChooseConfiguration (serial->serial->dev);
10611da177e4SLinus Torvalds 	if (status)
10621da177e4SLinus Torvalds 		return status;
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	interface = &serial->serial->interface->cur_altsetting->desc;
10651da177e4SLinus Torvalds 	if (!interface) {
1066441b62c1SHarvey Harrison 		dev_err (dev, "%s - no interface set, error!\n", __func__);
10671da177e4SLinus Torvalds 		return -ENODEV;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	// Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
10711da177e4SLinus Torvalds 	// if we have more than one endpoint we are definitely in download mode
10721da177e4SLinus Torvalds 	if (interface->bNumEndpoints > 1)
10731da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_DOWNLOAD;
10741da177e4SLinus Torvalds 	else
10751da177e4SLinus Torvalds 		// Otherwise we will remain in configuring mode
10761da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_CONFIGURING;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	// Save Download Version Number
10791da177e4SLinus Torvalds 	OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
10801da177e4SLinus Torvalds 	OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
10811da177e4SLinus Torvalds 	OperationalCodeImageVersion.BuildNumber	 = PagableOperationalCodeImageVersion.BuildNumber;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/********************************************************************/
10841da177e4SLinus Torvalds 	/* Download Mode */
10851da177e4SLinus Torvalds 	/********************************************************************/
10861da177e4SLinus Torvalds 	if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
10871da177e4SLinus Torvalds 		struct ti_i2c_desc *rom_desc;
10881da177e4SLinus Torvalds 
10899544e833SAndrew Morton 		dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 		status = TiValidateI2cImage (serial);
10921da177e4SLinus Torvalds 		if (status) {
10939544e833SAndrew Morton 			dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
10941da177e4SLinus Torvalds 			return status;
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 		/* Validate Hardware version number
10981da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
10991da177e4SLinus Torvalds 		 */
11001da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
11011da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1102441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
11031da177e4SLinus Torvalds 			return -ENOMEM;
11041da177e4SLinus Torvalds 		}
11051da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
11061da177e4SLinus Torvalds 		if (status) {
11071da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11081da177e4SLinus Torvalds 			return status;
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		// Check version number of ION descriptor
11121da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1113441b62c1SHarvey Harrison 			dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __func__,
11141da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
11151da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11161da177e4SLinus Torvalds 		   	return -EINVAL;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
11201da177e4SLinus Torvalds 		if (!rom_desc) {
1121441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
11221da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
11231da177e4SLinus Torvalds 			return -ENOMEM;
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 		// Search for type 2 record (firmware record)
11271da177e4SLinus Torvalds 		if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
11281da177e4SLinus Torvalds 			struct ti_i2c_firmware_rec *firmware_version;
11291da177e4SLinus Torvalds 			__u8 record;
11301da177e4SLinus Torvalds 
1131441b62c1SHarvey Harrison 			dbg ("%s - Found Type FIRMWARE (Type 2) record", __func__);
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 			firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
11341da177e4SLinus Torvalds 			if (!firmware_version) {
1135441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
11361da177e4SLinus Torvalds 				kfree (rom_desc);
11371da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11381da177e4SLinus Torvalds 				return -ENOMEM;
11391da177e4SLinus Torvalds 			}
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 			// Validate version number
11421da177e4SLinus Torvalds 			// Read the descriptor data
11431da177e4SLinus Torvalds 			status = TIReadRom (serial,
11441da177e4SLinus Torvalds 					start_address+sizeof(struct ti_i2c_desc),
11451da177e4SLinus Torvalds 					sizeof(struct ti_i2c_firmware_rec),
11461da177e4SLinus Torvalds 					(__u8 *)firmware_version);
11471da177e4SLinus Torvalds 			if (status) {
11481da177e4SLinus Torvalds 				kfree (firmware_version);
11491da177e4SLinus Torvalds 				kfree (rom_desc);
11501da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
11511da177e4SLinus Torvalds 				return status;
11521da177e4SLinus Torvalds 			}
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 			// Check version number of download with current version in I2c
11551da177e4SLinus Torvalds 			download_cur_ver = (firmware_version->Ver_Major << 8) +
11561da177e4SLinus Torvalds 					   (firmware_version->Ver_Minor);
11571da177e4SLinus Torvalds 			download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
11581da177e4SLinus Torvalds 					   (OperationalCodeImageVersion.MinorVersion);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 			dbg ("%s - >>>Firmware Versions Device %d.%d  Driver %d.%d",
1161441b62c1SHarvey Harrison 			     __func__,
11621da177e4SLinus Torvalds 			     firmware_version->Ver_Major,
11631da177e4SLinus Torvalds 			     firmware_version->Ver_Minor,
11641da177e4SLinus Torvalds 			     OperationalCodeImageVersion.MajorVersion,
11651da177e4SLinus Torvalds 			     OperationalCodeImageVersion.MinorVersion);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 			// Check if we have an old version in the I2C and update if necessary
11681da177e4SLinus Torvalds 			if (download_cur_ver != download_new_ver) {
11691da177e4SLinus Torvalds 				dbg ("%s - Update I2C Download from %d.%d to %d.%d",
1170441b62c1SHarvey Harrison 				     __func__,
11711da177e4SLinus Torvalds 				     firmware_version->Ver_Major,
11721da177e4SLinus Torvalds 				     firmware_version->Ver_Minor,
11731da177e4SLinus Torvalds 				     OperationalCodeImageVersion.MajorVersion,
11741da177e4SLinus Torvalds 				     OperationalCodeImageVersion.MinorVersion);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 				// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
11771da177e4SLinus Torvalds 				// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
11781da177e4SLinus Torvalds 				// will download the latest firmware (padded to 15.5k) into the UMP ram.
11791da177e4SLinus Torvalds 				// And finally when the device comes back up in download mode the driver will cause
11801da177e4SLinus Torvalds 				// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
11811da177e4SLinus Torvalds 				// the record type from 0xf2 to 0x02.
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 				record = I2C_DESC_TYPE_FIRMWARE_BLANK;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 				// Change the I2C Firmware record type to 0xf2 to trigger an update
11861da177e4SLinus Torvalds 				status = TIWriteRom (serial,
11871da177e4SLinus Torvalds 							start_address,
11881da177e4SLinus Torvalds 							sizeof(record),
11891da177e4SLinus Torvalds 							&record);
11901da177e4SLinus Torvalds 				if (status) {
11911da177e4SLinus Torvalds 					kfree (firmware_version);
11921da177e4SLinus Torvalds 					kfree (rom_desc);
11931da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
11941da177e4SLinus Torvalds 					return status;
11951da177e4SLinus Torvalds 				}
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 				// verify the write -- must do this in order for write to
11981da177e4SLinus Torvalds 				// complete before we do the hardware reset
11991da177e4SLinus Torvalds 				status = TIReadRom (serial,
12001da177e4SLinus Torvalds 							start_address,
12011da177e4SLinus Torvalds 							sizeof(record),
12021da177e4SLinus Torvalds 							&record);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 				if (status) {
12051da177e4SLinus Torvalds 					kfree (firmware_version);
12061da177e4SLinus Torvalds 					kfree (rom_desc);
12071da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12081da177e4SLinus Torvalds 					return status;
12091da177e4SLinus Torvalds 				}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 				if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1212441b62c1SHarvey Harrison 					dev_err (dev, "%s - error resetting device\n", __func__);
12131da177e4SLinus Torvalds 					kfree (firmware_version);
12141da177e4SLinus Torvalds 					kfree (rom_desc);
12151da177e4SLinus Torvalds 					kfree (ti_manuf_desc);
12161da177e4SLinus Torvalds 					return -ENODEV;
12171da177e4SLinus Torvalds 				}
12181da177e4SLinus Torvalds 
1219441b62c1SHarvey Harrison 				dbg ("%s - HARDWARE RESET", __func__);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 				// Reset UMP -- Back to BOOT MODE
12221da177e4SLinus Torvalds 				status = TISendVendorRequestSync (serial->serial->dev,
12231da177e4SLinus Torvalds 								UMPC_HARDWARE_RESET,	// Request
12241da177e4SLinus Torvalds 								0,			// wValue
12251da177e4SLinus Torvalds 								0,			// wIndex
12261da177e4SLinus Torvalds 								NULL,			// TransferBuffer
12271da177e4SLinus Torvalds 								0);			// TransferBufferLength
12281da177e4SLinus Torvalds 
1229441b62c1SHarvey Harrison 				dbg ( "%s - HARDWARE RESET return %d", __func__, status);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 				/* return an error on purpose. */
12321da177e4SLinus Torvalds 				kfree (firmware_version);
12331da177e4SLinus Torvalds 				kfree (rom_desc);
12341da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12351da177e4SLinus Torvalds 				return -ENODEV;
12361da177e4SLinus Torvalds 			}
12371da177e4SLinus Torvalds 			kfree (firmware_version);
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 		// Search for type 0xF2 record (firmware blank record)
12401da177e4SLinus Torvalds 		else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
12411da177e4SLinus Torvalds 			#define HEADER_SIZE	(sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
12421da177e4SLinus Torvalds 			__u8 *header;
12431da177e4SLinus Torvalds 			__u8 *vheader;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 			header  = kmalloc (HEADER_SIZE, GFP_KERNEL);
12461da177e4SLinus Torvalds 			if (!header) {
1247441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
12481da177e4SLinus Torvalds 				kfree (rom_desc);
12491da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12501da177e4SLinus Torvalds 				return -ENOMEM;
12511da177e4SLinus Torvalds 			}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 			vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
12541da177e4SLinus Torvalds 			if (!vheader) {
1255441b62c1SHarvey Harrison 				dev_err (dev, "%s - out of memory.\n", __func__);
12561da177e4SLinus Torvalds 				kfree (header);
12571da177e4SLinus Torvalds 				kfree (rom_desc);
12581da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12591da177e4SLinus Torvalds 				return -ENOMEM;
12601da177e4SLinus Torvalds 			}
12611da177e4SLinus Torvalds 
1262441b62c1SHarvey Harrison 			dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __func__);
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 			// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
12651da177e4SLinus Torvalds 			// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
12661da177e4SLinus Torvalds 			// will download the latest firmware (padded to 15.5k) into the UMP ram.
12671da177e4SLinus Torvalds 			// And finally when the device comes back up in download mode the driver will cause
12681da177e4SLinus Torvalds 			// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
12691da177e4SLinus Torvalds 			// the record type from 0xf2 to 0x02.
12701da177e4SLinus Torvalds 			status = BuildI2CFirmwareHeader(header, dev);
12711da177e4SLinus Torvalds 			if (status) {
12721da177e4SLinus Torvalds 				kfree (vheader);
12731da177e4SLinus Torvalds 				kfree (header);
12741da177e4SLinus Torvalds 				kfree (rom_desc);
12751da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12761da177e4SLinus Torvalds 				return status;
12771da177e4SLinus Torvalds 			}
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 			// Update I2C with type 0xf2 record with correct size and checksum
12801da177e4SLinus Torvalds 			status = TIWriteRom (serial,
12811da177e4SLinus Torvalds 						start_address,
12821da177e4SLinus Torvalds 						HEADER_SIZE,
12831da177e4SLinus Torvalds 						header);
12841da177e4SLinus Torvalds 			if (status) {
12851da177e4SLinus Torvalds 				kfree (vheader);
12861da177e4SLinus Torvalds 				kfree (header);
12871da177e4SLinus Torvalds 				kfree (rom_desc);
12881da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
12891da177e4SLinus Torvalds 				return status;
12901da177e4SLinus Torvalds 			}
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 			// verify the write -- must do this in order for write to
12931da177e4SLinus Torvalds 			// complete before we do the hardware reset
12941da177e4SLinus Torvalds 			status = TIReadRom (serial,
12951da177e4SLinus Torvalds 						start_address,
12961da177e4SLinus Torvalds 						HEADER_SIZE,
12971da177e4SLinus Torvalds 						vheader);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 			if (status) {
1300441b62c1SHarvey Harrison 				dbg ("%s - can't read header back", __func__);
13011da177e4SLinus Torvalds 				kfree (vheader);
13021da177e4SLinus Torvalds 				kfree (header);
13031da177e4SLinus Torvalds 				kfree (rom_desc);
13041da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13051da177e4SLinus Torvalds 				return status;
13061da177e4SLinus Torvalds 			}
13071da177e4SLinus Torvalds 			if (memcmp(vheader, header, HEADER_SIZE)) {
1308441b62c1SHarvey Harrison 				dbg ("%s - write download record failed", __func__);
13091da177e4SLinus Torvalds 				kfree (vheader);
13101da177e4SLinus Torvalds 				kfree (header);
13111da177e4SLinus Torvalds 				kfree (rom_desc);
13121da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13131da177e4SLinus Torvalds 				return status;
13141da177e4SLinus Torvalds 			}
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 			kfree (vheader);
13171da177e4SLinus Torvalds 			kfree (header);
13181da177e4SLinus Torvalds 
1319441b62c1SHarvey Harrison 			dbg ("%s - Start firmware update", __func__);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 			// Tell firmware to copy download image into I2C
13221da177e4SLinus Torvalds 			status = TISendVendorRequestSync (serial->serial->dev,
13231da177e4SLinus Torvalds 						UMPC_COPY_DNLD_TO_I2C,	// Request
13241da177e4SLinus Torvalds 						0,			// wValue
13251da177e4SLinus Torvalds 						0,			// wIndex
13261da177e4SLinus Torvalds 						NULL,			// TransferBuffer
13271da177e4SLinus Torvalds 						0);			// TransferBufferLength
13281da177e4SLinus Torvalds 
1329441b62c1SHarvey Harrison 		  	dbg ("%s - Update complete 0x%x", __func__, status);
13301da177e4SLinus Torvalds 			if (status) {
1331441b62c1SHarvey Harrison 				dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __func__);
13321da177e4SLinus Torvalds 				kfree (rom_desc);
13331da177e4SLinus Torvalds 				kfree (ti_manuf_desc);
13341da177e4SLinus Torvalds 				return status;
13351da177e4SLinus Torvalds 			}
13361da177e4SLinus Torvalds 		}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		// The device is running the download code
13391da177e4SLinus Torvalds 		kfree (rom_desc);
13401da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
13411da177e4SLinus Torvalds 		return 0;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	/********************************************************************/
13451da177e4SLinus Torvalds 	/* Boot Mode */
13461da177e4SLinus Torvalds 	/********************************************************************/
13479544e833SAndrew Morton 	dbg("%s - RUNNING IN BOOT MODE", __func__);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	// Configure the TI device so we can use the BULK pipes for download
13501da177e4SLinus Torvalds 	status = TIConfigureBootDevice (serial->serial->dev);
13511da177e4SLinus Torvalds 	if (status)
13521da177e4SLinus Torvalds 		return status;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
1355441b62c1SHarvey Harrison 		dbg ("%s - VID = 0x%x", __func__,
13561da177e4SLinus Torvalds 		     le16_to_cpu(serial->serial->dev->descriptor.idVendor));
13571da177e4SLinus Torvalds 		serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
13581da177e4SLinus Torvalds 		goto StayInBootMode;
13591da177e4SLinus Torvalds 	}
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	// We have an ION device (I2c Must be programmed)
13621da177e4SLinus Torvalds 	// Determine I2C image type
13631da177e4SLinus Torvalds 	if (TIGetI2cTypeInBootMode(serial)) {
13641da177e4SLinus Torvalds 		goto StayInBootMode;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	// Registry variable set?
13681da177e4SLinus Torvalds 	if (TIStayInBootMode) {
1369441b62c1SHarvey Harrison 		dbg ("%s - TIStayInBootMode", __func__);
13701da177e4SLinus Torvalds 		goto StayInBootMode;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	// Check for ION Vendor ID and that the I2C is valid
13741da177e4SLinus Torvalds 	if (!TiValidateI2cImage(serial)) {
13751da177e4SLinus Torvalds 		struct ti_i2c_image_header *header;
13761da177e4SLinus Torvalds 		int i;
13771da177e4SLinus Torvalds 		__u8 cs = 0;
13781da177e4SLinus Torvalds 		__u8 *buffer;
13791da177e4SLinus Torvalds 		int buffer_size;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		/* Validate Hardware version number
13821da177e4SLinus Torvalds 		 * Read Manufacturing Descriptor from TI Based Edgeport
13831da177e4SLinus Torvalds 		 */
13841da177e4SLinus Torvalds 		ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
13851da177e4SLinus Torvalds 		if (!ti_manuf_desc) {
1386441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory.\n", __func__);
13871da177e4SLinus Torvalds 			return -ENOMEM;
13881da177e4SLinus Torvalds 		}
13891da177e4SLinus Torvalds 		status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
13901da177e4SLinus Torvalds 		if (status) {
13911da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
13921da177e4SLinus Torvalds 			goto StayInBootMode;
13931da177e4SLinus Torvalds 		}
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 		// Check for version 2
13961da177e4SLinus Torvalds 		if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
1397441b62c1SHarvey Harrison 			dbg ("%s - Wrong CPU Rev %d (Must be 2)", __func__,
13981da177e4SLinus Torvalds 			     TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
13991da177e4SLinus Torvalds 			kfree (ti_manuf_desc);
14001da177e4SLinus Torvalds 			goto StayInBootMode;
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 		kfree (ti_manuf_desc);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 		// In order to update the I2C firmware we must change the type 2 record to type 0xF2.
14061da177e4SLinus Torvalds 		// This will force the UMP to come up in Boot Mode.  Then while in boot mode, the driver
14071da177e4SLinus Torvalds 		// will download the latest firmware (padded to 15.5k) into the UMP ram.
14081da177e4SLinus Torvalds 		// And finally when the device comes back up in download mode the driver will cause
14091da177e4SLinus Torvalds 		// the new firmware to be copied from the UMP Ram to I2C and the firmware will update
14101da177e4SLinus Torvalds 		// the record type from 0xf2 to 0x02.
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 		/*
14131da177e4SLinus Torvalds 		 * Do we really have to copy the whole firmware image,
14141da177e4SLinus Torvalds 		 * or could we do this in place!
14151da177e4SLinus Torvalds 		 */
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 		// Allocate a 15.5k buffer + 3 byte header
14181da177e4SLinus Torvalds 		buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
14191da177e4SLinus Torvalds 		buffer = kmalloc (buffer_size, GFP_KERNEL);
14201da177e4SLinus Torvalds 		if (!buffer) {
1421441b62c1SHarvey Harrison 			dev_err (dev, "%s - out of memory\n", __func__);
14221da177e4SLinus Torvalds 			return -ENOMEM;
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 		// Initialize the buffer to 0xff (pad the buffer)
14261da177e4SLinus Torvalds 		memset (buffer, 0xff, buffer_size);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 		memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 		for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
14311da177e4SLinus Torvalds 			cs = (__u8)(cs + buffer[i]);
14321da177e4SLinus Torvalds 		}
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 		header = (struct ti_i2c_image_header *)buffer;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 		// update length and checksum after padding
14371da177e4SLinus Torvalds 		header->Length 	 = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
14381da177e4SLinus Torvalds 		header->CheckSum = cs;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 		// Download the operational code
1441441b62c1SHarvey Harrison 		dbg ("%s - Downloading operational code image (TI UMP)", __func__);
14421da177e4SLinus Torvalds 		status = TIDownloadCodeImage (serial, buffer, buffer_size);
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 		kfree (buffer);
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 		if (status) {
1447441b62c1SHarvey Harrison 	  		dbg ("%s - Error downloading operational code image", __func__);
14481da177e4SLinus Torvalds 			return status;
14491da177e4SLinus Torvalds 		}
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		// Device will reboot
14521da177e4SLinus Torvalds 		serial->product_info.TiMode = TI_MODE_TRANSITIONING;
14531da177e4SLinus Torvalds 
1454441b62c1SHarvey Harrison   		dbg ("%s - Download successful -- Device rebooting...", __func__);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 		/* return an error on purpose */
14571da177e4SLinus Torvalds 		return -ENODEV;
14581da177e4SLinus Torvalds 	}
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds StayInBootMode:
14611da177e4SLinus Torvalds 	// Eprom is invalid or blank stay in boot mode
14629544e833SAndrew Morton 	dbg("%s - STAYING IN BOOT MODE", __func__);
14631da177e4SLinus Torvalds 	serial->product_info.TiMode = TI_MODE_BOOT;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	return 0;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds static int TISetDtr (struct edgeport_port *port)
14701da177e4SLinus Torvalds {
14711da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14721da177e4SLinus Torvalds 
1473441b62c1SHarvey Harrison 	dbg ("%s", __func__);
14741da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_DTR;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14771da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
14781da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14791da177e4SLinus Torvalds 				1,	/* set */
14801da177e4SLinus Torvalds 				NULL,
14811da177e4SLinus Torvalds 				0);
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds static int TIClearDtr (struct edgeport_port *port)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
14871da177e4SLinus Torvalds 
1488441b62c1SHarvey Harrison 	dbg ("%s", __func__);
14891da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_DTR;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
14921da177e4SLinus Torvalds 				UMPC_SET_CLR_DTR,
14931da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
14941da177e4SLinus Torvalds 				0,	/* clear */
14951da177e4SLinus Torvalds 				NULL,
14961da177e4SLinus Torvalds 				0);
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds static int TISetRts (struct edgeport_port *port)
15001da177e4SLinus Torvalds {
15011da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15021da177e4SLinus Torvalds 
1503441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15041da177e4SLinus Torvalds 	port->shadow_mcr |= MCR_RTS;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15071da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
15081da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15091da177e4SLinus Torvalds 				1,	/* set */
15101da177e4SLinus Torvalds 				NULL,
15111da177e4SLinus Torvalds 				0);
15121da177e4SLinus Torvalds }
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds static int TIClearRts (struct edgeport_port *port)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15171da177e4SLinus Torvalds 
1518441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15191da177e4SLinus Torvalds 	port->shadow_mcr &= ~MCR_RTS;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15221da177e4SLinus Torvalds 				UMPC_SET_CLR_RTS,
15231da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15241da177e4SLinus Torvalds 				0,	/* clear */
15251da177e4SLinus Torvalds 				NULL,
15261da177e4SLinus Torvalds 				0);
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds static int TISetLoopBack (struct edgeport_port *port)
15301da177e4SLinus Torvalds {
15311da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15321da177e4SLinus Torvalds 
1533441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15361da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15371da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15381da177e4SLinus Torvalds 				1,	/* set */
15391da177e4SLinus Torvalds 				NULL,
15401da177e4SLinus Torvalds 				0);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds static int TIClearLoopBack (struct edgeport_port *port)
15441da177e4SLinus Torvalds {
15451da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15461da177e4SLinus Torvalds 
1547441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15501da177e4SLinus Torvalds 				UMPC_SET_CLR_LOOPBACK,
15511da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15521da177e4SLinus Torvalds 				0,	/* clear */
15531da177e4SLinus Torvalds 				NULL,
15541da177e4SLinus Torvalds 				0);
15551da177e4SLinus Torvalds }
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds static int TISetBreak (struct edgeport_port *port)
15581da177e4SLinus Torvalds {
15591da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15601da177e4SLinus Torvalds 
1561441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15641da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15651da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15661da177e4SLinus Torvalds 				1,	/* set */
15671da177e4SLinus Torvalds 				NULL,
15681da177e4SLinus Torvalds 				0);
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds static int TIClearBreak (struct edgeport_port *port)
15721da177e4SLinus Torvalds {
15731da177e4SLinus Torvalds 	int port_number = port->port->number - port->port->serial->minor;
15741da177e4SLinus Torvalds 
1575441b62c1SHarvey Harrison 	dbg ("%s", __func__);
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	return TIWriteCommandSync (port->port->serial->dev,
15781da177e4SLinus Torvalds 				UMPC_SET_CLR_BREAK,
15791da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
15801da177e4SLinus Torvalds 				0,	/* clear */
15811da177e4SLinus Torvalds 				NULL,
15821da177e4SLinus Torvalds 				0);
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
15861da177e4SLinus Torvalds {
15871da177e4SLinus Torvalds 	int status = 0;
15881da177e4SLinus Torvalds 
1589441b62c1SHarvey Harrison 	dbg ("%s - %x", __func__, mcr);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	if (mcr & MCR_DTR)
15921da177e4SLinus Torvalds 		status = TISetDtr (port);
15931da177e4SLinus Torvalds 	else
15941da177e4SLinus Torvalds 		status = TIClearDtr (port);
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	if (status)
15971da177e4SLinus Torvalds 		return status;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	if (mcr & MCR_RTS)
16001da177e4SLinus Torvalds 		status = TISetRts (port);
16011da177e4SLinus Torvalds 	else
16021da177e4SLinus Torvalds 		status = TIClearRts (port);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	if (status)
16051da177e4SLinus Torvalds 		return status;
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	if (mcr & MCR_LOOPBACK)
16081da177e4SLinus Torvalds 		status = TISetLoopBack (port);
16091da177e4SLinus Torvalds 	else
16101da177e4SLinus Torvalds 		status = TIClearLoopBack (port);
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	return status;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds /* Convert TI LSR to standard UART flags */
16181da177e4SLinus Torvalds static __u8 MapLineStatus (__u8 ti_lsr)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	__u8 lsr = 0;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds #define MAP_FLAG(flagUmp, flagUart)    \
16231da177e4SLinus Torvalds 	if (ti_lsr & flagUmp) \
16241da177e4SLinus Torvalds 		lsr |= flagUart;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR)	/* overrun */
16271da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR)	/* parity error */
16281da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR)	/* framing error */
16291da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK)	/* break detected */
16301da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL)	/* receive data available */
16311da177e4SLinus Torvalds 	MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY)	/* transmit holding register empty */
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds #undef MAP_FLAG
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	return lsr;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	struct async_icount *icount;
16411da177e4SLinus Torvalds 	struct tty_struct *tty;
16421da177e4SLinus Torvalds 
1643441b62c1SHarvey Harrison 	dbg ("%s - %02x", __func__, msr);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
16461da177e4SLinus Torvalds 		icount = &edge_port->icount;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 		/* update input line counters */
16491da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CTS)
16501da177e4SLinus Torvalds 			icount->cts++;
16511da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_DSR)
16521da177e4SLinus Torvalds 			icount->dsr++;
16531da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_CD)
16541da177e4SLinus Torvalds 			icount->dcd++;
16551da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_DELTA_RI)
16561da177e4SLinus Torvalds 			icount->rng++;
16571da177e4SLinus Torvalds 		wake_up_interruptible (&edge_port->delta_msr_wait);
16581da177e4SLinus Torvalds 	}
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	/* Save the new modem status */
16611da177e4SLinus Torvalds 	edge_port->shadow_msr = msr & 0xf0;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	tty = edge_port->port->tty;
16641da177e4SLinus Torvalds 	/* handle CTS flow control */
16651da177e4SLinus Torvalds 	if (tty && C_CRTSCTS(tty)) {
16661da177e4SLinus Torvalds 		if (msr & EDGEPORT_MSR_CTS) {
16671da177e4SLinus Torvalds 			tty->hw_stopped = 0;
16681da177e4SLinus Torvalds 			tty_wakeup(tty);
16691da177e4SLinus Torvalds 		} else {
16701da177e4SLinus Torvalds 			tty->hw_stopped = 1;
16711da177e4SLinus Torvalds 		}
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	return;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
16781da177e4SLinus Torvalds {
16791da177e4SLinus Torvalds 	struct async_icount *icount;
16801da177e4SLinus Torvalds 	__u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
16811da177e4SLinus Torvalds 
1682441b62c1SHarvey Harrison 	dbg ("%s - %02x", __func__, new_lsr);
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	edge_port->shadow_lsr = lsr;
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK) {
16871da177e4SLinus Torvalds 		/*
16881da177e4SLinus Torvalds 		 * Parity and Framing errors only count if they
16891da177e4SLinus Torvalds 		 * occur exclusive of a break being received.
16901da177e4SLinus Torvalds 		 */
16911da177e4SLinus Torvalds 		new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
16921da177e4SLinus Torvalds 	}
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	/* Place LSR data byte into Rx buffer */
16951da177e4SLinus Torvalds 	if (lsr_data && edge_port->port->tty)
16961da177e4SLinus Torvalds 		edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	/* update input line counters */
16991da177e4SLinus Torvalds 	icount = &edge_port->icount;
17001da177e4SLinus Torvalds 	if (new_lsr & LSR_BREAK)
17011da177e4SLinus Torvalds 		icount->brk++;
17021da177e4SLinus Torvalds 	if (new_lsr & LSR_OVER_ERR)
17031da177e4SLinus Torvalds 		icount->overrun++;
17041da177e4SLinus Torvalds 	if (new_lsr & LSR_PAR_ERR)
17051da177e4SLinus Torvalds 		icount->parity++;
17061da177e4SLinus Torvalds 	if (new_lsr & LSR_FRM_ERR)
17071da177e4SLinus Torvalds 		icount->frame++;
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 
17117d12e780SDavid Howells static void edge_interrupt_callback (struct urb *urb)
17121da177e4SLinus Torvalds {
1713*cdc97792SMing Lei 	struct edgeport_serial *edge_serial = urb->context;
17141da177e4SLinus Torvalds 	struct usb_serial_port *port;
17151da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
17161da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
17171da177e4SLinus Torvalds 	int length = urb->actual_length;
17181da177e4SLinus Torvalds 	int port_number;
17191da177e4SLinus Torvalds 	int function;
1720ee337c21SGreg Kroah-Hartman 	int retval;
17211da177e4SLinus Torvalds 	__u8 lsr;
17221da177e4SLinus Torvalds 	__u8 msr;
1723ee337c21SGreg Kroah-Hartman 	int status = urb->status;
17241da177e4SLinus Torvalds 
1725441b62c1SHarvey Harrison 	dbg("%s", __func__);
17261da177e4SLinus Torvalds 
1727ee337c21SGreg Kroah-Hartman 	switch (status) {
17281da177e4SLinus Torvalds 	case 0:
17291da177e4SLinus Torvalds 		/* success */
17301da177e4SLinus Torvalds 		break;
17311da177e4SLinus Torvalds 	case -ECONNRESET:
17321da177e4SLinus Torvalds 	case -ENOENT:
17331da177e4SLinus Torvalds 	case -ESHUTDOWN:
17341da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1735ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1736441b62c1SHarvey Harrison 		    __func__, status);
17371da177e4SLinus Torvalds 		return;
17381da177e4SLinus Torvalds 	default:
1739ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
1740441b62c1SHarvey Harrison 			"%d\n", __func__, status);
17411da177e4SLinus Torvalds 		goto exit;
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	if (!length) {
1745441b62c1SHarvey Harrison 		dbg ("%s - no data in urb", __func__);
17461da177e4SLinus Torvalds 		goto exit;
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
1749441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if (length != 2) {
1752441b62c1SHarvey Harrison 		dbg ("%s - expecting packet of size 2, got %d", __func__, length);
17531da177e4SLinus Torvalds 		goto exit;
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
17571da177e4SLinus Torvalds 	function    = TIUMP_GET_FUNC_FROM_CODE (data[0]);
17581da177e4SLinus Torvalds 	dbg ("%s - port_number %d, function %d, info 0x%x",
1759441b62c1SHarvey Harrison 	     __func__, port_number, function, data[1]);
17601da177e4SLinus Torvalds 	port = edge_serial->serial->port[port_number];
17611da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
17621da177e4SLinus Torvalds 	if (!edge_port) {
1763441b62c1SHarvey Harrison 		dbg ("%s - edge_port not found", __func__);
17641da177e4SLinus Torvalds 		return;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 	switch (function) {
17671da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_LSR:
17681da177e4SLinus Torvalds 		lsr = MapLineStatus(data[1]);
17691da177e4SLinus Torvalds 		if (lsr & UMP_UART_LSR_DATA_MASK) {
17701da177e4SLinus Torvalds 			/* Save the LSR event for bulk read completion routine */
17711da177e4SLinus Torvalds 			dbg ("%s - LSR Event Port %u LSR Status = %02x",
1772441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17731da177e4SLinus Torvalds 			edge_port->lsr_event = 1;
17741da177e4SLinus Torvalds 			edge_port->lsr_mask = lsr;
17751da177e4SLinus Torvalds 		} else {
17761da177e4SLinus Torvalds 			dbg ("%s - ===== Port %d LSR Status = %02x ======",
1777441b62c1SHarvey Harrison 			     __func__, port_number, lsr);
17781da177e4SLinus Torvalds 			handle_new_lsr (edge_port, 0, lsr, 0);
17791da177e4SLinus Torvalds 		}
17801da177e4SLinus Torvalds 		break;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	case TIUMP_INTERRUPT_CODE_MSR:	// MSR
17831da177e4SLinus Torvalds 		/* Copy MSR from UMP */
17841da177e4SLinus Torvalds 		msr = data[1];
17851da177e4SLinus Torvalds 		dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
1786441b62c1SHarvey Harrison 		     __func__, port_number, msr);
17871da177e4SLinus Torvalds 		handle_new_msr (edge_port, msr);
17881da177e4SLinus Torvalds 		break;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	default:
17911da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
1792441b62c1SHarvey Harrison 			 __func__, data[1]);
17931da177e4SLinus Torvalds 		break;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	}
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds exit:
1798ee337c21SGreg Kroah-Hartman 	retval = usb_submit_urb (urb, GFP_ATOMIC);
1799ee337c21SGreg Kroah-Hartman 	if (retval)
18001da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1801441b62c1SHarvey Harrison 			 __func__, retval);
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
18047d12e780SDavid Howells static void edge_bulk_in_callback (struct urb *urb)
18051da177e4SLinus Torvalds {
1806*cdc97792SMing Lei 	struct edgeport_port *edge_port = urb->context;
18071da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
18081da177e4SLinus Torvalds 	struct tty_struct *tty;
1809ee337c21SGreg Kroah-Hartman 	int retval = 0;
18101da177e4SLinus Torvalds 	int port_number;
1811ee337c21SGreg Kroah-Hartman 	int status = urb->status;
18121da177e4SLinus Torvalds 
1813441b62c1SHarvey Harrison 	dbg("%s", __func__);
18141da177e4SLinus Torvalds 
1815ee337c21SGreg Kroah-Hartman 	switch (status) {
18161da177e4SLinus Torvalds 	case 0:
18171da177e4SLinus Torvalds 		/* success */
18181da177e4SLinus Torvalds 		break;
18191da177e4SLinus Torvalds 	case -ECONNRESET:
18201da177e4SLinus Torvalds 	case -ENOENT:
18211da177e4SLinus Torvalds 	case -ESHUTDOWN:
18221da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1823ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1824441b62c1SHarvey Harrison 		    __func__, status);
18251da177e4SLinus Torvalds 		return;
18261da177e4SLinus Torvalds 	default:
18271da177e4SLinus Torvalds 		dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
1828441b62c1SHarvey Harrison 		     __func__, status);
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 
1831ee337c21SGreg Kroah-Hartman 	if (status == -EPIPE)
18321da177e4SLinus Torvalds 		goto exit;
18331da177e4SLinus Torvalds 
1834ee337c21SGreg Kroah-Hartman 	if (status) {
1835441b62c1SHarvey Harrison 		dev_err(&urb->dev->dev,"%s - stopping read!\n", __func__);
18361da177e4SLinus Torvalds 		return;
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	port_number = edge_port->port->number - edge_port->port->serial->minor;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (edge_port->lsr_event) {
18421da177e4SLinus Torvalds 		edge_port->lsr_event = 0;
18431da177e4SLinus Torvalds 		dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
1844441b62c1SHarvey Harrison 		     __func__, port_number, edge_port->lsr_mask, *data);
18451da177e4SLinus Torvalds 		handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
18461da177e4SLinus Torvalds 		/* Adjust buffer length/pointer */
18471da177e4SLinus Torvalds 		--urb->actual_length;
18481da177e4SLinus Torvalds 		++data;
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	tty = edge_port->port->tty;
18521da177e4SLinus Torvalds 	if (tty && urb->actual_length) {
1853441b62c1SHarvey Harrison 		usb_serial_debug_data(debug, &edge_port->port->dev, __func__, urb->actual_length, data);
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 		if (edge_port->close_pending) {
1856441b62c1SHarvey Harrison 			dbg ("%s - close is pending, dropping data on the floor.", __func__);
18571da177e4SLinus Torvalds 		} else {
18581da177e4SLinus Torvalds 			edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
18591da177e4SLinus Torvalds 		}
18601da177e4SLinus Torvalds 		edge_port->icount.rx += urb->actual_length;
18611da177e4SLinus Torvalds 	}
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds exit:
18641da177e4SLinus Torvalds 	/* continue read unless stopped */
18651da177e4SLinus Torvalds 	spin_lock(&edge_port->ep_lock);
18661da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
18671da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
1868ee337c21SGreg Kroah-Hartman 		retval = usb_submit_urb(urb, GFP_ATOMIC);
18691da177e4SLinus Torvalds 	} else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
18701da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
18711da177e4SLinus Torvalds 	}
18721da177e4SLinus Torvalds 	spin_unlock(&edge_port->ep_lock);
1873ee337c21SGreg Kroah-Hartman 	if (retval)
18741da177e4SLinus Torvalds 		dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
1875441b62c1SHarvey Harrison 			 __func__, retval);
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
18791da177e4SLinus Torvalds {
18801da177e4SLinus Torvalds 	int cnt;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	do {
188333f0f88fSAlan Cox 		cnt = tty_buffer_request_room(tty, length);
188433f0f88fSAlan Cox 		if (cnt < length) {
18851da177e4SLinus Torvalds 			dev_err(dev, "%s - dropping data, %d bytes lost\n",
1886441b62c1SHarvey Harrison 				__func__, length - cnt);
188733f0f88fSAlan Cox 			if(cnt == 0)
188833f0f88fSAlan Cox 				break;
18891da177e4SLinus Torvalds 		}
189033f0f88fSAlan Cox 		tty_insert_flip_string(tty, data, cnt);
18911da177e4SLinus Torvalds 		data += cnt;
18921da177e4SLinus Torvalds 		length -= cnt;
18931da177e4SLinus Torvalds 	} while (length > 0);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	tty_flip_buffer_push(tty);
18961da177e4SLinus Torvalds }
18971da177e4SLinus Torvalds 
18987d12e780SDavid Howells static void edge_bulk_out_callback (struct urb *urb)
18991da177e4SLinus Torvalds {
1900*cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
19011da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1902ee337c21SGreg Kroah-Hartman 	int status = urb->status;
19031da177e4SLinus Torvalds 
1904441b62c1SHarvey Harrison 	dbg ("%s - port %d", __func__, port->number);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
19071da177e4SLinus Torvalds 
1908ee337c21SGreg Kroah-Hartman 	switch (status) {
19091da177e4SLinus Torvalds 	case 0:
19101da177e4SLinus Torvalds 		/* success */
19111da177e4SLinus Torvalds 		break;
19121da177e4SLinus Torvalds 	case -ECONNRESET:
19131da177e4SLinus Torvalds 	case -ENOENT:
19141da177e4SLinus Torvalds 	case -ESHUTDOWN:
19151da177e4SLinus Torvalds 		/* this urb is terminated, clean up */
1916ee337c21SGreg Kroah-Hartman 		dbg("%s - urb shutting down with status: %d",
1917441b62c1SHarvey Harrison 		    __func__, status);
19181da177e4SLinus Torvalds 		return;
19191da177e4SLinus Torvalds 	default:
1920ee337c21SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
1921441b62c1SHarvey Harrison 			"received: %d\n", __func__, status);
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	/* send any buffered data */
19251da177e4SLinus Torvalds 	edge_send(port);
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds static int edge_open (struct usb_serial_port *port, struct file * filp)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
19311da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
19321da177e4SLinus Torvalds 	struct usb_device *dev;
19331da177e4SLinus Torvalds 	struct urb *urb;
19341da177e4SLinus Torvalds 	int port_number;
19351da177e4SLinus Torvalds 	int status;
19361da177e4SLinus Torvalds 	u16 open_settings;
19371da177e4SLinus Torvalds 	u8 transaction_timeout;
19381da177e4SLinus Torvalds 
1939441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	if (edge_port == NULL)
19421da177e4SLinus Torvalds 		return -ENODEV;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	port->tty->low_latency = low_latency;
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
19471da177e4SLinus Torvalds 	switch (port_number) {
19481da177e4SLinus Torvalds 		case 0:
19491da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART1;
19501da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB1_ADDRESS;
19511da177e4SLinus Torvalds 			break;
19521da177e4SLinus Torvalds 		case 1:
19531da177e4SLinus Torvalds 			edge_port->uart_base = UMPMEM_BASE_UART2;
19541da177e4SLinus Torvalds 			edge_port->dma_address = UMPD_OEDB2_ADDRESS;
19551da177e4SLinus Torvalds 			break;
19561da177e4SLinus Torvalds 		default:
19571da177e4SLinus Torvalds 			dev_err (&port->dev, "Unknown port number!!!\n");
19581da177e4SLinus Torvalds 			return -ENODEV;
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1962441b62c1SHarvey Harrison 	     __func__, port_number, edge_port->uart_base, edge_port->dma_address);
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	dev = port->serial->dev;
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
19671da177e4SLinus Torvalds 	init_waitqueue_head (&edge_port->delta_msr_wait);
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	/* turn off loopback */
19701da177e4SLinus Torvalds 	status = TIClearLoopBack (edge_port);
19711da177e4SLinus Torvalds 	if (status) {
19721da177e4SLinus Torvalds 		dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
1973441b62c1SHarvey Harrison 			__func__, status);
19741da177e4SLinus Torvalds 		return status;
19751da177e4SLinus Torvalds 	}
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	/* set up the port settings */
1978d5f5bcd4SAlan Cox 	edge_set_termios (port, port->tty->termios);
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	/* open up the port */
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	/* milliseconds to timeout for DMA transfer */
19831da177e4SLinus Torvalds 	transaction_timeout = 2;
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	// milliseconds to timeout for DMA transfer
19881da177e4SLinus Torvalds 	open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
19891da177e4SLinus Torvalds 			     UMP_PIPE_TRANS_TIMEOUT_ENA |
19901da177e4SLinus Torvalds 			     (transaction_timeout << 2));
19911da177e4SLinus Torvalds 
1992441b62c1SHarvey Harrison 	dbg ("%s - Sending UMPC_OPEN_PORT", __func__);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/* Tell TI to open and start the port */
19951da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
19961da177e4SLinus Torvalds 					UMPC_OPEN_PORT,
19971da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
19981da177e4SLinus Torvalds 					open_settings,
19991da177e4SLinus Torvalds 					NULL,
20001da177e4SLinus Torvalds 					0);
20011da177e4SLinus Torvalds 	if (status) {
2002441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send open command, %d\n", __func__, status);
20031da177e4SLinus Torvalds 		return status;
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	/* Start the DMA? */
20071da177e4SLinus Torvalds 	status = TIWriteCommandSync (dev,
20081da177e4SLinus Torvalds 					UMPC_START_PORT,
20091da177e4SLinus Torvalds 					(u8)(UMPM_UART1_PORT + port_number),
20101da177e4SLinus Torvalds 					0,
20111da177e4SLinus Torvalds 					NULL,
20121da177e4SLinus Torvalds 					0);
20131da177e4SLinus Torvalds 	if (status) {
2014441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __func__, status);
20151da177e4SLinus Torvalds 		return status;
20161da177e4SLinus Torvalds 	}
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	/* Clear TX and RX buffers in UMP */
20191da177e4SLinus Torvalds 	status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
20201da177e4SLinus Torvalds 	if (status) {
2021441b62c1SHarvey Harrison 		dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __func__, status);
20221da177e4SLinus Torvalds 		return status;
20231da177e4SLinus Torvalds 	}
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	/* Read Initial MSR */
20261da177e4SLinus Torvalds 	status = TIReadVendorRequestSync (dev,
20271da177e4SLinus Torvalds 					UMPC_READ_MSR,	// Request
20281da177e4SLinus Torvalds 					0,		// wValue
20291da177e4SLinus Torvalds 					(__u16)(UMPM_UART1_PORT + port_number),	// wIndex (Address)
20301da177e4SLinus Torvalds 					&edge_port->shadow_msr,			// TransferBuffer
20311da177e4SLinus Torvalds 					1);					// TransferBufferLength
20321da177e4SLinus Torvalds 	if (status) {
2033441b62c1SHarvey Harrison  		dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __func__, status);
20341da177e4SLinus Torvalds 		return status;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	/* Set Initial MCR */
20401da177e4SLinus Torvalds 	edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
20411da177e4SLinus Torvalds 	dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	edge_serial = edge_port->edge_serial;
2044241ca64fSMatthias Kaehlcke 	if (mutex_lock_interruptible(&edge_serial->es_lock))
20451da177e4SLinus Torvalds 		return -ERESTARTSYS;
20461da177e4SLinus Torvalds 	if (edge_serial->num_ports_open == 0) {
20471da177e4SLinus Torvalds 		/* we are the first port to be opened, let's post the interrupt urb */
20481da177e4SLinus Torvalds 		urb = edge_serial->serial->port[0]->interrupt_in_urb;
20491da177e4SLinus Torvalds 		if (!urb) {
2050441b62c1SHarvey Harrison 			dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __func__);
20511da177e4SLinus Torvalds 			status = -EINVAL;
2052241ca64fSMatthias Kaehlcke 			goto release_es_lock;
20531da177e4SLinus Torvalds 		}
20541da177e4SLinus Torvalds 		urb->complete = edge_interrupt_callback;
20551da177e4SLinus Torvalds 		urb->context = edge_serial;
20561da177e4SLinus Torvalds 		urb->dev = dev;
20571da177e4SLinus Torvalds 		status = usb_submit_urb (urb, GFP_KERNEL);
20581da177e4SLinus Torvalds 		if (status) {
2059441b62c1SHarvey Harrison 			dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __func__, status);
2060241ca64fSMatthias Kaehlcke 			goto release_es_lock;
20611da177e4SLinus Torvalds 		}
20621da177e4SLinus Torvalds 	}
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	/*
20651da177e4SLinus Torvalds 	 * reset the data toggle on the bulk endpoints to work around bug in
20661da177e4SLinus Torvalds 	 * host controllers where things get out of sync some times
20671da177e4SLinus Torvalds 	 */
20681da177e4SLinus Torvalds 	usb_clear_halt (dev, port->write_urb->pipe);
20691da177e4SLinus Torvalds 	usb_clear_halt (dev, port->read_urb->pipe);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	/* start up our bulk read urb */
20721da177e4SLinus Torvalds 	urb = port->read_urb;
20731da177e4SLinus Torvalds 	if (!urb) {
2074441b62c1SHarvey Harrison 		dev_err (&port->dev, "%s - no read urb present, exiting\n", __func__);
20751da177e4SLinus Torvalds 		status = -EINVAL;
20761da177e4SLinus Torvalds 		goto unlink_int_urb;
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
20791da177e4SLinus Torvalds 	urb->complete = edge_bulk_in_callback;
20801da177e4SLinus Torvalds 	urb->context = edge_port;
20811da177e4SLinus Torvalds 	urb->dev = dev;
20821da177e4SLinus Torvalds 	status = usb_submit_urb (urb, GFP_KERNEL);
20831da177e4SLinus Torvalds 	if (status) {
2084441b62c1SHarvey Harrison 		dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
20851da177e4SLinus Torvalds 		goto unlink_int_urb;
20861da177e4SLinus Torvalds 	}
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	++edge_serial->num_ports_open;
20891da177e4SLinus Torvalds 
2090441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
20911da177e4SLinus Torvalds 
2092241ca64fSMatthias Kaehlcke 	goto release_es_lock;
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds unlink_int_urb:
20951da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open == 0)
20961da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2097241ca64fSMatthias Kaehlcke release_es_lock:
2098241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
20991da177e4SLinus Torvalds 	return status;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds static void edge_close (struct usb_serial_port *port, struct file *filp)
21031da177e4SLinus Torvalds {
21041da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
21051da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
21061da177e4SLinus Torvalds 	int port_number;
21071da177e4SLinus Torvalds 	int status;
21081da177e4SLinus Torvalds 
2109441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	edge_serial = usb_get_serial_data(port->serial);
21121da177e4SLinus Torvalds 	edge_port = usb_get_serial_port_data(port);
21131da177e4SLinus Torvalds 	if ((edge_serial == NULL) || (edge_port == NULL))
21141da177e4SLinus Torvalds 		return;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/* The bulkreadcompletion routine will check
21171da177e4SLinus Torvalds 	 * this flag and dump add read data */
21181da177e4SLinus Torvalds 	edge_port->close_pending = 1;
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 	/* chase the port close and flush */
21211da177e4SLinus Torvalds 	TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
21241da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
21251da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 0;
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	/* assuming we can still talk to the device,
21281da177e4SLinus Torvalds 	 * send a close port command to it */
2129441b62c1SHarvey Harrison 	dbg("%s - send umpc_close_port", __func__);
21301da177e4SLinus Torvalds 	port_number = port->number - port->serial->minor;
21311da177e4SLinus Torvalds 	status = TIWriteCommandSync (port->serial->dev,
21321da177e4SLinus Torvalds 				     UMPC_CLOSE_PORT,
21331da177e4SLinus Torvalds 				     (__u8)(UMPM_UART1_PORT + port_number),
21341da177e4SLinus Torvalds 				     0,
21351da177e4SLinus Torvalds 				     NULL,
21361da177e4SLinus Torvalds 				     0);
2137241ca64fSMatthias Kaehlcke 	mutex_lock(&edge_serial->es_lock);
21381da177e4SLinus Torvalds 	--edge_port->edge_serial->num_ports_open;
21391da177e4SLinus Torvalds 	if (edge_port->edge_serial->num_ports_open <= 0) {
21401da177e4SLinus Torvalds 		/* last port is now closed, let's shut down our interrupt urb */
21411da177e4SLinus Torvalds 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
21421da177e4SLinus Torvalds 		edge_port->edge_serial->num_ports_open = 0;
21431da177e4SLinus Torvalds 	}
2144241ca64fSMatthias Kaehlcke 	mutex_unlock(&edge_serial->es_lock);
21451da177e4SLinus Torvalds 	edge_port->close_pending = 0;
21461da177e4SLinus Torvalds 
2147441b62c1SHarvey Harrison 	dbg("%s - exited", __func__);
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
21511da177e4SLinus Torvalds {
21521da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21531da177e4SLinus Torvalds 	unsigned long flags;
21541da177e4SLinus Torvalds 
2155441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	if (count == 0) {
2158441b62c1SHarvey Harrison 		dbg("%s - write request of 0 bytes", __func__);
21591da177e4SLinus Torvalds 		return 0;
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	if (edge_port == NULL)
21631da177e4SLinus Torvalds 		return -ENODEV;
21641da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
21651da177e4SLinus Torvalds 		return -ENODEV;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21681da177e4SLinus Torvalds 	count = edge_buf_put(edge_port->ep_out_buf, data, count);
21691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	edge_send(port);
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	return count;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds static void edge_send(struct usb_serial_port *port)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	int count, result;
21791da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
21801da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
21811da177e4SLinus Torvalds 	unsigned long flags;
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 
2184441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	if (edge_port->ep_write_urb_in_use) {
21891da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21901da177e4SLinus Torvalds 		return;
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	count = edge_buf_get(edge_port->ep_out_buf,
21941da177e4SLinus Torvalds 				port->write_urb->transfer_buffer,
21951da177e4SLinus Torvalds 				port->bulk_out_size);
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	if (count == 0) {
21981da177e4SLinus Torvalds 		spin_unlock_irqrestore(&edge_port->ep_lock, flags);
21991da177e4SLinus Torvalds 		return;
22001da177e4SLinus Torvalds 	}
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 	edge_port->ep_write_urb_in_use = 1;
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22051da177e4SLinus Torvalds 
2206441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	/* set up our urb */
22091da177e4SLinus Torvalds 	usb_fill_bulk_urb (port->write_urb, port->serial->dev,
22101da177e4SLinus Torvalds 			   usb_sndbulkpipe (port->serial->dev,
22111da177e4SLinus Torvalds 					    port->bulk_out_endpointAddress),
22121da177e4SLinus Torvalds 			   port->write_urb->transfer_buffer, count,
22131da177e4SLinus Torvalds 			   edge_bulk_out_callback,
22141da177e4SLinus Torvalds 			   port);
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/* send the data out the bulk port */
22171da177e4SLinus Torvalds 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
22181da177e4SLinus Torvalds 	if (result) {
2219441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
22201da177e4SLinus Torvalds 		edge_port->ep_write_urb_in_use = 0;
22211da177e4SLinus Torvalds 		// TODO: reschedule edge_send
22221da177e4SLinus Torvalds 	} else {
22231da177e4SLinus Torvalds 		edge_port->icount.tx += count;
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	/* wakeup any process waiting for writes to complete */
22271da177e4SLinus Torvalds 	/* there is now more room in the buffer for new writes */
22281da177e4SLinus Torvalds 	if (tty) {
22291da177e4SLinus Torvalds 		/* let the tty driver wakeup if it has a special write_wakeup function */
22301da177e4SLinus Torvalds 		tty_wakeup(tty);
22311da177e4SLinus Torvalds 	}
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds static int edge_write_room (struct usb_serial_port *port)
22351da177e4SLinus Torvalds {
22361da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22371da177e4SLinus Torvalds 	int room = 0;
22381da177e4SLinus Torvalds 	unsigned long flags;
22391da177e4SLinus Torvalds 
2240441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	if (edge_port == NULL)
22431da177e4SLinus Torvalds 		return -ENODEV;
22441da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22451da177e4SLinus Torvalds 		return -ENODEV;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22481da177e4SLinus Torvalds 	room = edge_buf_space_avail(edge_port->ep_out_buf);
22491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22501da177e4SLinus Torvalds 
2251441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
22521da177e4SLinus Torvalds 	return room;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds static int edge_chars_in_buffer (struct usb_serial_port *port)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22581da177e4SLinus Torvalds 	int chars = 0;
22591da177e4SLinus Torvalds 	unsigned long flags;
22601da177e4SLinus Torvalds 
2261441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	if (edge_port == NULL)
22641da177e4SLinus Torvalds 		return -ENODEV;
22651da177e4SLinus Torvalds 	if (edge_port->close_pending == 1)
22661da177e4SLinus Torvalds 		return -ENODEV;
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
22691da177e4SLinus Torvalds 	chars = edge_buf_data_avail(edge_port->ep_out_buf);
22701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
22711da177e4SLinus Torvalds 
2272441b62c1SHarvey Harrison 	dbg ("%s - returns %d", __func__, chars);
22731da177e4SLinus Torvalds 	return chars;
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds static void edge_throttle (struct usb_serial_port *port)
22771da177e4SLinus Torvalds {
22781da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
22791da177e4SLinus Torvalds 	struct tty_struct *tty;
22801da177e4SLinus Torvalds 	int status;
22811da177e4SLinus Torvalds 
2282441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	if (edge_port == NULL)
22851da177e4SLinus Torvalds 		return;
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds 	tty = port->tty;
22881da177e4SLinus Torvalds 	if (!tty) {
2289441b62c1SHarvey Harrison 		dbg ("%s - no tty available", __func__);
22901da177e4SLinus Torvalds 		return;
22911da177e4SLinus Torvalds 	}
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the stop character */
22941da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
22951da177e4SLinus Torvalds 		unsigned char stop_char = STOP_CHAR(tty);
22961da177e4SLinus Torvalds 		status = edge_write (port, &stop_char, 1);
22971da177e4SLinus Torvalds 		if (status <= 0) {
2298441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
22991da177e4SLinus Torvalds 		}
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, stop reads */
23031da177e4SLinus Torvalds 	/* and the Edgeport will clear the RTS line */
23041da177e4SLinus Torvalds 	if (C_CRTSCTS(tty))
23051da177e4SLinus Torvalds 		stop_read(edge_port);
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds static void edge_unthrottle (struct usb_serial_port *port)
23101da177e4SLinus Torvalds {
23111da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
23121da177e4SLinus Torvalds 	struct tty_struct *tty;
23131da177e4SLinus Torvalds 	int status;
23141da177e4SLinus Torvalds 
2315441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	if (edge_port == NULL)
23181da177e4SLinus Torvalds 		return;
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	tty = port->tty;
23211da177e4SLinus Torvalds 	if (!tty) {
2322441b62c1SHarvey Harrison 		dbg ("%s - no tty available", __func__);
23231da177e4SLinus Torvalds 		return;
23241da177e4SLinus Torvalds 	}
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, send the start character */
23271da177e4SLinus Torvalds 	if (I_IXOFF(tty)) {
23281da177e4SLinus Torvalds 		unsigned char start_char = START_CHAR(tty);
23291da177e4SLinus Torvalds 		status = edge_write (port, &start_char, 1);
23301da177e4SLinus Torvalds 		if (status <= 0) {
2331441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
23321da177e4SLinus Torvalds 		}
23331da177e4SLinus Torvalds 	}
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 	/* if we are implementing RTS/CTS, restart reads */
23361da177e4SLinus Torvalds 	/* are the Edgeport will assert the RTS line */
23371da177e4SLinus Torvalds 	if (C_CRTSCTS(tty)) {
23381da177e4SLinus Torvalds 		status = restart_read(edge_port);
23391da177e4SLinus Torvalds 		if (status)
2340441b62c1SHarvey Harrison 			dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
23411da177e4SLinus Torvalds 	}
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds static void stop_read(struct edgeport_port *edge_port)
23461da177e4SLinus Torvalds {
23471da177e4SLinus Torvalds 	unsigned long flags;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
23521da177e4SLinus Torvalds 		edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
23531da177e4SLinus Torvalds 	edge_port->shadow_mcr &= ~MCR_RTS;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23561da177e4SLinus Torvalds }
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds static int restart_read(struct edgeport_port *edge_port)
23591da177e4SLinus Torvalds {
23601da177e4SLinus Torvalds 	struct urb *urb;
23611da177e4SLinus Torvalds 	int status = 0;
23621da177e4SLinus Torvalds 	unsigned long flags;
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	spin_lock_irqsave(&edge_port->ep_lock, flags);
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
23671da177e4SLinus Torvalds 		urb = edge_port->port->read_urb;
23681da177e4SLinus Torvalds 		urb->complete = edge_bulk_in_callback;
23691da177e4SLinus Torvalds 		urb->context = edge_port;
23701da177e4SLinus Torvalds 		urb->dev = edge_port->port->serial->dev;
2371efdff608SOliver Neukum 		status = usb_submit_urb(urb, GFP_ATOMIC);
23721da177e4SLinus Torvalds 	}
23731da177e4SLinus Torvalds 	edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
23741da177e4SLinus Torvalds 	edge_port->shadow_mcr |= MCR_RTS;
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	return status;
23791da177e4SLinus Torvalds }
23801da177e4SLinus Torvalds 
2381606d099cSAlan Cox static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
23821da177e4SLinus Torvalds {
23831da177e4SLinus Torvalds 	struct ump_uart_config *config;
23841da177e4SLinus Torvalds 	struct tty_struct *tty;
23851da177e4SLinus Torvalds 	int baud;
23861da177e4SLinus Torvalds 	unsigned cflag;
23871da177e4SLinus Torvalds 	int status;
23881da177e4SLinus Torvalds 	int port_number = edge_port->port->number - edge_port->port->serial->minor;
23891da177e4SLinus Torvalds 
2390441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, edge_port->port->number);
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	tty = edge_port->port->tty;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	config = kmalloc (sizeof (*config), GFP_KERNEL);
23951da177e4SLinus Torvalds 	if (!config) {
2396441b62c1SHarvey Harrison 		dev_err (&edge_port->port->dev, "%s - out of memory\n", __func__);
23971da177e4SLinus Torvalds 		return;
23981da177e4SLinus Torvalds 	}
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	config->wFlags = 0;
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 	/* These flags must be set */
24051da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
24061da177e4SLinus Torvalds 	config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
24071da177e4SLinus Torvalds 	config->bUartMode = (__u8)(edge_port->bUartMode);
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	switch (cflag & CSIZE) {
24101da177e4SLinus Torvalds 		case CS5:
24111da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR5BITS;
2412441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 5", __func__);
24131da177e4SLinus Torvalds 			    break;
24141da177e4SLinus Torvalds 		case CS6:
24151da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR6BITS;
2416441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 6", __func__);
24171da177e4SLinus Torvalds 			    break;
24181da177e4SLinus Torvalds 		case CS7:
24191da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR7BITS;
2420441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 7", __func__);
24211da177e4SLinus Torvalds 			    break;
24221da177e4SLinus Torvalds 		default:
24231da177e4SLinus Torvalds 		case CS8:
24241da177e4SLinus Torvalds 			    config->bDataBits = UMP_UART_CHAR8BITS;
2425441b62c1SHarvey Harrison 			    dbg ("%s - data bits = 8", __func__);
24261da177e4SLinus Torvalds 			    break;
24271da177e4SLinus Torvalds 	}
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	if (cflag & PARENB) {
24301da177e4SLinus Torvalds 		if (cflag & PARODD) {
24311da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24321da177e4SLinus Torvalds 			config->bParity = UMP_UART_ODDPARITY;
2433441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
24341da177e4SLinus Torvalds 		} else {
24351da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
24361da177e4SLinus Torvalds 			config->bParity = UMP_UART_EVENPARITY;
2437441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
24381da177e4SLinus Torvalds 		}
24391da177e4SLinus Torvalds 	} else {
24401da177e4SLinus Torvalds 		config->bParity = UMP_UART_NOPARITY;
2441441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	if (cflag & CSTOPB) {
24451da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT2;
2446441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
24471da177e4SLinus Torvalds 	} else {
24481da177e4SLinus Torvalds 		config->bStopBits = UMP_UART_STOPBIT1;
2449441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
24501da177e4SLinus Torvalds 	}
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	/* figure out the flow control settings */
24531da177e4SLinus Torvalds 	if (cflag & CRTSCTS) {
24541da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
24551da177e4SLinus Torvalds 		config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
2456441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is enabled", __func__);
24571da177e4SLinus Torvalds 	} else {
2458441b62c1SHarvey Harrison 		dbg("%s - RTS/CTS is disabled", __func__);
24591da177e4SLinus Torvalds 		tty->hw_stopped = 0;
24601da177e4SLinus Torvalds 		restart_read(edge_port);
24611da177e4SLinus Torvalds 	}
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	/* if we are implementing XON/XOFF, set the start and stop character in the device */
24641da177e4SLinus Torvalds 	if (I_IXOFF(tty) || I_IXON(tty)) {
24651da177e4SLinus Torvalds 		config->cXon  = START_CHAR(tty);
24661da177e4SLinus Torvalds 		config->cXoff = STOP_CHAR(tty);
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 		/* if we are implementing INBOUND XON/XOFF */
24691da177e4SLinus Torvalds 		if (I_IXOFF(tty)) {
24701da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
24711da177e4SLinus Torvalds 			dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2472441b62c1SHarvey Harrison 			     __func__, config->cXon, config->cXoff);
24731da177e4SLinus Torvalds 		} else {
2474441b62c1SHarvey Harrison 			dbg ("%s - INBOUND XON/XOFF is disabled", __func__);
24751da177e4SLinus Torvalds 		}
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 		/* if we are implementing OUTBOUND XON/XOFF */
24781da177e4SLinus Torvalds 		if (I_IXON(tty)) {
24791da177e4SLinus Torvalds 			config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
24801da177e4SLinus Torvalds 			dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2481441b62c1SHarvey Harrison 			     __func__, config->cXon, config->cXoff);
24821da177e4SLinus Torvalds 		} else {
2483441b62c1SHarvey Harrison 			dbg ("%s - OUTBOUND XON/XOFF is disabled", __func__);
24841da177e4SLinus Torvalds 		}
24851da177e4SLinus Torvalds 	}
24861da177e4SLinus Torvalds 
2487d5f5bcd4SAlan Cox 	tty->termios->c_cflag &= ~CMSPAR;
2488d5f5bcd4SAlan Cox 
24891da177e4SLinus Torvalds 	/* Round the baud rate */
24901da177e4SLinus Torvalds 	baud = tty_get_baud_rate(tty);
24911da177e4SLinus Torvalds 	if (!baud) {
24921da177e4SLinus Torvalds 		/* pick a default, any default... */
24931da177e4SLinus Torvalds 		baud = 9600;
2494d5f5bcd4SAlan Cox 	} else
2495d5f5bcd4SAlan Cox 		tty_encode_baud_rate(tty, baud, baud);
2496d5f5bcd4SAlan Cox 
24971da177e4SLinus Torvalds 	edge_port->baud_rate = baud;
24981da177e4SLinus Torvalds 	config->wBaudRate = (__u16)((461550L + baud/2) / baud);
24991da177e4SLinus Torvalds 
2500d5f5bcd4SAlan Cox 	/* FIXME: Recompute actual baud from divisor here */
2501d5f5bcd4SAlan Cox 
2502441b62c1SHarvey Harrison 	dbg ("%s - baud rate = %d, wBaudRate = %d", __func__, baud, config->wBaudRate);
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	dbg ("wBaudRate:   %d", (int)(461550L / config->wBaudRate));
25051da177e4SLinus Torvalds 	dbg ("wFlags:    0x%x", config->wFlags);
25061da177e4SLinus Torvalds 	dbg ("bDataBits:   %d", config->bDataBits);
25071da177e4SLinus Torvalds 	dbg ("bParity:     %d", config->bParity);
25081da177e4SLinus Torvalds 	dbg ("bStopBits:   %d", config->bStopBits);
25091da177e4SLinus Torvalds 	dbg ("cXon:        %d", config->cXon);
25101da177e4SLinus Torvalds 	dbg ("cXoff:       %d", config->cXoff);
25111da177e4SLinus Torvalds 	dbg ("bUartMode:   %d", config->bUartMode);
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	/* move the word values into big endian mode */
25141da177e4SLinus Torvalds 	cpu_to_be16s (&config->wFlags);
25151da177e4SLinus Torvalds 	cpu_to_be16s (&config->wBaudRate);
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	status = TIWriteCommandSync (edge_port->port->serial->dev,
25181da177e4SLinus Torvalds 				UMPC_SET_CONFIG,
25191da177e4SLinus Torvalds 				(__u8)(UMPM_UART1_PORT + port_number),
25201da177e4SLinus Torvalds 				0,
25211da177e4SLinus Torvalds 				(__u8 *)config,
25221da177e4SLinus Torvalds 				sizeof(*config));
25231da177e4SLinus Torvalds 	if (status) {
25241da177e4SLinus Torvalds 		dbg ("%s - error %d when trying to write config to device",
2525441b62c1SHarvey Harrison 		     __func__, status);
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	kfree (config);
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	return;
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
2533606d099cSAlan Cox static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
25341da177e4SLinus Torvalds {
25351da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25361da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
25371da177e4SLinus Torvalds 	unsigned int cflag;
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	cflag = tty->termios->c_cflag;
25401da177e4SLinus Torvalds 
2541441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
25421da177e4SLinus Torvalds 	    tty->termios->c_cflag, tty->termios->c_iflag);
2543441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
25441da177e4SLinus Torvalds 	    old_termios->c_cflag, old_termios->c_iflag);
25451da177e4SLinus Torvalds 
2546441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	if (edge_port == NULL)
25491da177e4SLinus Torvalds 		return;
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	/* change the port settings to the new ones specified */
25521da177e4SLinus Torvalds 	change_port_settings (edge_port, old_termios);
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	return;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
25581da177e4SLinus Torvalds {
25591da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25601da177e4SLinus Torvalds 	unsigned int mcr;
25613d71fe0bSAlan Cox 	unsigned long flags;
25621da177e4SLinus Torvalds 
2563441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25641da177e4SLinus Torvalds 
25653d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
25661da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
25671da177e4SLinus Torvalds 	if (set & TIOCM_RTS)
25681da177e4SLinus Torvalds 		mcr |= MCR_RTS;
25691da177e4SLinus Torvalds 	if (set & TIOCM_DTR)
25701da177e4SLinus Torvalds 		mcr |= MCR_DTR;
25711da177e4SLinus Torvalds 	if (set & TIOCM_LOOP)
25721da177e4SLinus Torvalds 		mcr |= MCR_LOOPBACK;
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	if (clear & TIOCM_RTS)
25751da177e4SLinus Torvalds 		mcr &= ~MCR_RTS;
25761da177e4SLinus Torvalds 	if (clear & TIOCM_DTR)
25771da177e4SLinus Torvalds 		mcr &= ~MCR_DTR;
25781da177e4SLinus Torvalds 	if (clear & TIOCM_LOOP)
25791da177e4SLinus Torvalds 		mcr &= ~MCR_LOOPBACK;
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 	edge_port->shadow_mcr = mcr;
25823d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	TIRestoreMCR (edge_port, mcr);
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 	return 0;
25871da177e4SLinus Torvalds }
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
25901da177e4SLinus Torvalds {
25911da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
25921da177e4SLinus Torvalds 	unsigned int result = 0;
25931da177e4SLinus Torvalds 	unsigned int msr;
25941da177e4SLinus Torvalds 	unsigned int mcr;
25953d71fe0bSAlan Cox 	unsigned long flags;
25961da177e4SLinus Torvalds 
2597441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
25981da177e4SLinus Torvalds 
25993d71fe0bSAlan Cox 	spin_lock_irqsave(&edge_port->ep_lock, flags);
26003d71fe0bSAlan Cox 
26011da177e4SLinus Torvalds 	msr = edge_port->shadow_msr;
26021da177e4SLinus Torvalds 	mcr = edge_port->shadow_mcr;
26031da177e4SLinus Torvalds 	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
26041da177e4SLinus Torvalds 		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
26051da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
26061da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
26071da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
26081da177e4SLinus Torvalds 		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 
2611441b62c1SHarvey Harrison 	dbg("%s -- %x", __func__, result);
26123d71fe0bSAlan Cox 	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	return result;
26151da177e4SLinus Torvalds }
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
26181da177e4SLinus Torvalds {
26191da177e4SLinus Torvalds 	struct serial_struct tmp;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	if (!retinfo)
26221da177e4SLinus Torvalds 		return -EFAULT;
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	tmp.type		= PORT_16550A;
26271da177e4SLinus Torvalds 	tmp.line		= edge_port->port->serial->minor;
26281da177e4SLinus Torvalds 	tmp.port		= edge_port->port->number;
26291da177e4SLinus Torvalds 	tmp.irq			= 0;
26301da177e4SLinus Torvalds 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
26311da177e4SLinus Torvalds 	tmp.xmit_fifo_size	= edge_port->port->bulk_out_size;
26321da177e4SLinus Torvalds 	tmp.baud_base		= 9600;
26331da177e4SLinus Torvalds 	tmp.close_delay		= 5*HZ;
26341da177e4SLinus Torvalds 	tmp.closing_wait	= closing_wait;
26351da177e4SLinus Torvalds //	tmp.custom_divisor	= state->custom_divisor;
26361da177e4SLinus Torvalds //	tmp.hub6		= state->hub6;
26371da177e4SLinus Torvalds //	tmp.io_type		= state->io_type;
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
26411da177e4SLinus Torvalds 		return -EFAULT;
26421da177e4SLinus Torvalds 	return 0;
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
26461da177e4SLinus Torvalds {
26471da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
26481da177e4SLinus Torvalds 	struct async_icount cnow;
26491da177e4SLinus Torvalds 	struct async_icount cprev;
26501da177e4SLinus Torvalds 
2651441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	switch (cmd) {
26541da177e4SLinus Torvalds 		case TIOCINQ:
2655441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCINQ", __func__, port->number);
26561da177e4SLinus Torvalds //			return get_number_bytes_avail(edge_port, (unsigned int *) arg);
26571da177e4SLinus Torvalds 			break;
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 		case TIOCSERGETLSR:
2660441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCSERGETLSR", __func__, port->number);
26611da177e4SLinus Torvalds //			return get_lsr_info(edge_port, (unsigned int *) arg);
26621da177e4SLinus Torvalds 			break;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 		case TIOCGSERIAL:
2665441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
26661da177e4SLinus Torvalds 			return get_serial_info(edge_port, (struct serial_struct __user *) arg);
26671da177e4SLinus Torvalds 			break;
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds 		case TIOCSSERIAL:
2670441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
26711da177e4SLinus Torvalds 			break;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 		case TIOCMIWAIT:
2674441b62c1SHarvey Harrison 			dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
26751da177e4SLinus Torvalds 			cprev = edge_port->icount;
26761da177e4SLinus Torvalds 			while (1) {
26771da177e4SLinus Torvalds 				interruptible_sleep_on(&edge_port->delta_msr_wait);
26781da177e4SLinus Torvalds 				/* see if a signal did it */
26791da177e4SLinus Torvalds 				if (signal_pending(current))
26801da177e4SLinus Torvalds 					return -ERESTARTSYS;
26811da177e4SLinus Torvalds 				cnow = edge_port->icount;
26821da177e4SLinus Torvalds 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
26831da177e4SLinus Torvalds 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
26841da177e4SLinus Torvalds 					return -EIO; /* no change => error */
26851da177e4SLinus Torvalds 				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
26861da177e4SLinus Torvalds 				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
26871da177e4SLinus Torvalds 				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
26881da177e4SLinus Torvalds 				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
26891da177e4SLinus Torvalds 					return 0;
26901da177e4SLinus Torvalds 				}
26911da177e4SLinus Torvalds 				cprev = cnow;
26921da177e4SLinus Torvalds 			}
26931da177e4SLinus Torvalds 			/* not reached */
26941da177e4SLinus Torvalds 			break;
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 		case TIOCGICOUNT:
2697441b62c1SHarvey Harrison 			dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
26981da177e4SLinus Torvalds 			     port->number, edge_port->icount.rx, edge_port->icount.tx);
26991da177e4SLinus Torvalds 			if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
27001da177e4SLinus Torvalds 				return -EFAULT;
27011da177e4SLinus Torvalds 			return 0;
27021da177e4SLinus Torvalds 	}
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static void edge_break (struct usb_serial_port *port, int break_state)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
27101da177e4SLinus Torvalds 	int status;
27111da177e4SLinus Torvalds 
2712441b62c1SHarvey Harrison 	dbg ("%s - state = %d", __func__, break_state);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	/* chase the port close */
27151da177e4SLinus Torvalds 	TIChasePort (edge_port, 0, 0);
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	if (break_state == -1) {
27181da177e4SLinus Torvalds 		status = TISetBreak (edge_port);
27191da177e4SLinus Torvalds 	} else {
27201da177e4SLinus Torvalds 		status = TIClearBreak (edge_port);
27211da177e4SLinus Torvalds 	}
27221da177e4SLinus Torvalds 	if (status) {
27231da177e4SLinus Torvalds 		dbg ("%s - error %d sending break set/clear command.",
2724441b62c1SHarvey Harrison 		     __func__, status);
27251da177e4SLinus Torvalds 	}
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds static int edge_startup (struct usb_serial *serial)
27291da177e4SLinus Torvalds {
27301da177e4SLinus Torvalds 	struct edgeport_serial *edge_serial;
27311da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
27321da177e4SLinus Torvalds 	struct usb_device *dev;
27331da177e4SLinus Torvalds 	int status;
27341da177e4SLinus Torvalds 	int i;
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds 	dev = serial->dev;
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	/* create our private serial structure */
273980b6ca48SEric Sesterhenn 	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
27401da177e4SLinus Torvalds 	if (edge_serial == NULL) {
2741441b62c1SHarvey Harrison 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27421da177e4SLinus Torvalds 		return -ENOMEM;
27431da177e4SLinus Torvalds 	}
2744241ca64fSMatthias Kaehlcke 	mutex_init(&edge_serial->es_lock);
27451da177e4SLinus Torvalds 	edge_serial->serial = serial;
27461da177e4SLinus Torvalds 	usb_set_serial_data(serial, edge_serial);
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	status = TIDownloadFirmware (edge_serial);
27491da177e4SLinus Torvalds 	if (status) {
27501da177e4SLinus Torvalds 		kfree (edge_serial);
27511da177e4SLinus Torvalds 		return status;
27521da177e4SLinus Torvalds 	}
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	/* set up our port private structures */
27551da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
275680b6ca48SEric Sesterhenn 		edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
27571da177e4SLinus Torvalds 		if (edge_port == NULL) {
2758441b62c1SHarvey Harrison 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27591da177e4SLinus Torvalds 			goto cleanup;
27601da177e4SLinus Torvalds 		}
27611da177e4SLinus Torvalds 		spin_lock_init(&edge_port->ep_lock);
27621da177e4SLinus Torvalds 		edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
27631da177e4SLinus Torvalds 		if (edge_port->ep_out_buf == NULL) {
2764441b62c1SHarvey Harrison 			dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
27651da177e4SLinus Torvalds 			kfree(edge_port);
27661da177e4SLinus Torvalds 			goto cleanup;
27671da177e4SLinus Torvalds 		}
27681da177e4SLinus Torvalds 		edge_port->port = serial->port[i];
27691da177e4SLinus Torvalds 		edge_port->edge_serial = edge_serial;
27701da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], edge_port);
2771fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = default_uart_mode;
27721da177e4SLinus Torvalds 	}
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	return 0;
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds cleanup:
27771da177e4SLinus Torvalds 	for (--i; i>=0; --i) {
27781da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
27791da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
27801da177e4SLinus Torvalds 		kfree(edge_port);
27811da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
27821da177e4SLinus Torvalds 	}
27831da177e4SLinus Torvalds 	kfree (edge_serial);
27841da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
27851da177e4SLinus Torvalds 	return -ENOMEM;
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds static void edge_shutdown (struct usb_serial *serial)
27891da177e4SLinus Torvalds {
27901da177e4SLinus Torvalds 	int i;
27911da177e4SLinus Torvalds 	struct edgeport_port *edge_port;
27921da177e4SLinus Torvalds 
2793441b62c1SHarvey Harrison 	dbg ("%s", __func__);
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
27961da177e4SLinus Torvalds 		edge_port = usb_get_serial_port_data(serial->port[i]);
2797fc4cbd75SMartin K. Petersen 		edge_remove_sysfs_attrs(edge_port->port);
27981da177e4SLinus Torvalds 		edge_buf_free(edge_port->ep_out_buf);
27991da177e4SLinus Torvalds 		kfree(edge_port);
28001da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
28011da177e4SLinus Torvalds 	}
28021da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
28031da177e4SLinus Torvalds 	usb_set_serial_data(serial, NULL);
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 
2807fc4cbd75SMartin K. Petersen /* Sysfs Attributes */
2808fc4cbd75SMartin K. Petersen 
2809fc4cbd75SMartin K. Petersen static ssize_t show_uart_mode(struct device *dev,
2810fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, char *buf)
2811fc4cbd75SMartin K. Petersen {
2812fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2813fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2814fc4cbd75SMartin K. Petersen 
2815fc4cbd75SMartin K. Petersen 	return sprintf(buf, "%d\n", edge_port->bUartMode);
2816fc4cbd75SMartin K. Petersen }
2817fc4cbd75SMartin K. Petersen 
2818fc4cbd75SMartin K. Petersen static ssize_t store_uart_mode(struct device *dev,
2819fc4cbd75SMartin K. Petersen 	struct device_attribute *attr, const char *valbuf, size_t count)
2820fc4cbd75SMartin K. Petersen {
2821fc4cbd75SMartin K. Petersen 	struct usb_serial_port *port = to_usb_serial_port(dev);
2822fc4cbd75SMartin K. Petersen 	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2823fc4cbd75SMartin K. Petersen 	unsigned int v = simple_strtoul(valbuf, NULL, 0);
2824fc4cbd75SMartin K. Petersen 
2825441b62c1SHarvey Harrison 	dbg("%s: setting uart_mode = %d", __func__, v);
2826fc4cbd75SMartin K. Petersen 
2827fc4cbd75SMartin K. Petersen 	if (v < 256)
2828fc4cbd75SMartin K. Petersen 		edge_port->bUartMode = v;
2829fc4cbd75SMartin K. Petersen 	else
2830441b62c1SHarvey Harrison 		dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
2831fc4cbd75SMartin K. Petersen 
2832fc4cbd75SMartin K. Petersen 	return count;
2833fc4cbd75SMartin K. Petersen }
2834fc4cbd75SMartin K. Petersen 
2835fc4cbd75SMartin K. Petersen static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode);
2836fc4cbd75SMartin K. Petersen 
2837fc4cbd75SMartin K. Petersen static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2838fc4cbd75SMartin K. Petersen {
2839fc4cbd75SMartin K. Petersen 	return device_create_file(&port->dev, &dev_attr_uart_mode);
2840fc4cbd75SMartin K. Petersen }
2841fc4cbd75SMartin K. Petersen 
2842fc4cbd75SMartin K. Petersen static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2843fc4cbd75SMartin K. Petersen {
2844fc4cbd75SMartin K. Petersen 	device_remove_file(&port->dev, &dev_attr_uart_mode);
2845fc4cbd75SMartin K. Petersen 	return 0;
2846fc4cbd75SMartin K. Petersen }
2847fc4cbd75SMartin K. Petersen 
2848fc4cbd75SMartin K. Petersen 
28491da177e4SLinus Torvalds /* Circular Buffer */
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds /*
28521da177e4SLinus Torvalds  * edge_buf_alloc
28531da177e4SLinus Torvalds  *
28541da177e4SLinus Torvalds  * Allocate a circular buffer and all associated memory.
28551da177e4SLinus Torvalds  */
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds static struct edge_buf *edge_buf_alloc(unsigned int size)
28581da177e4SLinus Torvalds {
28591da177e4SLinus Torvalds 	struct edge_buf *eb;
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds 	if (size == 0)
28631da177e4SLinus Torvalds 		return NULL;
28641da177e4SLinus Torvalds 
28655cbded58SRobert P. J. Day 	eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
28661da177e4SLinus Torvalds 	if (eb == NULL)
28671da177e4SLinus Torvalds 		return NULL;
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	eb->buf_buf = kmalloc(size, GFP_KERNEL);
28701da177e4SLinus Torvalds 	if (eb->buf_buf == NULL) {
28711da177e4SLinus Torvalds 		kfree(eb);
28721da177e4SLinus Torvalds 		return NULL;
28731da177e4SLinus Torvalds 	}
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	eb->buf_size = size;
28761da177e4SLinus Torvalds 	eb->buf_get = eb->buf_put = eb->buf_buf;
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	return eb;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds /*
28831da177e4SLinus Torvalds  * edge_buf_free
28841da177e4SLinus Torvalds  *
28851da177e4SLinus Torvalds  * Free the buffer and all associated memory.
28861da177e4SLinus Torvalds  */
28871da177e4SLinus Torvalds 
28883d48586cSAdrian Bunk static void edge_buf_free(struct edge_buf *eb)
28891da177e4SLinus Torvalds {
28901bc3c9e1SJesper Juhl 	if (eb) {
28911da177e4SLinus Torvalds 		kfree(eb->buf_buf);
28921da177e4SLinus Torvalds 		kfree(eb);
28931da177e4SLinus Torvalds 	}
28941da177e4SLinus Torvalds }
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds /*
28981da177e4SLinus Torvalds  * edge_buf_clear
28991da177e4SLinus Torvalds  *
29001da177e4SLinus Torvalds  * Clear out all data in the circular buffer.
29011da177e4SLinus Torvalds  */
29021da177e4SLinus Torvalds 
29031da177e4SLinus Torvalds static void edge_buf_clear(struct edge_buf *eb)
29041da177e4SLinus Torvalds {
29051da177e4SLinus Torvalds         if (eb != NULL)
29061da177e4SLinus Torvalds                 eb->buf_get = eb->buf_put;
29071da177e4SLinus Torvalds                 /* equivalent to a get of all data available */
29081da177e4SLinus Torvalds }
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds /*
29121da177e4SLinus Torvalds  * edge_buf_data_avail
29131da177e4SLinus Torvalds  *
29141da177e4SLinus Torvalds  * Return the number of bytes of data available in the circular
29151da177e4SLinus Torvalds  * buffer.
29161da177e4SLinus Torvalds  */
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds static unsigned int edge_buf_data_avail(struct edge_buf *eb)
29191da177e4SLinus Torvalds {
29201da177e4SLinus Torvalds 	if (eb != NULL)
29211da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
29221da177e4SLinus Torvalds 	else
29231da177e4SLinus Torvalds 		return 0;
29241da177e4SLinus Torvalds }
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds /*
29281da177e4SLinus Torvalds  * edge_buf_space_avail
29291da177e4SLinus Torvalds  *
29301da177e4SLinus Torvalds  * Return the number of bytes of space available in the circular
29311da177e4SLinus Torvalds  * buffer.
29321da177e4SLinus Torvalds  */
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds static unsigned int edge_buf_space_avail(struct edge_buf *eb)
29351da177e4SLinus Torvalds {
29361da177e4SLinus Torvalds 	if (eb != NULL)
29371da177e4SLinus Torvalds 		return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
29381da177e4SLinus Torvalds 	else
29391da177e4SLinus Torvalds 		return 0;
29401da177e4SLinus Torvalds }
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds /*
29441da177e4SLinus Torvalds  * edge_buf_put
29451da177e4SLinus Torvalds  *
29461da177e4SLinus Torvalds  * Copy data data from a user buffer and put it into the circular buffer.
29471da177e4SLinus Torvalds  * Restrict to the amount of space available.
29481da177e4SLinus Torvalds  *
29491da177e4SLinus Torvalds  * Return the number of bytes copied.
29501da177e4SLinus Torvalds  */
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
29531da177e4SLinus Torvalds 	unsigned int count)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	unsigned int len;
29561da177e4SLinus Torvalds 
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	if (eb == NULL)
29591da177e4SLinus Torvalds 		return 0;
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds 	len  = edge_buf_space_avail(eb);
29621da177e4SLinus Torvalds 	if (count > len)
29631da177e4SLinus Torvalds 		count = len;
29641da177e4SLinus Torvalds 
29651da177e4SLinus Torvalds 	if (count == 0)
29661da177e4SLinus Torvalds 		return 0;
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_put;
29691da177e4SLinus Torvalds 	if (count > len) {
29701da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, len);
29711da177e4SLinus Torvalds 		memcpy(eb->buf_buf, buf+len, count - len);
29721da177e4SLinus Torvalds 		eb->buf_put = eb->buf_buf + count - len;
29731da177e4SLinus Torvalds 	} else {
29741da177e4SLinus Torvalds 		memcpy(eb->buf_put, buf, count);
29751da177e4SLinus Torvalds 		if (count < len)
29761da177e4SLinus Torvalds 			eb->buf_put += count;
29771da177e4SLinus Torvalds 		else /* count == len */
29781da177e4SLinus Torvalds 			eb->buf_put = eb->buf_buf;
29791da177e4SLinus Torvalds 	}
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	return count;
29821da177e4SLinus Torvalds }
29831da177e4SLinus Torvalds 
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds /*
29861da177e4SLinus Torvalds  * edge_buf_get
29871da177e4SLinus Torvalds  *
29881da177e4SLinus Torvalds  * Get data from the circular buffer and copy to the given buffer.
29891da177e4SLinus Torvalds  * Restrict to the amount of data available.
29901da177e4SLinus Torvalds  *
29911da177e4SLinus Torvalds  * Return the number of bytes copied.
29921da177e4SLinus Torvalds  */
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
29951da177e4SLinus Torvalds 	unsigned int count)
29961da177e4SLinus Torvalds {
29971da177e4SLinus Torvalds 	unsigned int len;
29981da177e4SLinus Torvalds 
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds 	if (eb == NULL)
30011da177e4SLinus Torvalds 		return 0;
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	len = edge_buf_data_avail(eb);
30041da177e4SLinus Torvalds 	if (count > len)
30051da177e4SLinus Torvalds 		count = len;
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 	if (count == 0)
30081da177e4SLinus Torvalds 		return 0;
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 	len = eb->buf_buf + eb->buf_size - eb->buf_get;
30111da177e4SLinus Torvalds 	if (count > len) {
30121da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, len);
30131da177e4SLinus Torvalds 		memcpy(buf+len, eb->buf_buf, count - len);
30141da177e4SLinus Torvalds 		eb->buf_get = eb->buf_buf + count - len;
30151da177e4SLinus Torvalds 	} else {
30161da177e4SLinus Torvalds 		memcpy(buf, eb->buf_get, count);
30171da177e4SLinus Torvalds 		if (count < len)
30181da177e4SLinus Torvalds 			eb->buf_get += count;
30191da177e4SLinus Torvalds 		else /* count == len */
30201da177e4SLinus Torvalds 			eb->buf_get = eb->buf_buf;
30211da177e4SLinus Torvalds 	}
30221da177e4SLinus Torvalds 
30231da177e4SLinus Torvalds 	return count;
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds 
3027ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_1port_device = {
302818fcac35SGreg Kroah-Hartman 	.driver = {
30291da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
3030269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_1",
303118fcac35SGreg Kroah-Hartman 	},
3032269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 1 port adapter",
3033d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
30341da177e4SLinus Torvalds 	.id_table		= edgeport_1port_id_table,
30351da177e4SLinus Torvalds 	.num_ports		= 1,
30361da177e4SLinus Torvalds 	.open			= edge_open,
30371da177e4SLinus Torvalds 	.close			= edge_close,
30381da177e4SLinus Torvalds 	.throttle		= edge_throttle,
30391da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
30401da177e4SLinus Torvalds 	.attach			= edge_startup,
30411da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
3042fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
30431da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
30441da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
30451da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
30461da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
30471da177e4SLinus Torvalds 	.write			= edge_write,
30481da177e4SLinus Torvalds 	.write_room		= edge_write_room,
30491da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30501da177e4SLinus Torvalds 	.break_ctl		= edge_break,
30511da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
30521da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30531da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30541da177e4SLinus Torvalds };
30551da177e4SLinus Torvalds 
3056ea65370dSGreg Kroah-Hartman static struct usb_serial_driver edgeport_2port_device = {
305718fcac35SGreg Kroah-Hartman 	.driver = {
30581da177e4SLinus Torvalds 		.owner		= THIS_MODULE,
3059269bda1cSGreg Kroah-Hartman 		.name		= "edgeport_ti_2",
306018fcac35SGreg Kroah-Hartman 	},
3061269bda1cSGreg Kroah-Hartman 	.description		= "Edgeport TI 2 port adapter",
3062d9b1b787SJohannes Hölzl 	.usb_driver		= &io_driver,
30631da177e4SLinus Torvalds 	.id_table		= edgeport_2port_id_table,
30641da177e4SLinus Torvalds 	.num_ports		= 2,
30651da177e4SLinus Torvalds 	.open			= edge_open,
30661da177e4SLinus Torvalds 	.close			= edge_close,
30671da177e4SLinus Torvalds 	.throttle		= edge_throttle,
30681da177e4SLinus Torvalds 	.unthrottle		= edge_unthrottle,
30691da177e4SLinus Torvalds 	.attach			= edge_startup,
30701da177e4SLinus Torvalds 	.shutdown		= edge_shutdown,
3071fc4cbd75SMartin K. Petersen 	.port_probe		= edge_create_sysfs_attrs,
30721da177e4SLinus Torvalds 	.ioctl			= edge_ioctl,
30731da177e4SLinus Torvalds 	.set_termios		= edge_set_termios,
30741da177e4SLinus Torvalds 	.tiocmget		= edge_tiocmget,
30751da177e4SLinus Torvalds 	.tiocmset		= edge_tiocmset,
30761da177e4SLinus Torvalds 	.write			= edge_write,
30771da177e4SLinus Torvalds 	.write_room		= edge_write_room,
30781da177e4SLinus Torvalds 	.chars_in_buffer	= edge_chars_in_buffer,
30791da177e4SLinus Torvalds 	.break_ctl		= edge_break,
30801da177e4SLinus Torvalds 	.read_int_callback	= edge_interrupt_callback,
30811da177e4SLinus Torvalds 	.read_bulk_callback	= edge_bulk_in_callback,
30821da177e4SLinus Torvalds 	.write_bulk_callback	= edge_bulk_out_callback,
30831da177e4SLinus Torvalds };
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds static int __init edgeport_init(void)
30871da177e4SLinus Torvalds {
30881da177e4SLinus Torvalds 	int retval;
30891da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_1port_device);
30901da177e4SLinus Torvalds 	if (retval)
30911da177e4SLinus Torvalds 		goto failed_1port_device_register;
30921da177e4SLinus Torvalds 	retval = usb_serial_register(&edgeport_2port_device);
30931da177e4SLinus Torvalds 	if (retval)
30941da177e4SLinus Torvalds 		goto failed_2port_device_register;
30951da177e4SLinus Torvalds 	retval = usb_register(&io_driver);
30961da177e4SLinus Torvalds 	if (retval)
30971da177e4SLinus Torvalds 		goto failed_usb_register;
30981da177e4SLinus Torvalds 	info(DRIVER_DESC " " DRIVER_VERSION);
30991da177e4SLinus Torvalds 	return 0;
31001da177e4SLinus Torvalds failed_usb_register:
31011da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_2port_device);
31021da177e4SLinus Torvalds failed_2port_device_register:
31031da177e4SLinus Torvalds 	usb_serial_deregister(&edgeport_1port_device);
31041da177e4SLinus Torvalds failed_1port_device_register:
31051da177e4SLinus Torvalds 	return retval;
31061da177e4SLinus Torvalds }
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds static void __exit edgeport_exit (void)
31091da177e4SLinus Torvalds {
31101da177e4SLinus Torvalds 	usb_deregister (&io_driver);
31111da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_1port_device);
31121da177e4SLinus Torvalds 	usb_serial_deregister (&edgeport_2port_device);
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
31151da177e4SLinus Torvalds module_init(edgeport_init);
31161da177e4SLinus Torvalds module_exit(edgeport_exit);
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds /* Module information */
31191da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
31201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
31211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
31241da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds module_param(low_latency, bool, S_IRUGO | S_IWUSR);
31271da177e4SLinus Torvalds MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds module_param(closing_wait, int, S_IRUGO | S_IWUSR);
31301da177e4SLinus Torvalds MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
31311da177e4SLinus Torvalds 
31321da177e4SLinus Torvalds module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
31331da177e4SLinus Torvalds MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
31341da177e4SLinus Torvalds 
3135fc4cbd75SMartin K. Petersen module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3136fc4cbd75SMartin K. Petersen MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");
3137fc4cbd75SMartin K. Petersen 
3138