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