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