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