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