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