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