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