xref: /linux/drivers/usb/serial/keyspan.c (revision a5766f11cfd3a0c03450d99c8fe548c2940be884)
1 /*
2   Keyspan USB to Serial Converter driver
3 
4   (C) Copyright (C) 2000-2001	Hugh Blemings <hugh@blemings.org>
5   (C) Copyright (C) 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   See http://misc.nu/hugh/keyspan.html for more information.
13 
14   Code in this driver inspired by and in a number of places taken
15   from Brian Warner's original Keyspan-PDA driver.
16 
17   This driver has been put together with the support of Innosys, Inc.
18   and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
19   Thanks Guys :)
20 
21   Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22   of much nicer and/or completely new code and (perhaps most uniquely)
23   having the patience to sit down and explain why and where he'd changed
24   stuff.
25 
26   Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27   staff in their work on open source projects.
28 
29   Change History
30 
31     2003sep04	LPM (Keyspan) add support for new single port product USA19HS.
32 				Improve setup message handling for all devices.
33 
34     Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35       Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36       Linux source tree.  The Linux tree lacked support for the 49WLC and
37       others.  The Keyspan patches didn't work with the current kernel.
38 
39     2003jan30	LPM	add support for the 49WLC and MPR
40 
41     Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42       Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
43       now supported (including QI and QW).  Modified port open, port
44       close, and send setup() logic to fix various data and endpoint
45       synchronization bugs and device LED status bugs.  Changed keyspan_
46       write_room() to accurately return transmit buffer availability.
47       Changed forwardingLength from 1 to 16 for all adapters.
48 
49     Fri Oct 12 16:45:00 EST 2001
50       Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
51 
52     Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53       Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
54       now supported (including QI and QW).  Modified port open, port
55       close, and send setup() logic to fix various data and endpoint
56       synchronization bugs and device LED status bugs.  Changed keyspan_
57       write_room() to accurately return transmit buffer availability.
58       Changed forwardingLength from 1 to 16 for all adapters.
59 
60     Fri Oct 12 16:45:00 EST 2001
61       Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
62 
63     Mon Oct  8 14:29:00 EST 2001 hugh
64       Fixed bug that prevented mulitport devices operating correctly
65       if they weren't the first unit attached.
66 
67     Sat Oct  6 12:31:21 EST 2001 hugh
68       Added support for USA-28XA and -28XB, misc cleanups, break support
69       for usa26 based models thanks to David Gibson.
70 
71     Thu May 31 11:56:42 PDT 2001 gkh
72       switched from using spinlock to a semaphore
73 
74     (04/08/2001) gb
75 	Identify version on module load.
76 
77     (11/01/2000) Adam J. Richter
78 	usb_device_id table support.
79 
80     Tue Oct 10 23:15:33 EST 2000 Hugh
81       Merged Paul's changes with my USA-49W mods.  Work in progress
82       still...
83 
84     Wed Jul 19 14:00:42 EST 2000 gkh
85       Added module_init and module_exit functions to handle the fact that
86       this driver is a loadable module now.
87 
88     Tue Jul 18 16:14:52 EST 2000 Hugh
89       Basic character input/output for USA-19 now mostly works,
90       fixed at 9600 baud for the moment.
91 
92     Sat Jul  8 11:11:48 EST 2000 Hugh
93       First public release - nothing works except the firmware upload.
94       Tested on PPC and x86 architectures, seems to behave...
95 */
96 
97 
98 #include <linux/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <linux/firmware.h>
109 #include <linux/ihex.h>
110 #include <linux/uaccess.h>
111 #include <linux/usb.h>
112 #include <linux/usb/serial.h>
113 #include "keyspan.h"
114 
115 static int debug;
116 
117 /*
118  * Version Information
119  */
120 #define DRIVER_VERSION "v1.1.5"
121 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
122 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
123 
124 #define INSTAT_BUFLEN	32
125 #define GLOCONT_BUFLEN	64
126 #define INDAT49W_BUFLEN	512
127 
128 	/* Per device and per port private data */
129 struct keyspan_serial_private {
130 	const struct keyspan_device_details	*device_details;
131 
132 	struct urb	*instat_urb;
133 	char		instat_buf[INSTAT_BUFLEN];
134 
135 	/* added to support 49wg, where data from all 4 ports comes in
136 	   on 1 EP and high-speed supported */
137 	struct urb	*indat_urb;
138 	char		indat_buf[INDAT49W_BUFLEN];
139 
140 	/* XXX this one probably will need a lock */
141 	struct urb	*glocont_urb;
142 	char		glocont_buf[GLOCONT_BUFLEN];
143 	char		ctrl_buf[8];	/* for EP0 control message */
144 };
145 
146 struct keyspan_port_private {
147 	/* Keep track of which input & output endpoints to use */
148 	int		in_flip;
149 	int		out_flip;
150 
151 	/* Keep duplicate of device details in each port
152 	   structure as well - simplifies some of the
153 	   callback functions etc. */
154 	const struct keyspan_device_details	*device_details;
155 
156 	/* Input endpoints and buffer for this port */
157 	struct urb	*in_urbs[2];
158 	char		in_buffer[2][64];
159 	/* Output endpoints and buffer for this port */
160 	struct urb	*out_urbs[2];
161 	char		out_buffer[2][64];
162 
163 	/* Input ack endpoint */
164 	struct urb	*inack_urb;
165 	char		inack_buffer[1];
166 
167 	/* Output control endpoint */
168 	struct urb	*outcont_urb;
169 	char		outcont_buffer[64];
170 
171 	/* Settings for the port */
172 	int		baud;
173 	int		old_baud;
174 	unsigned int	cflag;
175 	unsigned int	old_cflag;
176 	enum		{flow_none, flow_cts, flow_xon} flow_control;
177 	int		rts_state;	/* Handshaking pins (outputs) */
178 	int		dtr_state;
179 	int		cts_state;	/* Handshaking pins (inputs) */
180 	int		dsr_state;
181 	int		dcd_state;
182 	int		ri_state;
183 	int		break_on;
184 
185 	unsigned long	tx_start_time[2];
186 	int		resend_cont;	/* need to resend control packet */
187 };
188 
189 /* Include Keyspan message headers.  All current Keyspan Adapters
190    make use of one of five message formats which are referred
191    to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
192    within this driver. */
193 #include "keyspan_usa26msg.h"
194 #include "keyspan_usa28msg.h"
195 #include "keyspan_usa49msg.h"
196 #include "keyspan_usa90msg.h"
197 #include "keyspan_usa67msg.h"
198 
199 
200 /* Functions used by new usb-serial code. */
201 static int __init keyspan_init(void)
202 {
203 	int retval;
204 	retval = usb_serial_register(&keyspan_pre_device);
205 	if (retval)
206 		goto failed_pre_device_register;
207 	retval = usb_serial_register(&keyspan_1port_device);
208 	if (retval)
209 		goto failed_1port_device_register;
210 	retval = usb_serial_register(&keyspan_2port_device);
211 	if (retval)
212 		goto failed_2port_device_register;
213 	retval = usb_serial_register(&keyspan_4port_device);
214 	if (retval)
215 		goto failed_4port_device_register;
216 	retval = usb_register(&keyspan_driver);
217 	if (retval)
218 		goto failed_usb_register;
219 
220 	info(DRIVER_VERSION ":" DRIVER_DESC);
221 
222 	return 0;
223 failed_usb_register:
224 	usb_serial_deregister(&keyspan_4port_device);
225 failed_4port_device_register:
226 	usb_serial_deregister(&keyspan_2port_device);
227 failed_2port_device_register:
228 	usb_serial_deregister(&keyspan_1port_device);
229 failed_1port_device_register:
230 	usb_serial_deregister(&keyspan_pre_device);
231 failed_pre_device_register:
232 	return retval;
233 }
234 
235 static void __exit keyspan_exit(void)
236 {
237 	usb_deregister(&keyspan_driver);
238 	usb_serial_deregister(&keyspan_pre_device);
239 	usb_serial_deregister(&keyspan_1port_device);
240 	usb_serial_deregister(&keyspan_2port_device);
241 	usb_serial_deregister(&keyspan_4port_device);
242 }
243 
244 module_init(keyspan_init);
245 module_exit(keyspan_exit);
246 
247 static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
248 {
249 	struct usb_serial_port *port = tty->driver_data;
250 	struct keyspan_port_private 	*p_priv;
251 
252 	dbg("%s", __func__);
253 
254 	p_priv = usb_get_serial_port_data(port);
255 
256 	if (break_state == -1)
257 		p_priv->break_on = 1;
258 	else
259 		p_priv->break_on = 0;
260 
261 	keyspan_send_setup(port, 0);
262 }
263 
264 
265 static void keyspan_set_termios(struct tty_struct *tty,
266 		struct usb_serial_port *port, struct ktermios *old_termios)
267 {
268 	int				baud_rate, device_port;
269 	struct keyspan_port_private 	*p_priv;
270 	const struct keyspan_device_details	*d_details;
271 	unsigned int 			cflag;
272 
273 	dbg("%s", __func__);
274 
275 	p_priv = usb_get_serial_port_data(port);
276 	d_details = p_priv->device_details;
277 	cflag = tty->termios->c_cflag;
278 	device_port = port->number - port->serial->minor;
279 
280 	/* Baud rate calculation takes baud rate as an integer
281 	   so other rates can be generated if desired. */
282 	baud_rate = tty_get_baud_rate(tty);
283 	/* If no match or invalid, don't change */
284 	if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
285 				NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
286 		/* FIXME - more to do here to ensure rate changes cleanly */
287 		/* FIXME - calcuate exact rate from divisor ? */
288 		p_priv->baud = baud_rate;
289 	} else
290 		baud_rate = tty_termios_baud_rate(old_termios);
291 
292 	tty_encode_baud_rate(tty, baud_rate, baud_rate);
293 	/* set CTS/RTS handshake etc. */
294 	p_priv->cflag = cflag;
295 	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
296 
297 	/* Mark/Space not supported */
298 	tty->termios->c_cflag &= ~CMSPAR;
299 
300 	keyspan_send_setup(port, 0);
301 }
302 
303 static int keyspan_tiocmget(struct tty_struct *tty, struct file *file)
304 {
305 	struct usb_serial_port *port = tty->driver_data;
306 	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
307 	unsigned int			value;
308 
309 	value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
310 		((p_priv->dtr_state) ? TIOCM_DTR : 0) |
311 		((p_priv->cts_state) ? TIOCM_CTS : 0) |
312 		((p_priv->dsr_state) ? TIOCM_DSR : 0) |
313 		((p_priv->dcd_state) ? TIOCM_CAR : 0) |
314 		((p_priv->ri_state) ? TIOCM_RNG : 0);
315 
316 	return value;
317 }
318 
319 static int keyspan_tiocmset(struct tty_struct *tty, struct file *file,
320 			    unsigned int set, unsigned int clear)
321 {
322 	struct usb_serial_port *port = tty->driver_data;
323 	struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
324 
325 	if (set & TIOCM_RTS)
326 		p_priv->rts_state = 1;
327 	if (set & TIOCM_DTR)
328 		p_priv->dtr_state = 1;
329 	if (clear & TIOCM_RTS)
330 		p_priv->rts_state = 0;
331 	if (clear & TIOCM_DTR)
332 		p_priv->dtr_state = 0;
333 	keyspan_send_setup(port, 0);
334 	return 0;
335 }
336 
337 /* Write function is similar for the four protocols used
338    with only a minor change for usa90 (usa19hs) required */
339 static int keyspan_write(struct tty_struct *tty,
340 	struct usb_serial_port *port, const unsigned char *buf, int count)
341 {
342 	struct keyspan_port_private 	*p_priv;
343 	const struct keyspan_device_details	*d_details;
344 	int				flip;
345 	int 				left, todo;
346 	struct urb			*this_urb;
347 	int 				err, maxDataLen, dataOffset;
348 
349 	p_priv = usb_get_serial_port_data(port);
350 	d_details = p_priv->device_details;
351 
352 	if (d_details->msg_format == msg_usa90) {
353 		maxDataLen = 64;
354 		dataOffset = 0;
355 	} else {
356 		maxDataLen = 63;
357 		dataOffset = 1;
358 	}
359 
360 	dbg("%s - for port %d (%d chars), flip=%d",
361 	    __func__, port->number, count, p_priv->out_flip);
362 
363 	for (left = count; left > 0; left -= todo) {
364 		todo = left;
365 		if (todo > maxDataLen)
366 			todo = maxDataLen;
367 
368 		flip = p_priv->out_flip;
369 
370 		/* Check we have a valid urb/endpoint before we use it... */
371 		this_urb = p_priv->out_urbs[flip];
372 		if (this_urb == NULL) {
373 			/* no bulk out, so return 0 bytes written */
374 			dbg("%s - no output urb :(", __func__);
375 			return count;
376 		}
377 
378 		dbg("%s - endpoint %d flip %d",
379 			__func__, usb_pipeendpoint(this_urb->pipe), flip);
380 
381 		if (this_urb->status == -EINPROGRESS) {
382 			if (time_before(jiffies,
383 					p_priv->tx_start_time[flip] + 10 * HZ))
384 				break;
385 			usb_unlink_urb(this_urb);
386 			break;
387 		}
388 
389 		/* First byte in buffer is "last flag" (except for usa19hx)
390 		   - unused so for now so set to zero */
391 		((char *)this_urb->transfer_buffer)[0] = 0;
392 
393 		memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
394 		buf += todo;
395 
396 		/* send the data out the bulk port */
397 		this_urb->transfer_buffer_length = todo + dataOffset;
398 
399 		this_urb->dev = port->serial->dev;
400 		err = usb_submit_urb(this_urb, GFP_ATOMIC);
401 		if (err != 0)
402 			dbg("usb_submit_urb(write bulk) failed (%d)", err);
403 		p_priv->tx_start_time[flip] = jiffies;
404 
405 		/* Flip for next time if usa26 or usa28 interface
406 		   (not used on usa49) */
407 		p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
408 	}
409 
410 	return count - left;
411 }
412 
413 static void	usa26_indat_callback(struct urb *urb)
414 {
415 	int			i, err;
416 	int			endpoint;
417 	struct usb_serial_port	*port;
418 	struct tty_struct	*tty;
419 	unsigned char 		*data = urb->transfer_buffer;
420 	int status = urb->status;
421 
422 	dbg("%s", __func__);
423 
424 	endpoint = usb_pipeendpoint(urb->pipe);
425 
426 	if (status) {
427 		dbg("%s - nonzero status: %x on endpoint %d.",
428 		    __func__, status, endpoint);
429 		return;
430 	}
431 
432 	port =  urb->context;
433 	tty = tty_port_tty_get(&port->port);
434 	if (tty && urb->actual_length) {
435 		/* 0x80 bit is error flag */
436 		if ((data[0] & 0x80) == 0) {
437 			/* no errors on individual bytes, only
438 			   possible overrun err */
439 			if (data[0] & RXERROR_OVERRUN)
440 				err = TTY_OVERRUN;
441 			else
442 				err = 0;
443 			for (i = 1; i < urb->actual_length ; ++i)
444 				tty_insert_flip_char(tty, data[i], err);
445 		} else {
446 			/* some bytes had errors, every byte has status */
447 			dbg("%s - RX error!!!!", __func__);
448 			for (i = 0; i + 1 < urb->actual_length; i += 2) {
449 				int stat = data[i], flag = 0;
450 				if (stat & RXERROR_OVERRUN)
451 					flag |= TTY_OVERRUN;
452 				if (stat & RXERROR_FRAMING)
453 					flag |= TTY_FRAME;
454 				if (stat & RXERROR_PARITY)
455 					flag |= TTY_PARITY;
456 				/* XXX should handle break (0x10) */
457 				tty_insert_flip_char(tty, data[i+1], flag);
458 			}
459 		}
460 		tty_flip_buffer_push(tty);
461 	}
462 	tty_kref_put(tty);
463 
464 	/* Resubmit urb so we continue receiving */
465 	urb->dev = port->serial->dev;
466 	if (port->port.count) {
467 		err = usb_submit_urb(urb, GFP_ATOMIC);
468 		if (err != 0)
469 			dbg("%s - resubmit read urb failed. (%d)",
470 					__func__, err);
471 	}
472 	return;
473 }
474 
475 /* Outdat handling is common for all devices */
476 static void	usa2x_outdat_callback(struct urb *urb)
477 {
478 	struct usb_serial_port *port;
479 	struct keyspan_port_private *p_priv;
480 
481 	port =  urb->context;
482 	p_priv = usb_get_serial_port_data(port);
483 	dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
484 
485 	if (port->port.count)
486 		usb_serial_port_softint(port);
487 }
488 
489 static void	usa26_inack_callback(struct urb *urb)
490 {
491 	dbg("%s", __func__);
492 
493 }
494 
495 static void	usa26_outcont_callback(struct urb *urb)
496 {
497 	struct usb_serial_port *port;
498 	struct keyspan_port_private *p_priv;
499 
500 	port =  urb->context;
501 	p_priv = usb_get_serial_port_data(port);
502 
503 	if (p_priv->resend_cont) {
504 		dbg("%s - sending setup", __func__);
505 		keyspan_usa26_send_setup(port->serial, port,
506 						p_priv->resend_cont - 1);
507 	}
508 }
509 
510 static void	usa26_instat_callback(struct urb *urb)
511 {
512 	unsigned char 				*data = urb->transfer_buffer;
513 	struct keyspan_usa26_portStatusMessage	*msg;
514 	struct usb_serial			*serial;
515 	struct usb_serial_port			*port;
516 	struct keyspan_port_private	 	*p_priv;
517 	struct tty_struct			*tty;
518 	int old_dcd_state, err;
519 	int status = urb->status;
520 
521 	serial =  urb->context;
522 
523 	if (status) {
524 		dbg("%s - nonzero status: %x", __func__, status);
525 		return;
526 	}
527 	if (urb->actual_length != 9) {
528 		dbg("%s - %d byte report??", __func__, urb->actual_length);
529 		goto exit;
530 	}
531 
532 	msg = (struct keyspan_usa26_portStatusMessage *)data;
533 
534 #if 0
535 	dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
536 	    __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
537 	    msg->_txXoff, msg->rxEnabled, msg->controlResponse);
538 #endif
539 
540 	/* Now do something useful with the data */
541 
542 
543 	/* Check port number from message and retrieve private data */
544 	if (msg->port >= serial->num_ports) {
545 		dbg("%s - Unexpected port number %d", __func__, msg->port);
546 		goto exit;
547 	}
548 	port = serial->port[msg->port];
549 	p_priv = usb_get_serial_port_data(port);
550 
551 	/* Update handshaking pin state information */
552 	old_dcd_state = p_priv->dcd_state;
553 	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
554 	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
555 	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
556 	p_priv->ri_state = ((msg->ri) ? 1 : 0);
557 
558 	if (old_dcd_state != p_priv->dcd_state) {
559 		tty = tty_port_tty_get(&port->port);
560 		if (tty && !C_CLOCAL(tty))
561 			tty_hangup(tty);
562 		tty_kref_put(tty);
563 	}
564 
565 	/* Resubmit urb so we continue receiving */
566 	urb->dev = serial->dev;
567 	err = usb_submit_urb(urb, GFP_ATOMIC);
568 	if (err != 0)
569 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
570 exit: ;
571 }
572 
573 static void	usa26_glocont_callback(struct urb *urb)
574 {
575 	dbg("%s", __func__);
576 }
577 
578 
579 static void usa28_indat_callback(struct urb *urb)
580 {
581 	int                     err;
582 	struct usb_serial_port  *port;
583 	struct tty_struct       *tty;
584 	unsigned char           *data;
585 	struct keyspan_port_private             *p_priv;
586 	int status = urb->status;
587 
588 	dbg("%s", __func__);
589 
590 	port =  urb->context;
591 	p_priv = usb_get_serial_port_data(port);
592 	data = urb->transfer_buffer;
593 
594 	if (urb != p_priv->in_urbs[p_priv->in_flip])
595 		return;
596 
597 	do {
598 		if (status) {
599 			dbg("%s - nonzero status: %x on endpoint %d.",
600 			    __func__, status, usb_pipeendpoint(urb->pipe));
601 			return;
602 		}
603 
604 		port =  urb->context;
605 		p_priv = usb_get_serial_port_data(port);
606 		data = urb->transfer_buffer;
607 
608 		tty =tty_port_tty_get(&port->port);
609 		if (tty && urb->actual_length) {
610 			tty_insert_flip_string(tty, data, urb->actual_length);
611 			tty_flip_buffer_push(tty);
612 		}
613 		tty_kref_put(tty);
614 
615 		/* Resubmit urb so we continue receiving */
616 		urb->dev = port->serial->dev;
617 		if (port->port.count) {
618 			err = usb_submit_urb(urb, GFP_ATOMIC);
619 			if (err != 0)
620 				dbg("%s - resubmit read urb failed. (%d)",
621 								__func__, err);
622 		}
623 		p_priv->in_flip ^= 1;
624 
625 		urb = p_priv->in_urbs[p_priv->in_flip];
626 	} while (urb->status != -EINPROGRESS);
627 }
628 
629 static void	usa28_inack_callback(struct urb *urb)
630 {
631 	dbg("%s", __func__);
632 }
633 
634 static void	usa28_outcont_callback(struct urb *urb)
635 {
636 	struct usb_serial_port *port;
637 	struct keyspan_port_private *p_priv;
638 
639 	port =  urb->context;
640 	p_priv = usb_get_serial_port_data(port);
641 
642 	if (p_priv->resend_cont) {
643 		dbg("%s - sending setup", __func__);
644 		keyspan_usa28_send_setup(port->serial, port,
645 						p_priv->resend_cont - 1);
646 	}
647 }
648 
649 static void	usa28_instat_callback(struct urb *urb)
650 {
651 	int					err;
652 	unsigned char 				*data = urb->transfer_buffer;
653 	struct keyspan_usa28_portStatusMessage	*msg;
654 	struct usb_serial			*serial;
655 	struct usb_serial_port			*port;
656 	struct keyspan_port_private	 	*p_priv;
657 	struct tty_struct			*tty;
658 	int old_dcd_state;
659 	int status = urb->status;
660 
661 	serial =  urb->context;
662 
663 	if (status) {
664 		dbg("%s - nonzero status: %x", __func__, status);
665 		return;
666 	}
667 
668 	if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
669 		dbg("%s - bad length %d", __func__, urb->actual_length);
670 		goto exit;
671 	}
672 
673 	/*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
674 	    data[0], data[1], data[2], data[3], data[4], data[5],
675 	    data[6], data[7], data[8], data[9], data[10], data[11]);*/
676 
677 	/* Now do something useful with the data */
678 	msg = (struct keyspan_usa28_portStatusMessage *)data;
679 
680 	/* Check port number from message and retrieve private data */
681 	if (msg->port >= serial->num_ports) {
682 		dbg("%s - Unexpected port number %d", __func__, msg->port);
683 		goto exit;
684 	}
685 	port = serial->port[msg->port];
686 	p_priv = usb_get_serial_port_data(port);
687 
688 	/* Update handshaking pin state information */
689 	old_dcd_state = p_priv->dcd_state;
690 	p_priv->cts_state = ((msg->cts) ? 1 : 0);
691 	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
692 	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
693 	p_priv->ri_state = ((msg->ri) ? 1 : 0);
694 
695 	if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
696 		tty = tty_port_tty_get(&port->port);
697 		if (tty && !C_CLOCAL(tty))
698 			tty_hangup(tty);
699 		tty_kref_put(tty);
700 	}
701 
702 		/* Resubmit urb so we continue receiving */
703 	urb->dev = serial->dev;
704 	err = usb_submit_urb(urb, GFP_ATOMIC);
705 	if (err != 0)
706 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
707 exit: ;
708 }
709 
710 static void	usa28_glocont_callback(struct urb *urb)
711 {
712 	dbg("%s", __func__);
713 }
714 
715 
716 static void	usa49_glocont_callback(struct urb *urb)
717 {
718 	struct usb_serial *serial;
719 	struct usb_serial_port *port;
720 	struct keyspan_port_private *p_priv;
721 	int i;
722 
723 	dbg("%s", __func__);
724 
725 	serial =  urb->context;
726 	for (i = 0; i < serial->num_ports; ++i) {
727 		port = serial->port[i];
728 		p_priv = usb_get_serial_port_data(port);
729 
730 		if (p_priv->resend_cont) {
731 			dbg("%s - sending setup", __func__);
732 			keyspan_usa49_send_setup(serial, port,
733 						p_priv->resend_cont - 1);
734 			break;
735 		}
736 	}
737 }
738 
739 	/* This is actually called glostat in the Keyspan
740 	   doco */
741 static void	usa49_instat_callback(struct urb *urb)
742 {
743 	int					err;
744 	unsigned char 				*data = urb->transfer_buffer;
745 	struct keyspan_usa49_portStatusMessage	*msg;
746 	struct usb_serial			*serial;
747 	struct usb_serial_port			*port;
748 	struct keyspan_port_private	 	*p_priv;
749 	int old_dcd_state;
750 	int status = urb->status;
751 
752 	dbg("%s", __func__);
753 
754 	serial =  urb->context;
755 
756 	if (status) {
757 		dbg("%s - nonzero status: %x", __func__, status);
758 		return;
759 	}
760 
761 	if (urb->actual_length !=
762 			sizeof(struct keyspan_usa49_portStatusMessage)) {
763 		dbg("%s - bad length %d", __func__, urb->actual_length);
764 		goto exit;
765 	}
766 
767 	/*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
768 	    data[0], data[1], data[2], data[3], data[4], data[5],
769 	    data[6], data[7], data[8], data[9], data[10]);*/
770 
771 	/* Now do something useful with the data */
772 	msg = (struct keyspan_usa49_portStatusMessage *)data;
773 
774 	/* Check port number from message and retrieve private data */
775 	if (msg->portNumber >= serial->num_ports) {
776 		dbg("%s - Unexpected port number %d",
777 					__func__, msg->portNumber);
778 		goto exit;
779 	}
780 	port = serial->port[msg->portNumber];
781 	p_priv = usb_get_serial_port_data(port);
782 
783 	/* Update handshaking pin state information */
784 	old_dcd_state = p_priv->dcd_state;
785 	p_priv->cts_state = ((msg->cts) ? 1 : 0);
786 	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
787 	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
788 	p_priv->ri_state = ((msg->ri) ? 1 : 0);
789 
790 	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
791 		struct tty_struct *tty = tty_port_tty_get(&port->port);
792 		if (tty && !C_CLOCAL(tty))
793 			tty_hangup(tty);
794 		tty_kref_put(tty);
795 	}
796 
797 	/* Resubmit urb so we continue receiving */
798 	urb->dev = serial->dev;
799 
800 	err = usb_submit_urb(urb, GFP_ATOMIC);
801 	if (err != 0)
802 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
803 exit:	;
804 }
805 
806 static void	usa49_inack_callback(struct urb *urb)
807 {
808 	dbg("%s", __func__);
809 }
810 
811 static void	usa49_indat_callback(struct urb *urb)
812 {
813 	int			i, err;
814 	int			endpoint;
815 	struct usb_serial_port	*port;
816 	struct tty_struct	*tty;
817 	unsigned char 		*data = urb->transfer_buffer;
818 	int status = urb->status;
819 
820 	dbg("%s", __func__);
821 
822 	endpoint = usb_pipeendpoint(urb->pipe);
823 
824 	if (status) {
825 		dbg("%s - nonzero status: %x on endpoint %d.", __func__,
826 		    status, endpoint);
827 		return;
828 	}
829 
830 	port =  urb->context;
831 	tty = tty_port_tty_get(&port->port);
832 	if (tty && urb->actual_length) {
833 		/* 0x80 bit is error flag */
834 		if ((data[0] & 0x80) == 0) {
835 			/* no error on any byte */
836 			tty_insert_flip_string(tty, data + 1,
837 						urb->actual_length - 1);
838 		} else {
839 			/* some bytes had errors, every byte has status */
840 			for (i = 0; i + 1 < urb->actual_length; i += 2) {
841 				int stat = data[i], flag = 0;
842 				if (stat & RXERROR_OVERRUN)
843 					flag |= TTY_OVERRUN;
844 				if (stat & RXERROR_FRAMING)
845 					flag |= TTY_FRAME;
846 				if (stat & RXERROR_PARITY)
847 					flag |= TTY_PARITY;
848 				/* XXX should handle break (0x10) */
849 				tty_insert_flip_char(tty, data[i+1], flag);
850 			}
851 		}
852 		tty_flip_buffer_push(tty);
853 	}
854 	tty_kref_put(tty);
855 
856 	/* Resubmit urb so we continue receiving */
857 	urb->dev = port->serial->dev;
858 	if (port->port.count) {
859 		err = usb_submit_urb(urb, GFP_ATOMIC);
860 		if (err != 0)
861 			dbg("%s - resubmit read urb failed. (%d)",
862 							__func__, err);
863 	}
864 }
865 
866 static void usa49wg_indat_callback(struct urb *urb)
867 {
868 	int			i, len, x, err;
869 	struct usb_serial	*serial;
870 	struct usb_serial_port	*port;
871 	struct tty_struct	*tty;
872 	unsigned char 		*data = urb->transfer_buffer;
873 	int status = urb->status;
874 
875 	dbg("%s", __func__);
876 
877 	serial = urb->context;
878 
879 	if (status) {
880 		dbg("%s - nonzero status: %x", __func__, status);
881 		return;
882 	}
883 
884 	/* inbound data is in the form P#, len, status, data */
885 	i = 0;
886 	len = 0;
887 
888 	if (urb->actual_length) {
889 		while (i < urb->actual_length) {
890 
891 			/* Check port number from message*/
892 			if (data[i] >= serial->num_ports) {
893 				dbg("%s - Unexpected port number %d",
894 					__func__, data[i]);
895 				return;
896 			}
897 			port = serial->port[data[i++]];
898 			tty = tty_port_tty_get(&port->port);
899 			len = data[i++];
900 
901 			/* 0x80 bit is error flag */
902 			if ((data[i] & 0x80) == 0) {
903 				/* no error on any byte */
904 				i++;
905 				for (x = 1; x < len ; ++x)
906 					if (port->port.count)
907 						tty_insert_flip_char(tty,
908 								data[i++], 0);
909 					else
910 						i++;
911 			} else {
912 				/*
913 				 * some bytes had errors, every byte has status
914 				 */
915 				for (x = 0; x + 1 < len; x += 2) {
916 					int stat = data[i], flag = 0;
917 					if (stat & RXERROR_OVERRUN)
918 						flag |= TTY_OVERRUN;
919 					if (stat & RXERROR_FRAMING)
920 						flag |= TTY_FRAME;
921 					if (stat & RXERROR_PARITY)
922 						flag |= TTY_PARITY;
923 					/* XXX should handle break (0x10) */
924 					if (port->port.count)
925 						tty_insert_flip_char(tty,
926 							data[i+1], flag);
927 					i += 2;
928 				}
929 			}
930 			if (port->port.count)
931 				tty_flip_buffer_push(tty);
932 			tty_kref_put(tty);
933 		}
934 	}
935 
936 	/* Resubmit urb so we continue receiving */
937 	urb->dev = serial->dev;
938 
939 	err = usb_submit_urb(urb, GFP_ATOMIC);
940 	if (err != 0)
941 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
942 }
943 
944 /* not used, usa-49 doesn't have per-port control endpoints */
945 static void usa49_outcont_callback(struct urb *urb)
946 {
947 	dbg("%s", __func__);
948 }
949 
950 static void usa90_indat_callback(struct urb *urb)
951 {
952 	int			i, err;
953 	int			endpoint;
954 	struct usb_serial_port	*port;
955 	struct keyspan_port_private	 	*p_priv;
956 	struct tty_struct	*tty;
957 	unsigned char 		*data = urb->transfer_buffer;
958 	int status = urb->status;
959 
960 	dbg("%s", __func__);
961 
962 	endpoint = usb_pipeendpoint(urb->pipe);
963 
964 	if (status) {
965 		dbg("%s - nonzero status: %x on endpoint %d.",
966 		    __func__, status, endpoint);
967 		return;
968 	}
969 
970 	port =  urb->context;
971 	p_priv = usb_get_serial_port_data(port);
972 
973 	if (urb->actual_length) {
974 		tty = tty_port_tty_get(&port->port);
975 		/* if current mode is DMA, looks like usa28 format
976 		   otherwise looks like usa26 data format */
977 
978 		if (p_priv->baud > 57600)
979 			tty_insert_flip_string(tty, data, urb->actual_length);
980 		else {
981 			/* 0x80 bit is error flag */
982 			if ((data[0] & 0x80) == 0) {
983 				/* no errors on individual bytes, only
984 				   possible overrun err*/
985 				if (data[0] & RXERROR_OVERRUN)
986 					err = TTY_OVERRUN;
987 				else
988 					err = 0;
989 				for (i = 1; i < urb->actual_length ; ++i)
990 					tty_insert_flip_char(tty, data[i],
991 									err);
992 			}  else {
993 			/* some bytes had errors, every byte has status */
994 				dbg("%s - RX error!!!!", __func__);
995 				for (i = 0; i + 1 < urb->actual_length; i += 2) {
996 					int stat = data[i], flag = 0;
997 					if (stat & RXERROR_OVERRUN)
998 						flag |= TTY_OVERRUN;
999 					if (stat & RXERROR_FRAMING)
1000 						flag |= TTY_FRAME;
1001 					if (stat & RXERROR_PARITY)
1002 						flag |= TTY_PARITY;
1003 					/* XXX should handle break (0x10) */
1004 					tty_insert_flip_char(tty, data[i+1],
1005 									flag);
1006 				}
1007 			}
1008 		}
1009 		tty_flip_buffer_push(tty);
1010 		tty_kref_put(tty);
1011 	}
1012 
1013 	/* Resubmit urb so we continue receiving */
1014 	urb->dev = port->serial->dev;
1015 	if (port->port.count) {
1016 		err = usb_submit_urb(urb, GFP_ATOMIC);
1017 		if (err != 0)
1018 			dbg("%s - resubmit read urb failed. (%d)",
1019 							__func__, err);
1020 	}
1021 	return;
1022 }
1023 
1024 
1025 static void	usa90_instat_callback(struct urb *urb)
1026 {
1027 	unsigned char 				*data = urb->transfer_buffer;
1028 	struct keyspan_usa90_portStatusMessage	*msg;
1029 	struct usb_serial			*serial;
1030 	struct usb_serial_port			*port;
1031 	struct keyspan_port_private	 	*p_priv;
1032 	struct tty_struct			*tty;
1033 	int old_dcd_state, err;
1034 	int status = urb->status;
1035 
1036 	serial =  urb->context;
1037 
1038 	if (status) {
1039 		dbg("%s - nonzero status: %x", __func__, status);
1040 		return;
1041 	}
1042 	if (urb->actual_length < 14) {
1043 		dbg("%s - %d byte report??", __func__, urb->actual_length);
1044 		goto exit;
1045 	}
1046 
1047 	msg = (struct keyspan_usa90_portStatusMessage *)data;
1048 
1049 	/* Now do something useful with the data */
1050 
1051 	port = serial->port[0];
1052 	p_priv = usb_get_serial_port_data(port);
1053 
1054 	/* Update handshaking pin state information */
1055 	old_dcd_state = p_priv->dcd_state;
1056 	p_priv->cts_state = ((msg->cts) ? 1 : 0);
1057 	p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1058 	p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1059 	p_priv->ri_state = ((msg->ri) ? 1 : 0);
1060 
1061 	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1062 		tty = tty_port_tty_get(&port->port);
1063 		if (tty && !C_CLOCAL(tty))
1064 			tty_hangup(tty);
1065 		tty_kref_put(tty);
1066 	}
1067 
1068 	/* Resubmit urb so we continue receiving */
1069 	urb->dev = serial->dev;
1070 	err = usb_submit_urb(urb, GFP_ATOMIC);
1071 	if (err != 0)
1072 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1073 exit:
1074 	;
1075 }
1076 
1077 static void	usa90_outcont_callback(struct urb *urb)
1078 {
1079 	struct usb_serial_port *port;
1080 	struct keyspan_port_private *p_priv;
1081 
1082 	port =  urb->context;
1083 	p_priv = usb_get_serial_port_data(port);
1084 
1085 	if (p_priv->resend_cont) {
1086 		dbg("%s - sending setup", __func__);
1087 		keyspan_usa90_send_setup(port->serial, port,
1088 						p_priv->resend_cont - 1);
1089 	}
1090 }
1091 
1092 /* Status messages from the 28xg */
1093 static void	usa67_instat_callback(struct urb *urb)
1094 {
1095 	int					err;
1096 	unsigned char 				*data = urb->transfer_buffer;
1097 	struct keyspan_usa67_portStatusMessage	*msg;
1098 	struct usb_serial			*serial;
1099 	struct usb_serial_port			*port;
1100 	struct keyspan_port_private	 	*p_priv;
1101 	int old_dcd_state;
1102 	int status = urb->status;
1103 
1104 	dbg("%s", __func__);
1105 
1106 	serial = urb->context;
1107 
1108 	if (status) {
1109 		dbg("%s - nonzero status: %x", __func__, status);
1110 		return;
1111 	}
1112 
1113 	if (urb->actual_length !=
1114 			sizeof(struct keyspan_usa67_portStatusMessage)) {
1115 		dbg("%s - bad length %d", __func__, urb->actual_length);
1116 		return;
1117 	}
1118 
1119 
1120 	/* Now do something useful with the data */
1121 	msg = (struct keyspan_usa67_portStatusMessage *)data;
1122 
1123 	/* Check port number from message and retrieve private data */
1124 	if (msg->port >= serial->num_ports) {
1125 		dbg("%s - Unexpected port number %d", __func__, msg->port);
1126 		return;
1127 	}
1128 
1129 	port = serial->port[msg->port];
1130 	p_priv = usb_get_serial_port_data(port);
1131 
1132 	/* Update handshaking pin state information */
1133 	old_dcd_state = p_priv->dcd_state;
1134 	p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1135 	p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1136 
1137 	if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1138 		struct tty_struct *tty = tty_port_tty_get(&port->port);
1139 		if (tty && !C_CLOCAL(tty))
1140 			tty_hangup(tty);
1141 		tty_kref_put(tty);
1142 	}
1143 
1144 	/* Resubmit urb so we continue receiving */
1145 	urb->dev = serial->dev;
1146 	err = usb_submit_urb(urb, GFP_ATOMIC);
1147 	if (err != 0)
1148 		dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1149 }
1150 
1151 static void usa67_glocont_callback(struct urb *urb)
1152 {
1153 	struct usb_serial *serial;
1154 	struct usb_serial_port *port;
1155 	struct keyspan_port_private *p_priv;
1156 	int i;
1157 
1158 	dbg("%s", __func__);
1159 
1160 	serial = urb->context;
1161 	for (i = 0; i < serial->num_ports; ++i) {
1162 		port = serial->port[i];
1163 		p_priv = usb_get_serial_port_data(port);
1164 
1165 		if (p_priv->resend_cont) {
1166 			dbg("%s - sending setup", __func__);
1167 			keyspan_usa67_send_setup(serial, port,
1168 						p_priv->resend_cont - 1);
1169 			break;
1170 		}
1171 	}
1172 }
1173 
1174 static int keyspan_write_room(struct tty_struct *tty)
1175 {
1176 	struct usb_serial_port *port = tty->driver_data;
1177 	struct keyspan_port_private	*p_priv;
1178 	const struct keyspan_device_details	*d_details;
1179 	int				flip;
1180 	int				data_len;
1181 	struct urb			*this_urb;
1182 
1183 	dbg("%s", __func__);
1184 	p_priv = usb_get_serial_port_data(port);
1185 	d_details = p_priv->device_details;
1186 
1187 	/* FIXME: locking */
1188 	if (d_details->msg_format == msg_usa90)
1189 		data_len = 64;
1190 	else
1191 		data_len = 63;
1192 
1193 	flip = p_priv->out_flip;
1194 
1195 	/* Check both endpoints to see if any are available. */
1196 	this_urb = p_priv->out_urbs[flip];
1197 	if (this_urb != NULL) {
1198 		if (this_urb->status != -EINPROGRESS)
1199 			return data_len;
1200 		flip = (flip + 1) & d_details->outdat_endp_flip;
1201 		this_urb = p_priv->out_urbs[flip];
1202 		if (this_urb != NULL) {
1203 			if (this_urb->status != -EINPROGRESS)
1204 				return data_len;
1205 		}
1206 	}
1207 	return 0;
1208 }
1209 
1210 
1211 static int keyspan_open(struct tty_struct *tty,
1212 			struct usb_serial_port *port, struct file *filp)
1213 {
1214 	struct keyspan_port_private 	*p_priv;
1215 	struct keyspan_serial_private 	*s_priv;
1216 	struct usb_serial 		*serial = port->serial;
1217 	const struct keyspan_device_details	*d_details;
1218 	int				i, err;
1219 	int				baud_rate, device_port;
1220 	struct urb			*urb;
1221 	unsigned int			cflag = 0;
1222 
1223 	s_priv = usb_get_serial_data(serial);
1224 	p_priv = usb_get_serial_port_data(port);
1225 	d_details = p_priv->device_details;
1226 
1227 	dbg("%s - port%d.", __func__, port->number);
1228 
1229 	/* Set some sane defaults */
1230 	p_priv->rts_state = 1;
1231 	p_priv->dtr_state = 1;
1232 	p_priv->baud = 9600;
1233 
1234 	/* force baud and lcr to be set on open */
1235 	p_priv->old_baud = 0;
1236 	p_priv->old_cflag = 0;
1237 
1238 	p_priv->out_flip = 0;
1239 	p_priv->in_flip = 0;
1240 
1241 	/* Reset low level data toggle and start reading from endpoints */
1242 	for (i = 0; i < 2; i++) {
1243 		urb = p_priv->in_urbs[i];
1244 		if (urb == NULL)
1245 			continue;
1246 		urb->dev = serial->dev;
1247 
1248 		/* make sure endpoint data toggle is synchronized
1249 		   with the device */
1250 		usb_clear_halt(urb->dev, urb->pipe);
1251 		err = usb_submit_urb(urb, GFP_KERNEL);
1252 		if (err != 0)
1253 			dbg("%s - submit urb %d failed (%d)",
1254 							__func__, i, err);
1255 	}
1256 
1257 	/* Reset low level data toggle on out endpoints */
1258 	for (i = 0; i < 2; i++) {
1259 		urb = p_priv->out_urbs[i];
1260 		if (urb == NULL)
1261 			continue;
1262 		urb->dev = serial->dev;
1263 		/* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1264 						usb_pipeout(urb->pipe), 0); */
1265 	}
1266 
1267 	/* get the terminal config for the setup message now so we don't
1268 	 * need to send 2 of them */
1269 
1270 	device_port = port->number - port->serial->minor;
1271 	if (tty) {
1272 		cflag = tty->termios->c_cflag;
1273 		/* Baud rate calculation takes baud rate as an integer
1274 		   so other rates can be generated if desired. */
1275 		baud_rate = tty_get_baud_rate(tty);
1276 		/* If no match or invalid, leave as default */
1277 		if (baud_rate >= 0
1278 		    && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1279 					NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1280 			p_priv->baud = baud_rate;
1281 		}
1282 	}
1283 	/* set CTS/RTS handshake etc. */
1284 	p_priv->cflag = cflag;
1285 	p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1286 
1287 	keyspan_send_setup(port, 1);
1288 	/* mdelay(100); */
1289 	/* keyspan_set_termios(port, NULL); */
1290 
1291 	return 0;
1292 }
1293 
1294 static inline void stop_urb(struct urb *urb)
1295 {
1296 	if (urb && urb->status == -EINPROGRESS)
1297 		usb_kill_urb(urb);
1298 }
1299 
1300 static void keyspan_close(struct tty_struct *tty,
1301 			struct usb_serial_port *port, struct file *filp)
1302 {
1303 	int			i;
1304 	struct usb_serial	*serial = port->serial;
1305 	struct keyspan_serial_private 	*s_priv;
1306 	struct keyspan_port_private 	*p_priv;
1307 
1308 	dbg("%s", __func__);
1309 	s_priv = usb_get_serial_data(serial);
1310 	p_priv = usb_get_serial_port_data(port);
1311 
1312 	p_priv->rts_state = 0;
1313 	p_priv->dtr_state = 0;
1314 
1315 	if (serial->dev) {
1316 		keyspan_send_setup(port, 2);
1317 		/* pilot-xfer seems to work best with this delay */
1318 		mdelay(100);
1319 		/* keyspan_set_termios(port, NULL); */
1320 	}
1321 
1322 	/*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1323 		dbg("%s - urb in progress", __func__);
1324 	}*/
1325 
1326 	p_priv->out_flip = 0;
1327 	p_priv->in_flip = 0;
1328 
1329 	if (serial->dev) {
1330 		/* Stop reading/writing urbs */
1331 		stop_urb(p_priv->inack_urb);
1332 		/* stop_urb(p_priv->outcont_urb); */
1333 		for (i = 0; i < 2; i++) {
1334 			stop_urb(p_priv->in_urbs[i]);
1335 			stop_urb(p_priv->out_urbs[i]);
1336 		}
1337 	}
1338 	tty_port_tty_set(&port->port, NULL);
1339 }
1340 
1341 /* download the firmware to a pre-renumeration device */
1342 static int keyspan_fake_startup(struct usb_serial *serial)
1343 {
1344 	int 				response;
1345 	const struct ihex_binrec 	*record;
1346 	char				*fw_name;
1347 	const struct firmware		*fw;
1348 
1349 	dbg("Keyspan startup version %04x product %04x",
1350 	    le16_to_cpu(serial->dev->descriptor.bcdDevice),
1351 	    le16_to_cpu(serial->dev->descriptor.idProduct));
1352 
1353 	if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1354 								!= 0x8000) {
1355 		dbg("Firmware already loaded.  Quitting.");
1356 		return 1;
1357 	}
1358 
1359 		/* Select firmware image on the basis of idProduct */
1360 	switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1361 	case keyspan_usa28_pre_product_id:
1362 		fw_name = "keyspan/usa28.fw";
1363 		break;
1364 
1365 	case keyspan_usa28x_pre_product_id:
1366 		fw_name = "keyspan/usa28x.fw";
1367 		break;
1368 
1369 	case keyspan_usa28xa_pre_product_id:
1370 		fw_name = "keyspan/usa28xa.fw";
1371 		break;
1372 
1373 	case keyspan_usa28xb_pre_product_id:
1374 		fw_name = "keyspan/usa28xb.fw";
1375 		break;
1376 
1377 	case keyspan_usa19_pre_product_id:
1378 		fw_name = "keyspan/usa19.fw";
1379 		break;
1380 
1381 	case keyspan_usa19qi_pre_product_id:
1382 		fw_name = "keyspan/usa19qi.fw";
1383 		break;
1384 
1385 	case keyspan_mpr_pre_product_id:
1386 		fw_name = "keyspan/mpr.fw";
1387 		break;
1388 
1389 	case keyspan_usa19qw_pre_product_id:
1390 		fw_name = "keyspan/usa19qw.fw";
1391 		break;
1392 
1393 	case keyspan_usa18x_pre_product_id:
1394 		fw_name = "keyspan/usa18x.fw";
1395 		break;
1396 
1397 	case keyspan_usa19w_pre_product_id:
1398 		fw_name = "keyspan/usa19w.fw";
1399 		break;
1400 
1401 	case keyspan_usa49w_pre_product_id:
1402 		fw_name = "keyspan/usa49w.fw";
1403 		break;
1404 
1405 	case keyspan_usa49wlc_pre_product_id:
1406 		fw_name = "keyspan/usa49wlc.fw";
1407 		break;
1408 
1409 	default:
1410 		dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1411 			le16_to_cpu(serial->dev->descriptor.idProduct));
1412 		return 1;
1413 	}
1414 
1415 	if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1416 		dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1417 		return(1);
1418 	}
1419 
1420 	dbg("Uploading Keyspan %s firmware.", fw_name);
1421 
1422 		/* download the firmware image */
1423 	response = ezusb_set_reset(serial, 1);
1424 
1425 	record = (const struct ihex_binrec *)fw->data;
1426 
1427 	while (record) {
1428 		response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1429 					     (unsigned char *)record->data,
1430 					     be16_to_cpu(record->len), 0xa0);
1431 		if (response < 0) {
1432 			dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1433 				response, be32_to_cpu(record->addr),
1434 				record->data, be16_to_cpu(record->len));
1435 			break;
1436 		}
1437 		record = ihex_next_binrec(record);
1438 	}
1439 	release_firmware(fw);
1440 		/* bring device out of reset. Renumeration will occur in a
1441 		   moment and the new device will bind to the real driver */
1442 	response = ezusb_set_reset(serial, 0);
1443 
1444 	/* we don't want this device to have a driver assigned to it. */
1445 	return 1;
1446 }
1447 
1448 /* Helper functions used by keyspan_setup_urbs */
1449 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1450 						     int endpoint)
1451 {
1452 	struct usb_host_interface *iface_desc;
1453 	struct usb_endpoint_descriptor *ep;
1454 	int i;
1455 
1456 	iface_desc = serial->interface->cur_altsetting;
1457 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1458 		ep = &iface_desc->endpoint[i].desc;
1459 		if (ep->bEndpointAddress == endpoint)
1460 			return ep;
1461 	}
1462 	dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1463 		 "endpoint %x\n", endpoint);
1464 	return NULL;
1465 }
1466 
1467 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1468 				      int dir, void *ctx, char *buf, int len,
1469 				      void (*callback)(struct urb *))
1470 {
1471 	struct urb *urb;
1472 	struct usb_endpoint_descriptor const *ep_desc;
1473 	char const *ep_type_name;
1474 
1475 	if (endpoint == -1)
1476 		return NULL;		/* endpoint not needed */
1477 
1478 	dbg("%s - alloc for endpoint %d.", __func__, endpoint);
1479 	urb = usb_alloc_urb(0, GFP_KERNEL);		/* No ISO */
1480 	if (urb == NULL) {
1481 		dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
1482 		return NULL;
1483 	}
1484 
1485 	if (endpoint == 0) {
1486 		/* control EP filled in when used */
1487 		return urb;
1488 	}
1489 
1490 	ep_desc = find_ep(serial, endpoint);
1491 	if (!ep_desc) {
1492 		/* leak the urb, something's wrong and the callers don't care */
1493 		return urb;
1494 	}
1495 	if (usb_endpoint_xfer_int(ep_desc)) {
1496 		ep_type_name = "INT";
1497 		usb_fill_int_urb(urb, serial->dev,
1498 				 usb_sndintpipe(serial->dev, endpoint) | dir,
1499 				 buf, len, callback, ctx,
1500 				 ep_desc->bInterval);
1501 	} else if (usb_endpoint_xfer_bulk(ep_desc)) {
1502 		ep_type_name = "BULK";
1503 		usb_fill_bulk_urb(urb, serial->dev,
1504 				  usb_sndbulkpipe(serial->dev, endpoint) | dir,
1505 				  buf, len, callback, ctx);
1506 	} else {
1507 		dev_warn(&serial->interface->dev,
1508 			 "unsupported endpoint type %x\n",
1509 			 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1510 		usb_free_urb(urb);
1511 		return NULL;
1512 	}
1513 
1514 	dbg("%s - using urb %p for %s endpoint %x",
1515 	    __func__, urb, ep_type_name, endpoint);
1516 	return urb;
1517 }
1518 
1519 static struct callbacks {
1520 	void	(*instat_callback)(struct urb *);
1521 	void	(*glocont_callback)(struct urb *);
1522 	void	(*indat_callback)(struct urb *);
1523 	void	(*outdat_callback)(struct urb *);
1524 	void	(*inack_callback)(struct urb *);
1525 	void	(*outcont_callback)(struct urb *);
1526 } keyspan_callbacks[] = {
1527 	{
1528 		/* msg_usa26 callbacks */
1529 		.instat_callback =	usa26_instat_callback,
1530 		.glocont_callback =	usa26_glocont_callback,
1531 		.indat_callback =	usa26_indat_callback,
1532 		.outdat_callback =	usa2x_outdat_callback,
1533 		.inack_callback =	usa26_inack_callback,
1534 		.outcont_callback =	usa26_outcont_callback,
1535 	}, {
1536 		/* msg_usa28 callbacks */
1537 		.instat_callback =	usa28_instat_callback,
1538 		.glocont_callback =	usa28_glocont_callback,
1539 		.indat_callback =	usa28_indat_callback,
1540 		.outdat_callback =	usa2x_outdat_callback,
1541 		.inack_callback =	usa28_inack_callback,
1542 		.outcont_callback =	usa28_outcont_callback,
1543 	}, {
1544 		/* msg_usa49 callbacks */
1545 		.instat_callback =	usa49_instat_callback,
1546 		.glocont_callback =	usa49_glocont_callback,
1547 		.indat_callback =	usa49_indat_callback,
1548 		.outdat_callback =	usa2x_outdat_callback,
1549 		.inack_callback =	usa49_inack_callback,
1550 		.outcont_callback =	usa49_outcont_callback,
1551 	}, {
1552 		/* msg_usa90 callbacks */
1553 		.instat_callback =	usa90_instat_callback,
1554 		.glocont_callback =	usa28_glocont_callback,
1555 		.indat_callback =	usa90_indat_callback,
1556 		.outdat_callback =	usa2x_outdat_callback,
1557 		.inack_callback =	usa28_inack_callback,
1558 		.outcont_callback =	usa90_outcont_callback,
1559 	}, {
1560 		/* msg_usa67 callbacks */
1561 		.instat_callback =	usa67_instat_callback,
1562 		.glocont_callback =	usa67_glocont_callback,
1563 		.indat_callback =	usa26_indat_callback,
1564 		.outdat_callback =	usa2x_outdat_callback,
1565 		.inack_callback =	usa26_inack_callback,
1566 		.outcont_callback =	usa26_outcont_callback,
1567 	}
1568 };
1569 
1570 	/* Generic setup urbs function that uses
1571 	   data in device_details */
1572 static void keyspan_setup_urbs(struct usb_serial *serial)
1573 {
1574 	int				i, j;
1575 	struct keyspan_serial_private 	*s_priv;
1576 	const struct keyspan_device_details	*d_details;
1577 	struct usb_serial_port		*port;
1578 	struct keyspan_port_private	*p_priv;
1579 	struct callbacks		*cback;
1580 	int				endp;
1581 
1582 	dbg("%s", __func__);
1583 
1584 	s_priv = usb_get_serial_data(serial);
1585 	d_details = s_priv->device_details;
1586 
1587 	/* Setup values for the various callback routines */
1588 	cback = &keyspan_callbacks[d_details->msg_format];
1589 
1590 	/* Allocate and set up urbs for each one that is in use,
1591 	   starting with instat endpoints */
1592 	s_priv->instat_urb = keyspan_setup_urb
1593 		(serial, d_details->instat_endpoint, USB_DIR_IN,
1594 		 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1595 		 cback->instat_callback);
1596 
1597 	s_priv->indat_urb = keyspan_setup_urb
1598 		(serial, d_details->indat_endpoint, USB_DIR_IN,
1599 		 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1600 		 usa49wg_indat_callback);
1601 
1602 	s_priv->glocont_urb = keyspan_setup_urb
1603 		(serial, d_details->glocont_endpoint, USB_DIR_OUT,
1604 		 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1605 		 cback->glocont_callback);
1606 
1607 	/* Setup endpoints for each port specific thing */
1608 	for (i = 0; i < d_details->num_ports; i++) {
1609 		port = serial->port[i];
1610 		p_priv = usb_get_serial_port_data(port);
1611 
1612 		/* Do indat endpoints first, once for each flip */
1613 		endp = d_details->indat_endpoints[i];
1614 		for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1615 			p_priv->in_urbs[j] = keyspan_setup_urb
1616 				(serial, endp, USB_DIR_IN, port,
1617 				 p_priv->in_buffer[j], 64,
1618 				 cback->indat_callback);
1619 		}
1620 		for (; j < 2; ++j)
1621 			p_priv->in_urbs[j] = NULL;
1622 
1623 		/* outdat endpoints also have flip */
1624 		endp = d_details->outdat_endpoints[i];
1625 		for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1626 			p_priv->out_urbs[j] = keyspan_setup_urb
1627 				(serial, endp, USB_DIR_OUT, port,
1628 				 p_priv->out_buffer[j], 64,
1629 				 cback->outdat_callback);
1630 		}
1631 		for (; j < 2; ++j)
1632 			p_priv->out_urbs[j] = NULL;
1633 
1634 		/* inack endpoint */
1635 		p_priv->inack_urb = keyspan_setup_urb
1636 			(serial, d_details->inack_endpoints[i], USB_DIR_IN,
1637 			 port, p_priv->inack_buffer, 1, cback->inack_callback);
1638 
1639 		/* outcont endpoint */
1640 		p_priv->outcont_urb = keyspan_setup_urb
1641 			(serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1642 			 port, p_priv->outcont_buffer, 64,
1643 			 cback->outcont_callback);
1644 	}
1645 }
1646 
1647 /* usa19 function doesn't require prescaler */
1648 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1649 				   u8 *rate_low, u8 *prescaler, int portnum)
1650 {
1651 	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
1652 		div,	/* divisor */
1653 		cnt;	/* inverse of divisor (programmed into 8051) */
1654 
1655 	dbg("%s - %d.", __func__, baud_rate);
1656 
1657 	/* prevent divide by zero...  */
1658 	b16 = baud_rate * 16L;
1659 	if (b16 == 0)
1660 		return KEYSPAN_INVALID_BAUD_RATE;
1661 	/* Any "standard" rate over 57k6 is marginal on the USA-19
1662 	   as we run out of divisor resolution. */
1663 	if (baud_rate > 57600)
1664 		return KEYSPAN_INVALID_BAUD_RATE;
1665 
1666 	/* calculate the divisor and the counter (its inverse) */
1667 	div = baudclk / b16;
1668 	if (div == 0)
1669 		return KEYSPAN_INVALID_BAUD_RATE;
1670 	else
1671 		cnt = 0 - div;
1672 
1673 	if (div > 0xffff)
1674 		return KEYSPAN_INVALID_BAUD_RATE;
1675 
1676 	/* return the counter values if non-null */
1677 	if (rate_low)
1678 		*rate_low = (u8) (cnt & 0xff);
1679 	if (rate_hi)
1680 		*rate_hi = (u8) ((cnt >> 8) & 0xff);
1681 	if (rate_low && rate_hi)
1682 		dbg("%s - %d %02x %02x.",
1683 				__func__, baud_rate, *rate_hi, *rate_low);
1684 	return KEYSPAN_BAUD_RATE_OK;
1685 }
1686 
1687 /* usa19hs function doesn't require prescaler */
1688 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1689 				   u8 *rate_low, u8 *prescaler, int portnum)
1690 {
1691 	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
1692 			div;	/* divisor */
1693 
1694 	dbg("%s - %d.", __func__, baud_rate);
1695 
1696 	/* prevent divide by zero...  */
1697 	b16 = baud_rate * 16L;
1698 	if (b16 == 0)
1699 		return KEYSPAN_INVALID_BAUD_RATE;
1700 
1701 	/* calculate the divisor */
1702 	div = baudclk / b16;
1703 	if (div == 0)
1704 		return KEYSPAN_INVALID_BAUD_RATE;
1705 
1706 	if (div > 0xffff)
1707 		return KEYSPAN_INVALID_BAUD_RATE;
1708 
1709 	/* return the counter values if non-null */
1710 	if (rate_low)
1711 		*rate_low = (u8) (div & 0xff);
1712 
1713 	if (rate_hi)
1714 		*rate_hi = (u8) ((div >> 8) & 0xff);
1715 
1716 	if (rate_low && rate_hi)
1717 		dbg("%s - %d %02x %02x.",
1718 			__func__, baud_rate, *rate_hi, *rate_low);
1719 
1720 	return KEYSPAN_BAUD_RATE_OK;
1721 }
1722 
1723 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1724 				    u8 *rate_low, u8 *prescaler, int portnum)
1725 {
1726 	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
1727 		clk,	/* clock with 13/8 prescaler */
1728 		div,	/* divisor using 13/8 prescaler */
1729 		res,	/* resulting baud rate using 13/8 prescaler */
1730 		diff,	/* error using 13/8 prescaler */
1731 		smallest_diff;
1732 	u8	best_prescaler;
1733 	int	i;
1734 
1735 	dbg("%s - %d.", __func__, baud_rate);
1736 
1737 	/* prevent divide by zero */
1738 	b16 = baud_rate * 16L;
1739 	if (b16 == 0)
1740 		return KEYSPAN_INVALID_BAUD_RATE;
1741 
1742 	/* Calculate prescaler by trying them all and looking
1743 	   for best fit */
1744 
1745 	/* start with largest possible difference */
1746 	smallest_diff = 0xffffffff;
1747 
1748 		/* 0 is an invalid prescaler, used as a flag */
1749 	best_prescaler = 0;
1750 
1751 	for (i = 8; i <= 0xff; ++i) {
1752 		clk = (baudclk * 8) / (u32) i;
1753 
1754 		div = clk / b16;
1755 		if (div == 0)
1756 			continue;
1757 
1758 		res = clk / div;
1759 		diff = (res > b16) ? (res-b16) : (b16-res);
1760 
1761 		if (diff < smallest_diff) {
1762 			best_prescaler = i;
1763 			smallest_diff = diff;
1764 		}
1765 	}
1766 
1767 	if (best_prescaler == 0)
1768 		return KEYSPAN_INVALID_BAUD_RATE;
1769 
1770 	clk = (baudclk * 8) / (u32) best_prescaler;
1771 	div = clk / b16;
1772 
1773 	/* return the divisor and prescaler if non-null */
1774 	if (rate_low)
1775 		*rate_low = (u8) (div & 0xff);
1776 	if (rate_hi)
1777 		*rate_hi = (u8) ((div >> 8) & 0xff);
1778 	if (prescaler) {
1779 		*prescaler = best_prescaler;
1780 		/*  dbg("%s - %d %d", __func__, *prescaler, div); */
1781 	}
1782 	return KEYSPAN_BAUD_RATE_OK;
1783 }
1784 
1785 	/* USA-28 supports different maximum baud rates on each port */
1786 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1787 				    u8 *rate_low, u8 *prescaler, int portnum)
1788 {
1789 	u32 	b16,	/* baud rate times 16 (actual rate used internally) */
1790 		div,	/* divisor */
1791 		cnt;	/* inverse of divisor (programmed into 8051) */
1792 
1793 	dbg("%s - %d.", __func__, baud_rate);
1794 
1795 		/* prevent divide by zero */
1796 	b16 = baud_rate * 16L;
1797 	if (b16 == 0)
1798 		return KEYSPAN_INVALID_BAUD_RATE;
1799 
1800 	/* calculate the divisor and the counter (its inverse) */
1801 	div = KEYSPAN_USA28_BAUDCLK / b16;
1802 	if (div == 0)
1803 		return KEYSPAN_INVALID_BAUD_RATE;
1804 	else
1805 		cnt = 0 - div;
1806 
1807 	/* check for out of range, based on portnum,
1808 	   and return result */
1809 	if (portnum == 0) {
1810 		if (div > 0xffff)
1811 			return KEYSPAN_INVALID_BAUD_RATE;
1812 	} else {
1813 		if (portnum == 1) {
1814 			if (div > 0xff)
1815 				return KEYSPAN_INVALID_BAUD_RATE;
1816 		} else
1817 			return KEYSPAN_INVALID_BAUD_RATE;
1818 	}
1819 
1820 		/* return the counter values if not NULL
1821 		   (port 1 will ignore retHi) */
1822 	if (rate_low)
1823 		*rate_low = (u8) (cnt & 0xff);
1824 	if (rate_hi)
1825 		*rate_hi = (u8) ((cnt >> 8) & 0xff);
1826 	dbg("%s - %d OK.", __func__, baud_rate);
1827 	return KEYSPAN_BAUD_RATE_OK;
1828 }
1829 
1830 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1831 				    struct usb_serial_port *port,
1832 				    int reset_port)
1833 {
1834 	struct keyspan_usa26_portControlMessage	msg;
1835 	struct keyspan_serial_private 		*s_priv;
1836 	struct keyspan_port_private 		*p_priv;
1837 	const struct keyspan_device_details	*d_details;
1838 	int 					outcont_urb;
1839 	struct urb				*this_urb;
1840 	int 					device_port, err;
1841 
1842 	dbg("%s reset=%d", __func__, reset_port);
1843 
1844 	s_priv = usb_get_serial_data(serial);
1845 	p_priv = usb_get_serial_port_data(port);
1846 	d_details = s_priv->device_details;
1847 	device_port = port->number - port->serial->minor;
1848 
1849 	outcont_urb = d_details->outcont_endpoints[port->number];
1850 	this_urb = p_priv->outcont_urb;
1851 
1852 	dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1853 
1854 		/* Make sure we have an urb then send the message */
1855 	if (this_urb == NULL) {
1856 		dbg("%s - oops no urb.", __func__);
1857 		return -1;
1858 	}
1859 
1860 	/* Save reset port val for resend.
1861 	   Don't overwrite resend for open/close condition. */
1862 	if ((reset_port + 1) > p_priv->resend_cont)
1863 		p_priv->resend_cont = reset_port + 1;
1864 	if (this_urb->status == -EINPROGRESS) {
1865 		/*  dbg("%s - already writing", __func__); */
1866 		mdelay(5);
1867 		return -1;
1868 	}
1869 
1870 	memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1871 
1872 	/* Only set baud rate if it's changed */
1873 	if (p_priv->old_baud != p_priv->baud) {
1874 		p_priv->old_baud = p_priv->baud;
1875 		msg.setClocking = 0xff;
1876 		if (d_details->calculate_baud_rate
1877 		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
1878 		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1879 			dbg("%s - Invalid baud rate %d requested, using 9600.",
1880 						__func__, p_priv->baud);
1881 			msg.baudLo = 0;
1882 			msg.baudHi = 125;	/* Values for 9600 baud */
1883 			msg.prescaler = 10;
1884 		}
1885 		msg.setPrescaler = 0xff;
1886 	}
1887 
1888 	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1889 	switch (p_priv->cflag & CSIZE) {
1890 	case CS5:
1891 		msg.lcr |= USA_DATABITS_5;
1892 		break;
1893 	case CS6:
1894 		msg.lcr |= USA_DATABITS_6;
1895 		break;
1896 	case CS7:
1897 		msg.lcr |= USA_DATABITS_7;
1898 		break;
1899 	case CS8:
1900 		msg.lcr |= USA_DATABITS_8;
1901 		break;
1902 	}
1903 	if (p_priv->cflag & PARENB) {
1904 		/* note USA_PARITY_NONE == 0 */
1905 		msg.lcr |= (p_priv->cflag & PARODD)?
1906 			USA_PARITY_ODD : USA_PARITY_EVEN;
1907 	}
1908 	msg.setLcr = 0xff;
1909 
1910 	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1911 	msg.xonFlowControl = 0;
1912 	msg.setFlowControl = 0xff;
1913 	msg.forwardingLength = 16;
1914 	msg.xonChar = 17;
1915 	msg.xoffChar = 19;
1916 
1917 	/* Opening port */
1918 	if (reset_port == 1) {
1919 		msg._txOn = 1;
1920 		msg._txOff = 0;
1921 		msg.txFlush = 0;
1922 		msg.txBreak = 0;
1923 		msg.rxOn = 1;
1924 		msg.rxOff = 0;
1925 		msg.rxFlush = 1;
1926 		msg.rxForward = 0;
1927 		msg.returnStatus = 0;
1928 		msg.resetDataToggle = 0xff;
1929 	}
1930 
1931 	/* Closing port */
1932 	else if (reset_port == 2) {
1933 		msg._txOn = 0;
1934 		msg._txOff = 1;
1935 		msg.txFlush = 0;
1936 		msg.txBreak = 0;
1937 		msg.rxOn = 0;
1938 		msg.rxOff = 1;
1939 		msg.rxFlush = 1;
1940 		msg.rxForward = 0;
1941 		msg.returnStatus = 0;
1942 		msg.resetDataToggle = 0;
1943 	}
1944 
1945 	/* Sending intermediate configs */
1946 	else {
1947 		msg._txOn = (!p_priv->break_on);
1948 		msg._txOff = 0;
1949 		msg.txFlush = 0;
1950 		msg.txBreak = (p_priv->break_on);
1951 		msg.rxOn = 0;
1952 		msg.rxOff = 0;
1953 		msg.rxFlush = 0;
1954 		msg.rxForward = 0;
1955 		msg.returnStatus = 0;
1956 		msg.resetDataToggle = 0x0;
1957 	}
1958 
1959 	/* Do handshaking outputs */
1960 	msg.setTxTriState_setRts = 0xff;
1961 	msg.txTriState_rts = p_priv->rts_state;
1962 
1963 	msg.setHskoa_setDtr = 0xff;
1964 	msg.hskoa_dtr = p_priv->dtr_state;
1965 
1966 	p_priv->resend_cont = 0;
1967 	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1968 
1969 	/* send the data out the device on control endpoint */
1970 	this_urb->transfer_buffer_length = sizeof(msg);
1971 
1972 	this_urb->dev = serial->dev;
1973 	err = usb_submit_urb(this_urb, GFP_ATOMIC);
1974 	if (err != 0)
1975 		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1976 #if 0
1977 	else {
1978 		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1979 		    outcont_urb, this_urb->transfer_buffer_length,
1980 		    usb_pipeendpoint(this_urb->pipe));
1981 	}
1982 #endif
1983 
1984 	return 0;
1985 }
1986 
1987 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1988 				    struct usb_serial_port *port,
1989 				    int reset_port)
1990 {
1991 	struct keyspan_usa28_portControlMessage	msg;
1992 	struct keyspan_serial_private	 	*s_priv;
1993 	struct keyspan_port_private 		*p_priv;
1994 	const struct keyspan_device_details	*d_details;
1995 	struct urb				*this_urb;
1996 	int 					device_port, err;
1997 
1998 	dbg("%s", __func__);
1999 
2000 	s_priv = usb_get_serial_data(serial);
2001 	p_priv = usb_get_serial_port_data(port);
2002 	d_details = s_priv->device_details;
2003 	device_port = port->number - port->serial->minor;
2004 
2005 	/* only do something if we have a bulk out endpoint */
2006 	this_urb = p_priv->outcont_urb;
2007 	if (this_urb == NULL) {
2008 		dbg("%s - oops no urb.", __func__);
2009 		return -1;
2010 	}
2011 
2012 	/* Save reset port val for resend.
2013 	   Don't overwrite resend for open/close condition. */
2014 	if ((reset_port + 1) > p_priv->resend_cont)
2015 		p_priv->resend_cont = reset_port + 1;
2016 	if (this_urb->status == -EINPROGRESS) {
2017 		dbg("%s already writing", __func__);
2018 		mdelay(5);
2019 		return -1;
2020 	}
2021 
2022 	memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
2023 
2024 	msg.setBaudRate = 1;
2025 	if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2026 		&msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2027 		dbg("%s - Invalid baud rate requested %d.",
2028 						__func__, p_priv->baud);
2029 		msg.baudLo = 0xff;
2030 		msg.baudHi = 0xb2;	/* Values for 9600 baud */
2031 	}
2032 
2033 	/* If parity is enabled, we must calculate it ourselves. */
2034 	msg.parity = 0;		/* XXX for now */
2035 
2036 	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2037 	msg.xonFlowControl = 0;
2038 
2039 	/* Do handshaking outputs, DTR is inverted relative to RTS */
2040 	msg.rts = p_priv->rts_state;
2041 	msg.dtr = p_priv->dtr_state;
2042 
2043 	msg.forwardingLength = 16;
2044 	msg.forwardMs = 10;
2045 	msg.breakThreshold = 45;
2046 	msg.xonChar = 17;
2047 	msg.xoffChar = 19;
2048 
2049 	/*msg.returnStatus = 1;
2050 	msg.resetDataToggle = 0xff;*/
2051 	/* Opening port */
2052 	if (reset_port == 1) {
2053 		msg._txOn = 1;
2054 		msg._txOff = 0;
2055 		msg.txFlush = 0;
2056 		msg.txForceXoff = 0;
2057 		msg.txBreak = 0;
2058 		msg.rxOn = 1;
2059 		msg.rxOff = 0;
2060 		msg.rxFlush = 1;
2061 		msg.rxForward = 0;
2062 		msg.returnStatus = 0;
2063 		msg.resetDataToggle = 0xff;
2064 	}
2065 	/* Closing port */
2066 	else if (reset_port == 2) {
2067 		msg._txOn = 0;
2068 		msg._txOff = 1;
2069 		msg.txFlush = 0;
2070 		msg.txForceXoff = 0;
2071 		msg.txBreak = 0;
2072 		msg.rxOn = 0;
2073 		msg.rxOff = 1;
2074 		msg.rxFlush = 1;
2075 		msg.rxForward = 0;
2076 		msg.returnStatus = 0;
2077 		msg.resetDataToggle = 0;
2078 	}
2079 	/* Sending intermediate configs */
2080 	else {
2081 		msg._txOn = (!p_priv->break_on);
2082 		msg._txOff = 0;
2083 		msg.txFlush = 0;
2084 		msg.txForceXoff = 0;
2085 		msg.txBreak = (p_priv->break_on);
2086 		msg.rxOn = 0;
2087 		msg.rxOff = 0;
2088 		msg.rxFlush = 0;
2089 		msg.rxForward = 0;
2090 		msg.returnStatus = 0;
2091 		msg.resetDataToggle = 0x0;
2092 	}
2093 
2094 	p_priv->resend_cont = 0;
2095 	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2096 
2097 	/* send the data out the device on control endpoint */
2098 	this_urb->transfer_buffer_length = sizeof(msg);
2099 
2100 	this_urb->dev = serial->dev;
2101 	err = usb_submit_urb(this_urb, GFP_ATOMIC);
2102 	if (err != 0)
2103 		dbg("%s - usb_submit_urb(setup) failed", __func__);
2104 #if 0
2105 	else {
2106 		dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
2107 		    this_urb->transfer_buffer_length);
2108 	}
2109 #endif
2110 
2111 	return 0;
2112 }
2113 
2114 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2115 				    struct usb_serial_port *port,
2116 				    int reset_port)
2117 {
2118 	struct keyspan_usa49_portControlMessage	msg;
2119 	struct usb_ctrlrequest 			*dr = NULL;
2120 	struct keyspan_serial_private 		*s_priv;
2121 	struct keyspan_port_private 		*p_priv;
2122 	const struct keyspan_device_details	*d_details;
2123 	struct urb				*this_urb;
2124 	int 					err, device_port;
2125 
2126 	dbg("%s", __func__);
2127 
2128 	s_priv = usb_get_serial_data(serial);
2129 	p_priv = usb_get_serial_port_data(port);
2130 	d_details = s_priv->device_details;
2131 
2132 	this_urb = s_priv->glocont_urb;
2133 
2134 	/* Work out which port within the device is being setup */
2135 	device_port = port->number - port->serial->minor;
2136 
2137 	dbg("%s - endpoint %d port %d (%d)",
2138 			__func__, usb_pipeendpoint(this_urb->pipe),
2139 			port->number, device_port);
2140 
2141 		/* Make sure we have an urb then send the message */
2142 	if (this_urb == NULL) {
2143 		dbg("%s - oops no urb for port %d.", __func__, port->number);
2144 		return -1;
2145 	}
2146 
2147 	/* Save reset port val for resend.
2148 	   Don't overwrite resend for open/close condition. */
2149 	if ((reset_port + 1) > p_priv->resend_cont)
2150 		p_priv->resend_cont = reset_port + 1;
2151 
2152 	if (this_urb->status == -EINPROGRESS) {
2153 		/*  dbg("%s - already writing", __func__); */
2154 		mdelay(5);
2155 		return -1;
2156 	}
2157 
2158 	memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
2159 
2160 	/*msg.portNumber = port->number;*/
2161 	msg.portNumber = device_port;
2162 
2163 	/* Only set baud rate if it's changed */
2164 	if (p_priv->old_baud != p_priv->baud) {
2165 		p_priv->old_baud = p_priv->baud;
2166 		msg.setClocking = 0xff;
2167 		if (d_details->calculate_baud_rate
2168 		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
2169 		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2170 			dbg("%s - Invalid baud rate %d requested, using 9600.",
2171 						__func__, p_priv->baud);
2172 			msg.baudLo = 0;
2173 			msg.baudHi = 125;	/* Values for 9600 baud */
2174 			msg.prescaler = 10;
2175 		}
2176 		/* msg.setPrescaler = 0xff; */
2177 	}
2178 
2179 	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2180 	switch (p_priv->cflag & CSIZE) {
2181 	case CS5:
2182 		msg.lcr |= USA_DATABITS_5;
2183 		break;
2184 	case CS6:
2185 		msg.lcr |= USA_DATABITS_6;
2186 		break;
2187 	case CS7:
2188 		msg.lcr |= USA_DATABITS_7;
2189 		break;
2190 	case CS8:
2191 		msg.lcr |= USA_DATABITS_8;
2192 		break;
2193 	}
2194 	if (p_priv->cflag & PARENB) {
2195 		/* note USA_PARITY_NONE == 0 */
2196 		msg.lcr |= (p_priv->cflag & PARODD)?
2197 			USA_PARITY_ODD : USA_PARITY_EVEN;
2198 	}
2199 	msg.setLcr = 0xff;
2200 
2201 	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2202 	msg.xonFlowControl = 0;
2203 	msg.setFlowControl = 0xff;
2204 
2205 	msg.forwardingLength = 16;
2206 	msg.xonChar = 17;
2207 	msg.xoffChar = 19;
2208 
2209 	/* Opening port */
2210 	if (reset_port == 1) {
2211 		msg._txOn = 1;
2212 		msg._txOff = 0;
2213 		msg.txFlush = 0;
2214 		msg.txBreak = 0;
2215 		msg.rxOn = 1;
2216 		msg.rxOff = 0;
2217 		msg.rxFlush = 1;
2218 		msg.rxForward = 0;
2219 		msg.returnStatus = 0;
2220 		msg.resetDataToggle = 0xff;
2221 		msg.enablePort = 1;
2222 		msg.disablePort = 0;
2223 	}
2224 	/* Closing port */
2225 	else if (reset_port == 2) {
2226 		msg._txOn = 0;
2227 		msg._txOff = 1;
2228 		msg.txFlush = 0;
2229 		msg.txBreak = 0;
2230 		msg.rxOn = 0;
2231 		msg.rxOff = 1;
2232 		msg.rxFlush = 1;
2233 		msg.rxForward = 0;
2234 		msg.returnStatus = 0;
2235 		msg.resetDataToggle = 0;
2236 		msg.enablePort = 0;
2237 		msg.disablePort = 1;
2238 	}
2239 	/* Sending intermediate configs */
2240 	else {
2241 		msg._txOn = (!p_priv->break_on);
2242 		msg._txOff = 0;
2243 		msg.txFlush = 0;
2244 		msg.txBreak = (p_priv->break_on);
2245 		msg.rxOn = 0;
2246 		msg.rxOff = 0;
2247 		msg.rxFlush = 0;
2248 		msg.rxForward = 0;
2249 		msg.returnStatus = 0;
2250 		msg.resetDataToggle = 0x0;
2251 		msg.enablePort = 0;
2252 		msg.disablePort = 0;
2253 	}
2254 
2255 	/* Do handshaking outputs */
2256 	msg.setRts = 0xff;
2257 	msg.rts = p_priv->rts_state;
2258 
2259 	msg.setDtr = 0xff;
2260 	msg.dtr = p_priv->dtr_state;
2261 
2262 	p_priv->resend_cont = 0;
2263 
2264 	/* if the device is a 49wg, we send control message on usb
2265 	   control EP 0 */
2266 
2267 	if (d_details->product_id == keyspan_usa49wg_product_id) {
2268 		dr = (void *)(s_priv->ctrl_buf);
2269 		dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2270 		dr->bRequest = 0xB0;	/* 49wg control message */;
2271 		dr->wValue = 0;
2272 		dr->wIndex = 0;
2273 		dr->wLength = cpu_to_le16(sizeof(msg));
2274 
2275 		memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2276 
2277 		usb_fill_control_urb(this_urb, serial->dev,
2278 				usb_sndctrlpipe(serial->dev, 0),
2279 				(unsigned char *)dr, s_priv->glocont_buf,
2280 				sizeof(msg), usa49_glocont_callback, serial);
2281 
2282 	} else {
2283 		memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2284 
2285 		/* send the data out the device on control endpoint */
2286 		this_urb->transfer_buffer_length = sizeof(msg);
2287 
2288 		this_urb->dev = serial->dev;
2289 	}
2290 	err = usb_submit_urb(this_urb, GFP_ATOMIC);
2291 	if (err != 0)
2292 		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2293 #if 0
2294 	else {
2295 		dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2296 			   outcont_urb, this_urb->transfer_buffer_length,
2297 			   usb_pipeendpoint(this_urb->pipe));
2298 	}
2299 #endif
2300 
2301 	return 0;
2302 }
2303 
2304 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2305 				    struct usb_serial_port *port,
2306 				    int reset_port)
2307 {
2308 	struct keyspan_usa90_portControlMessage	msg;
2309 	struct keyspan_serial_private 		*s_priv;
2310 	struct keyspan_port_private 		*p_priv;
2311 	const struct keyspan_device_details	*d_details;
2312 	struct urb				*this_urb;
2313 	int 					err;
2314 	u8						prescaler;
2315 
2316 	dbg("%s", __func__);
2317 
2318 	s_priv = usb_get_serial_data(serial);
2319 	p_priv = usb_get_serial_port_data(port);
2320 	d_details = s_priv->device_details;
2321 
2322 	/* only do something if we have a bulk out endpoint */
2323 	this_urb = p_priv->outcont_urb;
2324 	if (this_urb == NULL) {
2325 		dbg("%s - oops no urb.", __func__);
2326 		return -1;
2327 	}
2328 
2329 	/* Save reset port val for resend.
2330 	   Don't overwrite resend for open/close condition. */
2331 	if ((reset_port + 1) > p_priv->resend_cont)
2332 		p_priv->resend_cont = reset_port + 1;
2333 	if (this_urb->status == -EINPROGRESS) {
2334 		dbg("%s already writing", __func__);
2335 		mdelay(5);
2336 		return -1;
2337 	}
2338 
2339 	memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2340 
2341 	/* Only set baud rate if it's changed */
2342 	if (p_priv->old_baud != p_priv->baud) {
2343 		p_priv->old_baud = p_priv->baud;
2344 		msg.setClocking = 0x01;
2345 		if (d_details->calculate_baud_rate
2346 		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
2347 		     &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2348 			dbg("%s - Invalid baud rate %d requested, using 9600.",
2349 						__func__, p_priv->baud);
2350 			p_priv->baud = 9600;
2351 			d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2352 				&msg.baudHi, &msg.baudLo, &prescaler, 0);
2353 		}
2354 		msg.setRxMode = 1;
2355 		msg.setTxMode = 1;
2356 	}
2357 
2358 	/* modes must always be correctly specified */
2359 	if (p_priv->baud > 57600) {
2360 		msg.rxMode = RXMODE_DMA;
2361 		msg.txMode = TXMODE_DMA;
2362 	} else {
2363 		msg.rxMode = RXMODE_BYHAND;
2364 		msg.txMode = TXMODE_BYHAND;
2365 	}
2366 
2367 	msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2368 	switch (p_priv->cflag & CSIZE) {
2369 	case CS5:
2370 		msg.lcr |= USA_DATABITS_5;
2371 		break;
2372 	case CS6:
2373 		msg.lcr |= USA_DATABITS_6;
2374 		break;
2375 	case CS7:
2376 		msg.lcr |= USA_DATABITS_7;
2377 		break;
2378 	case CS8:
2379 		msg.lcr |= USA_DATABITS_8;
2380 		break;
2381 	}
2382 	if (p_priv->cflag & PARENB) {
2383 		/* note USA_PARITY_NONE == 0 */
2384 		msg.lcr |= (p_priv->cflag & PARODD)?
2385 			USA_PARITY_ODD : USA_PARITY_EVEN;
2386 	}
2387 	if (p_priv->old_cflag != p_priv->cflag) {
2388 		p_priv->old_cflag = p_priv->cflag;
2389 		msg.setLcr = 0x01;
2390 	}
2391 
2392 	if (p_priv->flow_control == flow_cts)
2393 		msg.txFlowControl = TXFLOW_CTS;
2394 	msg.setTxFlowControl = 0x01;
2395 	msg.setRxFlowControl = 0x01;
2396 
2397 	msg.rxForwardingLength = 16;
2398 	msg.rxForwardingTimeout = 16;
2399 	msg.txAckSetting = 0;
2400 	msg.xonChar = 17;
2401 	msg.xoffChar = 19;
2402 
2403 	/* Opening port */
2404 	if (reset_port == 1) {
2405 		msg.portEnabled = 1;
2406 		msg.rxFlush = 1;
2407 		msg.txBreak = (p_priv->break_on);
2408 	}
2409 	/* Closing port */
2410 	else if (reset_port == 2)
2411 		msg.portEnabled = 0;
2412 	/* Sending intermediate configs */
2413 	else {
2414 		if (port->port.count)
2415 			msg.portEnabled = 1;
2416 		msg.txBreak = (p_priv->break_on);
2417 	}
2418 
2419 	/* Do handshaking outputs */
2420 	msg.setRts = 0x01;
2421 	msg.rts = p_priv->rts_state;
2422 
2423 	msg.setDtr = 0x01;
2424 	msg.dtr = p_priv->dtr_state;
2425 
2426 	p_priv->resend_cont = 0;
2427 	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2428 
2429 	/* send the data out the device on control endpoint */
2430 	this_urb->transfer_buffer_length = sizeof(msg);
2431 
2432 	this_urb->dev = serial->dev;
2433 	err = usb_submit_urb(this_urb, GFP_ATOMIC);
2434 	if (err != 0)
2435 		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2436 	return 0;
2437 }
2438 
2439 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2440 				    struct usb_serial_port *port,
2441 				    int reset_port)
2442 {
2443 	struct keyspan_usa67_portControlMessage	msg;
2444 	struct keyspan_serial_private 		*s_priv;
2445 	struct keyspan_port_private 		*p_priv;
2446 	const struct keyspan_device_details	*d_details;
2447 	struct urb				*this_urb;
2448 	int 					err, device_port;
2449 
2450 	dbg("%s", __func__);
2451 
2452 	s_priv = usb_get_serial_data(serial);
2453 	p_priv = usb_get_serial_port_data(port);
2454 	d_details = s_priv->device_details;
2455 
2456 	this_urb = s_priv->glocont_urb;
2457 
2458 	/* Work out which port within the device is being setup */
2459 	device_port = port->number - port->serial->minor;
2460 
2461 	/* Make sure we have an urb then send the message */
2462 	if (this_urb == NULL) {
2463 		dbg("%s - oops no urb for port %d.", __func__,
2464 			port->number);
2465 		return -1;
2466 	}
2467 
2468 	/* Save reset port val for resend.
2469 	   Don't overwrite resend for open/close condition. */
2470 	if ((reset_port + 1) > p_priv->resend_cont)
2471 		p_priv->resend_cont = reset_port + 1;
2472 	if (this_urb->status == -EINPROGRESS) {
2473 		/*  dbg("%s - already writing", __func__); */
2474 		mdelay(5);
2475 		return -1;
2476 	}
2477 
2478 	memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2479 
2480 	msg.port = device_port;
2481 
2482 	/* Only set baud rate if it's changed */
2483 	if (p_priv->old_baud != p_priv->baud) {
2484 		p_priv->old_baud = p_priv->baud;
2485 		msg.setClocking = 0xff;
2486 		if (d_details->calculate_baud_rate
2487 		    (p_priv->baud, d_details->baudclk, &msg.baudHi,
2488 		     &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2489 			dbg("%s - Invalid baud rate %d requested, using 9600.",
2490 						__func__, p_priv->baud);
2491 			msg.baudLo = 0;
2492 			msg.baudHi = 125;	/* Values for 9600 baud */
2493 			msg.prescaler = 10;
2494 		}
2495 		msg.setPrescaler = 0xff;
2496 	}
2497 
2498 	msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2499 	switch (p_priv->cflag & CSIZE) {
2500 	case CS5:
2501 		msg.lcr |= USA_DATABITS_5;
2502 		break;
2503 	case CS6:
2504 		msg.lcr |= USA_DATABITS_6;
2505 		break;
2506 	case CS7:
2507 		msg.lcr |= USA_DATABITS_7;
2508 		break;
2509 	case CS8:
2510 		msg.lcr |= USA_DATABITS_8;
2511 		break;
2512 	}
2513 	if (p_priv->cflag & PARENB) {
2514 		/* note USA_PARITY_NONE == 0 */
2515 		msg.lcr |= (p_priv->cflag & PARODD)?
2516 					USA_PARITY_ODD : USA_PARITY_EVEN;
2517 	}
2518 	msg.setLcr = 0xff;
2519 
2520 	msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2521 	msg.xonFlowControl = 0;
2522 	msg.setFlowControl = 0xff;
2523 	msg.forwardingLength = 16;
2524 	msg.xonChar = 17;
2525 	msg.xoffChar = 19;
2526 
2527 	if (reset_port == 1) {
2528 		/* Opening port */
2529 		msg._txOn = 1;
2530 		msg._txOff = 0;
2531 		msg.txFlush = 0;
2532 		msg.txBreak = 0;
2533 		msg.rxOn = 1;
2534 		msg.rxOff = 0;
2535 		msg.rxFlush = 1;
2536 		msg.rxForward = 0;
2537 		msg.returnStatus = 0;
2538 		msg.resetDataToggle = 0xff;
2539 	} else if (reset_port == 2) {
2540 		/* Closing port */
2541 		msg._txOn = 0;
2542 		msg._txOff = 1;
2543 		msg.txFlush = 0;
2544 		msg.txBreak = 0;
2545 		msg.rxOn = 0;
2546 		msg.rxOff = 1;
2547 		msg.rxFlush = 1;
2548 		msg.rxForward = 0;
2549 		msg.returnStatus = 0;
2550 		msg.resetDataToggle = 0;
2551 	} else {
2552 		/* Sending intermediate configs */
2553 		msg._txOn = (!p_priv->break_on);
2554 		msg._txOff = 0;
2555 		msg.txFlush = 0;
2556 		msg.txBreak = (p_priv->break_on);
2557 		msg.rxOn = 0;
2558 		msg.rxOff = 0;
2559 		msg.rxFlush = 0;
2560 		msg.rxForward = 0;
2561 		msg.returnStatus = 0;
2562 		msg.resetDataToggle = 0x0;
2563 	}
2564 
2565 	/* Do handshaking outputs */
2566 	msg.setTxTriState_setRts = 0xff;
2567 	msg.txTriState_rts = p_priv->rts_state;
2568 
2569 	msg.setHskoa_setDtr = 0xff;
2570 	msg.hskoa_dtr = p_priv->dtr_state;
2571 
2572 	p_priv->resend_cont = 0;
2573 
2574 	memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2575 
2576 	/* send the data out the device on control endpoint */
2577 	this_urb->transfer_buffer_length = sizeof(msg);
2578 	this_urb->dev = serial->dev;
2579 
2580 	err = usb_submit_urb(this_urb, GFP_ATOMIC);
2581 	if (err != 0)
2582 		dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2583 				err);
2584 	return 0;
2585 }
2586 
2587 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2588 {
2589 	struct usb_serial *serial = port->serial;
2590 	struct keyspan_serial_private *s_priv;
2591 	const struct keyspan_device_details *d_details;
2592 
2593 	dbg("%s", __func__);
2594 
2595 	s_priv = usb_get_serial_data(serial);
2596 	d_details = s_priv->device_details;
2597 
2598 	switch (d_details->msg_format) {
2599 	case msg_usa26:
2600 		keyspan_usa26_send_setup(serial, port, reset_port);
2601 		break;
2602 	case msg_usa28:
2603 		keyspan_usa28_send_setup(serial, port, reset_port);
2604 		break;
2605 	case msg_usa49:
2606 		keyspan_usa49_send_setup(serial, port, reset_port);
2607 		break;
2608 	case msg_usa90:
2609 		keyspan_usa90_send_setup(serial, port, reset_port);
2610 		break;
2611 	case msg_usa67:
2612 		keyspan_usa67_send_setup(serial, port, reset_port);
2613 		break;
2614 	}
2615 }
2616 
2617 
2618 /* Gets called by the "real" driver (ie once firmware is loaded
2619    and renumeration has taken place. */
2620 static int keyspan_startup(struct usb_serial *serial)
2621 {
2622 	int				i, err;
2623 	struct usb_serial_port		*port;
2624 	struct keyspan_serial_private 	*s_priv;
2625 	struct keyspan_port_private	*p_priv;
2626 	const struct keyspan_device_details	*d_details;
2627 
2628 	dbg("%s", __func__);
2629 
2630 	for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2631 		if (d_details->product_id ==
2632 				le16_to_cpu(serial->dev->descriptor.idProduct))
2633 			break;
2634 	if (d_details == NULL) {
2635 		dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2636 		    __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2637 		return 1;
2638 	}
2639 
2640 	/* Setup private data for serial driver */
2641 	s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2642 	if (!s_priv) {
2643 		dbg("%s - kmalloc for keyspan_serial_private failed.",
2644 								__func__);
2645 		return -ENOMEM;
2646 	}
2647 
2648 	s_priv->device_details = d_details;
2649 	usb_set_serial_data(serial, s_priv);
2650 
2651 	/* Now setup per port private data */
2652 	for (i = 0; i < serial->num_ports; i++) {
2653 		port = serial->port[i];
2654 		p_priv = kzalloc(sizeof(struct keyspan_port_private),
2655 								GFP_KERNEL);
2656 		if (!p_priv) {
2657 			dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2658 			return 1;
2659 		}
2660 		p_priv->device_details = d_details;
2661 		usb_set_serial_port_data(port, p_priv);
2662 	}
2663 
2664 	keyspan_setup_urbs(serial);
2665 
2666 	if (s_priv->instat_urb != NULL) {
2667 		s_priv->instat_urb->dev = serial->dev;
2668 		err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2669 		if (err != 0)
2670 			dbg("%s - submit instat urb failed %d", __func__,
2671 				err);
2672 	}
2673 	if (s_priv->indat_urb != NULL) {
2674 		s_priv->indat_urb->dev = serial->dev;
2675 		err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2676 		if (err != 0)
2677 			dbg("%s - submit indat urb failed %d", __func__,
2678 				err);
2679 	}
2680 
2681 	return 0;
2682 }
2683 
2684 static void keyspan_shutdown(struct usb_serial *serial)
2685 {
2686 	int				i, j;
2687 	struct usb_serial_port		*port;
2688 	struct keyspan_serial_private 	*s_priv;
2689 	struct keyspan_port_private	*p_priv;
2690 
2691 	dbg("%s", __func__);
2692 
2693 	s_priv = usb_get_serial_data(serial);
2694 
2695 	/* Stop reading/writing urbs */
2696 	stop_urb(s_priv->instat_urb);
2697 	stop_urb(s_priv->glocont_urb);
2698 	stop_urb(s_priv->indat_urb);
2699 	for (i = 0; i < serial->num_ports; ++i) {
2700 		port = serial->port[i];
2701 		p_priv = usb_get_serial_port_data(port);
2702 		stop_urb(p_priv->inack_urb);
2703 		stop_urb(p_priv->outcont_urb);
2704 		for (j = 0; j < 2; j++) {
2705 			stop_urb(p_priv->in_urbs[j]);
2706 			stop_urb(p_priv->out_urbs[j]);
2707 		}
2708 	}
2709 
2710 	/* Now free them */
2711 	usb_free_urb(s_priv->instat_urb);
2712 	usb_free_urb(s_priv->indat_urb);
2713 	usb_free_urb(s_priv->glocont_urb);
2714 	for (i = 0; i < serial->num_ports; ++i) {
2715 		port = serial->port[i];
2716 		p_priv = usb_get_serial_port_data(port);
2717 		usb_free_urb(p_priv->inack_urb);
2718 		usb_free_urb(p_priv->outcont_urb);
2719 		for (j = 0; j < 2; j++) {
2720 			usb_free_urb(p_priv->in_urbs[j]);
2721 			usb_free_urb(p_priv->out_urbs[j]);
2722 		}
2723 	}
2724 
2725 	/*  dbg("Freeing serial->private."); */
2726 	kfree(s_priv);
2727 
2728 	/*  dbg("Freeing port->private."); */
2729 	/* Now free per port private data */
2730 	for (i = 0; i < serial->num_ports; i++) {
2731 		port = serial->port[i];
2732 		kfree(usb_get_serial_port_data(port));
2733 	}
2734 }
2735 
2736 MODULE_AUTHOR(DRIVER_AUTHOR);
2737 MODULE_DESCRIPTION(DRIVER_DESC);
2738 MODULE_LICENSE("GPL");
2739 
2740 MODULE_FIRMWARE("keyspan/usa28.fw");
2741 MODULE_FIRMWARE("keyspan/usa28x.fw");
2742 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2743 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2744 MODULE_FIRMWARE("keyspan/usa19.fw");
2745 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2746 MODULE_FIRMWARE("keyspan/mpr.fw");
2747 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2748 MODULE_FIRMWARE("keyspan/usa18x.fw");
2749 MODULE_FIRMWARE("keyspan/usa19w.fw");
2750 MODULE_FIRMWARE("keyspan/usa49w.fw");
2751 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2752 
2753 module_param(debug, bool, S_IRUGO | S_IWUSR);
2754 MODULE_PARM_DESC(debug, "Debug enabled or not");
2755 
2756