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