File Index
Files and directories
Sign in
FILE

io_edgeport.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/serial/io_edgeport.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Edgeport USB Serial Converter driver
   4 *
   5 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
   6 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
   7 *
   8 * Supports the following devices:
   9 *	Edgeport/4
  10 *	Edgeport/4t
  11 *	Edgeport/2
  12 *	Edgeport/4i
  13 *	Edgeport/2i
  14 *	Edgeport/421
  15 *	Edgeport/21
  16 *	Rapidport/4
  17 *	Edgeport/8
  18 *	Edgeport/2D8
  19 *	Edgeport/4D8
  20 *	Edgeport/8i
  21 *
  22 * For questions or problems with this driver, contact Inside Out
  23 * Networks technical support, or Peter Berger <pberger@brimson.com>,
  24 * or Al Borchers <alborchers@steinerpoint.com>.
  25 *
  26 */
  27
  28#include <linux/kernel.h>
  29#include <linux/jiffies.h>
  30#include <linux/errno.h>
  31#include <linux/slab.h>
  32#include <linux/tty.h>
  33#include <linux/tty_driver.h>
  34#include <linux/tty_flip.h>
  35#include <linux/module.h>
  36#include <linux/spinlock.h>
  37#include <linux/serial.h>
  38#include <linux/ioctl.h>
  39#include <linux/wait.h>
  40#include <linux/firmware.h>
  41#include <linux/ihex.h>
  42#include <linux/uaccess.h>
  43#include <linux/usb.h>
  44#include <linux/usb/serial.h>
  45#include "io_edgeport.h"
  46#include "io_ionsp.h"		/* info for the iosp messages */
  47#include "io_16654.h"		/* 16654 UART defines */
  48
  49#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  50#define DRIVER_DESC "Edgeport USB Serial Driver"
  51
  52#define MAX_NAME_LEN		64
  53
  54#define OPEN_TIMEOUT		(5*HZ)		/* 5 seconds */
  55
  56static const struct usb_device_id edgeport_2port_id_table[] = {
  57	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2) },
  58	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2I) },
  59	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_421) },
  60	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_21) },
  61	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2_DIN) },
  62	{ }
  63};
  64
  65static const struct usb_device_id edgeport_4port_id_table[] = {
  66	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4) },
  67	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_RAPIDPORT_4) },
  68	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4T) },
  69	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
  70	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4I) },
  71	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
  72	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4_DIN) },
  73	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
  74	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
  75	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
  76	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_BLACKBOX_IC135A) },
  77	{ }
  78};
  79
  80static const struct usb_device_id edgeport_8port_id_table[] = {
  81	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8) },
  82	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
  83	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8I) },
  84	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
  85	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
  86	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
  87	{ }
  88};
  89
  90static const struct usb_device_id Epic_port_id_table[] = {
  91	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
  92	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
  93	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
  94	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
  95	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
  96	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
  97	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
  98	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
  99	{ }
 100};
 101
 102/* Devices that this driver supports */
 103static const struct usb_device_id id_table_combined[] = {
 104	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4) },
 105	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_RAPIDPORT_4) },
 106	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4T) },
 107	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
 108	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2) },
 109	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4I) },
 110	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2I) },
 111	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_421) },
 112	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_21) },
 113	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
 114	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8) },
 115	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2_DIN) },
 116	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4_DIN) },
 117	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
 118	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
 119	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
 120	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
 121	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8I) },
 122	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
 123	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
 124	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
 125	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_BLACKBOX_IC135A) },
 126	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
 127	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
 128	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
 129	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
 130	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
 131	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
 132	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
 133	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
 134	{ } /* Terminating entry */
 135};
 136
 137MODULE_DEVICE_TABLE(usb, id_table_combined);
 138
 139
 140/* receive port state */
 141enum RXSTATE {
 142	EXPECT_HDR1 = 0,    /* Expect header byte 1 */
 143	EXPECT_HDR2 = 1,    /* Expect header byte 2 */
 144	EXPECT_DATA = 2,    /* Expect 'RxBytesRemaining' data */
 145	EXPECT_HDR3 = 3,    /* Expect header byte 3 (for status hdrs only) */
 146};
 147
 148
 149/* Transmit Fifo
 150 * This Transmit queue is an extension of the edgeport Rx buffer.
 151 * The maximum amount of data buffered in both the edgeport
 152 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
 153 */
 154struct TxFifo {
 155	unsigned int	head;	/* index to head pointer (write) */
 156	unsigned int	tail;	/* index to tail pointer (read)  */
 157	unsigned int	count;	/* Bytes in queue */
 158	unsigned int	size;	/* Max size of queue (equal to Max number of TxCredits) */
 159	unsigned char	*fifo;	/* allocated Buffer */
 160};
 161
 162/* This structure holds all of the local port information */
 163struct edgeport_port {
 164	__u16			txCredits;		/* our current credits for this port */
 165	__u16			maxTxCredits;		/* the max size of the port */
 166
 167	struct TxFifo		txfifo;			/* transmit fifo -- size will be maxTxCredits */
 168	struct urb		*write_urb;		/* write URB for this port */
 169	bool			write_in_progress;	/* 'true' while a write URB is outstanding */
 170	spinlock_t		ep_lock;
 171
 172	__u8			shadowLCR;		/* last LCR value received */
 173	__u8			shadowMCR;		/* last MCR value received */
 174	__u8			shadowMSR;		/* last MSR value received */
 175	__u8			shadowLSR;		/* last LSR value received */
 176	__u8			shadowXonChar;		/* last value set as XON char in Edgeport */
 177	__u8			shadowXoffChar;		/* last value set as XOFF char in Edgeport */
 178	__u8			validDataMask;
 179	__u32			baudRate;
 180
 181	bool			open;
 182	bool			openPending;
 183	bool			commandPending;
 184	bool			closePending;
 185	bool			chaseResponsePending;
 186
 187	wait_queue_head_t	wait_chase;		/* for handling sleeping while waiting for chase to finish */
 188	wait_queue_head_t	wait_open;		/* for handling sleeping while waiting for open to finish */
 189	wait_queue_head_t	wait_command;		/* for handling sleeping while waiting for command to finish */
 190
 191	struct usb_serial_port	*port;			/* loop back to the owner of this object */
 192};
 193
 194
 195/* This structure holds all of the individual device information */
 196struct edgeport_serial {
 197	char			name[MAX_NAME_LEN+2];		/* string name of this device */
 198
 199	struct edge_manuf_descriptor	manuf_descriptor;	/* the manufacturer descriptor */
 200	struct edge_boot_descriptor	boot_descriptor;	/* the boot firmware descriptor */
 201	struct edgeport_product_info	product_info;		/* Product Info */
 202	struct edge_compatibility_descriptor epic_descriptor;	/* Edgeport compatible descriptor */
 203	int			is_epic;			/* flag if EPiC device or not */
 204
 205	__u8			interrupt_in_endpoint;		/* the interrupt endpoint handle */
 206	unsigned char		*interrupt_in_buffer;		/* the buffer we use for the interrupt endpoint */
 207	struct urb		*interrupt_read_urb;		/* our interrupt urb */
 208
 209	__u8			bulk_in_endpoint;		/* the bulk in endpoint handle */
 210	unsigned char		*bulk_in_buffer;		/* the buffer we use for the bulk in endpoint */
 211	struct urb		*read_urb;			/* our bulk read urb */
 212	bool			read_in_progress;
 213	spinlock_t		es_lock;
 214
 215	__u8			bulk_out_endpoint;		/* the bulk out endpoint handle */
 216
 217	__s16			rxBytesAvail;			/* the number of bytes that we need to read from this device */
 218
 219	enum RXSTATE		rxState;			/* the current state of the bulk receive processor */
 220	__u8			rxHeader1;			/* receive header byte 1 */
 221	__u8			rxHeader2;			/* receive header byte 2 */
 222	__u8			rxHeader3;			/* receive header byte 3 */
 223	__u8			rxPort;				/* the port that we are currently receiving data for */
 224	__u8			rxStatusCode;			/* the receive status code */
 225	__u8			rxStatusParam;			/* the receive status parameter */
 226	__s16			rxBytesRemaining;		/* the number of port bytes left to read */
 227	struct usb_serial	*serial;			/* loop back to the owner of this object */
 228};
 229
 230/* baud rate information */
 231struct divisor_table_entry {
 232	__u32   BaudRate;
 233	__u16  Divisor;
 234};
 235
 236/*
 237 * Define table of divisors for Rev A EdgePort/4 hardware
 238 * These assume a 3.6864MHz crystal, the standard /16, and
 239 * MCR.7 = 0.
 240 */
 241
 242static const struct divisor_table_entry divisor_table[] = {
 243	{   50,		4608},
 244	{   75,		3072},
 245	{   110,	2095},	/* 2094.545455 => 230450   => .0217 % over */
 246	{   134,	1713},	/* 1713.011152 => 230398.5 => .00065% under */
 247	{   150,	1536},
 248	{   300,	768},
 249	{   600,	384},
 250	{   1200,	192},
 251	{   1800,	128},
 252	{   2400,	96},
 253	{   4800,	48},
 254	{   7200,	32},
 255	{   9600,	24},
 256	{   14400,	16},
 257	{   19200,	12},
 258	{   38400,	6},
 259	{   57600,	4},
 260	{   115200,	2},
 261	{   230400,	1},
 262};
 263
 264/* Number of outstanding Command Write Urbs */
 265static atomic_t CmdUrbs = ATOMIC_INIT(0);
 266
 267
 268/* function prototypes */
 269
 270static void edge_close(struct usb_serial_port *port);
 271
 272static void  process_rcvd_data(struct edgeport_serial *edge_serial,
 273				unsigned char *buffer, __u16 bufferLength);
 274static void process_rcvd_status(struct edgeport_serial *edge_serial,
 275				__u8 byte2, __u8 byte3);
 276static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
 277		int length);
 278static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
 279static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
 280				__u8 lsr, __u8 data);
 281static int  send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
 282				__u8 param);
 283static int  calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
 284static void change_port_settings(struct tty_struct *tty,
 285				struct edgeport_port *edge_port,
 286				const struct ktermios *old_termios);
 287static int  send_cmd_write_uart_register(struct edgeport_port *edge_port,
 288				__u8 regNum, __u8 regValue);
 289static int  write_cmd_usb(struct edgeport_port *edge_port,
 290				unsigned char *buffer, int writeLength);
 291static void send_more_port_data(struct edgeport_serial *edge_serial,
 292				struct edgeport_port *edge_port);
 293
 294static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
 295					__u16 length, const __u8 *data);
 296
 297/* ************************************************************************ */
 298/* ************************************************************************ */
 299/* ************************************************************************ */
 300/* ************************************************************************ */
 301
 302/************************************************************************
 303 *									*
 304 * update_edgeport_E2PROM()	Compare current versions of		*
 305 *				Boot ROM and Manufacture 		*
 306 *				Descriptors with versions		*
 307 *				embedded in this driver			*
 308 *									*
 309 ************************************************************************/
 310static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
 311{
 312	struct device *dev = &edge_serial->serial->dev->dev;
 313	__u32 BootCurVer;
 314	__u32 BootNewVer;
 315	__u8 BootMajorVersion;
 316	__u8 BootMinorVersion;
 317	__u16 BootBuildNumber;
 318	__u32 Bootaddr;
 319	const struct ihex_binrec *rec;
 320	const struct firmware *fw;
 321	const char *fw_name;
 322	int response;
 323
 324	switch (edge_serial->product_info.iDownloadFile) {
 325	case EDGE_DOWNLOAD_FILE_I930:
 326		fw_name	= "edgeport/boot.fw";
 327		break;
 328	case EDGE_DOWNLOAD_FILE_80251:
 329		fw_name	= "edgeport/boot2.fw";
 330		break;
 331	default:
 332		return;
 333	}
 334
 335	response = request_ihex_firmware(&fw, fw_name,
 336					 &edge_serial->serial->dev->dev);
 337	if (response) {
 338		dev_err(dev, "Failed to load image \"%s\" err %d\n",
 339		       fw_name, response);
 340		return;
 341	}
 342
 343	rec = (const struct ihex_binrec *)fw->data;
 344	BootMajorVersion = rec->data[0];
 345	BootMinorVersion = rec->data[1];
 346	BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
 347
 348	/* Check Boot Image Version */
 349	BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
 350		     (edge_serial->boot_descriptor.MinorVersion << 16) +
 351		      le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
 352
 353	BootNewVer = (BootMajorVersion << 24) +
 354		     (BootMinorVersion << 16) +
 355		      BootBuildNumber;
 356
 357	dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
 358	    edge_serial->boot_descriptor.MajorVersion,
 359	    edge_serial->boot_descriptor.MinorVersion,
 360	    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
 361
 362
 363	if (BootNewVer > BootCurVer) {
 364		dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
 365		    edge_serial->boot_descriptor.MajorVersion,
 366		    edge_serial->boot_descriptor.MinorVersion,
 367		    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
 368		    BootMajorVersion, BootMinorVersion, BootBuildNumber);
 369
 370		dev_dbg(dev, "Downloading new Boot Image\n");
 371
 372		for (rec = ihex_next_binrec(rec); rec;
 373		     rec = ihex_next_binrec(rec)) {
 374			Bootaddr = be32_to_cpu(rec->addr);
 375			response = rom_write(edge_serial->serial,
 376					     Bootaddr >> 16,
 377					     Bootaddr & 0xFFFF,
 378					     be16_to_cpu(rec->len),
 379					     &rec->data[0]);
 380			if (response < 0) {
 381				dev_err(&edge_serial->serial->dev->dev,
 382					"rom_write failed (%x, %x, %d)\n",
 383					Bootaddr >> 16, Bootaddr & 0xFFFF,
 384					be16_to_cpu(rec->len));
 385				break;
 386			}
 387		}
 388	} else {
 389		dev_dbg(dev, "Boot Image -- already up to date\n");
 390	}
 391	release_firmware(fw);
 392}
 393
 394static void dump_product_info(struct edgeport_serial *edge_serial,
 395			      struct edgeport_product_info *product_info)
 396{
 397	struct device *dev = &edge_serial->serial->dev->dev;
 398
 399	/* Dump Product Info structure */
 400	dev_dbg(dev, "**Product Information:\n");
 401	dev_dbg(dev, "  ProductId             %x\n", product_info->ProductId);
 402	dev_dbg(dev, "  NumPorts              %d\n", product_info->NumPorts);
 403	dev_dbg(dev, "  ProdInfoVer           %d\n", product_info->ProdInfoVer);
 404	dev_dbg(dev, "  IsServer              %d\n", product_info->IsServer);
 405	dev_dbg(dev, "  IsRS232               %d\n", product_info->IsRS232);
 406	dev_dbg(dev, "  IsRS422               %d\n", product_info->IsRS422);
 407	dev_dbg(dev, "  IsRS485               %d\n", product_info->IsRS485);
 408	dev_dbg(dev, "  RomSize               %d\n", product_info->RomSize);
 409	dev_dbg(dev, "  RamSize               %d\n", product_info->RamSize);
 410	dev_dbg(dev, "  CpuRev                %x\n", product_info->CpuRev);
 411	dev_dbg(dev, "  BoardRev              %x\n", product_info->BoardRev);
 412	dev_dbg(dev, "  BootMajorVersion      %d.%d.%d\n",
 413		product_info->BootMajorVersion,
 414		product_info->BootMinorVersion,
 415		le16_to_cpu(product_info->BootBuildNumber));
 416	dev_dbg(dev, "  FirmwareMajorVersion  %d.%d.%d\n",
 417		product_info->FirmwareMajorVersion,
 418		product_info->FirmwareMinorVersion,
 419		le16_to_cpu(product_info->FirmwareBuildNumber));
 420	dev_dbg(dev, "  ManufactureDescDate   %d/%d/%d\n",
 421		product_info->ManufactureDescDate[0],
 422		product_info->ManufactureDescDate[1],
 423		product_info->ManufactureDescDate[2]+1900);
 424	dev_dbg(dev, "  iDownloadFile         0x%x\n",
 425		product_info->iDownloadFile);
 426	dev_dbg(dev, "  EpicVer               %d\n", product_info->EpicVer);
 427}
 428
 429static void get_product_info(struct edgeport_serial *edge_serial)
 430{
 431	struct edgeport_product_info *product_info = &edge_serial->product_info;
 432
 433	memset(product_info, 0, sizeof(struct edgeport_product_info));
 434
 435	product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
 436	product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
 437	product_info->ProdInfoVer = 0;
 438
 439	product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
 440	product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
 441	product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
 442	product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
 443
 444	product_info->BootMajorVersion =
 445				edge_serial->boot_descriptor.MajorVersion;
 446	product_info->BootMinorVersion =
 447				edge_serial->boot_descriptor.MinorVersion;
 448	product_info->BootBuildNumber =
 449				edge_serial->boot_descriptor.BuildNumber;
 450
 451	memcpy(product_info->ManufactureDescDate,
 452			edge_serial->manuf_descriptor.DescDate,
 453			sizeof(edge_serial->manuf_descriptor.DescDate));
 454
 455	/* check if this is 2nd generation hardware */
 456	if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
 457					    & ION_DEVICE_ID_80251_NETCHIP)
 458		product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
 459	else
 460		product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
 461
 462	/* Determine Product type and set appropriate flags */
 463	switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
 464	case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
 465	case ION_DEVICE_ID_EDGEPORT_4T:
 466	case ION_DEVICE_ID_EDGEPORT_4:
 467	case ION_DEVICE_ID_EDGEPORT_2:
 468	case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
 469	case ION_DEVICE_ID_EDGEPORT_8:
 470	case ION_DEVICE_ID_EDGEPORT_421:
 471	case ION_DEVICE_ID_EDGEPORT_21:
 472	case ION_DEVICE_ID_EDGEPORT_2_DIN:
 473	case ION_DEVICE_ID_EDGEPORT_4_DIN:
 474	case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
 475	case ION_DEVICE_ID_BLACKBOX_IC135A:
 476		product_info->IsRS232 = 1;
 477		break;
 478
 479	case ION_DEVICE_ID_EDGEPORT_2I:	/* Edgeport/2 RS422/RS485 */
 480		product_info->IsRS422 = 1;
 481		product_info->IsRS485 = 1;
 482		break;
 483
 484	case ION_DEVICE_ID_EDGEPORT_8I:	/* Edgeport/4 RS422 */
 485	case ION_DEVICE_ID_EDGEPORT_4I:	/* Edgeport/4 RS422 */
 486		product_info->IsRS422 = 1;
 487		break;
 488	}
 489
 490	dump_product_info(edge_serial, product_info);
 491}
 492
 493static int get_epic_descriptor(struct edgeport_serial *ep)
 494{
 495	int result;
 496	struct usb_serial *serial = ep->serial;
 497	struct edgeport_product_info *product_info = &ep->product_info;
 498	struct edge_compatibility_descriptor *epic;
 499	struct edge_compatibility_bits *bits;
 500	struct device *dev = &serial->dev->dev;
 501
 502	ep->is_epic = 0;
 503
 504	epic = kmalloc(sizeof(*epic), GFP_KERNEL);
 505	if (!epic)
 506		return -ENOMEM;
 507
 508	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 509				 USB_REQUEST_ION_GET_EPIC_DESC,
 510				 0xC0, 0x00, 0x00,
 511				 epic, sizeof(*epic),
 512				 300);
 513	if (result == sizeof(*epic)) {
 514		ep->is_epic = 1;
 515		memcpy(&ep->epic_descriptor, epic, sizeof(*epic));
 516		memset(product_info, 0, sizeof(struct edgeport_product_info));
 517
 518		product_info->NumPorts = epic->NumPorts;
 519		product_info->ProdInfoVer = 0;
 520		product_info->FirmwareMajorVersion = epic->MajorVersion;
 521		product_info->FirmwareMinorVersion = epic->MinorVersion;
 522		product_info->FirmwareBuildNumber = epic->BuildNumber;
 523		product_info->iDownloadFile = epic->iDownloadFile;
 524		product_info->EpicVer = epic->EpicVer;
 525		product_info->Epic = epic->Supports;
 526		product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
 527		dump_product_info(ep, product_info);
 528
 529		bits = &ep->epic_descriptor.Supports;
 530		dev_dbg(dev, "**EPIC descriptor:\n");
 531		dev_dbg(dev, "  VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
 532		dev_dbg(dev, "  IOSPOpen         : %s\n", bits->IOSPOpen	? "TRUE": "FALSE");
 533		dev_dbg(dev, "  IOSPClose        : %s\n", bits->IOSPClose	? "TRUE": "FALSE");
 534		dev_dbg(dev, "  IOSPChase        : %s\n", bits->IOSPChase	? "TRUE": "FALSE");
 535		dev_dbg(dev, "  IOSPSetRxFlow    : %s\n", bits->IOSPSetRxFlow	? "TRUE": "FALSE");
 536		dev_dbg(dev, "  IOSPSetTxFlow    : %s\n", bits->IOSPSetTxFlow	? "TRUE": "FALSE");
 537		dev_dbg(dev, "  IOSPSetXChar     : %s\n", bits->IOSPSetXChar	? "TRUE": "FALSE");
 538		dev_dbg(dev, "  IOSPRxCheck      : %s\n", bits->IOSPRxCheck	? "TRUE": "FALSE");
 539		dev_dbg(dev, "  IOSPSetClrBreak  : %s\n", bits->IOSPSetClrBreak	? "TRUE": "FALSE");
 540		dev_dbg(dev, "  IOSPWriteMCR     : %s\n", bits->IOSPWriteMCR	? "TRUE": "FALSE");
 541		dev_dbg(dev, "  IOSPWriteLCR     : %s\n", bits->IOSPWriteLCR	? "TRUE": "FALSE");
 542		dev_dbg(dev, "  IOSPSetBaudRate  : %s\n", bits->IOSPSetBaudRate	? "TRUE": "FALSE");
 543		dev_dbg(dev, "  TrueEdgeport     : %s\n", bits->TrueEdgeport	? "TRUE": "FALSE");
 544
 545		result = 0;
 546	} else if (result >= 0) {
 547		dev_warn(&serial->interface->dev, "short epic descriptor received: %d\n",
 548			 result);
 549		result = -EIO;
 550	}
 551
 552	kfree(epic);
 553
 554	return result;
 555}
 556
 557
 558/************************************************************************/
 559/************************************************************************/
 560/*            U S B  C A L L B A C K   F U N C T I O N S                */
 561/*            U S B  C A L L B A C K   F U N C T I O N S                */
 562/************************************************************************/
 563/************************************************************************/
 564
 565/*****************************************************************************
 566 * edge_interrupt_callback
 567 *	this is the callback function for when we have received data on the
 568 *	interrupt endpoint.
 569 *****************************************************************************/
 570static void edge_interrupt_callback(struct urb *urb)
 571{
 572	struct edgeport_serial *edge_serial = urb->context;
 573	struct device *dev;
 574	struct edgeport_port *edge_port;
 575	struct usb_serial_port *port;
 576	unsigned char *data = urb->transfer_buffer;
 577	int length = urb->actual_length;
 578	unsigned long flags;
 579	int bytes_avail;
 580	int position;
 581	int txCredits;
 582	int portNumber;
 583	int result;
 584	int status = urb->status;
 585
 586	switch (status) {
 587	case 0:
 588		/* success */
 589		break;
 590	case -ECONNRESET:
 591	case -ENOENT:
 592	case -ESHUTDOWN:
 593		/* this urb is terminated, clean up */
 594		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
 595		return;
 596	default:
 597		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
 598		goto exit;
 599	}
 600
 601	dev = &edge_serial->serial->dev->dev;
 602
 603	/* process this interrupt-read even if there are no ports open */
 604	if (length) {
 605		usb_serial_debug_data(dev, __func__, length, data);
 606
 607		if (length > 1) {
 608			bytes_avail = data[0] | (data[1] << 8);
 609			if (bytes_avail) {
 610				spin_lock_irqsave(&edge_serial->es_lock, flags);
 611				edge_serial->rxBytesAvail += bytes_avail;
 612				dev_dbg(dev,
 613					"%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
 614					__func__, bytes_avail,
 615					edge_serial->rxBytesAvail,
 616					edge_serial->read_in_progress);
 617
 618				if (edge_serial->rxBytesAvail > 0 &&
 619				    !edge_serial->read_in_progress) {
 620					dev_dbg(dev, "%s - posting a read\n", __func__);
 621					edge_serial->read_in_progress = true;
 622
 623					/* we have pending bytes on the
 624					   bulk in pipe, send a request */
 625					result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
 626					if (result) {
 627						dev_err(dev,
 628							"%s - usb_submit_urb(read bulk) failed with result = %d\n",
 629							__func__, result);
 630						edge_serial->read_in_progress = false;
 631					}
 632				}
 633				spin_unlock_irqrestore(&edge_serial->es_lock,
 634						       flags);
 635			}
 636		}
 637		/* grab the txcredits for the ports if available */
 638		position = 2;
 639		portNumber = 0;
 640		while ((position < length - 1) &&
 641				(portNumber < edge_serial->serial->num_ports)) {
 642			txCredits = data[position] | (data[position+1] << 8);
 643			if (txCredits) {
 644				port = edge_serial->serial->port[portNumber];
 645				edge_port = usb_get_serial_port_data(port);
 646				if (edge_port && edge_port->open) {
 647					spin_lock_irqsave(&edge_port->ep_lock,
 648							  flags);
 649					edge_port->txCredits += txCredits;
 650					spin_unlock_irqrestore(&edge_port->ep_lock,
 651							       flags);
 652					dev_dbg(dev, "%s - txcredits for port%d = %d\n",
 653						__func__, portNumber,
 654						edge_port->txCredits);
 655
 656					/* tell the tty driver that something
 657					   has changed */
 658					tty_port_tty_wakeup(&edge_port->port->port);
 659					/* Since we have more credit, check
 660					   if more data can be sent */
 661					send_more_port_data(edge_serial,
 662								edge_port);
 663				}
 664			}
 665			position += 2;
 666			++portNumber;
 667		}
 668	}
 669
 670exit:
 671	result = usb_submit_urb(urb, GFP_ATOMIC);
 672	if (result)
 673		dev_err(&urb->dev->dev,
 674			"%s - Error %d submitting control urb\n",
 675						__func__, result);
 676}
 677
 678
 679/*****************************************************************************
 680 * edge_bulk_in_callback
 681 *	this is the callback function for when we have received data on the
 682 *	bulk in endpoint.
 683 *****************************************************************************/
 684static void edge_bulk_in_callback(struct urb *urb)
 685{
 686	struct edgeport_serial	*edge_serial = urb->context;
 687	struct device *dev;
 688	unsigned char		*data = urb->transfer_buffer;
 689	int			retval;
 690	__u16			raw_data_length;
 691	int status = urb->status;
 692	unsigned long flags;
 693
 694	if (status) {
 695		dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
 696			__func__, status);
 697		edge_serial->read_in_progress = false;
 698		return;
 699	}
 700
 701	if (urb->actual_length == 0) {
 702		dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
 703		edge_serial->read_in_progress = false;
 704		return;
 705	}
 706
 707	dev = &edge_serial->serial->dev->dev;
 708	raw_data_length = urb->actual_length;
 709
 710	usb_serial_debug_data(dev, __func__, raw_data_length, data);
 711
 712	spin_lock_irqsave(&edge_serial->es_lock, flags);
 713
 714	/* decrement our rxBytes available by the number that we just got */
 715	edge_serial->rxBytesAvail -= raw_data_length;
 716
 717	dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
 718		raw_data_length, edge_serial->rxBytesAvail);
 719
 720	process_rcvd_data(edge_serial, data, urb->actual_length);
 721
 722	/* check to see if there's any more data for us to read */
 723	if (edge_serial->rxBytesAvail > 0) {
 724		dev_dbg(dev, "%s - posting a read\n", __func__);
 725		retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
 726		if (retval) {
 727			dev_err(dev,
 728				"%s - usb_submit_urb(read bulk) failed, retval = %d\n",
 729				__func__, retval);
 730			edge_serial->read_in_progress = false;
 731		}
 732	} else {
 733		edge_serial->read_in_progress = false;
 734	}
 735
 736	spin_unlock_irqrestore(&edge_serial->es_lock, flags);
 737}
 738
 739
 740/*****************************************************************************
 741 * edge_bulk_out_data_callback
 742 *	this is the callback function for when we have finished sending
 743 *	serial data on the bulk out endpoint.
 744 *****************************************************************************/
 745static void edge_bulk_out_data_callback(struct urb *urb)
 746{
 747	struct edgeport_port *edge_port = urb->context;
 748	int status = urb->status;
 749
 750	if (status) {
 751		dev_dbg(&urb->dev->dev,
 752			"%s - nonzero write bulk status received: %d\n",
 753			__func__, status);
 754	}
 755
 756	if (edge_port->open)
 757		tty_port_tty_wakeup(&edge_port->port->port);
 758
 759	/* Release the Write URB */
 760	edge_port->write_in_progress = false;
 761
 762	/* Check if more data needs to be sent */
 763	send_more_port_data((struct edgeport_serial *)
 764		(usb_get_serial_data(edge_port->port->serial)), edge_port);
 765}
 766
 767
 768/*****************************************************************************
 769 * BulkOutCmdCallback
 770 *	this is the callback function for when we have finished sending a
 771 *	command	on the bulk out endpoint.
 772 *****************************************************************************/
 773static void edge_bulk_out_cmd_callback(struct urb *urb)
 774{
 775	struct edgeport_port *edge_port = urb->context;
 776	struct device *dev = &urb->dev->dev;
 777	int status = urb->status;
 778
 779	atomic_dec(&CmdUrbs);
 780	dev_dbg(dev, "%s - FREE URB %p (outstanding %d)\n", __func__, urb,
 781		atomic_read(&CmdUrbs));
 782
 783
 784	/* clean up the transfer buffer */
 785	kfree(urb->transfer_buffer);
 786
 787	/* Free the command urb */
 788	usb_free_urb(urb);
 789
 790	if (status) {
 791		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
 792			__func__, status);
 793		return;
 794	}
 795
 796	/* tell the tty driver that something has changed */
 797	if (edge_port->open)
 798		tty_port_tty_wakeup(&edge_port->port->port);
 799
 800	/* we have completed the command */
 801	edge_port->commandPending = false;
 802	wake_up(&edge_port->wait_command);
 803}
 804
 805
 806/*****************************************************************************
 807 * Driver tty interface functions
 808 *****************************************************************************/
 809
 810/*****************************************************************************
 811 * SerialOpen
 812 *	this function is called by the tty driver when a port is opened
 813 *	If successful, we return 0
 814 *	Otherwise we return a negative error number.
 815 *****************************************************************************/
 816static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
 817{
 818	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
 819	struct device *dev = &port->dev;
 820	struct usb_serial *serial;
 821	struct edgeport_serial *edge_serial;
 822	int response;
 823
 824	if (edge_port == NULL)
 825		return -ENODEV;
 826
 827	/* see if we've set up our endpoint info yet (can't set it up
 828	   in edge_startup as the structures were not set up at that time.) */
 829	serial = port->serial;
 830	edge_serial = usb_get_serial_data(serial);
 831	if (edge_serial == NULL)
 832		return -ENODEV;
 833	if (edge_serial->interrupt_in_buffer == NULL) {
 834		struct usb_serial_port *port0 = serial->port[0];
 835
 836		/* not set up yet, so do it now */
 837		edge_serial->interrupt_in_buffer =
 838					port0->interrupt_in_buffer;
 839		edge_serial->interrupt_in_endpoint =
 840					port0->interrupt_in_endpointAddress;
 841		edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
 842		edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
 843		edge_serial->bulk_in_endpoint =
 844					port0->bulk_in_endpointAddress;
 845		edge_serial->read_urb = port0->read_urb;
 846		edge_serial->bulk_out_endpoint =
 847					port0->bulk_out_endpointAddress;
 848
 849		/* set up our interrupt urb */
 850		usb_fill_int_urb(edge_serial->interrupt_read_urb,
 851		      serial->dev,
 852		      usb_rcvintpipe(serial->dev,
 853				port0->interrupt_in_endpointAddress),
 854		      port0->interrupt_in_buffer,
 855		      edge_serial->interrupt_read_urb->transfer_buffer_length,
 856		      edge_interrupt_callback, edge_serial,
 857		      edge_serial->interrupt_read_urb->interval);
 858
 859		/* set up our bulk in urb */
 860		usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
 861			usb_rcvbulkpipe(serial->dev,
 862				port0->bulk_in_endpointAddress),
 863			port0->bulk_in_buffer,
 864			edge_serial->read_urb->transfer_buffer_length,
 865			edge_bulk_in_callback, edge_serial);
 866		edge_serial->read_in_progress = false;
 867
 868		/* start interrupt read for this edgeport
 869		 * this interrupt will continue as long
 870		 * as the edgeport is connected */
 871		response = usb_submit_urb(edge_serial->interrupt_read_urb,
 872								GFP_KERNEL);
 873		if (response) {
 874			dev_err(dev, "%s - Error %d submitting control urb\n",
 875				__func__, response);
 876		}
 877	}
 878
 879	/* initialize our wait queues */
 880	init_waitqueue_head(&edge_port->wait_open);
 881	init_waitqueue_head(&edge_port->wait_chase);
 882	init_waitqueue_head(&edge_port->wait_command);
 883
 884	/* initialize our port settings */
 885	edge_port->txCredits = 0;	/* Can't send any data yet */
 886	/* Must always set this bit to enable ints! */
 887	edge_port->shadowMCR = MCR_MASTER_IE;
 888	edge_port->chaseResponsePending = false;
 889
 890	/* send a open port command */
 891	edge_port->openPending = true;
 892	edge_port->open        = false;
 893	response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
 894
 895	if (response < 0) {
 896		dev_err(dev, "%s - error sending open port command\n", __func__);
 897		edge_port->openPending = false;
 898		return -ENODEV;
 899	}
 900
 901	/* now wait for the port to be completely opened */
 902	wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
 903								OPEN_TIMEOUT);
 904
 905	if (!edge_port->open) {
 906		/* open timed out */
 907		dev_dbg(dev, "%s - open timeout\n", __func__);
 908		edge_port->openPending = false;
 909		return -ENODEV;
 910	}
 911
 912	/* create the txfifo */
 913	edge_port->txfifo.head	= 0;
 914	edge_port->txfifo.tail	= 0;
 915	edge_port->txfifo.count	= 0;
 916	edge_port->txfifo.size	= edge_port->maxTxCredits;
 917	edge_port->txfifo.fifo	= kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
 918
 919	if (!edge_port->txfifo.fifo) {
 920		edge_close(port);
 921		return -ENOMEM;
 922	}
 923
 924	/* Allocate a URB for the write */
 925	edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
 926	edge_port->write_in_progress = false;
 927
 928	if (!edge_port->write_urb) {
 929		edge_close(port);
 930		return -ENOMEM;
 931	}
 932
 933	dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
 934		__func__, edge_port->maxTxCredits);
 935
 936	return 0;
 937}
 938
 939
 940/************************************************************************
 941 *
 942 * block_until_chase_response
 943 *
 944 *	This function will block the close until one of the following:
 945 *		1. Response to our Chase comes from Edgeport
 946 *		2. A timeout of 10 seconds without activity has expired
 947 *		   (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
 948 *
 949 ************************************************************************/
 950static void block_until_chase_response(struct edgeport_port *edge_port)
 951{
 952	struct device *dev = &edge_port->port->dev;
 953	DEFINE_WAIT(wait);
 954	__u16 lastCredits;
 955	int timeout = 1*HZ;
 956	int loop = 10;
 957
 958	while (1) {
 959		/* Save Last credits */
 960		lastCredits = edge_port->txCredits;
 961
 962		/* Did we get our Chase response */
 963		if (!edge_port->chaseResponsePending) {
 964			dev_dbg(dev, "%s - Got Chase Response\n", __func__);
 965
 966			/* did we get all of our credit back? */
 967			if (edge_port->txCredits == edge_port->maxTxCredits) {
 968				dev_dbg(dev, "%s - Got all credits\n", __func__);
 969				return;
 970			}
 971		}
 972
 973		/* Block the thread for a while */
 974		prepare_to_wait(&edge_port->wait_chase, &wait,
 975						TASK_UNINTERRUPTIBLE);
 976		schedule_timeout(timeout);
 977		finish_wait(&edge_port->wait_chase, &wait);
 978
 979		if (lastCredits == edge_port->txCredits) {
 980			/* No activity.. count down. */
 981			loop--;
 982			if (loop == 0) {
 983				edge_port->chaseResponsePending = false;
 984				dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
 985				return;
 986			}
 987		} else {
 988			/* Reset timeout value back to 10 seconds */
 989			dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
 990					lastCredits, edge_port->txCredits);
 991			loop = 10;
 992		}
 993	}
 994}
 995
 996
 997/************************************************************************
 998 *
 999 * block_until_tx_empty
1000 *
1001 *	This function will block the close until one of the following:
1002 *		1. TX count are 0
1003 *		2. The edgeport has stopped
1004 *		3. A timeout of 3 seconds without activity has expired
1005 *
1006 ************************************************************************/
1007static void block_until_tx_empty(struct edgeport_port *edge_port)
1008{
1009	struct device *dev = &edge_port->port->dev;
1010	DEFINE_WAIT(wait);
1011	struct TxFifo *fifo = &edge_port->txfifo;
1012	__u32 lastCount;
1013	int timeout = HZ/10;
1014	int loop = 30;
1015
1016	while (1) {
1017		/* Save Last count */
1018		lastCount = fifo->count;
1019
1020		/* Is the Edgeport Buffer empty? */
1021		if (lastCount == 0) {
1022			dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1023			return;
1024		}
1025
1026		/* Block the thread for a while */
1027		prepare_to_wait(&edge_port->wait_chase, &wait,
1028						TASK_UNINTERRUPTIBLE);
1029		schedule_timeout(timeout);
1030		finish_wait(&edge_port->wait_chase, &wait);
1031
1032		dev_dbg(dev, "%s wait\n", __func__);
1033
1034		if (lastCount == fifo->count) {
1035			/* No activity.. count down. */
1036			loop--;
1037			if (loop == 0) {
1038				dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1039				return;
1040			}
1041		} else {
1042			/* Reset timeout value back to seconds */
1043			loop = 30;
1044		}
1045	}
1046}
1047
1048
1049/*****************************************************************************
1050 * edge_close
1051 *	this function is called by the tty driver when a port is closed
1052 *****************************************************************************/
1053static void edge_close(struct usb_serial_port *port)
1054{
1055	struct edgeport_serial *edge_serial;
1056	struct edgeport_port *edge_port;
1057	int status;
1058
1059	edge_serial = usb_get_serial_data(port->serial);
1060	edge_port = usb_get_serial_port_data(port);
1061	if (edge_serial == NULL || edge_port == NULL)
1062		return;
1063
1064	/* block until tx is empty */
1065	block_until_tx_empty(edge_port);
1066
1067	edge_port->closePending = true;
1068
1069	if (!edge_serial->is_epic ||
1070	    edge_serial->epic_descriptor.Supports.IOSPChase) {
1071		/* flush and chase */
1072		edge_port->chaseResponsePending = true;
1073
1074		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1075		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1076		if (status == 0)
1077			/* block until chase finished */
1078			block_until_chase_response(edge_port);
1079		else
1080			edge_port->chaseResponsePending = false;
1081	}
1082
1083	if (!edge_serial->is_epic ||
1084	    edge_serial->epic_descriptor.Supports.IOSPClose) {
1085	       /* close the port */
1086		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1087		send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1088	}
1089
1090	/* port->close = true; */
1091	edge_port->closePending = false;
1092	edge_port->open = false;
1093	edge_port->openPending = false;
1094
1095	usb_kill_urb(edge_port->write_urb);
1096
1097	if (edge_port->write_urb) {
1098		/* if this urb had a transfer buffer already
1099				(old transfer) free it */
1100		kfree(edge_port->write_urb->transfer_buffer);
1101		usb_free_urb(edge_port->write_urb);
1102		edge_port->write_urb = NULL;
1103	}
1104	kfree(edge_port->txfifo.fifo);
1105	edge_port->txfifo.fifo = NULL;
1106}
1107
1108/*****************************************************************************
1109 * SerialWrite
1110 *	this function is called by the tty driver when data should be written
1111 *	to the port.
1112 *	If successful, we return the number of bytes written, otherwise we
1113 *	return a negative error number.
1114 *****************************************************************************/
1115static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1116					const unsigned char *data, int count)
1117{
1118	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1119	struct TxFifo *fifo;
1120	int copySize;
1121	int bytesleft;
1122	int firsthalf;
1123	int secondhalf;
1124	unsigned long flags;
1125
1126	if (edge_port == NULL)
1127		return -ENODEV;
1128
1129	/* get a pointer to the Tx fifo */
1130	fifo = &edge_port->txfifo;
1131
1132	spin_lock_irqsave(&edge_port->ep_lock, flags);
1133
1134	/* calculate number of bytes to put in fifo */
1135	copySize = min_t(unsigned int, count,
1136				(edge_port->txCredits - fifo->count));
1137
1138	dev_dbg(&port->dev, "%s of %d byte(s) Fifo room  %d -- will copy %d bytes\n",
1139		__func__, count, edge_port->txCredits - fifo->count, copySize);
1140
1141	/* catch writes of 0 bytes which the tty driver likes to give us,
1142	   and when txCredits is empty */
1143	if (copySize == 0) {
1144		dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1145		goto finish_write;
1146	}
1147
1148	/* queue the data
1149	 * since we can never overflow the buffer we do not have to check for a
1150	 * full condition
1151	 *
1152	 * the copy is done is two parts -- first fill to the end of the buffer
1153	 * then copy the reset from the start of the buffer
1154	 */
1155	bytesleft = fifo->size - fifo->head;
1156	firsthalf = min(bytesleft, copySize);
1157	dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1158		firsthalf, bytesleft);
1159
1160	/* now copy our data */
1161	memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1162	usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1163
1164	/* update the index and size */
1165	fifo->head  += firsthalf;
1166	fifo->count += firsthalf;
1167
1168	/* wrap the index */
1169	if (fifo->head == fifo->size)
1170		fifo->head = 0;
1171
1172	secondhalf = copySize-firsthalf;
1173
1174	if (secondhalf) {
1175		dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1176		memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1177		usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1178		/* update the index and size */
1179		fifo->count += secondhalf;
1180		fifo->head  += secondhalf;
1181		/* No need to check for wrap since we can not get to end of
1182		 * the fifo in this part
1183		 */
1184	}
1185
1186finish_write:
1187	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1188
1189	send_more_port_data((struct edgeport_serial *)
1190			usb_get_serial_data(port->serial), edge_port);
1191
1192	dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1193		__func__, copySize, edge_port->txCredits, fifo->count);
1194
1195	return copySize;
1196}
1197
1198
1199/************************************************************************
1200 *
1201 * send_more_port_data()
1202 *
1203 *	This routine attempts to write additional UART transmit data
1204 *	to a port over the USB bulk pipe. It is called (1) when new
1205 *	data has been written to a port's TxBuffer from higher layers
1206 *	(2) when the peripheral sends us additional TxCredits indicating
1207 *	that it can accept more	Tx data for a given port; and (3) when
1208 *	a bulk write completes successfully and we want to see if we
1209 *	can transmit more.
1210 *
1211 ************************************************************************/
1212static void send_more_port_data(struct edgeport_serial *edge_serial,
1213					struct edgeport_port *edge_port)
1214{
1215	struct TxFifo	*fifo = &edge_port->txfifo;
1216	struct device	*dev = &edge_port->port->dev;
1217	struct urb	*urb;
1218	unsigned char	*buffer;
1219	int		status;
1220	int		count;
1221	int		bytesleft;
1222	int		firsthalf;
1223	int		secondhalf;
1224	unsigned long	flags;
1225
1226	spin_lock_irqsave(&edge_port->ep_lock, flags);
1227
1228	if (edge_port->write_in_progress ||
1229	    !edge_port->open             ||
1230	    (fifo->count == 0)) {
1231		dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
1232			__func__, fifo->count, edge_port->write_in_progress);
1233		goto exit_send;
1234	}
1235
1236	/* since the amount of data in the fifo will always fit into the
1237	 * edgeport buffer we do not need to check the write length
1238	 *
1239	 * Do we have enough credits for this port to make it worthwhile
1240	 * to bother queueing a write. If it's too small, say a few bytes,
1241	 * it's better to wait for more credits so we can do a larger write.
1242	 */
1243	if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1244		dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
1245			__func__, fifo->count, edge_port->txCredits);
1246		goto exit_send;
1247	}
1248
1249	/* lock this write */
1250	edge_port->write_in_progress = true;
1251
1252	/* get a pointer to the write_urb */
1253	urb = edge_port->write_urb;
1254
1255	/* make sure transfer buffer is freed */
1256	kfree(urb->transfer_buffer);
1257	urb->transfer_buffer = NULL;
1258
1259	/* build the data header for the buffer and port that we are about
1260	   to send out */
1261	count = fifo->count;
1262	buffer = kmalloc(count+2, GFP_ATOMIC);
1263	if (!buffer) {
1264		edge_port->write_in_progress = false;
1265		goto exit_send;
1266	}
1267	buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
1268	buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
1269
1270	/* now copy our data */
1271	bytesleft =  fifo->size - fifo->tail;
1272	firsthalf = min(bytesleft, count);
1273	memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1274	fifo->tail  += firsthalf;
1275	fifo->count -= firsthalf;
1276	if (fifo->tail == fifo->size)
1277		fifo->tail = 0;
1278
1279	secondhalf = count-firsthalf;
1280	if (secondhalf) {
1281		memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1282								secondhalf);
1283		fifo->tail  += secondhalf;
1284		fifo->count -= secondhalf;
1285	}
1286
1287	if (count)
1288		usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1289
1290	/* fill up the urb with all of our data and submit it */
1291	usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1292			usb_sndbulkpipe(edge_serial->serial->dev,
1293					edge_serial->bulk_out_endpoint),
1294			buffer, count+2,
1295			edge_bulk_out_data_callback, edge_port);
1296
1297	/* decrement the number of credits we have by the number we just sent */
1298	edge_port->txCredits -= count;
1299	edge_port->port->icount.tx += count;
1300
1301	status = usb_submit_urb(urb, GFP_ATOMIC);
1302	if (status) {
1303		/* something went wrong */
1304		dev_err_console(edge_port->port,
1305			"%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1306				__func__, status);
1307		edge_port->write_in_progress = false;
1308
1309		/* revert the credits as something bad happened. */
1310		edge_port->txCredits += count;
1311		edge_port->port->icount.tx -= count;
1312	}
1313	dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1314		__func__, count, edge_port->txCredits, fifo->count);
1315
1316exit_send:
1317	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1318}
1319
1320
1321/*****************************************************************************
1322 * edge_write_room
1323 *	this function is called by the tty driver when it wants to know how
1324 *	many bytes of data we can accept for a specific port.
1325 *****************************************************************************/
1326static unsigned int edge_write_room(struct tty_struct *tty)
1327{
1328	struct usb_serial_port *port = tty->driver_data;
1329	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1330	unsigned int room;
1331	unsigned long flags;
1332
1333	/* total of both buffers is still txCredit */
1334	spin_lock_irqsave(&edge_port->ep_lock, flags);
1335	room = edge_port->txCredits - edge_port->txfifo.count;
1336	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1337
1338	dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
1339	return room;
1340}
1341
1342
1343/*****************************************************************************
1344 * edge_chars_in_buffer
1345 *	this function is called by the tty driver when it wants to know how
1346 *	many bytes of data we currently have outstanding in the port (data that
1347 *	has been written, but hasn't made it out the port yet)
1348 *****************************************************************************/
1349static unsigned int edge_chars_in_buffer(struct tty_struct *tty)
1350{
1351	struct usb_serial_port *port = tty->driver_data;
1352	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1353	unsigned int num_chars;
1354	unsigned long flags;
1355
1356	spin_lock_irqsave(&edge_port->ep_lock, flags);
1357	num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1358						edge_port->txfifo.count;
1359	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1360	if (num_chars) {
1361		dev_dbg(&port->dev, "%s - returns %u\n", __func__, num_chars);
1362	}
1363
1364	return num_chars;
1365}
1366
1367
1368/*****************************************************************************
1369 * SerialThrottle
1370 *	this function is called by the tty driver when it wants to stop the data
1371 *	being read from the port.
1372 *****************************************************************************/
1373static void edge_throttle(struct tty_struct *tty)
1374{
1375	struct usb_serial_port *port = tty->driver_data;
1376	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1377	int status;
1378
1379	if (edge_port == NULL)
1380		return;
1381
1382	if (!edge_port->open) {
1383		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1384		return;
1385	}
1386
1387	/* if we are implementing XON/XOFF, send the stop character */
1388	if (I_IXOFF(tty)) {
1389		unsigned char stop_char = STOP_CHAR(tty);
1390		status = edge_write(tty, port, &stop_char, 1);
1391		if (status <= 0)
1392			return;
1393	}
1394
1395	/* if we are implementing RTS/CTS, toggle that line */
1396	if (C_CRTSCTS(tty)) {
1397		edge_port->shadowMCR &= ~MCR_RTS;
1398		status = send_cmd_write_uart_register(edge_port, MCR,
1399							edge_port->shadowMCR);
1400		if (status != 0)
1401			return;
1402	}
1403}
1404
1405
1406/*****************************************************************************
1407 * edge_unthrottle
1408 *	this function is called by the tty driver when it wants to resume the
1409 *	data being read from the port (called after SerialThrottle is called)
1410 *****************************************************************************/
1411static void edge_unthrottle(struct tty_struct *tty)
1412{
1413	struct usb_serial_port *port = tty->driver_data;
1414	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1415	int status;
1416
1417	if (edge_port == NULL)
1418		return;
1419
1420	if (!edge_port->open) {
1421		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1422		return;
1423	}
1424
1425	/* if we are implementing XON/XOFF, send the start character */
1426	if (I_IXOFF(tty)) {
1427		unsigned char start_char = START_CHAR(tty);
1428		status = edge_write(tty, port, &start_char, 1);
1429		if (status <= 0)
1430			return;
1431	}
1432	/* if we are implementing RTS/CTS, toggle that line */
1433	if (C_CRTSCTS(tty)) {
1434		edge_port->shadowMCR |= MCR_RTS;
1435		send_cmd_write_uart_register(edge_port, MCR,
1436						edge_port->shadowMCR);
1437	}
1438}
1439
1440
1441/*****************************************************************************
1442 * SerialSetTermios
1443 *	this function is called by the tty driver when it wants to change
1444 * the termios structure
1445 *****************************************************************************/
1446static void edge_set_termios(struct tty_struct *tty,
1447			     struct usb_serial_port *port,
1448			     const struct ktermios *old_termios)
1449{
1450	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1451
1452	if (edge_port == NULL)
1453		return;
1454
1455	if (!edge_port->open) {
1456		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1457		return;
1458	}
1459
1460	/* change the port settings to the new ones specified */
1461	change_port_settings(tty, edge_port, old_termios);
1462}
1463
1464
1465/*****************************************************************************
1466 * get_lsr_info - get line status register info
1467 *
1468 * Purpose: Let user call ioctl() to get info when the UART physically
1469 * 	    is emptied.  On bus types like RS485, the transmitter must
1470 * 	    release the bus after transmitting. This must be done when
1471 * 	    the transmit shift register is empty, not be done when the
1472 * 	    transmit holding register is empty.  This functionality
1473 * 	    allows an RS485 driver to be written in user space.
1474 *****************************************************************************/
1475static int get_lsr_info(struct edgeport_port *edge_port,
1476						unsigned int __user *value)
1477{
1478	unsigned int result = 0;
1479	unsigned long flags;
1480
1481	spin_lock_irqsave(&edge_port->ep_lock, flags);
1482	if (edge_port->maxTxCredits == edge_port->txCredits &&
1483	    edge_port->txfifo.count == 0) {
1484		dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1485		result = TIOCSER_TEMT;
1486	}
1487	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1488
1489	if (copy_to_user(value, &result, sizeof(int)))
1490		return -EFAULT;
1491	return 0;
1492}
1493
1494static int edge_tiocmset(struct tty_struct *tty,
1495					unsigned int set, unsigned int clear)
1496{
1497	struct usb_serial_port *port = tty->driver_data;
1498	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1499	unsigned int mcr;
1500
1501	mcr = edge_port->shadowMCR;
1502	if (set & TIOCM_RTS)
1503		mcr |= MCR_RTS;
1504	if (set & TIOCM_DTR)
1505		mcr |= MCR_DTR;
1506	if (set & TIOCM_LOOP)
1507		mcr |= MCR_LOOPBACK;
1508
1509	if (clear & TIOCM_RTS)
1510		mcr &= ~MCR_RTS;
1511	if (clear & TIOCM_DTR)
1512		mcr &= ~MCR_DTR;
1513	if (clear & TIOCM_LOOP)
1514		mcr &= ~MCR_LOOPBACK;
1515
1516	edge_port->shadowMCR = mcr;
1517
1518	send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1519
1520	return 0;
1521}
1522
1523static int edge_tiocmget(struct tty_struct *tty)
1524{
1525	struct usb_serial_port *port = tty->driver_data;
1526	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1527	unsigned int result = 0;
1528	unsigned int msr;
1529	unsigned int mcr;
1530
1531	msr = edge_port->shadowMSR;
1532	mcr = edge_port->shadowMCR;
1533	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
1534		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
1535		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
1536		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
1537		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
1538		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */
1539
1540	return result;
1541}
1542
1543/*****************************************************************************
1544 * SerialIoctl
1545 *	this function handles any ioctl calls to the driver
1546 *****************************************************************************/
1547static int edge_ioctl(struct tty_struct *tty,
1548					unsigned int cmd, unsigned long arg)
1549{
1550	struct usb_serial_port *port = tty->driver_data;
1551	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1552
1553	switch (cmd) {
1554	case TIOCSERGETLSR:
1555		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1556		return get_lsr_info(edge_port, (unsigned int __user *) arg);
1557	}
1558	return -ENOIOCTLCMD;
1559}
1560
1561
1562/*****************************************************************************
1563 * SerialBreak
1564 *	this function sends a break to the port
1565 *****************************************************************************/
1566static int edge_break(struct tty_struct *tty, int break_state)
1567{
1568	struct usb_serial_port *port = tty->driver_data;
1569	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1570	struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1571	int status = 0;
1572
1573	if (!edge_serial->is_epic ||
1574	    edge_serial->epic_descriptor.Supports.IOSPChase) {
1575		/* flush and chase */
1576		edge_port->chaseResponsePending = true;
1577
1578		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1579		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1580		if (status == 0) {
1581			/* block until chase finished */
1582			block_until_chase_response(edge_port);
1583		} else {
1584			edge_port->chaseResponsePending = false;
1585		}
1586	}
1587
1588	if (!edge_serial->is_epic ||
1589	    edge_serial->epic_descriptor.Supports.IOSPSetClrBreak) {
1590		if (break_state == -1) {
1591			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1592			status = send_iosp_ext_cmd(edge_port,
1593						IOSP_CMD_SET_BREAK, 0);
1594		} else {
1595			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1596			status = send_iosp_ext_cmd(edge_port,
1597						IOSP_CMD_CLEAR_BREAK, 0);
1598		}
1599		if (status)
1600			dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1601				__func__);
1602	}
1603
1604	return status;
1605}
1606
1607
1608/*****************************************************************************
1609 * process_rcvd_data
1610 *	this function handles the data received on the bulk in pipe.
1611 *****************************************************************************/
1612static void process_rcvd_data(struct edgeport_serial *edge_serial,
1613				unsigned char *buffer, __u16 bufferLength)
1614{
1615	struct usb_serial *serial = edge_serial->serial;
1616	struct device *dev = &serial->dev->dev;
1617	struct usb_serial_port *port;
1618	struct edgeport_port *edge_port;
1619	__u16 lastBufferLength;
1620	__u16 rxLen;
1621
1622	lastBufferLength = bufferLength + 1;
1623
1624	while (bufferLength > 0) {
1625		/* failsafe incase we get a message that we don't understand */
1626		if (lastBufferLength == bufferLength) {
1627			dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1628			break;
1629		}
1630		lastBufferLength = bufferLength;
1631
1632		switch (edge_serial->rxState) {
1633		case EXPECT_HDR1:
1634			edge_serial->rxHeader1 = *buffer;
1635			++buffer;
1636			--bufferLength;
1637
1638			if (bufferLength == 0) {
1639				edge_serial->rxState = EXPECT_HDR2;
1640				break;
1641			}
1642			fallthrough;
1643		case EXPECT_HDR2:
1644			edge_serial->rxHeader2 = *buffer;
1645			++buffer;
1646			--bufferLength;
1647
1648			dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1649				edge_serial->rxHeader1, edge_serial->rxHeader2);
1650			/* Process depending on whether this header is
1651			 * data or status */
1652
1653			if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1654				/* Decode this status header and go to
1655				 * EXPECT_HDR1 (if we can process the status
1656				 * with only 2 bytes), or go to EXPECT_HDR3 to
1657				 * get the third byte. */
1658				edge_serial->rxPort =
1659				    IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1660				edge_serial->rxStatusCode =
1661				    IOSP_GET_STATUS_CODE(
1662						edge_serial->rxHeader1);
1663
1664				if (!IOSP_STATUS_IS_2BYTE(
1665						edge_serial->rxStatusCode)) {
1666					/* This status needs additional bytes.
1667					 * Save what we have and then wait for
1668					 * more data.
1669					 */
1670					edge_serial->rxStatusParam
1671						= edge_serial->rxHeader2;
1672					edge_serial->rxState = EXPECT_HDR3;
1673					break;
1674				}
1675				/* We have all the header bytes, process the
1676				   status now */
1677				process_rcvd_status(edge_serial,
1678						edge_serial->rxHeader2, 0);
1679				edge_serial->rxState = EXPECT_HDR1;
1680				break;
1681			}
1682
1683			edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1684			edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1,
1685									      edge_serial->rxHeader2);
1686			dev_dbg(dev, "%s - Data for Port %u Len %u\n", __func__,
1687				edge_serial->rxPort,
1688				edge_serial->rxBytesRemaining);
1689
1690			if (bufferLength == 0) {
1691				edge_serial->rxState = EXPECT_DATA;
1692				break;
1693			}
1694			fallthrough;
1695		case EXPECT_DATA: /* Expect data */
1696			if (bufferLength < edge_serial->rxBytesRemaining) {
1697				rxLen = bufferLength;
1698				/* Expect data to start next buffer */
1699				edge_serial->rxState = EXPECT_DATA;
1700			} else {
1701				/* BufLen >= RxBytesRemaining */
1702				rxLen = edge_serial->rxBytesRemaining;
1703				/* Start another header next time */
1704				edge_serial->rxState = EXPECT_HDR1;
1705			}
1706
1707			bufferLength -= rxLen;
1708			edge_serial->rxBytesRemaining -= rxLen;
1709
1710			/* spit this data back into the tty driver if this
1711			   port is open */
1712			if (rxLen && edge_serial->rxPort < serial->num_ports) {
1713				port = serial->port[edge_serial->rxPort];
1714				edge_port = usb_get_serial_port_data(port);
1715				if (edge_port && edge_port->open) {
1716					dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1717						__func__, rxLen,
1718						edge_serial->rxPort);
1719					edge_tty_recv(edge_port->port, buffer,
1720							rxLen);
1721					edge_port->port->icount.rx += rxLen;
1722				}
1723			}
1724			buffer += rxLen;
1725			break;
1726
1727		case EXPECT_HDR3:	/* Expect 3rd byte of status header */
1728			edge_serial->rxHeader3 = *buffer;
1729			++buffer;
1730			--bufferLength;
1731
1732			/* We have all the header bytes, process the
1733			   status now */
1734			process_rcvd_status(edge_serial,
1735				edge_serial->rxStatusParam,
1736				edge_serial->rxHeader3);
1737			edge_serial->rxState = EXPECT_HDR1;
1738			break;
1739		}
1740	}
1741}
1742
1743
1744/*****************************************************************************
1745 * process_rcvd_status
1746 *	this function handles the any status messages received on the
1747 *	bulk in pipe.
1748 *****************************************************************************/
1749static void process_rcvd_status(struct edgeport_serial *edge_serial,
1750						__u8 byte2, __u8 byte3)
1751{
1752	struct usb_serial_port *port;
1753	struct edgeport_port *edge_port;
1754	struct tty_struct *tty;
1755	struct device *dev;
1756	__u8 code = edge_serial->rxStatusCode;
1757
1758	/* switch the port pointer to the one being currently talked about */
1759	if (edge_serial->rxPort >= edge_serial->serial->num_ports)
1760		return;
1761	port = edge_serial->serial->port[edge_serial->rxPort];
1762	edge_port = usb_get_serial_port_data(port);
1763	if (edge_port == NULL) {
1764		dev_err(&edge_serial->serial->dev->dev,
1765			"%s - edge_port == NULL for port %d\n",
1766					__func__, edge_serial->rxPort);
1767		return;
1768	}
1769	dev = &port->dev;
1770
1771	if (code == IOSP_EXT_STATUS) {
1772		switch (byte2) {
1773		case IOSP_EXT_STATUS_CHASE_RSP:
1774			/* we want to do EXT status regardless of port
1775			 * open/closed */
1776			dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1777				__func__, edge_serial->rxPort, byte3);
1778			/* Currently, the only EXT_STATUS is Chase, so process
1779			 * here instead of one more call to one more subroutine
1780			 * If/when more EXT_STATUS, there'll be more work to do
1781			 * Also, we currently clear flag and close the port
1782			 * regardless of content of above's Byte3.
1783			 * We could choose to do something else when Byte3 says
1784			 * Timeout on Chase from Edgeport, like wait longer in
1785			 * block_until_chase_response, but for now we don't.
1786			 */
1787			edge_port->chaseResponsePending = false;
1788			wake_up(&edge_port->wait_chase);
1789			return;
1790
1791		case IOSP_EXT_STATUS_RX_CHECK_RSP:
1792			dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1793				__func__, edge_serial->rxPort, byte3);
1794			/* Port->RxCheckRsp = true; */
1795			return;
1796		}
1797	}
1798
1799	if (code == IOSP_STATUS_OPEN_RSP) {
1800		edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1801		edge_port->maxTxCredits = edge_port->txCredits;
1802		dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1803			__func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1804		handle_new_msr(edge_port, byte2);
1805
1806		/* send the current line settings to the port so we are
1807		   in sync with any further termios calls */
1808		tty = tty_port_tty_get(&edge_port->port->port);
1809		if (tty) {
1810			change_port_settings(tty,
1811				edge_port, &tty->termios);
1812			tty_kref_put(tty);
1813		}
1814
1815		/* we have completed the open */
1816		edge_port->openPending = false;
1817		edge_port->open = true;
1818		wake_up(&edge_port->wait_open);
1819		return;
1820	}
1821
1822	/* If port is closed, silently discard all rcvd status. We can
1823	 * have cases where buffered status is received AFTER the close
1824	 * port command is sent to the Edgeport.
1825	 */
1826	if (!edge_port->open || edge_port->closePending)
1827		return;
1828
1829	switch (code) {
1830	/* Not currently sent by Edgeport */
1831	case IOSP_STATUS_LSR:
1832		dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1833			__func__, edge_serial->rxPort, byte2);
1834		handle_new_lsr(edge_port, false, byte2, 0);
1835		break;
1836
1837	case IOSP_STATUS_LSR_DATA:
1838		dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1839			__func__, edge_serial->rxPort, byte2, byte3);
1840		/* byte2 is LSR Register */
1841		/* byte3 is broken data byte */
1842		handle_new_lsr(edge_port, true, byte2, byte3);
1843		break;
1844	/*
1845	 *	case IOSP_EXT_4_STATUS:
1846	 *		dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1847	 *			__func__, edge_serial->rxPort, byte2, byte3);
1848	 *		break;
1849	 */
1850	case IOSP_STATUS_MSR:
1851		dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1852			__func__, edge_serial->rxPort, byte2);
1853		/*
1854		 * Process this new modem status and generate appropriate
1855		 * events, etc, based on the new status. This routine
1856		 * also saves the MSR in Port->ShadowMsr.
1857		 */
1858		handle_new_msr(edge_port, byte2);
1859		break;
1860
1861	default:
1862		dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
1863		break;
1864	}
1865}
1866
1867
1868/*****************************************************************************
1869 * edge_tty_recv
1870 *	this function passes data on to the tty flip buffer
1871 *****************************************************************************/
1872static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1873		int length)
1874{
1875	int cnt;
1876
1877	cnt = tty_insert_flip_string(&port->port, data, length);
1878	if (cnt < length) {
1879		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1880				__func__, length - cnt);
1881	}
1882	data += cnt;
1883	length -= cnt;
1884
1885	tty_flip_buffer_push(&port->port);
1886}
1887
1888
1889/*****************************************************************************
1890 * handle_new_msr
1891 *	this function handles any change to the msr register for a port.
1892 *****************************************************************************/
1893static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1894{
1895	struct  async_icount *icount;
1896
1897	if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1898			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1899		icount = &edge_port->port->icount;
1900
1901		/* update input line counters */
1902		if (newMsr & EDGEPORT_MSR_DELTA_CTS)
1903			icount->cts++;
1904		if (newMsr & EDGEPORT_MSR_DELTA_DSR)
1905			icount->dsr++;
1906		if (newMsr & EDGEPORT_MSR_DELTA_CD)
1907			icount->dcd++;
1908		if (newMsr & EDGEPORT_MSR_DELTA_RI)
1909			icount->rng++;
1910		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
1911	}
1912
1913	/* Save the new modem status */
1914	edge_port->shadowMSR = newMsr & 0xf0;
1915}
1916
1917
1918/*****************************************************************************
1919 * handle_new_lsr
1920 *	this function handles any change to the lsr register for a port.
1921 *****************************************************************************/
1922static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
1923							__u8 lsr, __u8 data)
1924{
1925	__u8 newLsr = (__u8) (lsr & (__u8)
1926		(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1927	struct async_icount *icount;
1928
1929	edge_port->shadowLSR = lsr;
1930
1931	if (newLsr & LSR_BREAK) {
1932		/*
1933		 * Parity and Framing errors only count if they
1934		 * occur exclusive of a break being
1935		 * received.
1936		 */
1937		newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1938	}
1939
1940	/* Place LSR data byte into Rx buffer */
1941	if (lsrData)
1942		edge_tty_recv(edge_port->port, &data, 1);
1943
1944	/* update input line counters */
1945	icount = &edge_port->port->icount;
1946	if (newLsr & LSR_BREAK)
1947		icount->brk++;
1948	if (newLsr & LSR_OVER_ERR)
1949		icount->overrun++;
1950	if (newLsr & LSR_PAR_ERR)
1951		icount->parity++;
1952	if (newLsr & LSR_FRM_ERR)
1953		icount->frame++;
1954}
1955
1956
1957/****************************************************************************
1958 * sram_write
1959 *	writes a number of bytes to the Edgeport device's sram starting at the
1960 *	given address.
1961 *	If successful returns the number of bytes written, otherwise it returns
1962 *	a negative error number of the problem.
1963 ****************************************************************************/
1964static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
1965					__u16 length, const __u8 *data)
1966{
1967	int result;
1968	__u16 current_length;
1969	unsigned char *transfer_buffer;
1970
1971	dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
1972
1973	transfer_buffer =  kmalloc(64, GFP_KERNEL);
1974	if (!transfer_buffer)
1975		return -ENOMEM;
1976
1977	/* need to split these writes up into 64 byte chunks */
1978	result = 0;
1979	while (length > 0) {
1980		if (length > 64)
1981			current_length = 64;
1982		else
1983			current_length = length;
1984
1985/*		dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
1986		memcpy(transfer_buffer, data, current_length);
1987		result = usb_control_msg(serial->dev,
1988					usb_sndctrlpipe(serial->dev, 0),
1989					USB_REQUEST_ION_WRITE_RAM,
1990					0x40, addr, extAddr, transfer_buffer,
1991					current_length, 300);
1992		if (result < 0)
1993			break;
1994		length -= current_length;
1995		addr += current_length;
1996		data += current_length;
1997	}
1998
1999	kfree(transfer_buffer);
2000	return result;
2001}
2002
2003
2004/****************************************************************************
2005 * rom_write
2006 *	writes a number of bytes to the Edgeport device's ROM starting at the
2007 *	given address.
2008 *	If successful returns the number of bytes written, otherwise it returns
2009 *	a negative error number of the problem.
2010 ****************************************************************************/
2011static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2012					__u16 length, const __u8 *data)
2013{
2014	int result;
2015	__u16 current_length;
2016	unsigned char *transfer_buffer;
2017
2018	transfer_buffer =  kmalloc(64, GFP_KERNEL);
2019	if (!transfer_buffer)
2020		return -ENOMEM;
2021
2022	/* need to split these writes up into 64 byte chunks */
2023	result = 0;
2024	while (length > 0) {
2025		if (length > 64)
2026			current_length = 64;
2027		else
2028			current_length = length;
2029		memcpy(transfer_buffer, data, current_length);
2030		result = usb_control_msg(serial->dev,
2031					usb_sndctrlpipe(serial->dev, 0),
2032					USB_REQUEST_ION_WRITE_ROM, 0x40,
2033					addr, extAddr,
2034					transfer_buffer, current_length, 300);
2035		if (result < 0)
2036			break;
2037		length -= current_length;
2038		addr += current_length;
2039		data += current_length;
2040	}
2041
2042	kfree(transfer_buffer);
2043	return result;
2044}
2045
2046
2047/****************************************************************************
2048 * rom_read
2049 *	reads a number of bytes from the Edgeport device starting at the given
2050 *	address.
2051 *	Returns zero on success or a negative error number.
2052 ****************************************************************************/
2053static int rom_read(struct usb_serial *serial, __u16 extAddr,
2054					__u16 addr, __u16 length, __u8 *data)
2055{
2056	int result;
2057	__u16 current_length;
2058	unsigned char *transfer_buffer;
2059
2060	transfer_buffer =  kmalloc(64, GFP_KERNEL);
2061	if (!transfer_buffer)
2062		return -ENOMEM;
2063
2064	/* need to split these reads up into 64 byte chunks */
2065	result = 0;
2066	while (length > 0) {
2067		if (length > 64)
2068			current_length = 64;
2069		else
2070			current_length = length;
2071		result = usb_control_msg(serial->dev,
2072					usb_rcvctrlpipe(serial->dev, 0),
2073					USB_REQUEST_ION_READ_ROM,
2074					0xC0, addr, extAddr, transfer_buffer,
2075					current_length, 300);
2076		if (result < current_length) {
2077			if (result >= 0)
2078				result = -EIO;
2079			break;
2080		}
2081		memcpy(data, transfer_buffer, current_length);
2082		length -= current_length;
2083		addr += current_length;
2084		data += current_length;
2085
2086		result = 0;
2087	}
2088
2089	kfree(transfer_buffer);
2090	return result;
2091}
2092
2093
2094/****************************************************************************
2095 * send_iosp_ext_cmd
2096 *	Is used to send a IOSP message to the Edgeport device
2097 ****************************************************************************/
2098static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2099						__u8 command, __u8 param)
2100{
2101	unsigned char   *buffer;
2102	unsigned char   *currentCommand;
2103	int             length = 0;
2104	int             status = 0;
2105
2106	buffer = kmalloc(10, GFP_ATOMIC);
2107	if (!buffer)
2108		return -ENOMEM;
2109
2110	currentCommand = buffer;
2111
2112	MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
2113			 command, param);
2114
2115	status = write_cmd_usb(edge_port, buffer, length);
2116	if (status) {
2117		/* something bad happened, let's free up the memory */
2118		kfree(buffer);
2119	}
2120
2121	return status;
2122}
2123
2124
2125/*****************************************************************************
2126 * write_cmd_usb
2127 *	this function writes the given buffer out to the bulk write endpoint.
2128 *****************************************************************************/
2129static int write_cmd_usb(struct edgeport_port *edge_port,
2130					unsigned char *buffer, int length)
2131{
2132	struct edgeport_serial *edge_serial =
2133				usb_get_serial_data(edge_port->port->serial);
2134	struct device *dev = &edge_port->port->dev;
2135	int status = 0;
2136	struct urb *urb;
2137
2138	usb_serial_debug_data(dev, __func__, length, buffer);
2139
2140	/* Allocate our next urb */
2141	urb = usb_alloc_urb(0, GFP_ATOMIC);
2142	if (!urb)
2143		return -ENOMEM;
2144
2145	atomic_inc(&CmdUrbs);
2146	dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2147		__func__, urb, atomic_read(&CmdUrbs));
2148
2149	usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2150			usb_sndbulkpipe(edge_serial->serial->dev,
2151					edge_serial->bulk_out_endpoint),
2152			buffer, length, edge_bulk_out_cmd_callback, edge_port);
2153
2154	edge_port->commandPending = true;
2155	status = usb_submit_urb(urb, GFP_ATOMIC);
2156
2157	if (status) {
2158		/* something went wrong */
2159		dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2160			__func__, status);
2161		usb_free_urb(urb);
2162		atomic_dec(&CmdUrbs);
2163		return status;
2164	}
2165
2166#if 0
2167	wait_event(&edge_port->wait_command, !edge_port->commandPending);
2168
2169	if (edge_port->commandPending) {
2170		/* command timed out */
2171		dev_dbg(dev, "%s - command timed out\n", __func__);
2172		status = -EINVAL;
2173	}
2174#endif
2175	return status;
2176}
2177
2178
2179/*****************************************************************************
2180 * send_cmd_write_baud_rate
2181 *	this function sends the proper command to change the baud rate of the
2182 *	specified port.
2183 *****************************************************************************/
2184static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2185								int baudRate)
2186{
2187	struct edgeport_serial *edge_serial =
2188				usb_get_serial_data(edge_port->port->serial);
2189	struct device *dev = &edge_port->port->dev;
2190	unsigned char *cmdBuffer;
2191	unsigned char *currCmd;
2192	int cmdLen = 0;
2193	int divisor;
2194	int status;
2195	u32 number = edge_port->port->port_number;
2196
2197	if (edge_serial->is_epic &&
2198	    !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2199		dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
2200			baudRate);
2201		return 0;
2202	}
2203
2204	dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
2205
2206	status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2207	if (status) {
2208		dev_err(dev, "%s - bad baud rate\n", __func__);
2209		return status;
2210	}
2211
2212	/* Alloc memory for the string of commands. */
2213	cmdBuffer =  kmalloc(0x100, GFP_ATOMIC);
2214	if (!cmdBuffer)
2215		return -ENOMEM;
2216
2217	currCmd = cmdBuffer;
2218
2219	/* Enable access to divisor latch */
2220	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2221
2222	/* Write the divisor itself */
2223	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2224	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2225
2226	/* Restore original value to disable access to divisor latch */
2227	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2228						edge_port->shadowLCR);
2229
2230	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2231	if (status) {
2232		/* something bad happened, let's free up the memory */
2233		kfree(cmdBuffer);
2234	}
2235
2236	return status;
2237}
2238
2239
2240/*****************************************************************************
2241 * calc_baud_rate_divisor
2242 *	this function calculates the proper baud rate divisor for the specified
2243 *	baud rate.
2244 *****************************************************************************/
2245static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2246{
2247	int i;
2248	__u16 custom;
2249
2250	for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2251		if (divisor_table[i].BaudRate == baudrate) {
2252			*divisor = divisor_table[i].Divisor;
2253			return 0;
2254		}
2255	}
2256
2257	/* We have tried all of the standard baud rates
2258	 * lets try to calculate the divisor for this baud rate
2259	 * Make sure the baud rate is reasonable */
2260	if (baudrate > 50 && baudrate < 230400) {
2261		/* get divisor */
2262		custom = (__u16)((230400L + baudrate/2) / baudrate);
2263
2264		*divisor = custom;
2265
2266		dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2267		return 0;
2268	}
2269
2270	return -1;
2271}
2272
2273
2274/*****************************************************************************
2275 * send_cmd_write_uart_register
2276 *  this function builds up a uart register message and sends to the device.
2277 *****************************************************************************/
2278static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2279						__u8 regNum, __u8 regValue)
2280{
2281	struct edgeport_serial *edge_serial =
2282				usb_get_serial_data(edge_port->port->serial);
2283	struct device *dev = &edge_port->port->dev;
2284	unsigned char *cmdBuffer;
2285	unsigned char *currCmd;
2286	unsigned long cmdLen = 0;
2287	int status;
2288
2289	dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2290		(regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2291
2292	if (edge_serial->is_epic &&
2293	    !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2294	    regNum == MCR) {
2295		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2296		return 0;
2297	}
2298
2299	if (edge_serial->is_epic &&
2300	    !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2301	    regNum == LCR) {
2302		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2303		return 0;
2304	}
2305
2306	/* Alloc memory for the string of commands. */
2307	cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2308	if (cmdBuffer == NULL)
2309		return -ENOMEM;
2310
2311	currCmd = cmdBuffer;
2312
2313	/* Build a cmd in the buffer to write the given register */
2314	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
2315			   regNum, regValue);
2316
2317	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2318	if (status) {
2319		/* something bad happened, let's free up the memory */
2320		kfree(cmdBuffer);
2321	}
2322
2323	return status;
2324}
2325
2326
2327/*****************************************************************************
2328 * change_port_settings
2329 *	This routine is called to set the UART on the device to match the
2330 *	specified new settings.
2331 *****************************************************************************/
2332
2333static void change_port_settings(struct tty_struct *tty,
2334	struct edgeport_port *edge_port, const struct ktermios *old_termios)
2335{
2336	struct device *dev = &edge_port->port->dev;
2337	struct edgeport_serial *edge_serial =
2338			usb_get_serial_data(edge_port->port->serial);
2339	int baud;
2340	unsigned cflag;
2341	__u8 mask = 0xff;
2342	__u8 lData;
2343	__u8 lParity;
2344	__u8 lStop;
2345	__u8 rxFlow;
2346	__u8 txFlow;
2347	int status;
2348
2349	if (!edge_port->open &&
2350	    !edge_port->openPending) {
2351		dev_dbg(dev, "%s - port not opened\n", __func__);
2352		return;
2353	}
2354
2355	cflag = tty->termios.c_cflag;
2356
2357	switch (cflag & CSIZE) {
2358	case CS5:
2359		lData = LCR_BITS_5; mask = 0x1f;
2360		dev_dbg(dev, "%s - data bits = 5\n", __func__);
2361		break;
2362	case CS6:
2363		lData = LCR_BITS_6; mask = 0x3f;
2364		dev_dbg(dev, "%s - data bits = 6\n", __func__);
2365		break;
2366	case CS7:
2367		lData = LCR_BITS_7; mask = 0x7f;
2368		dev_dbg(dev, "%s - data bits = 7\n", __func__);
2369		break;
2370	default:
2371	case CS8:
2372		lData = LCR_BITS_8;
2373		dev_dbg(dev, "%s - data bits = 8\n", __func__);
2374		break;
2375	}
2376
2377	lParity = LCR_PAR_NONE;
2378	if (cflag & PARENB) {
2379		if (cflag & CMSPAR) {
2380			if (cflag & PARODD) {
2381				lParity = LCR_PAR_MARK;
2382				dev_dbg(dev, "%s - parity = mark\n", __func__);
2383			} else {
2384				lParity = LCR_PAR_SPACE;
2385				dev_dbg(dev, "%s - parity = space\n", __func__);
2386			}
2387		} else if (cflag & PARODD) {
2388			lParity = LCR_PAR_ODD;
2389			dev_dbg(dev, "%s - parity = odd\n", __func__);
2390		} else {
2391			lParity = LCR_PAR_EVEN;
2392			dev_dbg(dev, "%s - parity = even\n", __func__);
2393		}
2394	} else {
2395		dev_dbg(dev, "%s - parity = none\n", __func__);
2396	}
2397
2398	if (cflag & CSTOPB) {
2399		lStop = LCR_STOP_2;
2400		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2401	} else {
2402		lStop = LCR_STOP_1;
2403		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2404	}
2405
2406	/* figure out the flow control settings */
2407	rxFlow = txFlow = 0x00;
2408	if (cflag & CRTSCTS) {
2409		rxFlow |= IOSP_RX_FLOW_RTS;
2410		txFlow |= IOSP_TX_FLOW_CTS;
2411		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2412	} else {
2413		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2414	}
2415
2416	/* if we are implementing XON/XOFF, set the start and stop character
2417	   in the device */
2418	if (I_IXOFF(tty) || I_IXON(tty)) {
2419		unsigned char stop_char  = STOP_CHAR(tty);
2420		unsigned char start_char = START_CHAR(tty);
2421
2422		if (!edge_serial->is_epic ||
2423		    edge_serial->epic_descriptor.Supports.IOSPSetXChar) {
2424			send_iosp_ext_cmd(edge_port,
2425					IOSP_CMD_SET_XON_CHAR, start_char);
2426			send_iosp_ext_cmd(edge_port,
2427					IOSP_CMD_SET_XOFF_CHAR, stop_char);
2428		}
2429
2430		/* if we are implementing INBOUND XON/XOFF */
2431		if (I_IXOFF(tty)) {
2432			rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2433			dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2434				__func__, start_char, stop_char);
2435		} else {
2436			dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2437		}
2438
2439		/* if we are implementing OUTBOUND XON/XOFF */
2440		if (I_IXON(tty)) {
2441			txFlow |= IOSP_TX_FLOW_XON_XOFF;
2442			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2443				__func__, start_char, stop_char);
2444		} else {
2445			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2446		}
2447	}
2448
2449	/* Set flow control to the configured value */
2450	if (!edge_serial->is_epic ||
2451	    edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)
2452		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2453	if (!edge_serial->is_epic ||
2454	    edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)
2455		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2456
2457
2458	edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2459	edge_port->shadowLCR |= (lData | lParity | lStop);
2460
2461	edge_port->validDataMask = mask;
2462
2463	/* Send the updated LCR value to the EdgePort */
2464	status = send_cmd_write_uart_register(edge_port, LCR,
2465							edge_port->shadowLCR);
2466	if (status != 0)
2467		return;
2468
2469	/* set up the MCR register and send it to the EdgePort */
2470	edge_port->shadowMCR = MCR_MASTER_IE;
2471	if (cflag & CBAUD)
2472		edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2473
2474	status = send_cmd_write_uart_register(edge_port, MCR,
2475						edge_port->shadowMCR);
2476	if (status != 0)
2477		return;
2478
2479	/* Determine divisor based on baud rate */
2480	baud = tty_get_baud_rate(tty);
2481	if (!baud) {
2482		/* pick a default, any default... */
2483		baud = 9600;
2484	}
2485
2486	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2487	status = send_cmd_write_baud_rate(edge_port, baud);
2488	if (status == -1) {
2489		/* Speed change was not possible - put back the old speed */
2490		baud = tty_termios_baud_rate(old_termios);
2491		tty_encode_baud_rate(tty, baud, baud);
2492	}
2493}
2494
2495
2496/****************************************************************************
2497 * unicode_to_ascii
2498 *	Turns a string from Unicode into ASCII.
2499 *	Doesn't do a good job with any characters that are outside the normal
2500 *	ASCII range, but it's only for debugging...
2501 *	NOTE: expects the unicode in LE format
2502 ****************************************************************************/
2503static void unicode_to_ascii(char *string, int buflen,
2504					__le16 *unicode, int unicode_size)
2505{
2506	int i;
2507
2508	if (buflen <= 0)	/* never happens, but... */
2509		return;
2510	--buflen;		/* space for nul */
2511
2512	for (i = 0; i < unicode_size; i++) {
2513		if (i >= buflen)
2514			break;
2515		string[i] = (char)(le16_to_cpu(unicode[i]));
2516	}
2517	string[i] = 0x00;
2518}
2519
2520
2521/****************************************************************************
2522 * get_manufacturing_desc
2523 *	reads in the manufacturing descriptor and stores it into the serial
2524 *	structure.
2525 ****************************************************************************/
2526static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2527{
2528	struct device *dev = &edge_serial->serial->dev->dev;
2529	int response;
2530
2531	dev_dbg(dev, "getting manufacturer descriptor\n");
2532
2533	response = rom_read(edge_serial->serial,
2534				(EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2535				(__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2536				EDGE_MANUF_DESC_LEN,
2537				(__u8 *)(&edge_serial->manuf_descriptor));
2538
2539	if (response < 0) {
2540		dev_err(dev, "error in getting manufacturer descriptor: %d\n",
2541				response);
2542	} else {
2543		char string[30];
2544		dev_dbg(dev, "**Manufacturer Descriptor\n");
2545		dev_dbg(dev, "  RomSize:        %dK\n",
2546			edge_serial->manuf_descriptor.RomSize);
2547		dev_dbg(dev, "  RamSize:        %dK\n",
2548			edge_serial->manuf_descriptor.RamSize);
2549		dev_dbg(dev, "  CpuRev:         %d\n",
2550			edge_serial->manuf_descriptor.CpuRev);
2551		dev_dbg(dev, "  BoardRev:       %d\n",
2552			edge_serial->manuf_descriptor.BoardRev);
2553		dev_dbg(dev, "  NumPorts:       %d\n",
2554			edge_serial->manuf_descriptor.NumPorts);
2555		dev_dbg(dev, "  DescDate:       %d/%d/%d\n",
2556			edge_serial->manuf_descriptor.DescDate[0],
2557			edge_serial->manuf_descriptor.DescDate[1],
2558			edge_serial->manuf_descriptor.DescDate[2]+1900);
2559		unicode_to_ascii(string, sizeof(string),
2560			edge_serial->manuf_descriptor.SerialNumber,
2561			edge_serial->manuf_descriptor.SerNumLength/2);
2562		dev_dbg(dev, "  SerialNumber: %s\n", string);
2563		unicode_to_ascii(string, sizeof(string),
2564			edge_serial->manuf_descriptor.AssemblyNumber,
2565			edge_serial->manuf_descriptor.AssemblyNumLength/2);
2566		dev_dbg(dev, "  AssemblyNumber: %s\n", string);
2567		unicode_to_ascii(string, sizeof(string),
2568		    edge_serial->manuf_descriptor.OemAssyNumber,
2569		    edge_serial->manuf_descriptor.OemAssyNumLength/2);
2570		dev_dbg(dev, "  OemAssyNumber:  %s\n", string);
2571		dev_dbg(dev, "  UartType:       %d\n",
2572			edge_serial->manuf_descriptor.UartType);
2573		dev_dbg(dev, "  IonPid:         %d\n",
2574			edge_serial->manuf_descriptor.IonPid);
2575		dev_dbg(dev, "  IonConfig:      %d\n",
2576			edge_serial->manuf_descriptor.IonConfig);
2577	}
2578}
2579
2580
2581/****************************************************************************
2582 * get_boot_desc
2583 *	reads in the bootloader descriptor and stores it into the serial
2584 *	structure.
2585 ****************************************************************************/
2586static void get_boot_desc(struct edgeport_serial *edge_serial)
2587{
2588	struct device *dev = &edge_serial->serial->dev->dev;
2589	int response;
2590
2591	dev_dbg(dev, "getting boot descriptor\n");
2592
2593	response = rom_read(edge_serial->serial,
2594				(EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2595				(__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2596				EDGE_BOOT_DESC_LEN,
2597				(__u8 *)(&edge_serial->boot_descriptor));
2598
2599	if (response < 0) {
2600		dev_err(dev, "error in getting boot descriptor: %d\n",
2601				response);
2602	} else {
2603		dev_dbg(dev, "**Boot Descriptor:\n");
2604		dev_dbg(dev, "  BootCodeLength: %d\n",
2605			le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2606		dev_dbg(dev, "  MajorVersion:   %d\n",
2607			edge_serial->boot_descriptor.MajorVersion);
2608		dev_dbg(dev, "  MinorVersion:   %d\n",
2609			edge_serial->boot_descriptor.MinorVersion);
2610		dev_dbg(dev, "  BuildNumber:    %d\n",
2611			le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2612		dev_dbg(dev, "  Capabilities:   0x%x\n",
2613		      le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2614		dev_dbg(dev, "  UConfig0:       %d\n",
2615			edge_serial->boot_descriptor.UConfig0);
2616		dev_dbg(dev, "  UConfig1:       %d\n",
2617			edge_serial->boot_descriptor.UConfig1);
2618	}
2619}
2620
2621
2622/****************************************************************************
2623 * load_application_firmware
2624 *	This is called to load the application firmware to the device
2625 ****************************************************************************/
2626static void load_application_firmware(struct edgeport_serial *edge_serial)
2627{
2628	struct device *dev = &edge_serial->serial->dev->dev;
2629	const struct ihex_binrec *rec;
2630	const struct firmware *fw;
2631	const char *fw_name;
2632	const char *fw_info;
2633	int response;
2634	__u32 Operaddr;
2635	__u16 build;
2636
2637	switch (edge_serial->product_info.iDownloadFile) {
2638		case EDGE_DOWNLOAD_FILE_I930:
2639			fw_info = "downloading firmware version (930)";
2640			fw_name	= "edgeport/down.fw";
2641			break;
2642
2643		case EDGE_DOWNLOAD_FILE_80251:
2644			fw_info = "downloading firmware version (80251)";
2645			fw_name	= "edgeport/down2.fw";
2646			break;
2647
2648		case EDGE_DOWNLOAD_FILE_NONE:
2649			dev_dbg(dev, "No download file specified, skipping download\n");
2650			return;
2651
2652		default:
2653			return;
2654	}
2655
2656	response = request_ihex_firmware(&fw, fw_name,
2657				    &edge_serial->serial->dev->dev);
2658	if (response) {
2659		dev_err(dev, "Failed to load image \"%s\" err %d\n",
2660		       fw_name, response);
2661		return;
2662	}
2663
2664	rec = (const struct ihex_binrec *)fw->data;
2665	build = (rec->data[2] << 8) | rec->data[3];
2666
2667	dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2668
2669	edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2670	edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2671	edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2672
2673	for (rec = ihex_next_binrec(rec); rec;
2674	     rec = ihex_next_binrec(rec)) {
2675		Operaddr = be32_to_cpu(rec->addr);
2676		response = sram_write(edge_serial->serial,
2677				     Operaddr >> 16,
2678				     Operaddr & 0xFFFF,
2679				     be16_to_cpu(rec->len),
2680				     &rec->data[0]);
2681		if (response < 0) {
2682			dev_err(&edge_serial->serial->dev->dev,
2683				"sram_write failed (%x, %x, %d)\n",
2684				Operaddr >> 16, Operaddr & 0xFFFF,
2685				be16_to_cpu(rec->len));
2686			break;
2687		}
2688	}
2689
2690	dev_dbg(dev, "sending exec_dl_code\n");
2691	response = usb_control_msg (edge_serial->serial->dev,
2692				    usb_sndctrlpipe(edge_serial->serial->dev, 0),
2693				    USB_REQUEST_ION_EXEC_DL_CODE,
2694				    0x40, 0x4000, 0x0001, NULL, 0, 3000);
2695
2696	release_firmware(fw);
2697}
2698
2699
2700/****************************************************************************
2701 * edge_startup
2702 ****************************************************************************/
2703static int edge_startup(struct usb_serial *serial)
2704{
2705	struct edgeport_serial *edge_serial;
2706	struct usb_device *dev;
2707	struct device *ddev = &serial->dev->dev;
2708	int i;
2709	int response;
2710	bool interrupt_in_found;
2711	bool bulk_in_found;
2712	bool bulk_out_found;
2713	static const __u32 descriptor[3] = {	EDGE_COMPATIBILITY_MASK0,
2714						EDGE_COMPATIBILITY_MASK1,
2715						EDGE_COMPATIBILITY_MASK2 };
2716
2717	dev = serial->dev;
2718
2719	/* create our private serial structure */
2720	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2721	if (!edge_serial)
2722		return -ENOMEM;
2723
2724	spin_lock_init(&edge_serial->es_lock);
2725	edge_serial->serial = serial;
2726	usb_set_serial_data(serial, edge_serial);
2727
2728	/* get the name for the device from the device */
2729	i = usb_string(dev, dev->descriptor.iManufacturer,
2730	    &edge_serial->name[0], MAX_NAME_LEN+1);
2731	if (i < 0)
2732		i = 0;
2733	edge_serial->name[i++] = ' ';
2734	usb_string(dev, dev->descriptor.iProduct,
2735	    &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2736
2737	dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2738
2739	/* Read the epic descriptor */
2740	if (get_epic_descriptor(edge_serial) < 0) {
2741		/* memcpy descriptor to Supports structures */
2742		memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2743		       sizeof(struct edge_compatibility_bits));
2744
2745		/* get the manufacturing descriptor for this device */
2746		get_manufacturing_desc(edge_serial);
2747
2748		/* get the boot descriptor */
2749		get_boot_desc(edge_serial);
2750
2751		get_product_info(edge_serial);
2752	}
2753
2754	/* set the number of ports from the manufacturing description */
2755	/* serial->num_ports = serial->product_info.NumPorts; */
2756	if ((!edge_serial->is_epic) &&
2757	    (edge_serial->product_info.NumPorts != serial->num_ports)) {
2758		dev_warn(ddev,
2759			"Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2760			 edge_serial->product_info.NumPorts,
2761			 serial->num_ports);
2762	}
2763
2764	dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2765
2766	/* If not an EPiC device */
2767	if (!edge_serial->is_epic) {
2768		/* now load the application firmware into this device */
2769		load_application_firmware(edge_serial);
2770
2771		dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2772
2773		/* Check current Edgeport EEPROM and update if necessary */
2774		update_edgeport_E2PROM(edge_serial);
2775
2776		dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2777
2778		/* set the configuration to use #1 */
2779/*		dev_dbg(ddev, "set_configuration 1\n"); */
2780/*		usb_set_configuration (dev, 1); */
2781	}
2782	dev_dbg(ddev, "  FirmwareMajorVersion  %d.%d.%d\n",
2783	    edge_serial->product_info.FirmwareMajorVersion,
2784	    edge_serial->product_info.FirmwareMinorVersion,
2785	    le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2786
2787	/* we set up the pointers to the endpoints in the edge_open function,
2788	 * as the structures aren't created yet. */
2789
2790	response = 0;
2791
2792	if (edge_serial->is_epic) {
2793		struct usb_host_interface *alt;
2794
2795		alt = serial->interface->cur_altsetting;
2796
2797		/* EPIC thing, set up our interrupt polling now and our read
2798		 * urb, so that the device knows it really is connected. */
2799		interrupt_in_found = bulk_in_found = bulk_out_found = false;
2800		for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
2801			struct usb_endpoint_descriptor *endpoint;
2802			int buffer_size;
2803
2804			endpoint = &alt->endpoint[i].desc;
2805			buffer_size = usb_endpoint_maxp(endpoint);
2806			if (!interrupt_in_found &&
2807			    (usb_endpoint_is_int_in(endpoint))) {
2808				/* we found a interrupt in endpoint */
2809				dev_dbg(ddev, "found interrupt in\n");
2810
2811				/* not set up yet, so do it now */
2812				edge_serial->interrupt_read_urb =
2813						usb_alloc_urb(0, GFP_KERNEL);
2814				if (!edge_serial->interrupt_read_urb) {
2815					response = -ENOMEM;
2816					break;
2817				}
2818
2819				edge_serial->interrupt_in_buffer =
2820					kmalloc(buffer_size, GFP_KERNEL);
2821				if (!edge_serial->interrupt_in_buffer) {
2822					response = -ENOMEM;
2823					break;
2824				}
2825				edge_serial->interrupt_in_endpoint =
2826						endpoint->bEndpointAddress;
2827
2828				/* set up our interrupt urb */
2829				usb_fill_int_urb(
2830					edge_serial->interrupt_read_urb,
2831					dev,
2832					usb_rcvintpipe(dev,
2833						endpoint->bEndpointAddress),
2834					edge_serial->interrupt_in_buffer,
2835					buffer_size,
2836					edge_interrupt_callback,
2837					edge_serial,
2838					endpoint->bInterval);
2839
2840				interrupt_in_found = true;
2841			}
2842
2843			if (!bulk_in_found &&
2844				(usb_endpoint_is_bulk_in(endpoint))) {
2845				/* we found a bulk in endpoint */
2846				dev_dbg(ddev, "found bulk in\n");
2847
2848				/* not set up yet, so do it now */
2849				edge_serial->read_urb =
2850						usb_alloc_urb(0, GFP_KERNEL);
2851				if (!edge_serial->read_urb) {
2852					response = -ENOMEM;
2853					break;
2854				}
2855
2856				edge_serial->bulk_in_buffer =
2857					kmalloc(buffer_size, GFP_KERNEL);
2858				if (!edge_serial->bulk_in_buffer) {
2859					response = -ENOMEM;
2860					break;
2861				}
2862				edge_serial->bulk_in_endpoint =
2863						endpoint->bEndpointAddress;
2864
2865				/* set up our bulk in urb */
2866				usb_fill_bulk_urb(edge_serial->read_urb, dev,
2867					usb_rcvbulkpipe(dev,
2868						endpoint->bEndpointAddress),
2869					edge_serial->bulk_in_buffer,
2870					usb_endpoint_maxp(endpoint),
2871					edge_bulk_in_callback,
2872					edge_serial);
2873				bulk_in_found = true;
2874			}
2875
2876			if (!bulk_out_found &&
2877			    (usb_endpoint_is_bulk_out(endpoint))) {
2878				/* we found a bulk out endpoint */
2879				dev_dbg(ddev, "found bulk out\n");
2880				edge_serial->bulk_out_endpoint =
2881						endpoint->bEndpointAddress;
2882				bulk_out_found = true;
2883			}
2884		}
2885
2886		if (response || !interrupt_in_found || !bulk_in_found ||
2887							!bulk_out_found) {
2888			if (!response) {
2889				dev_err(ddev, "expected endpoints not found\n");
2890				response = -ENODEV;
2891			}
2892
2893			goto error;
2894		}
2895
2896		/* start interrupt read for this edgeport this interrupt will
2897		 * continue as long as the edgeport is connected */
2898		response = usb_submit_urb(edge_serial->interrupt_read_urb,
2899								GFP_KERNEL);
2900		if (response) {
2901			dev_err(ddev, "%s - Error %d submitting control urb\n",
2902				__func__, response);
2903
2904			goto error;
2905		}
2906	}
2907	return response;
2908
2909error:
2910	usb_free_urb(edge_serial->interrupt_read_urb);
2911	kfree(edge_serial->interrupt_in_buffer);
2912
2913	usb_free_urb(edge_serial->read_urb);
2914	kfree(edge_serial->bulk_in_buffer);
2915
2916	kfree(edge_serial);
2917
2918	return response;
2919}
2920
2921
2922/****************************************************************************
2923 * edge_disconnect
2924 *	This function is called whenever the device is removed from the usb bus.
2925 ****************************************************************************/
2926static void edge_disconnect(struct usb_serial *serial)
2927{
2928	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2929
2930	if (edge_serial->is_epic) {
2931		usb_kill_urb(edge_serial->interrupt_read_urb);
2932		usb_kill_urb(edge_serial->read_urb);
2933	}
2934}
2935
2936
2937/****************************************************************************
2938 * edge_release
2939 *	This function is called when the device structure is deallocated.
2940 ****************************************************************************/
2941static void edge_release(struct usb_serial *serial)
2942{
2943	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2944
2945	if (edge_serial->is_epic) {
2946		usb_kill_urb(edge_serial->interrupt_read_urb);
2947		usb_free_urb(edge_serial->interrupt_read_urb);
2948		kfree(edge_serial->interrupt_in_buffer);
2949
2950		usb_kill_urb(edge_serial->read_urb);
2951		usb_free_urb(edge_serial->read_urb);
2952		kfree(edge_serial->bulk_in_buffer);
2953	}
2954
2955	kfree(edge_serial);
2956}
2957
2958static int edge_port_probe(struct usb_serial_port *port)
2959{
2960	struct edgeport_port *edge_port;
2961
2962	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
2963	if (!edge_port)
2964		return -ENOMEM;
2965
2966	spin_lock_init(&edge_port->ep_lock);
2967	edge_port->port = port;
2968
2969	usb_set_serial_port_data(port, edge_port);
2970
2971	return 0;
2972}
2973
2974static void edge_port_remove(struct usb_serial_port *port)
2975{
2976	struct edgeport_port *edge_port;
2977
2978	edge_port = usb_get_serial_port_data(port);
2979	kfree(edge_port);
2980}
2981
2982static struct usb_serial_driver edgeport_2port_device = {
2983	.driver = {
2984		.name		= "edgeport_2",
2985	},
2986	.description		= "Edgeport 2 port adapter",
2987	.id_table		= edgeport_2port_id_table,
2988	.num_ports		= 2,
2989	.num_bulk_in		= 1,
2990	.num_bulk_out		= 1,
2991	.num_interrupt_in	= 1,
2992	.open			= edge_open,
2993	.close			= edge_close,
2994	.throttle		= edge_throttle,
2995	.unthrottle		= edge_unthrottle,
2996	.attach			= edge_startup,
2997	.disconnect		= edge_disconnect,
2998	.release		= edge_release,
2999	.port_probe		= edge_port_probe,
3000	.port_remove		= edge_port_remove,
3001	.ioctl			= edge_ioctl,
3002	.set_termios		= edge_set_termios,
3003	.tiocmget		= edge_tiocmget,
3004	.tiocmset		= edge_tiocmset,
3005	.tiocmiwait		= usb_serial_generic_tiocmiwait,
3006	.get_icount		= usb_serial_generic_get_icount,
3007	.write			= edge_write,
3008	.write_room		= edge_write_room,
3009	.chars_in_buffer	= edge_chars_in_buffer,
3010	.break_ctl		= edge_break,
3011	.read_int_callback	= edge_interrupt_callback,
3012	.read_bulk_callback	= edge_bulk_in_callback,
3013	.write_bulk_callback	= edge_bulk_out_data_callback,
3014};
3015
3016static struct usb_serial_driver edgeport_4port_device = {
3017	.driver = {
3018		.name		= "edgeport_4",
3019	},
3020	.description		= "Edgeport 4 port adapter",
3021	.id_table		= edgeport_4port_id_table,
3022	.num_ports		= 4,
3023	.num_bulk_in		= 1,
3024	.num_bulk_out		= 1,
3025	.num_interrupt_in	= 1,
3026	.open			= edge_open,
3027	.close			= edge_close,
3028	.throttle		= edge_throttle,
3029	.unthrottle		= edge_unthrottle,
3030	.attach			= edge_startup,
3031	.disconnect		= edge_disconnect,
3032	.release		= edge_release,
3033	.port_probe		= edge_port_probe,
3034	.port_remove		= edge_port_remove,
3035	.ioctl			= edge_ioctl,
3036	.set_termios		= edge_set_termios,
3037	.tiocmget		= edge_tiocmget,
3038	.tiocmset		= edge_tiocmset,
3039	.tiocmiwait		= usb_serial_generic_tiocmiwait,
3040	.get_icount		= usb_serial_generic_get_icount,
3041	.write			= edge_write,
3042	.write_room		= edge_write_room,
3043	.chars_in_buffer	= edge_chars_in_buffer,
3044	.break_ctl		= edge_break,
3045	.read_int_callback	= edge_interrupt_callback,
3046	.read_bulk_callback	= edge_bulk_in_callback,
3047	.write_bulk_callback	= edge_bulk_out_data_callback,
3048};
3049
3050static struct usb_serial_driver edgeport_8port_device = {
3051	.driver = {
3052		.name		= "edgeport_8",
3053	},
3054	.description		= "Edgeport 8 port adapter",
3055	.id_table		= edgeport_8port_id_table,
3056	.num_ports		= 8,
3057	.num_bulk_in		= 1,
3058	.num_bulk_out		= 1,
3059	.num_interrupt_in	= 1,
3060	.open			= edge_open,
3061	.close			= edge_close,
3062	.throttle		= edge_throttle,
3063	.unthrottle		= edge_unthrottle,
3064	.attach			= edge_startup,
3065	.disconnect		= edge_disconnect,
3066	.release		= edge_release,
3067	.port_probe		= edge_port_probe,
3068	.port_remove		= edge_port_remove,
3069	.ioctl			= edge_ioctl,
3070	.set_termios		= edge_set_termios,
3071	.tiocmget		= edge_tiocmget,
3072	.tiocmset		= edge_tiocmset,
3073	.tiocmiwait		= usb_serial_generic_tiocmiwait,
3074	.get_icount		= usb_serial_generic_get_icount,
3075	.write			= edge_write,
3076	.write_room		= edge_write_room,
3077	.chars_in_buffer	= edge_chars_in_buffer,
3078	.break_ctl		= edge_break,
3079	.read_int_callback	= edge_interrupt_callback,
3080	.read_bulk_callback	= edge_bulk_in_callback,
3081	.write_bulk_callback	= edge_bulk_out_data_callback,
3082};
3083
3084static struct usb_serial_driver epic_device = {
3085	.driver = {
3086		.name		= "epic",
3087	},
3088	.description		= "EPiC device",
3089	.id_table		= Epic_port_id_table,
3090	.num_ports		= 1,
3091	.num_bulk_in		= 1,
3092	.num_bulk_out		= 1,
3093	.num_interrupt_in	= 1,
3094	.open			= edge_open,
3095	.close			= edge_close,
3096	.throttle		= edge_throttle,
3097	.unthrottle		= edge_unthrottle,
3098	.attach			= edge_startup,
3099	.disconnect		= edge_disconnect,
3100	.release		= edge_release,
3101	.port_probe		= edge_port_probe,
3102	.port_remove		= edge_port_remove,
3103	.ioctl			= edge_ioctl,
3104	.set_termios		= edge_set_termios,
3105	.tiocmget		= edge_tiocmget,
3106	.tiocmset		= edge_tiocmset,
3107	.tiocmiwait		= usb_serial_generic_tiocmiwait,
3108	.get_icount		= usb_serial_generic_get_icount,
3109	.write			= edge_write,
3110	.write_room		= edge_write_room,
3111	.chars_in_buffer	= edge_chars_in_buffer,
3112	.break_ctl		= edge_break,
3113	.read_int_callback	= edge_interrupt_callback,
3114	.read_bulk_callback	= edge_bulk_in_callback,
3115	.write_bulk_callback	= edge_bulk_out_data_callback,
3116};
3117
3118static struct usb_serial_driver * const serial_drivers[] = {
3119	&edgeport_2port_device, &edgeport_4port_device,
3120	&edgeport_8port_device, &epic_device, NULL
3121};
3122
3123module_usb_serial_driver(serial_drivers, id_table_combined);
3124
3125MODULE_AUTHOR(DRIVER_AUTHOR);
3126MODULE_DESCRIPTION(DRIVER_DESC);
3127MODULE_LICENSE("GPL");
3128MODULE_FIRMWARE("edgeport/boot.fw");
3129MODULE_FIRMWARE("edgeport/boot2.fw");
3130MODULE_FIRMWARE("edgeport/down.fw");
3131MODULE_FIRMWARE("edgeport/down2.fw");
~/ Read-only file preview.