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