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