io_edgeport.c 99.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * Edgeport USB Serial Converter driver
 *
 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
 *
 * Supports the following devices:
 *	Edgeport/4
 *	Edgeport/4t
 *	Edgeport/2
 *	Edgeport/4i
 *	Edgeport/2i
 *	Edgeport/421
 *	Edgeport/21
 *	Rapidport/4
 *	Edgeport/8
 *	Edgeport/2D8
 *	Edgeport/4D8
 *	Edgeport/8i
 *
 * For questions or problems with this driver, contact Inside Out
 * Networks technical support, or Peter Berger <pberger@brimson.com>,
 * or Al Borchers <alborchers@steinerpoint.com>.
 *
 */

#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/serial.h>
#include <linux/ioctl.h>
#include <linux/wait.h>
40 41
#include <linux/firmware.h>
#include <linux/ihex.h>
A
Alan Cox 已提交
42
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
43
#include <linux/usb.h>
44
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55
#include "io_edgeport.h"
#include "io_ionsp.h"		/* info for the iosp messages */
#include "io_16654.h"		/* 16654 UART defines */

#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
#define DRIVER_DESC "Edgeport USB Serial Driver"

#define MAX_NAME_LEN		64

#define OPEN_TIMEOUT		(5*HZ)		/* 5 seconds */

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static const struct usb_device_id edgeport_2port_id_table[] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2_DIN) },
	{ }
};

static const struct usb_device_id edgeport_4port_id_table[] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_RAPIDPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4T) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4_DIN) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
	{ }
};

static const struct usb_device_id edgeport_8port_id_table[] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
	{ }
};

static const struct usb_device_id Epic_port_id_table[] = {
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
	{ }
};

/* Devices that this driver supports */
static const struct usb_device_id id_table_combined[] = {
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_RAPIDPORT_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4T) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_421) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_21) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_2_DIN) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_4_DIN) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
	{ USB_DEVICE(USB_VENDOR_ID_ION,	ION_DEVICE_ID_EDGEPORT_8I) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
	{ USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
	{ USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
	{ USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
	{ } /* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, id_table_combined);


L
Linus Torvalds 已提交
138 139
/* receive port state */
enum RXSTATE {
A
Alan Cox 已提交
140 141 142 143
	EXPECT_HDR1 = 0,    /* Expect header byte 1 */
	EXPECT_HDR2 = 1,    /* Expect header byte 2 */
	EXPECT_DATA = 2,    /* Expect 'RxBytesRemaining' data */
	EXPECT_HDR3 = 3,    /* Expect header byte 3 (for status hdrs only) */
L
Linus Torvalds 已提交
144 145 146
};


A
Alan Cox 已提交
147 148 149
/* Transmit Fifo
 * This Transmit queue is an extension of the edgeport Rx buffer.
 * The maximum amount of data buffered in both the edgeport
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
 */
struct TxFifo {
	unsigned int	head;	/* index to head pointer (write) */
	unsigned int	tail;	/* index to tail pointer (read)  */
	unsigned int	count;	/* Bytes in queue */
	unsigned int	size;	/* Max size of queue (equal to Max number of TxCredits) */
	unsigned char	*fifo;	/* allocated Buffer */
};

/* This structure holds all of the local port information */
struct edgeport_port {
	__u16			txCredits;		/* our current credits for this port */
	__u16			maxTxCredits;		/* the max size of the port */

	struct TxFifo		txfifo;			/* transmit fifo -- size will be maxTxCredits */
	struct urb		*write_urb;		/* write URB for this port */
167
	bool			write_in_progress;	/* 'true' while a write URB is outstanding */
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178
	spinlock_t		ep_lock;

	__u8			shadowLCR;		/* last LCR value received */
	__u8			shadowMCR;		/* last MCR value received */
	__u8			shadowMSR;		/* last MSR value received */
	__u8			shadowLSR;		/* last LSR value received */
	__u8			shadowXonChar;		/* last value set as XON char in Edgeport */
	__u8			shadowXoffChar;		/* last value set as XOFF char in Edgeport */
	__u8			validDataMask;
	__u32			baudRate;

179 180 181 182 183
	bool			open;
	bool			openPending;
	bool			commandPending;
	bool			closePending;
	bool			chaseResponsePending;
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194

	wait_queue_head_t	wait_chase;		/* for handling sleeping while waiting for chase to finish */
	wait_queue_head_t	wait_open;		/* for handling sleeping while waiting for open to finish */
	wait_queue_head_t	wait_command;		/* for handling sleeping while waiting for command to finish */

	struct usb_serial_port	*port;			/* loop back to the owner of this object */
};


/* This structure holds all of the individual device information */
struct edgeport_serial {
195
	char			name[MAX_NAME_LEN+2];		/* string name of this device */
L
Linus Torvalds 已提交
196 197 198 199

	struct edge_manuf_descriptor	manuf_descriptor;	/* the manufacturer descriptor */
	struct edge_boot_descriptor	boot_descriptor;	/* the boot firmware descriptor */
	struct edgeport_product_info	product_info;		/* Product Info */
200 201
	struct edge_compatibility_descriptor epic_descriptor;	/* Edgeport compatible descriptor */
	int			is_epic;			/* flag if EPiC device or not */
L
Linus Torvalds 已提交
202 203

	__u8			interrupt_in_endpoint;		/* the interrupt endpoint handle */
A
Alan Cox 已提交
204 205
	unsigned char		*interrupt_in_buffer;		/* the buffer we use for the interrupt endpoint */
	struct urb		*interrupt_read_urb;		/* our interrupt urb */
L
Linus Torvalds 已提交
206 207

	__u8			bulk_in_endpoint;		/* the bulk in endpoint handle */
A
Alan Cox 已提交
208 209
	unsigned char		*bulk_in_buffer;		/* the buffer we use for the bulk in endpoint */
	struct urb		*read_urb;			/* our bulk read urb */
210
	bool			read_in_progress;
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	spinlock_t		es_lock;

	__u8			bulk_out_endpoint;		/* the bulk out endpoint handle */

	__s16			rxBytesAvail;			/* the number of bytes that we need to read from this device */

	enum RXSTATE		rxState;			/* the current state of the bulk receive processor */
	__u8			rxHeader1;			/* receive header byte 1 */
	__u8			rxHeader2;			/* receive header byte 2 */
	__u8			rxHeader3;			/* receive header byte 3 */
	__u8			rxPort;				/* the port that we are currently receiving data for */
	__u8			rxStatusCode;			/* the receive status code */
	__u8			rxStatusParam;			/* the receive status paramater */
	__s16			rxBytesRemaining;		/* the number of port bytes left to read */
	struct usb_serial	*serial;			/* loop back to the owner of this object */
};

/* baud rate information */
struct divisor_table_entry {
	__u32   BaudRate;
	__u16  Divisor;
};

A
Alan Cox 已提交
234 235 236 237 238 239
/*
 * Define table of divisors for Rev A EdgePort/4 hardware
 * These assume a 3.6864MHz crystal, the standard /16, and
 * MCR.7 = 0.
 */

240
static const struct divisor_table_entry divisor_table[] = {
A
Alan Cox 已提交
241 242 243 244
	{   50,		4608},
	{   75,		3072},
	{   110,	2095},	/* 2094.545455 => 230450   => .0217 % over */
	{   134,	1713},	/* 1713.011152 => 230398.5 => .00065% under */
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	{   150,	1536},
	{   300,	768},
	{   600,	384},
	{   1200,	192},
	{   1800,	128},
	{   2400,	96},
	{   4800,	48},
	{   7200,	32},
	{   9600,	24},
	{   14400,	16},
	{   19200,	12},
	{   38400,	6},
	{   57600,	4},
	{   115200,	2},
	{   230400,	1},
};

262 263
/* Number of outstanding Command Write Urbs */
static atomic_t CmdUrbs = ATOMIC_INIT(0);
L
Linus Torvalds 已提交
264 265 266 267 268


/* local function prototypes */

/* function prototypes for all URB callbacks */
A
Alan Cox 已提交
269 270 271 272
static void edge_interrupt_callback(struct urb *urb);
static void edge_bulk_in_callback(struct urb *urb);
static void edge_bulk_out_data_callback(struct urb *urb);
static void edge_bulk_out_cmd_callback(struct urb *urb);
L
Linus Torvalds 已提交
273 274

/* function prototypes for the usbserial callbacks */
275
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
276
static void edge_close(struct usb_serial_port *port);
A
Alan Cox 已提交
277 278 279 280 281 282 283 284 285
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *buf, int count);
static int edge_write_room(struct tty_struct *tty);
static int edge_chars_in_buffer(struct tty_struct *tty);
static void edge_throttle(struct tty_struct *tty);
static void edge_unthrottle(struct tty_struct *tty);
static void edge_set_termios(struct tty_struct *tty,
					struct usb_serial_port *port,
					struct ktermios *old_termios);
286
static int  edge_ioctl(struct tty_struct *tty,
A
Alan Cox 已提交
287 288
					unsigned int cmd, unsigned long arg);
static void edge_break(struct tty_struct *tty, int break_state);
289
static int  edge_tiocmget(struct tty_struct *tty);
290
static int  edge_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
291 292
					unsigned int set, unsigned int clear);
static int  edge_startup(struct usb_serial *serial);
293 294
static void edge_disconnect(struct usb_serial *serial);
static void edge_release(struct usb_serial *serial);
295 296
static int edge_port_probe(struct usb_serial_port *port);
static int edge_port_remove(struct usb_serial_port *port);
L
Linus Torvalds 已提交
297 298

/* function prototypes for all of our local functions */
A
Alan Cox 已提交
299 300 301 302 303

static void  process_rcvd_data(struct edgeport_serial *edge_serial,
				unsigned char *buffer, __u16 bufferLength);
static void process_rcvd_status(struct edgeport_serial *edge_serial,
				__u8 byte2, __u8 byte3);
J
Jiri Slaby 已提交
304 305
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length);
A
Alan Cox 已提交
306 307 308 309 310
static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
				__u8 lsr, __u8 data);
static int  send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
				__u8 param);
311
static int  calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
A
Alan Cox 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static int  send_cmd_write_baud_rate(struct edgeport_port *edge_port,
				int baudRate);
static void change_port_settings(struct tty_struct *tty,
				struct edgeport_port *edge_port,
				struct ktermios *old_termios);
static int  send_cmd_write_uart_register(struct edgeport_port *edge_port,
				__u8 regNum, __u8 regValue);
static int  write_cmd_usb(struct edgeport_port *edge_port,
				unsigned char *buffer, int writeLength);
static void send_more_port_data(struct edgeport_serial *edge_serial,
				struct edgeport_port *edge_port);

static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data);
static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
						__u16 length, __u8 *data);
static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data);
static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
static void get_boot_desc(struct edgeport_serial *edge_serial);
static void load_application_firmware(struct edgeport_serial *edge_serial);

static void unicode_to_ascii(char *string, int buflen,
				__le16 *unicode, int unicode_size);


/* ************************************************************************ */
/* ************************************************************************ */
/* ************************************************************************ */
/* ************************************************************************ */
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350

/************************************************************************
 *									*
 * update_edgeport_E2PROM()	Compare current versions of		*
 *				Boot ROM and Manufacture 		*
 *				Descriptors with versions		*
 *				embedded in this driver			*
 *									*
 ************************************************************************/
A
Alan Cox 已提交
351
static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
352
{
353
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
354 355
	__u32 BootCurVer;
	__u32 BootNewVer;
356 357 358 359 360 361 362
	__u8 BootMajorVersion;
	__u8 BootMinorVersion;
	__u16 BootBuildNumber;
	__u32 Bootaddr;
	const struct ihex_binrec *rec;
	const struct firmware *fw;
	const char *fw_name;
L
Linus Torvalds 已提交
363 364 365
	int response;

	switch (edge_serial->product_info.iDownloadFile) {
A
Alan Cox 已提交
366 367 368 369 370 371 372 373
	case EDGE_DOWNLOAD_FILE_I930:
		fw_name	= "edgeport/boot.fw";
		break;
	case EDGE_DOWNLOAD_FILE_80251:
		fw_name	= "edgeport/boot2.fw";
		break;
	default:
		return;
L
Linus Torvalds 已提交
374 375
	}

376 377 378
	response = request_ihex_firmware(&fw, fw_name,
					 &edge_serial->serial->dev->dev);
	if (response) {
379
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
380 381 382 383 384 385 386 387 388
		       fw_name, response);
		return;
	}

	rec = (const struct ihex_binrec *)fw->data;
	BootMajorVersion = rec->data[0];
	BootMinorVersion = rec->data[1];
	BootBuildNumber = (rec->data[2] << 8) | rec->data[3];

A
Alan Cox 已提交
389
	/* Check Boot Image Version */
L
Linus Torvalds 已提交
390 391 392 393 394 395
	BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
		     (edge_serial->boot_descriptor.MinorVersion << 16) +
		      le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);

	BootNewVer = (BootMajorVersion << 24) +
		     (BootMinorVersion << 16) +
396
		      BootBuildNumber;
L
Linus Torvalds 已提交
397

398
	dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
L
Linus Torvalds 已提交
399 400 401 402 403 404
	    edge_serial->boot_descriptor.MajorVersion,
	    edge_serial->boot_descriptor.MinorVersion,
	    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));


	if (BootNewVer > BootCurVer) {
405
		dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
L
Linus Torvalds 已提交
406 407 408
		    edge_serial->boot_descriptor.MajorVersion,
		    edge_serial->boot_descriptor.MinorVersion,
		    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
409
		    BootMajorVersion, BootMinorVersion, BootBuildNumber);
L
Linus Torvalds 已提交
410

411
		dev_dbg(dev, "Downloading new Boot Image\n");
L
Linus Torvalds 已提交
412

413 414 415 416 417 418 419 420
		for (rec = ihex_next_binrec(rec); rec;
		     rec = ihex_next_binrec(rec)) {
			Bootaddr = be32_to_cpu(rec->addr);
			response = rom_write(edge_serial->serial,
					     Bootaddr >> 16,
					     Bootaddr & 0xFFFF,
					     be16_to_cpu(rec->len),
					     &rec->data[0]);
L
Linus Torvalds 已提交
421
			if (response < 0) {
422 423 424 425
				dev_err(&edge_serial->serial->dev->dev,
					"rom_write failed (%x, %x, %d)\n",
					Bootaddr >> 16, Bootaddr & 0xFFFF,
					be16_to_cpu(rec->len));
L
Linus Torvalds 已提交
426 427 428 429
				break;
			}
		}
	} else {
430
		dev_dbg(dev, "Boot Image -- already up to date\n");
L
Linus Torvalds 已提交
431
	}
432
	release_firmware(fw);
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440
}

#if 0
/************************************************************************
 *
 *  Get string descriptor from device
 *
 ************************************************************************/
A
Alan Cox 已提交
441 442
static int get_string_desc(struct usb_device *dev, int Id,
				struct usb_string_descriptor **pRetDesc)
L
Linus Torvalds 已提交
443 444 445 446
{
	struct usb_string_descriptor StringDesc;
	struct usb_string_descriptor *pStringDesc;

447
	dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
L
Linus Torvalds 已提交
448

A
Alan Cox 已提交
449 450
	if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
						sizeof(StringDesc)))
L
Linus Torvalds 已提交
451 452
		return 0;

A
Alan Cox 已提交
453 454
	pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
	if (!pStringDesc)
L
Linus Torvalds 已提交
455 456
		return -1;

A
Alan Cox 已提交
457 458
	if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
							StringDesc.bLength)) {
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466 467
		kfree(pStringDesc);
		return -1;
	}

	*pRetDesc = pStringDesc;
	return 0;
}
#endif

468 469
static void dump_product_info(struct edgeport_serial *edge_serial,
			      struct edgeport_product_info *product_info)
470
{
471 472
	struct device *dev = &edge_serial->serial->dev->dev;

A
Alan Cox 已提交
473
	/* Dump Product Info structure */
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	dev_dbg(dev, "**Product Information:\n");
	dev_dbg(dev, "  ProductId             %x\n", product_info->ProductId);
	dev_dbg(dev, "  NumPorts              %d\n", product_info->NumPorts);
	dev_dbg(dev, "  ProdInfoVer           %d\n", product_info->ProdInfoVer);
	dev_dbg(dev, "  IsServer              %d\n", product_info->IsServer);
	dev_dbg(dev, "  IsRS232               %d\n", product_info->IsRS232);
	dev_dbg(dev, "  IsRS422               %d\n", product_info->IsRS422);
	dev_dbg(dev, "  IsRS485               %d\n", product_info->IsRS485);
	dev_dbg(dev, "  RomSize               %d\n", product_info->RomSize);
	dev_dbg(dev, "  RamSize               %d\n", product_info->RamSize);
	dev_dbg(dev, "  CpuRev                %x\n", product_info->CpuRev);
	dev_dbg(dev, "  BoardRev              %x\n", product_info->BoardRev);
	dev_dbg(dev, "  BootMajorVersion      %d.%d.%d\n",
		product_info->BootMajorVersion,
		product_info->BootMinorVersion,
		le16_to_cpu(product_info->BootBuildNumber));
	dev_dbg(dev, "  FirmwareMajorVersion  %d.%d.%d\n",
		product_info->FirmwareMajorVersion,
		product_info->FirmwareMinorVersion,
		le16_to_cpu(product_info->FirmwareBuildNumber));
	dev_dbg(dev, "  ManufactureDescDate   %d/%d/%d\n",
		product_info->ManufactureDescDate[0],
		product_info->ManufactureDescDate[1],
		product_info->ManufactureDescDate[2]+1900);
	dev_dbg(dev, "  iDownloadFile         0x%x\n",
		product_info->iDownloadFile);
	dev_dbg(dev, "  EpicVer               %d\n", product_info->EpicVer);
501 502
}

L
Linus Torvalds 已提交
503 504 505 506
static void get_product_info(struct edgeport_serial *edge_serial)
{
	struct edgeport_product_info *product_info = &edge_serial->product_info;

A
Alan Cox 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	memset(product_info, 0, sizeof(struct edgeport_product_info));

	product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
	product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
	product_info->ProdInfoVer = 0;

	product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
	product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
	product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
	product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;

	product_info->BootMajorVersion =
				edge_serial->boot_descriptor.MajorVersion;
	product_info->BootMinorVersion =
				edge_serial->boot_descriptor.MinorVersion;
	product_info->BootBuildNumber =
				edge_serial->boot_descriptor.BuildNumber;

	memcpy(product_info->ManufactureDescDate,
			edge_serial->manuf_descriptor.DescDate,
			sizeof(edge_serial->manuf_descriptor.DescDate));

	/* check if this is 2nd generation hardware */
	if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
					    & ION_DEVICE_ID_80251_NETCHIP)
		product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
	else
		product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
535

A
Alan Cox 已提交
536
	/* Determine Product type and set appropriate flags */
L
Linus Torvalds 已提交
537
	switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
A
Alan Cox 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550
	case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
	case ION_DEVICE_ID_EDGEPORT_4T:
	case ION_DEVICE_ID_EDGEPORT_4:
	case ION_DEVICE_ID_EDGEPORT_2:
	case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
	case ION_DEVICE_ID_EDGEPORT_8:
	case ION_DEVICE_ID_EDGEPORT_421:
	case ION_DEVICE_ID_EDGEPORT_21:
	case ION_DEVICE_ID_EDGEPORT_2_DIN:
	case ION_DEVICE_ID_EDGEPORT_4_DIN:
	case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
		product_info->IsRS232 = 1;
		break;
L
Linus Torvalds 已提交
551

A
Alan Cox 已提交
552 553 554 555
	case ION_DEVICE_ID_EDGEPORT_2I:	/* Edgeport/2 RS422/RS485 */
		product_info->IsRS422 = 1;
		product_info->IsRS485 = 1;
		break;
L
Linus Torvalds 已提交
556

A
Alan Cox 已提交
557 558 559 560
	case ION_DEVICE_ID_EDGEPORT_8I:	/* Edgeport/4 RS422 */
	case ION_DEVICE_ID_EDGEPORT_4I:	/* Edgeport/4 RS422 */
		product_info->IsRS422 = 1;
		break;
L
Linus Torvalds 已提交
561 562
	}

563
	dump_product_info(edge_serial, product_info);
564
}
L
Linus Torvalds 已提交
565

566 567 568 569 570
static int get_epic_descriptor(struct edgeport_serial *ep)
{
	int result;
	struct usb_serial *serial = ep->serial;
	struct edgeport_product_info *product_info = &ep->product_info;
571
	struct edge_compatibility_descriptor *epic;
572
	struct edge_compatibility_bits *bits;
573
	struct device *dev = &serial->dev->dev;
574 575

	ep->is_epic = 0;
576 577 578 579 580

	epic = kmalloc(sizeof(*epic), GFP_KERNEL);
	if (!epic)
		return -ENOMEM;

581 582 583
	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				 USB_REQUEST_ION_GET_EPIC_DESC,
				 0xC0, 0x00, 0x00,
584
				 epic, sizeof(*epic),
585
				 300);
586
	if (result == sizeof(*epic)) {
587
		ep->is_epic = 1;
588
		memcpy(&ep->epic_descriptor, epic, sizeof(*epic));
589 590
		memset(product_info, 0, sizeof(struct edgeport_product_info));

A
Alan Cox 已提交
591 592 593 594 595 596 597 598 599
		product_info->NumPorts = epic->NumPorts;
		product_info->ProdInfoVer = 0;
		product_info->FirmwareMajorVersion = epic->MajorVersion;
		product_info->FirmwareMinorVersion = epic->MinorVersion;
		product_info->FirmwareBuildNumber = epic->BuildNumber;
		product_info->iDownloadFile = epic->iDownloadFile;
		product_info->EpicVer = epic->EpicVer;
		product_info->Epic = epic->Supports;
		product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
600
		dump_product_info(ep, product_info);
601 602

		bits = &ep->epic_descriptor.Supports;
603 604 605 606 607 608 609 610 611 612 613 614 615 616
		dev_dbg(dev, "**EPIC descriptor:\n");
		dev_dbg(dev, "  VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPOpen         : %s\n", bits->IOSPOpen	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPClose        : %s\n", bits->IOSPClose	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPChase        : %s\n", bits->IOSPChase	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPSetRxFlow    : %s\n", bits->IOSPSetRxFlow	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPSetTxFlow    : %s\n", bits->IOSPSetTxFlow	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPSetXChar     : %s\n", bits->IOSPSetXChar	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPRxCheck      : %s\n", bits->IOSPRxCheck	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPSetClrBreak  : %s\n", bits->IOSPSetClrBreak	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPWriteMCR     : %s\n", bits->IOSPWriteMCR	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPWriteLCR     : %s\n", bits->IOSPWriteLCR	? "TRUE": "FALSE");
		dev_dbg(dev, "  IOSPSetBaudRate  : %s\n", bits->IOSPSetBaudRate	? "TRUE": "FALSE");
		dev_dbg(dev, "  TrueEdgeport     : %s\n", bits->TrueEdgeport	? "TRUE": "FALSE");
617 618 619 620 621 622

		result = 0;
	} else if (result >= 0) {
		dev_warn(&serial->interface->dev, "short epic descriptor received: %d\n",
			 result);
		result = -EIO;
623 624
	}

625 626
	kfree(epic);

627
	return result;
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636 637 638 639
}


/************************************************************************/
/************************************************************************/
/*            U S B  C A L L B A C K   F U N C T I O N S                */
/*            U S B  C A L L B A C K   F U N C T I O N S                */
/************************************************************************/
/************************************************************************/

/*****************************************************************************
 * edge_interrupt_callback
A
Alan Cox 已提交
640
 *	this is the callback function for when we have received data on the
L
Linus Torvalds 已提交
641 642
 *	interrupt endpoint.
 *****************************************************************************/
A
Alan Cox 已提交
643
static void edge_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
644
{
645 646
	struct edgeport_serial *edge_serial = urb->context;
	struct device *dev;
L
Linus Torvalds 已提交
647 648 649 650
	struct edgeport_port *edge_port;
	struct usb_serial_port *port;
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
651
	unsigned long flags;
L
Linus Torvalds 已提交
652 653 654 655 656
	int bytes_avail;
	int position;
	int txCredits;
	int portNumber;
	int result;
657
	int status = urb->status;
L
Linus Torvalds 已提交
658

659
	switch (status) {
L
Linus Torvalds 已提交
660 661 662 663 664 665 666
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
667
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
L
Linus Torvalds 已提交
668 669
		return;
	default:
670
		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
L
Linus Torvalds 已提交
671 672 673
		goto exit;
	}

674 675
	dev = &edge_serial->serial->dev->dev;

A
Alan Cox 已提交
676
	/* process this interrupt-read even if there are no ports open */
L
Linus Torvalds 已提交
677
	if (length) {
678
		usb_serial_debug_data(dev, __func__, length, data);
L
Linus Torvalds 已提交
679 680 681 682

		if (length > 1) {
			bytes_avail = data[0] | (data[1] << 8);
			if (bytes_avail) {
683
				spin_lock_irqsave(&edge_serial->es_lock, flags);
L
Linus Torvalds 已提交
684
				edge_serial->rxBytesAvail += bytes_avail;
685 686 687 688 689
				dev_dbg(dev,
					"%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
					__func__, bytes_avail,
					edge_serial->rxBytesAvail,
					edge_serial->read_in_progress);
L
Linus Torvalds 已提交
690 691 692

				if (edge_serial->rxBytesAvail > 0 &&
				    !edge_serial->read_in_progress) {
693
					dev_dbg(dev, "%s - posting a read\n", __func__);
694
					edge_serial->read_in_progress = true;
L
Linus Torvalds 已提交
695

A
Alan Cox 已提交
696 697
					/* we have pending bytes on the
					   bulk in pipe, send a request */
L
Linus Torvalds 已提交
698 699
					result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
					if (result) {
700 701 702
						dev_err(dev,
							"%s - usb_submit_urb(read bulk) failed with result = %d\n",
							__func__, result);
703
						edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
704 705
					}
				}
706 707
				spin_unlock_irqrestore(&edge_serial->es_lock,
						       flags);
L
Linus Torvalds 已提交
708 709 710 711 712
			}
		}
		/* grab the txcredits for the ports if available */
		position = 2;
		portNumber = 0;
A
Alan Cox 已提交
713 714
		while ((position < length) &&
				(portNumber < edge_serial->serial->num_ports)) {
L
Linus Torvalds 已提交
715 716 717 718 719
			txCredits = data[position] | (data[position+1] << 8);
			if (txCredits) {
				port = edge_serial->serial->port[portNumber];
				edge_port = usb_get_serial_port_data(port);
				if (edge_port->open) {
720 721
					spin_lock_irqsave(&edge_port->ep_lock,
							  flags);
L
Linus Torvalds 已提交
722
					edge_port->txCredits += txCredits;
723 724
					spin_unlock_irqrestore(&edge_port->ep_lock,
							       flags);
725 726 727
					dev_dbg(dev, "%s - txcredits for port%d = %d\n",
						__func__, portNumber,
						edge_port->txCredits);
L
Linus Torvalds 已提交
728

A
Alan Cox 已提交
729 730
					/* tell the tty driver that something
					   has changed */
J
Jiri Slaby 已提交
731
					tty_port_tty_wakeup(&edge_port->port->port);
A
Alan Cox 已提交
732 733 734 735
					/* Since we have more credit, check
					   if more data can be sent */
					send_more_port_data(edge_serial,
								edge_port);
L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743
				}
			}
			position += 2;
			++portNumber;
		}
	}

exit:
A
Alan Cox 已提交
744 745 746 747 748
	result = usb_submit_urb(urb, GFP_ATOMIC);
	if (result)
		dev_err(&urb->dev->dev,
			"%s - Error %d submitting control urb\n",
						__func__, result);
L
Linus Torvalds 已提交
749 750 751 752 753
}


/*****************************************************************************
 * edge_bulk_in_callback
A
Alan Cox 已提交
754
 *	this is the callback function for when we have received data on the
L
Linus Torvalds 已提交
755 756
 *	bulk in endpoint.
 *****************************************************************************/
A
Alan Cox 已提交
757
static void edge_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
758
{
759
	struct edgeport_serial	*edge_serial = urb->context;
760
	struct device *dev;
L
Linus Torvalds 已提交
761
	unsigned char		*data = urb->transfer_buffer;
762
	int			retval;
L
Linus Torvalds 已提交
763
	__u16			raw_data_length;
764
	int status = urb->status;
765
	unsigned long flags;
L
Linus Torvalds 已提交
766

767
	if (status) {
768 769
		dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
			__func__, status);
770
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
771 772 773 774
		return;
	}

	if (urb->actual_length == 0) {
775
		dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
776
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
777 778 779
		return;
	}

780
	dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
781 782
	raw_data_length = urb->actual_length;

783
	usb_serial_debug_data(dev, __func__, raw_data_length, data);
L
Linus Torvalds 已提交
784

785
	spin_lock_irqsave(&edge_serial->es_lock, flags);
L
Linus Torvalds 已提交
786 787 788 789

	/* decrement our rxBytes available by the number that we just got */
	edge_serial->rxBytesAvail -= raw_data_length;

790 791
	dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
		raw_data_length, edge_serial->rxBytesAvail);
L
Linus Torvalds 已提交
792

A
Alan Cox 已提交
793
	process_rcvd_data(edge_serial, data, urb->actual_length);
L
Linus Torvalds 已提交
794 795 796

	/* check to see if there's any more data for us to read */
	if (edge_serial->rxBytesAvail > 0) {
797
		dev_dbg(dev, "%s - posting a read\n", __func__);
798 799
		retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
		if (retval) {
800 801 802
			dev_err(dev,
				"%s - usb_submit_urb(read bulk) failed, retval = %d\n",
				__func__, retval);
803
			edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
804 805
		}
	} else {
806
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
807 808
	}

809
	spin_unlock_irqrestore(&edge_serial->es_lock, flags);
L
Linus Torvalds 已提交
810 811 812 813 814
}


/*****************************************************************************
 * edge_bulk_out_data_callback
A
Alan Cox 已提交
815 816
 *	this is the callback function for when we have finished sending
 *	serial data on the bulk out endpoint.
L
Linus Torvalds 已提交
817
 *****************************************************************************/
A
Alan Cox 已提交
818
static void edge_bulk_out_data_callback(struct urb *urb)
L
Linus Torvalds 已提交
819
{
820
	struct edgeport_port *edge_port = urb->context;
821
	int status = urb->status;
L
Linus Torvalds 已提交
822

823
	if (status) {
824 825 826
		dev_dbg(&urb->dev->dev,
			"%s - nonzero write bulk status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
827 828
	}

J
Jiri Slaby 已提交
829 830
	if (edge_port->open)
		tty_port_tty_wakeup(&edge_port->port->port);
L
Linus Torvalds 已提交
831

A
Alan Cox 已提交
832
	/* Release the Write URB */
833
	edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
834

A
Alan Cox 已提交
835 836 837
	/* Check if more data needs to be sent */
	send_more_port_data((struct edgeport_serial *)
		(usb_get_serial_data(edge_port->port->serial)), edge_port);
L
Linus Torvalds 已提交
838 839 840 841 842
}


/*****************************************************************************
 * BulkOutCmdCallback
A
Alan Cox 已提交
843 844
 *	this is the callback function for when we have finished sending a
 *	command	on the bulk out endpoint.
L
Linus Torvalds 已提交
845
 *****************************************************************************/
A
Alan Cox 已提交
846
static void edge_bulk_out_cmd_callback(struct urb *urb)
L
Linus Torvalds 已提交
847
{
848
	struct edgeport_port *edge_port = urb->context;
L
Linus Torvalds 已提交
849 850
	int status = urb->status;

851
	atomic_dec(&CmdUrbs);
852 853
	dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
		__func__, urb, atomic_read(&CmdUrbs));
L
Linus Torvalds 已提交
854 855 856


	/* clean up the transfer buffer */
857
	kfree(urb->transfer_buffer);
L
Linus Torvalds 已提交
858 859

	/* Free the command urb */
A
Alan Cox 已提交
860
	usb_free_urb(urb);
L
Linus Torvalds 已提交
861 862

	if (status) {
863 864 865
		dev_dbg(&urb->dev->dev,
			"%s - nonzero write bulk status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
866 867 868 869
		return;
	}

	/* tell the tty driver that something has changed */
J
Jiri Slaby 已提交
870 871
	if (edge_port->open)
		tty_port_tty_wakeup(&edge_port->port->port);
L
Linus Torvalds 已提交
872 873

	/* we have completed the command */
874
	edge_port->commandPending = false;
L
Linus Torvalds 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888
	wake_up(&edge_port->wait_command);
}


/*****************************************************************************
 * Driver tty interface functions
 *****************************************************************************/

/*****************************************************************************
 * SerialOpen
 *	this function is called by the tty driver when a port is opened
 *	If successful, we return 0
 *	Otherwise we return a negative error number.
 *****************************************************************************/
889
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
890 891
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
892
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
893 894 895 896 897 898 899
	struct usb_serial *serial;
	struct edgeport_serial *edge_serial;
	int response;

	if (edge_port == NULL)
		return -ENODEV;

A
Alan Cox 已提交
900 901
	/* see if we've set up our endpoint info yet (can't set it up
	   in edge_startup as the structures were not set up at that time.) */
L
Linus Torvalds 已提交
902 903
	serial = port->serial;
	edge_serial = usb_get_serial_data(serial);
A
Alan Cox 已提交
904
	if (edge_serial == NULL)
L
Linus Torvalds 已提交
905 906 907
		return -ENODEV;
	if (edge_serial->interrupt_in_buffer == NULL) {
		struct usb_serial_port *port0 = serial->port[0];
A
Alan Cox 已提交
908

L
Linus Torvalds 已提交
909
		/* not set up yet, so do it now */
A
Alan Cox 已提交
910 911 912 913
		edge_serial->interrupt_in_buffer =
					port0->interrupt_in_buffer;
		edge_serial->interrupt_in_endpoint =
					port0->interrupt_in_endpointAddress;
L
Linus Torvalds 已提交
914 915
		edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
		edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
A
Alan Cox 已提交
916 917
		edge_serial->bulk_in_endpoint =
					port0->bulk_in_endpointAddress;
L
Linus Torvalds 已提交
918
		edge_serial->read_urb = port0->read_urb;
A
Alan Cox 已提交
919 920 921
		edge_serial->bulk_out_endpoint =
					port0->bulk_out_endpointAddress;

L
Linus Torvalds 已提交
922 923
		/* set up our interrupt urb */
		usb_fill_int_urb(edge_serial->interrupt_read_urb,
A
Alan Cox 已提交
924 925 926 927 928 929 930 931
		      serial->dev,
		      usb_rcvintpipe(serial->dev,
				port0->interrupt_in_endpointAddress),
		      port0->interrupt_in_buffer,
		      edge_serial->interrupt_read_urb->transfer_buffer_length,
		      edge_interrupt_callback, edge_serial,
		      edge_serial->interrupt_read_urb->interval);

L
Linus Torvalds 已提交
932 933
		/* set up our bulk in urb */
		usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
A
Alan Cox 已提交
934 935 936 937 938
			usb_rcvbulkpipe(serial->dev,
				port0->bulk_in_endpointAddress),
			port0->bulk_in_buffer,
			edge_serial->read_urb->transfer_buffer_length,
			edge_bulk_in_callback, edge_serial);
939
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
940 941

		/* start interrupt read for this edgeport
A
Alan Cox 已提交
942 943 944 945
		 * this interrupt will continue as long
		 * as the edgeport is connected */
		response = usb_submit_urb(edge_serial->interrupt_read_urb,
								GFP_KERNEL);
L
Linus Torvalds 已提交
946
		if (response) {
947 948
			dev_err(dev, "%s - Error %d submitting control urb\n",
				__func__, response);
L
Linus Torvalds 已提交
949 950
		}
	}
A
Alan Cox 已提交
951

L
Linus Torvalds 已提交
952 953 954 955 956 957
	/* initialize our wait queues */
	init_waitqueue_head(&edge_port->wait_open);
	init_waitqueue_head(&edge_port->wait_chase);
	init_waitqueue_head(&edge_port->wait_command);

	/* initialize our port settings */
A
Alan Cox 已提交
958 959 960
	edge_port->txCredits = 0;	/* Can't send any data yet */
	/* Must always set this bit to enable ints! */
	edge_port->shadowMCR = MCR_MASTER_IE;
961
	edge_port->chaseResponsePending = false;
L
Linus Torvalds 已提交
962 963

	/* send a open port command */
964 965
	edge_port->openPending = true;
	edge_port->open        = false;
A
Alan Cox 已提交
966
	response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
L
Linus Torvalds 已提交
967 968

	if (response < 0) {
969
		dev_err(dev, "%s - error sending open port command\n", __func__);
970
		edge_port->openPending = false;
L
Linus Torvalds 已提交
971 972 973 974
		return -ENODEV;
	}

	/* now wait for the port to be completely opened */
A
Alan Cox 已提交
975 976
	wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
								OPEN_TIMEOUT);
L
Linus Torvalds 已提交
977

978
	if (!edge_port->open) {
L
Linus Torvalds 已提交
979
		/* open timed out */
980
		dev_dbg(dev, "%s - open timedout\n", __func__);
981
		edge_port->openPending = false;
L
Linus Torvalds 已提交
982 983 984 985 986 987 988 989
		return -ENODEV;
	}

	/* create the txfifo */
	edge_port->txfifo.head	= 0;
	edge_port->txfifo.tail	= 0;
	edge_port->txfifo.count	= 0;
	edge_port->txfifo.size	= edge_port->maxTxCredits;
A
Alan Cox 已提交
990
	edge_port->txfifo.fifo	= kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
L
Linus Torvalds 已提交
991 992

	if (!edge_port->txfifo.fifo) {
993
		edge_close(port);
L
Linus Torvalds 已提交
994 995 996 997
		return -ENOMEM;
	}

	/* Allocate a URB for the write */
A
Alan Cox 已提交
998
	edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
999
	edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
1000 1001

	if (!edge_port->write_urb) {
1002
		edge_close(port);
L
Linus Torvalds 已提交
1003 1004 1005
		return -ENOMEM;
	}

1006 1007
	dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
		__func__, edge_port->maxTxCredits);
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

	return 0;
}


/************************************************************************
 *
 * block_until_chase_response
 *
 *	This function will block the close until one of the following:
 *		1. Response to our Chase comes from Edgeport
J
Joe Perches 已提交
1019
 *		2. A timeout of 10 seconds without activity has expired
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024
 *		   (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
 *
 ************************************************************************/
static void block_until_chase_response(struct edgeport_port *edge_port)
{
1025
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031
	DEFINE_WAIT(wait);
	__u16 lastCredits;
	int timeout = 1*HZ;
	int loop = 10;

	while (1) {
A
Alan Cox 已提交
1032
		/* Save Last credits */
L
Linus Torvalds 已提交
1033 1034
		lastCredits = edge_port->txCredits;

A
Alan Cox 已提交
1035
		/* Did we get our Chase response */
1036
		if (!edge_port->chaseResponsePending) {
1037
			dev_dbg(dev, "%s - Got Chase Response\n", __func__);
L
Linus Torvalds 已提交
1038

A
Alan Cox 已提交
1039 1040
			/* did we get all of our credit back? */
			if (edge_port->txCredits == edge_port->maxTxCredits) {
1041
				dev_dbg(dev, "%s - Got all credits\n", __func__);
L
Linus Torvalds 已提交
1042 1043 1044 1045
				return;
			}
		}

A
Alan Cox 已提交
1046 1047 1048
		/* Block the thread for a while */
		prepare_to_wait(&edge_port->wait_chase, &wait,
						TASK_UNINTERRUPTIBLE);
L
Linus Torvalds 已提交
1049 1050 1051 1052
		schedule_timeout(timeout);
		finish_wait(&edge_port->wait_chase, &wait);

		if (lastCredits == edge_port->txCredits) {
A
Alan Cox 已提交
1053
			/* No activity.. count down. */
L
Linus Torvalds 已提交
1054 1055
			loop--;
			if (loop == 0) {
1056
				edge_port->chaseResponsePending = false;
1057
				dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
L
Linus Torvalds 已提交
1058 1059 1060
				return;
			}
		} else {
A
Alan Cox 已提交
1061
			/* Reset timeout value back to 10 seconds */
1062
			dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
A
Alan Cox 已提交
1063
					lastCredits, edge_port->txCredits);
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
			loop = 10;
		}
	}
}


/************************************************************************
 *
 * block_until_tx_empty
 *
 *	This function will block the close until one of the following:
 *		1. TX count are 0
 *		2. The edgeport has stopped
J
Joe Perches 已提交
1077
 *		3. A timeout of 3 seconds without activity has expired
L
Linus Torvalds 已提交
1078 1079
 *
 ************************************************************************/
A
Alan Cox 已提交
1080
static void block_until_tx_empty(struct edgeport_port *edge_port)
L
Linus Torvalds 已提交
1081
{
1082
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1083 1084 1085 1086 1087 1088 1089
	DEFINE_WAIT(wait);
	struct TxFifo *fifo = &edge_port->txfifo;
	__u32 lastCount;
	int timeout = HZ/10;
	int loop = 30;

	while (1) {
A
Alan Cox 已提交
1090
		/* Save Last count */
L
Linus Torvalds 已提交
1091 1092
		lastCount = fifo->count;

A
Alan Cox 已提交
1093
		/* Is the Edgeport Buffer empty? */
L
Linus Torvalds 已提交
1094
		if (lastCount == 0) {
1095
			dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
L
Linus Torvalds 已提交
1096 1097 1098
			return;
		}

A
Alan Cox 已提交
1099 1100 1101
		/* Block the thread for a while */
		prepare_to_wait(&edge_port->wait_chase, &wait,
						TASK_UNINTERRUPTIBLE);
L
Linus Torvalds 已提交
1102 1103 1104
		schedule_timeout(timeout);
		finish_wait(&edge_port->wait_chase, &wait);

1105
		dev_dbg(dev, "%s wait\n", __func__);
L
Linus Torvalds 已提交
1106 1107

		if (lastCount == fifo->count) {
A
Alan Cox 已提交
1108
			/* No activity.. count down. */
L
Linus Torvalds 已提交
1109 1110
			loop--;
			if (loop == 0) {
1111
				dev_dbg(dev, "%s - TIMEOUT\n", __func__);
L
Linus Torvalds 已提交
1112 1113 1114
				return;
			}
		} else {
A
Alan Cox 已提交
1115
			/* Reset timeout value back to seconds */
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
			loop = 30;
		}
	}
}


/*****************************************************************************
 * edge_close
 *	this function is called by the tty driver when a port is closed
 *****************************************************************************/
1126
static void edge_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133
{
	struct edgeport_serial *edge_serial;
	struct edgeport_port *edge_port;
	int status;

	edge_serial = usb_get_serial_data(port->serial);
	edge_port = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1134
	if (edge_serial == NULL || edge_port == NULL)
L
Linus Torvalds 已提交
1135
		return;
A
Alan Cox 已提交
1136 1137

	/* block until tx is empty */
L
Linus Torvalds 已提交
1138 1139
	block_until_tx_empty(edge_port);

1140
	edge_port->closePending = true;
L
Linus Torvalds 已提交
1141

1142 1143
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPChase) {
1144
		/* flush and chase */
1145
		edge_port->chaseResponsePending = true;
1146

1147
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
A
Alan Cox 已提交
1148 1149 1150
		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
		if (status == 0)
			/* block until chase finished */
1151
			block_until_chase_response(edge_port);
A
Alan Cox 已提交
1152
		else
1153
			edge_port->chaseResponsePending = false;
L
Linus Torvalds 已提交
1154 1155
	}

1156 1157
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPClose) {
1158
	       /* close the port */
1159
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
A
Alan Cox 已提交
1160
		send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1161
	}
L
Linus Torvalds 已提交
1162

A
Alan Cox 已提交
1163
	/* port->close = true; */
1164 1165 1166
	edge_port->closePending = false;
	edge_port->open = false;
	edge_port->openPending = false;
L
Linus Torvalds 已提交
1167

1168
	usb_kill_urb(edge_port->write_urb);
L
Linus Torvalds 已提交
1169 1170

	if (edge_port->write_urb) {
A
Alan Cox 已提交
1171 1172
		/* if this urb had a transfer buffer already
				(old transfer) free it */
1173 1174
		kfree(edge_port->write_urb->transfer_buffer);
		usb_free_urb(edge_port->write_urb);
L
Linus Torvalds 已提交
1175 1176
		edge_port->write_urb = NULL;
	}
1177 1178
	kfree(edge_port->txfifo.fifo);
	edge_port->txfifo.fifo = NULL;
A
Alan Cox 已提交
1179
}
L
Linus Torvalds 已提交
1180 1181 1182

/*****************************************************************************
 * SerialWrite
A
Alan Cox 已提交
1183 1184 1185 1186
 *	this function is called by the tty driver when data should be written
 *	to the port.
 *	If successful, we return the number of bytes written, otherwise we
 *	return a negative error number.
L
Linus Torvalds 已提交
1187
 *****************************************************************************/
A
Alan Cox 已提交
1188 1189
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *data, int count)
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	struct TxFifo *fifo;
	int copySize;
	int bytesleft;
	int firsthalf;
	int secondhalf;
	unsigned long flags;

	if (edge_port == NULL)
		return -ENODEV;

A
Alan Cox 已提交
1202
	/* get a pointer to the Tx fifo */
L
Linus Torvalds 已提交
1203 1204 1205 1206
	fifo = &edge_port->txfifo;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

A
Alan Cox 已提交
1207 1208 1209
	/* calculate number of bytes to put in fifo */
	copySize = min((unsigned int)count,
				(edge_port->txCredits - fifo->count));
L
Linus Torvalds 已提交
1210

1211 1212
	dev_dbg(&port->dev, "%s of %d byte(s) Fifo room  %d -- will copy %d bytes\n",
		__func__, count, edge_port->txCredits - fifo->count, copySize);
L
Linus Torvalds 已提交
1213

A
Alan Cox 已提交
1214 1215
	/* catch writes of 0 bytes which the tty driver likes to give us,
	   and when txCredits is empty */
L
Linus Torvalds 已提交
1216
	if (copySize == 0) {
1217
		dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
L
Linus Torvalds 已提交
1218 1219 1220
		goto finish_write;
	}

A
Alan Cox 已提交
1221 1222 1223 1224 1225 1226 1227
	/* queue the data
	 * since we can never overflow the buffer we do not have to check for a
	 * full condition
	 *
	 * the copy is done is two parts -- first fill to the end of the buffer
	 * then copy the reset from the start of the buffer
	 */
L
Linus Torvalds 已提交
1228
	bytesleft = fifo->size - fifo->head;
A
Alan Cox 已提交
1229
	firsthalf = min(bytesleft, copySize);
1230 1231
	dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
		firsthalf, bytesleft);
L
Linus Torvalds 已提交
1232 1233 1234

	/* now copy our data */
	memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1235
	usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
L
Linus Torvalds 已提交
1236

A
Alan Cox 已提交
1237
	/* update the index and size */
L
Linus Torvalds 已提交
1238 1239 1240
	fifo->head  += firsthalf;
	fifo->count += firsthalf;

A
Alan Cox 已提交
1241 1242
	/* wrap the index */
	if (fifo->head == fifo->size)
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247
		fifo->head = 0;

	secondhalf = copySize-firsthalf;

	if (secondhalf) {
1248
		dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
L
Linus Torvalds 已提交
1249
		memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1250
		usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
A
Alan Cox 已提交
1251
		/* update the index and size */
L
Linus Torvalds 已提交
1252 1253
		fifo->count += secondhalf;
		fifo->head  += secondhalf;
A
Alan Cox 已提交
1254 1255 1256
		/* No need to check for wrap since we can not get to end of
		 * the fifo in this part
		 */
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261
	}

finish_write:
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

A
Alan Cox 已提交
1262 1263
	send_more_port_data((struct edgeport_serial *)
			usb_get_serial_data(port->serial), edge_port);
L
Linus Torvalds 已提交
1264

1265 1266
	dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
		__func__, copySize, edge_port->txCredits, fifo->count);
L
Linus Torvalds 已提交
1267

A
Alan Cox 已提交
1268
	return copySize;
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
}


/************************************************************************
 *
 * send_more_port_data()
 *
 *	This routine attempts to write additional UART transmit data
 *	to a port over the USB bulk pipe. It is called (1) when new
 *	data has been written to a port's TxBuffer from higher layers
 *	(2) when the peripheral sends us additional TxCredits indicating
 *	that it can accept more	Tx data for a given port; and (3) when
 *	a bulk write completes successfully and we want to see if we
 *	can transmit more.
 *
 ************************************************************************/
A
Alan Cox 已提交
1285 1286
static void send_more_port_data(struct edgeport_serial *edge_serial,
					struct edgeport_port *edge_port)
L
Linus Torvalds 已提交
1287 1288
{
	struct TxFifo	*fifo = &edge_port->txfifo;
1289
	struct device	*dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	struct urb	*urb;
	unsigned char	*buffer;
	int		status;
	int		count;
	int		bytesleft;
	int		firsthalf;
	int		secondhalf;
	unsigned long	flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

	if (edge_port->write_in_progress ||
	    !edge_port->open             ||
	    (fifo->count == 0)) {
1304 1305
		dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
			__func__, fifo->count, edge_port->write_in_progress);
L
Linus Torvalds 已提交
1306 1307 1308
		goto exit_send;
	}

A
Alan Cox 已提交
1309 1310 1311 1312 1313 1314 1315 1316
	/* since the amount of data in the fifo will always fit into the
	 * edgeport buffer we do not need to check the write length
	 *
	 * Do we have enough credits for this port to make it worthwhile
	 * to bother queueing a write. If it's too small, say a few bytes,
	 * it's better to wait for more credits so we can do a larger write.
	 */
	if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1317 1318
		dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
			__func__, fifo->count, edge_port->txCredits);
L
Linus Torvalds 已提交
1319 1320 1321
		goto exit_send;
	}

A
Alan Cox 已提交
1322
	/* lock this write */
1323
	edge_port->write_in_progress = true;
L
Linus Torvalds 已提交
1324

A
Alan Cox 已提交
1325
	/* get a pointer to the write_urb */
L
Linus Torvalds 已提交
1326 1327
	urb = edge_port->write_urb;

1328 1329 1330
	/* make sure transfer buffer is freed */
	kfree(urb->transfer_buffer);
	urb->transfer_buffer = NULL;
L
Linus Torvalds 已提交
1331

A
Alan Cox 已提交
1332 1333
	/* build the data header for the buffer and port that we are about
	   to send out */
L
Linus Torvalds 已提交
1334
	count = fifo->count;
A
Alan Cox 已提交
1335
	buffer = kmalloc(count+2, GFP_ATOMIC);
1336
	if (!buffer) {
1337
		edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
1338 1339
		goto exit_send;
	}
1340 1341
	buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
	buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
L
Linus Torvalds 已提交
1342 1343 1344

	/* now copy our data */
	bytesleft =  fifo->size - fifo->tail;
A
Alan Cox 已提交
1345
	firsthalf = min(bytesleft, count);
L
Linus Torvalds 已提交
1346 1347 1348
	memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
	fifo->tail  += firsthalf;
	fifo->count -= firsthalf;
A
Alan Cox 已提交
1349
	if (fifo->tail == fifo->size)
L
Linus Torvalds 已提交
1350 1351 1352 1353
		fifo->tail = 0;

	secondhalf = count-firsthalf;
	if (secondhalf) {
A
Alan Cox 已提交
1354 1355
		memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
								secondhalf);
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360
		fifo->tail  += secondhalf;
		fifo->count -= secondhalf;
	}

	if (count)
1361
		usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
L
Linus Torvalds 已提交
1362 1363

	/* fill up the urb with all of our data and submit it */
A
Alan Cox 已提交
1364 1365 1366 1367 1368
	usb_fill_bulk_urb(urb, edge_serial->serial->dev,
			usb_sndbulkpipe(edge_serial->serial->dev,
					edge_serial->bulk_out_endpoint),
			buffer, count+2,
			edge_bulk_out_data_callback, edge_port);
L
Linus Torvalds 已提交
1369 1370 1371

	/* decrement the number of credits we have by the number we just sent */
	edge_port->txCredits -= count;
1372
	edge_port->port->icount.tx += count;
L
Linus Torvalds 已提交
1373 1374 1375 1376

	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status) {
		/* something went wrong */
1377
		dev_err_console(edge_port->port,
A
Alan Cox 已提交
1378 1379
			"%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
				__func__, status);
1380
		edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
1381 1382 1383

		/* revert the credits as something bad happened. */
		edge_port->txCredits += count;
1384
		edge_port->port->icount.tx -= count;
L
Linus Torvalds 已提交
1385
	}
1386 1387
	dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
		__func__, count, edge_port->txCredits, fifo->count);
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395

exit_send:
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
}


/*****************************************************************************
 * edge_write_room
A
Alan Cox 已提交
1396 1397 1398 1399
 *	this function is called by the tty driver when it wants to know how
 *	many bytes of data we can accept for a specific port. If successful,
 *	we return the amount of room that we have for this port	(the txCredits)
 *	otherwise we return a negative error number.
L
Linus Torvalds 已提交
1400
 *****************************************************************************/
A
Alan Cox 已提交
1401
static int edge_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1402
{
A
Alan Cox 已提交
1403
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room;
	unsigned long flags;

	if (edge_port == NULL)
1409
		return 0;
1410
	if (edge_port->closePending)
1411
		return 0;
L
Linus Torvalds 已提交
1412 1413

	if (!edge_port->open) {
1414
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1415
		return 0;
L
Linus Torvalds 已提交
1416 1417
	}

A
Alan Cox 已提交
1418
	/* total of both buffers is still txCredit */
L
Linus Torvalds 已提交
1419 1420 1421 1422
	spin_lock_irqsave(&edge_port->ep_lock, flags);
	room = edge_port->txCredits - edge_port->txfifo.count;
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

1423
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429
	return room;
}


/*****************************************************************************
 * edge_chars_in_buffer
A
Alan Cox 已提交
1430 1431 1432 1433 1434
 *	this function is called by the tty driver when it wants to know how
 *	many bytes of data we currently have outstanding in the port (data that
 *	has been written, but hasn't made it out the port yet)
 *	If successful, we return the number of bytes left to be written in the
 *	system,
L
Linus Torvalds 已提交
1435 1436
 *	Otherwise we return a negative error number.
 *****************************************************************************/
A
Alan Cox 已提交
1437
static int edge_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1438
{
A
Alan Cox 已提交
1439
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1440 1441 1442 1443 1444
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int num_chars;
	unsigned long flags;

	if (edge_port == NULL)
1445
		return 0;
1446
	if (edge_port->closePending)
1447
		return 0;
L
Linus Torvalds 已提交
1448 1449

	if (!edge_port->open) {
1450
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1451
		return 0;
L
Linus Torvalds 已提交
1452 1453 1454
	}

	spin_lock_irqsave(&edge_port->ep_lock, flags);
A
Alan Cox 已提交
1455 1456
	num_chars = edge_port->maxTxCredits - edge_port->txCredits +
						edge_port->txfifo.count;
L
Linus Torvalds 已提交
1457 1458
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
	if (num_chars) {
1459
		dev_dbg(&port->dev, "%s - returns %d\n", __func__, num_chars);
L
Linus Torvalds 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	}

	return num_chars;
}


/*****************************************************************************
 * SerialThrottle
 *	this function is called by the tty driver when it wants to stop the data
 *	being read from the port.
 *****************************************************************************/
A
Alan Cox 已提交
1471
static void edge_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1472
{
A
Alan Cox 已提交
1473
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	if (edge_port == NULL)
		return;

	if (!edge_port->open) {
1481
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487
		return;
	}

	/* if we are implementing XON/XOFF, send the stop character */
	if (I_IXOFF(tty)) {
		unsigned char stop_char = STOP_CHAR(tty);
A
Alan Cox 已提交
1488 1489
		status = edge_write(tty, port, &stop_char, 1);
		if (status <= 0)
L
Linus Torvalds 已提交
1490 1491 1492 1493
			return;
	}

	/* if we are implementing RTS/CTS, toggle that line */
P
Peter Hurley 已提交
1494
	if (C_CRTSCTS(tty)) {
L
Linus Torvalds 已提交
1495
		edge_port->shadowMCR &= ~MCR_RTS;
A
Alan Cox 已提交
1496 1497 1498
		status = send_cmd_write_uart_register(edge_port, MCR,
							edge_port->shadowMCR);
		if (status != 0)
L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505
			return;
	}
}


/*****************************************************************************
 * edge_unthrottle
A
Alan Cox 已提交
1506 1507
 *	this function is called by the tty driver when it wants to resume the
 *	data being read from the port (called after SerialThrottle is called)
L
Linus Torvalds 已提交
1508
 *****************************************************************************/
A
Alan Cox 已提交
1509
static void edge_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1510
{
A
Alan Cox 已提交
1511
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	if (edge_port == NULL)
		return;

	if (!edge_port->open) {
1519
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525
		return;
	}

	/* if we are implementing XON/XOFF, send the start character */
	if (I_IXOFF(tty)) {
		unsigned char start_char = START_CHAR(tty);
A
Alan Cox 已提交
1526 1527
		status = edge_write(tty, port, &start_char, 1);
		if (status <= 0)
L
Linus Torvalds 已提交
1528 1529 1530
			return;
	}
	/* if we are implementing RTS/CTS, toggle that line */
P
Peter Hurley 已提交
1531
	if (C_CRTSCTS(tty)) {
L
Linus Torvalds 已提交
1532
		edge_port->shadowMCR |= MCR_RTS;
A
Alan Cox 已提交
1533 1534
		send_cmd_write_uart_register(edge_port, MCR,
						edge_port->shadowMCR);
L
Linus Torvalds 已提交
1535 1536 1537 1538 1539 1540
	}
}


/*****************************************************************************
 * SerialSetTermios
A
Alan Cox 已提交
1541 1542
 *	this function is called by the tty driver when it wants to change
 * the termios structure
L
Linus Torvalds 已提交
1543
 *****************************************************************************/
A
Alan Cox 已提交
1544 1545
static void edge_set_termios(struct tty_struct *tty,
	struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1546 1547 1548 1549 1550 1551 1552
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	if (edge_port == NULL)
		return;

	if (!edge_port->open) {
1553
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
1554 1555 1556 1557
		return;
	}

	/* change the port settings to the new ones specified */
A
Alan Cox 已提交
1558
	change_port_settings(tty, edge_port, old_termios);
L
Linus Torvalds 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
}


/*****************************************************************************
 * get_lsr_info - get line status register info
 *
 * Purpose: Let user call ioctl() to get info when the UART physically
 * 	    is emptied.  On bus types like RS485, the transmitter must
 * 	    release the bus after transmitting. This must be done when
 * 	    the transmit shift register is empty, not be done when the
 * 	    transmit holding register is empty.  This functionality
A
Alan Cox 已提交
1570
 * 	    allows an RS485 driver to be written in user space.
L
Linus Torvalds 已提交
1571
 *****************************************************************************/
A
Alan Cox 已提交
1572 1573
static int get_lsr_info(struct edgeport_port *edge_port,
						unsigned int __user *value)
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578 1579 1580
{
	unsigned int result = 0;
	unsigned long flags;

	spin_lock_irqsave(&edge_port->ep_lock, flags);
	if (edge_port->maxTxCredits == edge_port->txCredits &&
	    edge_port->txfifo.count == 0) {
1581
		dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590
		result = TIOCSER_TEMT;
	}
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

	if (copy_to_user(value, &result, sizeof(int)))
		return -EFAULT;
	return 0;
}

1591
static int edge_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
1592
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1593
{
A
Alan Cox 已提交
1594
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int mcr;

	mcr = edge_port->shadowMCR;
	if (set & TIOCM_RTS)
		mcr |= MCR_RTS;
	if (set & TIOCM_DTR)
		mcr |= MCR_DTR;
	if (set & TIOCM_LOOP)
		mcr |= MCR_LOOPBACK;

	if (clear & TIOCM_RTS)
		mcr &= ~MCR_RTS;
	if (clear & TIOCM_DTR)
		mcr &= ~MCR_DTR;
	if (clear & TIOCM_LOOP)
		mcr &= ~MCR_LOOPBACK;

	edge_port->shadowMCR = mcr;

	send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);

	return 0;
}

1620
static int edge_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
1621
{
A
Alan Cox 已提交
1622
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int result = 0;
	unsigned int msr;
	unsigned int mcr;

	msr = edge_port->shadowMSR;
	mcr = edge_port->shadowMCR;
	result = ((mcr & MCR_DTR)	? TIOCM_DTR: 0)	  /* 0x002 */
		  | ((mcr & MCR_RTS)	? TIOCM_RTS: 0)   /* 0x004 */
		  | ((msr & EDGEPORT_MSR_CTS)	? TIOCM_CTS: 0)   /* 0x020 */
		  | ((msr & EDGEPORT_MSR_CD)	? TIOCM_CAR: 0)   /* 0x040 */
		  | ((msr & EDGEPORT_MSR_RI)	? TIOCM_RI:  0)   /* 0x080 */
		  | ((msr & EDGEPORT_MSR_DSR)	? TIOCM_DSR: 0);  /* 0x100 */

	return result;
}

A
Al Viro 已提交
1640 1641
static int get_serial_info(struct tty_struct *tty,
				struct serial_struct *ss)
L
Linus Torvalds 已提交
1642
{
A
Al Viro 已提交
1643 1644
	struct usb_serial_port *port = tty->driver_data;
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
1645

A
Al Viro 已提交
1646 1647 1648 1649 1650 1651 1652 1653
	ss->type		= PORT_16550A;
	ss->line		= edge_port->port->minor;
	ss->port		= edge_port->port->port_number;
	ss->irq			= 0;
	ss->xmit_fifo_size	= edge_port->maxTxCredits;
	ss->baud_base		= 9600;
	ss->close_delay		= 5*HZ;
	ss->closing_wait	= 30*HZ;
L
Linus Torvalds 已提交
1654 1655 1656 1657 1658 1659 1660 1661
	return 0;
}


/*****************************************************************************
 * SerialIoctl
 *	this function handles any ioctl calls to the driver
 *****************************************************************************/
1662
static int edge_ioctl(struct tty_struct *tty,
A
Alan Cox 已提交
1663
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1664
{
A
Alan Cox 已提交
1665
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1666 1667 1668
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

	switch (cmd) {
A
Alan Cox 已提交
1669
	case TIOCSERGETLSR:
1670
		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
A
Alan Cox 已提交
1671
		return get_lsr_info(edge_port, (unsigned int __user *) arg);
L
Linus Torvalds 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680
	}
	return -ENOIOCTLCMD;
}


/*****************************************************************************
 * SerialBreak
 *	this function sends a break to the port
 *****************************************************************************/
A
Alan Cox 已提交
1681
static void edge_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
1682
{
A
Alan Cox 已提交
1683
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1684
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1685
	struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
L
Linus Torvalds 已提交
1686 1687
	int status;

1688 1689
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPChase) {
1690
		/* flush and chase */
1691
		edge_port->chaseResponsePending = true;
1692

1693
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
A
Alan Cox 已提交
1694
		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1695
		if (status == 0) {
A
Alan Cox 已提交
1696
			/* block until chase finished */
1697 1698
			block_until_chase_response(edge_port);
		} else {
1699
			edge_port->chaseResponsePending = false;
1700
		}
L
Linus Torvalds 已提交
1701 1702
	}

1703 1704
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPSetClrBreak) {
1705
		if (break_state == -1) {
1706
			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
A
Alan Cox 已提交
1707 1708
			status = send_iosp_ext_cmd(edge_port,
						IOSP_CMD_SET_BREAK, 0);
1709
		} else {
1710
			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
A
Alan Cox 已提交
1711 1712
			status = send_iosp_ext_cmd(edge_port,
						IOSP_CMD_CLEAR_BREAK, 0);
1713
		}
A
Alan Cox 已提交
1714
		if (status)
1715
			dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
A
Alan Cox 已提交
1716
				__func__);
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721 1722 1723 1724
	}
}


/*****************************************************************************
 * process_rcvd_data
 *	this function handles the data received on the bulk in pipe.
 *****************************************************************************/
A
Alan Cox 已提交
1725 1726
static void process_rcvd_data(struct edgeport_serial *edge_serial,
				unsigned char *buffer, __u16 bufferLength)
L
Linus Torvalds 已提交
1727
{
1728
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
	__u16 lastBufferLength;
	__u16 rxLen;

	lastBufferLength = bufferLength + 1;

	while (bufferLength > 0) {
		/* failsafe incase we get a message that we don't understand */
		if (lastBufferLength == bufferLength) {
1739
			dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744
			break;
		}
		lastBufferLength = bufferLength;

		switch (edge_serial->rxState) {
A
Alan Cox 已提交
1745 1746 1747 1748
		case EXPECT_HDR1:
			edge_serial->rxHeader1 = *buffer;
			++buffer;
			--bufferLength;
L
Linus Torvalds 已提交
1749

A
Alan Cox 已提交
1750 1751 1752 1753
			if (bufferLength == 0) {
				edge_serial->rxState = EXPECT_HDR2;
				break;
			}
1754
			/* Fall through */
A
Alan Cox 已提交
1755 1756 1757 1758 1759
		case EXPECT_HDR2:
			edge_serial->rxHeader2 = *buffer;
			++buffer;
			--bufferLength;

1760 1761
			dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
				edge_serial->rxHeader1, edge_serial->rxHeader2);
A
Alan Cox 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
			/* Process depending on whether this header is
			 * data or status */

			if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
				/* Decode this status header and go to
				 * EXPECT_HDR1 (if we can process the status
				 * with only 2 bytes), or go to EXPECT_HDR3 to
				 * get the third byte. */
				edge_serial->rxPort =
				    IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
				edge_serial->rxStatusCode =
				    IOSP_GET_STATUS_CODE(
						edge_serial->rxHeader1);

				if (!IOSP_STATUS_IS_2BYTE(
						edge_serial->rxStatusCode)) {
					/* This status needs additional bytes.
					 * Save what we have and then wait for
					 * more data.
					 */
					edge_serial->rxStatusParam
						= edge_serial->rxHeader2;
					edge_serial->rxState = EXPECT_HDR3;
L
Linus Torvalds 已提交
1785 1786
					break;
				}
A
Alan Cox 已提交
1787 1788 1789 1790 1791 1792 1793
				/* We have all the header bytes, process the
				   status now */
				process_rcvd_status(edge_serial,
						edge_serial->rxHeader2, 0);
				edge_serial->rxState = EXPECT_HDR1;
				break;
			}
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806

			edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
			edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1,
									      edge_serial->rxHeader2);
			dev_dbg(dev, "%s - Data for Port %u Len %u\n", __func__,
				edge_serial->rxPort,
				edge_serial->rxBytesRemaining);

			if (bufferLength == 0) {
				edge_serial->rxState = EXPECT_DATA;
				break;
			}
			/* Fall through */
A
Alan Cox 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
		case EXPECT_DATA: /* Expect data */
			if (bufferLength < edge_serial->rxBytesRemaining) {
				rxLen = bufferLength;
				/* Expect data to start next buffer */
				edge_serial->rxState = EXPECT_DATA;
			} else {
				/* BufLen >= RxBytesRemaining */
				rxLen = edge_serial->rxBytesRemaining;
				/* Start another header next time */
				edge_serial->rxState = EXPECT_HDR1;
			}
L
Linus Torvalds 已提交
1818

A
Alan Cox 已提交
1819 1820
			bufferLength -= rxLen;
			edge_serial->rxBytesRemaining -= rxLen;
L
Linus Torvalds 已提交
1821

A
Alan Cox 已提交
1822 1823 1824 1825 1826 1827 1828
			/* spit this data back into the tty driver if this
			   port is open */
			if (rxLen) {
				port = edge_serial->serial->port[
							edge_serial->rxPort];
				edge_port = usb_get_serial_port_data(port);
				if (edge_port->open) {
J
Jiri Slaby 已提交
1829 1830 1831 1832 1833
					dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
						__func__, rxLen,
						edge_serial->rxPort);
					edge_tty_recv(edge_port->port, buffer,
							rxLen);
1834
					edge_port->port->icount.rx += rxLen;
L
Linus Torvalds 已提交
1835
				}
A
Alan Cox 已提交
1836 1837 1838
				buffer += rxLen;
			}
			break;
L
Linus Torvalds 已提交
1839

A
Alan Cox 已提交
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
		case EXPECT_HDR3:	/* Expect 3rd byte of status header */
			edge_serial->rxHeader3 = *buffer;
			++buffer;
			--bufferLength;

			/* We have all the header bytes, process the
			   status now */
			process_rcvd_status(edge_serial,
				edge_serial->rxStatusParam,
				edge_serial->rxHeader3);
			edge_serial->rxState = EXPECT_HDR1;
			break;
L
Linus Torvalds 已提交
1852 1853 1854 1855 1856 1857 1858
		}
	}
}


/*****************************************************************************
 * process_rcvd_status
A
Alan Cox 已提交
1859 1860
 *	this function handles the any status messages received on the
 *	bulk in pipe.
L
Linus Torvalds 已提交
1861
 *****************************************************************************/
A
Alan Cox 已提交
1862 1863
static void process_rcvd_status(struct edgeport_serial *edge_serial,
						__u8 byte2, __u8 byte3)
L
Linus Torvalds 已提交
1864 1865 1866
{
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
A
Alan Cox 已提交
1867
	struct tty_struct *tty;
1868
	struct device *dev;
L
Linus Torvalds 已提交
1869 1870 1871 1872 1873 1874
	__u8 code = edge_serial->rxStatusCode;

	/* switch the port pointer to the one being currently talked about */
	port = edge_serial->serial->port[edge_serial->rxPort];
	edge_port = usb_get_serial_port_data(port);
	if (edge_port == NULL) {
A
Alan Cox 已提交
1875 1876 1877
		dev_err(&edge_serial->serial->dev->dev,
			"%s - edge_port == NULL for port %d\n",
					__func__, edge_serial->rxPort);
L
Linus Torvalds 已提交
1878 1879
		return;
	}
1880
	dev = &port->dev;
L
Linus Torvalds 已提交
1881 1882 1883

	if (code == IOSP_EXT_STATUS) {
		switch (byte2) {
A
Alan Cox 已提交
1884 1885 1886
		case IOSP_EXT_STATUS_CHASE_RSP:
			/* we want to do EXT status regardless of port
			 * open/closed */
1887 1888
			dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
				__func__, edge_serial->rxPort, byte3);
A
Alan Cox 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
			/* Currently, the only EXT_STATUS is Chase, so process
			 * here instead of one more call to one more subroutine
			 * If/when more EXT_STATUS, there'll be more work to do
			 * Also, we currently clear flag and close the port
			 * regardless of content of above's Byte3.
			 * We could choose to do something else when Byte3 says
			 * Timeout on Chase from Edgeport, like wait longer in
			 * block_until_chase_response, but for now we don't.
			 */
			edge_port->chaseResponsePending = false;
			wake_up(&edge_port->wait_chase);
			return;
L
Linus Torvalds 已提交
1901

A
Alan Cox 已提交
1902
		case IOSP_EXT_STATUS_RX_CHECK_RSP:
1903 1904
			dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
				__func__, edge_serial->rxPort, byte3);
A
Alan Cox 已提交
1905 1906
			/* Port->RxCheckRsp = true; */
			return;
L
Linus Torvalds 已提交
1907 1908 1909 1910 1911 1912
		}
	}

	if (code == IOSP_STATUS_OPEN_RSP) {
		edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
		edge_port->maxTxCredits = edge_port->txCredits;
1913 1914
		dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
			__func__, edge_serial->rxPort, byte2, edge_port->txCredits);
A
Alan Cox 已提交
1915
		handle_new_msr(edge_port, byte2);
L
Linus Torvalds 已提交
1916

A
Alan Cox 已提交
1917 1918
		/* send the current line settings to the port so we are
		   in sync with any further termios calls */
A
Alan Cox 已提交
1919 1920 1921
		tty = tty_port_tty_get(&edge_port->port->port);
		if (tty) {
			change_port_settings(tty,
1922
				edge_port, &tty->termios);
A
Alan Cox 已提交
1923 1924
			tty_kref_put(tty);
		}
L
Linus Torvalds 已提交
1925 1926

		/* we have completed the open */
1927 1928
		edge_port->openPending = false;
		edge_port->open = true;
L
Linus Torvalds 已提交
1929 1930 1931 1932
		wake_up(&edge_port->wait_open);
		return;
	}

A
Alan Cox 已提交
1933 1934 1935 1936 1937
	/* If port is closed, silently discard all rcvd status. We can
	 * have cases where buffered status is received AFTER the close
	 * port command is sent to the Edgeport.
	 */
	if (!edge_port->open || edge_port->closePending)
L
Linus Torvalds 已提交
1938 1939 1940
		return;

	switch (code) {
A
Alan Cox 已提交
1941 1942
	/* Not currently sent by Edgeport */
	case IOSP_STATUS_LSR:
1943 1944
		dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
			__func__, edge_serial->rxPort, byte2);
A
Alan Cox 已提交
1945 1946
		handle_new_lsr(edge_port, false, byte2, 0);
		break;
L
Linus Torvalds 已提交
1947

A
Alan Cox 已提交
1948
	case IOSP_STATUS_LSR_DATA:
1949 1950
		dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
			__func__, edge_serial->rxPort, byte2, byte3);
A
Alan Cox 已提交
1951 1952 1953 1954 1955 1956
		/* byte2 is LSR Register */
		/* byte3 is broken data byte */
		handle_new_lsr(edge_port, true, byte2, byte3);
		break;
	/*
	 *	case IOSP_EXT_4_STATUS:
1957
	 *		dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
A
Alan Cox 已提交
1958 1959 1960 1961
	 *			__func__, edge_serial->rxPort, byte2, byte3);
	 *		break;
	 */
	case IOSP_STATUS_MSR:
1962 1963
		dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
			__func__, edge_serial->rxPort, byte2);
A
Alan Cox 已提交
1964 1965 1966 1967 1968 1969 1970
		/*
		 * Process this new modem status and generate appropriate
		 * events, etc, based on the new status. This routine
		 * also saves the MSR in Port->ShadowMsr.
		 */
		handle_new_msr(edge_port, byte2);
		break;
L
Linus Torvalds 已提交
1971

A
Alan Cox 已提交
1972
	default:
1973
		dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
A
Alan Cox 已提交
1974
		break;
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981 1982
	}
}


/*****************************************************************************
 * edge_tty_recv
 *	this function passes data on to the tty flip buffer
 *****************************************************************************/
J
Jiri Slaby 已提交
1983 1984
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length)
L
Linus Torvalds 已提交
1985 1986 1987
{
	int cnt;

J
Jiri Slaby 已提交
1988
	cnt = tty_insert_flip_string(&port->port, data, length);
1989
	if (cnt < length) {
J
Jiri Slaby 已提交
1990
		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1991 1992 1993 1994
				__func__, length - cnt);
	}
	data += cnt;
	length -= cnt;
L
Linus Torvalds 已提交
1995

J
Jiri Slaby 已提交
1996
	tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
}


/*****************************************************************************
 * handle_new_msr
 *	this function handles any change to the msr register for a port.
 *****************************************************************************/
static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
{
	struct  async_icount *icount;

A
Alan Cox 已提交
2008 2009
	if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2010
		icount = &edge_port->port->icount;
L
Linus Torvalds 已提交
2011 2012

		/* update input line counters */
A
Alan Cox 已提交
2013
		if (newMsr & EDGEPORT_MSR_DELTA_CTS)
L
Linus Torvalds 已提交
2014
			icount->cts++;
A
Alan Cox 已提交
2015
		if (newMsr & EDGEPORT_MSR_DELTA_DSR)
L
Linus Torvalds 已提交
2016
			icount->dsr++;
A
Alan Cox 已提交
2017
		if (newMsr & EDGEPORT_MSR_DELTA_CD)
L
Linus Torvalds 已提交
2018
			icount->dcd++;
A
Alan Cox 已提交
2019
		if (newMsr & EDGEPORT_MSR_DELTA_RI)
L
Linus Torvalds 已提交
2020
			icount->rng++;
2021
		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
L
Linus Torvalds 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
	}

	/* Save the new modem status */
	edge_port->shadowMSR = newMsr & 0xf0;
}


/*****************************************************************************
 * handle_new_lsr
 *	this function handles any change to the lsr register for a port.
 *****************************************************************************/
A
Alan Cox 已提交
2033 2034
static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
							__u8 lsr, __u8 data)
L
Linus Torvalds 已提交
2035
{
A
Alan Cox 已提交
2036 2037 2038
	__u8 newLsr = (__u8) (lsr & (__u8)
		(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
	struct async_icount *icount;
L
Linus Torvalds 已提交
2039 2040 2041 2042

	edge_port->shadowLSR = lsr;

	if (newLsr & LSR_BREAK) {
A
Alan Cox 已提交
2043 2044 2045 2046 2047
		/*
		 * Parity and Framing errors only count if they
		 * occur exclusive of a break being
		 * received.
		 */
L
Linus Torvalds 已提交
2048 2049 2050 2051
		newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
	}

	/* Place LSR data byte into Rx buffer */
J
Jiri Slaby 已提交
2052 2053 2054
	if (lsrData)
		edge_tty_recv(edge_port->port, &data, 1);

L
Linus Torvalds 已提交
2055
	/* update input line counters */
2056
	icount = &edge_port->port->icount;
A
Alan Cox 已提交
2057
	if (newLsr & LSR_BREAK)
L
Linus Torvalds 已提交
2058
		icount->brk++;
A
Alan Cox 已提交
2059
	if (newLsr & LSR_OVER_ERR)
L
Linus Torvalds 已提交
2060
		icount->overrun++;
A
Alan Cox 已提交
2061
	if (newLsr & LSR_PAR_ERR)
L
Linus Torvalds 已提交
2062
		icount->parity++;
A
Alan Cox 已提交
2063
	if (newLsr & LSR_FRM_ERR)
L
Linus Torvalds 已提交
2064 2065 2066 2067 2068 2069
		icount->frame++;
}


/****************************************************************************
 * sram_write
A
Alan Cox 已提交
2070
 *	writes a number of bytes to the Edgeport device's sram starting at the
L
Linus Torvalds 已提交
2071 2072 2073 2074
 *	given address.
 *	If successful returns the number of bytes written, otherwise it returns
 *	a negative error number of the problem.
 ****************************************************************************/
A
Alan Cox 已提交
2075 2076
static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data)
L
Linus Torvalds 已提交
2077 2078 2079 2080 2081
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

2082
	dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
L
Linus Torvalds 已提交
2083

A
Alan Cox 已提交
2084
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
2085
	if (!transfer_buffer)
L
Linus Torvalds 已提交
2086 2087 2088 2089 2090
		return -ENOMEM;

	/* need to split these writes up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2091
		if (length > 64)
L
Linus Torvalds 已提交
2092
			current_length = 64;
A
Alan Cox 已提交
2093
		else
L
Linus Torvalds 已提交
2094
			current_length = length;
A
Alan Cox 已提交
2095

2096
/*		dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
A
Alan Cox 已提交
2097 2098 2099 2100 2101 2102
		memcpy(transfer_buffer, data, current_length);
		result = usb_control_msg(serial->dev,
					usb_sndctrlpipe(serial->dev, 0),
					USB_REQUEST_ION_WRITE_RAM,
					0x40, addr, extAddr, transfer_buffer,
					current_length, 300);
L
Linus Torvalds 已提交
2103 2104 2105 2106 2107
		if (result < 0)
			break;
		length -= current_length;
		addr += current_length;
		data += current_length;
A
Alan Cox 已提交
2108
	}
L
Linus Torvalds 已提交
2109

A
Alan Cox 已提交
2110
	kfree(transfer_buffer);
L
Linus Torvalds 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
	return result;
}


/****************************************************************************
 * rom_write
 *	writes a number of bytes to the Edgeport device's ROM starting at the
 *	given address.
 *	If successful returns the number of bytes written, otherwise it returns
 *	a negative error number of the problem.
 ****************************************************************************/
A
Alan Cox 已提交
2122 2123
static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data)
L
Linus Torvalds 已提交
2124 2125 2126 2127 2128
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

A
Alan Cox 已提交
2129
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
2130
	if (!transfer_buffer)
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135
		return -ENOMEM;

	/* need to split these writes up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2136
		if (length > 64)
L
Linus Torvalds 已提交
2137
			current_length = 64;
A
Alan Cox 已提交
2138
		else
L
Linus Torvalds 已提交
2139
			current_length = length;
A
Alan Cox 已提交
2140 2141 2142 2143 2144 2145
		memcpy(transfer_buffer, data, current_length);
		result = usb_control_msg(serial->dev,
					usb_sndctrlpipe(serial->dev, 0),
					USB_REQUEST_ION_WRITE_ROM, 0x40,
					addr, extAddr,
					transfer_buffer, current_length, 300);
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150
		if (result < 0)
			break;
		length -= current_length;
		addr += current_length;
		data += current_length;
A
Alan Cox 已提交
2151
	}
L
Linus Torvalds 已提交
2152

A
Alan Cox 已提交
2153
	kfree(transfer_buffer);
L
Linus Torvalds 已提交
2154 2155 2156 2157 2158 2159 2160 2161
	return result;
}


/****************************************************************************
 * rom_read
 *	reads a number of bytes from the Edgeport device starting at the given
 *	address.
2162
 *	Returns zero on success or a negative error number.
L
Linus Torvalds 已提交
2163
 ****************************************************************************/
A
Alan Cox 已提交
2164 2165
static int rom_read(struct usb_serial *serial, __u16 extAddr,
					__u16 addr, __u16 length, __u8 *data)
L
Linus Torvalds 已提交
2166 2167 2168 2169 2170
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

A
Alan Cox 已提交
2171
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
2172
	if (!transfer_buffer)
L
Linus Torvalds 已提交
2173 2174 2175 2176 2177
		return -ENOMEM;

	/* need to split these reads up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2178
		if (length > 64)
L
Linus Torvalds 已提交
2179
			current_length = 64;
A
Alan Cox 已提交
2180
		else
L
Linus Torvalds 已提交
2181
			current_length = length;
A
Alan Cox 已提交
2182 2183 2184 2185 2186
		result = usb_control_msg(serial->dev,
					usb_rcvctrlpipe(serial->dev, 0),
					USB_REQUEST_ION_READ_ROM,
					0xC0, addr, extAddr, transfer_buffer,
					current_length, 300);
2187 2188 2189
		if (result < current_length) {
			if (result >= 0)
				result = -EIO;
L
Linus Torvalds 已提交
2190
			break;
2191
		}
A
Alan Cox 已提交
2192
		memcpy(data, transfer_buffer, current_length);
L
Linus Torvalds 已提交
2193 2194 2195
		length -= current_length;
		addr += current_length;
		data += current_length;
2196 2197

		result = 0;
A
Alan Cox 已提交
2198
	}
L
Linus Torvalds 已提交
2199

A
Alan Cox 已提交
2200
	kfree(transfer_buffer);
L
Linus Torvalds 已提交
2201 2202 2203 2204 2205 2206 2207 2208
	return result;
}


/****************************************************************************
 * send_iosp_ext_cmd
 *	Is used to send a IOSP message to the Edgeport device
 ****************************************************************************/
A
Alan Cox 已提交
2209 2210
static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
						__u8 command, __u8 param)
L
Linus Torvalds 已提交
2211 2212 2213 2214 2215 2216
{
	unsigned char   *buffer;
	unsigned char   *currentCommand;
	int             length = 0;
	int             status = 0;

A
Alan Cox 已提交
2217
	buffer = kmalloc(10, GFP_ATOMIC);
2218
	if (!buffer)
L
Linus Torvalds 已提交
2219 2220 2221 2222
		return -ENOMEM;

	currentCommand = buffer;

2223 2224
	MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
			 command, param);
L
Linus Torvalds 已提交
2225

A
Alan Cox 已提交
2226
	status = write_cmd_usb(edge_port, buffer, length);
L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	if (status) {
		/* something bad happened, let's free up the memory */
		kfree(buffer);
	}

	return status;
}


/*****************************************************************************
 * write_cmd_usb
 *	this function writes the given buffer out to the bulk write endpoint.
 *****************************************************************************/
A
Alan Cox 已提交
2240 2241
static int write_cmd_usb(struct edgeport_port *edge_port,
					unsigned char *buffer, int length)
L
Linus Torvalds 已提交
2242
{
A
Alan Cox 已提交
2243 2244
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2245
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2246 2247 2248
	int status = 0;
	struct urb *urb;

2249
	usb_serial_debug_data(dev, __func__, length, buffer);
L
Linus Torvalds 已提交
2250 2251

	/* Allocate our next urb */
A
Alan Cox 已提交
2252
	urb = usb_alloc_urb(0, GFP_ATOMIC);
L
Linus Torvalds 已提交
2253 2254 2255
	if (!urb)
		return -ENOMEM;

2256
	atomic_inc(&CmdUrbs);
2257 2258
	dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
		__func__, urb, atomic_read(&CmdUrbs));
L
Linus Torvalds 已提交
2259

A
Alan Cox 已提交
2260 2261 2262 2263
	usb_fill_bulk_urb(urb, edge_serial->serial->dev,
			usb_sndbulkpipe(edge_serial->serial->dev,
					edge_serial->bulk_out_endpoint),
			buffer, length, edge_bulk_out_cmd_callback, edge_port);
L
Linus Torvalds 已提交
2264

2265
	edge_port->commandPending = true;
L
Linus Torvalds 已提交
2266 2267 2268 2269
	status = usb_submit_urb(urb, GFP_ATOMIC);

	if (status) {
		/* something went wrong */
2270 2271
		dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
			__func__, status);
L
Linus Torvalds 已提交
2272
		usb_free_urb(urb);
2273
		atomic_dec(&CmdUrbs);
L
Linus Torvalds 已提交
2274 2275 2276 2277
		return status;
	}

#if 0
A
Alan Cox 已提交
2278
	wait_event(&edge_port->wait_command, !edge_port->commandPending);
L
Linus Torvalds 已提交
2279

2280
	if (edge_port->commandPending) {
L
Linus Torvalds 已提交
2281
		/* command timed out */
2282
		dev_dbg(dev, "%s - command timed out\n", __func__);
L
Linus Torvalds 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
		status = -EINVAL;
	}
#endif
	return status;
}


/*****************************************************************************
 * send_cmd_write_baud_rate
 *	this function sends the proper command to change the baud rate of the
 *	specified port.
 *****************************************************************************/
A
Alan Cox 已提交
2295 2296
static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
								int baudRate)
L
Linus Torvalds 已提交
2297
{
A
Alan Cox 已提交
2298 2299
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2300
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2301 2302 2303 2304 2305
	unsigned char *cmdBuffer;
	unsigned char *currCmd;
	int cmdLen = 0;
	int divisor;
	int status;
2306
	u32 number = edge_port->port->port_number;
L
Linus Torvalds 已提交
2307

2308 2309
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2310 2311
		dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
			baudRate);
2312 2313 2314
		return 0;
	}

2315
	dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
L
Linus Torvalds 已提交
2316

2317
	status = calc_baud_rate_divisor(dev, baudRate, &divisor);
L
Linus Torvalds 已提交
2318
	if (status) {
2319
		dev_err(dev, "%s - bad baud rate\n", __func__);
L
Linus Torvalds 已提交
2320 2321 2322
		return status;
	}

A
Alan Cox 已提交
2323 2324
	/* Alloc memory for the string of commands. */
	cmdBuffer =  kmalloc(0x100, GFP_ATOMIC);
2325
	if (!cmdBuffer)
L
Linus Torvalds 已提交
2326
		return -ENOMEM;
2327

L
Linus Torvalds 已提交
2328 2329
	currCmd = cmdBuffer;

A
Alan Cox 已提交
2330 2331
	/* Enable access to divisor latch */
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
L
Linus Torvalds 已提交
2332

A
Alan Cox 已提交
2333 2334 2335
	/* Write the divisor itself */
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
L
Linus Torvalds 已提交
2336

A
Alan Cox 已提交
2337 2338 2339
	/* Restore original value to disable access to divisor latch */
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
						edge_port->shadowLCR);
L
Linus Torvalds 已提交
2340

A
Alan Cox 已提交
2341
	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
L
Linus Torvalds 已提交
2342 2343
	if (status) {
		/* something bad happened, let's free up the memory */
A
Alan Cox 已提交
2344
		kfree(cmdBuffer);
L
Linus Torvalds 已提交
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
	}

	return status;
}


/*****************************************************************************
 * calc_baud_rate_divisor
 *	this function calculates the proper baud rate divisor for the specified
 *	baud rate.
 *****************************************************************************/
2356
static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
L
Linus Torvalds 已提交
2357 2358 2359 2360
{
	int i;
	__u16 custom;

2361
	for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
A
Alan Cox 已提交
2362
		if (divisor_table[i].BaudRate == baudrate) {
L
Linus Torvalds 已提交
2363 2364 2365 2366 2367
			*divisor = divisor_table[i].Divisor;
			return 0;
		}
	}

A
Alan Cox 已提交
2368 2369 2370
	/* We have tried all of the standard baud rates
	 * lets try to calculate the divisor for this baud rate
	 * Make sure the baud rate is reasonable */
L
Linus Torvalds 已提交
2371
	if (baudrate > 50 && baudrate < 230400) {
A
Alan Cox 已提交
2372
		/* get divisor */
L
Linus Torvalds 已提交
2373 2374 2375 2376
		custom = (__u16)((230400L + baudrate/2) / baudrate);

		*divisor = custom;

2377
		dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
L
Linus Torvalds 已提交
2378 2379 2380 2381 2382 2383 2384 2385 2386
		return 0;
	}

	return -1;
}


/*****************************************************************************
 * send_cmd_write_uart_register
2387
 *  this function builds up a uart register message and sends to the device.
L
Linus Torvalds 已提交
2388
 *****************************************************************************/
A
Alan Cox 已提交
2389 2390
static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
						__u8 regNum, __u8 regValue)
L
Linus Torvalds 已提交
2391
{
A
Alan Cox 已提交
2392 2393
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2394
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2395 2396 2397 2398 2399
	unsigned char *cmdBuffer;
	unsigned char *currCmd;
	unsigned long cmdLen = 0;
	int status;

2400 2401
	dev_dbg(dev, "%s - write to %s register 0x%02x\n",
		(regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
L
Linus Torvalds 已提交
2402

2403 2404 2405
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
	    regNum == MCR) {
2406
		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2407 2408 2409
		return 0;
	}

2410 2411 2412
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
	    regNum == LCR) {
2413
		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2414 2415 2416
		return 0;
	}

A
Alan Cox 已提交
2417 2418 2419
	/* Alloc memory for the string of commands. */
	cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
	if (cmdBuffer == NULL)
L
Linus Torvalds 已提交
2420 2421 2422 2423
		return -ENOMEM;

	currCmd = cmdBuffer;

A
Alan Cox 已提交
2424
	/* Build a cmd in the buffer to write the given register */
2425 2426
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
			   regNum, regValue);
L
Linus Torvalds 已提交
2427 2428 2429 2430

	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
	if (status) {
		/* something bad happened, let's free up the memory */
A
Alan Cox 已提交
2431
		kfree(cmdBuffer);
L
Linus Torvalds 已提交
2432 2433 2434 2435 2436 2437 2438 2439
	}

	return status;
}


/*****************************************************************************
 * change_port_settings
A
Alan Cox 已提交
2440 2441
 *	This routine is called to set the UART on the device to match the
 *	specified new settings.
L
Linus Torvalds 已提交
2442
 *****************************************************************************/
A
Alan Cox 已提交
2443 2444 2445

static void change_port_settings(struct tty_struct *tty,
	struct edgeport_port *edge_port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2446
{
2447
	struct device *dev = &edge_port->port->dev;
A
Alan Cox 已提交
2448 2449
	struct edgeport_serial *edge_serial =
			usb_get_serial_data(edge_port->port->serial);
L
Linus Torvalds 已提交
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
	int baud;
	unsigned cflag;
	__u8 mask = 0xff;
	__u8 lData;
	__u8 lParity;
	__u8 lStop;
	__u8 rxFlow;
	__u8 txFlow;
	int status;

2460 2461
	if (!edge_port->open &&
	    !edge_port->openPending) {
2462
		dev_dbg(dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
2463 2464 2465
		return;
	}

2466
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2467 2468

	switch (cflag & CSIZE) {
A
Alan Cox 已提交
2469 2470
	case CS5:
		lData = LCR_BITS_5; mask = 0x1f;
2471
		dev_dbg(dev, "%s - data bits = 5\n", __func__);
A
Alan Cox 已提交
2472 2473 2474
		break;
	case CS6:
		lData = LCR_BITS_6; mask = 0x3f;
2475
		dev_dbg(dev, "%s - data bits = 6\n", __func__);
A
Alan Cox 已提交
2476 2477 2478
		break;
	case CS7:
		lData = LCR_BITS_7; mask = 0x7f;
2479
		dev_dbg(dev, "%s - data bits = 7\n", __func__);
A
Alan Cox 已提交
2480 2481 2482 2483
		break;
	default:
	case CS8:
		lData = LCR_BITS_8;
2484
		dev_dbg(dev, "%s - data bits = 8\n", __func__);
A
Alan Cox 已提交
2485
		break;
L
Linus Torvalds 已提交
2486 2487 2488 2489 2490 2491 2492
	}

	lParity = LCR_PAR_NONE;
	if (cflag & PARENB) {
		if (cflag & CMSPAR) {
			if (cflag & PARODD) {
				lParity = LCR_PAR_MARK;
2493
				dev_dbg(dev, "%s - parity = mark\n", __func__);
L
Linus Torvalds 已提交
2494 2495
			} else {
				lParity = LCR_PAR_SPACE;
2496
				dev_dbg(dev, "%s - parity = space\n", __func__);
L
Linus Torvalds 已提交
2497 2498 2499
			}
		} else if (cflag & PARODD) {
			lParity = LCR_PAR_ODD;
2500
			dev_dbg(dev, "%s - parity = odd\n", __func__);
L
Linus Torvalds 已提交
2501 2502
		} else {
			lParity = LCR_PAR_EVEN;
2503
			dev_dbg(dev, "%s - parity = even\n", __func__);
L
Linus Torvalds 已提交
2504 2505
		}
	} else {
2506
		dev_dbg(dev, "%s - parity = none\n", __func__);
L
Linus Torvalds 已提交
2507 2508 2509 2510
	}

	if (cflag & CSTOPB) {
		lStop = LCR_STOP_2;
2511
		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
L
Linus Torvalds 已提交
2512 2513
	} else {
		lStop = LCR_STOP_1;
2514
		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
L
Linus Torvalds 已提交
2515 2516 2517 2518 2519 2520 2521
	}

	/* figure out the flow control settings */
	rxFlow = txFlow = 0x00;
	if (cflag & CRTSCTS) {
		rxFlow |= IOSP_RX_FLOW_RTS;
		txFlow |= IOSP_TX_FLOW_CTS;
2522
		dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
L
Linus Torvalds 已提交
2523
	} else {
2524
		dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
L
Linus Torvalds 已提交
2525 2526
	}

A
Alan Cox 已提交
2527 2528
	/* if we are implementing XON/XOFF, set the start and stop character
	   in the device */
L
Linus Torvalds 已提交
2529 2530 2531 2532
	if (I_IXOFF(tty) || I_IXON(tty)) {
		unsigned char stop_char  = STOP_CHAR(tty);
		unsigned char start_char = START_CHAR(tty);

2533 2534
		if (!edge_serial->is_epic ||
		    edge_serial->epic_descriptor.Supports.IOSPSetXChar) {
A
Alan Cox 已提交
2535 2536 2537 2538
			send_iosp_ext_cmd(edge_port,
					IOSP_CMD_SET_XON_CHAR, start_char);
			send_iosp_ext_cmd(edge_port,
					IOSP_CMD_SET_XOFF_CHAR, stop_char);
2539
		}
L
Linus Torvalds 已提交
2540 2541 2542 2543

		/* if we are implementing INBOUND XON/XOFF */
		if (I_IXOFF(tty)) {
			rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2544 2545
			dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
				__func__, start_char, stop_char);
L
Linus Torvalds 已提交
2546
		} else {
2547
			dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2548 2549 2550 2551 2552
		}

		/* if we are implementing OUTBOUND XON/XOFF */
		if (I_IXON(tty)) {
			txFlow |= IOSP_TX_FLOW_XON_XOFF;
2553 2554
			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
				__func__, start_char, stop_char);
L
Linus Torvalds 已提交
2555
		} else {
2556
			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2557 2558 2559 2560
		}
	}

	/* Set flow control to the configured value */
2561 2562
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)
2563
		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2564 2565
	if (!edge_serial->is_epic ||
	    edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)
2566
		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
L
Linus Torvalds 已提交
2567 2568 2569 2570 2571 2572 2573 2574


	edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
	edge_port->shadowLCR |= (lData | lParity | lStop);

	edge_port->validDataMask = mask;

	/* Send the updated LCR value to the EdgePort */
A
Alan Cox 已提交
2575 2576 2577
	status = send_cmd_write_uart_register(edge_port, LCR,
							edge_port->shadowLCR);
	if (status != 0)
L
Linus Torvalds 已提交
2578 2579 2580 2581
		return;

	/* set up the MCR register and send it to the EdgePort */
	edge_port->shadowMCR = MCR_MASTER_IE;
A
Alan Cox 已提交
2582
	if (cflag & CBAUD)
L
Linus Torvalds 已提交
2583
		edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
A
Alan Cox 已提交
2584 2585 2586 2587

	status = send_cmd_write_uart_register(edge_port, MCR,
						edge_port->shadowMCR);
	if (status != 0)
L
Linus Torvalds 已提交
2588 2589 2590 2591 2592 2593 2594 2595 2596
		return;

	/* Determine divisor based on baud rate */
	baud = tty_get_baud_rate(tty);
	if (!baud) {
		/* pick a default, any default... */
		baud = 9600;
	}

2597
	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
A
Alan Cox 已提交
2598
	status = send_cmd_write_baud_rate(edge_port, baud);
2599 2600 2601 2602 2603
	if (status == -1) {
		/* Speed change was not possible - put back the old speed */
		baud = tty_termios_baud_rate(old_termios);
		tty_encode_baud_rate(tty, baud, baud);
	}
L
Linus Torvalds 已提交
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
}


/****************************************************************************
 * unicode_to_ascii
 *	Turns a string from Unicode into ASCII.
 *	Doesn't do a good job with any characters that are outside the normal
 *	ASCII range, but it's only for debugging...
 *	NOTE: expects the unicode in LE format
 ****************************************************************************/
A
Alan Cox 已提交
2614 2615
static void unicode_to_ascii(char *string, int buflen,
					__le16 *unicode, int unicode_size)
L
Linus Torvalds 已提交
2616 2617 2618
{
	int i;

2619
	if (buflen <= 0)	/* never happens, but... */
L
Linus Torvalds 已提交
2620
		return;
2621
	--buflen;		/* space for nul */
L
Linus Torvalds 已提交
2622

2623 2624 2625
	for (i = 0; i < unicode_size; i++) {
		if (i >= buflen)
			break;
L
Linus Torvalds 已提交
2626
		string[i] = (char)(le16_to_cpu(unicode[i]));
2627 2628
	}
	string[i] = 0x00;
L
Linus Torvalds 已提交
2629 2630 2631 2632 2633
}


/****************************************************************************
 * get_manufacturing_desc
A
Alan Cox 已提交
2634
 *	reads in the manufacturing descriptor and stores it into the serial
L
Linus Torvalds 已提交
2635 2636
 *	structure.
 ****************************************************************************/
A
Alan Cox 已提交
2637
static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2638
{
2639
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
2640 2641
	int response;

2642
	dev_dbg(dev, "getting manufacturer descriptor\n");
L
Linus Torvalds 已提交
2643

A
Alan Cox 已提交
2644 2645 2646 2647 2648
	response = rom_read(edge_serial->serial,
				(EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
				(__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
				EDGE_MANUF_DESC_LEN,
				(__u8 *)(&edge_serial->manuf_descriptor));
L
Linus Torvalds 已提交
2649

2650 2651 2652 2653
	if (response < 0) {
		dev_err(dev, "error in getting manufacturer descriptor: %d\n",
				response);
	} else {
L
Linus Torvalds 已提交
2654
		char string[30];
2655 2656
		dev_dbg(dev, "**Manufacturer Descriptor\n");
		dev_dbg(dev, "  RomSize:        %dK\n",
A
Alan Cox 已提交
2657
			edge_serial->manuf_descriptor.RomSize);
2658
		dev_dbg(dev, "  RamSize:        %dK\n",
A
Alan Cox 已提交
2659
			edge_serial->manuf_descriptor.RamSize);
2660
		dev_dbg(dev, "  CpuRev:         %d\n",
A
Alan Cox 已提交
2661
			edge_serial->manuf_descriptor.CpuRev);
2662
		dev_dbg(dev, "  BoardRev:       %d\n",
A
Alan Cox 已提交
2663
			edge_serial->manuf_descriptor.BoardRev);
2664
		dev_dbg(dev, "  NumPorts:       %d\n",
A
Alan Cox 已提交
2665
			edge_serial->manuf_descriptor.NumPorts);
2666
		dev_dbg(dev, "  DescDate:       %d/%d/%d\n",
A
Alan Cox 已提交
2667 2668 2669
			edge_serial->manuf_descriptor.DescDate[0],
			edge_serial->manuf_descriptor.DescDate[1],
			edge_serial->manuf_descriptor.DescDate[2]+1900);
P
Pete Zaitcev 已提交
2670
		unicode_to_ascii(string, sizeof(string),
A
Alan Cox 已提交
2671 2672
			edge_serial->manuf_descriptor.SerialNumber,
			edge_serial->manuf_descriptor.SerNumLength/2);
2673
		dev_dbg(dev, "  SerialNumber: %s\n", string);
P
Pete Zaitcev 已提交
2674
		unicode_to_ascii(string, sizeof(string),
A
Alan Cox 已提交
2675 2676
			edge_serial->manuf_descriptor.AssemblyNumber,
			edge_serial->manuf_descriptor.AssemblyNumLength/2);
2677
		dev_dbg(dev, "  AssemblyNumber: %s\n", string);
P
Pete Zaitcev 已提交
2678
		unicode_to_ascii(string, sizeof(string),
2679 2680
		    edge_serial->manuf_descriptor.OemAssyNumber,
		    edge_serial->manuf_descriptor.OemAssyNumLength/2);
2681 2682
		dev_dbg(dev, "  OemAssyNumber:  %s\n", string);
		dev_dbg(dev, "  UartType:       %d\n",
A
Alan Cox 已提交
2683
			edge_serial->manuf_descriptor.UartType);
2684
		dev_dbg(dev, "  IonPid:         %d\n",
A
Alan Cox 已提交
2685
			edge_serial->manuf_descriptor.IonPid);
2686
		dev_dbg(dev, "  IonConfig:      %d\n",
A
Alan Cox 已提交
2687
			edge_serial->manuf_descriptor.IonConfig);
L
Linus Torvalds 已提交
2688 2689 2690 2691 2692 2693
	}
}


/****************************************************************************
 * get_boot_desc
A
Alan Cox 已提交
2694
 *	reads in the bootloader descriptor and stores it into the serial
L
Linus Torvalds 已提交
2695 2696
 *	structure.
 ****************************************************************************/
A
Alan Cox 已提交
2697
static void get_boot_desc(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2698
{
2699
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
2700 2701
	int response;

2702
	dev_dbg(dev, "getting boot descriptor\n");
L
Linus Torvalds 已提交
2703

A
Alan Cox 已提交
2704 2705 2706 2707 2708
	response = rom_read(edge_serial->serial,
				(EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
				(__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
				EDGE_BOOT_DESC_LEN,
				(__u8 *)(&edge_serial->boot_descriptor));
L
Linus Torvalds 已提交
2709

2710 2711 2712 2713
	if (response < 0) {
		dev_err(dev, "error in getting boot descriptor: %d\n",
				response);
	} else {
2714 2715 2716 2717
		dev_dbg(dev, "**Boot Descriptor:\n");
		dev_dbg(dev, "  BootCodeLength: %d\n",
			le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
		dev_dbg(dev, "  MajorVersion:   %d\n",
A
Alan Cox 已提交
2718
			edge_serial->boot_descriptor.MajorVersion);
2719
		dev_dbg(dev, "  MinorVersion:   %d\n",
A
Alan Cox 已提交
2720
			edge_serial->boot_descriptor.MinorVersion);
2721
		dev_dbg(dev, "  BuildNumber:    %d\n",
A
Alan Cox 已提交
2722
			le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2723
		dev_dbg(dev, "  Capabilities:   0x%x\n",
A
Alan Cox 已提交
2724
		      le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2725
		dev_dbg(dev, "  UConfig0:       %d\n",
A
Alan Cox 已提交
2726
			edge_serial->boot_descriptor.UConfig0);
2727
		dev_dbg(dev, "  UConfig1:       %d\n",
A
Alan Cox 已提交
2728
			edge_serial->boot_descriptor.UConfig1);
L
Linus Torvalds 已提交
2729 2730 2731 2732 2733 2734 2735 2736
	}
}


/****************************************************************************
 * load_application_firmware
 *	This is called to load the application firmware to the device
 ****************************************************************************/
A
Alan Cox 已提交
2737
static void load_application_firmware(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2738
{
2739
	struct device *dev = &edge_serial->serial->dev->dev;
2740 2741 2742 2743
	const struct ihex_binrec *rec;
	const struct firmware *fw;
	const char *fw_name;
	const char *fw_info;
L
Linus Torvalds 已提交
2744
	int response;
2745 2746
	__u32 Operaddr;
	__u16 build;
L
Linus Torvalds 已提交
2747 2748 2749

	switch (edge_serial->product_info.iDownloadFile) {
		case EDGE_DOWNLOAD_FILE_I930:
2750 2751
			fw_info = "downloading firmware version (930)";
			fw_name	= "edgeport/down.fw";
L
Linus Torvalds 已提交
2752 2753 2754
			break;

		case EDGE_DOWNLOAD_FILE_80251:
2755 2756
			fw_info = "downloading firmware version (80251)";
			fw_name	= "edgeport/down2.fw";
L
Linus Torvalds 已提交
2757 2758 2759
			break;

		case EDGE_DOWNLOAD_FILE_NONE:
2760
			dev_dbg(dev, "No download file specified, skipping download\n");
L
Linus Torvalds 已提交
2761 2762 2763 2764 2765 2766
			return;

		default:
			return;
	}

2767 2768 2769
	response = request_ihex_firmware(&fw, fw_name,
				    &edge_serial->serial->dev->dev);
	if (response) {
2770
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
2771 2772 2773 2774 2775 2776 2777
		       fw_name, response);
		return;
	}

	rec = (const struct ihex_binrec *)fw->data;
	build = (rec->data[2] << 8) | rec->data[3];

2778
	dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2779

2780 2781
	edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
	edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2782
	edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
L
Linus Torvalds 已提交
2783

2784 2785 2786 2787 2788 2789 2790 2791
	for (rec = ihex_next_binrec(rec); rec;
	     rec = ihex_next_binrec(rec)) {
		Operaddr = be32_to_cpu(rec->addr);
		response = sram_write(edge_serial->serial,
				     Operaddr >> 16,
				     Operaddr & 0xFFFF,
				     be16_to_cpu(rec->len),
				     &rec->data[0]);
L
Linus Torvalds 已提交
2792
		if (response < 0) {
2793 2794 2795 2796
			dev_err(&edge_serial->serial->dev->dev,
				"sram_write failed (%x, %x, %d)\n",
				Operaddr >> 16, Operaddr & 0xFFFF,
				be16_to_cpu(rec->len));
L
Linus Torvalds 已提交
2797 2798 2799 2800
			break;
		}
	}

2801 2802 2803 2804
	dev_dbg(dev, "sending exec_dl_code\n");
	response = usb_control_msg (edge_serial->serial->dev,
				    usb_sndctrlpipe(edge_serial->serial->dev, 0),
				    USB_REQUEST_ION_EXEC_DL_CODE,
L
Linus Torvalds 已提交
2805 2806
				    0x40, 0x4000, 0x0001, NULL, 0, 3000);

2807
	release_firmware(fw);
L
Linus Torvalds 已提交
2808 2809 2810 2811 2812 2813
}


/****************************************************************************
 * edge_startup
 ****************************************************************************/
A
Alan Cox 已提交
2814
static int edge_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2815 2816 2817
{
	struct edgeport_serial *edge_serial;
	struct usb_device *dev;
2818
	struct device *ddev = &serial->dev->dev;
2819
	int i;
2820
	int response;
2821 2822 2823
	bool interrupt_in_found;
	bool bulk_in_found;
	bool bulk_out_found;
2824 2825 2826
	static const __u32 descriptor[3] = {	EDGE_COMPATIBILITY_MASK0,
						EDGE_COMPATIBILITY_MASK1,
						EDGE_COMPATIBILITY_MASK2 };
L
Linus Torvalds 已提交
2827 2828 2829 2830

	dev = serial->dev;

	/* create our private serial structure */
2831
	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2832
	if (!edge_serial)
L
Linus Torvalds 已提交
2833
		return -ENOMEM;
2834

L
Linus Torvalds 已提交
2835 2836 2837 2838 2839
	spin_lock_init(&edge_serial->es_lock);
	edge_serial->serial = serial;
	usb_set_serial_data(serial, edge_serial);

	/* get the name for the device from the device */
2840
	i = usb_string(dev, dev->descriptor.iManufacturer,
2841
	    &edge_serial->name[0], MAX_NAME_LEN+1);
2842 2843
	if (i < 0)
		i = 0;
2844
	edge_serial->name[i++] = ' ';
2845
	usb_string(dev, dev->descriptor.iProduct,
2846
	    &edge_serial->name[i], MAX_NAME_LEN+2 - i);
L
Linus Torvalds 已提交
2847 2848 2849

	dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);

2850
	/* Read the epic descriptor */
2851
	if (get_epic_descriptor(edge_serial) < 0) {
2852 2853 2854
		/* memcpy descriptor to Supports structures */
		memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
		       sizeof(struct edge_compatibility_bits));
L
Linus Torvalds 已提交
2855

2856
		/* get the manufacturing descriptor for this device */
A
Alan Cox 已提交
2857
		get_manufacturing_desc(edge_serial);
L
Linus Torvalds 已提交
2858

2859
		/* get the boot descriptor */
A
Alan Cox 已提交
2860
		get_boot_desc(edge_serial);
2861 2862 2863

		get_product_info(edge_serial);
	}
L
Linus Torvalds 已提交
2864 2865 2866

	/* set the number of ports from the manufacturing description */
	/* serial->num_ports = serial->product_info.NumPorts; */
2867 2868
	if ((!edge_serial->is_epic) &&
	    (edge_serial->product_info.NumPorts != serial->num_ports)) {
2869 2870
		dev_warn(ddev,
			"Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2871 2872
			 edge_serial->product_info.NumPorts,
			 serial->num_ports);
L
Linus Torvalds 已提交
2873 2874
	}

2875
	dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
L
Linus Torvalds 已提交
2876

2877 2878 2879
	/* If not an EPiC device */
	if (!edge_serial->is_epic) {
		/* now load the application firmware into this device */
A
Alan Cox 已提交
2880
		load_application_firmware(edge_serial);
L
Linus Torvalds 已提交
2881

2882
		dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
L
Linus Torvalds 已提交
2883

2884
		/* Check current Edgeport EEPROM and update if necessary */
A
Alan Cox 已提交
2885
		update_edgeport_E2PROM(edge_serial);
L
Linus Torvalds 已提交
2886

2887
		dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2888 2889

		/* set the configuration to use #1 */
2890
/*		dev_dbg(ddev, "set_configuration 1\n"); */
A
Alan Cox 已提交
2891
/*		usb_set_configuration (dev, 1); */
2892
	}
2893
	dev_dbg(ddev, "  FirmwareMajorVersion  %d.%d.%d\n",
2894 2895 2896
	    edge_serial->product_info.FirmwareMajorVersion,
	    edge_serial->product_info.FirmwareMinorVersion,
	    le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
L
Linus Torvalds 已提交
2897

A
Alan Cox 已提交
2898
	/* we set up the pointers to the endpoints in the edge_open function,
L
Linus Torvalds 已提交
2899 2900
	 * as the structures aren't created yet. */

2901 2902 2903
	response = 0;

	if (edge_serial->is_epic) {
2904 2905 2906 2907
		struct usb_host_interface *alt;

		alt = serial->interface->cur_altsetting;

A
Alan Cox 已提交
2908 2909
		/* EPIC thing, set up our interrupt polling now and our read
		 * urb, so that the device knows it really is connected. */
2910
		interrupt_in_found = bulk_in_found = bulk_out_found = false;
2911
		for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
2912 2913 2914
			struct usb_endpoint_descriptor *endpoint;
			int buffer_size;

2915
			endpoint = &alt->endpoint[i].desc;
2916
			buffer_size = usb_endpoint_maxp(endpoint);
2917
			if (!interrupt_in_found &&
2918 2919
			    (usb_endpoint_is_int_in(endpoint))) {
				/* we found a interrupt in endpoint */
2920
				dev_dbg(ddev, "found interrupt in\n");
2921 2922

				/* not set up yet, so do it now */
A
Alan Cox 已提交
2923 2924
				edge_serial->interrupt_read_urb =
						usb_alloc_urb(0, GFP_KERNEL);
2925 2926 2927 2928
				if (!edge_serial->interrupt_read_urb) {
					response = -ENOMEM;
					break;
				}
2929

A
Alan Cox 已提交
2930 2931
				edge_serial->interrupt_in_buffer =
					kmalloc(buffer_size, GFP_KERNEL);
2932
				if (!edge_serial->interrupt_in_buffer) {
2933 2934
					response = -ENOMEM;
					break;
2935
				}
A
Alan Cox 已提交
2936 2937
				edge_serial->interrupt_in_endpoint =
						endpoint->bEndpointAddress;
2938 2939

				/* set up our interrupt urb */
A
Alan Cox 已提交
2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
				usb_fill_int_urb(
					edge_serial->interrupt_read_urb,
					dev,
					usb_rcvintpipe(dev,
						endpoint->bEndpointAddress),
					edge_serial->interrupt_in_buffer,
					buffer_size,
					edge_interrupt_callback,
					edge_serial,
					endpoint->bInterval);
2950

2951
				interrupt_in_found = true;
2952 2953
			}

2954
			if (!bulk_in_found &&
A
Alan Cox 已提交
2955
				(usb_endpoint_is_bulk_in(endpoint))) {
2956
				/* we found a bulk in endpoint */
2957
				dev_dbg(ddev, "found bulk in\n");
2958 2959

				/* not set up yet, so do it now */
A
Alan Cox 已提交
2960 2961
				edge_serial->read_urb =
						usb_alloc_urb(0, GFP_KERNEL);
2962 2963 2964 2965
				if (!edge_serial->read_urb) {
					response = -ENOMEM;
					break;
				}
2966

A
Alan Cox 已提交
2967 2968
				edge_serial->bulk_in_buffer =
					kmalloc(buffer_size, GFP_KERNEL);
2969
				if (!edge_serial->bulk_in_buffer) {
2970 2971
					response = -ENOMEM;
					break;
2972
				}
A
Alan Cox 已提交
2973 2974
				edge_serial->bulk_in_endpoint =
						endpoint->bEndpointAddress;
2975 2976 2977

				/* set up our bulk in urb */
				usb_fill_bulk_urb(edge_serial->read_urb, dev,
A
Alan Cox 已提交
2978 2979 2980
					usb_rcvbulkpipe(dev,
						endpoint->bEndpointAddress),
					edge_serial->bulk_in_buffer,
2981
					usb_endpoint_maxp(endpoint),
A
Alan Cox 已提交
2982 2983
					edge_bulk_in_callback,
					edge_serial);
2984
				bulk_in_found = true;
2985 2986
			}

2987
			if (!bulk_out_found &&
2988 2989
			    (usb_endpoint_is_bulk_out(endpoint))) {
				/* we found a bulk out endpoint */
2990
				dev_dbg(ddev, "found bulk out\n");
A
Alan Cox 已提交
2991 2992
				edge_serial->bulk_out_endpoint =
						endpoint->bEndpointAddress;
2993
				bulk_out_found = true;
2994 2995 2996
			}
		}

2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
		if (response || !interrupt_in_found || !bulk_in_found ||
							!bulk_out_found) {
			if (!response) {
				dev_err(ddev, "expected endpoints not found\n");
				response = -ENODEV;
			}

			usb_free_urb(edge_serial->interrupt_read_urb);
			kfree(edge_serial->interrupt_in_buffer);

			usb_free_urb(edge_serial->read_urb);
			kfree(edge_serial->bulk_in_buffer);

			kfree(edge_serial);

			return response;
3013 3014 3015 3016
		}

		/* start interrupt read for this edgeport this interrupt will
		 * continue as long as the edgeport is connected */
A
Alan Cox 已提交
3017 3018
		response = usb_submit_urb(edge_serial->interrupt_read_urb,
								GFP_KERNEL);
3019
		if (response)
3020
			dev_err(ddev, "%s - Error %d submitting control urb\n",
3021
				__func__, response);
3022 3023
	}
	return response;
L
Linus Torvalds 已提交
3024 3025 3026 3027
}


/****************************************************************************
3028
 * edge_disconnect
L
Linus Torvalds 已提交
3029 3030
 *	This function is called whenever the device is removed from the usb bus.
 ****************************************************************************/
3031
static void edge_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
3032
{
3033
	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
L
Linus Torvalds 已提交
3034

3035
	if (edge_serial->is_epic) {
3036 3037
		usb_kill_urb(edge_serial->interrupt_read_urb);
		usb_kill_urb(edge_serial->read_urb);
3038
	}
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
}


/****************************************************************************
 * edge_release
 *	This function is called when the device structure is deallocated.
 ****************************************************************************/
static void edge_release(struct usb_serial *serial)
{
	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3049

3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
	if (edge_serial->is_epic) {
		usb_kill_urb(edge_serial->interrupt_read_urb);
		usb_free_urb(edge_serial->interrupt_read_urb);
		kfree(edge_serial->interrupt_in_buffer);

		usb_kill_urb(edge_serial->read_urb);
		usb_free_urb(edge_serial->read_urb);
		kfree(edge_serial->bulk_in_buffer);
	}

3060
	kfree(edge_serial);
L
Linus Torvalds 已提交
3061 3062
}

3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
static int edge_port_probe(struct usb_serial_port *port)
{
	struct edgeport_port *edge_port;

	edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
	if (!edge_port)
		return -ENOMEM;

	spin_lock_init(&edge_port->ep_lock);
	edge_port->port = port;

	usb_set_serial_port_data(port, edge_port);

	return 0;
}

static int edge_port_remove(struct usb_serial_port *port)
{
	struct edgeport_port *edge_port;

	edge_port = usb_get_serial_port_data(port);
	kfree(edge_port);

	return 0;
}

3089 3090 3091 3092 3093 3094 3095 3096
static struct usb_serial_driver edgeport_2port_device = {
	.driver = {
		.owner		= THIS_MODULE,
		.name		= "edgeport_2",
	},
	.description		= "Edgeport 2 port adapter",
	.id_table		= edgeport_2port_id_table,
	.num_ports		= 2,
3097 3098 3099
	.num_bulk_in		= 1,
	.num_bulk_out		= 1,
	.num_interrupt_in	= 1,
3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.disconnect		= edge_disconnect,
	.release		= edge_release,
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
A
Al Viro 已提交
3113
	.get_serial		= get_serial_info,
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
	.get_icount		= usb_serial_generic_get_icount,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_data_callback,
};

static struct usb_serial_driver edgeport_4port_device = {
	.driver = {
		.owner		= THIS_MODULE,
		.name		= "edgeport_4",
	},
	.description		= "Edgeport 4 port adapter",
	.id_table		= edgeport_4port_id_table,
	.num_ports		= 4,
3133 3134 3135
	.num_bulk_in		= 1,
	.num_bulk_out		= 1,
	.num_interrupt_in	= 1,
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.disconnect		= edge_disconnect,
	.release		= edge_release,
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
A
Al Viro 已提交
3149
	.get_serial		= get_serial_info,
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
	.get_icount		= usb_serial_generic_get_icount,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_data_callback,
};

static struct usb_serial_driver edgeport_8port_device = {
	.driver = {
		.owner		= THIS_MODULE,
		.name		= "edgeport_8",
	},
	.description		= "Edgeport 8 port adapter",
	.id_table		= edgeport_8port_id_table,
	.num_ports		= 8,
3169 3170 3171
	.num_bulk_in		= 1,
	.num_bulk_out		= 1,
	.num_interrupt_in	= 1,
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.disconnect		= edge_disconnect,
	.release		= edge_release,
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
A
Al Viro 已提交
3185
	.get_serial		= get_serial_info,
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
	.get_icount		= usb_serial_generic_get_icount,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_data_callback,
};

static struct usb_serial_driver epic_device = {
	.driver = {
		.owner		= THIS_MODULE,
		.name		= "epic",
	},
	.description		= "EPiC device",
	.id_table		= Epic_port_id_table,
	.num_ports		= 1,
3205 3206 3207
	.num_bulk_in		= 1,
	.num_bulk_out		= 1,
	.num_interrupt_in	= 1,
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
	.open			= edge_open,
	.close			= edge_close,
	.throttle		= edge_throttle,
	.unthrottle		= edge_unthrottle,
	.attach			= edge_startup,
	.disconnect		= edge_disconnect,
	.release		= edge_release,
	.port_probe		= edge_port_probe,
	.port_remove		= edge_port_remove,
	.ioctl			= edge_ioctl,
	.set_termios		= edge_set_termios,
	.tiocmget		= edge_tiocmget,
	.tiocmset		= edge_tiocmset,
A
Al Viro 已提交
3221
	.get_serial		= get_serial_info,
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
	.get_icount		= usb_serial_generic_get_icount,
	.write			= edge_write,
	.write_room		= edge_write_room,
	.chars_in_buffer	= edge_chars_in_buffer,
	.break_ctl		= edge_break,
	.read_int_callback	= edge_interrupt_callback,
	.read_bulk_callback	= edge_bulk_in_callback,
	.write_bulk_callback	= edge_bulk_out_data_callback,
};

static struct usb_serial_driver * const serial_drivers[] = {
	&edgeport_2port_device, &edgeport_4port_device,
	&edgeport_8port_device, &epic_device, NULL
};

3238
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
3239

A
Alan Cox 已提交
3240 3241
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
3242
MODULE_LICENSE("GPL");
3243 3244 3245 3246
MODULE_FIRMWARE("edgeport/boot.fw");
MODULE_FIRMWARE("edgeport/boot2.fw");
MODULE_FIRMWARE("edgeport/down.fw");
MODULE_FIRMWARE("edgeport/down2.fw");