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