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