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