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