2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
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.
12 See http://misc.nu/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
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.
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
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
31 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
32 Improve setup message handling for all devices.
34 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36 Linux source tree. The Linux tree lacked support for the 49WLC and
37 others. The Keyspan patches didn't work with the current kernel.
39 2003jan30 LPM add support for the 49WLC and MPR
41 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
43 now supported (including QI and QW). Modified port open, port
44 close, and send setup() logic to fix various data and endpoint
45 synchronization bugs and device LED status bugs. Changed keyspan_
46 write_room() to accurately return transmit buffer availability.
47 Changed forwardingLength from 1 to 16 for all adapters.
49 Fri Oct 12 16:45:00 EST 2001
50 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
52 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
54 now supported (including QI and QW). Modified port open, port
55 close, and send setup() logic to fix various data and endpoint
56 synchronization bugs and device LED status bugs. Changed keyspan_
57 write_room() to accurately return transmit buffer availability.
58 Changed forwardingLength from 1 to 16 for all adapters.
60 Fri Oct 12 16:45:00 EST 2001
61 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
63 Mon Oct 8 14:29:00 EST 2001 hugh
64 Fixed bug that prevented mulitport devices operating correctly
65 if they weren't the first unit attached.
67 Sat Oct 6 12:31:21 EST 2001 hugh
68 Added support for USA-28XA and -28XB, misc cleanups, break support
69 for usa26 based models thanks to David Gibson.
71 Thu May 31 11:56:42 PDT 2001 gkh
72 switched from using spinlock to a semaphore
75 Identify version on module load.
77 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
80 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
84 Wed Jul 19 14:00:42 EST 2000 gkh
85 Added module_init and module_exit functions to handle the fact that
86 this driver is a loadable module now.
88 Tue Jul 18 16:14:52 EST 2000 Hugh
89 Basic character input/output for USA-19 now mostly works,
90 fixed at 9600 baud for the moment.
92 Sat Jul 8 11:11:48 EST 2000 Hugh
93 First public release - nothing works except the firmware upload.
94 Tested on PPC and x86 architectures, seems to behave...
98 #include <linux/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <linux/firmware.h>
109 #include <linux/ihex.h>
110 #include <linux/uaccess.h>
111 #include <linux/usb.h>
112 #include <linux/usb/serial.h>
118 * Version Information
120 #define DRIVER_VERSION "v1.1.5"
121 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
122 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
124 #define INSTAT_BUFLEN 32
125 #define GLOCONT_BUFLEN 64
126 #define INDAT49W_BUFLEN 512
128 /* Per device and per port private data */
129 struct keyspan_serial_private {
130 const struct keyspan_device_details *device_details;
132 struct urb *instat_urb;
133 char instat_buf[INSTAT_BUFLEN];
135 /* added to support 49wg, where data from all 4 ports comes in
136 on 1 EP and high-speed supported */
137 struct urb *indat_urb;
138 char indat_buf[INDAT49W_BUFLEN];
140 /* XXX this one probably will need a lock */
141 struct urb *glocont_urb;
142 char glocont_buf[GLOCONT_BUFLEN];
143 char ctrl_buf[8]; /* for EP0 control message */
146 struct keyspan_port_private {
147 /* Keep track of which input & output endpoints to use */
151 /* Keep duplicate of device details in each port
152 structure as well - simplifies some of the
153 callback functions etc. */
154 const struct keyspan_device_details *device_details;
156 /* Input endpoints and buffer for this port */
157 struct urb *in_urbs[2];
158 char in_buffer[2][64];
159 /* Output endpoints and buffer for this port */
160 struct urb *out_urbs[2];
161 char out_buffer[2][64];
163 /* Input ack endpoint */
164 struct urb *inack_urb;
165 char inack_buffer[1];
167 /* Output control endpoint */
168 struct urb *outcont_urb;
169 char outcont_buffer[64];
171 /* Settings for the port */
175 unsigned int old_cflag;
176 enum {flow_none, flow_cts, flow_xon} flow_control;
177 int rts_state; /* Handshaking pins (outputs) */
179 int cts_state; /* Handshaking pins (inputs) */
185 unsigned long tx_start_time[2];
186 int resend_cont; /* need to resend control packet */
189 /* Include Keyspan message headers. All current Keyspan Adapters
190 make use of one of five message formats which are referred
191 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
192 within this driver. */
193 #include "keyspan_usa26msg.h"
194 #include "keyspan_usa28msg.h"
195 #include "keyspan_usa49msg.h"
196 #include "keyspan_usa90msg.h"
197 #include "keyspan_usa67msg.h"
200 /* Functions used by new usb-serial code. */
201 static int __init keyspan_init(void)
204 retval = usb_serial_register(&keyspan_pre_device);
206 goto failed_pre_device_register;
207 retval = usb_serial_register(&keyspan_1port_device);
209 goto failed_1port_device_register;
210 retval = usb_serial_register(&keyspan_2port_device);
212 goto failed_2port_device_register;
213 retval = usb_serial_register(&keyspan_4port_device);
215 goto failed_4port_device_register;
216 retval = usb_register(&keyspan_driver);
218 goto failed_usb_register;
220 info(DRIVER_VERSION ":" DRIVER_DESC);
224 usb_serial_deregister(&keyspan_4port_device);
225 failed_4port_device_register:
226 usb_serial_deregister(&keyspan_2port_device);
227 failed_2port_device_register:
228 usb_serial_deregister(&keyspan_1port_device);
229 failed_1port_device_register:
230 usb_serial_deregister(&keyspan_pre_device);
231 failed_pre_device_register:
235 static void __exit keyspan_exit(void)
237 usb_deregister(&keyspan_driver);
238 usb_serial_deregister(&keyspan_pre_device);
239 usb_serial_deregister(&keyspan_1port_device);
240 usb_serial_deregister(&keyspan_2port_device);
241 usb_serial_deregister(&keyspan_4port_device);
244 module_init(keyspan_init);
245 module_exit(keyspan_exit);
247 static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
249 struct usb_serial_port *port = tty->driver_data;
250 struct keyspan_port_private *p_priv;
254 p_priv = usb_get_serial_port_data(port);
256 if (break_state == -1)
257 p_priv->break_on = 1;
259 p_priv->break_on = 0;
261 keyspan_send_setup(port, 0);
265 static void keyspan_set_termios(struct tty_struct *tty,
266 struct usb_serial_port *port, struct ktermios *old_termios)
268 int baud_rate, device_port;
269 struct keyspan_port_private *p_priv;
270 const struct keyspan_device_details *d_details;
275 p_priv = usb_get_serial_port_data(port);
276 d_details = p_priv->device_details;
277 cflag = tty->termios->c_cflag;
278 device_port = port->number - port->serial->minor;
280 /* Baud rate calculation takes baud rate as an integer
281 so other rates can be generated if desired. */
282 baud_rate = tty_get_baud_rate(tty);
283 /* If no match or invalid, don't change */
284 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
285 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
286 /* FIXME - more to do here to ensure rate changes cleanly */
287 /* FIXME - calcuate exact rate from divisor ? */
288 p_priv->baud = baud_rate;
290 baud_rate = tty_termios_baud_rate(old_termios);
292 tty_encode_baud_rate(tty, baud_rate, baud_rate);
293 /* set CTS/RTS handshake etc. */
294 p_priv->cflag = cflag;
295 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
297 /* Mark/Space not supported */
298 tty->termios->c_cflag &= ~CMSPAR;
300 keyspan_send_setup(port, 0);
303 static int keyspan_tiocmget(struct tty_struct *tty, struct file *file)
305 struct usb_serial_port *port = tty->driver_data;
306 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
309 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
310 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
311 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
312 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
313 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
314 ((p_priv->ri_state) ? TIOCM_RNG : 0);
319 static int keyspan_tiocmset(struct tty_struct *tty, struct file *file,
320 unsigned int set, unsigned int clear)
322 struct usb_serial_port *port = tty->driver_data;
323 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
326 p_priv->rts_state = 1;
328 p_priv->dtr_state = 1;
329 if (clear & TIOCM_RTS)
330 p_priv->rts_state = 0;
331 if (clear & TIOCM_DTR)
332 p_priv->dtr_state = 0;
333 keyspan_send_setup(port, 0);
337 /* Write function is similar for the four protocols used
338 with only a minor change for usa90 (usa19hs) required */
339 static int keyspan_write(struct tty_struct *tty,
340 struct usb_serial_port *port, const unsigned char *buf, int count)
342 struct keyspan_port_private *p_priv;
343 const struct keyspan_device_details *d_details;
346 struct urb *this_urb;
347 int err, maxDataLen, dataOffset;
349 p_priv = usb_get_serial_port_data(port);
350 d_details = p_priv->device_details;
352 if (d_details->msg_format == msg_usa90) {
360 dbg("%s - for port %d (%d chars), flip=%d",
361 __func__, port->number, count, p_priv->out_flip);
363 for (left = count; left > 0; left -= todo) {
365 if (todo > maxDataLen)
368 flip = p_priv->out_flip;
370 /* Check we have a valid urb/endpoint before we use it... */
371 this_urb = p_priv->out_urbs[flip];
372 if (this_urb == NULL) {
373 /* no bulk out, so return 0 bytes written */
374 dbg("%s - no output urb :(", __func__);
378 dbg("%s - endpoint %d flip %d",
379 __func__, usb_pipeendpoint(this_urb->pipe), flip);
381 if (this_urb->status == -EINPROGRESS) {
382 if (time_before(jiffies,
383 p_priv->tx_start_time[flip] + 10 * HZ))
385 usb_unlink_urb(this_urb);
389 /* First byte in buffer is "last flag" (except for usa19hx)
390 - unused so for now so set to zero */
391 ((char *)this_urb->transfer_buffer)[0] = 0;
393 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
396 /* send the data out the bulk port */
397 this_urb->transfer_buffer_length = todo + dataOffset;
399 this_urb->dev = port->serial->dev;
400 err = usb_submit_urb(this_urb, GFP_ATOMIC);
402 dbg("usb_submit_urb(write bulk) failed (%d)", err);
403 p_priv->tx_start_time[flip] = jiffies;
405 /* Flip for next time if usa26 or usa28 interface
406 (not used on usa49) */
407 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
413 static void usa26_indat_callback(struct urb *urb)
417 struct usb_serial_port *port;
418 struct tty_struct *tty;
419 unsigned char *data = urb->transfer_buffer;
420 int status = urb->status;
424 endpoint = usb_pipeendpoint(urb->pipe);
427 dbg("%s - nonzero status: %x on endpoint %d.",
428 __func__, status, endpoint);
433 tty = tty_port_tty_get(&port->port);
434 if (tty && urb->actual_length) {
435 /* 0x80 bit is error flag */
436 if ((data[0] & 0x80) == 0) {
437 /* no errors on individual bytes, only
438 possible overrun err */
439 if (data[0] & RXERROR_OVERRUN)
443 for (i = 1; i < urb->actual_length ; ++i)
444 tty_insert_flip_char(tty, data[i], err);
446 /* some bytes had errors, every byte has status */
447 dbg("%s - RX error!!!!", __func__);
448 for (i = 0; i + 1 < urb->actual_length; i += 2) {
449 int stat = data[i], flag = 0;
450 if (stat & RXERROR_OVERRUN)
452 if (stat & RXERROR_FRAMING)
454 if (stat & RXERROR_PARITY)
456 /* XXX should handle break (0x10) */
457 tty_insert_flip_char(tty, data[i+1], flag);
460 tty_flip_buffer_push(tty);
464 /* Resubmit urb so we continue receiving */
465 urb->dev = port->serial->dev;
466 if (port->port.count) {
467 err = usb_submit_urb(urb, GFP_ATOMIC);
469 dbg("%s - resubmit read urb failed. (%d)",
475 /* Outdat handling is common for all devices */
476 static void usa2x_outdat_callback(struct urb *urb)
478 struct usb_serial_port *port;
479 struct keyspan_port_private *p_priv;
482 p_priv = usb_get_serial_port_data(port);
483 dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
485 if (port->port.count)
486 usb_serial_port_softint(port);
489 static void usa26_inack_callback(struct urb *urb)
495 static void usa26_outcont_callback(struct urb *urb)
497 struct usb_serial_port *port;
498 struct keyspan_port_private *p_priv;
501 p_priv = usb_get_serial_port_data(port);
503 if (p_priv->resend_cont) {
504 dbg("%s - sending setup", __func__);
505 keyspan_usa26_send_setup(port->serial, port,
506 p_priv->resend_cont - 1);
510 static void usa26_instat_callback(struct urb *urb)
512 unsigned char *data = urb->transfer_buffer;
513 struct keyspan_usa26_portStatusMessage *msg;
514 struct usb_serial *serial;
515 struct usb_serial_port *port;
516 struct keyspan_port_private *p_priv;
517 struct tty_struct *tty;
518 int old_dcd_state, err;
519 int status = urb->status;
521 serial = urb->context;
524 dbg("%s - nonzero status: %x", __func__, status);
527 if (urb->actual_length != 9) {
528 dbg("%s - %d byte report??", __func__, urb->actual_length);
532 msg = (struct keyspan_usa26_portStatusMessage *)data;
535 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
536 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
537 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
540 /* Now do something useful with the data */
543 /* Check port number from message and retrieve private data */
544 if (msg->port >= serial->num_ports) {
545 dbg("%s - Unexpected port number %d", __func__, msg->port);
548 port = serial->port[msg->port];
549 p_priv = usb_get_serial_port_data(port);
551 /* Update handshaking pin state information */
552 old_dcd_state = p_priv->dcd_state;
553 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
554 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
555 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
556 p_priv->ri_state = ((msg->ri) ? 1 : 0);
558 if (old_dcd_state != p_priv->dcd_state) {
559 tty = tty_port_tty_get(&port->port);
560 if (tty && !C_CLOCAL(tty))
565 /* Resubmit urb so we continue receiving */
566 urb->dev = serial->dev;
567 err = usb_submit_urb(urb, GFP_ATOMIC);
569 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
573 static void usa26_glocont_callback(struct urb *urb)
579 static void usa28_indat_callback(struct urb *urb)
582 struct usb_serial_port *port;
583 struct tty_struct *tty;
585 struct keyspan_port_private *p_priv;
586 int status = urb->status;
591 p_priv = usb_get_serial_port_data(port);
592 data = urb->transfer_buffer;
594 if (urb != p_priv->in_urbs[p_priv->in_flip])
599 dbg("%s - nonzero status: %x on endpoint %d.",
600 __func__, status, usb_pipeendpoint(urb->pipe));
605 p_priv = usb_get_serial_port_data(port);
606 data = urb->transfer_buffer;
608 tty =tty_port_tty_get(&port->port);
609 if (tty && urb->actual_length) {
610 tty_insert_flip_string(tty, data, urb->actual_length);
611 tty_flip_buffer_push(tty);
615 /* Resubmit urb so we continue receiving */
616 urb->dev = port->serial->dev;
617 if (port->port.count) {
618 err = usb_submit_urb(urb, GFP_ATOMIC);
620 dbg("%s - resubmit read urb failed. (%d)",
623 p_priv->in_flip ^= 1;
625 urb = p_priv->in_urbs[p_priv->in_flip];
626 } while (urb->status != -EINPROGRESS);
629 static void usa28_inack_callback(struct urb *urb)
634 static void usa28_outcont_callback(struct urb *urb)
636 struct usb_serial_port *port;
637 struct keyspan_port_private *p_priv;
640 p_priv = usb_get_serial_port_data(port);
642 if (p_priv->resend_cont) {
643 dbg("%s - sending setup", __func__);
644 keyspan_usa28_send_setup(port->serial, port,
645 p_priv->resend_cont - 1);
649 static void usa28_instat_callback(struct urb *urb)
652 unsigned char *data = urb->transfer_buffer;
653 struct keyspan_usa28_portStatusMessage *msg;
654 struct usb_serial *serial;
655 struct usb_serial_port *port;
656 struct keyspan_port_private *p_priv;
657 struct tty_struct *tty;
659 int status = urb->status;
661 serial = urb->context;
664 dbg("%s - nonzero status: %x", __func__, status);
668 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
669 dbg("%s - bad length %d", __func__, urb->actual_length);
673 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
674 data[0], data[1], data[2], data[3], data[4], data[5],
675 data[6], data[7], data[8], data[9], data[10], data[11]);*/
677 /* Now do something useful with the data */
678 msg = (struct keyspan_usa28_portStatusMessage *)data;
680 /* Check port number from message and retrieve private data */
681 if (msg->port >= serial->num_ports) {
682 dbg("%s - Unexpected port number %d", __func__, msg->port);
685 port = serial->port[msg->port];
686 p_priv = usb_get_serial_port_data(port);
688 /* Update handshaking pin state information */
689 old_dcd_state = p_priv->dcd_state;
690 p_priv->cts_state = ((msg->cts) ? 1 : 0);
691 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
692 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
693 p_priv->ri_state = ((msg->ri) ? 1 : 0);
695 if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
696 tty = tty_port_tty_get(&port->port);
697 if (tty && !C_CLOCAL(tty))
702 /* Resubmit urb so we continue receiving */
703 urb->dev = serial->dev;
704 err = usb_submit_urb(urb, GFP_ATOMIC);
706 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
710 static void usa28_glocont_callback(struct urb *urb)
716 static void usa49_glocont_callback(struct urb *urb)
718 struct usb_serial *serial;
719 struct usb_serial_port *port;
720 struct keyspan_port_private *p_priv;
725 serial = urb->context;
726 for (i = 0; i < serial->num_ports; ++i) {
727 port = serial->port[i];
728 p_priv = usb_get_serial_port_data(port);
730 if (p_priv->resend_cont) {
731 dbg("%s - sending setup", __func__);
732 keyspan_usa49_send_setup(serial, port,
733 p_priv->resend_cont - 1);
739 /* This is actually called glostat in the Keyspan
741 static void usa49_instat_callback(struct urb *urb)
744 unsigned char *data = urb->transfer_buffer;
745 struct keyspan_usa49_portStatusMessage *msg;
746 struct usb_serial *serial;
747 struct usb_serial_port *port;
748 struct keyspan_port_private *p_priv;
750 int status = urb->status;
754 serial = urb->context;
757 dbg("%s - nonzero status: %x", __func__, status);
761 if (urb->actual_length !=
762 sizeof(struct keyspan_usa49_portStatusMessage)) {
763 dbg("%s - bad length %d", __func__, urb->actual_length);
767 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
768 data[0], data[1], data[2], data[3], data[4], data[5],
769 data[6], data[7], data[8], data[9], data[10]);*/
771 /* Now do something useful with the data */
772 msg = (struct keyspan_usa49_portStatusMessage *)data;
774 /* Check port number from message and retrieve private data */
775 if (msg->portNumber >= serial->num_ports) {
776 dbg("%s - Unexpected port number %d",
777 __func__, msg->portNumber);
780 port = serial->port[msg->portNumber];
781 p_priv = usb_get_serial_port_data(port);
783 /* Update handshaking pin state information */
784 old_dcd_state = p_priv->dcd_state;
785 p_priv->cts_state = ((msg->cts) ? 1 : 0);
786 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
787 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
788 p_priv->ri_state = ((msg->ri) ? 1 : 0);
790 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
791 struct tty_struct *tty = tty_port_tty_get(&port->port);
792 if (tty && !C_CLOCAL(tty))
797 /* Resubmit urb so we continue receiving */
798 urb->dev = serial->dev;
800 err = usb_submit_urb(urb, GFP_ATOMIC);
802 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
806 static void usa49_inack_callback(struct urb *urb)
811 static void usa49_indat_callback(struct urb *urb)
815 struct usb_serial_port *port;
816 struct tty_struct *tty;
817 unsigned char *data = urb->transfer_buffer;
818 int status = urb->status;
822 endpoint = usb_pipeendpoint(urb->pipe);
825 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
831 tty = tty_port_tty_get(&port->port);
832 if (tty && urb->actual_length) {
833 /* 0x80 bit is error flag */
834 if ((data[0] & 0x80) == 0) {
835 /* no error on any byte */
836 tty_insert_flip_string(tty, data + 1,
837 urb->actual_length - 1);
839 /* some bytes had errors, every byte has status */
840 for (i = 0; i + 1 < urb->actual_length; i += 2) {
841 int stat = data[i], flag = 0;
842 if (stat & RXERROR_OVERRUN)
844 if (stat & RXERROR_FRAMING)
846 if (stat & RXERROR_PARITY)
848 /* XXX should handle break (0x10) */
849 tty_insert_flip_char(tty, data[i+1], flag);
852 tty_flip_buffer_push(tty);
856 /* Resubmit urb so we continue receiving */
857 urb->dev = port->serial->dev;
858 if (port->port.count) {
859 err = usb_submit_urb(urb, GFP_ATOMIC);
861 dbg("%s - resubmit read urb failed. (%d)",
866 static void usa49wg_indat_callback(struct urb *urb)
869 struct usb_serial *serial;
870 struct usb_serial_port *port;
871 struct tty_struct *tty;
872 unsigned char *data = urb->transfer_buffer;
873 int status = urb->status;
877 serial = urb->context;
880 dbg("%s - nonzero status: %x", __func__, status);
884 /* inbound data is in the form P#, len, status, data */
888 if (urb->actual_length) {
889 while (i < urb->actual_length) {
891 /* Check port number from message*/
892 if (data[i] >= serial->num_ports) {
893 dbg("%s - Unexpected port number %d",
897 port = serial->port[data[i++]];
898 tty = tty_port_tty_get(&port->port);
901 /* 0x80 bit is error flag */
902 if ((data[i] & 0x80) == 0) {
903 /* no error on any byte */
905 for (x = 1; x < len ; ++x)
906 if (port->port.count)
907 tty_insert_flip_char(tty,
913 * some bytes had errors, every byte has status
915 for (x = 0; x + 1 < len; x += 2) {
916 int stat = data[i], flag = 0;
917 if (stat & RXERROR_OVERRUN)
919 if (stat & RXERROR_FRAMING)
921 if (stat & RXERROR_PARITY)
923 /* XXX should handle break (0x10) */
924 if (port->port.count)
925 tty_insert_flip_char(tty,
930 if (port->port.count)
931 tty_flip_buffer_push(tty);
936 /* Resubmit urb so we continue receiving */
937 urb->dev = serial->dev;
939 err = usb_submit_urb(urb, GFP_ATOMIC);
941 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
944 /* not used, usa-49 doesn't have per-port control endpoints */
945 static void usa49_outcont_callback(struct urb *urb)
950 static void usa90_indat_callback(struct urb *urb)
954 struct usb_serial_port *port;
955 struct keyspan_port_private *p_priv;
956 struct tty_struct *tty;
957 unsigned char *data = urb->transfer_buffer;
958 int status = urb->status;
962 endpoint = usb_pipeendpoint(urb->pipe);
965 dbg("%s - nonzero status: %x on endpoint %d.",
966 __func__, status, endpoint);
971 p_priv = usb_get_serial_port_data(port);
973 if (urb->actual_length) {
974 tty = tty_port_tty_get(&port->port);
975 /* if current mode is DMA, looks like usa28 format
976 otherwise looks like usa26 data format */
978 if (p_priv->baud > 57600)
979 tty_insert_flip_string(tty, data, urb->actual_length);
981 /* 0x80 bit is error flag */
982 if ((data[0] & 0x80) == 0) {
983 /* no errors on individual bytes, only
984 possible overrun err*/
985 if (data[0] & RXERROR_OVERRUN)
989 for (i = 1; i < urb->actual_length ; ++i)
990 tty_insert_flip_char(tty, data[i],
993 /* some bytes had errors, every byte has status */
994 dbg("%s - RX error!!!!", __func__);
995 for (i = 0; i + 1 < urb->actual_length; i += 2) {
996 int stat = data[i], flag = 0;
997 if (stat & RXERROR_OVERRUN)
999 if (stat & RXERROR_FRAMING)
1001 if (stat & RXERROR_PARITY)
1003 /* XXX should handle break (0x10) */
1004 tty_insert_flip_char(tty, data[i+1],
1009 tty_flip_buffer_push(tty);
1013 /* Resubmit urb so we continue receiving */
1014 urb->dev = port->serial->dev;
1015 if (port->port.count) {
1016 err = usb_submit_urb(urb, GFP_ATOMIC);
1018 dbg("%s - resubmit read urb failed. (%d)",
1025 static void usa90_instat_callback(struct urb *urb)
1027 unsigned char *data = urb->transfer_buffer;
1028 struct keyspan_usa90_portStatusMessage *msg;
1029 struct usb_serial *serial;
1030 struct usb_serial_port *port;
1031 struct keyspan_port_private *p_priv;
1032 struct tty_struct *tty;
1033 int old_dcd_state, err;
1034 int status = urb->status;
1036 serial = urb->context;
1039 dbg("%s - nonzero status: %x", __func__, status);
1042 if (urb->actual_length < 14) {
1043 dbg("%s - %d byte report??", __func__, urb->actual_length);
1047 msg = (struct keyspan_usa90_portStatusMessage *)data;
1049 /* Now do something useful with the data */
1051 port = serial->port[0];
1052 p_priv = usb_get_serial_port_data(port);
1054 /* Update handshaking pin state information */
1055 old_dcd_state = p_priv->dcd_state;
1056 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1057 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1058 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1059 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1061 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1062 tty = tty_port_tty_get(&port->port);
1063 if (tty && !C_CLOCAL(tty))
1068 /* Resubmit urb so we continue receiving */
1069 urb->dev = serial->dev;
1070 err = usb_submit_urb(urb, GFP_ATOMIC);
1072 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1077 static void usa90_outcont_callback(struct urb *urb)
1079 struct usb_serial_port *port;
1080 struct keyspan_port_private *p_priv;
1082 port = urb->context;
1083 p_priv = usb_get_serial_port_data(port);
1085 if (p_priv->resend_cont) {
1086 dbg("%s - sending setup", __func__);
1087 keyspan_usa90_send_setup(port->serial, port,
1088 p_priv->resend_cont - 1);
1092 /* Status messages from the 28xg */
1093 static void usa67_instat_callback(struct urb *urb)
1096 unsigned char *data = urb->transfer_buffer;
1097 struct keyspan_usa67_portStatusMessage *msg;
1098 struct usb_serial *serial;
1099 struct usb_serial_port *port;
1100 struct keyspan_port_private *p_priv;
1102 int status = urb->status;
1104 dbg("%s", __func__);
1106 serial = urb->context;
1109 dbg("%s - nonzero status: %x", __func__, status);
1113 if (urb->actual_length !=
1114 sizeof(struct keyspan_usa67_portStatusMessage)) {
1115 dbg("%s - bad length %d", __func__, urb->actual_length);
1120 /* Now do something useful with the data */
1121 msg = (struct keyspan_usa67_portStatusMessage *)data;
1123 /* Check port number from message and retrieve private data */
1124 if (msg->port >= serial->num_ports) {
1125 dbg("%s - Unexpected port number %d", __func__, msg->port);
1129 port = serial->port[msg->port];
1130 p_priv = usb_get_serial_port_data(port);
1132 /* Update handshaking pin state information */
1133 old_dcd_state = p_priv->dcd_state;
1134 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1135 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1137 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1138 struct tty_struct *tty = tty_port_tty_get(&port->port);
1139 if (tty && !C_CLOCAL(tty))
1144 /* Resubmit urb so we continue receiving */
1145 urb->dev = serial->dev;
1146 err = usb_submit_urb(urb, GFP_ATOMIC);
1148 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1151 static void usa67_glocont_callback(struct urb *urb)
1153 struct usb_serial *serial;
1154 struct usb_serial_port *port;
1155 struct keyspan_port_private *p_priv;
1158 dbg("%s", __func__);
1160 serial = urb->context;
1161 for (i = 0; i < serial->num_ports; ++i) {
1162 port = serial->port[i];
1163 p_priv = usb_get_serial_port_data(port);
1165 if (p_priv->resend_cont) {
1166 dbg("%s - sending setup", __func__);
1167 keyspan_usa67_send_setup(serial, port,
1168 p_priv->resend_cont - 1);
1174 static int keyspan_write_room(struct tty_struct *tty)
1176 struct usb_serial_port *port = tty->driver_data;
1177 struct keyspan_port_private *p_priv;
1178 const struct keyspan_device_details *d_details;
1181 struct urb *this_urb;
1183 dbg("%s", __func__);
1184 p_priv = usb_get_serial_port_data(port);
1185 d_details = p_priv->device_details;
1187 /* FIXME: locking */
1188 if (d_details->msg_format == msg_usa90)
1193 flip = p_priv->out_flip;
1195 /* Check both endpoints to see if any are available. */
1196 this_urb = p_priv->out_urbs[flip];
1197 if (this_urb != NULL) {
1198 if (this_urb->status != -EINPROGRESS)
1200 flip = (flip + 1) & d_details->outdat_endp_flip;
1201 this_urb = p_priv->out_urbs[flip];
1202 if (this_urb != NULL) {
1203 if (this_urb->status != -EINPROGRESS)
1211 static int keyspan_open(struct tty_struct *tty,
1212 struct usb_serial_port *port, struct file *filp)
1214 struct keyspan_port_private *p_priv;
1215 struct keyspan_serial_private *s_priv;
1216 struct usb_serial *serial = port->serial;
1217 const struct keyspan_device_details *d_details;
1219 int baud_rate, device_port;
1221 unsigned int cflag = 0;
1223 s_priv = usb_get_serial_data(serial);
1224 p_priv = usb_get_serial_port_data(port);
1225 d_details = p_priv->device_details;
1227 dbg("%s - port%d.", __func__, port->number);
1229 /* Set some sane defaults */
1230 p_priv->rts_state = 1;
1231 p_priv->dtr_state = 1;
1232 p_priv->baud = 9600;
1234 /* force baud and lcr to be set on open */
1235 p_priv->old_baud = 0;
1236 p_priv->old_cflag = 0;
1238 p_priv->out_flip = 0;
1239 p_priv->in_flip = 0;
1241 /* Reset low level data toggle and start reading from endpoints */
1242 for (i = 0; i < 2; i++) {
1243 urb = p_priv->in_urbs[i];
1246 urb->dev = serial->dev;
1248 /* make sure endpoint data toggle is synchronized
1250 usb_clear_halt(urb->dev, urb->pipe);
1251 err = usb_submit_urb(urb, GFP_KERNEL);
1253 dbg("%s - submit urb %d failed (%d)",
1257 /* Reset low level data toggle on out endpoints */
1258 for (i = 0; i < 2; i++) {
1259 urb = p_priv->out_urbs[i];
1262 urb->dev = serial->dev;
1263 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1264 usb_pipeout(urb->pipe), 0); */
1267 /* get the terminal config for the setup message now so we don't
1268 * need to send 2 of them */
1270 device_port = port->number - port->serial->minor;
1272 cflag = tty->termios->c_cflag;
1273 /* Baud rate calculation takes baud rate as an integer
1274 so other rates can be generated if desired. */
1275 baud_rate = tty_get_baud_rate(tty);
1276 /* If no match or invalid, leave as default */
1278 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1279 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1280 p_priv->baud = baud_rate;
1283 /* set CTS/RTS handshake etc. */
1284 p_priv->cflag = cflag;
1285 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1287 keyspan_send_setup(port, 1);
1289 /* keyspan_set_termios(port, NULL); */
1294 static inline void stop_urb(struct urb *urb)
1296 if (urb && urb->status == -EINPROGRESS)
1300 static void keyspan_close(struct tty_struct *tty,
1301 struct usb_serial_port *port, struct file *filp)
1304 struct usb_serial *serial = port->serial;
1305 struct keyspan_serial_private *s_priv;
1306 struct keyspan_port_private *p_priv;
1308 dbg("%s", __func__);
1309 s_priv = usb_get_serial_data(serial);
1310 p_priv = usb_get_serial_port_data(port);
1312 p_priv->rts_state = 0;
1313 p_priv->dtr_state = 0;
1316 keyspan_send_setup(port, 2);
1317 /* pilot-xfer seems to work best with this delay */
1319 /* keyspan_set_termios(port, NULL); */
1322 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1323 dbg("%s - urb in progress", __func__);
1326 p_priv->out_flip = 0;
1327 p_priv->in_flip = 0;
1330 /* Stop reading/writing urbs */
1331 stop_urb(p_priv->inack_urb);
1332 /* stop_urb(p_priv->outcont_urb); */
1333 for (i = 0; i < 2; i++) {
1334 stop_urb(p_priv->in_urbs[i]);
1335 stop_urb(p_priv->out_urbs[i]);
1338 tty_port_tty_set(&port->port, NULL);
1341 /* download the firmware to a pre-renumeration device */
1342 static int keyspan_fake_startup(struct usb_serial *serial)
1345 const struct ihex_binrec *record;
1347 const struct firmware *fw;
1349 dbg("Keyspan startup version %04x product %04x",
1350 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1351 le16_to_cpu(serial->dev->descriptor.idProduct));
1353 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1355 dbg("Firmware already loaded. Quitting.");
1359 /* Select firmware image on the basis of idProduct */
1360 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1361 case keyspan_usa28_pre_product_id:
1362 fw_name = "keyspan/usa28.fw";
1365 case keyspan_usa28x_pre_product_id:
1366 fw_name = "keyspan/usa28x.fw";
1369 case keyspan_usa28xa_pre_product_id:
1370 fw_name = "keyspan/usa28xa.fw";
1373 case keyspan_usa28xb_pre_product_id:
1374 fw_name = "keyspan/usa28xb.fw";
1377 case keyspan_usa19_pre_product_id:
1378 fw_name = "keyspan/usa19.fw";
1381 case keyspan_usa19qi_pre_product_id:
1382 fw_name = "keyspan/usa19qi.fw";
1385 case keyspan_mpr_pre_product_id:
1386 fw_name = "keyspan/mpr.fw";
1389 case keyspan_usa19qw_pre_product_id:
1390 fw_name = "keyspan/usa19qw.fw";
1393 case keyspan_usa18x_pre_product_id:
1394 fw_name = "keyspan/usa18x.fw";
1397 case keyspan_usa19w_pre_product_id:
1398 fw_name = "keyspan/usa19w.fw";
1401 case keyspan_usa49w_pre_product_id:
1402 fw_name = "keyspan/usa49w.fw";
1405 case keyspan_usa49wlc_pre_product_id:
1406 fw_name = "keyspan/usa49wlc.fw";
1410 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1411 le16_to_cpu(serial->dev->descriptor.idProduct));
1415 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1416 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1420 dbg("Uploading Keyspan %s firmware.", fw_name);
1422 /* download the firmware image */
1423 response = ezusb_set_reset(serial, 1);
1425 record = (const struct ihex_binrec *)fw->data;
1428 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1429 (unsigned char *)record->data,
1430 be16_to_cpu(record->len), 0xa0);
1432 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1433 response, be32_to_cpu(record->addr),
1434 record->data, be16_to_cpu(record->len));
1437 record = ihex_next_binrec(record);
1439 release_firmware(fw);
1440 /* bring device out of reset. Renumeration will occur in a
1441 moment and the new device will bind to the real driver */
1442 response = ezusb_set_reset(serial, 0);
1444 /* we don't want this device to have a driver assigned to it. */
1448 /* Helper functions used by keyspan_setup_urbs */
1449 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1452 struct usb_host_interface *iface_desc;
1453 struct usb_endpoint_descriptor *ep;
1456 iface_desc = serial->interface->cur_altsetting;
1457 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1458 ep = &iface_desc->endpoint[i].desc;
1459 if (ep->bEndpointAddress == endpoint)
1462 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1463 "endpoint %x\n", endpoint);
1467 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1468 int dir, void *ctx, char *buf, int len,
1469 void (*callback)(struct urb *))
1472 struct usb_endpoint_descriptor const *ep_desc;
1473 char const *ep_type_name;
1476 return NULL; /* endpoint not needed */
1478 dbg("%s - alloc for endpoint %d.", __func__, endpoint);
1479 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1481 dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
1485 if (endpoint == 0) {
1486 /* control EP filled in when used */
1490 ep_desc = find_ep(serial, endpoint);
1492 /* leak the urb, something's wrong and the callers don't care */
1495 if (usb_endpoint_xfer_int(ep_desc)) {
1496 ep_type_name = "INT";
1497 usb_fill_int_urb(urb, serial->dev,
1498 usb_sndintpipe(serial->dev, endpoint) | dir,
1499 buf, len, callback, ctx,
1500 ep_desc->bInterval);
1501 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1502 ep_type_name = "BULK";
1503 usb_fill_bulk_urb(urb, serial->dev,
1504 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1505 buf, len, callback, ctx);
1507 dev_warn(&serial->interface->dev,
1508 "unsupported endpoint type %x\n",
1509 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1514 dbg("%s - using urb %p for %s endpoint %x",
1515 __func__, urb, ep_type_name, endpoint);
1519 static struct callbacks {
1520 void (*instat_callback)(struct urb *);
1521 void (*glocont_callback)(struct urb *);
1522 void (*indat_callback)(struct urb *);
1523 void (*outdat_callback)(struct urb *);
1524 void (*inack_callback)(struct urb *);
1525 void (*outcont_callback)(struct urb *);
1526 } keyspan_callbacks[] = {
1528 /* msg_usa26 callbacks */
1529 .instat_callback = usa26_instat_callback,
1530 .glocont_callback = usa26_glocont_callback,
1531 .indat_callback = usa26_indat_callback,
1532 .outdat_callback = usa2x_outdat_callback,
1533 .inack_callback = usa26_inack_callback,
1534 .outcont_callback = usa26_outcont_callback,
1536 /* msg_usa28 callbacks */
1537 .instat_callback = usa28_instat_callback,
1538 .glocont_callback = usa28_glocont_callback,
1539 .indat_callback = usa28_indat_callback,
1540 .outdat_callback = usa2x_outdat_callback,
1541 .inack_callback = usa28_inack_callback,
1542 .outcont_callback = usa28_outcont_callback,
1544 /* msg_usa49 callbacks */
1545 .instat_callback = usa49_instat_callback,
1546 .glocont_callback = usa49_glocont_callback,
1547 .indat_callback = usa49_indat_callback,
1548 .outdat_callback = usa2x_outdat_callback,
1549 .inack_callback = usa49_inack_callback,
1550 .outcont_callback = usa49_outcont_callback,
1552 /* msg_usa90 callbacks */
1553 .instat_callback = usa90_instat_callback,
1554 .glocont_callback = usa28_glocont_callback,
1555 .indat_callback = usa90_indat_callback,
1556 .outdat_callback = usa2x_outdat_callback,
1557 .inack_callback = usa28_inack_callback,
1558 .outcont_callback = usa90_outcont_callback,
1560 /* msg_usa67 callbacks */
1561 .instat_callback = usa67_instat_callback,
1562 .glocont_callback = usa67_glocont_callback,
1563 .indat_callback = usa26_indat_callback,
1564 .outdat_callback = usa2x_outdat_callback,
1565 .inack_callback = usa26_inack_callback,
1566 .outcont_callback = usa26_outcont_callback,
1570 /* Generic setup urbs function that uses
1571 data in device_details */
1572 static void keyspan_setup_urbs(struct usb_serial *serial)
1575 struct keyspan_serial_private *s_priv;
1576 const struct keyspan_device_details *d_details;
1577 struct usb_serial_port *port;
1578 struct keyspan_port_private *p_priv;
1579 struct callbacks *cback;
1582 dbg("%s", __func__);
1584 s_priv = usb_get_serial_data(serial);
1585 d_details = s_priv->device_details;
1587 /* Setup values for the various callback routines */
1588 cback = &keyspan_callbacks[d_details->msg_format];
1590 /* Allocate and set up urbs for each one that is in use,
1591 starting with instat endpoints */
1592 s_priv->instat_urb = keyspan_setup_urb
1593 (serial, d_details->instat_endpoint, USB_DIR_IN,
1594 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1595 cback->instat_callback);
1597 s_priv->indat_urb = keyspan_setup_urb
1598 (serial, d_details->indat_endpoint, USB_DIR_IN,
1599 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1600 usa49wg_indat_callback);
1602 s_priv->glocont_urb = keyspan_setup_urb
1603 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1604 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1605 cback->glocont_callback);
1607 /* Setup endpoints for each port specific thing */
1608 for (i = 0; i < d_details->num_ports; i++) {
1609 port = serial->port[i];
1610 p_priv = usb_get_serial_port_data(port);
1612 /* Do indat endpoints first, once for each flip */
1613 endp = d_details->indat_endpoints[i];
1614 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1615 p_priv->in_urbs[j] = keyspan_setup_urb
1616 (serial, endp, USB_DIR_IN, port,
1617 p_priv->in_buffer[j], 64,
1618 cback->indat_callback);
1621 p_priv->in_urbs[j] = NULL;
1623 /* outdat endpoints also have flip */
1624 endp = d_details->outdat_endpoints[i];
1625 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1626 p_priv->out_urbs[j] = keyspan_setup_urb
1627 (serial, endp, USB_DIR_OUT, port,
1628 p_priv->out_buffer[j], 64,
1629 cback->outdat_callback);
1632 p_priv->out_urbs[j] = NULL;
1634 /* inack endpoint */
1635 p_priv->inack_urb = keyspan_setup_urb
1636 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1637 port, p_priv->inack_buffer, 1, cback->inack_callback);
1639 /* outcont endpoint */
1640 p_priv->outcont_urb = keyspan_setup_urb
1641 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1642 port, p_priv->outcont_buffer, 64,
1643 cback->outcont_callback);
1647 /* usa19 function doesn't require prescaler */
1648 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1649 u8 *rate_low, u8 *prescaler, int portnum)
1651 u32 b16, /* baud rate times 16 (actual rate used internally) */
1653 cnt; /* inverse of divisor (programmed into 8051) */
1655 dbg("%s - %d.", __func__, baud_rate);
1657 /* prevent divide by zero... */
1658 b16 = baud_rate * 16L;
1660 return KEYSPAN_INVALID_BAUD_RATE;
1661 /* Any "standard" rate over 57k6 is marginal on the USA-19
1662 as we run out of divisor resolution. */
1663 if (baud_rate > 57600)
1664 return KEYSPAN_INVALID_BAUD_RATE;
1666 /* calculate the divisor and the counter (its inverse) */
1667 div = baudclk / b16;
1669 return KEYSPAN_INVALID_BAUD_RATE;
1674 return KEYSPAN_INVALID_BAUD_RATE;
1676 /* return the counter values if non-null */
1678 *rate_low = (u8) (cnt & 0xff);
1680 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1681 if (rate_low && rate_hi)
1682 dbg("%s - %d %02x %02x.",
1683 __func__, baud_rate, *rate_hi, *rate_low);
1684 return KEYSPAN_BAUD_RATE_OK;
1687 /* usa19hs function doesn't require prescaler */
1688 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1689 u8 *rate_low, u8 *prescaler, int portnum)
1691 u32 b16, /* baud rate times 16 (actual rate used internally) */
1694 dbg("%s - %d.", __func__, baud_rate);
1696 /* prevent divide by zero... */
1697 b16 = baud_rate * 16L;
1699 return KEYSPAN_INVALID_BAUD_RATE;
1701 /* calculate the divisor */
1702 div = baudclk / b16;
1704 return KEYSPAN_INVALID_BAUD_RATE;
1707 return KEYSPAN_INVALID_BAUD_RATE;
1709 /* return the counter values if non-null */
1711 *rate_low = (u8) (div & 0xff);
1714 *rate_hi = (u8) ((div >> 8) & 0xff);
1716 if (rate_low && rate_hi)
1717 dbg("%s - %d %02x %02x.",
1718 __func__, baud_rate, *rate_hi, *rate_low);
1720 return KEYSPAN_BAUD_RATE_OK;
1723 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1724 u8 *rate_low, u8 *prescaler, int portnum)
1726 u32 b16, /* baud rate times 16 (actual rate used internally) */
1727 clk, /* clock with 13/8 prescaler */
1728 div, /* divisor using 13/8 prescaler */
1729 res, /* resulting baud rate using 13/8 prescaler */
1730 diff, /* error using 13/8 prescaler */
1735 dbg("%s - %d.", __func__, baud_rate);
1737 /* prevent divide by zero */
1738 b16 = baud_rate * 16L;
1740 return KEYSPAN_INVALID_BAUD_RATE;
1742 /* Calculate prescaler by trying them all and looking
1745 /* start with largest possible difference */
1746 smallest_diff = 0xffffffff;
1748 /* 0 is an invalid prescaler, used as a flag */
1751 for (i = 8; i <= 0xff; ++i) {
1752 clk = (baudclk * 8) / (u32) i;
1759 diff = (res > b16) ? (res-b16) : (b16-res);
1761 if (diff < smallest_diff) {
1763 smallest_diff = diff;
1767 if (best_prescaler == 0)
1768 return KEYSPAN_INVALID_BAUD_RATE;
1770 clk = (baudclk * 8) / (u32) best_prescaler;
1773 /* return the divisor and prescaler if non-null */
1775 *rate_low = (u8) (div & 0xff);
1777 *rate_hi = (u8) ((div >> 8) & 0xff);
1779 *prescaler = best_prescaler;
1780 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1782 return KEYSPAN_BAUD_RATE_OK;
1785 /* USA-28 supports different maximum baud rates on each port */
1786 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1787 u8 *rate_low, u8 *prescaler, int portnum)
1789 u32 b16, /* baud rate times 16 (actual rate used internally) */
1791 cnt; /* inverse of divisor (programmed into 8051) */
1793 dbg("%s - %d.", __func__, baud_rate);
1795 /* prevent divide by zero */
1796 b16 = baud_rate * 16L;
1798 return KEYSPAN_INVALID_BAUD_RATE;
1800 /* calculate the divisor and the counter (its inverse) */
1801 div = KEYSPAN_USA28_BAUDCLK / b16;
1803 return KEYSPAN_INVALID_BAUD_RATE;
1807 /* check for out of range, based on portnum,
1808 and return result */
1811 return KEYSPAN_INVALID_BAUD_RATE;
1815 return KEYSPAN_INVALID_BAUD_RATE;
1817 return KEYSPAN_INVALID_BAUD_RATE;
1820 /* return the counter values if not NULL
1821 (port 1 will ignore retHi) */
1823 *rate_low = (u8) (cnt & 0xff);
1825 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1826 dbg("%s - %d OK.", __func__, baud_rate);
1827 return KEYSPAN_BAUD_RATE_OK;
1830 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1831 struct usb_serial_port *port,
1834 struct keyspan_usa26_portControlMessage msg;
1835 struct keyspan_serial_private *s_priv;
1836 struct keyspan_port_private *p_priv;
1837 const struct keyspan_device_details *d_details;
1839 struct urb *this_urb;
1840 int device_port, err;
1842 dbg("%s reset=%d", __func__, reset_port);
1844 s_priv = usb_get_serial_data(serial);
1845 p_priv = usb_get_serial_port_data(port);
1846 d_details = s_priv->device_details;
1847 device_port = port->number - port->serial->minor;
1849 outcont_urb = d_details->outcont_endpoints[port->number];
1850 this_urb = p_priv->outcont_urb;
1852 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1854 /* Make sure we have an urb then send the message */
1855 if (this_urb == NULL) {
1856 dbg("%s - oops no urb.", __func__);
1860 /* Save reset port val for resend.
1861 Don't overwrite resend for open/close condition. */
1862 if ((reset_port + 1) > p_priv->resend_cont)
1863 p_priv->resend_cont = reset_port + 1;
1864 if (this_urb->status == -EINPROGRESS) {
1865 /* dbg("%s - already writing", __func__); */
1870 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1872 /* Only set baud rate if it's changed */
1873 if (p_priv->old_baud != p_priv->baud) {
1874 p_priv->old_baud = p_priv->baud;
1875 msg.setClocking = 0xff;
1876 if (d_details->calculate_baud_rate
1877 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1878 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1879 dbg("%s - Invalid baud rate %d requested, using 9600.",
1880 __func__, p_priv->baud);
1882 msg.baudHi = 125; /* Values for 9600 baud */
1885 msg.setPrescaler = 0xff;
1888 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1889 switch (p_priv->cflag & CSIZE) {
1891 msg.lcr |= USA_DATABITS_5;
1894 msg.lcr |= USA_DATABITS_6;
1897 msg.lcr |= USA_DATABITS_7;
1900 msg.lcr |= USA_DATABITS_8;
1903 if (p_priv->cflag & PARENB) {
1904 /* note USA_PARITY_NONE == 0 */
1905 msg.lcr |= (p_priv->cflag & PARODD)?
1906 USA_PARITY_ODD : USA_PARITY_EVEN;
1910 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1911 msg.xonFlowControl = 0;
1912 msg.setFlowControl = 0xff;
1913 msg.forwardingLength = 16;
1918 if (reset_port == 1) {
1927 msg.returnStatus = 0;
1928 msg.resetDataToggle = 0xff;
1932 else if (reset_port == 2) {
1941 msg.returnStatus = 0;
1942 msg.resetDataToggle = 0;
1945 /* Sending intermediate configs */
1947 msg._txOn = (!p_priv->break_on);
1950 msg.txBreak = (p_priv->break_on);
1955 msg.returnStatus = 0;
1956 msg.resetDataToggle = 0x0;
1959 /* Do handshaking outputs */
1960 msg.setTxTriState_setRts = 0xff;
1961 msg.txTriState_rts = p_priv->rts_state;
1963 msg.setHskoa_setDtr = 0xff;
1964 msg.hskoa_dtr = p_priv->dtr_state;
1966 p_priv->resend_cont = 0;
1967 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1969 /* send the data out the device on control endpoint */
1970 this_urb->transfer_buffer_length = sizeof(msg);
1972 this_urb->dev = serial->dev;
1973 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1975 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1978 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1979 outcont_urb, this_urb->transfer_buffer_length,
1980 usb_pipeendpoint(this_urb->pipe));
1987 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1988 struct usb_serial_port *port,
1991 struct keyspan_usa28_portControlMessage msg;
1992 struct keyspan_serial_private *s_priv;
1993 struct keyspan_port_private *p_priv;
1994 const struct keyspan_device_details *d_details;
1995 struct urb *this_urb;
1996 int device_port, err;
1998 dbg("%s", __func__);
2000 s_priv = usb_get_serial_data(serial);
2001 p_priv = usb_get_serial_port_data(port);
2002 d_details = s_priv->device_details;
2003 device_port = port->number - port->serial->minor;
2005 /* only do something if we have a bulk out endpoint */
2006 this_urb = p_priv->outcont_urb;
2007 if (this_urb == NULL) {
2008 dbg("%s - oops no urb.", __func__);
2012 /* Save reset port val for resend.
2013 Don't overwrite resend for open/close condition. */
2014 if ((reset_port + 1) > p_priv->resend_cont)
2015 p_priv->resend_cont = reset_port + 1;
2016 if (this_urb->status == -EINPROGRESS) {
2017 dbg("%s already writing", __func__);
2022 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
2024 msg.setBaudRate = 1;
2025 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2026 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2027 dbg("%s - Invalid baud rate requested %d.",
2028 __func__, p_priv->baud);
2030 msg.baudHi = 0xb2; /* Values for 9600 baud */
2033 /* If parity is enabled, we must calculate it ourselves. */
2034 msg.parity = 0; /* XXX for now */
2036 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2037 msg.xonFlowControl = 0;
2039 /* Do handshaking outputs, DTR is inverted relative to RTS */
2040 msg.rts = p_priv->rts_state;
2041 msg.dtr = p_priv->dtr_state;
2043 msg.forwardingLength = 16;
2045 msg.breakThreshold = 45;
2049 /*msg.returnStatus = 1;
2050 msg.resetDataToggle = 0xff;*/
2052 if (reset_port == 1) {
2056 msg.txForceXoff = 0;
2062 msg.returnStatus = 0;
2063 msg.resetDataToggle = 0xff;
2066 else if (reset_port == 2) {
2070 msg.txForceXoff = 0;
2076 msg.returnStatus = 0;
2077 msg.resetDataToggle = 0;
2079 /* Sending intermediate configs */
2081 msg._txOn = (!p_priv->break_on);
2084 msg.txForceXoff = 0;
2085 msg.txBreak = (p_priv->break_on);
2090 msg.returnStatus = 0;
2091 msg.resetDataToggle = 0x0;
2094 p_priv->resend_cont = 0;
2095 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2097 /* send the data out the device on control endpoint */
2098 this_urb->transfer_buffer_length = sizeof(msg);
2100 this_urb->dev = serial->dev;
2101 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2103 dbg("%s - usb_submit_urb(setup) failed", __func__);
2106 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
2107 this_urb->transfer_buffer_length);
2114 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2115 struct usb_serial_port *port,
2118 struct keyspan_usa49_portControlMessage msg;
2119 struct usb_ctrlrequest *dr = NULL;
2120 struct keyspan_serial_private *s_priv;
2121 struct keyspan_port_private *p_priv;
2122 const struct keyspan_device_details *d_details;
2123 struct urb *this_urb;
2124 int err, device_port;
2126 dbg("%s", __func__);
2128 s_priv = usb_get_serial_data(serial);
2129 p_priv = usb_get_serial_port_data(port);
2130 d_details = s_priv->device_details;
2132 this_urb = s_priv->glocont_urb;
2134 /* Work out which port within the device is being setup */
2135 device_port = port->number - port->serial->minor;
2137 dbg("%s - endpoint %d port %d (%d)",
2138 __func__, usb_pipeendpoint(this_urb->pipe),
2139 port->number, device_port);
2141 /* Make sure we have an urb then send the message */
2142 if (this_urb == NULL) {
2143 dbg("%s - oops no urb for port %d.", __func__, port->number);
2147 /* Save reset port val for resend.
2148 Don't overwrite resend for open/close condition. */
2149 if ((reset_port + 1) > p_priv->resend_cont)
2150 p_priv->resend_cont = reset_port + 1;
2152 if (this_urb->status == -EINPROGRESS) {
2153 /* dbg("%s - already writing", __func__); */
2158 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
2160 /*msg.portNumber = port->number;*/
2161 msg.portNumber = device_port;
2163 /* Only set baud rate if it's changed */
2164 if (p_priv->old_baud != p_priv->baud) {
2165 p_priv->old_baud = p_priv->baud;
2166 msg.setClocking = 0xff;
2167 if (d_details->calculate_baud_rate
2168 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2169 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2170 dbg("%s - Invalid baud rate %d requested, using 9600.",
2171 __func__, p_priv->baud);
2173 msg.baudHi = 125; /* Values for 9600 baud */
2176 /* msg.setPrescaler = 0xff; */
2179 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2180 switch (p_priv->cflag & CSIZE) {
2182 msg.lcr |= USA_DATABITS_5;
2185 msg.lcr |= USA_DATABITS_6;
2188 msg.lcr |= USA_DATABITS_7;
2191 msg.lcr |= USA_DATABITS_8;
2194 if (p_priv->cflag & PARENB) {
2195 /* note USA_PARITY_NONE == 0 */
2196 msg.lcr |= (p_priv->cflag & PARODD)?
2197 USA_PARITY_ODD : USA_PARITY_EVEN;
2201 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2202 msg.xonFlowControl = 0;
2203 msg.setFlowControl = 0xff;
2205 msg.forwardingLength = 16;
2210 if (reset_port == 1) {
2219 msg.returnStatus = 0;
2220 msg.resetDataToggle = 0xff;
2222 msg.disablePort = 0;
2225 else if (reset_port == 2) {
2234 msg.returnStatus = 0;
2235 msg.resetDataToggle = 0;
2237 msg.disablePort = 1;
2239 /* Sending intermediate configs */
2241 msg._txOn = (!p_priv->break_on);
2244 msg.txBreak = (p_priv->break_on);
2249 msg.returnStatus = 0;
2250 msg.resetDataToggle = 0x0;
2252 msg.disablePort = 0;
2255 /* Do handshaking outputs */
2257 msg.rts = p_priv->rts_state;
2260 msg.dtr = p_priv->dtr_state;
2262 p_priv->resend_cont = 0;
2264 /* if the device is a 49wg, we send control message on usb
2267 if (d_details->product_id == keyspan_usa49wg_product_id) {
2268 dr = (void *)(s_priv->ctrl_buf);
2269 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2270 dr->bRequest = 0xB0; /* 49wg control message */;
2273 dr->wLength = cpu_to_le16(sizeof(msg));
2275 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2277 usb_fill_control_urb(this_urb, serial->dev,
2278 usb_sndctrlpipe(serial->dev, 0),
2279 (unsigned char *)dr, s_priv->glocont_buf,
2280 sizeof(msg), usa49_glocont_callback, serial);
2283 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2285 /* send the data out the device on control endpoint */
2286 this_urb->transfer_buffer_length = sizeof(msg);
2288 this_urb->dev = serial->dev;
2290 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2292 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2295 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2296 outcont_urb, this_urb->transfer_buffer_length,
2297 usb_pipeendpoint(this_urb->pipe));
2304 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2305 struct usb_serial_port *port,
2308 struct keyspan_usa90_portControlMessage msg;
2309 struct keyspan_serial_private *s_priv;
2310 struct keyspan_port_private *p_priv;
2311 const struct keyspan_device_details *d_details;
2312 struct urb *this_urb;
2316 dbg("%s", __func__);
2318 s_priv = usb_get_serial_data(serial);
2319 p_priv = usb_get_serial_port_data(port);
2320 d_details = s_priv->device_details;
2322 /* only do something if we have a bulk out endpoint */
2323 this_urb = p_priv->outcont_urb;
2324 if (this_urb == NULL) {
2325 dbg("%s - oops no urb.", __func__);
2329 /* Save reset port val for resend.
2330 Don't overwrite resend for open/close condition. */
2331 if ((reset_port + 1) > p_priv->resend_cont)
2332 p_priv->resend_cont = reset_port + 1;
2333 if (this_urb->status == -EINPROGRESS) {
2334 dbg("%s already writing", __func__);
2339 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2341 /* Only set baud rate if it's changed */
2342 if (p_priv->old_baud != p_priv->baud) {
2343 p_priv->old_baud = p_priv->baud;
2344 msg.setClocking = 0x01;
2345 if (d_details->calculate_baud_rate
2346 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2347 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2348 dbg("%s - Invalid baud rate %d requested, using 9600.",
2349 __func__, p_priv->baud);
2350 p_priv->baud = 9600;
2351 d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2352 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2358 /* modes must always be correctly specified */
2359 if (p_priv->baud > 57600) {
2360 msg.rxMode = RXMODE_DMA;
2361 msg.txMode = TXMODE_DMA;
2363 msg.rxMode = RXMODE_BYHAND;
2364 msg.txMode = TXMODE_BYHAND;
2367 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2368 switch (p_priv->cflag & CSIZE) {
2370 msg.lcr |= USA_DATABITS_5;
2373 msg.lcr |= USA_DATABITS_6;
2376 msg.lcr |= USA_DATABITS_7;
2379 msg.lcr |= USA_DATABITS_8;
2382 if (p_priv->cflag & PARENB) {
2383 /* note USA_PARITY_NONE == 0 */
2384 msg.lcr |= (p_priv->cflag & PARODD)?
2385 USA_PARITY_ODD : USA_PARITY_EVEN;
2387 if (p_priv->old_cflag != p_priv->cflag) {
2388 p_priv->old_cflag = p_priv->cflag;
2392 if (p_priv->flow_control == flow_cts)
2393 msg.txFlowControl = TXFLOW_CTS;
2394 msg.setTxFlowControl = 0x01;
2395 msg.setRxFlowControl = 0x01;
2397 msg.rxForwardingLength = 16;
2398 msg.rxForwardingTimeout = 16;
2399 msg.txAckSetting = 0;
2404 if (reset_port == 1) {
2405 msg.portEnabled = 1;
2407 msg.txBreak = (p_priv->break_on);
2410 else if (reset_port == 2)
2411 msg.portEnabled = 0;
2412 /* Sending intermediate configs */
2414 if (port->port.count)
2415 msg.portEnabled = 1;
2416 msg.txBreak = (p_priv->break_on);
2419 /* Do handshaking outputs */
2421 msg.rts = p_priv->rts_state;
2424 msg.dtr = p_priv->dtr_state;
2426 p_priv->resend_cont = 0;
2427 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2429 /* send the data out the device on control endpoint */
2430 this_urb->transfer_buffer_length = sizeof(msg);
2432 this_urb->dev = serial->dev;
2433 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2435 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2439 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2440 struct usb_serial_port *port,
2443 struct keyspan_usa67_portControlMessage msg;
2444 struct keyspan_serial_private *s_priv;
2445 struct keyspan_port_private *p_priv;
2446 const struct keyspan_device_details *d_details;
2447 struct urb *this_urb;
2448 int err, device_port;
2450 dbg("%s", __func__);
2452 s_priv = usb_get_serial_data(serial);
2453 p_priv = usb_get_serial_port_data(port);
2454 d_details = s_priv->device_details;
2456 this_urb = s_priv->glocont_urb;
2458 /* Work out which port within the device is being setup */
2459 device_port = port->number - port->serial->minor;
2461 /* Make sure we have an urb then send the message */
2462 if (this_urb == NULL) {
2463 dbg("%s - oops no urb for port %d.", __func__,
2468 /* Save reset port val for resend.
2469 Don't overwrite resend for open/close condition. */
2470 if ((reset_port + 1) > p_priv->resend_cont)
2471 p_priv->resend_cont = reset_port + 1;
2472 if (this_urb->status == -EINPROGRESS) {
2473 /* dbg("%s - already writing", __func__); */
2478 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2480 msg.port = device_port;
2482 /* Only set baud rate if it's changed */
2483 if (p_priv->old_baud != p_priv->baud) {
2484 p_priv->old_baud = p_priv->baud;
2485 msg.setClocking = 0xff;
2486 if (d_details->calculate_baud_rate
2487 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2488 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2489 dbg("%s - Invalid baud rate %d requested, using 9600.",
2490 __func__, p_priv->baud);
2492 msg.baudHi = 125; /* Values for 9600 baud */
2495 msg.setPrescaler = 0xff;
2498 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2499 switch (p_priv->cflag & CSIZE) {
2501 msg.lcr |= USA_DATABITS_5;
2504 msg.lcr |= USA_DATABITS_6;
2507 msg.lcr |= USA_DATABITS_7;
2510 msg.lcr |= USA_DATABITS_8;
2513 if (p_priv->cflag & PARENB) {
2514 /* note USA_PARITY_NONE == 0 */
2515 msg.lcr |= (p_priv->cflag & PARODD)?
2516 USA_PARITY_ODD : USA_PARITY_EVEN;
2520 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2521 msg.xonFlowControl = 0;
2522 msg.setFlowControl = 0xff;
2523 msg.forwardingLength = 16;
2527 if (reset_port == 1) {
2537 msg.returnStatus = 0;
2538 msg.resetDataToggle = 0xff;
2539 } else if (reset_port == 2) {
2549 msg.returnStatus = 0;
2550 msg.resetDataToggle = 0;
2552 /* Sending intermediate configs */
2553 msg._txOn = (!p_priv->break_on);
2556 msg.txBreak = (p_priv->break_on);
2561 msg.returnStatus = 0;
2562 msg.resetDataToggle = 0x0;
2565 /* Do handshaking outputs */
2566 msg.setTxTriState_setRts = 0xff;
2567 msg.txTriState_rts = p_priv->rts_state;
2569 msg.setHskoa_setDtr = 0xff;
2570 msg.hskoa_dtr = p_priv->dtr_state;
2572 p_priv->resend_cont = 0;
2574 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2576 /* send the data out the device on control endpoint */
2577 this_urb->transfer_buffer_length = sizeof(msg);
2578 this_urb->dev = serial->dev;
2580 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2582 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2587 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2589 struct usb_serial *serial = port->serial;
2590 struct keyspan_serial_private *s_priv;
2591 const struct keyspan_device_details *d_details;
2593 dbg("%s", __func__);
2595 s_priv = usb_get_serial_data(serial);
2596 d_details = s_priv->device_details;
2598 switch (d_details->msg_format) {
2600 keyspan_usa26_send_setup(serial, port, reset_port);
2603 keyspan_usa28_send_setup(serial, port, reset_port);
2606 keyspan_usa49_send_setup(serial, port, reset_port);
2609 keyspan_usa90_send_setup(serial, port, reset_port);
2612 keyspan_usa67_send_setup(serial, port, reset_port);
2618 /* Gets called by the "real" driver (ie once firmware is loaded
2619 and renumeration has taken place. */
2620 static int keyspan_startup(struct usb_serial *serial)
2623 struct usb_serial_port *port;
2624 struct keyspan_serial_private *s_priv;
2625 struct keyspan_port_private *p_priv;
2626 const struct keyspan_device_details *d_details;
2628 dbg("%s", __func__);
2630 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2631 if (d_details->product_id ==
2632 le16_to_cpu(serial->dev->descriptor.idProduct))
2634 if (d_details == NULL) {
2635 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2636 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2640 /* Setup private data for serial driver */
2641 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2643 dbg("%s - kmalloc for keyspan_serial_private failed.",
2648 s_priv->device_details = d_details;
2649 usb_set_serial_data(serial, s_priv);
2651 /* Now setup per port private data */
2652 for (i = 0; i < serial->num_ports; i++) {
2653 port = serial->port[i];
2654 p_priv = kzalloc(sizeof(struct keyspan_port_private),
2657 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2660 p_priv->device_details = d_details;
2661 usb_set_serial_port_data(port, p_priv);
2664 keyspan_setup_urbs(serial);
2666 if (s_priv->instat_urb != NULL) {
2667 s_priv->instat_urb->dev = serial->dev;
2668 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2670 dbg("%s - submit instat urb failed %d", __func__,
2673 if (s_priv->indat_urb != NULL) {
2674 s_priv->indat_urb->dev = serial->dev;
2675 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2677 dbg("%s - submit indat urb failed %d", __func__,
2684 static void keyspan_shutdown(struct usb_serial *serial)
2687 struct usb_serial_port *port;
2688 struct keyspan_serial_private *s_priv;
2689 struct keyspan_port_private *p_priv;
2691 dbg("%s", __func__);
2693 s_priv = usb_get_serial_data(serial);
2695 /* Stop reading/writing urbs */
2696 stop_urb(s_priv->instat_urb);
2697 stop_urb(s_priv->glocont_urb);
2698 stop_urb(s_priv->indat_urb);
2699 for (i = 0; i < serial->num_ports; ++i) {
2700 port = serial->port[i];
2701 p_priv = usb_get_serial_port_data(port);
2702 stop_urb(p_priv->inack_urb);
2703 stop_urb(p_priv->outcont_urb);
2704 for (j = 0; j < 2; j++) {
2705 stop_urb(p_priv->in_urbs[j]);
2706 stop_urb(p_priv->out_urbs[j]);
2711 usb_free_urb(s_priv->instat_urb);
2712 usb_free_urb(s_priv->indat_urb);
2713 usb_free_urb(s_priv->glocont_urb);
2714 for (i = 0; i < serial->num_ports; ++i) {
2715 port = serial->port[i];
2716 p_priv = usb_get_serial_port_data(port);
2717 usb_free_urb(p_priv->inack_urb);
2718 usb_free_urb(p_priv->outcont_urb);
2719 for (j = 0; j < 2; j++) {
2720 usb_free_urb(p_priv->in_urbs[j]);
2721 usb_free_urb(p_priv->out_urbs[j]);
2725 /* dbg("Freeing serial->private."); */
2728 /* dbg("Freeing port->private."); */
2729 /* Now free per port private data */
2730 for (i = 0; i < serial->num_ports; i++) {
2731 port = serial->port[i];
2732 kfree(usb_get_serial_port_data(port));
2736 MODULE_AUTHOR(DRIVER_AUTHOR);
2737 MODULE_DESCRIPTION(DRIVER_DESC);
2738 MODULE_LICENSE("GPL");
2740 MODULE_FIRMWARE("keyspan/usa28.fw");
2741 MODULE_FIRMWARE("keyspan/usa28x.fw");
2742 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2743 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2744 MODULE_FIRMWARE("keyspan/usa19.fw");
2745 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2746 MODULE_FIRMWARE("keyspan/mpr.fw");
2747 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2748 MODULE_FIRMWARE("keyspan/usa18x.fw");
2749 MODULE_FIRMWARE("keyspan/usa19w.fw");
2750 MODULE_FIRMWARE("keyspan/usa49w.fw");
2751 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2753 module_param(debug, bool, S_IRUGO | S_IWUSR);
2754 MODULE_PARM_DESC(debug, "Debug enabled or not");