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 <asm/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 on 1 EP */
136 /* 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 */
190 /* Include Keyspan message headers. All current Keyspan Adapters
191 make use of one of five message formats which are referred
192 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and 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_rx_throttle (struct usb_serial_port *port)
249 dbg("%s - port %d", __func__, port->number);
253 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
255 dbg("%s - port %d", __func__, port->number);
259 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
261 struct keyspan_port_private *p_priv;
265 p_priv = usb_get_serial_port_data(port);
267 if (break_state == -1)
268 p_priv->break_on = 1;
270 p_priv->break_on = 0;
272 keyspan_send_setup(port, 0);
276 static void keyspan_set_termios (struct usb_serial_port *port,
277 struct ktermios *old_termios)
279 int baud_rate, device_port;
280 struct keyspan_port_private *p_priv;
281 const struct keyspan_device_details *d_details;
283 struct tty_struct *tty = port->tty;
287 p_priv = usb_get_serial_port_data(port);
288 d_details = p_priv->device_details;
289 cflag = tty->termios->c_cflag;
290 device_port = port->number - port->serial->minor;
292 /* Baud rate calculation takes baud rate as an integer
293 so other rates can be generated if desired. */
294 baud_rate = tty_get_baud_rate(tty);
295 /* If no match or invalid, don't change */
296 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
297 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
298 /* FIXME - more to do here to ensure rate changes cleanly */
299 /* FIXME - calcuate exact rate from divisor ? */
300 p_priv->baud = baud_rate;
302 baud_rate = tty_termios_baud_rate(old_termios);
304 tty_encode_baud_rate(tty, baud_rate, baud_rate);
305 /* set CTS/RTS handshake etc. */
306 p_priv->cflag = cflag;
307 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
309 /* Mark/Space not supported */
310 tty->termios->c_cflag &= ~CMSPAR;
312 keyspan_send_setup(port, 0);
315 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
318 struct keyspan_port_private *p_priv;
320 p_priv = usb_get_serial_port_data(port);
322 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
323 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
324 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
325 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
326 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
327 ((p_priv->ri_state) ? TIOCM_RNG : 0);
332 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
333 unsigned int set, unsigned int clear)
335 struct keyspan_port_private *p_priv;
337 p_priv = usb_get_serial_port_data(port);
340 p_priv->rts_state = 1;
342 p_priv->dtr_state = 1;
344 if (clear & TIOCM_RTS)
345 p_priv->rts_state = 0;
346 if (clear & TIOCM_DTR)
347 p_priv->dtr_state = 0;
348 keyspan_send_setup(port, 0);
352 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
353 unsigned int cmd, unsigned long arg)
358 /* Write function is similar for the four protocols used
359 with only a minor change for usa90 (usa19hs) required */
360 static int keyspan_write(struct usb_serial_port *port,
361 const unsigned char *buf, int count)
363 struct keyspan_port_private *p_priv;
364 const struct keyspan_device_details *d_details;
367 struct urb *this_urb;
368 int err, maxDataLen, dataOffset;
370 p_priv = usb_get_serial_port_data(port);
371 d_details = p_priv->device_details;
373 if (d_details->msg_format == msg_usa90) {
381 dbg("%s - for port %d (%d chars), flip=%d",
382 __func__, port->number, count, p_priv->out_flip);
384 for (left = count; left > 0; left -= todo) {
386 if (todo > maxDataLen)
389 flip = p_priv->out_flip;
391 /* Check we have a valid urb/endpoint before we use it... */
392 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
393 /* no bulk out, so return 0 bytes written */
394 dbg("%s - no output urb :(", __func__);
398 dbg("%s - endpoint %d flip %d", __func__, usb_pipeendpoint(this_urb->pipe), flip);
400 if (this_urb->status == -EINPROGRESS) {
401 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
403 usb_unlink_urb(this_urb);
407 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
408 for now so set to zero */
409 ((char *)this_urb->transfer_buffer)[0] = 0;
411 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
414 /* send the data out the bulk port */
415 this_urb->transfer_buffer_length = todo + dataOffset;
417 this_urb->dev = port->serial->dev;
418 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
419 dbg("usb_submit_urb(write bulk) failed (%d)", err);
421 p_priv->tx_start_time[flip] = jiffies;
423 /* Flip for next time if usa26 or usa28 interface
424 (not used on usa49) */
425 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
431 static void usa26_indat_callback(struct urb *urb)
435 struct usb_serial_port *port;
436 struct tty_struct *tty;
437 unsigned char *data = urb->transfer_buffer;
438 int status = urb->status;
440 dbg ("%s", __func__);
442 endpoint = usb_pipeendpoint(urb->pipe);
445 dbg("%s - nonzero status: %x on endpoint %d.",
446 __func__, status, endpoint);
452 if (tty && urb->actual_length) {
453 /* 0x80 bit is error flag */
454 if ((data[0] & 0x80) == 0) {
455 /* no errors on individual bytes, only possible overrun err*/
456 if (data[0] & RXERROR_OVERRUN)
459 for (i = 1; i < urb->actual_length ; ++i) {
460 tty_insert_flip_char(tty, data[i], err);
463 /* some bytes had errors, every byte has status */
464 dbg("%s - RX error!!!!", __func__);
465 for (i = 0; i + 1 < urb->actual_length; i += 2) {
466 int stat = data[i], flag = 0;
467 if (stat & RXERROR_OVERRUN)
469 if (stat & RXERROR_FRAMING)
471 if (stat & RXERROR_PARITY)
473 /* XXX should handle break (0x10) */
474 tty_insert_flip_char(tty, data[i+1], flag);
477 tty_flip_buffer_push(tty);
480 /* Resubmit urb so we continue receiving */
481 urb->dev = port->serial->dev;
482 if (port->open_count)
483 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
484 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
489 /* Outdat handling is common for all devices */
490 static void usa2x_outdat_callback(struct urb *urb)
492 struct usb_serial_port *port;
493 struct keyspan_port_private *p_priv;
496 p_priv = usb_get_serial_port_data(port);
497 dbg ("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
499 if (port->open_count)
500 usb_serial_port_softint(port);
503 static void usa26_inack_callback(struct urb *urb)
505 dbg ("%s", __func__);
509 static void usa26_outcont_callback(struct urb *urb)
511 struct usb_serial_port *port;
512 struct keyspan_port_private *p_priv;
515 p_priv = usb_get_serial_port_data(port);
517 if (p_priv->resend_cont) {
518 dbg ("%s - sending setup", __func__);
519 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
523 static void usa26_instat_callback(struct urb *urb)
525 unsigned char *data = urb->transfer_buffer;
526 struct keyspan_usa26_portStatusMessage *msg;
527 struct usb_serial *serial;
528 struct usb_serial_port *port;
529 struct keyspan_port_private *p_priv;
530 int old_dcd_state, err;
531 int status = urb->status;
533 serial = urb->context;
536 dbg("%s - nonzero status: %x", __func__, status);
539 if (urb->actual_length != 9) {
540 dbg("%s - %d byte report??", __func__, urb->actual_length);
544 msg = (struct keyspan_usa26_portStatusMessage *)data;
547 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
548 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
549 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
552 /* Now do something useful with the data */
555 /* Check port number from message and retrieve private data */
556 if (msg->port >= serial->num_ports) {
557 dbg ("%s - Unexpected port number %d", __func__, msg->port);
560 port = serial->port[msg->port];
561 p_priv = usb_get_serial_port_data(port);
563 /* Update handshaking pin state information */
564 old_dcd_state = p_priv->dcd_state;
565 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
566 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
567 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
568 p_priv->ri_state = ((msg->ri) ? 1 : 0);
570 if (port->tty && !C_CLOCAL(port->tty)
571 && old_dcd_state != p_priv->dcd_state) {
573 tty_hangup(port->tty);
575 /* wake_up_interruptible(&p_priv->open_wait); */
578 /* Resubmit urb so we continue receiving */
579 urb->dev = serial->dev;
580 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
581 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
586 static void usa26_glocont_callback(struct urb *urb)
588 dbg ("%s", __func__);
593 static void usa28_indat_callback(struct urb *urb)
596 struct usb_serial_port *port;
597 struct tty_struct *tty;
599 struct keyspan_port_private *p_priv;
600 int status = urb->status;
602 dbg ("%s", __func__);
605 p_priv = usb_get_serial_port_data(port);
606 data = urb->transfer_buffer;
608 if (urb != p_priv->in_urbs[p_priv->in_flip])
613 dbg("%s - nonzero status: %x on endpoint %d.",
614 __func__, status, usb_pipeendpoint(urb->pipe));
619 p_priv = usb_get_serial_port_data(port);
620 data = urb->transfer_buffer;
623 if (urb->actual_length) {
624 for (i = 0; i < urb->actual_length ; ++i) {
625 tty_insert_flip_char(tty, data[i], 0);
627 tty_flip_buffer_push(tty);
630 /* Resubmit urb so we continue receiving */
631 urb->dev = port->serial->dev;
632 if (port->open_count)
633 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
634 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
636 p_priv->in_flip ^= 1;
638 urb = p_priv->in_urbs[p_priv->in_flip];
639 } while (urb->status != -EINPROGRESS);
642 static void usa28_inack_callback(struct urb *urb)
644 dbg ("%s", __func__);
647 static void usa28_outcont_callback(struct urb *urb)
649 struct usb_serial_port *port;
650 struct keyspan_port_private *p_priv;
653 p_priv = usb_get_serial_port_data(port);
655 if (p_priv->resend_cont) {
656 dbg ("%s - sending setup", __func__);
657 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
661 static void usa28_instat_callback(struct urb *urb)
664 unsigned char *data = urb->transfer_buffer;
665 struct keyspan_usa28_portStatusMessage *msg;
666 struct usb_serial *serial;
667 struct usb_serial_port *port;
668 struct keyspan_port_private *p_priv;
670 int status = urb->status;
672 serial = urb->context;
675 dbg("%s - nonzero status: %x", __func__, status);
679 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
680 dbg("%s - bad length %d", __func__, urb->actual_length);
684 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
685 data[0], data[1], data[2], data[3], data[4], data[5],
686 data[6], data[7], data[8], data[9], data[10], data[11]);*/
688 /* Now do something useful with the data */
689 msg = (struct keyspan_usa28_portStatusMessage *)data;
692 /* Check port number from message and retrieve private data */
693 if (msg->port >= serial->num_ports) {
694 dbg ("%s - Unexpected port number %d", __func__, msg->port);
697 port = serial->port[msg->port];
698 p_priv = usb_get_serial_port_data(port);
700 /* Update handshaking pin state information */
701 old_dcd_state = p_priv->dcd_state;
702 p_priv->cts_state = ((msg->cts) ? 1 : 0);
703 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
704 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
705 p_priv->ri_state = ((msg->ri) ? 1 : 0);
707 if (port->tty && !C_CLOCAL(port->tty)
708 && old_dcd_state != p_priv->dcd_state) {
710 tty_hangup(port->tty);
712 /* wake_up_interruptible(&p_priv->open_wait); */
715 /* Resubmit urb so we continue receiving */
716 urb->dev = serial->dev;
717 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
718 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
723 static void usa28_glocont_callback(struct urb *urb)
725 dbg ("%s", __func__);
729 static void usa49_glocont_callback(struct urb *urb)
731 struct usb_serial *serial;
732 struct usb_serial_port *port;
733 struct keyspan_port_private *p_priv;
736 dbg ("%s", __func__);
738 serial = urb->context;
739 for (i = 0; i < serial->num_ports; ++i) {
740 port = serial->port[i];
741 p_priv = usb_get_serial_port_data(port);
743 if (p_priv->resend_cont) {
744 dbg ("%s - sending setup", __func__);
745 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
751 /* This is actually called glostat in the Keyspan
753 static void usa49_instat_callback(struct urb *urb)
756 unsigned char *data = urb->transfer_buffer;
757 struct keyspan_usa49_portStatusMessage *msg;
758 struct usb_serial *serial;
759 struct usb_serial_port *port;
760 struct keyspan_port_private *p_priv;
762 int status = urb->status;
764 dbg ("%s", __func__);
766 serial = urb->context;
769 dbg("%s - nonzero status: %x", __func__, status);
773 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
774 dbg("%s - bad length %d", __func__, urb->actual_length);
778 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
779 data[0], data[1], data[2], data[3], data[4], data[5],
780 data[6], data[7], data[8], data[9], data[10]);*/
782 /* Now do something useful with the data */
783 msg = (struct keyspan_usa49_portStatusMessage *)data;
785 /* Check port number from message and retrieve private data */
786 if (msg->portNumber >= serial->num_ports) {
787 dbg ("%s - Unexpected port number %d", __func__, msg->portNumber);
790 port = serial->port[msg->portNumber];
791 p_priv = usb_get_serial_port_data(port);
793 /* Update handshaking pin state information */
794 old_dcd_state = p_priv->dcd_state;
795 p_priv->cts_state = ((msg->cts) ? 1 : 0);
796 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
797 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
798 p_priv->ri_state = ((msg->ri) ? 1 : 0);
800 if (port->tty && !C_CLOCAL(port->tty)
801 && old_dcd_state != p_priv->dcd_state) {
803 tty_hangup(port->tty);
805 /* wake_up_interruptible(&p_priv->open_wait); */
808 /* Resubmit urb so we continue receiving */
809 urb->dev = serial->dev;
811 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
812 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
817 static void usa49_inack_callback(struct urb *urb)
819 dbg ("%s", __func__);
822 static void usa49_indat_callback(struct urb *urb)
826 struct usb_serial_port *port;
827 struct tty_struct *tty;
828 unsigned char *data = urb->transfer_buffer;
829 int status = urb->status;
831 dbg ("%s", __func__);
833 endpoint = usb_pipeendpoint(urb->pipe);
836 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
843 if (tty && urb->actual_length) {
844 /* 0x80 bit is error flag */
845 if ((data[0] & 0x80) == 0) {
846 /* no error on any byte */
847 for (i = 1; i < urb->actual_length ; ++i) {
848 tty_insert_flip_char(tty, data[i], 0);
851 /* some bytes had errors, every byte has status */
852 for (i = 0; i + 1 < urb->actual_length; i += 2) {
853 int stat = data[i], flag = 0;
854 if (stat & RXERROR_OVERRUN)
856 if (stat & RXERROR_FRAMING)
858 if (stat & RXERROR_PARITY)
860 /* XXX should handle break (0x10) */
861 tty_insert_flip_char(tty, data[i+1], flag);
864 tty_flip_buffer_push(tty);
867 /* Resubmit urb so we continue receiving */
868 urb->dev = port->serial->dev;
869 if (port->open_count)
870 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
871 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
875 static void usa49wg_indat_callback(struct urb *urb)
878 struct usb_serial *serial;
879 struct usb_serial_port *port;
880 struct tty_struct *tty;
881 unsigned char *data = urb->transfer_buffer;
882 int status = urb->status;
884 dbg ("%s", __func__);
886 serial = urb->context;
889 dbg("%s - nonzero status: %x", __func__, status);
893 /* inbound data is in the form P#, len, status, data */
897 if (urb->actual_length) {
898 while (i < urb->actual_length) {
900 /* Check port number from message*/
901 if (data[i] >= serial->num_ports) {
902 dbg ("%s - Unexpected port number %d",
906 port = serial->port[data[i++]];
910 /* 0x80 bit is error flag */
911 if ((data[i] & 0x80) == 0) {
912 /* no error on any byte */
914 for (x = 1; x < len ; ++x)
915 if (port->open_count)
916 tty_insert_flip_char(tty,
922 * some bytes had errors, every byte has status
924 for (x = 0; x + 1 < len; x += 2) {
925 int stat = data[i], flag = 0;
926 if (stat & RXERROR_OVERRUN)
928 if (stat & RXERROR_FRAMING)
930 if (stat & RXERROR_PARITY)
932 /* XXX should handle break (0x10) */
933 if (port->open_count)
934 tty_insert_flip_char(tty,
939 if (port->open_count)
940 tty_flip_buffer_push(tty);
944 /* Resubmit urb so we continue receiving */
945 urb->dev = serial->dev;
947 err = usb_submit_urb(urb, GFP_ATOMIC);
949 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
952 /* not used, usa-49 doesn't have per-port control endpoints */
953 static void usa49_outcont_callback(struct urb *urb)
955 dbg ("%s", __func__);
958 static void usa90_indat_callback(struct urb *urb)
962 struct usb_serial_port *port;
963 struct keyspan_port_private *p_priv;
964 struct tty_struct *tty;
965 unsigned char *data = urb->transfer_buffer;
966 int status = urb->status;
968 dbg ("%s", __func__);
970 endpoint = usb_pipeendpoint(urb->pipe);
973 dbg("%s - nonzero status: %x on endpoint %d.",
974 __func__, status, endpoint);
979 p_priv = usb_get_serial_port_data(port);
982 if (urb->actual_length) {
984 /* if current mode is DMA, looks like usa28 format
985 otherwise looks like usa26 data format */
987 if (p_priv->baud > 57600) {
988 for (i = 0; i < urb->actual_length ; ++i)
989 tty_insert_flip_char(tty, data[i], 0);
993 /* 0x80 bit is error flag */
994 if ((data[0] & 0x80) == 0) {
995 /* no errors on individual bytes, only possible overrun err*/
996 if (data[0] & RXERROR_OVERRUN)
999 for (i = 1; i < urb->actual_length ; ++i)
1000 tty_insert_flip_char(tty, data[i], err);
1004 /* some bytes had errors, every byte has status */
1005 dbg("%s - RX error!!!!", __func__);
1006 for (i = 0; i + 1 < urb->actual_length; i += 2) {
1007 int stat = data[i], flag = 0;
1008 if (stat & RXERROR_OVERRUN)
1009 flag |= TTY_OVERRUN;
1010 if (stat & RXERROR_FRAMING)
1012 if (stat & RXERROR_PARITY)
1014 /* XXX should handle break (0x10) */
1015 tty_insert_flip_char(tty, data[i+1], flag);
1019 tty_flip_buffer_push(tty);
1022 /* Resubmit urb so we continue receiving */
1023 urb->dev = port->serial->dev;
1024 if (port->open_count)
1025 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1026 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1032 static void usa90_instat_callback(struct urb *urb)
1034 unsigned char *data = urb->transfer_buffer;
1035 struct keyspan_usa90_portStatusMessage *msg;
1036 struct usb_serial *serial;
1037 struct usb_serial_port *port;
1038 struct keyspan_port_private *p_priv;
1039 int old_dcd_state, err;
1040 int status = urb->status;
1042 serial = urb->context;
1045 dbg("%s - nonzero status: %x", __func__, status);
1048 if (urb->actual_length < 14) {
1049 dbg("%s - %d byte report??", __func__, urb->actual_length);
1053 msg = (struct keyspan_usa90_portStatusMessage *)data;
1055 /* Now do something useful with the data */
1057 port = serial->port[0];
1058 p_priv = usb_get_serial_port_data(port);
1060 /* Update handshaking pin state information */
1061 old_dcd_state = p_priv->dcd_state;
1062 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1063 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1064 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1065 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1067 if (port->tty && !C_CLOCAL(port->tty)
1068 && old_dcd_state != p_priv->dcd_state) {
1070 tty_hangup(port->tty);
1072 /* wake_up_interruptible(&p_priv->open_wait); */
1075 /* Resubmit urb so we continue receiving */
1076 urb->dev = serial->dev;
1077 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1078 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1084 static void usa90_outcont_callback(struct urb *urb)
1086 struct usb_serial_port *port;
1087 struct keyspan_port_private *p_priv;
1089 port = urb->context;
1090 p_priv = usb_get_serial_port_data(port);
1092 if (p_priv->resend_cont) {
1093 dbg ("%s - sending setup", __func__);
1094 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1098 /* Status messages from the 28xg */
1099 static void usa67_instat_callback(struct urb *urb)
1102 unsigned char *data = urb->transfer_buffer;
1103 struct keyspan_usa67_portStatusMessage *msg;
1104 struct usb_serial *serial;
1105 struct usb_serial_port *port;
1106 struct keyspan_port_private *p_priv;
1108 int status = urb->status;
1110 dbg ("%s", __func__);
1112 serial = urb->context;
1115 dbg("%s - nonzero status: %x", __func__, status);
1119 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1120 dbg("%s - bad length %d", __func__, urb->actual_length);
1125 /* Now do something useful with the data */
1126 msg = (struct keyspan_usa67_portStatusMessage *)data;
1128 /* Check port number from message and retrieve private data */
1129 if (msg->port >= serial->num_ports) {
1130 dbg ("%s - Unexpected port number %d", __func__, msg->port);
1134 port = serial->port[msg->port];
1135 p_priv = usb_get_serial_port_data(port);
1137 /* Update handshaking pin state information */
1138 old_dcd_state = p_priv->dcd_state;
1139 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1140 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1142 if (port->tty && !C_CLOCAL(port->tty)
1143 && old_dcd_state != p_priv->dcd_state) {
1145 tty_hangup(port->tty);
1147 /* wake_up_interruptible(&p_priv->open_wait); */
1150 /* Resubmit urb so we continue receiving */
1151 urb->dev = serial->dev;
1152 err = usb_submit_urb(urb, GFP_ATOMIC);
1154 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1157 static void usa67_glocont_callback(struct urb *urb)
1159 struct usb_serial *serial;
1160 struct usb_serial_port *port;
1161 struct keyspan_port_private *p_priv;
1164 dbg ("%s", __func__);
1166 serial = urb->context;
1167 for (i = 0; i < serial->num_ports; ++i) {
1168 port = serial->port[i];
1169 p_priv = usb_get_serial_port_data(port);
1171 if (p_priv->resend_cont) {
1172 dbg ("%s - sending setup", __func__);
1173 keyspan_usa67_send_setup(serial, port,
1174 p_priv->resend_cont - 1);
1180 static int keyspan_write_room (struct usb_serial_port *port)
1182 struct keyspan_port_private *p_priv;
1183 const struct keyspan_device_details *d_details;
1186 struct urb *this_urb;
1188 dbg("%s", __func__);
1189 p_priv = usb_get_serial_port_data(port);
1190 d_details = p_priv->device_details;
1192 /* FIXME: locking */
1193 if (d_details->msg_format == msg_usa90)
1198 flip = p_priv->out_flip;
1200 /* Check both endpoints to see if any are available. */
1201 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1202 if (this_urb->status != -EINPROGRESS)
1204 flip = (flip + 1) & d_details->outdat_endp_flip;
1205 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1206 if (this_urb->status != -EINPROGRESS)
1213 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1219 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1221 struct keyspan_port_private *p_priv;
1222 struct keyspan_serial_private *s_priv;
1223 struct usb_serial *serial = port->serial;
1224 const struct keyspan_device_details *d_details;
1226 int baud_rate, device_port;
1230 s_priv = usb_get_serial_data(serial);
1231 p_priv = usb_get_serial_port_data(port);
1232 d_details = p_priv->device_details;
1234 dbg("%s - port%d.", __func__, port->number);
1236 /* Set some sane defaults */
1237 p_priv->rts_state = 1;
1238 p_priv->dtr_state = 1;
1239 p_priv->baud = 9600;
1241 /* force baud and lcr to be set on open */
1242 p_priv->old_baud = 0;
1243 p_priv->old_cflag = 0;
1245 p_priv->out_flip = 0;
1246 p_priv->in_flip = 0;
1248 /* Reset low level data toggle and start reading from endpoints */
1249 for (i = 0; i < 2; i++) {
1250 if ((urb = p_priv->in_urbs[i]) == NULL)
1252 urb->dev = serial->dev;
1254 /* make sure endpoint data toggle is synchronized with the device */
1256 usb_clear_halt(urb->dev, urb->pipe);
1258 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1259 dbg("%s - submit urb %d failed (%d)", __func__, i, err);
1263 /* Reset low level data toggle on out endpoints */
1264 for (i = 0; i < 2; i++) {
1265 if ((urb = p_priv->out_urbs[i]) == NULL)
1267 urb->dev = serial->dev;
1268 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1271 /* get the terminal config for the setup message now so we don't
1272 * need to send 2 of them */
1274 cflag = port->tty->termios->c_cflag;
1275 device_port = port->number - port->serial->minor;
1277 /* Baud rate calculation takes baud rate as an integer
1278 so other rates can be generated if desired. */
1279 baud_rate = tty_get_baud_rate(port->tty);
1280 /* If no match or invalid, leave as default */
1282 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1283 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1284 p_priv->baud = baud_rate;
1287 /* set CTS/RTS handshake etc. */
1288 p_priv->cflag = cflag;
1289 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1291 keyspan_send_setup(port, 1);
1293 //keyspan_set_termios(port, NULL);
1298 static inline void stop_urb(struct urb *urb)
1300 if (urb && urb->status == -EINPROGRESS)
1304 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1307 struct usb_serial *serial = port->serial;
1308 struct keyspan_serial_private *s_priv;
1309 struct keyspan_port_private *p_priv;
1311 dbg("%s", __func__);
1312 s_priv = usb_get_serial_data(serial);
1313 p_priv = usb_get_serial_port_data(port);
1315 p_priv->rts_state = 0;
1316 p_priv->dtr_state = 0;
1319 keyspan_send_setup(port, 2);
1320 /* pilot-xfer seems to work best with this delay */
1322 // keyspan_set_termios(port, NULL);
1325 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1326 dbg("%s - urb in progress", __func__);
1329 p_priv->out_flip = 0;
1330 p_priv->in_flip = 0;
1333 /* Stop reading/writing urbs */
1334 stop_urb(p_priv->inack_urb);
1335 /* stop_urb(p_priv->outcont_urb); */
1336 for (i = 0; i < 2; i++) {
1337 stop_urb(p_priv->in_urbs[i]);
1338 stop_urb(p_priv->out_urbs[i]);
1344 /* download the firmware to a pre-renumeration device */
1345 static int keyspan_fake_startup (struct usb_serial *serial)
1348 const struct ihex_binrec *record;
1350 const struct firmware *fw;
1352 dbg("Keyspan startup version %04x product %04x",
1353 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1354 le16_to_cpu(serial->dev->descriptor.idProduct));
1356 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1357 dbg("Firmware already loaded. Quitting.");
1361 /* Select firmware image on the basis of idProduct */
1362 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1363 case keyspan_usa28_pre_product_id:
1364 fw_name = "keyspan/usa28.fw";
1367 case keyspan_usa28x_pre_product_id:
1368 fw_name = "keyspan/usa28x.fw";
1371 case keyspan_usa28xa_pre_product_id:
1372 fw_name = "keyspan/usa28xa.fw";
1375 case keyspan_usa28xb_pre_product_id:
1376 fw_name = "keyspan/usa28xb.fw";
1379 case keyspan_usa19_pre_product_id:
1380 fw_name = "keyspan/usa19.fw";
1383 case keyspan_usa19qi_pre_product_id:
1384 fw_name = "keyspan/usa19qi.fw";
1387 case keyspan_mpr_pre_product_id:
1388 fw_name = "keyspan/mpr.fw";
1391 case keyspan_usa19qw_pre_product_id:
1392 fw_name = "keyspan/usa19qw.fw";
1395 case keyspan_usa18x_pre_product_id:
1396 fw_name = "keyspan/usa18x.fw";
1399 case keyspan_usa19w_pre_product_id:
1400 fw_name = "keyspan/usa19w.fw";
1403 case keyspan_usa49w_pre_product_id:
1404 fw_name = "keyspan/usa49w.fw";
1407 case keyspan_usa49wlc_pre_product_id:
1408 fw_name = "keyspan/usa49wlc.fw";
1412 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1413 le16_to_cpu(serial->dev->descriptor.idProduct));
1417 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1418 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1422 dbg("Uploading Keyspan %s firmware.", fw_name);
1424 /* download the firmware image */
1425 response = ezusb_set_reset(serial, 1);
1427 record = (const struct ihex_binrec *)fw->data;
1430 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1431 (unsigned char *)record->data,
1432 be16_to_cpu(record->len), 0xa0);
1434 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1435 "firmware (%d %04X %p %d)\n",
1436 response, be32_to_cpu(record->addr),
1437 record->data, be16_to_cpu(record->len));
1440 record = ihex_next_binrec(record);
1442 release_firmware(fw);
1443 /* bring device out of reset. Renumeration will occur in a
1444 moment and the new device will bind to the real driver */
1445 response = ezusb_set_reset(serial, 0);
1447 /* we don't want this device to have a driver assigned to it. */
1451 /* Helper functions used by keyspan_setup_urbs */
1452 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1455 struct usb_host_interface *iface_desc;
1456 struct usb_endpoint_descriptor *ep;
1459 iface_desc = serial->interface->cur_altsetting;
1460 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1461 ep = &iface_desc->endpoint[i].desc;
1462 if (ep->bEndpointAddress == endpoint)
1465 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1466 "endpoint %x\n", endpoint);
1470 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1471 int dir, void *ctx, char *buf, int len,
1472 void (*callback)(struct urb *))
1475 struct usb_endpoint_descriptor const *ep_desc;
1476 char const *ep_type_name;
1479 return NULL; /* endpoint not needed */
1481 dbg ("%s - alloc for endpoint %d.", __func__, endpoint);
1482 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1484 dbg ("%s - alloc for endpoint %d failed.", __func__, endpoint);
1488 if (endpoint == 0) {
1489 /* control EP filled in when used */
1493 ep_desc = find_ep(serial, endpoint);
1495 /* leak the urb, something's wrong and the callers don't care */
1498 if (usb_endpoint_xfer_int(ep_desc)) {
1499 ep_type_name = "INT";
1500 usb_fill_int_urb(urb, serial->dev,
1501 usb_sndintpipe(serial->dev, endpoint) | dir,
1502 buf, len, callback, ctx,
1503 ep_desc->bInterval);
1504 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1505 ep_type_name = "BULK";
1506 usb_fill_bulk_urb(urb, serial->dev,
1507 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1508 buf, len, callback, ctx);
1510 dev_warn(&serial->interface->dev,
1511 "unsupported endpoint type %x\n",
1512 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1517 dbg("%s - using urb %p for %s endpoint %x",
1518 __func__, urb, ep_type_name, endpoint);
1522 static struct callbacks {
1523 void (*instat_callback)(struct urb *);
1524 void (*glocont_callback)(struct urb *);
1525 void (*indat_callback)(struct urb *);
1526 void (*outdat_callback)(struct urb *);
1527 void (*inack_callback)(struct urb *);
1528 void (*outcont_callback)(struct urb *);
1529 } keyspan_callbacks[] = {
1531 /* msg_usa26 callbacks */
1532 .instat_callback = usa26_instat_callback,
1533 .glocont_callback = usa26_glocont_callback,
1534 .indat_callback = usa26_indat_callback,
1535 .outdat_callback = usa2x_outdat_callback,
1536 .inack_callback = usa26_inack_callback,
1537 .outcont_callback = usa26_outcont_callback,
1539 /* msg_usa28 callbacks */
1540 .instat_callback = usa28_instat_callback,
1541 .glocont_callback = usa28_glocont_callback,
1542 .indat_callback = usa28_indat_callback,
1543 .outdat_callback = usa2x_outdat_callback,
1544 .inack_callback = usa28_inack_callback,
1545 .outcont_callback = usa28_outcont_callback,
1547 /* msg_usa49 callbacks */
1548 .instat_callback = usa49_instat_callback,
1549 .glocont_callback = usa49_glocont_callback,
1550 .indat_callback = usa49_indat_callback,
1551 .outdat_callback = usa2x_outdat_callback,
1552 .inack_callback = usa49_inack_callback,
1553 .outcont_callback = usa49_outcont_callback,
1555 /* msg_usa90 callbacks */
1556 .instat_callback = usa90_instat_callback,
1557 .glocont_callback = usa28_glocont_callback,
1558 .indat_callback = usa90_indat_callback,
1559 .outdat_callback = usa2x_outdat_callback,
1560 .inack_callback = usa28_inack_callback,
1561 .outcont_callback = usa90_outcont_callback,
1563 /* msg_usa67 callbacks */
1564 .instat_callback = usa67_instat_callback,
1565 .glocont_callback = usa67_glocont_callback,
1566 .indat_callback = usa26_indat_callback,
1567 .outdat_callback = usa2x_outdat_callback,
1568 .inack_callback = usa26_inack_callback,
1569 .outcont_callback = usa26_outcont_callback,
1573 /* Generic setup urbs function that uses
1574 data in device_details */
1575 static void keyspan_setup_urbs(struct usb_serial *serial)
1578 struct keyspan_serial_private *s_priv;
1579 const struct keyspan_device_details *d_details;
1580 struct usb_serial_port *port;
1581 struct keyspan_port_private *p_priv;
1582 struct callbacks *cback;
1585 dbg ("%s", __func__);
1587 s_priv = usb_get_serial_data(serial);
1588 d_details = s_priv->device_details;
1590 /* Setup values for the various callback routines */
1591 cback = &keyspan_callbacks[d_details->msg_format];
1593 /* Allocate and set up urbs for each one that is in use,
1594 starting with instat endpoints */
1595 s_priv->instat_urb = keyspan_setup_urb
1596 (serial, d_details->instat_endpoint, USB_DIR_IN,
1597 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1598 cback->instat_callback);
1600 s_priv->indat_urb = keyspan_setup_urb
1601 (serial, d_details->indat_endpoint, USB_DIR_IN,
1602 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1603 usa49wg_indat_callback);
1605 s_priv->glocont_urb = keyspan_setup_urb
1606 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1607 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1608 cback->glocont_callback);
1610 /* Setup endpoints for each port specific thing */
1611 for (i = 0; i < d_details->num_ports; i ++) {
1612 port = serial->port[i];
1613 p_priv = usb_get_serial_port_data(port);
1615 /* Do indat endpoints first, once for each flip */
1616 endp = d_details->indat_endpoints[i];
1617 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1618 p_priv->in_urbs[j] = keyspan_setup_urb
1619 (serial, endp, USB_DIR_IN, port,
1620 p_priv->in_buffer[j], 64,
1621 cback->indat_callback);
1624 p_priv->in_urbs[j] = NULL;
1626 /* outdat endpoints also have flip */
1627 endp = d_details->outdat_endpoints[i];
1628 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1629 p_priv->out_urbs[j] = keyspan_setup_urb
1630 (serial, endp, USB_DIR_OUT, port,
1631 p_priv->out_buffer[j], 64,
1632 cback->outdat_callback);
1635 p_priv->out_urbs[j] = NULL;
1637 /* inack endpoint */
1638 p_priv->inack_urb = keyspan_setup_urb
1639 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1640 port, p_priv->inack_buffer, 1, cback->inack_callback);
1642 /* outcont endpoint */
1643 p_priv->outcont_urb = keyspan_setup_urb
1644 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1645 port, p_priv->outcont_buffer, 64,
1646 cback->outcont_callback);
1651 /* usa19 function doesn't require prescaler */
1652 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1653 u8 *rate_low, u8 *prescaler, int portnum)
1655 u32 b16, /* baud rate times 16 (actual rate used internally) */
1657 cnt; /* inverse of divisor (programmed into 8051) */
1659 dbg ("%s - %d.", __func__, baud_rate);
1661 /* prevent divide by zero... */
1662 if( (b16 = (baud_rate * 16L)) == 0) {
1663 return (KEYSPAN_INVALID_BAUD_RATE);
1666 /* Any "standard" rate over 57k6 is marginal on the USA-19
1667 as we run out of divisor resolution. */
1668 if (baud_rate > 57600) {
1669 return (KEYSPAN_INVALID_BAUD_RATE);
1672 /* calculate the divisor and the counter (its inverse) */
1673 if( (div = (baudclk / b16)) == 0) {
1674 return (KEYSPAN_INVALID_BAUD_RATE);
1681 return (KEYSPAN_INVALID_BAUD_RATE);
1684 /* return the counter values if non-null */
1686 *rate_low = (u8) (cnt & 0xff);
1689 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1691 if (rate_low && rate_hi) {
1692 dbg ("%s - %d %02x %02x.", __func__, baud_rate, *rate_hi, *rate_low);
1695 return (KEYSPAN_BAUD_RATE_OK);
1698 /* usa19hs function doesn't require prescaler */
1699 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1700 u8 *rate_low, u8 *prescaler, int portnum)
1702 u32 b16, /* baud rate times 16 (actual rate used internally) */
1705 dbg ("%s - %d.", __func__, baud_rate);
1707 /* prevent divide by zero... */
1708 if( (b16 = (baud_rate * 16L)) == 0)
1709 return (KEYSPAN_INVALID_BAUD_RATE);
1713 /* calculate the divisor */
1714 if( (div = (baudclk / b16)) == 0)
1715 return (KEYSPAN_INVALID_BAUD_RATE);
1718 return (KEYSPAN_INVALID_BAUD_RATE);
1720 /* return the counter values if non-null */
1722 *rate_low = (u8) (div & 0xff);
1725 *rate_hi = (u8) ((div >> 8) & 0xff);
1727 if (rate_low && rate_hi)
1728 dbg ("%s - %d %02x %02x.", __func__, baud_rate, *rate_hi, *rate_low);
1730 return (KEYSPAN_BAUD_RATE_OK);
1733 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1734 u8 *rate_low, u8 *prescaler, int portnum)
1736 u32 b16, /* baud rate times 16 (actual rate used internally) */
1737 clk, /* clock with 13/8 prescaler */
1738 div, /* divisor using 13/8 prescaler */
1739 res, /* resulting baud rate using 13/8 prescaler */
1740 diff, /* error using 13/8 prescaler */
1745 dbg ("%s - %d.", __func__, baud_rate);
1747 /* prevent divide by zero */
1748 if( (b16 = baud_rate * 16L) == 0) {
1749 return (KEYSPAN_INVALID_BAUD_RATE);
1752 /* Calculate prescaler by trying them all and looking
1755 /* start with largest possible difference */
1756 smallest_diff = 0xffffffff;
1758 /* 0 is an invalid prescaler, used as a flag */
1761 for(i = 8; i <= 0xff; ++i) {
1762 clk = (baudclk * 8) / (u32) i;
1764 if( (div = clk / b16) == 0) {
1769 diff= (res > b16) ? (res-b16) : (b16-res);
1771 if(diff < smallest_diff) {
1773 smallest_diff = diff;
1777 if(best_prescaler == 0) {
1778 return (KEYSPAN_INVALID_BAUD_RATE);
1781 clk = (baudclk * 8) / (u32) best_prescaler;
1784 /* return the divisor and prescaler if non-null */
1786 *rate_low = (u8) (div & 0xff);
1789 *rate_hi = (u8) ((div >> 8) & 0xff);
1792 *prescaler = best_prescaler;
1793 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1795 return (KEYSPAN_BAUD_RATE_OK);
1798 /* USA-28 supports different maximum baud rates on each port */
1799 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1800 u8 *rate_low, u8 *prescaler, int portnum)
1802 u32 b16, /* baud rate times 16 (actual rate used internally) */
1804 cnt; /* inverse of divisor (programmed into 8051) */
1806 dbg ("%s - %d.", __func__, baud_rate);
1808 /* prevent divide by zero */
1809 if ((b16 = baud_rate * 16L) == 0)
1810 return (KEYSPAN_INVALID_BAUD_RATE);
1812 /* calculate the divisor and the counter (its inverse) */
1813 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1814 return (KEYSPAN_INVALID_BAUD_RATE);
1820 /* check for out of range, based on portnum,
1821 and return result */
1824 return (KEYSPAN_INVALID_BAUD_RATE);
1829 return (KEYSPAN_INVALID_BAUD_RATE);
1833 return (KEYSPAN_INVALID_BAUD_RATE);
1837 /* return the counter values if not NULL
1838 (port 1 will ignore retHi) */
1840 *rate_low = (u8) (cnt & 0xff);
1843 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1845 dbg ("%s - %d OK.", __func__, baud_rate);
1846 return (KEYSPAN_BAUD_RATE_OK);
1849 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1850 struct usb_serial_port *port,
1853 struct keyspan_usa26_portControlMessage msg;
1854 struct keyspan_serial_private *s_priv;
1855 struct keyspan_port_private *p_priv;
1856 const struct keyspan_device_details *d_details;
1858 struct urb *this_urb;
1859 int device_port, err;
1861 dbg ("%s reset=%d", __func__, reset_port);
1863 s_priv = usb_get_serial_data(serial);
1864 p_priv = usb_get_serial_port_data(port);
1865 d_details = s_priv->device_details;
1866 device_port = port->number - port->serial->minor;
1868 outcont_urb = d_details->outcont_endpoints[port->number];
1869 this_urb = p_priv->outcont_urb;
1871 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1873 /* Make sure we have an urb then send the message */
1874 if (this_urb == NULL) {
1875 dbg("%s - oops no urb.", __func__);
1879 /* Save reset port val for resend.
1880 Don't overwrite resend for open/close condition. */
1881 if ((reset_port + 1) > p_priv->resend_cont)
1882 p_priv->resend_cont = reset_port + 1;
1883 if (this_urb->status == -EINPROGRESS) {
1884 /* dbg ("%s - already writing", __func__); */
1889 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1891 /* Only set baud rate if it's changed */
1892 if (p_priv->old_baud != p_priv->baud) {
1893 p_priv->old_baud = p_priv->baud;
1894 msg.setClocking = 0xff;
1895 if (d_details->calculate_baud_rate
1896 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1897 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1898 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
1901 msg.baudHi = 125; /* Values for 9600 baud */
1904 msg.setPrescaler = 0xff;
1907 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1908 switch (p_priv->cflag & CSIZE) {
1910 msg.lcr |= USA_DATABITS_5;
1913 msg.lcr |= USA_DATABITS_6;
1916 msg.lcr |= USA_DATABITS_7;
1919 msg.lcr |= USA_DATABITS_8;
1922 if (p_priv->cflag & PARENB) {
1923 /* note USA_PARITY_NONE == 0 */
1924 msg.lcr |= (p_priv->cflag & PARODD)?
1925 USA_PARITY_ODD: USA_PARITY_EVEN;
1929 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1930 msg.xonFlowControl = 0;
1931 msg.setFlowControl = 0xff;
1932 msg.forwardingLength = 16;
1937 if (reset_port == 1) {
1946 msg.returnStatus = 0;
1947 msg.resetDataToggle = 0xff;
1951 else if (reset_port == 2) {
1960 msg.returnStatus = 0;
1961 msg.resetDataToggle = 0;
1964 /* Sending intermediate configs */
1966 msg._txOn = (! p_priv->break_on);
1969 msg.txBreak = (p_priv->break_on);
1974 msg.returnStatus = 0;
1975 msg.resetDataToggle = 0x0;
1978 /* Do handshaking outputs */
1979 msg.setTxTriState_setRts = 0xff;
1980 msg.txTriState_rts = p_priv->rts_state;
1982 msg.setHskoa_setDtr = 0xff;
1983 msg.hskoa_dtr = p_priv->dtr_state;
1985 p_priv->resend_cont = 0;
1986 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1988 /* send the data out the device on control endpoint */
1989 this_urb->transfer_buffer_length = sizeof(msg);
1991 this_urb->dev = serial->dev;
1992 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1993 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1997 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1998 outcont_urb, this_urb->transfer_buffer_length,
1999 usb_pipeendpoint(this_urb->pipe));
2006 static int keyspan_usa28_send_setup(struct usb_serial *serial,
2007 struct usb_serial_port *port,
2010 struct keyspan_usa28_portControlMessage msg;
2011 struct keyspan_serial_private *s_priv;
2012 struct keyspan_port_private *p_priv;
2013 const struct keyspan_device_details *d_details;
2014 struct urb *this_urb;
2015 int device_port, err;
2017 dbg ("%s", __func__);
2019 s_priv = usb_get_serial_data(serial);
2020 p_priv = usb_get_serial_port_data(port);
2021 d_details = s_priv->device_details;
2022 device_port = port->number - port->serial->minor;
2024 /* only do something if we have a bulk out endpoint */
2025 if ((this_urb = p_priv->outcont_urb) == NULL) {
2026 dbg("%s - oops no urb.", __func__);
2030 /* Save reset port val for resend.
2031 Don't overwrite resend for open/close condition. */
2032 if ((reset_port + 1) > p_priv->resend_cont)
2033 p_priv->resend_cont = reset_port + 1;
2034 if (this_urb->status == -EINPROGRESS) {
2035 dbg ("%s already writing", __func__);
2040 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2042 msg.setBaudRate = 1;
2043 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2044 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2045 dbg("%s - Invalid baud rate requested %d.", __func__, p_priv->baud);
2047 msg.baudHi = 0xb2; /* Values for 9600 baud */
2050 /* If parity is enabled, we must calculate it ourselves. */
2051 msg.parity = 0; /* XXX for now */
2053 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2054 msg.xonFlowControl = 0;
2056 /* Do handshaking outputs, DTR is inverted relative to RTS */
2057 msg.rts = p_priv->rts_state;
2058 msg.dtr = p_priv->dtr_state;
2060 msg.forwardingLength = 16;
2062 msg.breakThreshold = 45;
2066 /*msg.returnStatus = 1;
2067 msg.resetDataToggle = 0xff;*/
2069 if (reset_port == 1) {
2073 msg.txForceXoff = 0;
2079 msg.returnStatus = 0;
2080 msg.resetDataToggle = 0xff;
2083 else if (reset_port == 2) {
2087 msg.txForceXoff = 0;
2093 msg.returnStatus = 0;
2094 msg.resetDataToggle = 0;
2096 /* Sending intermediate configs */
2098 msg._txOn = (! p_priv->break_on);
2101 msg.txForceXoff = 0;
2102 msg.txBreak = (p_priv->break_on);
2107 msg.returnStatus = 0;
2108 msg.resetDataToggle = 0x0;
2111 p_priv->resend_cont = 0;
2112 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2114 /* send the data out the device on control endpoint */
2115 this_urb->transfer_buffer_length = sizeof(msg);
2117 this_urb->dev = serial->dev;
2118 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2119 dbg("%s - usb_submit_urb(setup) failed", __func__);
2123 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
2124 this_urb->transfer_buffer_length);
2131 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2132 struct usb_serial_port *port,
2135 struct keyspan_usa49_portControlMessage msg;
2136 struct usb_ctrlrequest *dr = NULL;
2137 struct keyspan_serial_private *s_priv;
2138 struct keyspan_port_private *p_priv;
2139 const struct keyspan_device_details *d_details;
2140 struct urb *this_urb;
2141 int err, device_port;
2143 dbg ("%s", __func__);
2145 s_priv = usb_get_serial_data(serial);
2146 p_priv = usb_get_serial_port_data(port);
2147 d_details = s_priv->device_details;
2149 this_urb = s_priv->glocont_urb;
2151 /* Work out which port within the device is being setup */
2152 device_port = port->number - port->serial->minor;
2154 dbg("%s - endpoint %d port %d (%d)",__func__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2156 /* Make sure we have an urb then send the message */
2157 if (this_urb == NULL) {
2158 dbg("%s - oops no urb for port %d.", __func__, port->number);
2162 /* Save reset port val for resend.
2163 Don't overwrite resend for open/close condition. */
2164 if ((reset_port + 1) > p_priv->resend_cont)
2165 p_priv->resend_cont = reset_port + 1;
2167 if (this_urb->status == -EINPROGRESS) {
2168 /* dbg ("%s - already writing", __func__); */
2173 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2175 /*msg.portNumber = port->number;*/
2176 msg.portNumber = device_port;
2178 /* Only set baud rate if it's changed */
2179 if (p_priv->old_baud != p_priv->baud) {
2180 p_priv->old_baud = p_priv->baud;
2181 msg.setClocking = 0xff;
2182 if (d_details->calculate_baud_rate
2183 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2184 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2185 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2188 msg.baudHi = 125; /* Values for 9600 baud */
2191 //msg.setPrescaler = 0xff;
2194 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2195 switch (p_priv->cflag & CSIZE) {
2197 msg.lcr |= USA_DATABITS_5;
2200 msg.lcr |= USA_DATABITS_6;
2203 msg.lcr |= USA_DATABITS_7;
2206 msg.lcr |= USA_DATABITS_8;
2209 if (p_priv->cflag & PARENB) {
2210 /* note USA_PARITY_NONE == 0 */
2211 msg.lcr |= (p_priv->cflag & PARODD)?
2212 USA_PARITY_ODD: USA_PARITY_EVEN;
2216 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2217 msg.xonFlowControl = 0;
2218 msg.setFlowControl = 0xff;
2220 msg.forwardingLength = 16;
2225 if (reset_port == 1) {
2234 msg.returnStatus = 0;
2235 msg.resetDataToggle = 0xff;
2237 msg.disablePort = 0;
2240 else if (reset_port == 2) {
2249 msg.returnStatus = 0;
2250 msg.resetDataToggle = 0;
2252 msg.disablePort = 1;
2254 /* Sending intermediate configs */
2256 msg._txOn = (! p_priv->break_on);
2259 msg.txBreak = (p_priv->break_on);
2264 msg.returnStatus = 0;
2265 msg.resetDataToggle = 0x0;
2267 msg.disablePort = 0;
2270 /* Do handshaking outputs */
2272 msg.rts = p_priv->rts_state;
2275 msg.dtr = p_priv->dtr_state;
2277 p_priv->resend_cont = 0;
2279 /* if the device is a 49wg, we send control message on usb control EP 0 */
2281 if (d_details->product_id == keyspan_usa49wg_product_id) {
2282 dr = (void *)(s_priv->ctrl_buf);
2283 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2284 dr->bRequest = 0xB0; /* 49wg control message */;
2287 dr->wLength = cpu_to_le16(sizeof(msg));
2289 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2291 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2292 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2293 usa49_glocont_callback, serial);
2296 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2298 /* send the data out the device on control endpoint */
2299 this_urb->transfer_buffer_length = sizeof(msg);
2301 this_urb->dev = serial->dev;
2303 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2304 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2308 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2309 outcont_urb, this_urb->transfer_buffer_length,
2310 usb_pipeendpoint(this_urb->pipe));
2317 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2318 struct usb_serial_port *port,
2321 struct keyspan_usa90_portControlMessage msg;
2322 struct keyspan_serial_private *s_priv;
2323 struct keyspan_port_private *p_priv;
2324 const struct keyspan_device_details *d_details;
2325 struct urb *this_urb;
2329 dbg ("%s", __func__);
2331 s_priv = usb_get_serial_data(serial);
2332 p_priv = usb_get_serial_port_data(port);
2333 d_details = s_priv->device_details;
2335 /* only do something if we have a bulk out endpoint */
2336 if ((this_urb = p_priv->outcont_urb) == NULL) {
2337 dbg("%s - oops no urb.", __func__);
2341 /* Save reset port val for resend.
2342 Don't overwrite resend for open/close condition. */
2343 if ((reset_port + 1) > p_priv->resend_cont)
2344 p_priv->resend_cont = reset_port + 1;
2345 if (this_urb->status == -EINPROGRESS) {
2346 dbg ("%s already writing", __func__);
2351 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2353 /* Only set baud rate if it's changed */
2354 if (p_priv->old_baud != p_priv->baud) {
2355 p_priv->old_baud = p_priv->baud;
2356 msg.setClocking = 0x01;
2357 if (d_details->calculate_baud_rate
2358 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2359 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2360 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2362 p_priv->baud = 9600;
2363 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2364 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2370 /* modes must always be correctly specified */
2371 if (p_priv->baud > 57600)
2373 msg.rxMode = RXMODE_DMA;
2374 msg.txMode = TXMODE_DMA;
2378 msg.rxMode = RXMODE_BYHAND;
2379 msg.txMode = TXMODE_BYHAND;
2382 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2383 switch (p_priv->cflag & CSIZE) {
2385 msg.lcr |= USA_DATABITS_5;
2388 msg.lcr |= USA_DATABITS_6;
2391 msg.lcr |= USA_DATABITS_7;
2394 msg.lcr |= USA_DATABITS_8;
2397 if (p_priv->cflag & PARENB) {
2398 /* note USA_PARITY_NONE == 0 */
2399 msg.lcr |= (p_priv->cflag & PARODD)?
2400 USA_PARITY_ODD: USA_PARITY_EVEN;
2402 if (p_priv->old_cflag != p_priv->cflag) {
2403 p_priv->old_cflag = p_priv->cflag;
2407 if (p_priv->flow_control == flow_cts)
2408 msg.txFlowControl = TXFLOW_CTS;
2409 msg.setTxFlowControl = 0x01;
2410 msg.setRxFlowControl = 0x01;
2412 msg.rxForwardingLength = 16;
2413 msg.rxForwardingTimeout = 16;
2414 msg.txAckSetting = 0;
2419 if (reset_port == 1) {
2420 msg.portEnabled = 1;
2422 msg.txBreak = (p_priv->break_on);
2425 else if (reset_port == 2) {
2426 msg.portEnabled = 0;
2428 /* Sending intermediate configs */
2430 if (port->open_count)
2431 msg.portEnabled = 1;
2432 msg.txBreak = (p_priv->break_on);
2435 /* Do handshaking outputs */
2437 msg.rts = p_priv->rts_state;
2440 msg.dtr = p_priv->dtr_state;
2442 p_priv->resend_cont = 0;
2443 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2445 /* send the data out the device on control endpoint */
2446 this_urb->transfer_buffer_length = sizeof(msg);
2448 this_urb->dev = serial->dev;
2449 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2450 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2455 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2456 struct usb_serial_port *port,
2459 struct keyspan_usa67_portControlMessage msg;
2460 struct keyspan_serial_private *s_priv;
2461 struct keyspan_port_private *p_priv;
2462 const struct keyspan_device_details *d_details;
2463 struct urb *this_urb;
2464 int err, device_port;
2466 dbg ("%s", __func__);
2468 s_priv = usb_get_serial_data(serial);
2469 p_priv = usb_get_serial_port_data(port);
2470 d_details = s_priv->device_details;
2472 this_urb = s_priv->glocont_urb;
2474 /* Work out which port within the device is being setup */
2475 device_port = port->number - port->serial->minor;
2477 /* Make sure we have an urb then send the message */
2478 if (this_urb == NULL) {
2479 dbg("%s - oops no urb for port %d.", __func__,
2484 /* Save reset port val for resend.
2485 Don't overwrite resend for open/close condition. */
2486 if ((reset_port + 1) > p_priv->resend_cont)
2487 p_priv->resend_cont = reset_port + 1;
2488 if (this_urb->status == -EINPROGRESS) {
2489 /* dbg ("%s - already writing", __func__); */
2494 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2496 msg.port = device_port;
2498 /* Only set baud rate if it's changed */
2499 if (p_priv->old_baud != p_priv->baud) {
2500 p_priv->old_baud = p_priv->baud;
2501 msg.setClocking = 0xff;
2502 if (d_details->calculate_baud_rate
2503 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2504 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2505 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2508 msg.baudHi = 125; /* Values for 9600 baud */
2511 msg.setPrescaler = 0xff;
2514 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2515 switch (p_priv->cflag & CSIZE) {
2517 msg.lcr |= USA_DATABITS_5;
2520 msg.lcr |= USA_DATABITS_6;
2523 msg.lcr |= USA_DATABITS_7;
2526 msg.lcr |= USA_DATABITS_8;
2529 if (p_priv->cflag & PARENB) {
2530 /* note USA_PARITY_NONE == 0 */
2531 msg.lcr |= (p_priv->cflag & PARODD)?
2532 USA_PARITY_ODD: USA_PARITY_EVEN;
2536 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2537 msg.xonFlowControl = 0;
2538 msg.setFlowControl = 0xff;
2539 msg.forwardingLength = 16;
2543 if (reset_port == 1) {
2553 msg.returnStatus = 0;
2554 msg.resetDataToggle = 0xff;
2555 } else if (reset_port == 2) {
2565 msg.returnStatus = 0;
2566 msg.resetDataToggle = 0;
2568 /* Sending intermediate configs */
2569 msg._txOn = (! p_priv->break_on);
2572 msg.txBreak = (p_priv->break_on);
2577 msg.returnStatus = 0;
2578 msg.resetDataToggle = 0x0;
2581 /* Do handshaking outputs */
2582 msg.setTxTriState_setRts = 0xff;
2583 msg.txTriState_rts = p_priv->rts_state;
2585 msg.setHskoa_setDtr = 0xff;
2586 msg.hskoa_dtr = p_priv->dtr_state;
2588 p_priv->resend_cont = 0;
2590 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2592 /* send the data out the device on control endpoint */
2593 this_urb->transfer_buffer_length = sizeof(msg);
2594 this_urb->dev = serial->dev;
2596 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2598 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2603 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2605 struct usb_serial *serial = port->serial;
2606 struct keyspan_serial_private *s_priv;
2607 const struct keyspan_device_details *d_details;
2609 dbg ("%s", __func__);
2611 s_priv = usb_get_serial_data(serial);
2612 d_details = s_priv->device_details;
2614 switch (d_details->msg_format) {
2616 keyspan_usa26_send_setup(serial, port, reset_port);
2619 keyspan_usa28_send_setup(serial, port, reset_port);
2622 keyspan_usa49_send_setup(serial, port, reset_port);
2625 keyspan_usa90_send_setup(serial, port, reset_port);
2628 keyspan_usa67_send_setup(serial, port, reset_port);
2634 /* Gets called by the "real" driver (ie once firmware is loaded
2635 and renumeration has taken place. */
2636 static int keyspan_startup (struct usb_serial *serial)
2639 struct usb_serial_port *port;
2640 struct keyspan_serial_private *s_priv;
2641 struct keyspan_port_private *p_priv;
2642 const struct keyspan_device_details *d_details;
2644 dbg("%s", __func__);
2646 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2647 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2649 if (d_details == NULL) {
2650 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2654 /* Setup private data for serial driver */
2655 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2657 dbg("%s - kmalloc for keyspan_serial_private failed.", __func__);
2661 s_priv->device_details = d_details;
2662 usb_set_serial_data(serial, s_priv);
2664 /* Now setup per port private data */
2665 for (i = 0; i < serial->num_ports; i++) {
2666 port = serial->port[i];
2667 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2669 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2672 p_priv->device_details = d_details;
2673 usb_set_serial_port_data(port, p_priv);
2676 keyspan_setup_urbs(serial);
2678 if (s_priv->instat_urb != NULL) {
2679 s_priv->instat_urb->dev = serial->dev;
2680 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2682 dbg("%s - submit instat urb failed %d", __func__,
2685 if (s_priv->indat_urb != NULL) {
2686 s_priv->indat_urb->dev = serial->dev;
2687 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2689 dbg("%s - submit indat urb failed %d", __func__,
2696 static void keyspan_shutdown (struct usb_serial *serial)
2699 struct usb_serial_port *port;
2700 struct keyspan_serial_private *s_priv;
2701 struct keyspan_port_private *p_priv;
2703 dbg("%s", __func__);
2705 s_priv = usb_get_serial_data(serial);
2707 /* Stop reading/writing urbs */
2708 stop_urb(s_priv->instat_urb);
2709 stop_urb(s_priv->glocont_urb);
2710 stop_urb(s_priv->indat_urb);
2711 for (i = 0; i < serial->num_ports; ++i) {
2712 port = serial->port[i];
2713 p_priv = usb_get_serial_port_data(port);
2714 stop_urb(p_priv->inack_urb);
2715 stop_urb(p_priv->outcont_urb);
2716 for (j = 0; j < 2; j++) {
2717 stop_urb(p_priv->in_urbs[j]);
2718 stop_urb(p_priv->out_urbs[j]);
2723 usb_free_urb(s_priv->instat_urb);
2724 usb_free_urb(s_priv->indat_urb);
2725 usb_free_urb(s_priv->glocont_urb);
2726 for (i = 0; i < serial->num_ports; ++i) {
2727 port = serial->port[i];
2728 p_priv = usb_get_serial_port_data(port);
2729 usb_free_urb(p_priv->inack_urb);
2730 usb_free_urb(p_priv->outcont_urb);
2731 for (j = 0; j < 2; j++) {
2732 usb_free_urb(p_priv->in_urbs[j]);
2733 usb_free_urb(p_priv->out_urbs[j]);
2737 /* dbg("Freeing serial->private."); */
2740 /* dbg("Freeing port->private."); */
2741 /* Now free per port private data */
2742 for (i = 0; i < serial->num_ports; i++) {
2743 port = serial->port[i];
2744 kfree(usb_get_serial_port_data(port));
2748 MODULE_AUTHOR( DRIVER_AUTHOR );
2749 MODULE_DESCRIPTION( DRIVER_DESC );
2750 MODULE_LICENSE("GPL");
2752 MODULE_FIRMWARE("keyspan/usa28.fw");
2753 MODULE_FIRMWARE("keyspan/usa28x.fw");
2754 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2755 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2756 MODULE_FIRMWARE("keyspan/usa19.fw");
2757 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2758 MODULE_FIRMWARE("keyspan/mpr.fw");
2759 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2760 MODULE_FIRMWARE("keyspan/usa18x.fw");
2761 MODULE_FIRMWARE("keyspan/usa19w.fw");
2762 MODULE_FIRMWARE("keyspan/usa49w.fw");
2763 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2765 module_param(debug, bool, S_IRUGO | S_IWUSR);
2766 MODULE_PARM_DESC(debug, "Debug enabled or not");