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