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 2003Apr16 LPM (Keyspan) fix delayed control message resend for multi-port
35 'Open' case. Fix 'mpr' entries in keyspan.h (previously broken)
37 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
38 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
39 Linux source tree. The Linux tree lacked support for the 49WLC and
40 others. The Keyspan patches didn't work with the current kernel.
42 2003jan30 LPM add support for the 49WLC and MPR
44 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
45 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
46 now supported (including QI and QW). Modified port open, port
47 close, and send setup() logic to fix various data and endpoint
48 synchronization bugs and device LED status bugs. Changed keyspan_
49 write_room() to accurately return transmit buffer availability.
50 Changed forwardingLength from 1 to 16 for all adapters.
52 Fri Oct 12 16:45:00 EST 2001
53 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
55 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
56 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
57 now supported (including QI and QW). Modified port open, port
58 close, and send setup() logic to fix various data and endpoint
59 synchronization bugs and device LED status bugs. Changed keyspan_
60 write_room() to accurately return transmit buffer availability.
61 Changed forwardingLength from 1 to 16 for all adapters.
63 Fri Oct 12 16:45:00 EST 2001
64 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
66 Mon Oct 8 14:29:00 EST 2001 hugh
67 Fixed bug that prevented mulitport devices operating correctly
68 if they weren't the first unit attached.
70 Sat Oct 6 12:31:21 EST 2001 hugh
71 Added support for USA-28XA and -28XB, misc cleanups, break support
72 for usa26 based models thanks to David Gibson.
74 Thu May 31 11:56:42 PDT 2001 gkh
75 switched from using spinlock to a semaphore
78 Identify version on module load.
80 (11/01/2000) Adam J. Richter
81 usb_device_id table support.
83 Tue Oct 10 23:15:33 EST 2000 Hugh
84 Merged Paul's changes with my USA-49W mods. Work in progress
87 Wed Jul 19 14:00:42 EST 2000 gkh
88 Added module_init and module_exit functions to handle the fact that
89 this driver is a loadable module now.
91 Tue Jul 18 16:14:52 EST 2000 Hugh
92 Basic character input/output for USA-19 now mostly works,
93 fixed at 9600 baud for the moment.
95 Sat Jul 8 11:11:48 EST 2000 Hugh
96 First public release - nothing works except the firmware upload.
97 Tested on PPC and x86 architectures, seems to behave...
101 #include <linux/config.h>
102 #include <linux/kernel.h>
103 #include <linux/errno.h>
104 #include <linux/init.h>
105 #include <linux/slab.h>
106 #include <linux/tty.h>
107 #include <linux/tty_driver.h>
108 #include <linux/tty_flip.h>
109 #include <linux/module.h>
110 #include <linux/spinlock.h>
111 #include <asm/uaccess.h>
113 #ifdef CONFIG_USB_SERIAL_DEBUG
114 static int debug = 1;
121 #include <linux/usb.h>
123 #include "usb-serial.h"
127 * Version Information
129 #define DRIVER_VERSION "v1.1.4"
130 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
131 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
133 #define INSTAT_BUFLEN 32
134 #define GLOCONT_BUFLEN 64
136 /* Per device and per port private data */
137 struct keyspan_serial_private {
138 /* number of active ports */
139 atomic_t active_count;
141 const struct keyspan_device_details *device_details;
143 struct urb *instat_urb;
144 char instat_buf[INSTAT_BUFLEN];
146 /* XXX this one probably will need a lock */
147 struct urb *glocont_urb;
148 char glocont_buf[GLOCONT_BUFLEN];
151 struct keyspan_port_private {
152 /* Keep track of which input & output endpoints to use */
156 /* Keep duplicate of device details in each port
157 structure as well - simplifies some of the
158 callback functions etc. */
159 const struct keyspan_device_details *device_details;
161 /* Input endpoints and buffer for this port */
162 struct urb *in_urbs[2];
163 char in_buffer[2][64];
164 /* Output endpoints and buffer for this port */
165 struct urb *out_urbs[2];
166 char out_buffer[2][64];
168 /* Input ack endpoint */
169 struct urb *inack_urb;
170 char inack_buffer[1];
172 /* Output control endpoint */
173 struct urb *outcont_urb;
174 char outcont_buffer[64];
176 /* Settings for the port */
180 unsigned int old_cflag;
181 enum {flow_none, flow_cts, flow_xon} flow_control;
182 int rts_state; /* Handshaking pins (outputs) */
184 int cts_state; /* Handshaking pins (inputs) */
190 unsigned long tx_start_time[2];
191 int resend_cont; /* need to resend control packet */
195 /* Include Keyspan message headers. All current Keyspan Adapters
196 make use of one of four message formats which are referred
197 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
198 #include "keyspan_usa26msg.h"
199 #include "keyspan_usa28msg.h"
200 #include "keyspan_usa49msg.h"
201 #include "keyspan_usa90msg.h"
204 /* Functions used by new usb-serial code. */
205 static int __init keyspan_init (void)
207 usb_serial_register (&keyspan_pre_device);
208 usb_serial_register (&keyspan_1port_device);
209 usb_serial_register (&keyspan_2port_device);
210 usb_serial_register (&keyspan_4port_device);
212 info(DRIVER_VERSION ":" DRIVER_DESC);
217 static void __exit keyspan_exit (void)
219 usb_serial_deregister (&keyspan_pre_device);
220 usb_serial_deregister (&keyspan_1port_device);
221 usb_serial_deregister (&keyspan_2port_device);
222 usb_serial_deregister (&keyspan_4port_device);
225 module_init(keyspan_init);
226 module_exit(keyspan_exit);
228 static void keyspan_rx_throttle (struct usb_serial_port *port)
230 dbg("%s - port %d", __FUNCTION__, port->number);
234 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
236 dbg("%s - port %d", __FUNCTION__, port->number);
240 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
242 struct keyspan_port_private *p_priv;
244 dbg("%s", __FUNCTION__);
246 p_priv = (struct keyspan_port_private *)port->private;
248 if (break_state == -1)
249 p_priv->break_on = 1;
251 p_priv->break_on = 0;
253 keyspan_send_setup(port, 0);
257 static void keyspan_set_termios (struct usb_serial_port *port,
258 struct termios *old_termios)
260 int baud_rate, device_port;
261 struct keyspan_port_private *p_priv;
262 const struct keyspan_device_details *d_details;
265 dbg("%s", __FUNCTION__);
267 p_priv = (struct keyspan_port_private *)(port->private);
268 d_details = p_priv->device_details;
269 cflag = port->tty->termios->c_cflag;
270 device_port = port->number - port->serial->minor;
272 /* Baud rate calculation takes baud rate as an integer
273 so other rates can be generated if desired. */
274 baud_rate = tty_get_baud_rate(port->tty);
275 /* If no match or invalid, don't change */
277 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
278 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
279 /* FIXME - more to do here to ensure rate changes cleanly */
280 p_priv->baud = baud_rate;
283 /* set CTS/RTS handshake etc. */
284 p_priv->cflag = cflag;
285 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
287 keyspan_send_setup(port, 0);
290 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
291 unsigned int cmd, unsigned long arg)
293 unsigned int value, set;
294 struct keyspan_port_private *p_priv;
296 p_priv = (struct keyspan_port_private *)(port->private);
300 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
301 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
302 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
303 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
304 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
305 ((p_priv->ri_state) ? TIOCM_RNG : 0);
307 if (put_user(value, (unsigned int *) arg))
312 if (get_user(value, (unsigned int *) arg))
314 p_priv->rts_state = ((value & TIOCM_RTS) ? 1 : 0);
315 p_priv->dtr_state = ((value & TIOCM_DTR) ? 1 : 0);
316 keyspan_send_setup(port, 0);
321 if (get_user(value, (unsigned int *) arg))
323 set = (cmd == TIOCMBIS);
324 if (value & TIOCM_RTS)
325 p_priv->rts_state = set;
326 if (value & TIOCM_DTR)
327 p_priv->dtr_state = set;
328 keyspan_send_setup(port, 0);
335 /* Write function is similar for the four protocols used
336 with only a minor change for usa90 (usa19hs) required */
337 static int keyspan_write(struct usb_serial_port *port, int from_user,
338 const unsigned char *buf, int count)
340 struct keyspan_port_private *p_priv;
341 const struct keyspan_device_details *d_details;
344 struct urb *this_urb;
345 int err, maxDataLen, dataOffset;
347 p_priv = (struct keyspan_port_private *)(port->private);
348 d_details = p_priv->device_details;
350 if (d_details->msg_format == msg_usa90) {
359 dbg("%s - for port %d (%d chars [%x]), flip=%d",
360 __FUNCTION__, port->number, count, buf[0], p_priv->out_flip);
362 for (left = count; left > 0; left -= todo) {
364 if (todo > maxDataLen)
367 flip = p_priv->out_flip;
369 /* Check we have a valid urb/endpoint before we use it... */
370 if ((this_urb = p_priv->out_urbs[flip]) == 0) {
371 /* no bulk out, so return 0 bytes written */
372 dbg("%s - no output urb :(", __FUNCTION__);
376 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
378 if (this_urb->status == -EINPROGRESS) {
379 if (this_urb->transfer_flags & USB_ASYNC_UNLINK)
381 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
383 this_urb->transfer_flags |= USB_ASYNC_UNLINK;
384 usb_unlink_urb(this_urb);
388 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
389 for now so set to zero */
390 ((char *)this_urb->transfer_buffer)[0] = 0;
393 if (copy_from_user(this_urb->transfer_buffer + dataOffset, buf, todo))
396 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
400 /* send the data out the bulk port */
401 this_urb->transfer_buffer_length = todo + dataOffset;
403 this_urb->transfer_flags &= ~USB_ASYNC_UNLINK;
404 this_urb->dev = port->serial->dev;
405 if ((err = usb_submit_urb(this_urb)) != 0) {
406 dbg("usb_submit_urb(write bulk) failed (%d)", err);
408 p_priv->tx_start_time[flip] = jiffies;
410 /* Flip for next time if usa26 or usa28 interface
411 (not used on usa49) */
412 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
418 static void usa26_indat_callback(struct urb *urb)
422 struct usb_serial_port *port;
423 struct tty_struct *tty;
424 unsigned char *data = urb->transfer_buffer;
426 dbg ("%s", __FUNCTION__);
428 endpoint = usb_pipeendpoint(urb->pipe);
431 dbg("%s - nonzero status: %x on endpoint %d.",
432 __FUNCTION__, urb->status, endpoint);
436 port = (struct usb_serial_port *) urb->context;
438 if (urb->actual_length) {
439 /* 0x80 bit is error flag */
440 if ((data[0] & 0x80) == 0) {
441 /* no errors on individual bytes, only possible overrun err*/
442 if (data[0] & RXERROR_OVERRUN)
445 for (i = 1; i < urb->actual_length ; ++i) {
446 tty_insert_flip_char(tty, data[i], err);
449 /* some bytes had errors, every byte has status */
450 dbg("%s - RX error!!!!", __FUNCTION__);
451 for (i = 0; i + 1 < urb->actual_length; i += 2) {
452 int stat = data[i], flag = 0;
453 if (stat & RXERROR_OVERRUN)
455 if (stat & RXERROR_FRAMING)
457 if (stat & RXERROR_PARITY)
459 /* XXX should handle break (0x10) */
460 tty_insert_flip_char(tty, data[i+1], flag);
463 tty_flip_buffer_push(tty);
466 /* Resubmit urb so we continue receiving */
467 urb->dev = port->serial->dev;
468 if (port->open_count)
469 if ((err = usb_submit_urb(urb)) != 0) {
470 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
475 /* Outdat handling is common for all devices */
476 static void usa2x_outdat_callback(struct urb *urb)
478 struct usb_serial_port *port;
479 struct keyspan_port_private *p_priv;
481 port = (struct usb_serial_port *) urb->context;
482 p_priv = (struct keyspan_port_private *)(port->private);
483 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
485 if (port->open_count) {
486 queue_task(&port->tqueue, &tq_immediate);
487 mark_bh(IMMEDIATE_BH);
491 static void usa26_inack_callback(struct urb *urb)
493 dbg ("%s", __FUNCTION__);
497 static void usa26_outcont_callback(struct urb *urb)
499 struct usb_serial_port *port;
500 struct keyspan_port_private *p_priv;
502 port = (struct usb_serial_port *) urb->context;
503 p_priv = (struct keyspan_port_private *)(port->private);
505 if (p_priv->resend_cont) {
506 dbg ("%s - sending setup", __FUNCTION__);
507 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
511 static void usa26_instat_callback(struct urb *urb)
513 unsigned char *data = urb->transfer_buffer;
514 struct keyspan_usa26_portStatusMessage *msg;
515 struct usb_serial *serial;
516 struct usb_serial_port *port;
517 struct keyspan_port_private *p_priv;
518 int old_dcd_state, err;
520 serial = (struct usb_serial *) urb->context;
523 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
526 if (urb->actual_length != 9) {
527 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
531 msg = (struct keyspan_usa26_portStatusMessage *)data;
534 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
535 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
536 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
539 /* Now do something useful with the data */
542 /* Check port number from message and retrieve private data */
543 if (msg->port >= serial->num_ports) {
544 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
547 port = &serial->port[msg->port];
548 p_priv = (struct keyspan_port_private *)(port->private);
550 /* Update handshaking pin state information */
551 old_dcd_state = p_priv->dcd_state;
552 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
553 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
554 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
555 p_priv->ri_state = ((msg->ri) ? 1 : 0);
557 if (port->tty && !C_CLOCAL(port->tty)
558 && old_dcd_state != p_priv->dcd_state) {
560 tty_hangup(port->tty);
562 /* wake_up_interruptible(&p_priv->open_wait); */
565 /* Resubmit urb so we continue receiving */
566 urb->dev = serial->dev;
567 if ((err = usb_submit_urb(urb)) != 0) {
568 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
574 static void usa26_glocont_callback(struct urb *urb)
576 dbg ("%s", __FUNCTION__);
581 static void usa28_indat_callback(struct urb *urb)
584 struct usb_serial_port *port;
585 struct tty_struct *tty;
587 struct keyspan_port_private *p_priv;
589 dbg ("%s", __FUNCTION__);
591 port = (struct usb_serial_port *) urb->context;
592 p_priv = (struct keyspan_port_private *)(port->private);
593 data = urb->transfer_buffer;
595 if (urb != p_priv->in_urbs[p_priv->in_flip])
600 dbg("%s - nonzero status: %x on endpoint %d.",
601 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
605 port = (struct usb_serial_port *) urb->context;
606 p_priv = (struct keyspan_port_private *)(port->private);
607 data = urb->transfer_buffer;
610 if (urb->actual_length) {
611 for (i = 0; i < urb->actual_length ; ++i) {
612 tty_insert_flip_char(tty, data[i], 0);
614 tty_flip_buffer_push(tty);
617 /* Resubmit urb so we continue receiving */
618 urb->dev = port->serial->dev;
619 if (port->open_count)
620 if ((err = usb_submit_urb(urb)) != 0) {
621 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
623 p_priv->in_flip ^= 1;
625 urb = p_priv->in_urbs[p_priv->in_flip];
626 } while (urb->status != -EINPROGRESS);
629 static void usa28_inack_callback(struct urb *urb)
631 dbg ("%s", __FUNCTION__);
634 static void usa28_outcont_callback(struct urb *urb)
636 struct usb_serial_port *port;
637 struct keyspan_port_private *p_priv;
639 port = (struct usb_serial_port *) urb->context;
640 p_priv = (struct keyspan_port_private *)(port->private);
642 if (p_priv->resend_cont) {
643 dbg ("%s - sending setup", __FUNCTION__);
644 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
648 static void usa28_instat_callback(struct urb *urb)
651 unsigned char *data = urb->transfer_buffer;
652 struct keyspan_usa28_portStatusMessage *msg;
653 struct usb_serial *serial;
654 struct usb_serial_port *port;
655 struct keyspan_port_private *p_priv;
658 serial = (struct usb_serial *) urb->context;
661 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
665 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
666 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
670 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
671 data[0], data[1], data[2], data[3], data[4], data[5],
672 data[6], data[7], data[8], data[9], data[10], data[11]);*/
674 /* Now do something useful with the data */
675 msg = (struct keyspan_usa28_portStatusMessage *)data;
678 /* Check port number from message and retrieve private data */
679 if (msg->port >= serial->num_ports) {
680 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
683 port = &serial->port[msg->port];
684 p_priv = (struct keyspan_port_private *)(port->private);
686 /* Update handshaking pin state information */
687 old_dcd_state = p_priv->dcd_state;
688 p_priv->cts_state = ((msg->cts) ? 1 : 0);
689 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
690 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
691 p_priv->ri_state = ((msg->ri) ? 1 : 0);
693 if (port->tty && !C_CLOCAL(port->tty)
694 && old_dcd_state != p_priv->dcd_state) {
696 tty_hangup(port->tty);
698 /* wake_up_interruptible(&p_priv->open_wait); */
701 /* Resubmit urb so we continue receiving */
702 urb->dev = serial->dev;
703 if ((err = usb_submit_urb(urb)) != 0) {
704 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
710 static void usa28_glocont_callback(struct urb *urb)
712 dbg ("%s", __FUNCTION__);
716 static void usa49_glocont_callback(struct urb *urb)
718 struct usb_serial *serial;
719 struct usb_serial_port *port;
720 struct keyspan_port_private *p_priv;
723 dbg ("%s", __FUNCTION__);
725 serial = (struct usb_serial *) urb->context;
726 for (i = 0; i < serial->num_ports; ++i) {
727 port = &serial->port[i];
728 p_priv = (struct keyspan_port_private *)(port->private);
730 if (p_priv->resend_cont) {
731 dbg ("%s - sending setup", __FUNCTION__);
732 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
738 /* This is actually called glostat in the Keyspan
740 static void usa49_instat_callback(struct urb *urb)
743 unsigned char *data = urb->transfer_buffer;
744 struct keyspan_usa49_portStatusMessage *msg;
745 struct usb_serial *serial;
746 struct usb_serial_port *port;
747 struct keyspan_port_private *p_priv;
750 dbg ("%s", __FUNCTION__);
752 serial = (struct usb_serial *) urb->context;
755 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
759 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
760 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
764 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
765 data[0], data[1], data[2], data[3], data[4], data[5],
766 data[6], data[7], data[8], data[9], data[10]);*/
768 /* Now do something useful with the data */
769 msg = (struct keyspan_usa49_portStatusMessage *)data;
771 /* Check port number from message and retrieve private data */
772 if (msg->portNumber >= serial->num_ports) {
773 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
776 port = &serial->port[msg->portNumber];
777 p_priv = (struct keyspan_port_private *)(port->private);
779 /* Update handshaking pin state information */
780 old_dcd_state = p_priv->dcd_state;
781 p_priv->cts_state = ((msg->cts) ? 1 : 0);
782 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
783 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
784 p_priv->ri_state = ((msg->ri) ? 1 : 0);
786 if (port->tty && !C_CLOCAL(port->tty)
787 && old_dcd_state != p_priv->dcd_state) {
789 tty_hangup(port->tty);
791 /* wake_up_interruptible(&p_priv->open_wait); */
794 /* Resubmit urb so we continue receiving */
795 urb->dev = serial->dev;
797 if ((err = usb_submit_urb(urb)) != 0) {
798 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
804 static void usa49_inack_callback(struct urb *urb)
806 dbg ("%s", __FUNCTION__);
809 static void usa49_indat_callback(struct urb *urb)
813 struct usb_serial_port *port;
814 struct tty_struct *tty;
815 unsigned char *data = urb->transfer_buffer;
817 dbg ("%s", __FUNCTION__);
819 endpoint = usb_pipeendpoint(urb->pipe);
822 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
823 urb->status, endpoint);
827 port = (struct usb_serial_port *) urb->context;
829 if (urb->actual_length) {
830 /* 0x80 bit is error flag */
831 if ((data[0] & 0x80) == 0) {
832 /* no error on any byte */
833 for (i = 1; i < urb->actual_length ; ++i) {
834 tty_insert_flip_char(tty, data[i], 0);
837 /* some bytes had errors, every byte has status */
838 for (i = 0; i + 1 < urb->actual_length; i += 2) {
839 int stat = data[i], flag = 0;
840 if (stat & RXERROR_OVERRUN)
842 if (stat & RXERROR_FRAMING)
844 if (stat & RXERROR_PARITY)
846 /* XXX should handle break (0x10) */
847 tty_insert_flip_char(tty, data[i+1], flag);
850 tty_flip_buffer_push(tty);
853 /* Resubmit urb so we continue receiving */
854 urb->dev = port->serial->dev;
855 if (port->open_count)
856 if ((err = usb_submit_urb(urb)) != 0) {
857 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
861 /* not used, usa-49 doesn't have per-port control endpoints */
862 static void usa49_outcont_callback(struct urb *urb)
864 dbg ("%s", __FUNCTION__);
868 static void usa90_indat_callback(struct urb *urb)
872 struct usb_serial_port *port;
873 struct keyspan_port_private *p_priv;
874 struct tty_struct *tty;
875 unsigned char *data = urb->transfer_buffer;
877 dbg ("%s", __FUNCTION__);
879 endpoint = usb_pipeendpoint(urb->pipe);
883 dbg("%s - nonzero status: %x on endpoint %d.",
884 __FUNCTION__, urb->status, endpoint);
888 port = (struct usb_serial_port *) urb->context;
889 p_priv = (struct keyspan_port_private *)(port->private);
892 if (urb->actual_length) {
894 /* if current mode is DMA, looks like usa28 format
895 otherwise looks like usa26 data format */
897 if (p_priv->baud > 57600) {
898 for (i = 0; i < urb->actual_length ; ++i)
899 tty_insert_flip_char(tty, data[i], 0);
903 /* 0x80 bit is error flag */
904 if ((data[0] & 0x80) == 0) {
905 /* no errors on individual bytes, only possible overrun err*/
906 if (data[0] & RXERROR_OVERRUN)
909 for (i = 1; i < urb->actual_length ; ++i)
910 tty_insert_flip_char(tty, data[i], err);
914 /* some bytes had errors, every byte has status */
915 dbg("%s - RX error!!!!", __FUNCTION__);
916 for (i = 0; i + 1 < urb->actual_length; i += 2) {
917 int stat = data[i], flag = 0;
918 if (stat & RXERROR_OVERRUN)
920 if (stat & RXERROR_FRAMING)
922 if (stat & RXERROR_PARITY)
924 /* XXX should handle break (0x10) */
925 tty_insert_flip_char(tty, data[i+1], flag);
929 tty_flip_buffer_push(tty);
932 /* Resubmit urb so we continue receiving */
933 urb->dev = port->serial->dev;
934 if (port->open_count)
935 if ((err = usb_submit_urb(urb)) != 0) {
936 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
942 static void usa90_instat_callback(struct urb *urb)
944 unsigned char *data = urb->transfer_buffer;
945 struct keyspan_usa90_portStatusMessage *msg;
946 struct usb_serial *serial;
947 struct usb_serial_port *port;
948 struct keyspan_port_private *p_priv;
949 int old_dcd_state, err;
951 serial = (struct usb_serial *) urb->context;
954 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
957 if (urb->actual_length < 14) {
958 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
962 msg = (struct keyspan_usa90_portStatusMessage *)data;
964 /* Now do something useful with the data */
966 port = &serial->port[0];
967 p_priv = (struct keyspan_port_private *)(port->private);
969 /* Update handshaking pin state information */
970 old_dcd_state = p_priv->dcd_state;
971 p_priv->cts_state = ((msg->cts) ? 1 : 0);
972 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
973 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
974 p_priv->ri_state = ((msg->ri) ? 1 : 0);
976 if (port->tty && !C_CLOCAL(port->tty)
977 && old_dcd_state != p_priv->dcd_state) {
979 tty_hangup(port->tty);
981 /* wake_up_interruptible(&p_priv->open_wait); */
984 /* Resubmit urb so we continue receiving */
985 urb->dev = serial->dev;
986 if ((err = usb_submit_urb(urb)) != 0) {
987 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
993 static void usa90_outcont_callback(struct urb *urb)
995 struct usb_serial_port *port;
996 struct keyspan_port_private *p_priv;
998 port = (struct usb_serial_port *) urb->context;
999 p_priv = (struct keyspan_port_private *)(port->private);
1001 if (p_priv->resend_cont) {
1002 dbg ("%s - sending setup", __FUNCTION__);
1003 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1008 static int keyspan_write_room (struct usb_serial_port *port)
1010 struct keyspan_port_private *p_priv;
1011 const struct keyspan_device_details *d_details;
1013 struct urb *this_urb;
1015 dbg("%s", __FUNCTION__);
1016 p_priv = (struct keyspan_port_private *)(port->private);
1017 d_details = p_priv->device_details;
1019 if (d_details->msg_format == msg_usa90)
1023 flip = p_priv->out_flip;
1025 /* Check both endpoints to see if any are available. */
1026 if ((this_urb = p_priv->out_urbs[flip]) != 0) {
1027 if (this_urb->status != -EINPROGRESS)
1029 flip = (flip + 1) & d_details->outdat_endp_flip;
1030 if ((this_urb = p_priv->out_urbs[flip]) != 0)
1031 if (this_urb->status != -EINPROGRESS)
1038 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1044 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1046 struct keyspan_port_private *p_priv;
1047 struct keyspan_serial_private *s_priv;
1048 struct usb_serial *serial = port->serial;
1049 const struct keyspan_device_details *d_details;
1051 int baud_rate, device_port;
1055 s_priv = (struct keyspan_serial_private *)(serial->private);
1056 p_priv = (struct keyspan_port_private *)(port->private);
1057 d_details = p_priv->device_details;
1059 dbg("%s - port%d.", __FUNCTION__, port->number);
1061 /* Set some sane defaults */
1062 p_priv->rts_state = 1;
1063 p_priv->dtr_state = 1;
1064 p_priv->baud = 9600;
1066 /* force baud and lcr to be set on open */
1067 p_priv->old_baud = 0;
1068 p_priv->old_cflag = 0;
1070 p_priv->out_flip = 0;
1071 p_priv->in_flip = 0;
1073 /* Reset low level data toggle and start reading from endpoints */
1074 for (i = 0; i < 2; i++) {
1075 if ((urb = p_priv->in_urbs[i]) == NULL)
1077 urb->dev = serial->dev;
1079 /* make sure endpoint data toggle is synchronized with the device */
1081 usb_clear_halt(urb->dev, urb->pipe);
1083 if ((err = usb_submit_urb(urb)) != 0) {
1084 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1088 /* Reset low level data toggle on out endpoints */
1089 for (i = 0; i < 2; i++) {
1090 if ((urb = p_priv->out_urbs[i]) == NULL)
1092 urb->dev = serial->dev;
1093 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1096 /* get the terminal config for the setup message now so we don't
1097 * need to send 2 of them */
1099 cflag = port->tty->termios->c_cflag;
1100 device_port = port->number - port->serial->minor;
1102 /* Baud rate calculation takes baud rate as an integer
1103 so other rates can be generated if desired. */
1104 baud_rate = tty_get_baud_rate(port->tty);
1105 /* If no match or invalid, leave as default */
1107 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1108 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1109 p_priv->baud = baud_rate;
1112 /* set CTS/RTS handshake etc. */
1113 p_priv->cflag = cflag;
1114 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1116 keyspan_send_setup(port, 1);
1118 //keyspan_set_termios(port, NULL);
1123 static inline void stop_urb(struct urb *urb)
1125 if (urb && urb->status == -EINPROGRESS) {
1126 urb->transfer_flags &= ~USB_ASYNC_UNLINK;
1127 usb_unlink_urb(urb);
1131 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1134 struct usb_serial *serial;
1135 struct keyspan_serial_private *s_priv;
1136 struct keyspan_port_private *p_priv;
1138 serial = get_usb_serial (port, __FUNCTION__);
1142 dbg("%s", __FUNCTION__);
1143 s_priv = (struct keyspan_serial_private *)(serial->private);
1144 p_priv = (struct keyspan_port_private *)(port->private);
1146 p_priv->rts_state = 0;
1147 p_priv->dtr_state = 0;
1150 keyspan_send_setup(port, 2);
1151 /* pilot-xfer seems to work best with this delay */
1153 // keyspan_set_termios(port, NULL);
1156 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1157 dbg("%s - urb in progress", __FUNCTION__);
1160 p_priv->out_flip = 0;
1161 p_priv->in_flip = 0;
1164 /* Stop reading/writing urbs */
1165 stop_urb(p_priv->inack_urb);
1166 /* stop_urb(p_priv->outcont_urb); */
1167 for (i = 0; i < 2; i++) {
1168 stop_urb(p_priv->in_urbs[i]);
1169 stop_urb(p_priv->out_urbs[i]);
1176 /* download the firmware to a pre-renumeration device */
1177 static int keyspan_fake_startup (struct usb_serial *serial)
1180 const struct ezusb_hex_record *record;
1183 dbg("Keyspan startup version %04x product %04x",
1184 serial->dev->descriptor.bcdDevice,
1185 serial->dev->descriptor.idProduct);
1187 if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
1188 dbg("Firmware already loaded. Quitting.");
1192 /* Select firmware image on the basis of idProduct */
1193 switch (serial->dev->descriptor.idProduct) {
1194 case keyspan_usa28_pre_product_id:
1195 record = &keyspan_usa28_firmware[0];
1199 case keyspan_usa28x_pre_product_id:
1200 record = &keyspan_usa28x_firmware[0];
1204 case keyspan_usa28xa_pre_product_id:
1205 record = &keyspan_usa28xa_firmware[0];
1206 fw_name = "USA28XA";
1209 case keyspan_usa28xb_pre_product_id:
1210 record = &keyspan_usa28xb_firmware[0];
1211 fw_name = "USA28XB";
1214 case keyspan_usa19_pre_product_id:
1215 record = &keyspan_usa19_firmware[0];
1219 case keyspan_usa19qi_pre_product_id:
1220 record = &keyspan_usa19qi_firmware[0];
1221 fw_name = "USA19QI";
1224 case keyspan_mpr_pre_product_id:
1225 record = &keyspan_mpr_firmware[0];
1229 case keyspan_usa19qw_pre_product_id:
1230 record = &keyspan_usa19qw_firmware[0];
1231 fw_name = "USA19QI";
1234 case keyspan_usa18x_pre_product_id:
1235 record = &keyspan_usa18x_firmware[0];
1239 case keyspan_usa19w_pre_product_id:
1240 record = &keyspan_usa19w_firmware[0];
1244 case keyspan_usa49w_pre_product_id:
1245 record = &keyspan_usa49w_firmware[0];
1249 case keyspan_usa49wlc_pre_product_id:
1250 record = &keyspan_usa49wlc_firmware[0];
1251 fw_name = "USA49WLC";
1256 fw_name = "Unknown";
1260 if (record == NULL) {
1261 err("Required keyspan firmware image (%s) unavailable.", fw_name);
1265 dbg("Uploading Keyspan %s firmware.", fw_name);
1267 /* download the firmware image */
1268 response = ezusb_set_reset(serial, 1);
1270 while(record->address != 0xffff) {
1271 response = ezusb_writememory(serial, record->address,
1272 (unsigned char *)record->data,
1273 record->data_size, 0xa0);
1275 err("ezusb_writememory failed for Keyspan"
1276 "firmware (%d %04X %p %d)",
1278 record->address, record->data, record->data_size);
1283 /* bring device out of reset. Renumeration will occur in a
1284 moment and the new device will bind to the real driver */
1285 response = ezusb_set_reset(serial, 0);
1287 /* we don't want this device to have a driver assigned to it. */
1291 /* Helper functions used by keyspan_setup_urbs */
1292 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1293 int dir, void *ctx, char *buf, int len,
1294 void (*callback)(struct urb *))
1299 return NULL; /* endpoint not needed */
1301 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1302 urb = usb_alloc_urb(0); /* No ISO */
1304 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1308 /* Fill URB using supplied data. */
1309 FILL_BULK_URB(urb, serial->dev,
1310 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1311 buf, len, callback, ctx);
1316 static struct callbacks {
1317 void (*instat_callback)(struct urb *);
1318 void (*glocont_callback)(struct urb *);
1319 void (*indat_callback)(struct urb *);
1320 void (*outdat_callback)(struct urb *);
1321 void (*inack_callback)(struct urb *);
1322 void (*outcont_callback)(struct urb *);
1323 } keyspan_callbacks[] = {
1325 /* msg_usa26 callbacks */
1326 .instat_callback = usa26_instat_callback,
1327 .glocont_callback = usa26_glocont_callback,
1328 .indat_callback = usa26_indat_callback,
1329 .outdat_callback = usa2x_outdat_callback,
1330 .inack_callback = usa26_inack_callback,
1331 .outcont_callback = usa26_outcont_callback,
1333 /* msg_usa28 callbacks */
1334 .instat_callback = usa28_instat_callback,
1335 .glocont_callback = usa28_glocont_callback,
1336 .indat_callback = usa28_indat_callback,
1337 .outdat_callback = usa2x_outdat_callback,
1338 .inack_callback = usa28_inack_callback,
1339 .outcont_callback = usa28_outcont_callback,
1341 /* msg_usa49 callbacks */
1342 .instat_callback = usa49_instat_callback,
1343 .glocont_callback = usa49_glocont_callback,
1344 .indat_callback = usa49_indat_callback,
1345 .outdat_callback = usa2x_outdat_callback,
1346 .inack_callback = usa49_inack_callback,
1347 .outcont_callback = usa49_outcont_callback,
1349 /* msg_usa90 callbacks */
1350 .instat_callback = usa90_instat_callback,
1351 .glocont_callback = usa28_glocont_callback,
1352 .indat_callback = usa90_indat_callback,
1353 .outdat_callback = usa2x_outdat_callback,
1354 .inack_callback = usa28_inack_callback,
1355 .outcont_callback = usa90_outcont_callback,
1359 /* Generic setup urbs function that uses
1360 data in device_details */
1361 static void keyspan_setup_urbs(struct usb_serial *serial)
1364 struct keyspan_serial_private *s_priv;
1365 const struct keyspan_device_details *d_details;
1366 struct usb_serial_port *port;
1367 struct keyspan_port_private *p_priv;
1368 struct callbacks *cback;
1371 dbg ("%s", __FUNCTION__);
1373 s_priv = (struct keyspan_serial_private *)(serial->private);
1374 d_details = s_priv->device_details;
1376 /* Setup values for the various callback routines */
1377 cback = &keyspan_callbacks[d_details->msg_format];
1379 /* Allocate and set up urbs for each one that is in use,
1380 starting with instat endpoints */
1381 s_priv->instat_urb = keyspan_setup_urb
1382 (serial, d_details->instat_endpoint, USB_DIR_IN,
1383 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1384 cback->instat_callback);
1386 s_priv->glocont_urb = keyspan_setup_urb
1387 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1388 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1389 cback->glocont_callback);
1391 /* Setup endpoints for each port specific thing */
1392 for (i = 0; i < d_details->num_ports; i ++) {
1393 port = &serial->port[i];
1394 p_priv = (struct keyspan_port_private *)(port->private);
1396 /* Do indat endpoints first, once for each flip */
1397 endp = d_details->indat_endpoints[i];
1398 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1399 p_priv->in_urbs[j] = keyspan_setup_urb
1400 (serial, endp, USB_DIR_IN, port,
1401 p_priv->in_buffer[j], 64,
1402 cback->indat_callback);
1405 p_priv->in_urbs[j] = NULL;
1407 /* outdat endpoints also have flip */
1408 endp = d_details->outdat_endpoints[i];
1409 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1410 p_priv->out_urbs[j] = keyspan_setup_urb
1411 (serial, endp, USB_DIR_OUT, port,
1412 p_priv->out_buffer[j], 64,
1413 cback->outdat_callback);
1416 p_priv->out_urbs[j] = NULL;
1418 /* inack endpoint */
1419 p_priv->inack_urb = keyspan_setup_urb
1420 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1421 port, p_priv->inack_buffer, 1, cback->inack_callback);
1423 /* outcont endpoint */
1424 p_priv->outcont_urb = keyspan_setup_urb
1425 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1426 port, p_priv->outcont_buffer, 64,
1427 cback->outcont_callback);
1432 /* usa19 function doesn't require prescaler */
1433 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1434 u8 *rate_low, u8 *prescaler, int portnum)
1436 u32 b16, /* baud rate times 16 (actual rate used internally) */
1438 cnt; /* inverse of divisor (programmed into 8051) */
1440 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1442 /* prevent divide by zero... */
1443 if( (b16 = (baud_rate * 16L)) == 0) {
1444 return (KEYSPAN_INVALID_BAUD_RATE);
1447 /* Any "standard" rate over 57k6 is marginal on the USA-19
1448 as we run out of divisor resolution. */
1449 if (baud_rate > 57600) {
1450 return (KEYSPAN_INVALID_BAUD_RATE);
1453 /* calculate the divisor and the counter (its inverse) */
1454 if( (div = (baudclk / b16)) == 0) {
1455 return (KEYSPAN_INVALID_BAUD_RATE);
1462 return (KEYSPAN_INVALID_BAUD_RATE);
1465 /* return the counter values if non-null */
1467 *rate_low = (u8) (cnt & 0xff);
1470 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1472 if (rate_low && rate_hi) {
1473 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1476 return (KEYSPAN_BAUD_RATE_OK);
1479 /* usa19hs function doesn't require prescaler */
1480 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1481 u8 *rate_low, u8 *prescaler, int portnum)
1483 u32 b16, /* baud rate times 16 (actual rate used internally) */
1486 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1488 /* prevent divide by zero... */
1489 if( (b16 = (baud_rate * 16L)) == 0)
1490 return (KEYSPAN_INVALID_BAUD_RATE);
1494 /* calculate the divisor */
1495 if( (div = (baudclk / b16)) == 0)
1496 return (KEYSPAN_INVALID_BAUD_RATE);
1499 return (KEYSPAN_INVALID_BAUD_RATE);
1501 /* return the counter values if non-null */
1503 *rate_low = (u8) (div & 0xff);
1506 *rate_hi = (u8) ((div >> 8) & 0xff);
1508 if (rate_low && rate_hi)
1509 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1511 return (KEYSPAN_BAUD_RATE_OK);
1514 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1515 u8 *rate_low, u8 *prescaler, int portnum)
1517 u32 b16, /* baud rate times 16 (actual rate used internally) */
1518 clk, /* clock with 13/8 prescaler */
1519 div, /* divisor using 13/8 prescaler */
1520 res, /* resulting baud rate using 13/8 prescaler */
1521 diff, /* error using 13/8 prescaler */
1526 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1528 /* prevent divide by zero */
1529 if( (b16 = baud_rate * 16L) == 0) {
1530 return (KEYSPAN_INVALID_BAUD_RATE);
1533 /* Calculate prescaler by trying them all and looking
1536 /* start with largest possible difference */
1537 smallest_diff = 0xffffffff;
1539 /* 0 is an invalid prescaler, used as a flag */
1542 for(i = 8; i <= 0xff; ++i) {
1543 clk = (baudclk * 8) / (u32) i;
1545 if( (div = clk / b16) == 0) {
1550 diff= (res > b16) ? (res-b16) : (b16-res);
1552 if(diff < smallest_diff) {
1554 smallest_diff = diff;
1558 if(best_prescaler == 0) {
1559 return (KEYSPAN_INVALID_BAUD_RATE);
1562 clk = (baudclk * 8) / (u32) best_prescaler;
1565 /* return the divisor and prescaler if non-null */
1567 *rate_low = (u8) (div & 0xff);
1570 *rate_hi = (u8) ((div >> 8) & 0xff);
1573 *prescaler = best_prescaler;
1574 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1576 return (KEYSPAN_BAUD_RATE_OK);
1579 /* USA-28 supports different maximum baud rates on each port */
1580 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1581 u8 *rate_low, u8 *prescaler, int portnum)
1583 u32 b16, /* baud rate times 16 (actual rate used internally) */
1585 cnt; /* inverse of divisor (programmed into 8051) */
1587 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1589 /* prevent divide by zero */
1590 if ((b16 = baud_rate * 16L) == 0)
1591 return (KEYSPAN_INVALID_BAUD_RATE);
1593 /* calculate the divisor and the counter (its inverse) */
1594 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1595 return (KEYSPAN_INVALID_BAUD_RATE);
1601 /* check for out of range, based on portnum,
1602 and return result */
1605 return (KEYSPAN_INVALID_BAUD_RATE);
1610 return (KEYSPAN_INVALID_BAUD_RATE);
1614 return (KEYSPAN_INVALID_BAUD_RATE);
1618 /* return the counter values if not NULL
1619 (port 1 will ignore retHi) */
1621 *rate_low = (u8) (cnt & 0xff);
1624 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1626 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1627 return (KEYSPAN_BAUD_RATE_OK);
1630 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1631 struct usb_serial_port *port,
1634 struct keyspan_usa26_portControlMessage msg;
1635 struct keyspan_serial_private *s_priv;
1636 struct keyspan_port_private *p_priv;
1637 const struct keyspan_device_details *d_details;
1639 struct urb *this_urb;
1640 int device_port, err;
1642 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1644 s_priv = (struct keyspan_serial_private *)(serial->private);
1645 p_priv = (struct keyspan_port_private *)(port->private);
1646 d_details = s_priv->device_details;
1647 device_port = port->number - port->serial->minor;
1649 outcont_urb = d_details->outcont_endpoints[port->number];
1650 this_urb = p_priv->outcont_urb;
1652 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1654 /* Make sure we have an urb then send the message */
1655 if (this_urb == NULL) {
1656 dbg("%s - oops no urb.", __FUNCTION__);
1660 /* Save reset port val for resend.
1661 Don't overwrite resend for open/close condition. */
1662 if ((reset_port + 1) > p_priv->resend_cont)
1663 p_priv->resend_cont = reset_port + 1;
1664 if (this_urb->status == -EINPROGRESS) {
1665 /* dbg ("%s - already writing", __FUNCTION__); */
1670 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1672 /* Only set baud rate if it's changed */
1673 if (p_priv->old_baud != p_priv->baud) {
1674 p_priv->old_baud = p_priv->baud;
1675 msg.setClocking = 0xff;
1676 if (d_details->calculate_baud_rate
1677 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1678 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1679 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1682 msg.baudHi = 125; /* Values for 9600 baud */
1685 msg.setPrescaler = 0xff;
1688 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1689 switch (p_priv->cflag & CSIZE) {
1691 msg.lcr |= USA_DATABITS_5;
1694 msg.lcr |= USA_DATABITS_6;
1697 msg.lcr |= USA_DATABITS_7;
1700 msg.lcr |= USA_DATABITS_8;
1703 if (p_priv->cflag & PARENB) {
1704 /* note USA_PARITY_NONE == 0 */
1705 msg.lcr |= (p_priv->cflag & PARODD)?
1706 USA_PARITY_ODD: USA_PARITY_EVEN;
1710 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1711 msg.xonFlowControl = 0;
1712 msg.setFlowControl = 0xff;
1713 msg.forwardingLength = 16;
1718 if (reset_port == 1) {
1727 msg.returnStatus = 0;
1728 msg.resetDataToggle = 0xff;
1732 else if (reset_port == 2) {
1741 msg.returnStatus = 0;
1742 msg.resetDataToggle = 0;
1745 /* Sending intermediate configs */
1747 msg._txOn = (! p_priv->break_on);
1750 msg.txBreak = (p_priv->break_on);
1755 msg.returnStatus = 0;
1756 msg.resetDataToggle = 0x0;
1759 /* Do handshaking outputs */
1760 msg.setTxTriState_setRts = 0xff;
1761 msg.txTriState_rts = p_priv->rts_state;
1763 msg.setHskoa_setDtr = 0xff;
1764 msg.hskoa_dtr = p_priv->dtr_state;
1766 p_priv->resend_cont = 0;
1767 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1769 /* send the data out the device on control endpoint */
1770 this_urb->transfer_buffer_length = sizeof(msg);
1772 this_urb->dev = serial->dev;
1773 if ((err = usb_submit_urb(this_urb)) != 0) {
1774 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1778 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1779 outcont_urb, this_urb->transfer_buffer_length,
1780 usb_pipeendpoint(this_urb->pipe));
1787 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1788 struct usb_serial_port *port,
1791 struct keyspan_usa28_portControlMessage msg;
1792 struct keyspan_serial_private *s_priv;
1793 struct keyspan_port_private *p_priv;
1794 const struct keyspan_device_details *d_details;
1795 struct urb *this_urb;
1796 int device_port, err;
1798 dbg ("%s", __FUNCTION__);
1800 s_priv = (struct keyspan_serial_private *)(serial->private);
1801 p_priv = (struct keyspan_port_private *)(port->private);
1802 d_details = s_priv->device_details;
1803 device_port = port->number - port->serial->minor;
1805 /* only do something if we have a bulk out endpoint */
1806 if ((this_urb = p_priv->outcont_urb) == NULL) {
1807 dbg("%s - oops no urb.", __FUNCTION__);
1811 /* Save reset port val for resend.
1812 Don't overwrite resend for open/close condition. */
1813 if ((reset_port + 1) > p_priv->resend_cont)
1814 p_priv->resend_cont = reset_port + 1;
1815 if (this_urb->status == -EINPROGRESS) {
1816 dbg ("%s already writing", __FUNCTION__);
1821 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1823 msg.setBaudRate = 1;
1824 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1825 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1826 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1828 msg.baudHi = 0xb2; /* Values for 9600 baud */
1831 /* If parity is enabled, we must calculate it ourselves. */
1832 msg.parity = 0; /* XXX for now */
1834 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1835 msg.xonFlowControl = 0;
1837 /* Do handshaking outputs, DTR is inverted relative to RTS */
1838 msg.rts = p_priv->rts_state;
1839 msg.dtr = p_priv->dtr_state;
1841 msg.forwardingLength = 16;
1843 msg.breakThreshold = 45;
1847 /*msg.returnStatus = 1;
1848 msg.resetDataToggle = 0xff;*/
1850 if (reset_port == 1) {
1854 msg.txForceXoff = 0;
1860 msg.returnStatus = 0;
1861 msg.resetDataToggle = 0xff;
1864 else if (reset_port == 2) {
1868 msg.txForceXoff = 0;
1874 msg.returnStatus = 0;
1875 msg.resetDataToggle = 0;
1877 /* Sending intermediate configs */
1879 msg._txOn = (! p_priv->break_on);
1882 msg.txForceXoff = 0;
1883 msg.txBreak = (p_priv->break_on);
1888 msg.returnStatus = 0;
1889 msg.resetDataToggle = 0x0;
1892 p_priv->resend_cont = 0;
1893 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1895 /* send the data out the device on control endpoint */
1896 this_urb->transfer_buffer_length = sizeof(msg);
1898 this_urb->dev = serial->dev;
1899 if ((err = usb_submit_urb(this_urb)) != 0) {
1900 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1904 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1905 this_urb->transfer_buffer_length);
1912 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1913 struct usb_serial_port *port,
1916 struct keyspan_usa49_portControlMessage msg;
1917 struct keyspan_serial_private *s_priv;
1918 struct keyspan_port_private *p_priv;
1919 const struct keyspan_device_details *d_details;
1921 struct urb *this_urb;
1922 int err, device_port;
1924 dbg ("%s", __FUNCTION__);
1926 s_priv = (struct keyspan_serial_private *)(serial->private);
1927 p_priv = (struct keyspan_port_private *)(port->private);
1928 d_details = s_priv->device_details;
1930 glocont_urb = d_details->glocont_endpoint;
1931 this_urb = s_priv->glocont_urb;
1933 /* Work out which port within the device is being setup */
1934 device_port = port->number - port->serial->minor;
1936 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1938 /* Make sure we have an urb then send the message */
1939 if (this_urb == NULL) {
1940 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1944 /* Save reset port val for resend.
1945 Don't overwrite resend for open/close condition. */
1946 if ((reset_port+1) > p_priv->resend_cont)
1947 p_priv->resend_cont = reset_port + 1;
1948 if (this_urb->status == -EINPROGRESS) {
1949 /* dbg ("%s - already writing", __FUNCTION__); */
1954 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1956 /*msg.portNumber = port->number;*/
1957 msg.portNumber = device_port;
1959 /* Only set baud rate if it's changed */
1960 if (p_priv->old_baud != p_priv->baud) {
1961 p_priv->old_baud = p_priv->baud;
1962 msg.setClocking = 0xff;
1963 if (d_details->calculate_baud_rate
1964 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1965 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1966 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1969 msg.baudHi = 125; /* Values for 9600 baud */
1972 //msg.setPrescaler = 0xff;
1975 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1976 switch (p_priv->cflag & CSIZE) {
1978 msg.lcr |= USA_DATABITS_5;
1981 msg.lcr |= USA_DATABITS_6;
1984 msg.lcr |= USA_DATABITS_7;
1987 msg.lcr |= USA_DATABITS_8;
1990 if (p_priv->cflag & PARENB) {
1991 /* note USA_PARITY_NONE == 0 */
1992 msg.lcr |= (p_priv->cflag & PARODD)?
1993 USA_PARITY_ODD: USA_PARITY_EVEN;
1997 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1998 msg.xonFlowControl = 0;
1999 msg.setFlowControl = 0xff;
2001 msg.forwardingLength = 16;
2006 if (reset_port == 1) {
2015 msg.returnStatus = 0;
2016 msg.resetDataToggle = 0xff;
2018 msg.disablePort = 0;
2021 else if (reset_port == 2) {
2030 msg.returnStatus = 0;
2031 msg.resetDataToggle = 0;
2033 msg.disablePort = 1;
2035 /* Sending intermediate configs */
2037 msg._txOn = (! p_priv->break_on);
2040 msg.txBreak = (p_priv->break_on);
2045 msg.returnStatus = 0;
2046 msg.resetDataToggle = 0x0;
2048 msg.disablePort = 0;
2051 /* Do handshaking outputs */
2053 msg.rts = p_priv->rts_state;
2056 msg.dtr = p_priv->dtr_state;
2058 p_priv->resend_cont = 0;
2059 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2061 /* send the data out the device on control endpoint */
2062 this_urb->transfer_buffer_length = sizeof(msg);
2064 this_urb->dev = serial->dev;
2065 if ((err = usb_submit_urb(this_urb)) != 0) {
2066 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2070 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2071 outcont_urb, this_urb->transfer_buffer_length,
2072 usb_pipeendpoint(this_urb->pipe));
2079 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2080 struct usb_serial_port *port,
2083 struct keyspan_usa90_portControlMessage msg;
2084 struct keyspan_serial_private *s_priv;
2085 struct keyspan_port_private *p_priv;
2086 const struct keyspan_device_details *d_details;
2087 struct urb *this_urb;
2091 dbg ("%s", __FUNCTION__);
2093 s_priv = (struct keyspan_serial_private *)(serial->private);
2094 p_priv = (struct keyspan_port_private *)(port->private);
2095 d_details = s_priv->device_details;
2097 /* only do something if we have a bulk out endpoint */
2098 if ((this_urb = p_priv->outcont_urb) == NULL) {
2099 dbg("%s - oops no urb.", __FUNCTION__);
2103 /* Save reset port val for resend.
2104 Don't overwrite resend for open/close condition. */
2105 if ((reset_port + 1) > p_priv->resend_cont)
2106 p_priv->resend_cont = reset_port + 1;
2107 if (this_urb->status == -EINPROGRESS) {
2108 dbg ("%s already writing", __FUNCTION__);
2113 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2115 /* Only set baud rate if it's changed */
2116 if (p_priv->old_baud != p_priv->baud) {
2117 p_priv->old_baud = p_priv->baud;
2118 msg.setClocking = 0x01;
2119 if (d_details->calculate_baud_rate
2120 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2121 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2122 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2124 p_priv->baud = 9600;
2125 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2126 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2132 /* modes must always be correctly specified */
2133 if (p_priv->baud > 57600)
2135 msg.rxMode = RXMODE_DMA;
2136 msg.txMode = TXMODE_DMA;
2140 msg.rxMode = RXMODE_BYHAND;
2141 msg.txMode = TXMODE_BYHAND;
2144 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2145 switch (p_priv->cflag & CSIZE) {
2147 msg.lcr |= USA_DATABITS_5;
2150 msg.lcr |= USA_DATABITS_6;
2153 msg.lcr |= USA_DATABITS_7;
2156 msg.lcr |= USA_DATABITS_8;
2159 if (p_priv->cflag & PARENB) {
2160 /* note USA_PARITY_NONE == 0 */
2161 msg.lcr |= (p_priv->cflag & PARODD)?
2162 USA_PARITY_ODD: USA_PARITY_EVEN;
2164 if (p_priv->old_cflag != p_priv->cflag) {
2165 p_priv->old_cflag = p_priv->cflag;
2169 if (p_priv->flow_control == flow_cts)
2170 msg.txFlowControl = TXFLOW_CTS;
2171 msg.setTxFlowControl = 0x01;
2172 msg.setRxFlowControl = 0x01;
2174 msg.rxForwardingLength = 16;
2175 msg.rxForwardingTimeout = 16;
2176 msg.txAckSetting = 0;
2181 if (reset_port == 1) {
2182 msg.portEnabled = 1;
2184 msg.txBreak = (p_priv->break_on);
2187 else if (reset_port == 2) {
2188 msg.portEnabled = 0;
2190 /* Sending intermediate configs */
2192 if (port->open_count)
2193 msg.portEnabled = 1;
2194 msg.txBreak = (p_priv->break_on);
2197 /* Do handshaking outputs */
2199 msg.rts = p_priv->rts_state;
2202 msg.dtr = p_priv->dtr_state;
2204 p_priv->resend_cont = 0;
2205 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2207 /* send the data out the device on control endpoint */
2208 this_urb->transfer_buffer_length = sizeof(msg);
2210 this_urb->dev = serial->dev;
2211 if ((err = usb_submit_urb(this_urb)) != 0) {
2212 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2217 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2219 struct usb_serial *serial = port->serial;
2220 struct keyspan_serial_private *s_priv;
2221 const struct keyspan_device_details *d_details;
2223 dbg ("%s", __FUNCTION__);
2225 s_priv = (struct keyspan_serial_private *)(serial->private);
2226 d_details = s_priv->device_details;
2228 switch (d_details->msg_format) {
2230 keyspan_usa26_send_setup(serial, port, reset_port);
2233 keyspan_usa28_send_setup(serial, port, reset_port);
2236 keyspan_usa49_send_setup(serial, port, reset_port);
2239 keyspan_usa90_send_setup(serial, port, reset_port);
2245 /* Gets called by the "real" driver (ie once firmware is loaded
2246 and renumeration has taken place. */
2247 static int keyspan_startup (struct usb_serial *serial)
2250 struct usb_serial_port *port;
2251 struct keyspan_serial_private *s_priv;
2252 struct keyspan_port_private *p_priv;
2253 const struct keyspan_device_details *d_details;
2255 dbg("%s", __FUNCTION__);
2257 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2258 if (d_details->product_id == serial->dev->descriptor.idProduct)
2260 if (d_details == NULL) {
2261 err("%s - unknown product id %x", __FUNCTION__, serial->dev->descriptor.idProduct);
2265 /* Setup private data for serial driver */
2266 serial->private = kmalloc(sizeof(struct keyspan_serial_private),
2268 if (!serial->private) {
2269 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2272 memset(serial->private, 0, sizeof(struct keyspan_serial_private));
2274 s_priv = (struct keyspan_serial_private *)(serial->private);
2275 s_priv->device_details = d_details;
2277 /* Now setup per port private data */
2278 for (i = 0; i < serial->num_ports; i++) {
2279 port = &serial->port[i];
2280 port->private = kmalloc(sizeof(struct keyspan_port_private),
2282 if (!port->private) {
2283 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2286 memset(port->private, 0, sizeof(struct keyspan_port_private));
2287 p_priv = (struct keyspan_port_private *)(port->private);
2288 p_priv->device_details = d_details;
2291 keyspan_setup_urbs(serial);
2293 s_priv->instat_urb->dev = serial->dev;
2294 if ((err = usb_submit_urb(s_priv->instat_urb)) != 0) {
2295 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2301 static void keyspan_shutdown (struct usb_serial *serial)
2304 struct usb_serial_port *port;
2305 struct keyspan_serial_private *s_priv;
2306 struct keyspan_port_private *p_priv;
2308 dbg("%s", __FUNCTION__);
2310 s_priv = (struct keyspan_serial_private *)(serial->private);
2312 /* Stop reading/writing urbs */
2313 stop_urb(s_priv->instat_urb);
2314 stop_urb(s_priv->glocont_urb);
2315 for (i = 0; i < serial->num_ports; ++i) {
2316 port = &serial->port[i];
2317 p_priv = (struct keyspan_port_private *)(port->private);
2318 stop_urb(p_priv->inack_urb);
2319 stop_urb(p_priv->outcont_urb);
2320 for (j = 0; j < 2; j++) {
2321 stop_urb(p_priv->in_urbs[j]);
2322 stop_urb(p_priv->out_urbs[j]);
2327 if (s_priv->instat_urb)
2328 usb_free_urb(s_priv->instat_urb);
2329 if (s_priv->glocont_urb)
2330 usb_free_urb(s_priv->glocont_urb);
2331 for (i = 0; i < serial->num_ports; ++i) {
2332 port = &serial->port[i];
2333 p_priv = (struct keyspan_port_private *)(port->private);
2334 if (p_priv->inack_urb)
2335 usb_free_urb(p_priv->inack_urb);
2336 if (p_priv->outcont_urb)
2337 usb_free_urb(p_priv->outcont_urb);
2338 for (j = 0; j < 2; j++) {
2339 if (p_priv->in_urbs[j])
2340 usb_free_urb(p_priv->in_urbs[j]);
2341 if (p_priv->out_urbs[j])
2342 usb_free_urb(p_priv->out_urbs[j]);
2346 /* dbg("Freeing serial->private."); */
2347 kfree(serial->private);
2349 /* dbg("Freeing port->private."); */
2350 /* Now free per port private data */
2351 for (i = 0; i < serial->num_ports; i++) {
2352 port = &serial->port[i];
2353 kfree(port->private);
2357 MODULE_AUTHOR( DRIVER_AUTHOR );
2358 MODULE_DESCRIPTION( DRIVER_DESC );
2359 MODULE_LICENSE("GPL");
2361 MODULE_PARM(debug, "i");
2362 MODULE_PARM_DESC(debug, "Debug enabled or not");