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_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 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
372 /* no bulk out, so return 0 bytes written */
373 dbg("%s - no output urb :(", __func__);
377 dbg("%s - endpoint %d flip %d", __func__, usb_pipeendpoint(this_urb->pipe), flip);
379 if (this_urb->status == -EINPROGRESS) {
380 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
382 usb_unlink_urb(this_urb);
386 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
387 for now so set to zero */
388 ((char *)this_urb->transfer_buffer)[0] = 0;
390 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
393 /* send the data out the bulk port */
394 this_urb->transfer_buffer_length = todo + dataOffset;
396 this_urb->dev = port->serial->dev;
397 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
398 dbg("usb_submit_urb(write bulk) failed (%d)", err);
400 p_priv->tx_start_time[flip] = jiffies;
402 /* Flip for next time if usa26 or usa28 interface
403 (not used on usa49) */
404 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
410 static void usa26_indat_callback(struct urb *urb)
414 struct usb_serial_port *port;
415 struct tty_struct *tty;
416 unsigned char *data = urb->transfer_buffer;
417 int status = urb->status;
419 dbg ("%s", __func__);
421 endpoint = usb_pipeendpoint(urb->pipe);
424 dbg("%s - nonzero status: %x on endpoint %d.",
425 __func__, status, endpoint);
430 tty = port->port.tty;
431 if (tty && urb->actual_length) {
432 /* 0x80 bit is error flag */
433 if ((data[0] & 0x80) == 0) {
434 /* no errors on individual bytes, only possible overrun err*/
435 if (data[0] & RXERROR_OVERRUN)
438 for (i = 1; i < urb->actual_length ; ++i) {
439 tty_insert_flip_char(tty, data[i], err);
442 /* some bytes had errors, every byte has status */
443 dbg("%s - RX error!!!!", __func__);
444 for (i = 0; i + 1 < urb->actual_length; i += 2) {
445 int stat = data[i], flag = 0;
446 if (stat & RXERROR_OVERRUN)
448 if (stat & RXERROR_FRAMING)
450 if (stat & RXERROR_PARITY)
452 /* XXX should handle break (0x10) */
453 tty_insert_flip_char(tty, data[i+1], flag);
456 tty_flip_buffer_push(tty);
459 /* Resubmit urb so we continue receiving */
460 urb->dev = port->serial->dev;
461 if (port->port.count)
462 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
463 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
468 /* Outdat handling is common for all devices */
469 static void usa2x_outdat_callback(struct urb *urb)
471 struct usb_serial_port *port;
472 struct keyspan_port_private *p_priv;
475 p_priv = usb_get_serial_port_data(port);
476 dbg ("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
478 if (port->port.count)
479 usb_serial_port_softint(port);
482 static void usa26_inack_callback(struct urb *urb)
484 dbg ("%s", __func__);
488 static void usa26_outcont_callback(struct urb *urb)
490 struct usb_serial_port *port;
491 struct keyspan_port_private *p_priv;
494 p_priv = usb_get_serial_port_data(port);
496 if (p_priv->resend_cont) {
497 dbg ("%s - sending setup", __func__);
498 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
502 static void usa26_instat_callback(struct urb *urb)
504 unsigned char *data = urb->transfer_buffer;
505 struct keyspan_usa26_portStatusMessage *msg;
506 struct usb_serial *serial;
507 struct usb_serial_port *port;
508 struct keyspan_port_private *p_priv;
509 int old_dcd_state, err;
510 int status = urb->status;
512 serial = urb->context;
515 dbg("%s - nonzero status: %x", __func__, status);
518 if (urb->actual_length != 9) {
519 dbg("%s - %d byte report??", __func__, urb->actual_length);
523 msg = (struct keyspan_usa26_portStatusMessage *)data;
526 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
527 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
528 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
531 /* Now do something useful with the data */
534 /* Check port number from message and retrieve private data */
535 if (msg->port >= serial->num_ports) {
536 dbg ("%s - Unexpected port number %d", __func__, msg->port);
539 port = serial->port[msg->port];
540 p_priv = usb_get_serial_port_data(port);
542 /* Update handshaking pin state information */
543 old_dcd_state = p_priv->dcd_state;
544 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
545 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
546 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
547 p_priv->ri_state = ((msg->ri) ? 1 : 0);
549 if (port->port.tty && !C_CLOCAL(port->port.tty)
550 && old_dcd_state != p_priv->dcd_state) {
552 tty_hangup(port->port.tty);
554 /* wake_up_interruptible(&p_priv->open_wait); */
557 /* Resubmit urb so we continue receiving */
558 urb->dev = serial->dev;
559 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
560 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
565 static void usa26_glocont_callback(struct urb *urb)
567 dbg ("%s", __func__);
572 static void usa28_indat_callback(struct urb *urb)
575 struct usb_serial_port *port;
576 struct tty_struct *tty;
578 struct keyspan_port_private *p_priv;
579 int status = urb->status;
581 dbg ("%s", __func__);
584 p_priv = usb_get_serial_port_data(port);
585 data = urb->transfer_buffer;
587 if (urb != p_priv->in_urbs[p_priv->in_flip])
592 dbg("%s - nonzero status: %x on endpoint %d.",
593 __func__, status, usb_pipeendpoint(urb->pipe));
598 p_priv = usb_get_serial_port_data(port);
599 data = urb->transfer_buffer;
601 tty = port->port.tty;
602 if (urb->actual_length) {
603 for (i = 0; i < urb->actual_length ; ++i) {
604 tty_insert_flip_char(tty, data[i], 0);
606 tty_flip_buffer_push(tty);
609 /* Resubmit urb so we continue receiving */
610 urb->dev = port->serial->dev;
611 if (port->port.count)
612 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
613 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
615 p_priv->in_flip ^= 1;
617 urb = p_priv->in_urbs[p_priv->in_flip];
618 } while (urb->status != -EINPROGRESS);
621 static void usa28_inack_callback(struct urb *urb)
623 dbg ("%s", __func__);
626 static void usa28_outcont_callback(struct urb *urb)
628 struct usb_serial_port *port;
629 struct keyspan_port_private *p_priv;
632 p_priv = usb_get_serial_port_data(port);
634 if (p_priv->resend_cont) {
635 dbg ("%s - sending setup", __func__);
636 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
640 static void usa28_instat_callback(struct urb *urb)
643 unsigned char *data = urb->transfer_buffer;
644 struct keyspan_usa28_portStatusMessage *msg;
645 struct usb_serial *serial;
646 struct usb_serial_port *port;
647 struct keyspan_port_private *p_priv;
649 int status = urb->status;
651 serial = urb->context;
654 dbg("%s - nonzero status: %x", __func__, status);
658 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
659 dbg("%s - bad length %d", __func__, urb->actual_length);
663 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
664 data[0], data[1], data[2], data[3], data[4], data[5],
665 data[6], data[7], data[8], data[9], data[10], data[11]);*/
667 /* Now do something useful with the data */
668 msg = (struct keyspan_usa28_portStatusMessage *)data;
671 /* Check port number from message and retrieve private data */
672 if (msg->port >= serial->num_ports) {
673 dbg ("%s - Unexpected port number %d", __func__, msg->port);
676 port = serial->port[msg->port];
677 p_priv = usb_get_serial_port_data(port);
679 /* Update handshaking pin state information */
680 old_dcd_state = p_priv->dcd_state;
681 p_priv->cts_state = ((msg->cts) ? 1 : 0);
682 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
683 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
684 p_priv->ri_state = ((msg->ri) ? 1 : 0);
686 if (port->port.tty && !C_CLOCAL(port->port.tty)
687 && old_dcd_state != p_priv->dcd_state) {
689 tty_hangup(port->port.tty);
691 /* wake_up_interruptible(&p_priv->open_wait); */
694 /* Resubmit urb so we continue receiving */
695 urb->dev = serial->dev;
696 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
697 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
702 static void usa28_glocont_callback(struct urb *urb)
704 dbg ("%s", __func__);
708 static void usa49_glocont_callback(struct urb *urb)
710 struct usb_serial *serial;
711 struct usb_serial_port *port;
712 struct keyspan_port_private *p_priv;
715 dbg ("%s", __func__);
717 serial = urb->context;
718 for (i = 0; i < serial->num_ports; ++i) {
719 port = serial->port[i];
720 p_priv = usb_get_serial_port_data(port);
722 if (p_priv->resend_cont) {
723 dbg ("%s - sending setup", __func__);
724 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
730 /* This is actually called glostat in the Keyspan
732 static void usa49_instat_callback(struct urb *urb)
735 unsigned char *data = urb->transfer_buffer;
736 struct keyspan_usa49_portStatusMessage *msg;
737 struct usb_serial *serial;
738 struct usb_serial_port *port;
739 struct keyspan_port_private *p_priv;
741 int status = urb->status;
743 dbg ("%s", __func__);
745 serial = urb->context;
748 dbg("%s - nonzero status: %x", __func__, status);
752 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
753 dbg("%s - bad length %d", __func__, urb->actual_length);
757 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
758 data[0], data[1], data[2], data[3], data[4], data[5],
759 data[6], data[7], data[8], data[9], data[10]);*/
761 /* Now do something useful with the data */
762 msg = (struct keyspan_usa49_portStatusMessage *)data;
764 /* Check port number from message and retrieve private data */
765 if (msg->portNumber >= serial->num_ports) {
766 dbg ("%s - Unexpected port number %d", __func__, msg->portNumber);
769 port = serial->port[msg->portNumber];
770 p_priv = usb_get_serial_port_data(port);
772 /* Update handshaking pin state information */
773 old_dcd_state = p_priv->dcd_state;
774 p_priv->cts_state = ((msg->cts) ? 1 : 0);
775 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
776 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
777 p_priv->ri_state = ((msg->ri) ? 1 : 0);
779 if (port->port.tty && !C_CLOCAL(port->port.tty)
780 && old_dcd_state != p_priv->dcd_state) {
782 tty_hangup(port->port.tty);
784 /* wake_up_interruptible(&p_priv->open_wait); */
787 /* Resubmit urb so we continue receiving */
788 urb->dev = serial->dev;
790 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
791 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
796 static void usa49_inack_callback(struct urb *urb)
798 dbg ("%s", __func__);
801 static void usa49_indat_callback(struct urb *urb)
805 struct usb_serial_port *port;
806 struct tty_struct *tty;
807 unsigned char *data = urb->transfer_buffer;
808 int status = urb->status;
810 dbg ("%s", __func__);
812 endpoint = usb_pipeendpoint(urb->pipe);
815 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
821 tty = port->port.tty;
822 if (tty && urb->actual_length) {
823 /* 0x80 bit is error flag */
824 if ((data[0] & 0x80) == 0) {
825 /* no error on any byte */
826 for (i = 1; i < urb->actual_length ; ++i) {
827 tty_insert_flip_char(tty, data[i], 0);
830 /* some bytes had errors, every byte has status */
831 for (i = 0; i + 1 < urb->actual_length; i += 2) {
832 int stat = data[i], flag = 0;
833 if (stat & RXERROR_OVERRUN)
835 if (stat & RXERROR_FRAMING)
837 if (stat & RXERROR_PARITY)
839 /* XXX should handle break (0x10) */
840 tty_insert_flip_char(tty, data[i+1], flag);
843 tty_flip_buffer_push(tty);
846 /* Resubmit urb so we continue receiving */
847 urb->dev = port->serial->dev;
848 if (port->port.count)
849 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
850 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
854 static void usa49wg_indat_callback(struct urb *urb)
857 struct usb_serial *serial;
858 struct usb_serial_port *port;
859 struct tty_struct *tty;
860 unsigned char *data = urb->transfer_buffer;
861 int status = urb->status;
863 dbg ("%s", __func__);
865 serial = urb->context;
868 dbg("%s - nonzero status: %x", __func__, status);
872 /* inbound data is in the form P#, len, status, data */
876 if (urb->actual_length) {
877 while (i < urb->actual_length) {
879 /* Check port number from message*/
880 if (data[i] >= serial->num_ports) {
881 dbg ("%s - Unexpected port number %d",
885 port = serial->port[data[i++]];
886 tty = port->port.tty;
889 /* 0x80 bit is error flag */
890 if ((data[i] & 0x80) == 0) {
891 /* no error on any byte */
893 for (x = 1; x < len ; ++x)
894 if (port->port.count)
895 tty_insert_flip_char(tty,
901 * some bytes had errors, every byte has status
903 for (x = 0; x + 1 < len; x += 2) {
904 int stat = data[i], flag = 0;
905 if (stat & RXERROR_OVERRUN)
907 if (stat & RXERROR_FRAMING)
909 if (stat & RXERROR_PARITY)
911 /* XXX should handle break (0x10) */
912 if (port->port.count)
913 tty_insert_flip_char(tty,
918 if (port->port.count)
919 tty_flip_buffer_push(tty);
923 /* Resubmit urb so we continue receiving */
924 urb->dev = serial->dev;
926 err = usb_submit_urb(urb, GFP_ATOMIC);
928 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
931 /* not used, usa-49 doesn't have per-port control endpoints */
932 static void usa49_outcont_callback(struct urb *urb)
934 dbg ("%s", __func__);
937 static void usa90_indat_callback(struct urb *urb)
941 struct usb_serial_port *port;
942 struct keyspan_port_private *p_priv;
943 struct tty_struct *tty;
944 unsigned char *data = urb->transfer_buffer;
945 int status = urb->status;
947 dbg ("%s", __func__);
949 endpoint = usb_pipeendpoint(urb->pipe);
952 dbg("%s - nonzero status: %x on endpoint %d.",
953 __func__, status, endpoint);
958 p_priv = usb_get_serial_port_data(port);
960 tty = port->port.tty;
961 if (urb->actual_length) {
963 /* if current mode is DMA, looks like usa28 format
964 otherwise looks like usa26 data format */
966 if (p_priv->baud > 57600) {
967 for (i = 0; i < urb->actual_length ; ++i)
968 tty_insert_flip_char(tty, data[i], 0);
972 /* 0x80 bit is error flag */
973 if ((data[0] & 0x80) == 0) {
974 /* no errors on individual bytes, only possible overrun err*/
975 if (data[0] & RXERROR_OVERRUN)
978 for (i = 1; i < urb->actual_length ; ++i)
979 tty_insert_flip_char(tty, data[i], err);
983 /* some bytes had errors, every byte has status */
984 dbg("%s - RX error!!!!", __func__);
985 for (i = 0; i + 1 < urb->actual_length; i += 2) {
986 int stat = data[i], flag = 0;
987 if (stat & RXERROR_OVERRUN)
989 if (stat & RXERROR_FRAMING)
991 if (stat & RXERROR_PARITY)
993 /* XXX should handle break (0x10) */
994 tty_insert_flip_char(tty, data[i+1], flag);
998 tty_flip_buffer_push(tty);
1001 /* Resubmit urb so we continue receiving */
1002 urb->dev = port->serial->dev;
1003 if (port->port.count)
1004 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1005 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1011 static void usa90_instat_callback(struct urb *urb)
1013 unsigned char *data = urb->transfer_buffer;
1014 struct keyspan_usa90_portStatusMessage *msg;
1015 struct usb_serial *serial;
1016 struct usb_serial_port *port;
1017 struct keyspan_port_private *p_priv;
1018 int old_dcd_state, err;
1019 int status = urb->status;
1021 serial = urb->context;
1024 dbg("%s - nonzero status: %x", __func__, status);
1027 if (urb->actual_length < 14) {
1028 dbg("%s - %d byte report??", __func__, urb->actual_length);
1032 msg = (struct keyspan_usa90_portStatusMessage *)data;
1034 /* Now do something useful with the data */
1036 port = serial->port[0];
1037 p_priv = usb_get_serial_port_data(port);
1039 /* Update handshaking pin state information */
1040 old_dcd_state = p_priv->dcd_state;
1041 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1042 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1043 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1044 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1046 if (port->port.tty && !C_CLOCAL(port->port.tty)
1047 && old_dcd_state != p_priv->dcd_state) {
1049 tty_hangup(port->port.tty);
1051 /* wake_up_interruptible(&p_priv->open_wait); */
1054 /* Resubmit urb so we continue receiving */
1055 urb->dev = serial->dev;
1056 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1057 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1063 static void usa90_outcont_callback(struct urb *urb)
1065 struct usb_serial_port *port;
1066 struct keyspan_port_private *p_priv;
1068 port = urb->context;
1069 p_priv = usb_get_serial_port_data(port);
1071 if (p_priv->resend_cont) {
1072 dbg ("%s - sending setup", __func__);
1073 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1077 /* Status messages from the 28xg */
1078 static void usa67_instat_callback(struct urb *urb)
1081 unsigned char *data = urb->transfer_buffer;
1082 struct keyspan_usa67_portStatusMessage *msg;
1083 struct usb_serial *serial;
1084 struct usb_serial_port *port;
1085 struct keyspan_port_private *p_priv;
1087 int status = urb->status;
1089 dbg ("%s", __func__);
1091 serial = urb->context;
1094 dbg("%s - nonzero status: %x", __func__, status);
1098 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1099 dbg("%s - bad length %d", __func__, urb->actual_length);
1104 /* Now do something useful with the data */
1105 msg = (struct keyspan_usa67_portStatusMessage *)data;
1107 /* Check port number from message and retrieve private data */
1108 if (msg->port >= serial->num_ports) {
1109 dbg ("%s - Unexpected port number %d", __func__, msg->port);
1113 port = serial->port[msg->port];
1114 p_priv = usb_get_serial_port_data(port);
1116 /* Update handshaking pin state information */
1117 old_dcd_state = p_priv->dcd_state;
1118 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1119 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1121 if (port->port.tty && !C_CLOCAL(port->port.tty)
1122 && old_dcd_state != p_priv->dcd_state) {
1124 tty_hangup(port->port.tty);
1126 /* wake_up_interruptible(&p_priv->open_wait); */
1129 /* Resubmit urb so we continue receiving */
1130 urb->dev = serial->dev;
1131 err = usb_submit_urb(urb, GFP_ATOMIC);
1133 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1136 static void usa67_glocont_callback(struct urb *urb)
1138 struct usb_serial *serial;
1139 struct usb_serial_port *port;
1140 struct keyspan_port_private *p_priv;
1143 dbg ("%s", __func__);
1145 serial = urb->context;
1146 for (i = 0; i < serial->num_ports; ++i) {
1147 port = serial->port[i];
1148 p_priv = usb_get_serial_port_data(port);
1150 if (p_priv->resend_cont) {
1151 dbg ("%s - sending setup", __func__);
1152 keyspan_usa67_send_setup(serial, port,
1153 p_priv->resend_cont - 1);
1159 static int keyspan_write_room(struct tty_struct *tty)
1161 struct usb_serial_port *port = tty->driver_data;
1162 struct keyspan_port_private *p_priv;
1163 const struct keyspan_device_details *d_details;
1166 struct urb *this_urb;
1168 dbg("%s", __func__);
1169 p_priv = usb_get_serial_port_data(port);
1170 d_details = p_priv->device_details;
1172 /* FIXME: locking */
1173 if (d_details->msg_format == msg_usa90)
1178 flip = p_priv->out_flip;
1180 /* Check both endpoints to see if any are available. */
1181 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1182 if (this_urb->status != -EINPROGRESS)
1184 flip = (flip + 1) & d_details->outdat_endp_flip;
1185 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1186 if (this_urb->status != -EINPROGRESS)
1193 static int keyspan_open(struct tty_struct *tty,
1194 struct usb_serial_port *port, struct file *filp)
1196 struct keyspan_port_private *p_priv;
1197 struct keyspan_serial_private *s_priv;
1198 struct usb_serial *serial = port->serial;
1199 const struct keyspan_device_details *d_details;
1201 int baud_rate, device_port;
1203 unsigned int cflag = 0;
1205 s_priv = usb_get_serial_data(serial);
1206 p_priv = usb_get_serial_port_data(port);
1207 d_details = p_priv->device_details;
1209 dbg("%s - port%d.", __func__, port->number);
1211 /* Set some sane defaults */
1212 p_priv->rts_state = 1;
1213 p_priv->dtr_state = 1;
1214 p_priv->baud = 9600;
1216 /* force baud and lcr to be set on open */
1217 p_priv->old_baud = 0;
1218 p_priv->old_cflag = 0;
1220 p_priv->out_flip = 0;
1221 p_priv->in_flip = 0;
1223 /* Reset low level data toggle and start reading from endpoints */
1224 for (i = 0; i < 2; i++) {
1225 if ((urb = p_priv->in_urbs[i]) == NULL)
1227 urb->dev = serial->dev;
1229 /* make sure endpoint data toggle is synchronized with the device */
1231 usb_clear_halt(urb->dev, urb->pipe);
1233 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1234 dbg("%s - submit urb %d failed (%d)", __func__, i, err);
1238 /* Reset low level data toggle on out endpoints */
1239 for (i = 0; i < 2; i++) {
1240 if ((urb = p_priv->out_urbs[i]) == NULL)
1242 urb->dev = serial->dev;
1243 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1246 /* get the terminal config for the setup message now so we don't
1247 * need to send 2 of them */
1249 device_port = port->number - port->serial->minor;
1251 cflag = tty->termios->c_cflag;
1252 /* Baud rate calculation takes baud rate as an integer
1253 so other rates can be generated if desired. */
1254 baud_rate = tty_get_baud_rate(tty);
1255 /* If no match or invalid, leave as default */
1257 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1258 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1259 p_priv->baud = baud_rate;
1262 /* set CTS/RTS handshake etc. */
1263 p_priv->cflag = cflag;
1264 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1266 keyspan_send_setup(port, 1);
1268 //keyspan_set_termios(port, NULL);
1273 static inline void stop_urb(struct urb *urb)
1275 if (urb && urb->status == -EINPROGRESS)
1279 static void keyspan_close(struct tty_struct *tty,
1280 struct usb_serial_port *port, struct file *filp)
1283 struct usb_serial *serial = port->serial;
1284 struct keyspan_serial_private *s_priv;
1285 struct keyspan_port_private *p_priv;
1287 dbg("%s", __func__);
1288 s_priv = usb_get_serial_data(serial);
1289 p_priv = usb_get_serial_port_data(port);
1291 p_priv->rts_state = 0;
1292 p_priv->dtr_state = 0;
1295 keyspan_send_setup(port, 2);
1296 /* pilot-xfer seems to work best with this delay */
1298 // keyspan_set_termios(port, NULL);
1301 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1302 dbg("%s - urb in progress", __func__);
1305 p_priv->out_flip = 0;
1306 p_priv->in_flip = 0;
1309 /* Stop reading/writing urbs */
1310 stop_urb(p_priv->inack_urb);
1311 /* stop_urb(p_priv->outcont_urb); */
1312 for (i = 0; i < 2; i++) {
1313 stop_urb(p_priv->in_urbs[i]);
1314 stop_urb(p_priv->out_urbs[i]);
1317 port->port.tty = NULL;
1320 /* download the firmware to a pre-renumeration device */
1321 static int keyspan_fake_startup (struct usb_serial *serial)
1324 const struct ihex_binrec *record;
1326 const struct firmware *fw;
1328 dbg("Keyspan startup version %04x product %04x",
1329 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1330 le16_to_cpu(serial->dev->descriptor.idProduct));
1332 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1333 dbg("Firmware already loaded. Quitting.");
1337 /* Select firmware image on the basis of idProduct */
1338 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1339 case keyspan_usa28_pre_product_id:
1340 fw_name = "keyspan/usa28.fw";
1343 case keyspan_usa28x_pre_product_id:
1344 fw_name = "keyspan/usa28x.fw";
1347 case keyspan_usa28xa_pre_product_id:
1348 fw_name = "keyspan/usa28xa.fw";
1351 case keyspan_usa28xb_pre_product_id:
1352 fw_name = "keyspan/usa28xb.fw";
1355 case keyspan_usa19_pre_product_id:
1356 fw_name = "keyspan/usa19.fw";
1359 case keyspan_usa19qi_pre_product_id:
1360 fw_name = "keyspan/usa19qi.fw";
1363 case keyspan_mpr_pre_product_id:
1364 fw_name = "keyspan/mpr.fw";
1367 case keyspan_usa19qw_pre_product_id:
1368 fw_name = "keyspan/usa19qw.fw";
1371 case keyspan_usa18x_pre_product_id:
1372 fw_name = "keyspan/usa18x.fw";
1375 case keyspan_usa19w_pre_product_id:
1376 fw_name = "keyspan/usa19w.fw";
1379 case keyspan_usa49w_pre_product_id:
1380 fw_name = "keyspan/usa49w.fw";
1383 case keyspan_usa49wlc_pre_product_id:
1384 fw_name = "keyspan/usa49wlc.fw";
1388 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1389 le16_to_cpu(serial->dev->descriptor.idProduct));
1393 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1394 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1398 dbg("Uploading Keyspan %s firmware.", fw_name);
1400 /* download the firmware image */
1401 response = ezusb_set_reset(serial, 1);
1403 record = (const struct ihex_binrec *)fw->data;
1406 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1407 (unsigned char *)record->data,
1408 be16_to_cpu(record->len), 0xa0);
1410 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1411 "firmware (%d %04X %p %d)\n",
1412 response, be32_to_cpu(record->addr),
1413 record->data, be16_to_cpu(record->len));
1416 record = ihex_next_binrec(record);
1418 release_firmware(fw);
1419 /* bring device out of reset. Renumeration will occur in a
1420 moment and the new device will bind to the real driver */
1421 response = ezusb_set_reset(serial, 0);
1423 /* we don't want this device to have a driver assigned to it. */
1427 /* Helper functions used by keyspan_setup_urbs */
1428 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1431 struct usb_host_interface *iface_desc;
1432 struct usb_endpoint_descriptor *ep;
1435 iface_desc = serial->interface->cur_altsetting;
1436 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1437 ep = &iface_desc->endpoint[i].desc;
1438 if (ep->bEndpointAddress == endpoint)
1441 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1442 "endpoint %x\n", endpoint);
1446 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1447 int dir, void *ctx, char *buf, int len,
1448 void (*callback)(struct urb *))
1451 struct usb_endpoint_descriptor const *ep_desc;
1452 char const *ep_type_name;
1455 return NULL; /* endpoint not needed */
1457 dbg ("%s - alloc for endpoint %d.", __func__, endpoint);
1458 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1460 dbg ("%s - alloc for endpoint %d failed.", __func__, endpoint);
1464 if (endpoint == 0) {
1465 /* control EP filled in when used */
1469 ep_desc = find_ep(serial, endpoint);
1471 /* leak the urb, something's wrong and the callers don't care */
1474 if (usb_endpoint_xfer_int(ep_desc)) {
1475 ep_type_name = "INT";
1476 usb_fill_int_urb(urb, serial->dev,
1477 usb_sndintpipe(serial->dev, endpoint) | dir,
1478 buf, len, callback, ctx,
1479 ep_desc->bInterval);
1480 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1481 ep_type_name = "BULK";
1482 usb_fill_bulk_urb(urb, serial->dev,
1483 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1484 buf, len, callback, ctx);
1486 dev_warn(&serial->interface->dev,
1487 "unsupported endpoint type %x\n",
1488 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1493 dbg("%s - using urb %p for %s endpoint %x",
1494 __func__, urb, ep_type_name, endpoint);
1498 static struct callbacks {
1499 void (*instat_callback)(struct urb *);
1500 void (*glocont_callback)(struct urb *);
1501 void (*indat_callback)(struct urb *);
1502 void (*outdat_callback)(struct urb *);
1503 void (*inack_callback)(struct urb *);
1504 void (*outcont_callback)(struct urb *);
1505 } keyspan_callbacks[] = {
1507 /* msg_usa26 callbacks */
1508 .instat_callback = usa26_instat_callback,
1509 .glocont_callback = usa26_glocont_callback,
1510 .indat_callback = usa26_indat_callback,
1511 .outdat_callback = usa2x_outdat_callback,
1512 .inack_callback = usa26_inack_callback,
1513 .outcont_callback = usa26_outcont_callback,
1515 /* msg_usa28 callbacks */
1516 .instat_callback = usa28_instat_callback,
1517 .glocont_callback = usa28_glocont_callback,
1518 .indat_callback = usa28_indat_callback,
1519 .outdat_callback = usa2x_outdat_callback,
1520 .inack_callback = usa28_inack_callback,
1521 .outcont_callback = usa28_outcont_callback,
1523 /* msg_usa49 callbacks */
1524 .instat_callback = usa49_instat_callback,
1525 .glocont_callback = usa49_glocont_callback,
1526 .indat_callback = usa49_indat_callback,
1527 .outdat_callback = usa2x_outdat_callback,
1528 .inack_callback = usa49_inack_callback,
1529 .outcont_callback = usa49_outcont_callback,
1531 /* msg_usa90 callbacks */
1532 .instat_callback = usa90_instat_callback,
1533 .glocont_callback = usa28_glocont_callback,
1534 .indat_callback = usa90_indat_callback,
1535 .outdat_callback = usa2x_outdat_callback,
1536 .inack_callback = usa28_inack_callback,
1537 .outcont_callback = usa90_outcont_callback,
1539 /* msg_usa67 callbacks */
1540 .instat_callback = usa67_instat_callback,
1541 .glocont_callback = usa67_glocont_callback,
1542 .indat_callback = usa26_indat_callback,
1543 .outdat_callback = usa2x_outdat_callback,
1544 .inack_callback = usa26_inack_callback,
1545 .outcont_callback = usa26_outcont_callback,
1549 /* Generic setup urbs function that uses
1550 data in device_details */
1551 static void keyspan_setup_urbs(struct usb_serial *serial)
1554 struct keyspan_serial_private *s_priv;
1555 const struct keyspan_device_details *d_details;
1556 struct usb_serial_port *port;
1557 struct keyspan_port_private *p_priv;
1558 struct callbacks *cback;
1561 dbg ("%s", __func__);
1563 s_priv = usb_get_serial_data(serial);
1564 d_details = s_priv->device_details;
1566 /* Setup values for the various callback routines */
1567 cback = &keyspan_callbacks[d_details->msg_format];
1569 /* Allocate and set up urbs for each one that is in use,
1570 starting with instat endpoints */
1571 s_priv->instat_urb = keyspan_setup_urb
1572 (serial, d_details->instat_endpoint, USB_DIR_IN,
1573 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1574 cback->instat_callback);
1576 s_priv->indat_urb = keyspan_setup_urb
1577 (serial, d_details->indat_endpoint, USB_DIR_IN,
1578 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1579 usa49wg_indat_callback);
1581 s_priv->glocont_urb = keyspan_setup_urb
1582 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1583 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1584 cback->glocont_callback);
1586 /* Setup endpoints for each port specific thing */
1587 for (i = 0; i < d_details->num_ports; i ++) {
1588 port = serial->port[i];
1589 p_priv = usb_get_serial_port_data(port);
1591 /* Do indat endpoints first, once for each flip */
1592 endp = d_details->indat_endpoints[i];
1593 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1594 p_priv->in_urbs[j] = keyspan_setup_urb
1595 (serial, endp, USB_DIR_IN, port,
1596 p_priv->in_buffer[j], 64,
1597 cback->indat_callback);
1600 p_priv->in_urbs[j] = NULL;
1602 /* outdat endpoints also have flip */
1603 endp = d_details->outdat_endpoints[i];
1604 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1605 p_priv->out_urbs[j] = keyspan_setup_urb
1606 (serial, endp, USB_DIR_OUT, port,
1607 p_priv->out_buffer[j], 64,
1608 cback->outdat_callback);
1611 p_priv->out_urbs[j] = NULL;
1613 /* inack endpoint */
1614 p_priv->inack_urb = keyspan_setup_urb
1615 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1616 port, p_priv->inack_buffer, 1, cback->inack_callback);
1618 /* outcont endpoint */
1619 p_priv->outcont_urb = keyspan_setup_urb
1620 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1621 port, p_priv->outcont_buffer, 64,
1622 cback->outcont_callback);
1627 /* usa19 function doesn't require prescaler */
1628 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1629 u8 *rate_low, u8 *prescaler, int portnum)
1631 u32 b16, /* baud rate times 16 (actual rate used internally) */
1633 cnt; /* inverse of divisor (programmed into 8051) */
1635 dbg ("%s - %d.", __func__, baud_rate);
1637 /* prevent divide by zero... */
1638 if( (b16 = (baud_rate * 16L)) == 0) {
1639 return (KEYSPAN_INVALID_BAUD_RATE);
1642 /* Any "standard" rate over 57k6 is marginal on the USA-19
1643 as we run out of divisor resolution. */
1644 if (baud_rate > 57600) {
1645 return (KEYSPAN_INVALID_BAUD_RATE);
1648 /* calculate the divisor and the counter (its inverse) */
1649 if( (div = (baudclk / b16)) == 0) {
1650 return (KEYSPAN_INVALID_BAUD_RATE);
1657 return (KEYSPAN_INVALID_BAUD_RATE);
1660 /* return the counter values if non-null */
1662 *rate_low = (u8) (cnt & 0xff);
1665 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1667 if (rate_low && rate_hi) {
1668 dbg ("%s - %d %02x %02x.", __func__, baud_rate, *rate_hi, *rate_low);
1671 return (KEYSPAN_BAUD_RATE_OK);
1674 /* usa19hs function doesn't require prescaler */
1675 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1676 u8 *rate_low, u8 *prescaler, int portnum)
1678 u32 b16, /* baud rate times 16 (actual rate used internally) */
1681 dbg ("%s - %d.", __func__, baud_rate);
1683 /* prevent divide by zero... */
1684 if( (b16 = (baud_rate * 16L)) == 0)
1685 return (KEYSPAN_INVALID_BAUD_RATE);
1689 /* calculate the divisor */
1690 if( (div = (baudclk / b16)) == 0)
1691 return (KEYSPAN_INVALID_BAUD_RATE);
1694 return (KEYSPAN_INVALID_BAUD_RATE);
1696 /* return the counter values if non-null */
1698 *rate_low = (u8) (div & 0xff);
1701 *rate_hi = (u8) ((div >> 8) & 0xff);
1703 if (rate_low && rate_hi)
1704 dbg ("%s - %d %02x %02x.", __func__, baud_rate, *rate_hi, *rate_low);
1706 return (KEYSPAN_BAUD_RATE_OK);
1709 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1710 u8 *rate_low, u8 *prescaler, int portnum)
1712 u32 b16, /* baud rate times 16 (actual rate used internally) */
1713 clk, /* clock with 13/8 prescaler */
1714 div, /* divisor using 13/8 prescaler */
1715 res, /* resulting baud rate using 13/8 prescaler */
1716 diff, /* error using 13/8 prescaler */
1721 dbg ("%s - %d.", __func__, baud_rate);
1723 /* prevent divide by zero */
1724 if( (b16 = baud_rate * 16L) == 0) {
1725 return (KEYSPAN_INVALID_BAUD_RATE);
1728 /* Calculate prescaler by trying them all and looking
1731 /* start with largest possible difference */
1732 smallest_diff = 0xffffffff;
1734 /* 0 is an invalid prescaler, used as a flag */
1737 for(i = 8; i <= 0xff; ++i) {
1738 clk = (baudclk * 8) / (u32) i;
1740 if( (div = clk / b16) == 0) {
1745 diff= (res > b16) ? (res-b16) : (b16-res);
1747 if(diff < smallest_diff) {
1749 smallest_diff = diff;
1753 if(best_prescaler == 0) {
1754 return (KEYSPAN_INVALID_BAUD_RATE);
1757 clk = (baudclk * 8) / (u32) best_prescaler;
1760 /* return the divisor and prescaler if non-null */
1762 *rate_low = (u8) (div & 0xff);
1765 *rate_hi = (u8) ((div >> 8) & 0xff);
1768 *prescaler = best_prescaler;
1769 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1771 return (KEYSPAN_BAUD_RATE_OK);
1774 /* USA-28 supports different maximum baud rates on each port */
1775 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1776 u8 *rate_low, u8 *prescaler, int portnum)
1778 u32 b16, /* baud rate times 16 (actual rate used internally) */
1780 cnt; /* inverse of divisor (programmed into 8051) */
1782 dbg ("%s - %d.", __func__, baud_rate);
1784 /* prevent divide by zero */
1785 if ((b16 = baud_rate * 16L) == 0)
1786 return (KEYSPAN_INVALID_BAUD_RATE);
1788 /* calculate the divisor and the counter (its inverse) */
1789 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1790 return (KEYSPAN_INVALID_BAUD_RATE);
1796 /* check for out of range, based on portnum,
1797 and return result */
1800 return (KEYSPAN_INVALID_BAUD_RATE);
1805 return (KEYSPAN_INVALID_BAUD_RATE);
1809 return (KEYSPAN_INVALID_BAUD_RATE);
1813 /* return the counter values if not NULL
1814 (port 1 will ignore retHi) */
1816 *rate_low = (u8) (cnt & 0xff);
1819 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1821 dbg ("%s - %d OK.", __func__, baud_rate);
1822 return (KEYSPAN_BAUD_RATE_OK);
1825 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1826 struct usb_serial_port *port,
1829 struct keyspan_usa26_portControlMessage msg;
1830 struct keyspan_serial_private *s_priv;
1831 struct keyspan_port_private *p_priv;
1832 const struct keyspan_device_details *d_details;
1834 struct urb *this_urb;
1835 int device_port, err;
1837 dbg ("%s reset=%d", __func__, reset_port);
1839 s_priv = usb_get_serial_data(serial);
1840 p_priv = usb_get_serial_port_data(port);
1841 d_details = s_priv->device_details;
1842 device_port = port->number - port->serial->minor;
1844 outcont_urb = d_details->outcont_endpoints[port->number];
1845 this_urb = p_priv->outcont_urb;
1847 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1849 /* Make sure we have an urb then send the message */
1850 if (this_urb == NULL) {
1851 dbg("%s - oops no urb.", __func__);
1855 /* Save reset port val for resend.
1856 Don't overwrite resend for open/close condition. */
1857 if ((reset_port + 1) > p_priv->resend_cont)
1858 p_priv->resend_cont = reset_port + 1;
1859 if (this_urb->status == -EINPROGRESS) {
1860 /* dbg ("%s - already writing", __func__); */
1865 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1867 /* Only set baud rate if it's changed */
1868 if (p_priv->old_baud != p_priv->baud) {
1869 p_priv->old_baud = p_priv->baud;
1870 msg.setClocking = 0xff;
1871 if (d_details->calculate_baud_rate
1872 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1873 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1874 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
1877 msg.baudHi = 125; /* Values for 9600 baud */
1880 msg.setPrescaler = 0xff;
1883 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1884 switch (p_priv->cflag & CSIZE) {
1886 msg.lcr |= USA_DATABITS_5;
1889 msg.lcr |= USA_DATABITS_6;
1892 msg.lcr |= USA_DATABITS_7;
1895 msg.lcr |= USA_DATABITS_8;
1898 if (p_priv->cflag & PARENB) {
1899 /* note USA_PARITY_NONE == 0 */
1900 msg.lcr |= (p_priv->cflag & PARODD)?
1901 USA_PARITY_ODD: USA_PARITY_EVEN;
1905 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1906 msg.xonFlowControl = 0;
1907 msg.setFlowControl = 0xff;
1908 msg.forwardingLength = 16;
1913 if (reset_port == 1) {
1922 msg.returnStatus = 0;
1923 msg.resetDataToggle = 0xff;
1927 else if (reset_port == 2) {
1936 msg.returnStatus = 0;
1937 msg.resetDataToggle = 0;
1940 /* Sending intermediate configs */
1942 msg._txOn = (! p_priv->break_on);
1945 msg.txBreak = (p_priv->break_on);
1950 msg.returnStatus = 0;
1951 msg.resetDataToggle = 0x0;
1954 /* Do handshaking outputs */
1955 msg.setTxTriState_setRts = 0xff;
1956 msg.txTriState_rts = p_priv->rts_state;
1958 msg.setHskoa_setDtr = 0xff;
1959 msg.hskoa_dtr = p_priv->dtr_state;
1961 p_priv->resend_cont = 0;
1962 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1964 /* send the data out the device on control endpoint */
1965 this_urb->transfer_buffer_length = sizeof(msg);
1967 this_urb->dev = serial->dev;
1968 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1969 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1973 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1974 outcont_urb, this_urb->transfer_buffer_length,
1975 usb_pipeendpoint(this_urb->pipe));
1982 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1983 struct usb_serial_port *port,
1986 struct keyspan_usa28_portControlMessage msg;
1987 struct keyspan_serial_private *s_priv;
1988 struct keyspan_port_private *p_priv;
1989 const struct keyspan_device_details *d_details;
1990 struct urb *this_urb;
1991 int device_port, err;
1993 dbg ("%s", __func__);
1995 s_priv = usb_get_serial_data(serial);
1996 p_priv = usb_get_serial_port_data(port);
1997 d_details = s_priv->device_details;
1998 device_port = port->number - port->serial->minor;
2000 /* only do something if we have a bulk out endpoint */
2001 if ((this_urb = p_priv->outcont_urb) == NULL) {
2002 dbg("%s - oops no urb.", __func__);
2006 /* Save reset port val for resend.
2007 Don't overwrite resend for open/close condition. */
2008 if ((reset_port + 1) > p_priv->resend_cont)
2009 p_priv->resend_cont = reset_port + 1;
2010 if (this_urb->status == -EINPROGRESS) {
2011 dbg ("%s already writing", __func__);
2016 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2018 msg.setBaudRate = 1;
2019 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2020 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2021 dbg("%s - Invalid baud rate requested %d.", __func__, p_priv->baud);
2023 msg.baudHi = 0xb2; /* Values for 9600 baud */
2026 /* If parity is enabled, we must calculate it ourselves. */
2027 msg.parity = 0; /* XXX for now */
2029 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2030 msg.xonFlowControl = 0;
2032 /* Do handshaking outputs, DTR is inverted relative to RTS */
2033 msg.rts = p_priv->rts_state;
2034 msg.dtr = p_priv->dtr_state;
2036 msg.forwardingLength = 16;
2038 msg.breakThreshold = 45;
2042 /*msg.returnStatus = 1;
2043 msg.resetDataToggle = 0xff;*/
2045 if (reset_port == 1) {
2049 msg.txForceXoff = 0;
2055 msg.returnStatus = 0;
2056 msg.resetDataToggle = 0xff;
2059 else if (reset_port == 2) {
2063 msg.txForceXoff = 0;
2069 msg.returnStatus = 0;
2070 msg.resetDataToggle = 0;
2072 /* Sending intermediate configs */
2074 msg._txOn = (! p_priv->break_on);
2077 msg.txForceXoff = 0;
2078 msg.txBreak = (p_priv->break_on);
2083 msg.returnStatus = 0;
2084 msg.resetDataToggle = 0x0;
2087 p_priv->resend_cont = 0;
2088 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2090 /* send the data out the device on control endpoint */
2091 this_urb->transfer_buffer_length = sizeof(msg);
2093 this_urb->dev = serial->dev;
2094 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2095 dbg("%s - usb_submit_urb(setup) failed", __func__);
2099 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
2100 this_urb->transfer_buffer_length);
2107 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2108 struct usb_serial_port *port,
2111 struct keyspan_usa49_portControlMessage msg;
2112 struct usb_ctrlrequest *dr = NULL;
2113 struct keyspan_serial_private *s_priv;
2114 struct keyspan_port_private *p_priv;
2115 const struct keyspan_device_details *d_details;
2116 struct urb *this_urb;
2117 int err, device_port;
2119 dbg ("%s", __func__);
2121 s_priv = usb_get_serial_data(serial);
2122 p_priv = usb_get_serial_port_data(port);
2123 d_details = s_priv->device_details;
2125 this_urb = s_priv->glocont_urb;
2127 /* Work out which port within the device is being setup */
2128 device_port = port->number - port->serial->minor;
2130 dbg("%s - endpoint %d port %d (%d)",__func__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2132 /* Make sure we have an urb then send the message */
2133 if (this_urb == NULL) {
2134 dbg("%s - oops no urb for port %d.", __func__, port->number);
2138 /* Save reset port val for resend.
2139 Don't overwrite resend for open/close condition. */
2140 if ((reset_port + 1) > p_priv->resend_cont)
2141 p_priv->resend_cont = reset_port + 1;
2143 if (this_urb->status == -EINPROGRESS) {
2144 /* dbg ("%s - already writing", __func__); */
2149 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2151 /*msg.portNumber = port->number;*/
2152 msg.portNumber = device_port;
2154 /* Only set baud rate if it's changed */
2155 if (p_priv->old_baud != p_priv->baud) {
2156 p_priv->old_baud = p_priv->baud;
2157 msg.setClocking = 0xff;
2158 if (d_details->calculate_baud_rate
2159 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2160 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2161 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2164 msg.baudHi = 125; /* Values for 9600 baud */
2167 //msg.setPrescaler = 0xff;
2170 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2171 switch (p_priv->cflag & CSIZE) {
2173 msg.lcr |= USA_DATABITS_5;
2176 msg.lcr |= USA_DATABITS_6;
2179 msg.lcr |= USA_DATABITS_7;
2182 msg.lcr |= USA_DATABITS_8;
2185 if (p_priv->cflag & PARENB) {
2186 /* note USA_PARITY_NONE == 0 */
2187 msg.lcr |= (p_priv->cflag & PARODD)?
2188 USA_PARITY_ODD: USA_PARITY_EVEN;
2192 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2193 msg.xonFlowControl = 0;
2194 msg.setFlowControl = 0xff;
2196 msg.forwardingLength = 16;
2201 if (reset_port == 1) {
2210 msg.returnStatus = 0;
2211 msg.resetDataToggle = 0xff;
2213 msg.disablePort = 0;
2216 else if (reset_port == 2) {
2225 msg.returnStatus = 0;
2226 msg.resetDataToggle = 0;
2228 msg.disablePort = 1;
2230 /* Sending intermediate configs */
2232 msg._txOn = (! p_priv->break_on);
2235 msg.txBreak = (p_priv->break_on);
2240 msg.returnStatus = 0;
2241 msg.resetDataToggle = 0x0;
2243 msg.disablePort = 0;
2246 /* Do handshaking outputs */
2248 msg.rts = p_priv->rts_state;
2251 msg.dtr = p_priv->dtr_state;
2253 p_priv->resend_cont = 0;
2255 /* if the device is a 49wg, we send control message on usb control EP 0 */
2257 if (d_details->product_id == keyspan_usa49wg_product_id) {
2258 dr = (void *)(s_priv->ctrl_buf);
2259 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2260 dr->bRequest = 0xB0; /* 49wg control message */;
2263 dr->wLength = cpu_to_le16(sizeof(msg));
2265 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2267 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2268 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2269 usa49_glocont_callback, serial);
2272 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2274 /* send the data out the device on control endpoint */
2275 this_urb->transfer_buffer_length = sizeof(msg);
2277 this_urb->dev = serial->dev;
2279 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2280 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2284 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2285 outcont_urb, this_urb->transfer_buffer_length,
2286 usb_pipeendpoint(this_urb->pipe));
2293 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2294 struct usb_serial_port *port,
2297 struct keyspan_usa90_portControlMessage msg;
2298 struct keyspan_serial_private *s_priv;
2299 struct keyspan_port_private *p_priv;
2300 const struct keyspan_device_details *d_details;
2301 struct urb *this_urb;
2305 dbg ("%s", __func__);
2307 s_priv = usb_get_serial_data(serial);
2308 p_priv = usb_get_serial_port_data(port);
2309 d_details = s_priv->device_details;
2311 /* only do something if we have a bulk out endpoint */
2312 if ((this_urb = p_priv->outcont_urb) == NULL) {
2313 dbg("%s - oops no urb.", __func__);
2317 /* Save reset port val for resend.
2318 Don't overwrite resend for open/close condition. */
2319 if ((reset_port + 1) > p_priv->resend_cont)
2320 p_priv->resend_cont = reset_port + 1;
2321 if (this_urb->status == -EINPROGRESS) {
2322 dbg ("%s already writing", __func__);
2327 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2329 /* Only set baud rate if it's changed */
2330 if (p_priv->old_baud != p_priv->baud) {
2331 p_priv->old_baud = p_priv->baud;
2332 msg.setClocking = 0x01;
2333 if (d_details->calculate_baud_rate
2334 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2335 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2336 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2338 p_priv->baud = 9600;
2339 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2340 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2346 /* modes must always be correctly specified */
2347 if (p_priv->baud > 57600)
2349 msg.rxMode = RXMODE_DMA;
2350 msg.txMode = TXMODE_DMA;
2354 msg.rxMode = RXMODE_BYHAND;
2355 msg.txMode = TXMODE_BYHAND;
2358 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2359 switch (p_priv->cflag & CSIZE) {
2361 msg.lcr |= USA_DATABITS_5;
2364 msg.lcr |= USA_DATABITS_6;
2367 msg.lcr |= USA_DATABITS_7;
2370 msg.lcr |= USA_DATABITS_8;
2373 if (p_priv->cflag & PARENB) {
2374 /* note USA_PARITY_NONE == 0 */
2375 msg.lcr |= (p_priv->cflag & PARODD)?
2376 USA_PARITY_ODD: USA_PARITY_EVEN;
2378 if (p_priv->old_cflag != p_priv->cflag) {
2379 p_priv->old_cflag = p_priv->cflag;
2383 if (p_priv->flow_control == flow_cts)
2384 msg.txFlowControl = TXFLOW_CTS;
2385 msg.setTxFlowControl = 0x01;
2386 msg.setRxFlowControl = 0x01;
2388 msg.rxForwardingLength = 16;
2389 msg.rxForwardingTimeout = 16;
2390 msg.txAckSetting = 0;
2395 if (reset_port == 1) {
2396 msg.portEnabled = 1;
2398 msg.txBreak = (p_priv->break_on);
2401 else if (reset_port == 2) {
2402 msg.portEnabled = 0;
2404 /* Sending intermediate configs */
2406 if (port->port.count)
2407 msg.portEnabled = 1;
2408 msg.txBreak = (p_priv->break_on);
2411 /* Do handshaking outputs */
2413 msg.rts = p_priv->rts_state;
2416 msg.dtr = p_priv->dtr_state;
2418 p_priv->resend_cont = 0;
2419 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2421 /* send the data out the device on control endpoint */
2422 this_urb->transfer_buffer_length = sizeof(msg);
2424 this_urb->dev = serial->dev;
2425 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2426 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2431 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2432 struct usb_serial_port *port,
2435 struct keyspan_usa67_portControlMessage msg;
2436 struct keyspan_serial_private *s_priv;
2437 struct keyspan_port_private *p_priv;
2438 const struct keyspan_device_details *d_details;
2439 struct urb *this_urb;
2440 int err, device_port;
2442 dbg ("%s", __func__);
2444 s_priv = usb_get_serial_data(serial);
2445 p_priv = usb_get_serial_port_data(port);
2446 d_details = s_priv->device_details;
2448 this_urb = s_priv->glocont_urb;
2450 /* Work out which port within the device is being setup */
2451 device_port = port->number - port->serial->minor;
2453 /* Make sure we have an urb then send the message */
2454 if (this_urb == NULL) {
2455 dbg("%s - oops no urb for port %d.", __func__,
2460 /* Save reset port val for resend.
2461 Don't overwrite resend for open/close condition. */
2462 if ((reset_port + 1) > p_priv->resend_cont)
2463 p_priv->resend_cont = reset_port + 1;
2464 if (this_urb->status == -EINPROGRESS) {
2465 /* dbg ("%s - already writing", __func__); */
2470 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2472 msg.port = device_port;
2474 /* Only set baud rate if it's changed */
2475 if (p_priv->old_baud != p_priv->baud) {
2476 p_priv->old_baud = p_priv->baud;
2477 msg.setClocking = 0xff;
2478 if (d_details->calculate_baud_rate
2479 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2480 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2481 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__,
2484 msg.baudHi = 125; /* Values for 9600 baud */
2487 msg.setPrescaler = 0xff;
2490 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2491 switch (p_priv->cflag & CSIZE) {
2493 msg.lcr |= USA_DATABITS_5;
2496 msg.lcr |= USA_DATABITS_6;
2499 msg.lcr |= USA_DATABITS_7;
2502 msg.lcr |= USA_DATABITS_8;
2505 if (p_priv->cflag & PARENB) {
2506 /* note USA_PARITY_NONE == 0 */
2507 msg.lcr |= (p_priv->cflag & PARODD)?
2508 USA_PARITY_ODD: USA_PARITY_EVEN;
2512 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2513 msg.xonFlowControl = 0;
2514 msg.setFlowControl = 0xff;
2515 msg.forwardingLength = 16;
2519 if (reset_port == 1) {
2529 msg.returnStatus = 0;
2530 msg.resetDataToggle = 0xff;
2531 } else if (reset_port == 2) {
2541 msg.returnStatus = 0;
2542 msg.resetDataToggle = 0;
2544 /* Sending intermediate configs */
2545 msg._txOn = (! p_priv->break_on);
2548 msg.txBreak = (p_priv->break_on);
2553 msg.returnStatus = 0;
2554 msg.resetDataToggle = 0x0;
2557 /* Do handshaking outputs */
2558 msg.setTxTriState_setRts = 0xff;
2559 msg.txTriState_rts = p_priv->rts_state;
2561 msg.setHskoa_setDtr = 0xff;
2562 msg.hskoa_dtr = p_priv->dtr_state;
2564 p_priv->resend_cont = 0;
2566 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2568 /* send the data out the device on control endpoint */
2569 this_urb->transfer_buffer_length = sizeof(msg);
2570 this_urb->dev = serial->dev;
2572 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2574 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2579 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2581 struct usb_serial *serial = port->serial;
2582 struct keyspan_serial_private *s_priv;
2583 const struct keyspan_device_details *d_details;
2585 dbg ("%s", __func__);
2587 s_priv = usb_get_serial_data(serial);
2588 d_details = s_priv->device_details;
2590 switch (d_details->msg_format) {
2592 keyspan_usa26_send_setup(serial, port, reset_port);
2595 keyspan_usa28_send_setup(serial, port, reset_port);
2598 keyspan_usa49_send_setup(serial, port, reset_port);
2601 keyspan_usa90_send_setup(serial, port, reset_port);
2604 keyspan_usa67_send_setup(serial, port, reset_port);
2610 /* Gets called by the "real" driver (ie once firmware is loaded
2611 and renumeration has taken place. */
2612 static int keyspan_startup (struct usb_serial *serial)
2615 struct usb_serial_port *port;
2616 struct keyspan_serial_private *s_priv;
2617 struct keyspan_port_private *p_priv;
2618 const struct keyspan_device_details *d_details;
2620 dbg("%s", __func__);
2622 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2623 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2625 if (d_details == NULL) {
2626 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2630 /* Setup private data for serial driver */
2631 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2633 dbg("%s - kmalloc for keyspan_serial_private failed.", __func__);
2637 s_priv->device_details = d_details;
2638 usb_set_serial_data(serial, s_priv);
2640 /* Now setup per port private data */
2641 for (i = 0; i < serial->num_ports; i++) {
2642 port = serial->port[i];
2643 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2645 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2648 p_priv->device_details = d_details;
2649 usb_set_serial_port_data(port, p_priv);
2652 keyspan_setup_urbs(serial);
2654 if (s_priv->instat_urb != NULL) {
2655 s_priv->instat_urb->dev = serial->dev;
2656 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2658 dbg("%s - submit instat urb failed %d", __func__,
2661 if (s_priv->indat_urb != NULL) {
2662 s_priv->indat_urb->dev = serial->dev;
2663 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2665 dbg("%s - submit indat urb failed %d", __func__,
2672 static void keyspan_shutdown (struct usb_serial *serial)
2675 struct usb_serial_port *port;
2676 struct keyspan_serial_private *s_priv;
2677 struct keyspan_port_private *p_priv;
2679 dbg("%s", __func__);
2681 s_priv = usb_get_serial_data(serial);
2683 /* Stop reading/writing urbs */
2684 stop_urb(s_priv->instat_urb);
2685 stop_urb(s_priv->glocont_urb);
2686 stop_urb(s_priv->indat_urb);
2687 for (i = 0; i < serial->num_ports; ++i) {
2688 port = serial->port[i];
2689 p_priv = usb_get_serial_port_data(port);
2690 stop_urb(p_priv->inack_urb);
2691 stop_urb(p_priv->outcont_urb);
2692 for (j = 0; j < 2; j++) {
2693 stop_urb(p_priv->in_urbs[j]);
2694 stop_urb(p_priv->out_urbs[j]);
2699 usb_free_urb(s_priv->instat_urb);
2700 usb_free_urb(s_priv->indat_urb);
2701 usb_free_urb(s_priv->glocont_urb);
2702 for (i = 0; i < serial->num_ports; ++i) {
2703 port = serial->port[i];
2704 p_priv = usb_get_serial_port_data(port);
2705 usb_free_urb(p_priv->inack_urb);
2706 usb_free_urb(p_priv->outcont_urb);
2707 for (j = 0; j < 2; j++) {
2708 usb_free_urb(p_priv->in_urbs[j]);
2709 usb_free_urb(p_priv->out_urbs[j]);
2713 /* dbg("Freeing serial->private."); */
2716 /* dbg("Freeing port->private."); */
2717 /* Now free per port private data */
2718 for (i = 0; i < serial->num_ports; i++) {
2719 port = serial->port[i];
2720 kfree(usb_get_serial_port_data(port));
2724 MODULE_AUTHOR( DRIVER_AUTHOR );
2725 MODULE_DESCRIPTION( DRIVER_DESC );
2726 MODULE_LICENSE("GPL");
2728 MODULE_FIRMWARE("keyspan/usa28.fw");
2729 MODULE_FIRMWARE("keyspan/usa28x.fw");
2730 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2731 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2732 MODULE_FIRMWARE("keyspan/usa19.fw");
2733 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2734 MODULE_FIRMWARE("keyspan/mpr.fw");
2735 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2736 MODULE_FIRMWARE("keyspan/usa18x.fw");
2737 MODULE_FIRMWARE("keyspan/usa19w.fw");
2738 MODULE_FIRMWARE("keyspan/usa49w.fw");
2739 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2741 module_param(debug, bool, S_IRUGO | S_IWUSR);
2742 MODULE_PARM_DESC(debug, "Debug enabled or not");