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