io_edgeport.c 93.5 KB
Newer Older
L
Linus Torvalds 已提交
1 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 40 41 42 43 44
/*
 * Edgeport USB Serial Converter driver
 *
 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 * 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/init.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>
45 46
#include <linux/firmware.h>
#include <linux/ihex.h>
A
Alan Cox 已提交
47
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
48
#include <linux/usb.h>
49
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#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 CHASE_TIMEOUT		(5*HZ)		/* 5 seconds */
#define OPEN_TIMEOUT		(5*HZ)		/* 5 seconds */
#define COMMAND_TIMEOUT		(5*HZ)		/* 5 seconds */

/* receive port state */
enum RXSTATE {
A
Alan Cox 已提交
65 66 67 68
	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 已提交
69 70 71
};


A
Alan Cox 已提交
72 73 74
/* 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 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 * 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 */
92
	bool			write_in_progress;	/* 'true' while a write URB is outstanding */
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103
	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;

104 105 106 107 108
	bool			open;
	bool			openPending;
	bool			commandPending;
	bool			closePending;
	bool			chaseResponsePending;
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119

	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 {
120
	char			name[MAX_NAME_LEN+2];		/* string name of this device */
L
Linus Torvalds 已提交
121 122 123 124

	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 */
125 126
	struct edge_compatibility_descriptor epic_descriptor;	/* Edgeport compatible descriptor */
	int			is_epic;			/* flag if EPiC device or not */
L
Linus Torvalds 已提交
127 128

	__u8			interrupt_in_endpoint;		/* the interrupt endpoint handle */
A
Alan Cox 已提交
129 130
	unsigned char		*interrupt_in_buffer;		/* the buffer we use for the interrupt endpoint */
	struct urb		*interrupt_read_urb;		/* our interrupt urb */
L
Linus Torvalds 已提交
131 132

	__u8			bulk_in_endpoint;		/* the bulk in endpoint handle */
A
Alan Cox 已提交
133 134
	unsigned char		*bulk_in_buffer;		/* the buffer we use for the bulk in endpoint */
	struct urb		*read_urb;			/* our bulk read urb */
135
	bool			read_in_progress;
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	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 已提交
159 160 161 162 163 164
/*
 * Define table of divisors for Rev A EdgePort/4 hardware
 * These assume a 3.6864MHz crystal, the standard /16, and
 * MCR.7 = 0.
 */

165
static const struct divisor_table_entry divisor_table[] = {
A
Alan Cox 已提交
166 167 168 169
	{   50,		4608},
	{   75,		3072},
	{   110,	2095},	/* 2094.545455 => 230450   => .0217 % over */
	{   134,	1713},	/* 1713.011152 => 230398.5 => .00065% under */
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	{   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},
};

187 188
/* Number of outstanding Command Write Urbs */
static atomic_t CmdUrbs = ATOMIC_INIT(0);
L
Linus Torvalds 已提交
189 190 191 192 193


/* local function prototypes */

/* function prototypes for all URB callbacks */
A
Alan Cox 已提交
194 195 196 197
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 已提交
198 199

/* function prototypes for the usbserial callbacks */
200
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
201
static void edge_close(struct usb_serial_port *port);
A
Alan Cox 已提交
202 203 204 205 206 207 208 209 210
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);
211
static int  edge_ioctl(struct tty_struct *tty,
A
Alan Cox 已提交
212 213
					unsigned int cmd, unsigned long arg);
static void edge_break(struct tty_struct *tty, int break_state);
214
static int  edge_tiocmget(struct tty_struct *tty);
215
static int  edge_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
216 217
					unsigned int set, unsigned int clear);
static int  edge_startup(struct usb_serial *serial);
218 219
static void edge_disconnect(struct usb_serial *serial);
static void edge_release(struct usb_serial *serial);
220 221
static int edge_port_probe(struct usb_serial_port *port);
static int edge_port_remove(struct usb_serial_port *port);
L
Linus Torvalds 已提交
222 223 224 225

#include "io_tables.h"	/* all of the devices that this driver supports */

/* function prototypes for all of our local functions */
A
Alan Cox 已提交
226 227 228 229 230

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 已提交
231 232
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length);
A
Alan Cox 已提交
233 234 235 236 237
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);
238
static int  calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
A
Alan Cox 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
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 已提交
269 270 271 272 273 274 275 276 277

/************************************************************************
 *									*
 * update_edgeport_E2PROM()	Compare current versions of		*
 *				Boot ROM and Manufacture 		*
 *				Descriptors with versions		*
 *				embedded in this driver			*
 *									*
 ************************************************************************/
A
Alan Cox 已提交
278
static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
279
{
280
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
281 282
	__u32 BootCurVer;
	__u32 BootNewVer;
283 284 285 286 287 288 289
	__u8 BootMajorVersion;
	__u8 BootMinorVersion;
	__u16 BootBuildNumber;
	__u32 Bootaddr;
	const struct ihex_binrec *rec;
	const struct firmware *fw;
	const char *fw_name;
L
Linus Torvalds 已提交
290 291 292
	int response;

	switch (edge_serial->product_info.iDownloadFile) {
A
Alan Cox 已提交
293 294 295 296 297 298 299 300
	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 已提交
301 302
	}

303 304 305
	response = request_ihex_firmware(&fw, fw_name,
					 &edge_serial->serial->dev->dev);
	if (response) {
306
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
307 308 309 310 311 312 313 314 315
		       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 已提交
316
	/* Check Boot Image Version */
L
Linus Torvalds 已提交
317 318 319 320 321 322
	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) +
323
		      BootBuildNumber;
L
Linus Torvalds 已提交
324

325
	dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
L
Linus Torvalds 已提交
326 327 328 329 330 331
	    edge_serial->boot_descriptor.MajorVersion,
	    edge_serial->boot_descriptor.MinorVersion,
	    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));


	if (BootNewVer > BootCurVer) {
332
		dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
L
Linus Torvalds 已提交
333 334 335
		    edge_serial->boot_descriptor.MajorVersion,
		    edge_serial->boot_descriptor.MinorVersion,
		    le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
336
		    BootMajorVersion, BootMinorVersion, BootBuildNumber);
L
Linus Torvalds 已提交
337

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

340 341 342 343 344 345 346 347
		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 已提交
348
			if (response < 0) {
349 350 351 352
				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 已提交
353 354 355 356
				break;
			}
		}
	} else {
357
		dev_dbg(dev, "Boot Image -- already up to date\n");
L
Linus Torvalds 已提交
358
	}
359
	release_firmware(fw);
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367
}

#if 0
/************************************************************************
 *
 *  Get string descriptor from device
 *
 ************************************************************************/
A
Alan Cox 已提交
368 369
static int get_string_desc(struct usb_device *dev, int Id,
				struct usb_string_descriptor **pRetDesc)
L
Linus Torvalds 已提交
370 371 372 373
{
	struct usb_string_descriptor StringDesc;
	struct usb_string_descriptor *pStringDesc;

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

A
Alan Cox 已提交
376 377
	if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
						sizeof(StringDesc)))
L
Linus Torvalds 已提交
378 379
		return 0;

A
Alan Cox 已提交
380 381
	pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
	if (!pStringDesc)
L
Linus Torvalds 已提交
382 383
		return -1;

A
Alan Cox 已提交
384 385
	if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
							StringDesc.bLength)) {
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394
		kfree(pStringDesc);
		return -1;
	}

	*pRetDesc = pStringDesc;
	return 0;
}
#endif

395 396
static void dump_product_info(struct edgeport_serial *edge_serial,
			      struct edgeport_product_info *product_info)
397
{
398 399
	struct device *dev = &edge_serial->serial->dev->dev;

A
Alan Cox 已提交
400
	/* Dump Product Info structure */
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	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);
428 429
}

L
Linus Torvalds 已提交
430 431 432 433
static void get_product_info(struct edgeport_serial *edge_serial)
{
	struct edgeport_product_info *product_info = &edge_serial->product_info;

A
Alan Cox 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	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;
462

A
Alan Cox 已提交
463
	/* Determine Product type and set appropriate flags */
L
Linus Torvalds 已提交
464
	switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
A
Alan Cox 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477
	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 已提交
478

A
Alan Cox 已提交
479 480 481 482
	case ION_DEVICE_ID_EDGEPORT_2I:	/* Edgeport/2 RS422/RS485 */
		product_info->IsRS422 = 1;
		product_info->IsRS485 = 1;
		break;
L
Linus Torvalds 已提交
483

A
Alan Cox 已提交
484 485 486 487
	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 已提交
488 489
	}

490
	dump_product_info(edge_serial, product_info);
491
}
L
Linus Torvalds 已提交
492

493 494 495 496 497 498 499
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;
	struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
	struct edge_compatibility_bits *bits;
500
	struct device *dev = &serial->dev->dev;
501 502 503 504 505 506 507 508 509 510 511 512 513

	ep->is_epic = 0;
	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				 USB_REQUEST_ION_GET_EPIC_DESC,
				 0xC0, 0x00, 0x00,
				 &ep->epic_descriptor,
				 sizeof(struct edge_compatibility_descriptor),
				 300);

	if (result > 0) {
		ep->is_epic = 1;
		memset(product_info, 0, sizeof(struct edgeport_product_info));

A
Alan Cox 已提交
514 515 516 517 518 519 520 521 522
		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;
523
		dump_product_info(ep, product_info);
524 525

		bits = &ep->epic_descriptor.Supports;
526 527 528 529 530 531 532 533 534 535 536 537 538 539
		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");
540 541 542
	}

	return result;
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554
}


/************************************************************************/
/************************************************************************/
/*            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 已提交
555
 *	this is the callback function for when we have received data on the
L
Linus Torvalds 已提交
556 557
 *	interrupt endpoint.
 *****************************************************************************/
A
Alan Cox 已提交
558
static void edge_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
559
{
560 561
	struct edgeport_serial *edge_serial = urb->context;
	struct device *dev;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570
	struct edgeport_port *edge_port;
	struct usb_serial_port *port;
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int bytes_avail;
	int position;
	int txCredits;
	int portNumber;
	int result;
571
	int status = urb->status;
L
Linus Torvalds 已提交
572

573
	switch (status) {
L
Linus Torvalds 已提交
574 575 576 577 578 579 580
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
581
		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
L
Linus Torvalds 已提交
582 583
		return;
	default:
584
		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
L
Linus Torvalds 已提交
585 586 587
		goto exit;
	}

588 589
	dev = &edge_serial->serial->dev->dev;

A
Alan Cox 已提交
590
	/* process this interrupt-read even if there are no ports open */
L
Linus Torvalds 已提交
591
	if (length) {
592
		usb_serial_debug_data(dev, __func__, length, data);
L
Linus Torvalds 已提交
593 594 595 596 597 598

		if (length > 1) {
			bytes_avail = data[0] | (data[1] << 8);
			if (bytes_avail) {
				spin_lock(&edge_serial->es_lock);
				edge_serial->rxBytesAvail += bytes_avail;
599 600 601 602 603
				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 已提交
604 605 606

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

A
Alan Cox 已提交
610 611
					/* we have pending bytes on the
					   bulk in pipe, send a request */
L
Linus Torvalds 已提交
612 613
					result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
					if (result) {
614 615 616
						dev_err(dev,
							"%s - usb_submit_urb(read bulk) failed with result = %d\n",
							__func__, result);
617
						edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625
					}
				}
				spin_unlock(&edge_serial->es_lock);
			}
		}
		/* grab the txcredits for the ports if available */
		position = 2;
		portNumber = 0;
A
Alan Cox 已提交
626 627
		while ((position < length) &&
				(portNumber < edge_serial->serial->num_ports)) {
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635
			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) {
					spin_lock(&edge_port->ep_lock);
					edge_port->txCredits += txCredits;
					spin_unlock(&edge_port->ep_lock);
636 637 638
					dev_dbg(dev, "%s - txcredits for port%d = %d\n",
						__func__, portNumber,
						edge_port->txCredits);
L
Linus Torvalds 已提交
639

A
Alan Cox 已提交
640 641
					/* tell the tty driver that something
					   has changed */
J
Jiri Slaby 已提交
642
					tty_port_tty_wakeup(&edge_port->port->port);
A
Alan Cox 已提交
643 644 645 646
					/* Since we have more credit, check
					   if more data can be sent */
					send_more_port_data(edge_serial,
								edge_port);
L
Linus Torvalds 已提交
647 648 649 650 651 652 653 654
				}
			}
			position += 2;
			++portNumber;
		}
	}

exit:
A
Alan Cox 已提交
655 656 657 658 659
	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 已提交
660 661 662 663 664
}


/*****************************************************************************
 * edge_bulk_in_callback
A
Alan Cox 已提交
665
 *	this is the callback function for when we have received data on the
L
Linus Torvalds 已提交
666 667
 *	bulk in endpoint.
 *****************************************************************************/
A
Alan Cox 已提交
668
static void edge_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
669
{
670
	struct edgeport_serial	*edge_serial = urb->context;
671
	struct device *dev;
L
Linus Torvalds 已提交
672
	unsigned char		*data = urb->transfer_buffer;
673
	int			retval;
L
Linus Torvalds 已提交
674
	__u16			raw_data_length;
675
	int status = urb->status;
L
Linus Torvalds 已提交
676

677
	if (status) {
678 679
		dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
			__func__, status);
680
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
681 682 683 684
		return;
	}

	if (urb->actual_length == 0) {
685
		dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
686
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
687 688 689
		return;
	}

690
	dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
691 692
	raw_data_length = urb->actual_length;

693
	usb_serial_debug_data(dev, __func__, raw_data_length, data);
L
Linus Torvalds 已提交
694 695 696 697 698 699

	spin_lock(&edge_serial->es_lock);

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

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

A
Alan Cox 已提交
703
	process_rcvd_data(edge_serial, data, urb->actual_length);
L
Linus Torvalds 已提交
704 705 706

	/* check to see if there's any more data for us to read */
	if (edge_serial->rxBytesAvail > 0) {
707
		dev_dbg(dev, "%s - posting a read\n", __func__);
708 709
		retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
		if (retval) {
710 711 712
			dev_err(dev,
				"%s - usb_submit_urb(read bulk) failed, retval = %d\n",
				__func__, retval);
713
			edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
714 715
		}
	} else {
716
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724
	}

	spin_unlock(&edge_serial->es_lock);
}


/*****************************************************************************
 * edge_bulk_out_data_callback
A
Alan Cox 已提交
725 726
 *	this is the callback function for when we have finished sending
 *	serial data on the bulk out endpoint.
L
Linus Torvalds 已提交
727
 *****************************************************************************/
A
Alan Cox 已提交
728
static void edge_bulk_out_data_callback(struct urb *urb)
L
Linus Torvalds 已提交
729
{
730
	struct edgeport_port *edge_port = urb->context;
731
	int status = urb->status;
L
Linus Torvalds 已提交
732

733
	if (status) {
734 735 736
		dev_dbg(&urb->dev->dev,
			"%s - nonzero write bulk status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
737 738
	}

J
Jiri Slaby 已提交
739 740
	if (edge_port->open)
		tty_port_tty_wakeup(&edge_port->port->port);
L
Linus Torvalds 已提交
741

A
Alan Cox 已提交
742
	/* Release the Write URB */
743
	edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
744

A
Alan Cox 已提交
745 746 747
	/* 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 已提交
748 749 750 751 752
}


/*****************************************************************************
 * BulkOutCmdCallback
A
Alan Cox 已提交
753 754
 *	this is the callback function for when we have finished sending a
 *	command	on the bulk out endpoint.
L
Linus Torvalds 已提交
755
 *****************************************************************************/
A
Alan Cox 已提交
756
static void edge_bulk_out_cmd_callback(struct urb *urb)
L
Linus Torvalds 已提交
757
{
758
	struct edgeport_port *edge_port = urb->context;
L
Linus Torvalds 已提交
759 760
	int status = urb->status;

761
	atomic_dec(&CmdUrbs);
762 763
	dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
		__func__, urb, atomic_read(&CmdUrbs));
L
Linus Torvalds 已提交
764 765 766


	/* clean up the transfer buffer */
767
	kfree(urb->transfer_buffer);
L
Linus Torvalds 已提交
768 769

	/* Free the command urb */
A
Alan Cox 已提交
770
	usb_free_urb(urb);
L
Linus Torvalds 已提交
771 772

	if (status) {
773 774 775
		dev_dbg(&urb->dev->dev,
			"%s - nonzero write bulk status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
776 777 778 779
		return;
	}

	/* tell the tty driver that something has changed */
J
Jiri Slaby 已提交
780 781
	if (edge_port->open)
		tty_port_tty_wakeup(&edge_port->port->port);
L
Linus Torvalds 已提交
782 783

	/* we have completed the command */
784
	edge_port->commandPending = false;
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798
	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.
 *****************************************************************************/
799
static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
800 801
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
802
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
803 804 805 806 807 808 809
	struct usb_serial *serial;
	struct edgeport_serial *edge_serial;
	int response;

	if (edge_port == NULL)
		return -ENODEV;

A
Alan Cox 已提交
810 811
	/* 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 已提交
812 813
	serial = port->serial;
	edge_serial = usb_get_serial_data(serial);
A
Alan Cox 已提交
814
	if (edge_serial == NULL)
L
Linus Torvalds 已提交
815 816 817
		return -ENODEV;
	if (edge_serial->interrupt_in_buffer == NULL) {
		struct usb_serial_port *port0 = serial->port[0];
A
Alan Cox 已提交
818

L
Linus Torvalds 已提交
819
		/* not set up yet, so do it now */
A
Alan Cox 已提交
820 821 822 823
		edge_serial->interrupt_in_buffer =
					port0->interrupt_in_buffer;
		edge_serial->interrupt_in_endpoint =
					port0->interrupt_in_endpointAddress;
L
Linus Torvalds 已提交
824 825
		edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
		edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
A
Alan Cox 已提交
826 827
		edge_serial->bulk_in_endpoint =
					port0->bulk_in_endpointAddress;
L
Linus Torvalds 已提交
828
		edge_serial->read_urb = port0->read_urb;
A
Alan Cox 已提交
829 830 831
		edge_serial->bulk_out_endpoint =
					port0->bulk_out_endpointAddress;

L
Linus Torvalds 已提交
832 833
		/* set up our interrupt urb */
		usb_fill_int_urb(edge_serial->interrupt_read_urb,
A
Alan Cox 已提交
834 835 836 837 838 839 840 841
		      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 已提交
842 843
		/* set up our bulk in urb */
		usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
A
Alan Cox 已提交
844 845 846 847 848
			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);
849
		edge_serial->read_in_progress = false;
L
Linus Torvalds 已提交
850 851

		/* start interrupt read for this edgeport
A
Alan Cox 已提交
852 853 854 855
		 * 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 已提交
856
		if (response) {
857 858
			dev_err(dev, "%s - Error %d submitting control urb\n",
				__func__, response);
L
Linus Torvalds 已提交
859 860
		}
	}
A
Alan Cox 已提交
861

L
Linus Torvalds 已提交
862 863 864 865 866 867
	/* 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 已提交
868 869 870
	edge_port->txCredits = 0;	/* Can't send any data yet */
	/* Must always set this bit to enable ints! */
	edge_port->shadowMCR = MCR_MASTER_IE;
871
	edge_port->chaseResponsePending = false;
L
Linus Torvalds 已提交
872 873

	/* send a open port command */
874 875
	edge_port->openPending = true;
	edge_port->open        = false;
A
Alan Cox 已提交
876
	response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
L
Linus Torvalds 已提交
877 878

	if (response < 0) {
879
		dev_err(dev, "%s - error sending open port command\n", __func__);
880
		edge_port->openPending = false;
L
Linus Torvalds 已提交
881 882 883 884
		return -ENODEV;
	}

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

888
	if (!edge_port->open) {
L
Linus Torvalds 已提交
889
		/* open timed out */
890
		dev_dbg(dev, "%s - open timedout\n", __func__);
891
		edge_port->openPending = false;
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899
		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 已提交
900
	edge_port->txfifo.fifo	= kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
L
Linus Torvalds 已提交
901 902

	if (!edge_port->txfifo.fifo) {
903
		dev_dbg(dev, "%s - no memory\n", __func__);
904
		edge_close(port);
L
Linus Torvalds 已提交
905 906 907 908
		return -ENOMEM;
	}

	/* Allocate a URB for the write */
A
Alan Cox 已提交
909
	edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
910
	edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
911 912

	if (!edge_port->write_urb) {
913
		dev_dbg(dev, "%s - no memory\n", __func__);
914
		edge_close(port);
L
Linus Torvalds 已提交
915 916 917
		return -ENOMEM;
	}

918 919
	dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
		__func__, port->number, edge_port->maxTxCredits);
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930

	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 已提交
931
 *		2. A timeout of 10 seconds without activity has expired
L
Linus Torvalds 已提交
932 933 934 935 936
 *		   (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
 *
 ************************************************************************/
static void block_until_chase_response(struct edgeport_port *edge_port)
{
937
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
938 939 940 941 942 943
	DEFINE_WAIT(wait);
	__u16 lastCredits;
	int timeout = 1*HZ;
	int loop = 10;

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

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

A
Alan Cox 已提交
951 952
			/* did we get all of our credit back? */
			if (edge_port->txCredits == edge_port->maxTxCredits) {
953
				dev_dbg(dev, "%s - Got all credits\n", __func__);
L
Linus Torvalds 已提交
954 955 956 957
				return;
			}
		}

A
Alan Cox 已提交
958 959 960
		/* Block the thread for a while */
		prepare_to_wait(&edge_port->wait_chase, &wait,
						TASK_UNINTERRUPTIBLE);
L
Linus Torvalds 已提交
961 962 963 964
		schedule_timeout(timeout);
		finish_wait(&edge_port->wait_chase, &wait);

		if (lastCredits == edge_port->txCredits) {
A
Alan Cox 已提交
965
			/* No activity.. count down. */
L
Linus Torvalds 已提交
966 967
			loop--;
			if (loop == 0) {
968
				edge_port->chaseResponsePending = false;
969
				dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
L
Linus Torvalds 已提交
970 971 972
				return;
			}
		} else {
A
Alan Cox 已提交
973
			/* Reset timeout value back to 10 seconds */
974
			dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
A
Alan Cox 已提交
975
					lastCredits, edge_port->txCredits);
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988
			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 已提交
989
 *		3. A timeout of 3 seconds without activity has expired
L
Linus Torvalds 已提交
990 991
 *
 ************************************************************************/
A
Alan Cox 已提交
992
static void block_until_tx_empty(struct edgeport_port *edge_port)
L
Linus Torvalds 已提交
993
{
994
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001
	DEFINE_WAIT(wait);
	struct TxFifo *fifo = &edge_port->txfifo;
	__u32 lastCount;
	int timeout = HZ/10;
	int loop = 30;

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

A
Alan Cox 已提交
1005
		/* Is the Edgeport Buffer empty? */
L
Linus Torvalds 已提交
1006
		if (lastCount == 0) {
1007
			dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
L
Linus Torvalds 已提交
1008 1009 1010
			return;
		}

A
Alan Cox 已提交
1011 1012 1013
		/* Block the thread for a while */
		prepare_to_wait(&edge_port->wait_chase, &wait,
						TASK_UNINTERRUPTIBLE);
L
Linus Torvalds 已提交
1014 1015 1016
		schedule_timeout(timeout);
		finish_wait(&edge_port->wait_chase, &wait);

1017
		dev_dbg(dev, "%s wait\n", __func__);
L
Linus Torvalds 已提交
1018 1019

		if (lastCount == fifo->count) {
A
Alan Cox 已提交
1020
			/* No activity.. count down. */
L
Linus Torvalds 已提交
1021 1022
			loop--;
			if (loop == 0) {
1023
				dev_dbg(dev, "%s - TIMEOUT\n", __func__);
L
Linus Torvalds 已提交
1024 1025 1026
				return;
			}
		} else {
A
Alan Cox 已提交
1027
			/* Reset timeout value back to seconds */
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
			loop = 30;
		}
	}
}


/*****************************************************************************
 * edge_close
 *	this function is called by the tty driver when a port is closed
 *****************************************************************************/
1038
static void edge_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044 1045
{
	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 已提交
1046
	if (edge_serial == NULL || edge_port == NULL)
L
Linus Torvalds 已提交
1047
		return;
A
Alan Cox 已提交
1048 1049

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

1052
	edge_port->closePending = true;
L
Linus Torvalds 已提交
1053

1054 1055 1056 1057
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPChase))) {
		/* flush and chase */
1058
		edge_port->chaseResponsePending = true;
1059

1060
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
A
Alan Cox 已提交
1061 1062 1063
		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
		if (status == 0)
			/* block until chase finished */
1064
			block_until_chase_response(edge_port);
A
Alan Cox 已提交
1065
		else
1066
			edge_port->chaseResponsePending = false;
L
Linus Torvalds 已提交
1067 1068
	}

1069 1070 1071 1072
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPClose))) {
	       /* close the port */
1073
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
A
Alan Cox 已提交
1074
		send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1075
	}
L
Linus Torvalds 已提交
1076

A
Alan Cox 已提交
1077
	/* port->close = true; */
1078 1079 1080
	edge_port->closePending = false;
	edge_port->open = false;
	edge_port->openPending = false;
L
Linus Torvalds 已提交
1081

1082
	usb_kill_urb(edge_port->write_urb);
L
Linus Torvalds 已提交
1083 1084

	if (edge_port->write_urb) {
A
Alan Cox 已提交
1085 1086
		/* if this urb had a transfer buffer already
				(old transfer) free it */
1087 1088
		kfree(edge_port->write_urb->transfer_buffer);
		usb_free_urb(edge_port->write_urb);
L
Linus Torvalds 已提交
1089 1090
		edge_port->write_urb = NULL;
	}
1091 1092
	kfree(edge_port->txfifo.fifo);
	edge_port->txfifo.fifo = NULL;
A
Alan Cox 已提交
1093
}
L
Linus Torvalds 已提交
1094 1095 1096

/*****************************************************************************
 * SerialWrite
A
Alan Cox 已提交
1097 1098 1099 1100
 *	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 已提交
1101
 *****************************************************************************/
A
Alan Cox 已提交
1102 1103
static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *data, int count)
L
Linus Torvalds 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
{
	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 已提交
1116
	/* get a pointer to the Tx fifo */
L
Linus Torvalds 已提交
1117 1118 1119 1120
	fifo = &edge_port->txfifo;

	spin_lock_irqsave(&edge_port->ep_lock, flags);

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

1125 1126
	dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room  %d -- will copy %d bytes\n",
		__func__, port->number, count,
A
Alan Cox 已提交
1127
			edge_port->txCredits - fifo->count, copySize);
L
Linus Torvalds 已提交
1128

A
Alan Cox 已提交
1129 1130
	/* catch writes of 0 bytes which the tty driver likes to give us,
	   and when txCredits is empty */
L
Linus Torvalds 已提交
1131
	if (copySize == 0) {
1132
		dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
L
Linus Torvalds 已提交
1133 1134 1135
		goto finish_write;
	}

A
Alan Cox 已提交
1136 1137 1138 1139 1140 1141 1142
	/* 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 已提交
1143
	bytesleft = fifo->size - fifo->head;
A
Alan Cox 已提交
1144
	firsthalf = min(bytesleft, copySize);
1145 1146
	dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
		firsthalf, bytesleft);
L
Linus Torvalds 已提交
1147 1148 1149

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

A
Alan Cox 已提交
1152
	/* update the index and size */
L
Linus Torvalds 已提交
1153 1154 1155
	fifo->head  += firsthalf;
	fifo->count += firsthalf;

A
Alan Cox 已提交
1156 1157
	/* wrap the index */
	if (fifo->head == fifo->size)
L
Linus Torvalds 已提交
1158 1159 1160 1161 1162
		fifo->head = 0;

	secondhalf = copySize-firsthalf;

	if (secondhalf) {
1163
		dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
L
Linus Torvalds 已提交
1164
		memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1165
		usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
A
Alan Cox 已提交
1166
		/* update the index and size */
L
Linus Torvalds 已提交
1167 1168
		fifo->count += secondhalf;
		fifo->head  += secondhalf;
A
Alan Cox 已提交
1169 1170 1171
		/* No need to check for wrap since we can not get to end of
		 * the fifo in this part
		 */
L
Linus Torvalds 已提交
1172 1173 1174 1175 1176
	}

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

A
Alan Cox 已提交
1177 1178
	send_more_port_data((struct edgeport_serial *)
			usb_get_serial_data(port->serial), edge_port);
L
Linus Torvalds 已提交
1179

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

A
Alan Cox 已提交
1183
	return copySize;
L
Linus Torvalds 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
}


/************************************************************************
 *
 * 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 已提交
1200 1201
static void send_more_port_data(struct edgeport_serial *edge_serial,
					struct edgeport_port *edge_port)
L
Linus Torvalds 已提交
1202 1203
{
	struct TxFifo	*fifo = &edge_port->txfifo;
1204
	struct device	*dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	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)) {
1219 1220 1221
		dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
			__func__, edge_port->port->number,
			fifo->count, edge_port->write_in_progress);
L
Linus Torvalds 已提交
1222 1223 1224
		goto exit_send;
	}

A
Alan Cox 已提交
1225 1226 1227 1228 1229 1230 1231 1232
	/* 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)) {
1233
		dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
A
Alan Cox 已提交
1234 1235
			__func__, edge_port->port->number, fifo->count,
			edge_port->txCredits);
L
Linus Torvalds 已提交
1236 1237 1238
		goto exit_send;
	}

A
Alan Cox 已提交
1239
	/* lock this write */
1240
	edge_port->write_in_progress = true;
L
Linus Torvalds 已提交
1241

A
Alan Cox 已提交
1242
	/* get a pointer to the write_urb */
L
Linus Torvalds 已提交
1243 1244
	urb = edge_port->write_urb;

1245 1246 1247
	/* make sure transfer buffer is freed */
	kfree(urb->transfer_buffer);
	urb->transfer_buffer = NULL;
L
Linus Torvalds 已提交
1248

A
Alan Cox 已提交
1249 1250
	/* build the data header for the buffer and port that we are about
	   to send out */
L
Linus Torvalds 已提交
1251
	count = fifo->count;
A
Alan Cox 已提交
1252
	buffer = kmalloc(count+2, GFP_ATOMIC);
L
Linus Torvalds 已提交
1253
	if (buffer == NULL) {
1254
		dev_err_console(edge_port->port,
A
Alan Cox 已提交
1255
				"%s - no more kernel memory...\n", __func__);
1256
		edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
1257 1258
		goto exit_send;
	}
A
Alan Cox 已提交
1259 1260 1261 1262
	buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
				- edge_port->port->serial->minor, count);
	buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
				- edge_port->port->serial->minor, count);
L
Linus Torvalds 已提交
1263 1264 1265

	/* now copy our data */
	bytesleft =  fifo->size - fifo->tail;
A
Alan Cox 已提交
1266
	firsthalf = min(bytesleft, count);
L
Linus Torvalds 已提交
1267 1268 1269
	memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
	fifo->tail  += firsthalf;
	fifo->count -= firsthalf;
A
Alan Cox 已提交
1270
	if (fifo->tail == fifo->size)
L
Linus Torvalds 已提交
1271 1272 1273 1274
		fifo->tail = 0;

	secondhalf = count-firsthalf;
	if (secondhalf) {
A
Alan Cox 已提交
1275 1276
		memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
								secondhalf);
L
Linus Torvalds 已提交
1277 1278 1279 1280 1281
		fifo->tail  += secondhalf;
		fifo->count -= secondhalf;
	}

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

	/* fill up the urb with all of our data and submit it */
A
Alan Cox 已提交
1285 1286 1287 1288 1289
	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 已提交
1290 1291 1292

	/* decrement the number of credits we have by the number we just sent */
	edge_port->txCredits -= count;
1293
	edge_port->port->icount.tx += count;
L
Linus Torvalds 已提交
1294 1295 1296 1297

	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status) {
		/* something went wrong */
1298
		dev_err_console(edge_port->port,
A
Alan Cox 已提交
1299 1300
			"%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
				__func__, status);
1301
		edge_port->write_in_progress = false;
L
Linus Torvalds 已提交
1302 1303 1304

		/* revert the credits as something bad happened. */
		edge_port->txCredits += count;
1305
		edge_port->port->icount.tx -= count;
L
Linus Torvalds 已提交
1306
	}
1307 1308
	dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
		__func__, count, edge_port->txCredits, fifo->count);
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315 1316

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


/*****************************************************************************
 * edge_write_room
A
Alan Cox 已提交
1317 1318 1319 1320
 *	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 已提交
1321
 *****************************************************************************/
A
Alan Cox 已提交
1322
static int edge_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1323
{
A
Alan Cox 已提交
1324
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1325 1326 1327 1328 1329
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int room;
	unsigned long flags;

	if (edge_port == NULL)
1330
		return 0;
1331
	if (edge_port->closePending)
1332
		return 0;
L
Linus Torvalds 已提交
1333 1334

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

A
Alan Cox 已提交
1339
	/* total of both buffers is still txCredit */
L
Linus Torvalds 已提交
1340 1341 1342 1343
	spin_lock_irqsave(&edge_port->ep_lock, flags);
	room = edge_port->txCredits - edge_port->txfifo.count;
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

1344
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
1345 1346 1347 1348 1349 1350
	return room;
}


/*****************************************************************************
 * edge_chars_in_buffer
A
Alan Cox 已提交
1351 1352 1353 1354 1355
 *	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 已提交
1356 1357
 *	Otherwise we return a negative error number.
 *****************************************************************************/
A
Alan Cox 已提交
1358
static int edge_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1359
{
A
Alan Cox 已提交
1360
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int num_chars;
	unsigned long flags;

	if (edge_port == NULL)
1366
		return 0;
1367
	if (edge_port->closePending)
1368
		return 0;
L
Linus Torvalds 已提交
1369 1370

	if (!edge_port->open) {
1371
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1372
		return 0;
L
Linus Torvalds 已提交
1373 1374 1375
	}

	spin_lock_irqsave(&edge_port->ep_lock, flags);
A
Alan Cox 已提交
1376 1377
	num_chars = edge_port->maxTxCredits - edge_port->txCredits +
						edge_port->txfifo.count;
L
Linus Torvalds 已提交
1378 1379
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);
	if (num_chars) {
1380 1381
		dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
			port->number, num_chars);
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	}

	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 已提交
1393
static void edge_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1394
{
A
Alan Cox 已提交
1395
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	if (edge_port == NULL)
		return;

	if (!edge_port->open) {
1403
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409
		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 已提交
1410 1411
		status = edge_write(tty, port, &stop_char, 1);
		if (status <= 0)
L
Linus Torvalds 已提交
1412 1413 1414 1415
			return;
	}

	/* if we are implementing RTS/CTS, toggle that line */
1416
	if (tty->termios.c_cflag & CRTSCTS) {
L
Linus Torvalds 已提交
1417
		edge_port->shadowMCR &= ~MCR_RTS;
A
Alan Cox 已提交
1418 1419 1420
		status = send_cmd_write_uart_register(edge_port, MCR,
							edge_port->shadowMCR);
		if (status != 0)
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427
			return;
	}
}


/*****************************************************************************
 * edge_unthrottle
A
Alan Cox 已提交
1428 1429
 *	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 已提交
1430
 *****************************************************************************/
A
Alan Cox 已提交
1431
static void edge_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
1432
{
A
Alan Cox 已提交
1433
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439 1440
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	int status;

	if (edge_port == NULL)
		return;

	if (!edge_port->open) {
1441
		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
1442 1443 1444 1445 1446 1447
		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 已提交
1448 1449
		status = edge_write(tty, port, &start_char, 1);
		if (status <= 0)
L
Linus Torvalds 已提交
1450 1451 1452
			return;
	}
	/* if we are implementing RTS/CTS, toggle that line */
1453
	if (tty->termios.c_cflag & CRTSCTS) {
L
Linus Torvalds 已提交
1454
		edge_port->shadowMCR |= MCR_RTS;
A
Alan Cox 已提交
1455 1456
		send_cmd_write_uart_register(edge_port, MCR,
						edge_port->shadowMCR);
L
Linus Torvalds 已提交
1457 1458 1459 1460 1461 1462
	}
}


/*****************************************************************************
 * SerialSetTermios
A
Alan Cox 已提交
1463 1464
 *	this function is called by the tty driver when it wants to change
 * the termios structure
L
Linus Torvalds 已提交
1465
 *****************************************************************************/
A
Alan Cox 已提交
1466 1467
static void edge_set_termios(struct tty_struct *tty,
	struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1468 1469 1470 1471
{
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
	unsigned int cflag;

1472
	cflag = tty->termios.c_cflag;
1473
	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
1474
	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479

	if (edge_port == NULL)
		return;

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

	/* change the port settings to the new ones specified */
A
Alan Cox 已提交
1485
	change_port_settings(tty, edge_port, old_termios);
L
Linus Torvalds 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
}


/*****************************************************************************
 * 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 已提交
1497
 * 	    allows an RS485 driver to be written in user space.
L
Linus Torvalds 已提交
1498
 *****************************************************************************/
A
Alan Cox 已提交
1499 1500
static int get_lsr_info(struct edgeport_port *edge_port,
						unsigned int __user *value)
L
Linus Torvalds 已提交
1501 1502 1503 1504 1505 1506 1507
{
	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) {
1508
		dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517
		result = TIOCSER_TEMT;
	}
	spin_unlock_irqrestore(&edge_port->ep_lock, flags);

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

1518
static int edge_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
1519
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1520
{
A
Alan Cox 已提交
1521
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	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;
}

1547
static int edge_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
1548
{
A
Alan Cox 已提交
1549
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	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
Alan Cox 已提交
1567 1568
static int get_serial_info(struct edgeport_port *edge_port,
				struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
{
	struct serial_struct tmp;

	if (!retinfo)
		return -EFAULT;

	memset(&tmp, 0, sizeof(tmp));

	tmp.type		= PORT_16550A;
	tmp.line		= edge_port->port->serial->minor;
	tmp.port		= edge_port->port->number;
	tmp.irq			= 0;
	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
	tmp.xmit_fifo_size	= edge_port->maxTxCredits;
	tmp.baud_base		= 9600;
	tmp.close_delay		= 5*HZ;
	tmp.closing_wait	= 30*HZ;

	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return 0;
}


/*****************************************************************************
 * SerialIoctl
 *	this function handles any ioctl calls to the driver
 *****************************************************************************/
1597
static int edge_ioctl(struct tty_struct *tty,
A
Alan Cox 已提交
1598
					unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1599
{
A
Alan Cox 已提交
1600
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1601 1602 1603
	DEFINE_WAIT(wait);
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);

1604
	dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
L
Linus Torvalds 已提交
1605 1606

	switch (cmd) {
A
Alan Cox 已提交
1607
	case TIOCSERGETLSR:
1608
		dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__,  port->number);
A
Alan Cox 已提交
1609 1610 1611
		return get_lsr_info(edge_port, (unsigned int __user *) arg);

	case TIOCGSERIAL:
1612
		dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__,  port->number);
A
Alan Cox 已提交
1613
		return get_serial_info(edge_port, (struct serial_struct __user *) arg);
L
Linus Torvalds 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622
	}
	return -ENOIOCTLCMD;
}


/*****************************************************************************
 * SerialBreak
 *	this function sends a break to the port
 *****************************************************************************/
A
Alan Cox 已提交
1623
static void edge_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
1624
{
A
Alan Cox 已提交
1625
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1626
	struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1627
	struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
L
Linus Torvalds 已提交
1628 1629
	int status;

1630 1631 1632 1633
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPChase))) {
		/* flush and chase */
1634
		edge_port->chaseResponsePending = true;
1635

1636
		dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
A
Alan Cox 已提交
1637
		status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1638
		if (status == 0) {
A
Alan Cox 已提交
1639
			/* block until chase finished */
1640 1641
			block_until_chase_response(edge_port);
		} else {
1642
			edge_port->chaseResponsePending = false;
1643
		}
L
Linus Torvalds 已提交
1644 1645
	}

1646 1647 1648 1649
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
		if (break_state == -1) {
1650
			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
A
Alan Cox 已提交
1651 1652
			status = send_iosp_ext_cmd(edge_port,
						IOSP_CMD_SET_BREAK, 0);
1653
		} else {
1654
			dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
A
Alan Cox 已提交
1655 1656
			status = send_iosp_ext_cmd(edge_port,
						IOSP_CMD_CLEAR_BREAK, 0);
1657
		}
A
Alan Cox 已提交
1658
		if (status)
1659
			dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
A
Alan Cox 已提交
1660
				__func__);
L
Linus Torvalds 已提交
1661 1662 1663 1664 1665 1666 1667 1668
	}
}


/*****************************************************************************
 * process_rcvd_data
 *	this function handles the data received on the bulk in pipe.
 *****************************************************************************/
A
Alan Cox 已提交
1669 1670
static void process_rcvd_data(struct edgeport_serial *edge_serial,
				unsigned char *buffer, __u16 bufferLength)
L
Linus Torvalds 已提交
1671
{
1672
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	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) {
1683
			dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
L
Linus Torvalds 已提交
1684 1685 1686 1687 1688
			break;
		}
		lastBufferLength = bufferLength;

		switch (edge_serial->rxState) {
A
Alan Cox 已提交
1689 1690 1691 1692
		case EXPECT_HDR1:
			edge_serial->rxHeader1 = *buffer;
			++buffer;
			--bufferLength;
L
Linus Torvalds 已提交
1693

A
Alan Cox 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
			if (bufferLength == 0) {
				edge_serial->rxState = EXPECT_HDR2;
				break;
			}
			/* otherwise, drop on through */
		case EXPECT_HDR2:
			edge_serial->rxHeader2 = *buffer;
			++buffer;
			--bufferLength;

1704 1705
			dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
				edge_serial->rxHeader1, edge_serial->rxHeader2);
A
Alan Cox 已提交
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
			/* 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 已提交
1729 1730
					break;
				}
A
Alan Cox 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
				/* 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;
			} else {
				edge_serial->rxPort =
				    IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
				edge_serial->rxBytesRemaining =
				    IOSP_GET_HDR_DATA_LEN(
						edge_serial->rxHeader1,
						edge_serial->rxHeader2);
1744 1745 1746 1747
				dev_dbg(dev, "%s - Data for Port %u Len %u\n",
					__func__,
					edge_serial->rxPort,
					edge_serial->rxBytesRemaining);
A
Alan Cox 已提交
1748 1749 1750 1751 1752

				/* ASSERT(DevExt->RxPort < DevExt->NumPorts);
				 * ASSERT(DevExt->RxBytesRemaining <
				 *		IOSP_MAX_DATA_LENGTH);
				 */
L
Linus Torvalds 已提交
1753

A
Alan Cox 已提交
1754 1755
				if (bufferLength == 0) {
					edge_serial->rxState = EXPECT_DATA;
L
Linus Torvalds 已提交
1756 1757
					break;
				}
A
Alan Cox 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
				/* Else, drop through */
			}
		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 已提交
1771

A
Alan Cox 已提交
1772 1773
			bufferLength -= rxLen;
			edge_serial->rxBytesRemaining -= rxLen;
L
Linus Torvalds 已提交
1774

A
Alan Cox 已提交
1775 1776 1777 1778 1779 1780 1781
			/* 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 已提交
1782 1783 1784 1785 1786
					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);
1787
					edge_port->port->icount.rx += rxLen;
L
Linus Torvalds 已提交
1788
				}
A
Alan Cox 已提交
1789 1790 1791
				buffer += rxLen;
			}
			break;
L
Linus Torvalds 已提交
1792

A
Alan Cox 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
		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 已提交
1805 1806 1807 1808 1809 1810 1811
		}
	}
}


/*****************************************************************************
 * process_rcvd_status
A
Alan Cox 已提交
1812 1813
 *	this function handles the any status messages received on the
 *	bulk in pipe.
L
Linus Torvalds 已提交
1814
 *****************************************************************************/
A
Alan Cox 已提交
1815 1816
static void process_rcvd_status(struct edgeport_serial *edge_serial,
						__u8 byte2, __u8 byte3)
L
Linus Torvalds 已提交
1817 1818 1819
{
	struct usb_serial_port *port;
	struct edgeport_port *edge_port;
A
Alan Cox 已提交
1820
	struct tty_struct *tty;
1821
	struct device *dev;
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827
	__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 已提交
1828 1829 1830
		dev_err(&edge_serial->serial->dev->dev,
			"%s - edge_port == NULL for port %d\n",
					__func__, edge_serial->rxPort);
L
Linus Torvalds 已提交
1831 1832
		return;
	}
1833
	dev = &port->dev;
L
Linus Torvalds 已提交
1834 1835 1836

	if (code == IOSP_EXT_STATUS) {
		switch (byte2) {
A
Alan Cox 已提交
1837 1838 1839
		case IOSP_EXT_STATUS_CHASE_RSP:
			/* we want to do EXT status regardless of port
			 * open/closed */
1840 1841
			dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
				__func__, edge_serial->rxPort, byte3);
A
Alan Cox 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
			/* 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 已提交
1854

A
Alan Cox 已提交
1855
		case IOSP_EXT_STATUS_RX_CHECK_RSP:
1856 1857
			dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
				__func__, edge_serial->rxPort, byte3);
A
Alan Cox 已提交
1858 1859
			/* Port->RxCheckRsp = true; */
			return;
L
Linus Torvalds 已提交
1860 1861 1862 1863 1864 1865
		}
	}

	if (code == IOSP_STATUS_OPEN_RSP) {
		edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
		edge_port->maxTxCredits = edge_port->txCredits;
1866 1867
		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 已提交
1868
		handle_new_msr(edge_port, byte2);
L
Linus Torvalds 已提交
1869

A
Alan Cox 已提交
1870 1871
		/* send the current line settings to the port so we are
		   in sync with any further termios calls */
A
Alan Cox 已提交
1872 1873 1874
		tty = tty_port_tty_get(&edge_port->port->port);
		if (tty) {
			change_port_settings(tty,
1875
				edge_port, &tty->termios);
A
Alan Cox 已提交
1876 1877
			tty_kref_put(tty);
		}
L
Linus Torvalds 已提交
1878 1879

		/* we have completed the open */
1880 1881
		edge_port->openPending = false;
		edge_port->open = true;
L
Linus Torvalds 已提交
1882 1883 1884 1885
		wake_up(&edge_port->wait_open);
		return;
	}

A
Alan Cox 已提交
1886 1887 1888 1889 1890
	/* 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 已提交
1891 1892 1893
		return;

	switch (code) {
A
Alan Cox 已提交
1894 1895
	/* Not currently sent by Edgeport */
	case IOSP_STATUS_LSR:
1896 1897
		dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
			__func__, edge_serial->rxPort, byte2);
A
Alan Cox 已提交
1898 1899
		handle_new_lsr(edge_port, false, byte2, 0);
		break;
L
Linus Torvalds 已提交
1900

A
Alan Cox 已提交
1901
	case IOSP_STATUS_LSR_DATA:
1902 1903
		dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
			__func__, edge_serial->rxPort, byte2, byte3);
A
Alan Cox 已提交
1904 1905 1906 1907 1908 1909
		/* byte2 is LSR Register */
		/* byte3 is broken data byte */
		handle_new_lsr(edge_port, true, byte2, byte3);
		break;
	/*
	 *	case IOSP_EXT_4_STATUS:
1910
	 *		dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
A
Alan Cox 已提交
1911 1912 1913 1914
	 *			__func__, edge_serial->rxPort, byte2, byte3);
	 *		break;
	 */
	case IOSP_STATUS_MSR:
1915 1916
		dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
			__func__, edge_serial->rxPort, byte2);
A
Alan Cox 已提交
1917 1918 1919 1920 1921 1922 1923
		/*
		 * 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 已提交
1924

A
Alan Cox 已提交
1925
	default:
1926
		dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
A
Alan Cox 已提交
1927
		break;
L
Linus Torvalds 已提交
1928 1929 1930 1931 1932 1933 1934 1935
	}
}


/*****************************************************************************
 * edge_tty_recv
 *	this function passes data on to the tty flip buffer
 *****************************************************************************/
J
Jiri Slaby 已提交
1936 1937
static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
		int length)
L
Linus Torvalds 已提交
1938 1939 1940
{
	int cnt;

J
Jiri Slaby 已提交
1941
	cnt = tty_insert_flip_string(&port->port, data, length);
1942
	if (cnt < length) {
J
Jiri Slaby 已提交
1943
		dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1944 1945 1946 1947
				__func__, length - cnt);
	}
	data += cnt;
	length -= cnt;
L
Linus Torvalds 已提交
1948

J
Jiri Slaby 已提交
1949
	tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
}


/*****************************************************************************
 * 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 已提交
1961 1962
	if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
			EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1963
		icount = &edge_port->port->icount;
L
Linus Torvalds 已提交
1964 1965

		/* update input line counters */
A
Alan Cox 已提交
1966
		if (newMsr & EDGEPORT_MSR_DELTA_CTS)
L
Linus Torvalds 已提交
1967
			icount->cts++;
A
Alan Cox 已提交
1968
		if (newMsr & EDGEPORT_MSR_DELTA_DSR)
L
Linus Torvalds 已提交
1969
			icount->dsr++;
A
Alan Cox 已提交
1970
		if (newMsr & EDGEPORT_MSR_DELTA_CD)
L
Linus Torvalds 已提交
1971
			icount->dcd++;
A
Alan Cox 已提交
1972
		if (newMsr & EDGEPORT_MSR_DELTA_RI)
L
Linus Torvalds 已提交
1973
			icount->rng++;
1974
		wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
	}

	/* 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 已提交
1986 1987
static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
							__u8 lsr, __u8 data)
L
Linus Torvalds 已提交
1988
{
A
Alan Cox 已提交
1989 1990 1991
	__u8 newLsr = (__u8) (lsr & (__u8)
		(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
	struct async_icount *icount;
L
Linus Torvalds 已提交
1992 1993 1994 1995

	edge_port->shadowLSR = lsr;

	if (newLsr & LSR_BREAK) {
A
Alan Cox 已提交
1996 1997 1998 1999 2000
		/*
		 * Parity and Framing errors only count if they
		 * occur exclusive of a break being
		 * received.
		 */
L
Linus Torvalds 已提交
2001 2002 2003 2004
		newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
	}

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

L
Linus Torvalds 已提交
2008
	/* update input line counters */
2009
	icount = &edge_port->port->icount;
A
Alan Cox 已提交
2010
	if (newLsr & LSR_BREAK)
L
Linus Torvalds 已提交
2011
		icount->brk++;
A
Alan Cox 已提交
2012
	if (newLsr & LSR_OVER_ERR)
L
Linus Torvalds 已提交
2013
		icount->overrun++;
A
Alan Cox 已提交
2014
	if (newLsr & LSR_PAR_ERR)
L
Linus Torvalds 已提交
2015
		icount->parity++;
A
Alan Cox 已提交
2016
	if (newLsr & LSR_FRM_ERR)
L
Linus Torvalds 已提交
2017 2018 2019 2020 2021 2022
		icount->frame++;
}


/****************************************************************************
 * sram_write
A
Alan Cox 已提交
2023
 *	writes a number of bytes to the Edgeport device's sram starting at the
L
Linus Torvalds 已提交
2024 2025 2026 2027
 *	given address.
 *	If successful returns the number of bytes written, otherwise it returns
 *	a negative error number of the problem.
 ****************************************************************************/
A
Alan Cox 已提交
2028 2029
static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data)
L
Linus Torvalds 已提交
2030 2031 2032 2033 2034
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

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

A
Alan Cox 已提交
2037
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
L
Linus Torvalds 已提交
2038
	if (!transfer_buffer) {
A
Alan Cox 已提交
2039 2040
		dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
							__func__, 64);
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045 2046
		return -ENOMEM;
	}

	/* need to split these writes up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2047
		if (length > 64)
L
Linus Torvalds 已提交
2048
			current_length = 64;
A
Alan Cox 已提交
2049
		else
L
Linus Torvalds 已提交
2050
			current_length = length;
A
Alan Cox 已提交
2051

2052
/*		dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
A
Alan Cox 已提交
2053 2054 2055 2056 2057 2058
		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 已提交
2059 2060 2061 2062 2063
		if (result < 0)
			break;
		length -= current_length;
		addr += current_length;
		data += current_length;
A
Alan Cox 已提交
2064
	}
L
Linus Torvalds 已提交
2065

A
Alan Cox 已提交
2066
	kfree(transfer_buffer);
L
Linus Torvalds 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
	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 已提交
2078 2079
static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
					__u16 length, const __u8 *data)
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

A
Alan Cox 已提交
2085
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
L
Linus Torvalds 已提交
2086
	if (!transfer_buffer) {
A
Alan Cox 已提交
2087 2088
		dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
								__func__, 64);
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094
		return -ENOMEM;
	}

	/* need to split these writes up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2095
		if (length > 64)
L
Linus Torvalds 已提交
2096
			current_length = 64;
A
Alan Cox 已提交
2097
		else
L
Linus Torvalds 已提交
2098
			current_length = length;
A
Alan Cox 已提交
2099 2100 2101 2102 2103 2104
		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 已提交
2105 2106 2107 2108 2109
		if (result < 0)
			break;
		length -= current_length;
		addr += current_length;
		data += current_length;
A
Alan Cox 已提交
2110
	}
L
Linus Torvalds 已提交
2111

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


/****************************************************************************
 * rom_read
 *	reads a number of bytes from the Edgeport device starting at the given
 *	address.
 *	If successful returns the number of bytes read, otherwise it returns
 *	a negative error number of the problem.
 ****************************************************************************/
A
Alan Cox 已提交
2124 2125
static int rom_read(struct usb_serial *serial, __u16 extAddr,
					__u16 addr, __u16 length, __u8 *data)
L
Linus Torvalds 已提交
2126 2127 2128 2129 2130
{
	int result;
	__u16 current_length;
	unsigned char *transfer_buffer;

A
Alan Cox 已提交
2131
	transfer_buffer =  kmalloc(64, GFP_KERNEL);
L
Linus Torvalds 已提交
2132
	if (!transfer_buffer) {
A
Alan Cox 已提交
2133 2134
		dev_err(&serial->dev->dev,
			"%s - kmalloc(%d) failed.\n", __func__, 64);
L
Linus Torvalds 已提交
2135 2136 2137 2138 2139 2140
		return -ENOMEM;
	}

	/* need to split these reads up into 64 byte chunks */
	result = 0;
	while (length > 0) {
A
Alan Cox 已提交
2141
		if (length > 64)
L
Linus Torvalds 已提交
2142
			current_length = 64;
A
Alan Cox 已提交
2143
		else
L
Linus Torvalds 已提交
2144
			current_length = length;
A
Alan Cox 已提交
2145 2146 2147 2148 2149
		result = usb_control_msg(serial->dev,
					usb_rcvctrlpipe(serial->dev, 0),
					USB_REQUEST_ION_READ_ROM,
					0xC0, addr, extAddr, transfer_buffer,
					current_length, 300);
L
Linus Torvalds 已提交
2150 2151
		if (result < 0)
			break;
A
Alan Cox 已提交
2152
		memcpy(data, transfer_buffer, current_length);
L
Linus Torvalds 已提交
2153 2154 2155
		length -= current_length;
		addr += current_length;
		data += current_length;
A
Alan Cox 已提交
2156
	}
L
Linus Torvalds 已提交
2157

A
Alan Cox 已提交
2158
	kfree(transfer_buffer);
L
Linus Torvalds 已提交
2159 2160 2161 2162 2163 2164 2165 2166
	return result;
}


/****************************************************************************
 * send_iosp_ext_cmd
 *	Is used to send a IOSP message to the Edgeport device
 ****************************************************************************/
A
Alan Cox 已提交
2167 2168
static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
						__u8 command, __u8 param)
L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174
{
	unsigned char   *buffer;
	unsigned char   *currentCommand;
	int             length = 0;
	int             status = 0;

A
Alan Cox 已提交
2175
	buffer = kmalloc(10, GFP_ATOMIC);
L
Linus Torvalds 已提交
2176
	if (!buffer) {
A
Alan Cox 已提交
2177 2178
		dev_err(&edge_port->port->dev,
				"%s - kmalloc(%d) failed.\n", __func__, 10);
L
Linus Torvalds 已提交
2179 2180 2181 2182 2183
		return -ENOMEM;
	}

	currentCommand = buffer;

A
Alan Cox 已提交
2184 2185 2186
	MAKE_CMD_EXT_CMD(&currentCommand, &length,
		edge_port->port->number - edge_port->port->serial->minor,
		command, param);
L
Linus Torvalds 已提交
2187

A
Alan Cox 已提交
2188
	status = write_cmd_usb(edge_port, buffer, length);
L
Linus Torvalds 已提交
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
	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 已提交
2202 2203
static int write_cmd_usb(struct edgeport_port *edge_port,
					unsigned char *buffer, int length)
L
Linus Torvalds 已提交
2204
{
A
Alan Cox 已提交
2205 2206
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2207
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2208 2209 2210
	int status = 0;
	struct urb *urb;

2211
	usb_serial_debug_data(dev, __func__, length, buffer);
L
Linus Torvalds 已提交
2212 2213

	/* Allocate our next urb */
A
Alan Cox 已提交
2214
	urb = usb_alloc_urb(0, GFP_ATOMIC);
L
Linus Torvalds 已提交
2215 2216 2217
	if (!urb)
		return -ENOMEM;

2218
	atomic_inc(&CmdUrbs);
2219 2220
	dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
		__func__, urb, atomic_read(&CmdUrbs));
L
Linus Torvalds 已提交
2221

A
Alan Cox 已提交
2222 2223 2224 2225
	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 已提交
2226

2227
	edge_port->commandPending = true;
L
Linus Torvalds 已提交
2228 2229 2230 2231
	status = usb_submit_urb(urb, GFP_ATOMIC);

	if (status) {
		/* something went wrong */
2232 2233
		dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
			__func__, status);
L
Linus Torvalds 已提交
2234 2235
		usb_kill_urb(urb);
		usb_free_urb(urb);
2236
		atomic_dec(&CmdUrbs);
L
Linus Torvalds 已提交
2237 2238 2239 2240
		return status;
	}

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

2243
	if (edge_port->commandPending) {
L
Linus Torvalds 已提交
2244
		/* command timed out */
2245
		dev_dbg(dev, "%s - command timed out\n", __func__);
L
Linus Torvalds 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
		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 已提交
2258 2259
static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
								int baudRate)
L
Linus Torvalds 已提交
2260
{
A
Alan Cox 已提交
2261 2262
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2263
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2264 2265 2266 2267 2268
	unsigned char *cmdBuffer;
	unsigned char *currCmd;
	int cmdLen = 0;
	int divisor;
	int status;
A
Alan Cox 已提交
2269 2270
	unsigned char number =
		edge_port->port->number - edge_port->port->serial->minor;
L
Linus Torvalds 已提交
2271

2272 2273
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2274 2275
		dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
			edge_port->port->number, baudRate);
2276 2277 2278
		return 0;
	}

2279 2280
	dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
		edge_port->port->number, baudRate);
L
Linus Torvalds 已提交
2281

2282
	status = calc_baud_rate_divisor(dev, baudRate, &divisor);
L
Linus Torvalds 已提交
2283
	if (status) {
2284
		dev_err(dev, "%s - bad baud rate\n", __func__);
L
Linus Torvalds 已提交
2285 2286 2287
		return status;
	}

A
Alan Cox 已提交
2288 2289
	/* Alloc memory for the string of commands. */
	cmdBuffer =  kmalloc(0x100, GFP_ATOMIC);
L
Linus Torvalds 已提交
2290
	if (!cmdBuffer) {
2291
		dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
L
Linus Torvalds 已提交
2292 2293 2294 2295
		return -ENOMEM;
	}
	currCmd = cmdBuffer;

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

A
Alan Cox 已提交
2299 2300 2301
	/* 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 已提交
2302

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

A
Alan Cox 已提交
2307
	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
L
Linus Torvalds 已提交
2308 2309
	if (status) {
		/* something bad happened, let's free up the memory */
A
Alan Cox 已提交
2310
		kfree(cmdBuffer);
L
Linus Torvalds 已提交
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
	}

	return status;
}


/*****************************************************************************
 * calc_baud_rate_divisor
 *	this function calculates the proper baud rate divisor for the specified
 *	baud rate.
 *****************************************************************************/
2322
static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
L
Linus Torvalds 已提交
2323 2324 2325 2326
{
	int i;
	__u16 custom;

2327
	for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
A
Alan Cox 已提交
2328
		if (divisor_table[i].BaudRate == baudrate) {
L
Linus Torvalds 已提交
2329 2330 2331 2332 2333
			*divisor = divisor_table[i].Divisor;
			return 0;
		}
	}

A
Alan Cox 已提交
2334 2335 2336
	/* 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 已提交
2337
	if (baudrate > 50 && baudrate < 230400) {
A
Alan Cox 已提交
2338
		/* get divisor */
L
Linus Torvalds 已提交
2339 2340 2341 2342
		custom = (__u16)((230400L + baudrate/2) / baudrate);

		*divisor = custom;

2343
		dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
L
Linus Torvalds 已提交
2344 2345 2346 2347 2348 2349 2350 2351 2352
		return 0;
	}

	return -1;
}


/*****************************************************************************
 * send_cmd_write_uart_register
2353
 *  this function builds up a uart register message and sends to the device.
L
Linus Torvalds 已提交
2354
 *****************************************************************************/
A
Alan Cox 已提交
2355 2356
static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
						__u8 regNum, __u8 regValue)
L
Linus Torvalds 已提交
2357
{
A
Alan Cox 已提交
2358 2359
	struct edgeport_serial *edge_serial =
				usb_get_serial_data(edge_port->port->serial);
2360
	struct device *dev = &edge_port->port->dev;
L
Linus Torvalds 已提交
2361 2362 2363 2364 2365
	unsigned char *cmdBuffer;
	unsigned char *currCmd;
	unsigned long cmdLen = 0;
	int status;

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

2369 2370 2371
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
	    regNum == MCR) {
2372
		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2373 2374 2375
		return 0;
	}

2376 2377 2378
	if (edge_serial->is_epic &&
	    !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
	    regNum == LCR) {
2379
		dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2380 2381 2382
		return 0;
	}

A
Alan Cox 已提交
2383 2384 2385
	/* Alloc memory for the string of commands. */
	cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
	if (cmdBuffer == NULL)
L
Linus Torvalds 已提交
2386 2387 2388 2389
		return -ENOMEM;

	currCmd = cmdBuffer;

A
Alan Cox 已提交
2390 2391 2392 2393
	/* Build a cmd in the buffer to write the given register */
	MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
		edge_port->port->number - edge_port->port->serial->minor,
		regNum, regValue);
L
Linus Torvalds 已提交
2394 2395 2396 2397

	status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
	if (status) {
		/* something bad happened, let's free up the memory */
A
Alan Cox 已提交
2398
		kfree(cmdBuffer);
L
Linus Torvalds 已提交
2399 2400 2401 2402 2403 2404 2405 2406
	}

	return status;
}


/*****************************************************************************
 * change_port_settings
A
Alan Cox 已提交
2407 2408
 *	This routine is called to set the UART on the device to match the
 *	specified new settings.
L
Linus Torvalds 已提交
2409
 *****************************************************************************/
A
Alan Cox 已提交
2410 2411 2412

static void change_port_settings(struct tty_struct *tty,
	struct edgeport_port *edge_port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
2413
{
2414
	struct device *dev = &edge_port->port->dev;
A
Alan Cox 已提交
2415 2416
	struct edgeport_serial *edge_serial =
			usb_get_serial_data(edge_port->port->serial);
L
Linus Torvalds 已提交
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
	int baud;
	unsigned cflag;
	__u8 mask = 0xff;
	__u8 lData;
	__u8 lParity;
	__u8 lStop;
	__u8 rxFlow;
	__u8 txFlow;
	int status;

2427
	dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
L
Linus Torvalds 已提交
2428

2429 2430
	if (!edge_port->open &&
	    !edge_port->openPending) {
2431
		dev_dbg(dev, "%s - port not opened\n", __func__);
L
Linus Torvalds 已提交
2432 2433 2434
		return;
	}

2435
	cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
2436 2437

	switch (cflag & CSIZE) {
A
Alan Cox 已提交
2438 2439
	case CS5:
		lData = LCR_BITS_5; mask = 0x1f;
2440
		dev_dbg(dev, "%s - data bits = 5\n", __func__);
A
Alan Cox 已提交
2441 2442 2443
		break;
	case CS6:
		lData = LCR_BITS_6; mask = 0x3f;
2444
		dev_dbg(dev, "%s - data bits = 6\n", __func__);
A
Alan Cox 已提交
2445 2446 2447
		break;
	case CS7:
		lData = LCR_BITS_7; mask = 0x7f;
2448
		dev_dbg(dev, "%s - data bits = 7\n", __func__);
A
Alan Cox 已提交
2449 2450 2451 2452
		break;
	default:
	case CS8:
		lData = LCR_BITS_8;
2453
		dev_dbg(dev, "%s - data bits = 8\n", __func__);
A
Alan Cox 已提交
2454
		break;
L
Linus Torvalds 已提交
2455 2456 2457 2458 2459 2460 2461
	}

	lParity = LCR_PAR_NONE;
	if (cflag & PARENB) {
		if (cflag & CMSPAR) {
			if (cflag & PARODD) {
				lParity = LCR_PAR_MARK;
2462
				dev_dbg(dev, "%s - parity = mark\n", __func__);
L
Linus Torvalds 已提交
2463 2464
			} else {
				lParity = LCR_PAR_SPACE;
2465
				dev_dbg(dev, "%s - parity = space\n", __func__);
L
Linus Torvalds 已提交
2466 2467 2468
			}
		} else if (cflag & PARODD) {
			lParity = LCR_PAR_ODD;
2469
			dev_dbg(dev, "%s - parity = odd\n", __func__);
L
Linus Torvalds 已提交
2470 2471
		} else {
			lParity = LCR_PAR_EVEN;
2472
			dev_dbg(dev, "%s - parity = even\n", __func__);
L
Linus Torvalds 已提交
2473 2474
		}
	} else {
2475
		dev_dbg(dev, "%s - parity = none\n", __func__);
L
Linus Torvalds 已提交
2476 2477 2478 2479
	}

	if (cflag & CSTOPB) {
		lStop = LCR_STOP_2;
2480
		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
L
Linus Torvalds 已提交
2481 2482
	} else {
		lStop = LCR_STOP_1;
2483
		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
L
Linus Torvalds 已提交
2484 2485 2486 2487 2488 2489 2490
	}

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

A
Alan Cox 已提交
2496 2497
	/* if we are implementing XON/XOFF, set the start and stop character
	   in the device */
L
Linus Torvalds 已提交
2498 2499 2500 2501
	if (I_IXOFF(tty) || I_IXON(tty)) {
		unsigned char stop_char  = STOP_CHAR(tty);
		unsigned char start_char = START_CHAR(tty);

2502 2503 2504
		if ((!edge_serial->is_epic) ||
		    ((edge_serial->is_epic) &&
		     (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
A
Alan Cox 已提交
2505 2506 2507 2508
			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);
2509
		}
L
Linus Torvalds 已提交
2510 2511 2512 2513

		/* if we are implementing INBOUND XON/XOFF */
		if (I_IXOFF(tty)) {
			rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2514 2515
			dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
				__func__, start_char, stop_char);
L
Linus Torvalds 已提交
2516
		} else {
2517
			dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2518 2519 2520 2521 2522
		}

		/* if we are implementing OUTBOUND XON/XOFF */
		if (I_IXON(tty)) {
			txFlow |= IOSP_TX_FLOW_XON_XOFF;
2523 2524
			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
				__func__, start_char, stop_char);
L
Linus Torvalds 已提交
2525
		} else {
2526
			dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
L
Linus Torvalds 已提交
2527 2528 2529 2530
		}
	}

	/* Set flow control to the configured value */
2531 2532 2533 2534 2535 2536 2537 2538
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
	if ((!edge_serial->is_epic) ||
	    ((edge_serial->is_epic) &&
	     (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
		send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543 2544 2545 2546


	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 已提交
2547 2548 2549
	status = send_cmd_write_uart_register(edge_port, LCR,
							edge_port->shadowLCR);
	if (status != 0)
L
Linus Torvalds 已提交
2550 2551 2552 2553
		return;

	/* set up the MCR register and send it to the EdgePort */
	edge_port->shadowMCR = MCR_MASTER_IE;
A
Alan Cox 已提交
2554
	if (cflag & CBAUD)
L
Linus Torvalds 已提交
2555
		edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
A
Alan Cox 已提交
2556 2557 2558 2559

	status = send_cmd_write_uart_register(edge_port, MCR,
						edge_port->shadowMCR);
	if (status != 0)
L
Linus Torvalds 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568
		return;

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

2569
	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
A
Alan Cox 已提交
2570
	status = send_cmd_write_baud_rate(edge_port, baud);
2571 2572 2573 2574 2575
	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 已提交
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
}


/****************************************************************************
 * 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 已提交
2586 2587
static void unicode_to_ascii(char *string, int buflen,
					__le16 *unicode, int unicode_size)
L
Linus Torvalds 已提交
2588 2589 2590
{
	int i;

2591
	if (buflen <= 0)	/* never happens, but... */
L
Linus Torvalds 已提交
2592
		return;
2593
	--buflen;		/* space for nul */
L
Linus Torvalds 已提交
2594

2595 2596 2597
	for (i = 0; i < unicode_size; i++) {
		if (i >= buflen)
			break;
L
Linus Torvalds 已提交
2598
		string[i] = (char)(le16_to_cpu(unicode[i]));
2599 2600
	}
	string[i] = 0x00;
L
Linus Torvalds 已提交
2601 2602 2603 2604 2605
}


/****************************************************************************
 * get_manufacturing_desc
A
Alan Cox 已提交
2606
 *	reads in the manufacturing descriptor and stores it into the serial
L
Linus Torvalds 已提交
2607 2608
 *	structure.
 ****************************************************************************/
A
Alan Cox 已提交
2609
static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2610
{
2611
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
2612 2613
	int response;

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

A
Alan Cox 已提交
2616 2617 2618 2619 2620
	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 已提交
2621

A
Alan Cox 已提交
2622
	if (response < 1)
2623
		dev_err(dev, "error in getting manufacturer descriptor\n");
A
Alan Cox 已提交
2624
	else {
L
Linus Torvalds 已提交
2625
		char string[30];
2626 2627
		dev_dbg(dev, "**Manufacturer Descriptor\n");
		dev_dbg(dev, "  RomSize:        %dK\n",
A
Alan Cox 已提交
2628
			edge_serial->manuf_descriptor.RomSize);
2629
		dev_dbg(dev, "  RamSize:        %dK\n",
A
Alan Cox 已提交
2630
			edge_serial->manuf_descriptor.RamSize);
2631
		dev_dbg(dev, "  CpuRev:         %d\n",
A
Alan Cox 已提交
2632
			edge_serial->manuf_descriptor.CpuRev);
2633
		dev_dbg(dev, "  BoardRev:       %d\n",
A
Alan Cox 已提交
2634
			edge_serial->manuf_descriptor.BoardRev);
2635
		dev_dbg(dev, "  NumPorts:       %d\n",
A
Alan Cox 已提交
2636
			edge_serial->manuf_descriptor.NumPorts);
2637
		dev_dbg(dev, "  DescDate:       %d/%d/%d\n",
A
Alan Cox 已提交
2638 2639 2640
			edge_serial->manuf_descriptor.DescDate[0],
			edge_serial->manuf_descriptor.DescDate[1],
			edge_serial->manuf_descriptor.DescDate[2]+1900);
P
Pete Zaitcev 已提交
2641
		unicode_to_ascii(string, sizeof(string),
A
Alan Cox 已提交
2642 2643
			edge_serial->manuf_descriptor.SerialNumber,
			edge_serial->manuf_descriptor.SerNumLength/2);
2644
		dev_dbg(dev, "  SerialNumber: %s\n", string);
P
Pete Zaitcev 已提交
2645
		unicode_to_ascii(string, sizeof(string),
A
Alan Cox 已提交
2646 2647
			edge_serial->manuf_descriptor.AssemblyNumber,
			edge_serial->manuf_descriptor.AssemblyNumLength/2);
2648
		dev_dbg(dev, "  AssemblyNumber: %s\n", string);
P
Pete Zaitcev 已提交
2649
		unicode_to_ascii(string, sizeof(string),
2650 2651
		    edge_serial->manuf_descriptor.OemAssyNumber,
		    edge_serial->manuf_descriptor.OemAssyNumLength/2);
2652 2653
		dev_dbg(dev, "  OemAssyNumber:  %s\n", string);
		dev_dbg(dev, "  UartType:       %d\n",
A
Alan Cox 已提交
2654
			edge_serial->manuf_descriptor.UartType);
2655
		dev_dbg(dev, "  IonPid:         %d\n",
A
Alan Cox 已提交
2656
			edge_serial->manuf_descriptor.IonPid);
2657
		dev_dbg(dev, "  IonConfig:      %d\n",
A
Alan Cox 已提交
2658
			edge_serial->manuf_descriptor.IonConfig);
L
Linus Torvalds 已提交
2659 2660 2661 2662 2663 2664
	}
}


/****************************************************************************
 * get_boot_desc
A
Alan Cox 已提交
2665
 *	reads in the bootloader descriptor and stores it into the serial
L
Linus Torvalds 已提交
2666 2667
 *	structure.
 ****************************************************************************/
A
Alan Cox 已提交
2668
static void get_boot_desc(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2669
{
2670
	struct device *dev = &edge_serial->serial->dev->dev;
L
Linus Torvalds 已提交
2671 2672
	int response;

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

A
Alan Cox 已提交
2675 2676 2677 2678 2679
	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 已提交
2680

A
Alan Cox 已提交
2681
	if (response < 1)
2682
		dev_err(dev, "error in getting boot descriptor\n");
A
Alan Cox 已提交
2683
	else {
2684 2685 2686 2687
		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 已提交
2688
			edge_serial->boot_descriptor.MajorVersion);
2689
		dev_dbg(dev, "  MinorVersion:   %d\n",
A
Alan Cox 已提交
2690
			edge_serial->boot_descriptor.MinorVersion);
2691
		dev_dbg(dev, "  BuildNumber:    %d\n",
A
Alan Cox 已提交
2692
			le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2693
		dev_dbg(dev, "  Capabilities:   0x%x\n",
A
Alan Cox 已提交
2694
		      le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2695
		dev_dbg(dev, "  UConfig0:       %d\n",
A
Alan Cox 已提交
2696
			edge_serial->boot_descriptor.UConfig0);
2697
		dev_dbg(dev, "  UConfig1:       %d\n",
A
Alan Cox 已提交
2698
			edge_serial->boot_descriptor.UConfig1);
L
Linus Torvalds 已提交
2699 2700 2701 2702 2703 2704 2705 2706
	}
}


/****************************************************************************
 * load_application_firmware
 *	This is called to load the application firmware to the device
 ****************************************************************************/
A
Alan Cox 已提交
2707
static void load_application_firmware(struct edgeport_serial *edge_serial)
L
Linus Torvalds 已提交
2708
{
2709
	struct device *dev = &edge_serial->serial->dev->dev;
2710 2711 2712 2713
	const struct ihex_binrec *rec;
	const struct firmware *fw;
	const char *fw_name;
	const char *fw_info;
L
Linus Torvalds 已提交
2714
	int response;
2715 2716
	__u32 Operaddr;
	__u16 build;
L
Linus Torvalds 已提交
2717 2718 2719

	switch (edge_serial->product_info.iDownloadFile) {
		case EDGE_DOWNLOAD_FILE_I930:
2720 2721
			fw_info = "downloading firmware version (930)";
			fw_name	= "edgeport/down.fw";
L
Linus Torvalds 已提交
2722 2723 2724
			break;

		case EDGE_DOWNLOAD_FILE_80251:
2725 2726
			fw_info = "downloading firmware version (80251)";
			fw_name	= "edgeport/down2.fw";
L
Linus Torvalds 已提交
2727 2728 2729
			break;

		case EDGE_DOWNLOAD_FILE_NONE:
2730
			dev_dbg(dev, "No download file specified, skipping download\n");
L
Linus Torvalds 已提交
2731 2732 2733 2734 2735 2736
			return;

		default:
			return;
	}

2737 2738 2739
	response = request_ihex_firmware(&fw, fw_name,
				    &edge_serial->serial->dev->dev);
	if (response) {
2740
		dev_err(dev, "Failed to load image \"%s\" err %d\n",
2741 2742 2743 2744 2745 2746 2747
		       fw_name, response);
		return;
	}

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

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

2750 2751
	edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
	edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2752
	edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
L
Linus Torvalds 已提交
2753

2754 2755 2756 2757 2758 2759 2760 2761
	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 已提交
2762
		if (response < 0) {
2763 2764 2765 2766
			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 已提交
2767 2768 2769 2770
			break;
		}
	}

2771 2772 2773 2774
	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 已提交
2775 2776
				    0x40, 0x4000, 0x0001, NULL, 0, 3000);

2777
	release_firmware(fw);
L
Linus Torvalds 已提交
2778 2779 2780 2781 2782 2783
}


/****************************************************************************
 * edge_startup
 ****************************************************************************/
A
Alan Cox 已提交
2784
static int edge_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
2785 2786 2787
{
	struct edgeport_serial *edge_serial;
	struct usb_device *dev;
2788
	struct device *ddev = &serial->dev->dev;
2789
	int i;
2790
	int response;
2791 2792 2793
	bool interrupt_in_found;
	bool bulk_in_found;
	bool bulk_out_found;
2794 2795 2796
	static __u32 descriptor[3] = {	EDGE_COMPATIBILITY_MASK0,
					EDGE_COMPATIBILITY_MASK1,
					EDGE_COMPATIBILITY_MASK2 };
L
Linus Torvalds 已提交
2797 2798 2799 2800

	dev = serial->dev;

	/* create our private serial structure */
2801
	edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
L
Linus Torvalds 已提交
2802
	if (edge_serial == NULL) {
2803
		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
L
Linus Torvalds 已提交
2804 2805 2806 2807 2808 2809 2810
		return -ENOMEM;
	}
	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 */
2811
	i = usb_string(dev, dev->descriptor.iManufacturer,
2812
	    &edge_serial->name[0], MAX_NAME_LEN+1);
2813 2814
	if (i < 0)
		i = 0;
2815
	edge_serial->name[i++] = ' ';
2816
	usb_string(dev, dev->descriptor.iProduct,
2817
	    &edge_serial->name[i], MAX_NAME_LEN+2 - i);
L
Linus Torvalds 已提交
2818 2819 2820

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

2821 2822 2823 2824 2825
	/* Read the epic descriptor */
	if (get_epic_descriptor(edge_serial) <= 0) {
		/* memcpy descriptor to Supports structures */
		memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
		       sizeof(struct edge_compatibility_bits));
L
Linus Torvalds 已提交
2826

2827
		/* get the manufacturing descriptor for this device */
A
Alan Cox 已提交
2828
		get_manufacturing_desc(edge_serial);
L
Linus Torvalds 已提交
2829

2830
		/* get the boot descriptor */
A
Alan Cox 已提交
2831
		get_boot_desc(edge_serial);
2832 2833 2834

		get_product_info(edge_serial);
	}
L
Linus Torvalds 已提交
2835 2836 2837

	/* set the number of ports from the manufacturing description */
	/* serial->num_ports = serial->product_info.NumPorts; */
2838 2839
	if ((!edge_serial->is_epic) &&
	    (edge_serial->product_info.NumPorts != serial->num_ports)) {
2840 2841
		dev_warn(ddev,
			"Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2842 2843
			 edge_serial->product_info.NumPorts,
			 serial->num_ports);
L
Linus Torvalds 已提交
2844 2845
	}

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

2848 2849 2850
	/* If not an EPiC device */
	if (!edge_serial->is_epic) {
		/* now load the application firmware into this device */
A
Alan Cox 已提交
2851
		load_application_firmware(edge_serial);
L
Linus Torvalds 已提交
2852

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

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

2858
		dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2859 2860

		/* set the configuration to use #1 */
2861
/*		dev_dbg(ddev, "set_configuration 1\n"); */
A
Alan Cox 已提交
2862
/*		usb_set_configuration (dev, 1); */
2863
	}
2864
	dev_dbg(ddev, "  FirmwareMajorVersion  %d.%d.%d\n",
2865 2866 2867
	    edge_serial->product_info.FirmwareMajorVersion,
	    edge_serial->product_info.FirmwareMinorVersion,
	    le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
L
Linus Torvalds 已提交
2868

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

2872 2873 2874
	response = 0;

	if (edge_serial->is_epic) {
A
Alan Cox 已提交
2875 2876
		/* EPIC thing, set up our interrupt polling now and our read
		 * urb, so that the device knows it really is connected. */
2877
		interrupt_in_found = bulk_in_found = bulk_out_found = false;
A
Alan Cox 已提交
2878 2879
		for (i = 0; i < serial->interface->altsetting[0]
						.desc.bNumEndpoints; ++i) {
2880 2881 2882
			struct usb_endpoint_descriptor *endpoint;
			int buffer_size;

A
Alan Cox 已提交
2883 2884
			endpoint = &serial->interface->altsetting[0].
							endpoint[i].desc;
2885
			buffer_size = usb_endpoint_maxp(endpoint);
2886
			if (!interrupt_in_found &&
2887 2888
			    (usb_endpoint_is_int_in(endpoint))) {
				/* we found a interrupt in endpoint */
2889
				dev_dbg(ddev, "found interrupt in\n");
2890 2891

				/* not set up yet, so do it now */
A
Alan Cox 已提交
2892 2893
				edge_serial->interrupt_read_urb =
						usb_alloc_urb(0, GFP_KERNEL);
2894
				if (!edge_serial->interrupt_read_urb) {
2895
					dev_err(ddev, "out of memory\n");
2896 2897
					return -ENOMEM;
				}
A
Alan Cox 已提交
2898 2899
				edge_serial->interrupt_in_buffer =
					kmalloc(buffer_size, GFP_KERNEL);
2900
				if (!edge_serial->interrupt_in_buffer) {
2901
					dev_err(ddev, "out of memory\n");
2902 2903 2904
					usb_free_urb(edge_serial->interrupt_read_urb);
					return -ENOMEM;
				}
A
Alan Cox 已提交
2905 2906
				edge_serial->interrupt_in_endpoint =
						endpoint->bEndpointAddress;
2907 2908

				/* set up our interrupt urb */
A
Alan Cox 已提交
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
				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);
2919

2920
				interrupt_in_found = true;
2921 2922
			}

2923
			if (!bulk_in_found &&
A
Alan Cox 已提交
2924
				(usb_endpoint_is_bulk_in(endpoint))) {
2925
				/* we found a bulk in endpoint */
2926
				dev_dbg(ddev, "found bulk in\n");
2927 2928

				/* not set up yet, so do it now */
A
Alan Cox 已提交
2929 2930
				edge_serial->read_urb =
						usb_alloc_urb(0, GFP_KERNEL);
2931
				if (!edge_serial->read_urb) {
2932
					dev_err(ddev, "out of memory\n");
2933 2934
					return -ENOMEM;
				}
A
Alan Cox 已提交
2935 2936
				edge_serial->bulk_in_buffer =
					kmalloc(buffer_size, GFP_KERNEL);
2937
				if (!edge_serial->bulk_in_buffer) {
2938
					dev_err(&dev->dev, "out of memory\n");
2939 2940 2941
					usb_free_urb(edge_serial->read_urb);
					return -ENOMEM;
				}
A
Alan Cox 已提交
2942 2943
				edge_serial->bulk_in_endpoint =
						endpoint->bEndpointAddress;
2944 2945 2946

				/* set up our bulk in urb */
				usb_fill_bulk_urb(edge_serial->read_urb, dev,
A
Alan Cox 已提交
2947 2948 2949
					usb_rcvbulkpipe(dev,
						endpoint->bEndpointAddress),
					edge_serial->bulk_in_buffer,
2950
					usb_endpoint_maxp(endpoint),
A
Alan Cox 已提交
2951 2952
					edge_bulk_in_callback,
					edge_serial);
2953
				bulk_in_found = true;
2954 2955
			}

2956
			if (!bulk_out_found &&
2957 2958
			    (usb_endpoint_is_bulk_out(endpoint))) {
				/* we found a bulk out endpoint */
2959
				dev_dbg(ddev, "found bulk out\n");
A
Alan Cox 已提交
2960 2961
				edge_serial->bulk_out_endpoint =
						endpoint->bEndpointAddress;
2962
				bulk_out_found = true;
2963 2964 2965
			}
		}

2966
		if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
2967
			dev_err(ddev, "Error - the proper endpoints were not found!\n");
2968 2969 2970 2971 2972
			return -ENODEV;
		}

		/* start interrupt read for this edgeport this interrupt will
		 * continue as long as the edgeport is connected */
A
Alan Cox 已提交
2973 2974
		response = usb_submit_urb(edge_serial->interrupt_read_urb,
								GFP_KERNEL);
2975
		if (response)
2976
			dev_err(ddev, "%s - Error %d submitting control urb\n",
2977
				__func__, response);
2978 2979
	}
	return response;
L
Linus Torvalds 已提交
2980 2981 2982 2983
}


/****************************************************************************
2984
 * edge_disconnect
L
Linus Torvalds 已提交
2985 2986
 *	This function is called whenever the device is removed from the usb bus.
 ****************************************************************************/
2987
static void edge_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
2988
{
2989
	struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
L
Linus Torvalds 已提交
2990 2991

	/* stop reads and writes on all ports */
2992 2993
	/* free up our endpoint stuff */
	if (edge_serial->is_epic) {
2994
		usb_kill_urb(edge_serial->interrupt_read_urb);
2995 2996 2997
		usb_free_urb(edge_serial->interrupt_read_urb);
		kfree(edge_serial->interrupt_in_buffer);

2998
		usb_kill_urb(edge_serial->read_urb);
2999 3000 3001
		usb_free_urb(edge_serial->read_urb);
		kfree(edge_serial->bulk_in_buffer);
	}
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
}


/****************************************************************************
 * 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);
3012 3013

	kfree(edge_serial);
L
Linus Torvalds 已提交
3014 3015
}

3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041
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;
}

3042
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
3043

A
Alan Cox 已提交
3044 3045
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
3046
MODULE_LICENSE("GPL");
3047 3048 3049 3050
MODULE_FIRMWARE("edgeport/boot.fw");
MODULE_FIRMWARE("edgeport/boot2.fw");
MODULE_FIRMWARE("edgeport/down.fw");
MODULE_FIRMWARE("edgeport/down2.fw");