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