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