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