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