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