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