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