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