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