v2.5.0.7 -> v2.5.0.8
[opensuse:kernel.git] / drivers / usb / serial / keyspan.c
1 /*
2   Keyspan USB to Serial Converter driver
3  
4   (C) Copyright (C) 2000-2001
5       Hugh Blemings <hugh@misc.nu>
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     Mon Oct  8 14:29:00 EST 2001 hugh
32       Fixed bug that prevented mulitport devices operating correctly
33       if they weren't the first unit attached.
34
35     Sat Oct  6 12:31:21 EST 2001 hugh
36       Added support for USA-28XA and -28XB, misc cleanups, break support
37       for usa26 based models thanks to David Gibson.
38
39     Thu May 31 11:56:42 PDT 2001 gkh
40       switched from using spinlock to a semaphore
41    
42     (04/08/2001) gb
43         Identify version on module load.
44    
45     (11/01/2000) Adam J. Richter
46         usb_device_id table support.
47    
48     Tue Oct 10 23:15:33 EST 2000 Hugh
49       Merged Paul's changes with my USA-49W mods.  Work in progress
50       still...
51   
52     Wed Jul 19 14:00:42 EST 2000 gkh
53       Added module_init and module_exit functions to handle the fact that
54       this driver is a loadable module now.
55  
56     Tue Jul 18 16:14:52 EST 2000 Hugh
57       Basic character input/output for USA-19 now mostly works,
58       fixed at 9600 baud for the moment.
59
60     Sat Jul  8 11:11:48 EST 2000 Hugh
61       First public release - nothing works except the firmware upload.
62       Tested on PPC and x86 architectures, seems to behave...
63 */
64
65
66 #include <linux/config.h>
67 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/signal.h>
70 #include <linux/errno.h>
71 #include <linux/poll.h>
72 #include <linux/init.h>
73 #include <linux/slab.h>
74 #include <linux/fcntl.h>
75 #include <linux/tty.h>
76 #include <linux/tty_driver.h>
77 #include <linux/tty_flip.h>
78 #include <linux/module.h>
79 #include <linux/spinlock.h>
80 #include <linux/usb.h>
81
82 #ifdef CONFIG_USB_SERIAL_DEBUG
83         static int debug = 1;
84         #define DEBUG
85 #else
86         static int debug;
87         #undef DEBUG
88 #endif
89
90 #include <linux/usb.h>
91
92 #include "usb-serial.h"
93 #include "keyspan.h"
94
95 /*
96  * Version Information
97  */
98 #define DRIVER_VERSION "v1.1.1"
99 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
100 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
101
102 #define INSTAT_BUFLEN   32
103 #define GLOCONT_BUFLEN  64
104
105         /* Per device and per port private data */
106 struct keyspan_serial_private {
107         /* number of active ports */
108         atomic_t        active_count;
109
110         const keyspan_device_details    *device_details;
111
112         urb_t           *instat_urb;
113         char            instat_buf[INSTAT_BUFLEN];
114
115         /* XXX this one probably will need a lock */
116         urb_t           *glocont_urb;
117         char            glocont_buf[GLOCONT_BUFLEN];
118 };
119
120 struct keyspan_port_private {
121         /* Keep track of which input & output endpoints to use */
122         int             in_flip;
123         int             out_flip;
124
125         /* Keep duplicate of device details in each port
126            structure as well - simplifies some of the
127            callback functions etc. */
128         const keyspan_device_details    *device_details;
129
130         /* Input endpoints and buffer for this port */
131         urb_t           *in_urbs[2];
132         char            in_buffer[2][64];
133         /* Output endpoints and buffer for this port */
134         urb_t           *out_urbs[2];
135         char            out_buffer[2][64];
136
137         /* Input ack endpoint */
138         urb_t           *inack_urb;
139         char            inack_buffer[1];
140
141         /* Output control endpoint */
142         urb_t           *outcont_urb;
143         char            outcont_buffer[64];
144
145         /* Settings for the port */
146         int             baud;
147         int             old_baud;
148         unsigned int    cflag;
149         enum            {flow_none, flow_cts, flow_xon} flow_control;
150         int             rts_state;      /* Handshaking pins (outputs) */
151         int             dtr_state;
152         int             cts_state;      /* Handshaking pins (inputs) */
153         int             dsr_state;
154         int             dcd_state;
155         int             ri_state;
156         int             break_on;
157
158         unsigned long   tx_start_time[2];
159         int             resend_cont;    /* need to resend control packet */
160 };
161
162         
163 /* Include Keyspan message headers.  All current Keyspan Adapters
164    make use of one of three message formats which are referred
165    to as USA-26, USA-28 and USA-49 by Keyspan and within this driver. */
166 #include "keyspan_usa26msg.h"
167 #include "keyspan_usa28msg.h"
168 #include "keyspan_usa49msg.h"
169         
170 /* If you don't get debugging output, uncomment the following
171    two lines to enable cheat. */
172 #if 0
173   #undef        dbg 
174   #define       dbg     printk 
175 #endif
176
177
178 /* Functions used by new usb-serial code. */
179 static int __init keyspan_init (void)
180 {
181         usb_serial_register (&keyspan_usa18x_pre_device);
182         usb_serial_register (&keyspan_usa19_pre_device);
183         usb_serial_register (&keyspan_usa19w_pre_device);
184         usb_serial_register (&keyspan_usa28_pre_device);
185         usb_serial_register (&keyspan_usa28x_pre_device);
186         usb_serial_register (&keyspan_usa28xa_pre_device);
187         usb_serial_register (&keyspan_usa28xb_pre_device);
188         usb_serial_register (&keyspan_usa49w_pre_device);
189
190         usb_serial_register (&keyspan_usa18x_device);
191         usb_serial_register (&keyspan_usa19_device);
192         usb_serial_register (&keyspan_usa19w_device);
193         usb_serial_register (&keyspan_usa28_device);
194         usb_serial_register (&keyspan_usa28x_device);
195         usb_serial_register (&keyspan_usa28xa_device);
196         /* We don't need a separate entry for the usa28xb as it appears as a 28x anyway */
197         usb_serial_register (&keyspan_usa49w_device);
198
199         info(DRIVER_VERSION ":" DRIVER_DESC);
200
201         return 0;
202 }
203
204 static void __exit keyspan_exit (void)
205 {
206         usb_serial_deregister (&keyspan_usa18x_pre_device);
207         usb_serial_deregister (&keyspan_usa19_pre_device);
208         usb_serial_deregister (&keyspan_usa19w_pre_device);
209         usb_serial_deregister (&keyspan_usa28_pre_device);
210         usb_serial_deregister (&keyspan_usa28x_pre_device);
211         usb_serial_deregister (&keyspan_usa28xa_pre_device);
212         usb_serial_deregister (&keyspan_usa28xb_pre_device);
213         usb_serial_deregister (&keyspan_usa49w_pre_device);
214
215         usb_serial_deregister (&keyspan_usa18x_device);
216         usb_serial_deregister (&keyspan_usa19_device);
217         usb_serial_deregister (&keyspan_usa19w_device);
218         usb_serial_deregister (&keyspan_usa28_device);
219         usb_serial_deregister (&keyspan_usa28x_device);
220         usb_serial_deregister (&keyspan_usa28xa_device);
221         /* We don't need a separate entry for the usa28xb as it appears as a 28x anyway */
222         usb_serial_deregister (&keyspan_usa49w_device);
223 }
224
225 module_init(keyspan_init);
226 module_exit(keyspan_exit);
227
228 static void keyspan_rx_throttle (struct usb_serial_port *port)
229 {
230         dbg("keyspan_rx_throttle port %d\n", port->number);
231 }
232
233
234 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
235 {
236         dbg("keyspan_rx_unthrottle port %d\n", port->number);
237 }
238
239
240 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
241 {
242         struct keyspan_port_private     *p_priv;
243
244         dbg("keyspan_break_ctl\n");
245
246         p_priv = (struct keyspan_port_private *)port->private;
247
248         if (break_state == -1)
249                 p_priv->break_on = 1;
250         else
251                 p_priv->break_on = 0;
252
253         keyspan_send_setup(port, 0);
254 }
255
256
257 static void keyspan_set_termios (struct usb_serial_port *port, 
258                                      struct termios *old_termios)
259 {
260         int                             baud_rate;
261         struct keyspan_port_private     *p_priv;
262         const keyspan_device_details    *d_details;
263         unsigned int                    cflag;
264
265         dbg(__FUNCTION__ ".\n"); 
266
267         p_priv = (struct keyspan_port_private *)(port->private);
268         d_details = p_priv->device_details;
269         cflag = port->tty->termios->c_cflag;
270
271         /* Baud rate calculation takes baud rate as an integer
272            so other rates can be generated if desired. */
273         baud_rate = tty_get_baud_rate(port->tty);
274         /* If no match or invalid, don't change */              
275         if (baud_rate >= 0
276             && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
277                                 NULL, NULL, NULL) == KEYSPAN_BAUD_RATE_OK) {
278                 /* FIXME - more to do here to ensure rate changes cleanly */
279                 p_priv->baud = baud_rate;
280         }
281
282         /* set CTS/RTS handshake etc. */
283         p_priv->cflag = cflag;
284         p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
285
286         keyspan_send_setup(port, 0);
287 }
288
289 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
290                              unsigned int cmd, unsigned long arg)
291 {
292         unsigned int                    value, set;
293         struct keyspan_port_private     *p_priv;
294
295         p_priv = (struct keyspan_port_private *)(port->private);
296         
297         switch (cmd) {
298         case TIOCMGET:
299                 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
300                         ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
301                         ((p_priv->cts_state) ? TIOCM_CTS : 0) |
302                         ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
303                         ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
304                         ((p_priv->ri_state) ? TIOCM_RNG : 0); 
305
306                 if (put_user(value, (unsigned int *) arg))
307                         return -EFAULT;
308                 return 0;
309         
310         case TIOCMSET:
311                 if (get_user(value, (unsigned int *) arg))
312                         return -EFAULT;
313                 p_priv->rts_state = ((value & TIOCM_RTS) ? 1 : 0);
314                 p_priv->dtr_state = ((value & TIOCM_DTR) ? 1 : 0);
315                 keyspan_send_setup(port, 0);
316                 return 0;
317
318         case TIOCMBIS:
319         case TIOCMBIC:
320                 if (get_user(value, (unsigned int *) arg))
321                         return -EFAULT;
322                 set = (cmd == TIOCMBIS);
323                 if (value & TIOCM_RTS)
324                         p_priv->rts_state = set;
325                 if (value & TIOCM_DTR)
326                         p_priv->dtr_state = set;
327                 keyspan_send_setup(port, 0);
328                 return 0;
329         }
330
331         return -ENOIOCTLCMD;
332 }
333
334         /* Write function is generic for the three protocols used
335            with only a minor change for usa49 required */
336 static int keyspan_write(struct usb_serial_port *port, int from_user, 
337                          const unsigned char *buf, int count)
338 {
339         struct keyspan_port_private     *p_priv;
340         const keyspan_device_details    *d_details;
341         int                             flip;
342         int                             left, todo;
343         urb_t                           *this_urb;
344         int                             err;
345
346         p_priv = (struct keyspan_port_private *)(port->private);
347         d_details = p_priv->device_details;
348
349 #if 0
350         dbg(__FUNCTION__ " for port %d (%d chars [%x]), flip=%d\n",
351             port->number, count, buf[0], p_priv->out_flip);
352 #endif
353
354         for (left = count; left > 0; left -= todo) {
355                 todo = left;
356                 if (todo > 63)
357                         todo = 63;
358
359                 flip = p_priv->out_flip;
360         
361                 /* Check we have a valid urb/endpoint before we use it... */
362                 if ((this_urb = p_priv->out_urbs[flip]) == 0) {
363                         /* no bulk out, so return 0 bytes written */
364                         dbg(__FUNCTION__ " no output urb :(\n");
365                         return count;
366                 }
367
368                 dbg(__FUNCTION__ " endpoint %d\n", usb_pipeendpoint(this_urb->pipe));
369
370                 if (this_urb->status == -EINPROGRESS) {
371                         if (this_urb->transfer_flags & USB_ASYNC_UNLINK)
372                                 break;
373                         if (jiffies - p_priv->tx_start_time[flip] < 10 * HZ)
374                                 break;
375                         this_urb->transfer_flags |= USB_ASYNC_UNLINK;
376                         usb_unlink_urb(this_urb);
377                         break;
378                 }
379
380                 /* First byte in buffer is "last flag" - unused so
381                    for now so set to zero */
382                 ((char *)this_urb->transfer_buffer)[0] = 0;
383
384                 if (from_user) {
385                         if (copy_from_user(this_urb->transfer_buffer + 1, buf, todo))
386                                 return -EFAULT;
387                 } else {
388                         memcpy (this_urb->transfer_buffer + 1, buf, todo);
389                 }
390                 buf += todo;
391
392                 /* send the data out the bulk port */
393                 this_urb->transfer_buffer_length = todo + 1;
394
395                 this_urb->transfer_flags &= ~USB_ASYNC_UNLINK;
396                 this_urb->dev = port->serial->dev;
397                 if ((err = usb_submit_urb(this_urb)) != 0) {
398                         dbg("usb_submit_urb(write bulk) failed (%d)\n", err);
399                 }
400                 p_priv->tx_start_time[flip] = jiffies;
401
402                 /* Flip for next time if usa26 or usa28 interface
403                    (not used on usa49) */
404                 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
405         }
406
407         return count - left;
408 }
409
410 static void     usa26_indat_callback(struct urb *urb)
411 {
412         int                     i, err;
413         int                     endpoint;
414         struct usb_serial_port  *port;
415         struct tty_struct       *tty;
416         unsigned char           *data = urb->transfer_buffer;
417
418         dbg ("%s\n", __FUNCTION__); 
419
420         endpoint = usb_pipeendpoint(urb->pipe);
421
422         if (urb->status) {
423                 dbg(__FUNCTION__ "nonzero status: %x on endpoint %d.\n",
424                                         urb->status, endpoint);
425                 return;
426         }
427
428         port = (struct usb_serial_port *) urb->context;
429         tty = port->tty;
430         if (urb->actual_length) {
431                 if (data[0] == 0) {
432                         /* no error on any byte */
433                         for (i = 1; i < urb->actual_length ; ++i) {
434                                 tty_insert_flip_char(tty, data[i], 0);
435                         }
436                 } else {
437                         /* some bytes had errors, every byte has status */
438                         for (i = 0; i + 1 < urb->actual_length; i += 2) {
439                                 int stat = data[i], flag = 0;
440                                 if (stat & RXERROR_OVERRUN)
441                                         flag |= TTY_OVERRUN;
442                                 if (stat & RXERROR_FRAMING)
443                                         flag |= TTY_FRAME;
444                                 if (stat & RXERROR_PARITY)
445                                         flag |= TTY_PARITY;
446                                 /* XXX should handle break (0x10) */
447                                 tty_insert_flip_char(tty, data[i+1], flag);
448                         }
449                 }
450                 tty_flip_buffer_push(tty);
451         }
452                                 
453                 /* Resubmit urb so we continue receiving */
454         urb->dev = port->serial->dev;
455         if ((err = usb_submit_urb(urb)) != 0) {
456                 dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n", err);
457         }
458         return;
459 }
460
461         /* Outdat handling is common for usa26, usa28 and usa49 messages */
462 static void     usa2x_outdat_callback(struct urb *urb)
463 {
464         struct usb_serial_port *port;
465         struct keyspan_port_private *p_priv;
466
467         port = (struct usb_serial_port *) urb->context;
468         p_priv = (struct keyspan_port_private *)(port->private);
469         dbg (__FUNCTION__ " urb %d\n", urb == p_priv->out_urbs[1]); 
470
471         if (port->open_count) {
472                 queue_task(&port->tqueue, &tq_immediate);
473                 mark_bh(IMMEDIATE_BH);
474         }
475 }
476
477 static void     usa26_inack_callback(struct urb *urb)
478 {
479         dbg ("%s\n", __FUNCTION__); 
480         
481 }
482
483 static void     usa26_outcont_callback(struct urb *urb)
484 {
485         struct usb_serial_port *port;
486         struct keyspan_port_private *p_priv;
487
488         port = (struct usb_serial_port *) urb->context;
489         p_priv = (struct keyspan_port_private *)(port->private);
490
491         if (p_priv->resend_cont) {
492                 dbg (__FUNCTION__ " sending setup\n"); 
493                 keyspan_usa26_send_setup(port->serial, port, 0);
494         }
495 }
496
497 static void     usa26_instat_callback(struct urb *urb)
498 {
499         unsigned char                           *data = urb->transfer_buffer;
500         keyspan_usa26_portStatusMessage         *msg;
501         struct usb_serial                       *serial;
502         struct usb_serial_port                  *port;
503         struct keyspan_port_private             *p_priv;
504         int old_dcd_state, err;
505
506         serial = (struct usb_serial *) urb->context;
507
508         if (urb->status) {
509                 dbg(__FUNCTION__ " nonzero status: %x\n", urb->status);
510                 return;
511         }
512         if (urb->actual_length != 9) {
513                 dbg(__FUNCTION__ " %d byte report??\n", urb->actual_length);
514                 goto exit;
515         }
516
517         msg = (keyspan_usa26_portStatusMessage *)data;
518
519 #if 0
520         dbg(__FUNCTION__ " port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d\n",
521             msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
522             msg->_txXoff, msg->rxEnabled, msg->controlResponse);
523 #endif
524
525         /* Now do something useful with the data */
526
527
528         /* Check port number from message and retrieve private data */  
529         if (msg->port >= serial->num_ports) {
530                 dbg ("Unexpected port number %d\n", msg->port);
531                 goto exit;
532         }
533         port = &serial->port[msg->port];
534         p_priv = (struct keyspan_port_private *)(port->private);
535         
536         /* Update handshaking pin state information */
537         old_dcd_state = p_priv->dcd_state;
538         p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
539         p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
540         p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
541         p_priv->ri_state = ((msg->ri) ? 1 : 0);
542
543         if (port->tty && !C_CLOCAL(port->tty)
544             && old_dcd_state != p_priv->dcd_state) {
545                 if (old_dcd_state)
546                         tty_hangup(port->tty);
547                 /*  else */
548                 /*      wake_up_interruptible(&p_priv->open_wait); */
549         }
550         
551 exit:
552         /* Resubmit urb so we continue receiving */
553         urb->dev = serial->dev;
554         if ((err = usb_submit_urb(urb)) != 0) {
555                 dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n", err);
556         }
557 }
558
559 static void     usa26_glocont_callback(struct urb *urb)
560 {
561         dbg ("%s\n", __FUNCTION__);
562         
563 }
564
565
566 static void     usa28_indat_callback(struct urb *urb)
567 {
568         int                     i, err;
569         struct usb_serial_port  *port;
570         struct tty_struct       *tty;
571         unsigned char           *data;
572         struct keyspan_port_private             *p_priv;
573
574         dbg ("%s\n", __FUNCTION__);
575
576         port = (struct usb_serial_port *) urb->context;
577         p_priv = (struct keyspan_port_private *)(port->private);
578         data = urb->transfer_buffer;
579
580         if (urb != p_priv->in_urbs[p_priv->in_flip])
581                 return;
582
583         do {
584                 if (urb->status) {
585                         dbg(__FUNCTION__ "nonzero status: %x on endpoint
586 %d.\n",
587                             urb->status, usb_pipeendpoint(urb->pipe));
588                         return;
589                 }
590
591                 port = (struct usb_serial_port *) urb->context;
592                 p_priv = (struct keyspan_port_private *)(port->private);
593                 data = urb->transfer_buffer;
594
595                 tty = port->tty;
596                 if (urb->actual_length) {
597                         for (i = 0; i < urb->actual_length ; ++i) {
598                                 tty_insert_flip_char(tty, data[i], 0);
599                         }
600                         tty_flip_buffer_push(tty);
601                 }
602
603                 /* Resubmit urb so we continue receiving */
604                 urb->dev = port->serial->dev;
605                 if ((err = usb_submit_urb(urb)) != 0) {
606                         dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n",
607 err);
608                 }
609                 p_priv->in_flip ^= 1;
610
611                 urb = p_priv->in_urbs[p_priv->in_flip];
612         } while (urb->status != -EINPROGRESS);
613 }
614
615 static void     usa28_inack_callback(struct urb *urb)
616 {
617         dbg ("%s\n", __FUNCTION__);
618 }
619
620 static void     usa28_outcont_callback(struct urb *urb)
621 {
622         struct usb_serial_port *port;
623         struct keyspan_port_private *p_priv;
624
625         port = (struct usb_serial_port *) urb->context;
626         p_priv = (struct keyspan_port_private *)(port->private);
627
628         if (p_priv->resend_cont) {
629                 dbg (__FUNCTION__ " sending setup\n");
630                 keyspan_usa28_send_setup(port->serial, port, 0);
631         }
632 }
633
634 static void     usa28_instat_callback(struct urb *urb)
635 {
636         int                                     err;
637         unsigned char                           *data = urb->transfer_buffer;
638         keyspan_usa28_portStatusMessage         *msg;
639         struct usb_serial                       *serial;
640         struct usb_serial_port                  *port;
641         struct keyspan_port_private             *p_priv;
642         int old_dcd_state;
643
644         serial = (struct usb_serial *) urb->context;
645
646         if (urb->status) {
647                 dbg(__FUNCTION__ " nonzero status: %x\n", urb->status);
648                 return;
649         }
650
651         if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
652                 dbg(__FUNCTION__ " bad length %d\n", urb->actual_length);
653                 goto exit;
654         }
655
656         /*dbg(__FUNCTION__ " %x %x %x %x %x %x %x %x %x %x %x %x\n",
657             data[0], data[1], data[2], data[3], data[4], data[5],
658             data[6], data[7], data[8], data[9], data[10], data[11]);*/
659         
660                 /* Now do something useful with the data */
661         msg = (keyspan_usa28_portStatusMessage *)data;
662
663
664                 /* Check port number from message and retrieve private data */  
665         if (msg->port >= serial->num_ports) {
666                 dbg ("Unexpected port number %d\n", msg->port);
667                 goto exit;
668         }
669         port = &serial->port[msg->port];
670         p_priv = (struct keyspan_port_private *)(port->private);
671         
672         /* Update handshaking pin state information */
673         old_dcd_state = p_priv->dcd_state;
674         p_priv->cts_state = ((msg->cts) ? 1 : 0);
675         p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
676         p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
677         p_priv->ri_state = ((msg->ri) ? 1 : 0);
678
679         if (port->tty && !C_CLOCAL(port->tty)
680             && old_dcd_state != p_priv->dcd_state) {
681                 if (old_dcd_state)
682                         tty_hangup(port->tty);
683                 /*  else */
684                 /*      wake_up_interruptible(&p_priv->open_wait); */
685         }
686
687 exit:   
688                 /* Resubmit urb so we continue receiving */
689         urb->dev = serial->dev;
690         if ((err = usb_submit_urb(urb)) != 0) {
691                 dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n", err);
692         }
693 }
694
695 static void     usa28_glocont_callback(struct urb *urb)
696 {
697         dbg ("%s\n", __FUNCTION__);
698 }
699
700
701 static void     usa49_glocont_callback(struct urb *urb)
702 {
703         struct usb_serial *serial;
704         struct usb_serial_port *port;
705         struct keyspan_port_private *p_priv;
706         int i;
707
708         dbg ("%s\n", __FUNCTION__);
709
710         serial = (struct usb_serial *) urb->context;
711         for (i = 0; i < serial->num_ports; ++i) {
712                 port = &serial->port[i];
713                 p_priv = (struct keyspan_port_private *)(port->private);
714
715                 if (p_priv->resend_cont) {
716                         dbg (__FUNCTION__ " sending setup\n"); 
717                         keyspan_usa49_send_setup(serial, port, 0);
718                         break;
719                 }
720         }
721 }
722
723         /* This is actually called glostat in the Keyspan
724            doco */
725 static void     usa49_instat_callback(struct urb *urb)
726 {
727         int                                     err;
728         unsigned char                           *data = urb->transfer_buffer;
729         keyspan_usa49_portStatusMessage         *msg;
730         struct usb_serial                       *serial;
731         struct usb_serial_port                  *port;
732         struct keyspan_port_private             *p_priv;
733         int old_dcd_state;
734
735         dbg ("%s\n", __FUNCTION__);
736
737         serial = (struct usb_serial *) urb->context;
738
739         if (urb->status) {
740                 dbg(__FUNCTION__ " nonzero status: %x\n", urb->status);
741                 return;
742         }
743
744         if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
745                 dbg(__FUNCTION__ " bad length %d\n", urb->actual_length);
746                 goto exit;
747         }
748
749         /*dbg(__FUNCTION__ " %x %x %x %x %x %x %x %x %x %x %x\n",
750             data[0], data[1], data[2], data[3], data[4], data[5],
751             data[6], data[7], data[8], data[9], data[10]);*/
752         
753                 /* Now do something useful with the data */
754         msg = (keyspan_usa49_portStatusMessage *)data;
755
756                 /* Check port number from message and retrieve private data */  
757         if (msg->portNumber >= serial->num_ports) {
758                 dbg ("Unexpected port number %d\n", msg->portNumber);
759                 goto exit;
760         }
761         port = &serial->port[msg->portNumber];
762         p_priv = (struct keyspan_port_private *)(port->private);
763         
764         /* Update handshaking pin state information */
765         old_dcd_state = p_priv->dcd_state;
766         p_priv->cts_state = ((msg->cts) ? 1 : 0);
767         p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
768         p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
769         p_priv->ri_state = ((msg->ri) ? 1 : 0);
770
771         if (port->tty && !C_CLOCAL(port->tty)
772             && old_dcd_state != p_priv->dcd_state) {
773                 if (old_dcd_state)
774                         tty_hangup(port->tty);
775                 /*  else */
776                 /*      wake_up_interruptible(&p_priv->open_wait); */
777         }
778
779 exit:   
780                 /* Resubmit urb so we continue receiving */
781         urb->dev = serial->dev;
782
783         if ((err = usb_submit_urb(urb)) != 0) {
784                 dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n", err);
785         }
786 }
787
788 static void     usa49_inack_callback(struct urb *urb)
789 {
790         dbg ("%s\n", __FUNCTION__);
791 }
792
793 static void     usa49_indat_callback(struct urb *urb)
794 {
795         int                     i, err;
796         int                     endpoint;
797         struct usb_serial_port  *port;
798         struct tty_struct       *tty;
799         unsigned char           *data = urb->transfer_buffer;
800
801         dbg ("%s\n", __FUNCTION__);
802
803         endpoint = usb_pipeendpoint(urb->pipe);
804
805         if (urb->status) {
806                 dbg(__FUNCTION__ "nonzero status: %x on endpoint %d.\n",
807                                         urb->status, endpoint);
808                 return;
809         }
810
811         port = (struct usb_serial_port *) urb->context;
812         tty = port->tty;
813         if (urb->actual_length) {
814                 if (data[0] == 0) {
815                         /* no error on any byte */
816                         for (i = 1; i < urb->actual_length ; ++i) {
817                                 tty_insert_flip_char(tty, data[i], 0);
818                         }
819                 } else {
820                         /* some bytes had errors, every byte has status */
821                         for (i = 0; i + 1 < urb->actual_length; i += 2) {
822                                 int stat = data[i], flag = 0;
823                                 if (stat & RXERROR_OVERRUN)
824                                         flag |= TTY_OVERRUN;
825                                 if (stat & RXERROR_FRAMING)
826                                         flag |= TTY_FRAME;
827                                 if (stat & RXERROR_PARITY)
828                                         flag |= TTY_PARITY;
829                                 /* XXX should handle break (0x10) */
830                                 tty_insert_flip_char(tty, data[i+1], flag);
831                         }
832                 }
833                 tty_flip_buffer_push(tty);
834         }
835                                 
836                 /* Resubmit urb so we continue receiving */
837         urb->dev = port->serial->dev;
838         if ((err = usb_submit_urb(urb)) != 0) {
839                 dbg(__FUNCTION__ "resubmit read urb failed. (%d)\n", err);
840         }
841 }
842
843 /* not used, usa-49 doesn't have per-port control endpoints */
844 static void     usa49_outcont_callback(struct urb *urb)
845 {
846         dbg ("%s\n", __FUNCTION__);
847 }
848
849
850
851 static int keyspan_write_room (struct usb_serial_port *port)
852 {
853         dbg("keyspan_write_room called\n");
854         return (32);
855
856 }
857
858
859 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
860 {
861         return (0);
862 }
863
864
865 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
866 {
867         struct keyspan_port_private     *p_priv;
868         struct keyspan_serial_private   *s_priv;
869         struct usb_serial               *serial = port->serial;
870         const keyspan_device_details    *d_details;
871         int                             i, already_active, err;
872         urb_t *urb;
873
874         s_priv = (struct keyspan_serial_private *)(serial->private);
875         p_priv = (struct keyspan_port_private *)(port->private);
876         d_details = s_priv->device_details;
877         
878         dbg("keyspan_open called for port%d.\n", port->number); 
879
880         MOD_INC_USE_COUNT;
881
882         down (&port->sem);
883         already_active = port->open_count;
884         ++port->open_count;
885         up (&port->sem);
886
887         if (already_active)
888                 return 0;
889
890         p_priv = (struct keyspan_port_private *)(port->private);
891         
892         /* Set some sane defaults */
893         p_priv->rts_state = 1;
894         p_priv->dtr_state = 1;
895
896         /* Start reading from endpoints */
897         for (i = 0; i < 2; i++) {
898                 if ((urb = p_priv->in_urbs[i]) == NULL)
899                         continue;
900                 urb->dev = serial->dev;
901                 if ((err = usb_submit_urb(urb)) != 0) {
902                         dbg(__FUNCTION__ " submit urb %d failed (%d)\n", i, err);
903                 }
904         }
905
906         keyspan_set_termios(port, NULL);
907
908         return (0);
909 }
910
911 static inline void stop_urb(urb_t *urb)
912 {
913         if (urb && urb->status == -EINPROGRESS) {
914                 urb->transfer_flags &= ~USB_ASYNC_UNLINK;
915                 usb_unlink_urb(urb);
916         }
917 }
918
919 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
920 {
921         int                     i;
922         struct usb_serial       *serial;
923         struct keyspan_serial_private   *s_priv;
924         struct keyspan_port_private     *p_priv;
925
926         serial = get_usb_serial (port, __FUNCTION__);
927         if (!serial)
928                 return;
929
930         dbg("keyspan_close called\n");
931         s_priv = (struct keyspan_serial_private *)(serial->private);
932         p_priv = (struct keyspan_port_private *)(port->private);
933         
934         p_priv->rts_state = 0;
935         p_priv->dtr_state = 0;
936         
937         if (serial->dev)
938                 keyspan_send_setup(port, 1);
939
940         /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
941                 dbg("close - urb in progress\n");
942         }*/
943
944         p_priv->out_flip = 0;
945         p_priv->in_flip = 0;
946
947         down (&port->sem);
948
949         if (--port->open_count <= 0) {
950                 if (serial->dev) {
951                         /* Stop reading/writing urbs */
952                         stop_urb(p_priv->inack_urb);
953                         stop_urb(p_priv->outcont_urb);
954                         for (i = 0; i < 2; i++) {
955                                 stop_urb(p_priv->in_urbs[i]);
956                                 stop_urb(p_priv->out_urbs[i]);
957                         }
958                 }
959                 port->open_count = 0;
960                 port->tty = 0;
961         }
962         up (&port->sem);
963
964         MOD_DEC_USE_COUNT;
965 }
966
967
968         /* download the firmware to a pre-renumeration device */
969 static int keyspan_fake_startup (struct usb_serial *serial)
970 {
971         int                             response;
972         const struct ezusb_hex_record   *record;
973         char                            *fw_name;
974
975         dbg("Keyspan startup version %04x product %04x\n",
976             serial->dev->descriptor.bcdDevice,
977             serial->dev->descriptor.idProduct); 
978         
979         if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
980                 dbg("Firmware already loaded.  Quitting.\n");
981                 return(1);
982         }
983
984                 /* Select firmware image on the basis of idProduct */
985         switch (serial->dev->descriptor.idProduct) {
986         case keyspan_usa28_pre_product_id:
987                 record = &keyspan_usa28_firmware[0];
988                 fw_name = "USA28";
989                 break;
990
991         case keyspan_usa28x_pre_product_id:
992                 record = &keyspan_usa28x_firmware[0];
993                 fw_name = "USA28X";
994                 break;
995
996         case keyspan_usa28xa_pre_product_id:
997                 record = &keyspan_usa28xa_firmware[0];
998                 fw_name = "USA28XA";
999                 break;
1000
1001         case keyspan_usa28xb_pre_product_id:
1002                 record = &keyspan_usa28xb_firmware[0];
1003                 fw_name = "USA28XB";
1004                 break;
1005
1006         case keyspan_usa19_pre_product_id:
1007                 record = &keyspan_usa19_firmware[0];
1008                 fw_name = "USA19";
1009                 break;
1010                              
1011         case keyspan_usa18x_pre_product_id:
1012                 record = &keyspan_usa18x_firmware[0];
1013                 fw_name = "USA18X";
1014                 break;
1015                              
1016         case keyspan_usa19w_pre_product_id:
1017                 record = &keyspan_usa19w_firmware[0];
1018                 fw_name = "USA19W";
1019                 break;
1020                 
1021         case keyspan_usa49w_pre_product_id:
1022                 record = &keyspan_usa49w_firmware[0];
1023                 fw_name = "USA49W";
1024                 break;
1025
1026         default:
1027                 record = NULL;
1028                 fw_name = "Unknown";
1029                 break;
1030         }
1031
1032         if (record == NULL) {
1033                 err("Required keyspan firmware image (%s) unavailable.", fw_name);
1034                 return(1);
1035         }
1036
1037         dbg("Uploading Keyspan %s firmware.\n", fw_name);
1038
1039                 /* download the firmware image */
1040         response = ezusb_set_reset(serial, 1);
1041
1042         while(record->address != 0xffff) {
1043                 response = ezusb_writememory(serial, record->address,
1044                                              (unsigned char *)record->data,
1045                                              record->data_size, 0xa0);
1046                 if (response < 0) {
1047                         err("ezusb_writememory failed for Keyspan"
1048                             "firmware (%d %04X %p %d)",
1049                             response, 
1050                             record->address, record->data, record->data_size);
1051                         break;
1052                 }
1053                 record++;
1054         }
1055                 /* bring device out of reset. Renumeration will occur in a
1056                    moment and the new device will bind to the real driver */
1057         response = ezusb_set_reset(serial, 0);
1058
1059         /* we don't want this device to have a driver assigned to it. */
1060         return (1);
1061 }
1062
1063 /* Helper functions used by keyspan_setup_urbs */
1064 static urb_t *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1065                                 int dir, void *ctx, char *buf, int len,
1066                                 void (*callback)(urb_t *))
1067 {
1068         urb_t *urb;
1069
1070         if (endpoint == -1)
1071                 return NULL;            /* endpoint not needed */
1072
1073         dbg (__FUNCTION__ " alloc for endpoint %d.\n", endpoint);
1074         urb = usb_alloc_urb(0);         /* No ISO */
1075         if (urb == NULL) {
1076                 dbg (__FUNCTION__ " alloc for endpoint %d failed.\n", endpoint);
1077                 return NULL;
1078         }
1079
1080                 /* Fill URB using supplied data. */
1081         FILL_BULK_URB(urb, serial->dev,
1082                       usb_sndbulkpipe(serial->dev, endpoint) | dir,
1083                       buf, len, callback, ctx);
1084
1085         return urb;
1086 }
1087
1088 static struct callbacks {
1089         void    (*instat_callback)(urb_t *);
1090         void    (*glocont_callback)(urb_t *);
1091         void    (*indat_callback)(urb_t *);
1092         void    (*outdat_callback)(urb_t *);
1093         void    (*inack_callback)(urb_t *);
1094         void    (*outcont_callback)(urb_t *);
1095 } keyspan_callbacks[] = {
1096         {
1097                 /* msg_usa26 callbacks */
1098                 instat_callback: usa26_instat_callback,
1099                 glocont_callback: usa26_glocont_callback,
1100                 indat_callback: usa26_indat_callback,
1101                 outdat_callback: usa2x_outdat_callback,
1102                 inack_callback: usa26_inack_callback,
1103                 outcont_callback: usa26_outcont_callback,
1104         }, {
1105                 /* msg_usa28 callbacks */
1106                 instat_callback: usa28_instat_callback,
1107                 glocont_callback: usa28_glocont_callback,
1108                 indat_callback: usa28_indat_callback,
1109                 outdat_callback: usa2x_outdat_callback,
1110                 inack_callback: usa28_inack_callback,
1111                 outcont_callback: usa28_outcont_callback,
1112         }, {
1113                 /* msg_usa49 callbacks */
1114                 instat_callback: usa49_instat_callback,
1115                 glocont_callback: usa49_glocont_callback,
1116                 indat_callback: usa49_indat_callback,
1117                 outdat_callback: usa2x_outdat_callback,
1118                 inack_callback: usa49_inack_callback,
1119                 outcont_callback: usa49_outcont_callback,
1120         }
1121 };
1122
1123         /* Generic setup urbs function that uses
1124            data in device_details */
1125 static void keyspan_setup_urbs(struct usb_serial *serial)
1126 {
1127         int                             i, j;
1128         struct keyspan_serial_private   *s_priv;
1129         const keyspan_device_details    *d_details;
1130         struct usb_serial_port          *port;
1131         struct keyspan_port_private     *p_priv;
1132         struct callbacks                *cback;
1133         int                             endp;
1134
1135         dbg ("%s\n", __FUNCTION__);
1136
1137         s_priv = (struct keyspan_serial_private *)(serial->private);
1138         d_details = s_priv->device_details;
1139
1140                 /* Setup values for the various callback routines */
1141         cback = &keyspan_callbacks[d_details->msg_format];
1142
1143                 /* Allocate and set up urbs for each one that is in use, 
1144                    starting with instat endpoints */
1145         s_priv->instat_urb = keyspan_setup_urb
1146                 (serial, d_details->instat_endpoint, USB_DIR_IN,
1147                  serial, s_priv->instat_buf, INSTAT_BUFLEN,
1148                  cback->instat_callback);
1149
1150         s_priv->glocont_urb = keyspan_setup_urb
1151                 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1152                  serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1153                  cback->glocont_callback);
1154
1155                 /* Setup endpoints for each port specific thing */
1156         for (i = 0; i < d_details->num_ports; i ++) {
1157                 port = &serial->port[i];
1158                 p_priv = (struct keyspan_port_private *)(port->private);
1159
1160                 /* Do indat endpoints first, once for each flip */
1161                 endp = d_details->indat_endpoints[i];
1162                 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1163                         p_priv->in_urbs[j] = keyspan_setup_urb
1164                                 (serial, endp, USB_DIR_IN, port,
1165                                  p_priv->in_buffer[j], 64,
1166                                  cback->indat_callback);
1167                 }
1168                 for (; j < 2; ++j)
1169                         p_priv->in_urbs[j] = NULL;
1170
1171                 /* outdat endpoints also have flip */
1172                 endp = d_details->outdat_endpoints[i];
1173                 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1174                         p_priv->out_urbs[j] = keyspan_setup_urb
1175                                 (serial, endp, USB_DIR_OUT, port,
1176                                  p_priv->out_buffer[j], 64,
1177                                  cback->outdat_callback);
1178                 }
1179                 for (; j < 2; ++j)
1180                         p_priv->out_urbs[j] = NULL;
1181
1182                 /* inack endpoint */
1183                 p_priv->inack_urb = keyspan_setup_urb
1184                         (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1185                          port, p_priv->inack_buffer, 1, cback->inack_callback);
1186
1187                 /* outcont endpoint */
1188                 p_priv->outcont_urb = keyspan_setup_urb
1189                         (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1190                          port, p_priv->outcont_buffer, 64,
1191                          cback->outcont_callback);
1192         }       
1193
1194 }
1195
1196 /* usa19 function doesn't require prescaler */
1197 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk,
1198                                    u8 *rate_hi, u8 *rate_low, u8 *prescaler)
1199 {
1200         u32     b16,    /* baud rate times 16 (actual rate used internally) */
1201                 div,    /* divisor */   
1202                 cnt;    /* inverse of divisor (programmed into 8051) */
1203                 
1204
1205                 /* prevent divide by zero...  */
1206         if( (b16 = (baud_rate * 16L)) == 0) {
1207                 return (KEYSPAN_INVALID_BAUD_RATE);
1208         }
1209
1210                 /* Any "standard" rate over 57k6 is marginal on the USA-19
1211                    as we run out of divisor resolution. */
1212         if (baud_rate > 57600) {
1213                 return (KEYSPAN_INVALID_BAUD_RATE);
1214         }
1215
1216                 /* calculate the divisor and the counter (its inverse) */
1217         if( (div = (baudclk / b16)) == 0) {
1218                 return (KEYSPAN_INVALID_BAUD_RATE);
1219         }
1220         else {
1221                 cnt = 0 - div;
1222         }
1223
1224         if(div > 0xffff) {
1225                 return (KEYSPAN_INVALID_BAUD_RATE);
1226         }
1227
1228                 /* return the counter values if non-null */
1229         if (rate_low) {
1230                 *rate_low = (u8) (cnt & 0xff);
1231         }
1232         if (rate_hi) {
1233                 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1234         }
1235         if (rate_low && rate_hi) {
1236                 dbg (__FUNCTION__ " %d %02x %02x.", baud_rate, *rate_hi, *rate_low);
1237         }
1238         
1239         return (KEYSPAN_BAUD_RATE_OK);
1240 }
1241
1242 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk,
1243                                     u8 *rate_hi, u8 *rate_low, u8 *prescaler)
1244 {
1245         u32     b16,    /* baud rate times 16 (actual rate used internally) */
1246                 clk,    /* clock with 13/8 prescaler */
1247                 div,    /* divisor using 13/8 prescaler */      
1248                 res,    /* resulting baud rate using 13/8 prescaler */
1249                 diff,   /* error using 13/8 prescaler */
1250                 smallest_diff;
1251         u8      best_prescaler;
1252         int     i;
1253
1254         dbg (__FUNCTION__ " %d.\n", baud_rate);
1255
1256                 /* prevent divide by zero */
1257         if( (b16 = baud_rate * 16L) == 0) {
1258                 return (KEYSPAN_INVALID_BAUD_RATE);
1259         }
1260
1261                 /* Calculate prescaler by trying them all and looking
1262                    for best fit */
1263                 
1264                 /* start with largest possible difference */
1265         smallest_diff = 0xffffffff;
1266
1267                 /* 0 is an invalid prescaler, used as a flag */
1268         best_prescaler = 0;
1269
1270         for(i = 8; i <= 0xff; ++i)
1271         {
1272                 clk = (baudclk * 8) / (u32) i;
1273                 
1274                 if( (div = clk / b16) == 0) {
1275                         continue;
1276                 }
1277
1278                 res = clk / div;
1279                 diff= (res > b16) ? (res-b16) : (b16-res);
1280
1281                 if(diff < smallest_diff)
1282                 {
1283                         best_prescaler = i;
1284                         smallest_diff = diff;
1285                 }
1286         }
1287
1288         if(best_prescaler == 0) {
1289                 return (KEYSPAN_INVALID_BAUD_RATE);
1290         }
1291
1292         clk = (baudclk * 8) / (u32) best_prescaler;
1293         div = clk / b16;
1294
1295                 /* return the divisor and prescaler if non-null */
1296         if (rate_low) {
1297                 *rate_low = (u8) (div & 0xff);
1298         }
1299         if (rate_hi) {
1300                 *rate_hi = (u8) ((div >> 8) & 0xff);
1301         }
1302         if (prescaler) {
1303                 *prescaler = best_prescaler;
1304                 /*  dbg(__FUNCTION__ " %d %d", *prescaler, div); */
1305         }
1306         return (KEYSPAN_BAUD_RATE_OK);
1307 }
1308
1309 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1310                                     struct usb_serial_port *port,
1311                                     int reset_port)
1312 {
1313         struct keyspan_usa26_portControlMessage msg;            
1314         struct keyspan_serial_private           *s_priv;
1315         struct keyspan_port_private             *p_priv;
1316         const  keyspan_device_details           *d_details;
1317         int                                     outcont_urb;
1318         urb_t *this_urb;
1319         int err;
1320
1321         dbg ("%s reset=%d\n", __FUNCTION__, reset_port); 
1322
1323         s_priv = (struct keyspan_serial_private *)(serial->private);
1324         p_priv = (struct keyspan_port_private *)(port->private);
1325         d_details = s_priv->device_details;
1326
1327         outcont_urb = d_details->outcont_endpoints[port->number];
1328         this_urb = p_priv->outcont_urb;
1329
1330         dbg(__FUNCTION__ " endpoint %d\n", usb_pipeendpoint(this_urb->pipe));
1331
1332                 /* Make sure we have an urb then send the message */
1333         if (this_urb == NULL) {
1334                 dbg(__FUNCTION__ " oops no urb.\n");
1335                 return -1;
1336         }
1337
1338         p_priv->resend_cont = 1;
1339         if (this_urb->status == -EINPROGRESS) {
1340                 /*  dbg (__FUNCTION__ " already writing"); */
1341                 return(-1);
1342         }
1343
1344         memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1345         
1346                 /* Only set baud rate if it's changed */        
1347         if (p_priv->old_baud != p_priv->baud) {
1348                 p_priv->old_baud = p_priv->baud;
1349                 msg.setClocking = 0xff;
1350                 if (d_details->calculate_baud_rate
1351                     (p_priv->baud, d_details->baudclk, &msg.baudHi,
1352                      &msg.baudLo, &msg.prescaler) == KEYSPAN_INVALID_BAUD_RATE ) {
1353                         dbg(__FUNCTION__ "Invalid baud rate %d requested, using 9600.\n",
1354                             p_priv->baud);
1355                         msg.baudLo = 0;
1356                         msg.baudHi = 125;       /* Values for 9600 baud */
1357                         msg.prescaler = 10;
1358                 }
1359                 msg.setPrescaler = 0xff;
1360         }
1361
1362         msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1363         switch (p_priv->cflag & CSIZE) {
1364         case CS5:
1365                 msg.lcr |= USA_DATABITS_5;
1366                 break;
1367         case CS6:
1368                 msg.lcr |= USA_DATABITS_6;
1369                 break;
1370         case CS7:
1371                 msg.lcr |= USA_DATABITS_7;
1372                 break;
1373         case CS8:
1374                 msg.lcr |= USA_DATABITS_8;
1375                 break;
1376         }
1377         if (p_priv->cflag & PARENB) {
1378                 /* note USA_PARITY_NONE == 0 */
1379                 msg.lcr |= (p_priv->cflag & PARODD)?
1380                         USA_PARITY_ODD: USA_PARITY_EVEN;
1381         }
1382         msg.setLcr = 0xff;
1383
1384         msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1385         msg.xonFlowControl = 0;
1386         msg.setFlowControl = 0xff;
1387         
1388         msg.forwardingLength = 1;
1389         msg.xonChar = 17;
1390         msg.xoffChar = 19;
1391
1392         if (reset_port) {
1393                 msg._txOn = 0;
1394                 msg._txOff = 1;
1395                 msg.txFlush = 0;
1396                 msg.txBreak = 0;
1397                 msg.rxOn = 0;
1398                 msg.rxOff = 1;
1399                 msg.rxFlush = 1;
1400                 msg.rxForward = 0;
1401                 msg.returnStatus = 0;
1402                 msg.resetDataToggle = 0xff;
1403         }
1404         else {
1405                 msg._txOn = (! p_priv->break_on);
1406                 msg._txOff = 0;
1407                 msg.txFlush = 0;
1408                 msg.txBreak = (p_priv->break_on);
1409                 msg.rxOn = 1;
1410                 msg.rxOff = 0;
1411                 msg.rxFlush = 0;
1412                 msg.rxForward = 0;
1413                 msg.returnStatus = 0;
1414                 msg.resetDataToggle = 0x0;
1415         }
1416
1417                 /* Do handshaking outputs */    
1418         msg.setTxTriState_setRts = 0xff;
1419         msg.txTriState_rts = p_priv->rts_state;
1420
1421         msg.setHskoa_setDtr = 0xff;
1422         msg.hskoa_dtr = p_priv->dtr_state;
1423                 
1424         p_priv->resend_cont = 0;
1425         memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1426         
1427         /* send the data out the device on control endpoint */
1428         this_urb->transfer_buffer_length = sizeof(msg);
1429
1430         this_urb->dev = serial->dev;
1431         if ((err = usb_submit_urb(this_urb)) != 0) {
1432                 dbg(__FUNCTION__ " usb_submit_urb(setup) failed (%d)\n", err);
1433         }
1434 #if 0
1435         else {
1436                 dbg(__FUNCTION__ " usb_submit_urb(%d) OK %d bytes (end %d)",
1437                     outcont_urb, this_urb->transfer_buffer_length,
1438                     usb_pipeendpoint(this_urb->pipe));
1439         }
1440 #endif
1441
1442         return (0);
1443 }
1444
1445 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1446                                     struct usb_serial_port *port,
1447                                     int reset_port)
1448 {
1449         struct keyspan_usa28_portControlMessage msg;            
1450         struct keyspan_serial_private           *s_priv;
1451         struct keyspan_port_private             *p_priv;
1452         const  keyspan_device_details           *d_details;
1453         urb_t *this_urb;
1454         int err;
1455
1456         s_priv = (struct keyspan_serial_private *)(serial->private);
1457         p_priv = (struct keyspan_port_private *)(port->private);
1458         d_details = s_priv->device_details;
1459
1460         /* only do something if we have a bulk out endpoint */
1461         if ((this_urb = p_priv->outcont_urb) == NULL) {
1462                 dbg(__FUNCTION__ " oops no urb.\n");
1463                 return -1;
1464         }
1465
1466         p_priv->resend_cont = 1;
1467         if (this_urb->status == -EINPROGRESS) {
1468                 dbg (__FUNCTION__ " already writing\n");
1469                 return(-1);
1470         }
1471
1472         memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1473
1474         msg.setBaudRate = 1;
1475         if (keyspan_usa19_calc_baud(p_priv->baud, d_details->baudclk,
1476                 &msg.baudHi, &msg.baudLo, NULL) == KEYSPAN_INVALID_BAUD_RATE ) {
1477                 dbg(__FUNCTION__ "Invalid baud rate requested %d.", p_priv->baud);
1478                 msg.baudLo = 0xff;
1479                 msg.baudHi = 0xb2;      /* Values for 9600 baud */
1480         }
1481
1482         /* If parity is enabled, we must calculate it ourselves. */
1483         msg.parity = 0;         /* XXX for now */
1484
1485         msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1486         msg.xonFlowControl = 0;
1487
1488         /* Do handshaking outputs, DTR is inverted relative to RTS */   
1489         msg.rts = p_priv->rts_state;
1490         msg.dtr = p_priv->dtr_state;
1491
1492         msg.forwardingLength = 1;
1493         msg.forwardMs = 10;
1494         msg.breakThreshold = 45;
1495         msg.xonChar = 17;
1496         msg.xoffChar = 19;
1497
1498         msg._txOn = 1;
1499         msg._txOff = 0;
1500         msg.txFlush = 0;
1501         msg.txForceXoff = 0;
1502         msg.txBreak = 0;
1503         msg.rxOn = 1;
1504         msg.rxOff = 0;
1505         msg.rxFlush = 0;
1506         msg.rxForward = 0;
1507         /*msg.returnStatus = 1;
1508         msg.resetDataToggle = 0xff;*/
1509
1510         p_priv->resend_cont = 0;
1511         memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1512
1513         /* send the data out the device on control endpoint */
1514         this_urb->transfer_buffer_length = sizeof(msg);
1515
1516         this_urb->dev = serial->dev;
1517         if ((err = usb_submit_urb(this_urb)) != 0) {
1518                 dbg(__FUNCTION__ " usb_submit_urb(setup) failed\n");
1519         }
1520 #if 0
1521         else {
1522                 dbg(__FUNCTION__ " usb_submit_urb(setup) OK %d bytes",
1523                     this_urb->transfer_buffer_length);
1524         }
1525 #endif
1526
1527         return (0);
1528 }
1529
1530 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1531                                     struct usb_serial_port *port,
1532                                     int reset_port)
1533 {
1534         struct keyspan_usa49_portControlMessage msg;            
1535         struct keyspan_serial_private           *s_priv;
1536         struct keyspan_port_private             *p_priv;
1537         const  keyspan_device_details           *d_details;
1538         int                                     glocont_urb;
1539         urb_t                                   *this_urb;
1540         int                                     err;
1541         int                                     device_port;
1542
1543         dbg ("%s\n", __FUNCTION__);
1544
1545         s_priv = (struct keyspan_serial_private *)(serial->private);
1546         p_priv = (struct keyspan_port_private *)(port->private);
1547         d_details = s_priv->device_details;
1548
1549         glocont_urb = d_details->glocont_endpoint;
1550         this_urb = s_priv->glocont_urb;
1551
1552                 /* Work out which port within the device is being setup */
1553         device_port = port->number - port->serial->minor;
1554
1555         dbg(__FUNCTION__ " endpoint %d port %d (%d)\n", usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1556
1557                 /* Make sure we have an urb then send the message */
1558         if (this_urb == NULL) {
1559                 dbg(__FUNCTION__ " oops no urb for port %d.\n", port->number);
1560                 return -1;
1561         }
1562
1563         p_priv->resend_cont = 1;
1564         if (this_urb->status == -EINPROGRESS) {
1565                 /*  dbg (__FUNCTION__ " already writing"); */
1566                 return(-1);
1567         }
1568
1569         memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1570
1571         /*msg.portNumber = port->number;*/
1572         msg.portNumber = device_port;
1573         
1574                 /* Only set baud rate if it's changed */        
1575         if (p_priv->old_baud != p_priv->baud) {
1576                 p_priv->old_baud = p_priv->baud;
1577                 msg.setClocking = 0xff;
1578                 if (d_details->calculate_baud_rate
1579                     (p_priv->baud, d_details->baudclk, &msg.baudHi,
1580                      &msg.baudLo, &msg.prescaler) == KEYSPAN_INVALID_BAUD_RATE ) {
1581                         dbg(__FUNCTION__ "Invalid baud rate %d requested, using 9600.\n",
1582                             p_priv->baud);
1583                         msg.baudLo = 0;
1584                         msg.baudHi = 125;       /* Values for 9600 baud */
1585                         msg.prescaler = 10;
1586                 }
1587                 //msg.setPrescaler = 0xff;
1588         }
1589
1590         msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1591         switch (p_priv->cflag & CSIZE) {
1592         case CS5:
1593                 msg.lcr |= USA_DATABITS_5;
1594                 break;
1595         case CS6:
1596                 msg.lcr |= USA_DATABITS_6;
1597                 break;
1598         case CS7:
1599                 msg.lcr |= USA_DATABITS_7;
1600                 break;
1601         case CS8:
1602                 msg.lcr |= USA_DATABITS_8;
1603                 break;
1604         }
1605         if (p_priv->cflag & PARENB) {
1606                 /* note USA_PARITY_NONE == 0 */
1607                 msg.lcr |= (p_priv->cflag & PARODD)?
1608                         USA_PARITY_ODD: USA_PARITY_EVEN;
1609         }
1610         msg.setLcr = 0xff;
1611
1612         msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1613         msg.xonFlowControl = 0;
1614         msg.setFlowControl = 0xff;
1615         
1616         msg.forwardingLength = 1;
1617         msg.xonChar = 17;
1618         msg.xoffChar = 19;
1619         
1620         msg._txOn = 1;
1621         msg._txOff = 0;
1622         msg.txFlush = 0;
1623         msg.txBreak = 0;
1624         msg.rxOn = 1;
1625         msg.rxOff = 0;
1626         msg.rxFlush = 0;
1627         msg.rxForward = 0;
1628         msg.enablePort = 0xff;
1629         msg.disablePort = 0;
1630
1631                 /* Do handshaking outputs */    
1632         msg.setRts = 0xff;
1633         msg.rts = p_priv->rts_state;
1634
1635         msg.setDtr = 0xff;
1636         msg.dtr = p_priv->dtr_state;
1637                 
1638         p_priv->resend_cont = 0;
1639         memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1640         
1641         /* send the data out the device on control endpoint */
1642         this_urb->transfer_buffer_length = sizeof(msg);
1643
1644         this_urb->dev = serial->dev;
1645         if ((err = usb_submit_urb(this_urb)) != 0) {
1646                 dbg(__FUNCTION__ " usb_submit_urb(setup) failed (%d)\n", err);
1647         }
1648 #if 0
1649         else {
1650                 dbg(__FUNCTION__ " usb_submit_urb(%d) OK %d bytes (end %d)",
1651                     outcont_urb, this_urb->transfer_buffer_length,
1652                     usb_pipeendpoint(this_urb->pipe));
1653         }
1654 #endif
1655
1656         return (0);
1657 }
1658
1659 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
1660 {
1661         struct usb_serial *serial = port->serial;
1662         struct keyspan_serial_private   *s_priv;
1663         const keyspan_device_details    *d_details;
1664
1665         s_priv = (struct keyspan_serial_private *)(serial->private);
1666         d_details = s_priv->device_details;
1667
1668         switch (d_details->msg_format) {
1669         case msg_usa26:
1670                 keyspan_usa26_send_setup(serial, port, reset_port);
1671                 break;
1672         case msg_usa28:
1673                 keyspan_usa28_send_setup(serial, port, reset_port);
1674                 break;
1675         case msg_usa49:
1676                 keyspan_usa49_send_setup(serial, port, reset_port);
1677                 break;
1678         }
1679 }
1680
1681 /* Gets called by the "real" driver (ie once firmware is loaded
1682    and renumeration has taken place. */
1683 static int keyspan_startup (struct usb_serial *serial)
1684 {
1685         int                             i, err;
1686         struct usb_serial_port          *port;
1687         struct keyspan_serial_private   *s_priv;
1688         struct keyspan_port_private     *p_priv;
1689         const keyspan_device_details    *d_details;
1690
1691         dbg("keyspan_startup called.\n");
1692
1693         for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
1694                 if (d_details->product_id == serial->dev->descriptor.idProduct)
1695                         break;
1696         if (d_details == NULL) {
1697                 printk(KERN_ERR __FUNCTION__ ": unknown product id %x\n",
1698                        serial->dev->descriptor.idProduct);
1699                 return 1;
1700         }
1701
1702         /* Setup private data for serial driver */
1703         serial->private = kmalloc(sizeof(struct keyspan_serial_private),
1704                                   GFP_KERNEL);
1705         if (!serial->private) {
1706                 dbg(__FUNCTION__ "kmalloc for keyspan_serial_private failed.\n");
1707                 return (1);
1708         }
1709         memset(serial->private, 0, sizeof(struct keyspan_serial_private));
1710
1711         s_priv = (struct keyspan_serial_private *)(serial->private);
1712         s_priv->device_details = d_details;
1713                 
1714         /* Now setup per port private data */
1715         for (i = 0; i < serial->num_ports; i++) {
1716                 port = &serial->port[i];
1717                 port->private = kmalloc(sizeof(struct keyspan_port_private),
1718                                         GFP_KERNEL);
1719                 if (!port->private) {
1720                         dbg(__FUNCTION__ "kmalloc for keyspan_port_private (%d) failed!.\n", i);
1721                         return (1);
1722                 }
1723                 memset(port->private, 0, sizeof(struct keyspan_port_private));
1724                 p_priv = (struct keyspan_port_private *)(port->private);
1725                 p_priv->device_details = d_details;
1726         }
1727
1728         keyspan_setup_urbs(serial);
1729
1730         s_priv->instat_urb->dev = serial->dev;
1731         if ((err = usb_submit_urb(s_priv->instat_urb)) != 0) {
1732                 dbg(__FUNCTION__ " submit instat urb failed %d\n", err);
1733         }
1734                         
1735         return (0);
1736 }
1737
1738 static void keyspan_shutdown (struct usb_serial *serial)
1739 {
1740         int                             i, j;
1741         struct usb_serial_port          *port;
1742         struct keyspan_serial_private   *s_priv;
1743         struct keyspan_port_private     *p_priv;
1744
1745         dbg("keyspan_shutdown called\n");
1746
1747         s_priv = (struct keyspan_serial_private *)(serial->private);
1748
1749         /* Stop reading/writing urbs */
1750         stop_urb(s_priv->instat_urb);
1751         stop_urb(s_priv->glocont_urb);
1752         for (i = 0; i < serial->num_ports; ++i) {
1753                 port = &serial->port[i];
1754                 p_priv = (struct keyspan_port_private *)(port->private);
1755                 stop_urb(p_priv->inack_urb);
1756                 stop_urb(p_priv->outcont_urb);
1757                 for (j = 0; j < 2; j++) {
1758                         stop_urb(p_priv->in_urbs[j]);
1759                         stop_urb(p_priv->out_urbs[j]);
1760                 }
1761         }
1762
1763         /* Now free them */
1764         if (s_priv->instat_urb)
1765                 usb_free_urb(s_priv->instat_urb);
1766         if (s_priv->glocont_urb)
1767                 usb_free_urb(s_priv->glocont_urb);
1768         for (i = 0; i < serial->num_ports; ++i) {
1769                 port = &serial->port[i];
1770                 p_priv = (struct keyspan_port_private *)(port->private);
1771                 if (p_priv->inack_urb)
1772                         usb_free_urb(p_priv->inack_urb);
1773                 if (p_priv->outcont_urb)
1774                         usb_free_urb(p_priv->outcont_urb);
1775                 for (j = 0; j < 2; j++) {
1776                         if (p_priv->in_urbs[j])
1777                                 usb_free_urb(p_priv->in_urbs[j]);
1778                         if (p_priv->out_urbs[j])
1779                                 usb_free_urb(p_priv->out_urbs[j]);
1780                 }
1781         }
1782
1783         /*  dbg("Freeing serial->private."); */
1784         kfree(serial->private);
1785
1786         /*  dbg("Freeing port->private."); */
1787         /* Now free per port private data */
1788         for (i = 0; i < serial->num_ports; i++) {
1789                 port = &serial->port[i];
1790                 while (port->open_count > 0) {
1791                         --port->open_count;
1792                         MOD_DEC_USE_COUNT;
1793                 }
1794                 kfree(port->private);
1795         }
1796 }
1797
1798 MODULE_AUTHOR( DRIVER_AUTHOR );
1799 MODULE_DESCRIPTION( DRIVER_DESC );
1800 MODULE_LICENSE("GPL");
1801
1802 MODULE_PARM(debug, "i");
1803 MODULE_PARM_DESC(debug, "Debug enabled or not");
1804