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