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/config.h>
99 #include <linux/kernel.h>
100 #include <linux/jiffies.h>
101 #include <linux/errno.h>
102 #include <linux/init.h>
103 #include <linux/slab.h>
104 #include <linux/tty.h>
105 #include <linux/tty_driver.h>
106 #include <linux/tty_flip.h>
107 #include <linux/module.h>
108 #include <linux/spinlock.h>
109 #include <asm/uaccess.h>
111 #ifdef CONFIG_USB_SERIAL_DEBUG
112 static int debug = 1;
119 #include <linux/usb.h>
121 #include "usb-serial.h"
125 * Version Information
127 #define DRIVER_VERSION "v1.1.4"
128 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
129 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
131 #define INSTAT_BUFLEN 32
132 #define GLOCONT_BUFLEN 64
134 /* Per device and per port private data */
135 struct keyspan_serial_private {
136 const struct keyspan_device_details *device_details;
138 struct urb *instat_urb;
139 char instat_buf[INSTAT_BUFLEN];
141 /* XXX this one probably will need a lock */
142 struct urb *glocont_urb;
143 char glocont_buf[GLOCONT_BUFLEN];
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 four message formats which are referred
192 to as USA-26, USA-28 and USA-49, USA-90 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"
199 /* Functions used by new usb-serial code. */
200 static int __init keyspan_init (void)
203 retval = usb_serial_register(&keyspan_pre_device);
205 goto failed_pre_device_register;
206 retval = usb_serial_register(&keyspan_1port_device);
208 goto failed_1port_device_register;
209 retval = usb_serial_register(&keyspan_2port_device);
211 goto failed_2port_device_register;
212 retval = usb_serial_register(&keyspan_4port_device);
214 goto failed_4port_device_register;
215 retval = usb_register(&keyspan_driver);
217 goto failed_usb_register;
219 info(DRIVER_VERSION ":" DRIVER_DESC);
223 usb_serial_deregister(&keyspan_4port_device);
224 failed_4port_device_register:
225 usb_serial_deregister(&keyspan_2port_device);
226 failed_2port_device_register:
227 usb_serial_deregister(&keyspan_1port_device);
228 failed_1port_device_register:
229 usb_serial_deregister(&keyspan_pre_device);
230 failed_pre_device_register:
234 static void __exit keyspan_exit (void)
236 usb_deregister (&keyspan_driver);
237 usb_serial_deregister (&keyspan_pre_device);
238 usb_serial_deregister (&keyspan_1port_device);
239 usb_serial_deregister (&keyspan_2port_device);
240 usb_serial_deregister (&keyspan_4port_device);
243 module_init(keyspan_init);
244 module_exit(keyspan_exit);
246 static void keyspan_rx_throttle (struct usb_serial_port *port)
248 dbg("%s - port %d", __FUNCTION__, port->number);
252 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
254 dbg("%s - port %d", __FUNCTION__, port->number);
258 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
260 struct keyspan_port_private *p_priv;
262 dbg("%s", __FUNCTION__);
264 p_priv = usb_get_serial_port_data(port);
266 if (break_state == -1)
267 p_priv->break_on = 1;
269 p_priv->break_on = 0;
271 keyspan_send_setup(port, 0);
275 static void keyspan_set_termios (struct usb_serial_port *port,
276 struct termios *old_termios)
278 int baud_rate, device_port;
279 struct keyspan_port_private *p_priv;
280 const struct keyspan_device_details *d_details;
283 dbg("%s", __FUNCTION__);
285 p_priv = usb_get_serial_port_data(port);
286 d_details = p_priv->device_details;
287 cflag = port->tty->termios->c_cflag;
288 device_port = port->number - port->serial->minor;
290 /* Baud rate calculation takes baud rate as an integer
291 so other rates can be generated if desired. */
292 baud_rate = tty_get_baud_rate(port->tty);
293 /* If no match or invalid, don't change */
295 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
296 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
297 /* FIXME - more to do here to ensure rate changes cleanly */
298 p_priv->baud = baud_rate;
301 /* set CTS/RTS handshake etc. */
302 p_priv->cflag = cflag;
303 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
305 keyspan_send_setup(port, 0);
308 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
311 struct keyspan_port_private *p_priv;
313 p_priv = usb_get_serial_port_data(port);
315 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
316 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
317 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
318 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
319 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
320 ((p_priv->ri_state) ? TIOCM_RNG : 0);
325 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
326 unsigned int set, unsigned int clear)
328 struct keyspan_port_private *p_priv;
330 p_priv = usb_get_serial_port_data(port);
333 p_priv->rts_state = 1;
335 p_priv->dtr_state = 1;
337 if (clear & TIOCM_RTS)
338 p_priv->rts_state = 0;
339 if (clear & TIOCM_DTR)
340 p_priv->dtr_state = 0;
341 keyspan_send_setup(port, 0);
345 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
346 unsigned int cmd, unsigned long arg)
351 /* Write function is similar for the four protocols used
352 with only a minor change for usa90 (usa19hs) required */
353 static int keyspan_write(struct usb_serial_port *port, int from_user,
354 const unsigned char *buf, int count)
356 struct keyspan_port_private *p_priv;
357 const struct keyspan_device_details *d_details;
360 struct urb *this_urb;
361 int err, maxDataLen, dataOffset;
363 p_priv = usb_get_serial_port_data(port);
364 d_details = p_priv->device_details;
366 if (d_details->msg_format == msg_usa90) {
374 dbg("%s - for port %d (%d chars), flip=%d",
375 __FUNCTION__, port->number, count, p_priv->out_flip);
377 for (left = count; left > 0; left -= todo) {
379 if (todo > maxDataLen)
382 flip = p_priv->out_flip;
384 /* Check we have a valid urb/endpoint before we use it... */
385 if ((this_urb = p_priv->out_urbs[flip]) == 0) {
386 /* no bulk out, so return 0 bytes written */
387 dbg("%s - no output urb :(", __FUNCTION__);
391 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
393 if (this_urb->status == -EINPROGRESS) {
394 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
396 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
398 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
399 usb_unlink_urb(this_urb);
403 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
404 for now so set to zero */
405 ((char *)this_urb->transfer_buffer)[0] = 0;
408 if (copy_from_user(this_urb->transfer_buffer + dataOffset, buf, todo))
411 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
415 /* send the data out the bulk port */
416 this_urb->transfer_buffer_length = todo + dataOffset;
418 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
419 this_urb->dev = port->serial->dev;
420 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
421 dbg("usb_submit_urb(write bulk) failed (%d)", err);
423 p_priv->tx_start_time[flip] = jiffies;
425 /* Flip for next time if usa26 or usa28 interface
426 (not used on usa49) */
427 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
433 static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
437 struct usb_serial_port *port;
438 struct tty_struct *tty;
439 unsigned char *data = urb->transfer_buffer;
441 dbg ("%s", __FUNCTION__);
443 endpoint = usb_pipeendpoint(urb->pipe);
446 dbg("%s - nonzero status: %x on endpoint %d.",
447 __FUNCTION__, urb->status, endpoint);
451 port = (struct usb_serial_port *) urb->context;
453 if (urb->actual_length) {
454 /* 0x80 bit is error flag */
455 if ((data[0] & 0x80) == 0) {
456 /* no errors on individual bytes, only possible overrun err*/
457 if (data[0] & RXERROR_OVERRUN)
460 for (i = 1; i < urb->actual_length ; ++i) {
461 tty_insert_flip_char(tty, data[i], err);
464 /* some bytes had errors, every byte has status */
465 dbg("%s - RX error!!!!", __FUNCTION__);
466 for (i = 0; i + 1 < urb->actual_length; i += 2) {
467 int stat = data[i], flag = 0;
468 if (stat & RXERROR_OVERRUN)
470 if (stat & RXERROR_FRAMING)
472 if (stat & RXERROR_PARITY)
474 /* XXX should handle break (0x10) */
475 tty_insert_flip_char(tty, data[i+1], flag);
478 tty_flip_buffer_push(tty);
481 /* Resubmit urb so we continue receiving */
482 urb->dev = port->serial->dev;
483 if (port->open_count)
484 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
485 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
490 /* Outdat handling is common for all devices */
491 static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
493 struct usb_serial_port *port;
494 struct keyspan_port_private *p_priv;
496 port = (struct usb_serial_port *) urb->context;
497 p_priv = usb_get_serial_port_data(port);
498 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
500 if (port->open_count)
501 schedule_work(&port->work);
504 static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
506 dbg ("%s", __FUNCTION__);
510 static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
512 struct usb_serial_port *port;
513 struct keyspan_port_private *p_priv;
515 port = (struct usb_serial_port *) urb->context;
516 p_priv = usb_get_serial_port_data(port);
518 if (p_priv->resend_cont) {
519 dbg ("%s - sending setup", __FUNCTION__);
520 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
524 static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
526 unsigned char *data = urb->transfer_buffer;
527 struct keyspan_usa26_portStatusMessage *msg;
528 struct usb_serial *serial;
529 struct usb_serial_port *port;
530 struct keyspan_port_private *p_priv;
531 int old_dcd_state, err;
533 serial = (struct usb_serial *) urb->context;
536 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
539 if (urb->actual_length != 9) {
540 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
544 msg = (struct keyspan_usa26_portStatusMessage *)data;
547 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
548 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
549 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
552 /* Now do something useful with the data */
555 /* Check port number from message and retrieve private data */
556 if (msg->port >= serial->num_ports) {
557 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
560 port = serial->port[msg->port];
561 p_priv = usb_get_serial_port_data(port);
563 /* Update handshaking pin state information */
564 old_dcd_state = p_priv->dcd_state;
565 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
566 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
567 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
568 p_priv->ri_state = ((msg->ri) ? 1 : 0);
570 if (port->tty && !C_CLOCAL(port->tty)
571 && old_dcd_state != p_priv->dcd_state) {
573 tty_hangup(port->tty);
575 /* wake_up_interruptible(&p_priv->open_wait); */
578 /* Resubmit urb so we continue receiving */
579 urb->dev = serial->dev;
580 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
581 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
586 static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
588 dbg ("%s", __FUNCTION__);
593 static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
596 struct usb_serial_port *port;
597 struct tty_struct *tty;
599 struct keyspan_port_private *p_priv;
601 dbg ("%s", __FUNCTION__);
603 port = (struct usb_serial_port *) urb->context;
604 p_priv = usb_get_serial_port_data(port);
605 data = urb->transfer_buffer;
607 if (urb != p_priv->in_urbs[p_priv->in_flip])
612 dbg("%s - nonzero status: %x on endpoint %d.",
613 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
617 port = (struct usb_serial_port *) urb->context;
618 p_priv = usb_get_serial_port_data(port);
619 data = urb->transfer_buffer;
622 if (urb->actual_length) {
623 for (i = 0; i < urb->actual_length ; ++i) {
624 tty_insert_flip_char(tty, data[i], 0);
626 tty_flip_buffer_push(tty);
629 /* Resubmit urb so we continue receiving */
630 urb->dev = port->serial->dev;
631 if (port->open_count)
632 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
633 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
635 p_priv->in_flip ^= 1;
637 urb = p_priv->in_urbs[p_priv->in_flip];
638 } while (urb->status != -EINPROGRESS);
641 static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
643 dbg ("%s", __FUNCTION__);
646 static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
648 struct usb_serial_port *port;
649 struct keyspan_port_private *p_priv;
651 port = (struct usb_serial_port *) urb->context;
652 p_priv = usb_get_serial_port_data(port);
654 if (p_priv->resend_cont) {
655 dbg ("%s - sending setup", __FUNCTION__);
656 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
660 static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
663 unsigned char *data = urb->transfer_buffer;
664 struct keyspan_usa28_portStatusMessage *msg;
665 struct usb_serial *serial;
666 struct usb_serial_port *port;
667 struct keyspan_port_private *p_priv;
670 serial = (struct usb_serial *) urb->context;
673 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
677 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
678 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
682 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
683 data[0], data[1], data[2], data[3], data[4], data[5],
684 data[6], data[7], data[8], data[9], data[10], data[11]);*/
686 /* Now do something useful with the data */
687 msg = (struct keyspan_usa28_portStatusMessage *)data;
690 /* Check port number from message and retrieve private data */
691 if (msg->port >= serial->num_ports) {
692 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
695 port = serial->port[msg->port];
696 p_priv = usb_get_serial_port_data(port);
698 /* Update handshaking pin state information */
699 old_dcd_state = p_priv->dcd_state;
700 p_priv->cts_state = ((msg->cts) ? 1 : 0);
701 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
702 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
703 p_priv->ri_state = ((msg->ri) ? 1 : 0);
705 if (port->tty && !C_CLOCAL(port->tty)
706 && old_dcd_state != p_priv->dcd_state) {
708 tty_hangup(port->tty);
710 /* wake_up_interruptible(&p_priv->open_wait); */
713 /* Resubmit urb so we continue receiving */
714 urb->dev = serial->dev;
715 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
716 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
721 static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
723 dbg ("%s", __FUNCTION__);
727 static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
729 struct usb_serial *serial;
730 struct usb_serial_port *port;
731 struct keyspan_port_private *p_priv;
734 dbg ("%s", __FUNCTION__);
736 serial = (struct usb_serial *) urb->context;
737 for (i = 0; i < serial->num_ports; ++i) {
738 port = serial->port[i];
739 p_priv = usb_get_serial_port_data(port);
741 if (p_priv->resend_cont) {
742 dbg ("%s - sending setup", __FUNCTION__);
743 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
749 /* This is actually called glostat in the Keyspan
751 static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
754 unsigned char *data = urb->transfer_buffer;
755 struct keyspan_usa49_portStatusMessage *msg;
756 struct usb_serial *serial;
757 struct usb_serial_port *port;
758 struct keyspan_port_private *p_priv;
761 dbg ("%s", __FUNCTION__);
763 serial = (struct usb_serial *) urb->context;
766 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
770 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
771 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
775 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
776 data[0], data[1], data[2], data[3], data[4], data[5],
777 data[6], data[7], data[8], data[9], data[10]);*/
779 /* Now do something useful with the data */
780 msg = (struct keyspan_usa49_portStatusMessage *)data;
782 /* Check port number from message and retrieve private data */
783 if (msg->portNumber >= serial->num_ports) {
784 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
787 port = serial->port[msg->portNumber];
788 p_priv = usb_get_serial_port_data(port);
790 /* Update handshaking pin state information */
791 old_dcd_state = p_priv->dcd_state;
792 p_priv->cts_state = ((msg->cts) ? 1 : 0);
793 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
794 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
795 p_priv->ri_state = ((msg->ri) ? 1 : 0);
797 if (port->tty && !C_CLOCAL(port->tty)
798 && old_dcd_state != p_priv->dcd_state) {
800 tty_hangup(port->tty);
802 /* wake_up_interruptible(&p_priv->open_wait); */
805 /* Resubmit urb so we continue receiving */
806 urb->dev = serial->dev;
808 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
809 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
814 static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
816 dbg ("%s", __FUNCTION__);
819 static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
823 struct usb_serial_port *port;
824 struct tty_struct *tty;
825 unsigned char *data = urb->transfer_buffer;
827 dbg ("%s", __FUNCTION__);
829 endpoint = usb_pipeendpoint(urb->pipe);
832 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
833 urb->status, endpoint);
837 port = (struct usb_serial_port *) urb->context;
839 if (urb->actual_length) {
840 /* 0x80 bit is error flag */
841 if ((data[0] & 0x80) == 0) {
842 /* no error on any byte */
843 for (i = 1; i < urb->actual_length ; ++i) {
844 tty_insert_flip_char(tty, data[i], 0);
847 /* some bytes had errors, every byte has status */
848 for (i = 0; i + 1 < urb->actual_length; i += 2) {
849 int stat = data[i], flag = 0;
850 if (stat & RXERROR_OVERRUN)
852 if (stat & RXERROR_FRAMING)
854 if (stat & RXERROR_PARITY)
856 /* XXX should handle break (0x10) */
857 tty_insert_flip_char(tty, data[i+1], flag);
860 tty_flip_buffer_push(tty);
863 /* Resubmit urb so we continue receiving */
864 urb->dev = port->serial->dev;
865 if (port->open_count)
866 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
867 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
871 /* not used, usa-49 doesn't have per-port control endpoints */
872 static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
874 dbg ("%s", __FUNCTION__);
877 static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
881 struct usb_serial_port *port;
882 struct keyspan_port_private *p_priv;
883 struct tty_struct *tty;
884 unsigned char *data = urb->transfer_buffer;
886 dbg ("%s", __FUNCTION__);
888 endpoint = usb_pipeendpoint(urb->pipe);
892 dbg("%s - nonzero status: %x on endpoint %d.",
893 __FUNCTION__, urb->status, endpoint);
897 port = (struct usb_serial_port *) urb->context;
898 p_priv = usb_get_serial_port_data(port);
901 if (urb->actual_length) {
903 /* if current mode is DMA, looks like usa28 format
904 otherwise looks like usa26 data format */
906 if (p_priv->baud > 57600) {
907 for (i = 0; i < urb->actual_length ; ++i)
908 tty_insert_flip_char(tty, data[i], 0);
912 /* 0x80 bit is error flag */
913 if ((data[0] & 0x80) == 0) {
914 /* no errors on individual bytes, only possible overrun err*/
915 if (data[0] & RXERROR_OVERRUN)
918 for (i = 1; i < urb->actual_length ; ++i)
919 tty_insert_flip_char(tty, data[i], err);
923 /* some bytes had errors, every byte has status */
924 dbg("%s - RX error!!!!", __FUNCTION__);
925 for (i = 0; i + 1 < urb->actual_length; i += 2) {
926 int stat = data[i], flag = 0;
927 if (stat & RXERROR_OVERRUN)
929 if (stat & RXERROR_FRAMING)
931 if (stat & RXERROR_PARITY)
933 /* XXX should handle break (0x10) */
934 tty_insert_flip_char(tty, data[i+1], flag);
938 tty_flip_buffer_push(tty);
941 /* Resubmit urb so we continue receiving */
942 urb->dev = port->serial->dev;
943 if (port->open_count)
944 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
945 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
951 static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
953 unsigned char *data = urb->transfer_buffer;
954 struct keyspan_usa90_portStatusMessage *msg;
955 struct usb_serial *serial;
956 struct usb_serial_port *port;
957 struct keyspan_port_private *p_priv;
958 int old_dcd_state, err;
960 serial = (struct usb_serial *) urb->context;
963 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
966 if (urb->actual_length < 14) {
967 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
971 msg = (struct keyspan_usa90_portStatusMessage *)data;
973 /* Now do something useful with the data */
975 port = serial->port[0];
976 p_priv = usb_get_serial_port_data(port);
978 /* Update handshaking pin state information */
979 old_dcd_state = p_priv->dcd_state;
980 p_priv->cts_state = ((msg->cts) ? 1 : 0);
981 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
982 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
983 p_priv->ri_state = ((msg->ri) ? 1 : 0);
985 if (port->tty && !C_CLOCAL(port->tty)
986 && old_dcd_state != p_priv->dcd_state) {
988 tty_hangup(port->tty);
990 /* wake_up_interruptible(&p_priv->open_wait); */
993 /* Resubmit urb so we continue receiving */
994 urb->dev = serial->dev;
995 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
996 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1002 static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
1004 struct usb_serial_port *port;
1005 struct keyspan_port_private *p_priv;
1007 port = (struct usb_serial_port *) urb->context;
1008 p_priv = usb_get_serial_port_data(port);
1010 if (p_priv->resend_cont) {
1011 dbg ("%s - sending setup", __FUNCTION__);
1012 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1016 static int keyspan_write_room (struct usb_serial_port *port)
1018 struct keyspan_port_private *p_priv;
1019 const struct keyspan_device_details *d_details;
1022 struct urb *this_urb;
1024 dbg("%s", __FUNCTION__);
1025 p_priv = usb_get_serial_port_data(port);
1026 d_details = p_priv->device_details;
1028 if (d_details->msg_format == msg_usa90)
1033 flip = p_priv->out_flip;
1035 /* Check both endpoints to see if any are available. */
1036 if ((this_urb = p_priv->out_urbs[flip]) != 0) {
1037 if (this_urb->status != -EINPROGRESS)
1039 flip = (flip + 1) & d_details->outdat_endp_flip;
1040 if ((this_urb = p_priv->out_urbs[flip]) != 0)
1041 if (this_urb->status != -EINPROGRESS)
1048 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1054 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1056 struct keyspan_port_private *p_priv;
1057 struct keyspan_serial_private *s_priv;
1058 struct usb_serial *serial = port->serial;
1059 const struct keyspan_device_details *d_details;
1061 int baud_rate, device_port;
1065 s_priv = usb_get_serial_data(serial);
1066 p_priv = usb_get_serial_port_data(port);
1067 d_details = p_priv->device_details;
1069 dbg("%s - port%d.", __FUNCTION__, port->number);
1071 /* Set some sane defaults */
1072 p_priv->rts_state = 1;
1073 p_priv->dtr_state = 1;
1074 p_priv->baud = 9600;
1076 /* force baud and lcr to be set on open */
1077 p_priv->old_baud = 0;
1078 p_priv->old_cflag = 0;
1080 p_priv->out_flip = 0;
1081 p_priv->in_flip = 0;
1083 /* Reset low level data toggle and start reading from endpoints */
1084 for (i = 0; i < 2; i++) {
1085 if ((urb = p_priv->in_urbs[i]) == NULL)
1087 urb->dev = serial->dev;
1089 /* make sure endpoint data toggle is synchronized with the device */
1091 usb_clear_halt(urb->dev, urb->pipe);
1093 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1094 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1098 /* Reset low level data toggle on out endpoints */
1099 for (i = 0; i < 2; i++) {
1100 if ((urb = p_priv->out_urbs[i]) == NULL)
1102 urb->dev = serial->dev;
1103 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1106 /* get the terminal config for the setup message now so we don't
1107 * need to send 2 of them */
1109 cflag = port->tty->termios->c_cflag;
1110 device_port = port->number - port->serial->minor;
1112 /* Baud rate calculation takes baud rate as an integer
1113 so other rates can be generated if desired. */
1114 baud_rate = tty_get_baud_rate(port->tty);
1115 /* If no match or invalid, leave as default */
1117 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1118 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1119 p_priv->baud = baud_rate;
1122 /* set CTS/RTS handshake etc. */
1123 p_priv->cflag = cflag;
1124 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1126 keyspan_send_setup(port, 1);
1128 //keyspan_set_termios(port, NULL);
1133 static inline void stop_urb(struct urb *urb)
1135 if (urb && urb->status == -EINPROGRESS) {
1136 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1137 usb_unlink_urb(urb);
1141 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1144 struct usb_serial *serial = port->serial;
1145 struct keyspan_serial_private *s_priv;
1146 struct keyspan_port_private *p_priv;
1148 dbg("%s", __FUNCTION__);
1149 s_priv = usb_get_serial_data(serial);
1150 p_priv = usb_get_serial_port_data(port);
1152 p_priv->rts_state = 0;
1153 p_priv->dtr_state = 0;
1156 keyspan_send_setup(port, 2);
1157 /* pilot-xfer seems to work best with this delay */
1159 // keyspan_set_termios(port, NULL);
1162 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1163 dbg("%s - urb in progress", __FUNCTION__);
1166 p_priv->out_flip = 0;
1167 p_priv->in_flip = 0;
1170 /* Stop reading/writing urbs */
1171 stop_urb(p_priv->inack_urb);
1172 /* stop_urb(p_priv->outcont_urb); */
1173 for (i = 0; i < 2; i++) {
1174 stop_urb(p_priv->in_urbs[i]);
1175 stop_urb(p_priv->out_urbs[i]);
1182 /* download the firmware to a pre-renumeration device */
1183 static int keyspan_fake_startup (struct usb_serial *serial)
1186 const struct ezusb_hex_record *record;
1189 dbg("Keyspan startup version %04x product %04x",
1190 serial->dev->descriptor.bcdDevice,
1191 serial->dev->descriptor.idProduct);
1193 if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
1194 dbg("Firmware already loaded. Quitting.");
1198 /* Select firmware image on the basis of idProduct */
1199 switch (serial->dev->descriptor.idProduct) {
1200 case keyspan_usa28_pre_product_id:
1201 record = &keyspan_usa28_firmware[0];
1205 case keyspan_usa28x_pre_product_id:
1206 record = &keyspan_usa28x_firmware[0];
1210 case keyspan_usa28xa_pre_product_id:
1211 record = &keyspan_usa28xa_firmware[0];
1212 fw_name = "USA28XA";
1215 case keyspan_usa28xb_pre_product_id:
1216 record = &keyspan_usa28xb_firmware[0];
1217 fw_name = "USA28XB";
1220 case keyspan_usa19_pre_product_id:
1221 record = &keyspan_usa19_firmware[0];
1225 case keyspan_usa19qi_pre_product_id:
1226 record = &keyspan_usa19qi_firmware[0];
1227 fw_name = "USA19QI";
1230 case keyspan_mpr_pre_product_id:
1231 record = &keyspan_mpr_firmware[0];
1235 case keyspan_usa19qw_pre_product_id:
1236 record = &keyspan_usa19qw_firmware[0];
1237 fw_name = "USA19QI";
1240 case keyspan_usa18x_pre_product_id:
1241 record = &keyspan_usa18x_firmware[0];
1245 case keyspan_usa19w_pre_product_id:
1246 record = &keyspan_usa19w_firmware[0];
1250 case keyspan_usa49w_pre_product_id:
1251 record = &keyspan_usa49w_firmware[0];
1255 case keyspan_usa49wlc_pre_product_id:
1256 record = &keyspan_usa49wlc_firmware[0];
1257 fw_name = "USA49WLC";
1262 fw_name = "Unknown";
1266 if (record == NULL) {
1267 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1271 dbg("Uploading Keyspan %s firmware.", fw_name);
1273 /* download the firmware image */
1274 response = ezusb_set_reset(serial, 1);
1276 while(record->address != 0xffff) {
1277 response = ezusb_writememory(serial, record->address,
1278 (unsigned char *)record->data,
1279 record->data_size, 0xa0);
1281 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1282 "firmware (%d %04X %p %d)\n",
1284 record->address, record->data, record->data_size);
1289 /* bring device out of reset. Renumeration will occur in a
1290 moment and the new device will bind to the real driver */
1291 response = ezusb_set_reset(serial, 0);
1293 /* we don't want this device to have a driver assigned to it. */
1297 /* Helper functions used by keyspan_setup_urbs */
1298 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1299 int dir, void *ctx, char *buf, int len,
1300 void (*callback)(struct urb *, struct pt_regs *regs))
1305 return NULL; /* endpoint not needed */
1307 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1308 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1310 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1314 /* Fill URB using supplied data. */
1315 usb_fill_bulk_urb(urb, serial->dev,
1316 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1317 buf, len, callback, ctx);
1322 static struct callbacks {
1323 void (*instat_callback)(struct urb *, struct pt_regs *regs);
1324 void (*glocont_callback)(struct urb *, struct pt_regs *regs);
1325 void (*indat_callback)(struct urb *, struct pt_regs *regs);
1326 void (*outdat_callback)(struct urb *, struct pt_regs *regs);
1327 void (*inack_callback)(struct urb *, struct pt_regs *regs);
1328 void (*outcont_callback)(struct urb *, struct pt_regs *regs);
1329 } keyspan_callbacks[] = {
1331 /* msg_usa26 callbacks */
1332 .instat_callback = usa26_instat_callback,
1333 .glocont_callback = usa26_glocont_callback,
1334 .indat_callback = usa26_indat_callback,
1335 .outdat_callback = usa2x_outdat_callback,
1336 .inack_callback = usa26_inack_callback,
1337 .outcont_callback = usa26_outcont_callback,
1339 /* msg_usa28 callbacks */
1340 .instat_callback = usa28_instat_callback,
1341 .glocont_callback = usa28_glocont_callback,
1342 .indat_callback = usa28_indat_callback,
1343 .outdat_callback = usa2x_outdat_callback,
1344 .inack_callback = usa28_inack_callback,
1345 .outcont_callback = usa28_outcont_callback,
1347 /* msg_usa49 callbacks */
1348 .instat_callback = usa49_instat_callback,
1349 .glocont_callback = usa49_glocont_callback,
1350 .indat_callback = usa49_indat_callback,
1351 .outdat_callback = usa2x_outdat_callback,
1352 .inack_callback = usa49_inack_callback,
1353 .outcont_callback = usa49_outcont_callback,
1355 /* msg_usa90 callbacks */
1356 .instat_callback = usa90_instat_callback,
1357 .glocont_callback = usa28_glocont_callback,
1358 .indat_callback = usa90_indat_callback,
1359 .outdat_callback = usa2x_outdat_callback,
1360 .inack_callback = usa28_inack_callback,
1361 .outcont_callback = usa90_outcont_callback,
1365 /* Generic setup urbs function that uses
1366 data in device_details */
1367 static void keyspan_setup_urbs(struct usb_serial *serial)
1370 struct keyspan_serial_private *s_priv;
1371 const struct keyspan_device_details *d_details;
1372 struct usb_serial_port *port;
1373 struct keyspan_port_private *p_priv;
1374 struct callbacks *cback;
1377 dbg ("%s", __FUNCTION__);
1379 s_priv = usb_get_serial_data(serial);
1380 d_details = s_priv->device_details;
1382 /* Setup values for the various callback routines */
1383 cback = &keyspan_callbacks[d_details->msg_format];
1385 /* Allocate and set up urbs for each one that is in use,
1386 starting with instat endpoints */
1387 s_priv->instat_urb = keyspan_setup_urb
1388 (serial, d_details->instat_endpoint, USB_DIR_IN,
1389 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1390 cback->instat_callback);
1392 s_priv->glocont_urb = keyspan_setup_urb
1393 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1394 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1395 cback->glocont_callback);
1397 /* Setup endpoints for each port specific thing */
1398 for (i = 0; i < d_details->num_ports; i ++) {
1399 port = serial->port[i];
1400 p_priv = usb_get_serial_port_data(port);
1402 /* Do indat endpoints first, once for each flip */
1403 endp = d_details->indat_endpoints[i];
1404 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1405 p_priv->in_urbs[j] = keyspan_setup_urb
1406 (serial, endp, USB_DIR_IN, port,
1407 p_priv->in_buffer[j], 64,
1408 cback->indat_callback);
1411 p_priv->in_urbs[j] = NULL;
1413 /* outdat endpoints also have flip */
1414 endp = d_details->outdat_endpoints[i];
1415 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1416 p_priv->out_urbs[j] = keyspan_setup_urb
1417 (serial, endp, USB_DIR_OUT, port,
1418 p_priv->out_buffer[j], 64,
1419 cback->outdat_callback);
1422 p_priv->out_urbs[j] = NULL;
1424 /* inack endpoint */
1425 p_priv->inack_urb = keyspan_setup_urb
1426 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1427 port, p_priv->inack_buffer, 1, cback->inack_callback);
1429 /* outcont endpoint */
1430 p_priv->outcont_urb = keyspan_setup_urb
1431 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1432 port, p_priv->outcont_buffer, 64,
1433 cback->outcont_callback);
1438 /* usa19 function doesn't require prescaler */
1439 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1440 u8 *rate_low, u8 *prescaler, int portnum)
1442 u32 b16, /* baud rate times 16 (actual rate used internally) */
1444 cnt; /* inverse of divisor (programmed into 8051) */
1446 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1448 /* prevent divide by zero... */
1449 if( (b16 = (baud_rate * 16L)) == 0) {
1450 return (KEYSPAN_INVALID_BAUD_RATE);
1453 /* Any "standard" rate over 57k6 is marginal on the USA-19
1454 as we run out of divisor resolution. */
1455 if (baud_rate > 57600) {
1456 return (KEYSPAN_INVALID_BAUD_RATE);
1459 /* calculate the divisor and the counter (its inverse) */
1460 if( (div = (baudclk / b16)) == 0) {
1461 return (KEYSPAN_INVALID_BAUD_RATE);
1468 return (KEYSPAN_INVALID_BAUD_RATE);
1471 /* return the counter values if non-null */
1473 *rate_low = (u8) (cnt & 0xff);
1476 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1478 if (rate_low && rate_hi) {
1479 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1482 return (KEYSPAN_BAUD_RATE_OK);
1485 /* usa19hs function doesn't require prescaler */
1486 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1487 u8 *rate_low, u8 *prescaler, int portnum)
1489 u32 b16, /* baud rate times 16 (actual rate used internally) */
1492 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1494 /* prevent divide by zero... */
1495 if( (b16 = (baud_rate * 16L)) == 0)
1496 return (KEYSPAN_INVALID_BAUD_RATE);
1500 /* calculate the divisor */
1501 if( (div = (baudclk / b16)) == 0)
1502 return (KEYSPAN_INVALID_BAUD_RATE);
1505 return (KEYSPAN_INVALID_BAUD_RATE);
1507 /* return the counter values if non-null */
1509 *rate_low = (u8) (div & 0xff);
1512 *rate_hi = (u8) ((div >> 8) & 0xff);
1514 if (rate_low && rate_hi)
1515 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1517 return (KEYSPAN_BAUD_RATE_OK);
1520 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1521 u8 *rate_low, u8 *prescaler, int portnum)
1523 u32 b16, /* baud rate times 16 (actual rate used internally) */
1524 clk, /* clock with 13/8 prescaler */
1525 div, /* divisor using 13/8 prescaler */
1526 res, /* resulting baud rate using 13/8 prescaler */
1527 diff, /* error using 13/8 prescaler */
1532 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1534 /* prevent divide by zero */
1535 if( (b16 = baud_rate * 16L) == 0) {
1536 return (KEYSPAN_INVALID_BAUD_RATE);
1539 /* Calculate prescaler by trying them all and looking
1542 /* start with largest possible difference */
1543 smallest_diff = 0xffffffff;
1545 /* 0 is an invalid prescaler, used as a flag */
1548 for(i = 8; i <= 0xff; ++i) {
1549 clk = (baudclk * 8) / (u32) i;
1551 if( (div = clk / b16) == 0) {
1556 diff= (res > b16) ? (res-b16) : (b16-res);
1558 if(diff < smallest_diff) {
1560 smallest_diff = diff;
1564 if(best_prescaler == 0) {
1565 return (KEYSPAN_INVALID_BAUD_RATE);
1568 clk = (baudclk * 8) / (u32) best_prescaler;
1571 /* return the divisor and prescaler if non-null */
1573 *rate_low = (u8) (div & 0xff);
1576 *rate_hi = (u8) ((div >> 8) & 0xff);
1579 *prescaler = best_prescaler;
1580 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1582 return (KEYSPAN_BAUD_RATE_OK);
1585 /* USA-28 supports different maximum baud rates on each port */
1586 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1587 u8 *rate_low, u8 *prescaler, int portnum)
1589 u32 b16, /* baud rate times 16 (actual rate used internally) */
1591 cnt; /* inverse of divisor (programmed into 8051) */
1593 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1595 /* prevent divide by zero */
1596 if ((b16 = baud_rate * 16L) == 0)
1597 return (KEYSPAN_INVALID_BAUD_RATE);
1599 /* calculate the divisor and the counter (its inverse) */
1600 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1601 return (KEYSPAN_INVALID_BAUD_RATE);
1607 /* check for out of range, based on portnum,
1608 and return result */
1611 return (KEYSPAN_INVALID_BAUD_RATE);
1616 return (KEYSPAN_INVALID_BAUD_RATE);
1620 return (KEYSPAN_INVALID_BAUD_RATE);
1624 /* return the counter values if not NULL
1625 (port 1 will ignore retHi) */
1627 *rate_low = (u8) (cnt & 0xff);
1630 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1632 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1633 return (KEYSPAN_BAUD_RATE_OK);
1636 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1637 struct usb_serial_port *port,
1640 struct keyspan_usa26_portControlMessage msg;
1641 struct keyspan_serial_private *s_priv;
1642 struct keyspan_port_private *p_priv;
1643 const struct keyspan_device_details *d_details;
1645 struct urb *this_urb;
1646 int device_port, err;
1648 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1650 s_priv = usb_get_serial_data(serial);
1651 p_priv = usb_get_serial_port_data(port);
1652 d_details = s_priv->device_details;
1653 device_port = port->number - port->serial->minor;
1655 outcont_urb = d_details->outcont_endpoints[port->number];
1656 this_urb = p_priv->outcont_urb;
1658 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1660 /* Make sure we have an urb then send the message */
1661 if (this_urb == NULL) {
1662 dbg("%s - oops no urb.", __FUNCTION__);
1666 /* Save reset port val for resend.
1667 Don't overwrite resend for close condition. */
1668 if (p_priv->resend_cont != 3)
1669 p_priv->resend_cont = reset_port + 1;
1670 if (this_urb->status == -EINPROGRESS) {
1671 /* dbg ("%s - already writing", __FUNCTION__); */
1676 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1678 /* Only set baud rate if it's changed */
1679 if (p_priv->old_baud != p_priv->baud) {
1680 p_priv->old_baud = p_priv->baud;
1681 msg.setClocking = 0xff;
1682 if (d_details->calculate_baud_rate
1683 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1684 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1685 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1688 msg.baudHi = 125; /* Values for 9600 baud */
1691 msg.setPrescaler = 0xff;
1694 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1695 switch (p_priv->cflag & CSIZE) {
1697 msg.lcr |= USA_DATABITS_5;
1700 msg.lcr |= USA_DATABITS_6;
1703 msg.lcr |= USA_DATABITS_7;
1706 msg.lcr |= USA_DATABITS_8;
1709 if (p_priv->cflag & PARENB) {
1710 /* note USA_PARITY_NONE == 0 */
1711 msg.lcr |= (p_priv->cflag & PARODD)?
1712 USA_PARITY_ODD: USA_PARITY_EVEN;
1716 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1717 msg.xonFlowControl = 0;
1718 msg.setFlowControl = 0xff;
1719 msg.forwardingLength = 16;
1724 if (reset_port == 1) {
1733 msg.returnStatus = 0;
1734 msg.resetDataToggle = 0xff;
1738 else if (reset_port == 2) {
1747 msg.returnStatus = 0;
1748 msg.resetDataToggle = 0;
1751 /* Sending intermediate configs */
1753 msg._txOn = (! p_priv->break_on);
1756 msg.txBreak = (p_priv->break_on);
1761 msg.returnStatus = 0;
1762 msg.resetDataToggle = 0x0;
1765 /* Do handshaking outputs */
1766 msg.setTxTriState_setRts = 0xff;
1767 msg.txTriState_rts = p_priv->rts_state;
1769 msg.setHskoa_setDtr = 0xff;
1770 msg.hskoa_dtr = p_priv->dtr_state;
1772 p_priv->resend_cont = 0;
1773 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1775 /* send the data out the device on control endpoint */
1776 this_urb->transfer_buffer_length = sizeof(msg);
1778 this_urb->dev = serial->dev;
1779 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1780 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1784 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1785 outcont_urb, this_urb->transfer_buffer_length,
1786 usb_pipeendpoint(this_urb->pipe));
1793 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1794 struct usb_serial_port *port,
1797 struct keyspan_usa28_portControlMessage msg;
1798 struct keyspan_serial_private *s_priv;
1799 struct keyspan_port_private *p_priv;
1800 const struct keyspan_device_details *d_details;
1801 struct urb *this_urb;
1802 int device_port, err;
1804 dbg ("%s", __FUNCTION__);
1806 s_priv = usb_get_serial_data(serial);
1807 p_priv = usb_get_serial_port_data(port);
1808 d_details = s_priv->device_details;
1809 device_port = port->number - port->serial->minor;
1811 /* only do something if we have a bulk out endpoint */
1812 if ((this_urb = p_priv->outcont_urb) == NULL) {
1813 dbg("%s - oops no urb.", __FUNCTION__);
1817 /* Save reset port val for resend.
1818 Don't overwrite resend for close condition. */
1819 if (p_priv->resend_cont != 3)
1820 p_priv->resend_cont = reset_port + 1;
1821 if (this_urb->status == -EINPROGRESS) {
1822 dbg ("%s already writing", __FUNCTION__);
1827 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1829 msg.setBaudRate = 1;
1830 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1831 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1832 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1834 msg.baudHi = 0xb2; /* Values for 9600 baud */
1837 /* If parity is enabled, we must calculate it ourselves. */
1838 msg.parity = 0; /* XXX for now */
1840 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1841 msg.xonFlowControl = 0;
1843 /* Do handshaking outputs, DTR is inverted relative to RTS */
1844 msg.rts = p_priv->rts_state;
1845 msg.dtr = p_priv->dtr_state;
1847 msg.forwardingLength = 16;
1849 msg.breakThreshold = 45;
1853 /*msg.returnStatus = 1;
1854 msg.resetDataToggle = 0xff;*/
1856 if (reset_port == 1) {
1860 msg.txForceXoff = 0;
1866 msg.returnStatus = 0;
1867 msg.resetDataToggle = 0xff;
1870 else if (reset_port == 2) {
1874 msg.txForceXoff = 0;
1880 msg.returnStatus = 0;
1881 msg.resetDataToggle = 0;
1883 /* Sending intermediate configs */
1885 msg._txOn = (! p_priv->break_on);
1888 msg.txForceXoff = 0;
1889 msg.txBreak = (p_priv->break_on);
1894 msg.returnStatus = 0;
1895 msg.resetDataToggle = 0x0;
1898 p_priv->resend_cont = 0;
1899 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1901 /* send the data out the device on control endpoint */
1902 this_urb->transfer_buffer_length = sizeof(msg);
1904 this_urb->dev = serial->dev;
1905 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1906 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1910 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1911 this_urb->transfer_buffer_length);
1918 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1919 struct usb_serial_port *port,
1922 struct keyspan_usa49_portControlMessage msg;
1923 struct keyspan_serial_private *s_priv;
1924 struct keyspan_port_private *p_priv;
1925 const struct keyspan_device_details *d_details;
1927 struct urb *this_urb;
1928 int err, device_port;
1930 dbg ("%s", __FUNCTION__);
1932 s_priv = usb_get_serial_data(serial);
1933 p_priv = usb_get_serial_port_data(port);
1934 d_details = s_priv->device_details;
1936 glocont_urb = d_details->glocont_endpoint;
1937 this_urb = s_priv->glocont_urb;
1939 /* Work out which port within the device is being setup */
1940 device_port = port->number - port->serial->minor;
1942 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1944 /* Make sure we have an urb then send the message */
1945 if (this_urb == NULL) {
1946 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1950 /* Save reset port val for resend.
1951 Don't overwrite resend for close condition. */
1952 if (p_priv->resend_cont != 3)
1953 p_priv->resend_cont = reset_port + 1;
1954 if (this_urb->status == -EINPROGRESS) {
1955 /* dbg ("%s - already writing", __FUNCTION__); */
1960 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1962 /*msg.portNumber = port->number;*/
1963 msg.portNumber = device_port;
1965 /* Only set baud rate if it's changed */
1966 if (p_priv->old_baud != p_priv->baud) {
1967 p_priv->old_baud = p_priv->baud;
1968 msg.setClocking = 0xff;
1969 if (d_details->calculate_baud_rate
1970 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1971 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1972 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1975 msg.baudHi = 125; /* Values for 9600 baud */
1978 //msg.setPrescaler = 0xff;
1981 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1982 switch (p_priv->cflag & CSIZE) {
1984 msg.lcr |= USA_DATABITS_5;
1987 msg.lcr |= USA_DATABITS_6;
1990 msg.lcr |= USA_DATABITS_7;
1993 msg.lcr |= USA_DATABITS_8;
1996 if (p_priv->cflag & PARENB) {
1997 /* note USA_PARITY_NONE == 0 */
1998 msg.lcr |= (p_priv->cflag & PARODD)?
1999 USA_PARITY_ODD: USA_PARITY_EVEN;
2003 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2004 msg.xonFlowControl = 0;
2005 msg.setFlowControl = 0xff;
2007 msg.forwardingLength = 16;
2012 if (reset_port == 1) {
2021 msg.returnStatus = 0;
2022 msg.resetDataToggle = 0xff;
2024 msg.disablePort = 0;
2027 else if (reset_port == 2) {
2036 msg.returnStatus = 0;
2037 msg.resetDataToggle = 0;
2039 msg.disablePort = 1;
2041 /* Sending intermediate configs */
2043 msg._txOn = (! p_priv->break_on);
2046 msg.txBreak = (p_priv->break_on);
2051 msg.returnStatus = 0;
2052 msg.resetDataToggle = 0x0;
2054 msg.disablePort = 0;
2057 /* Do handshaking outputs */
2059 msg.rts = p_priv->rts_state;
2062 msg.dtr = p_priv->dtr_state;
2064 p_priv->resend_cont = 0;
2065 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2067 /* send the data out the device on control endpoint */
2068 this_urb->transfer_buffer_length = sizeof(msg);
2070 this_urb->dev = serial->dev;
2071 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2072 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2076 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2077 outcont_urb, this_urb->transfer_buffer_length,
2078 usb_pipeendpoint(this_urb->pipe));
2085 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2086 struct usb_serial_port *port,
2089 struct keyspan_usa90_portControlMessage msg;
2090 struct keyspan_serial_private *s_priv;
2091 struct keyspan_port_private *p_priv;
2092 const struct keyspan_device_details *d_details;
2093 struct urb *this_urb;
2097 dbg ("%s", __FUNCTION__);
2099 s_priv = usb_get_serial_data(serial);
2100 p_priv = usb_get_serial_port_data(port);
2101 d_details = s_priv->device_details;
2103 /* only do something if we have a bulk out endpoint */
2104 if ((this_urb = p_priv->outcont_urb) == NULL) {
2105 dbg("%s - oops no urb.", __FUNCTION__);
2109 /* Save reset port val for resend.
2110 Don't overwrite resend for open/close condition. */
2111 if ((reset_port + 1) > p_priv->resend_cont)
2112 p_priv->resend_cont = reset_port + 1;
2113 if (this_urb->status == -EINPROGRESS) {
2114 dbg ("%s already writing", __FUNCTION__);
2119 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2121 /* Only set baud rate if it's changed */
2122 if (p_priv->old_baud != p_priv->baud) {
2123 p_priv->old_baud = p_priv->baud;
2124 msg.setClocking = 0x01;
2125 if (d_details->calculate_baud_rate
2126 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2127 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2128 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2130 p_priv->baud = 9600;
2131 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2132 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2138 /* modes must always be correctly specified */
2139 if (p_priv->baud > 57600)
2141 msg.rxMode = RXMODE_DMA;
2142 msg.txMode = TXMODE_DMA;
2146 msg.rxMode = RXMODE_BYHAND;
2147 msg.txMode = TXMODE_BYHAND;
2150 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2151 switch (p_priv->cflag & CSIZE) {
2153 msg.lcr |= USA_DATABITS_5;
2156 msg.lcr |= USA_DATABITS_6;
2159 msg.lcr |= USA_DATABITS_7;
2162 msg.lcr |= USA_DATABITS_8;
2165 if (p_priv->cflag & PARENB) {
2166 /* note USA_PARITY_NONE == 0 */
2167 msg.lcr |= (p_priv->cflag & PARODD)?
2168 USA_PARITY_ODD: USA_PARITY_EVEN;
2170 if (p_priv->old_cflag != p_priv->cflag) {
2171 p_priv->old_cflag = p_priv->cflag;
2175 if (p_priv->flow_control == flow_cts)
2176 msg.txFlowControl = TXFLOW_CTS;
2177 msg.setTxFlowControl = 0x01;
2178 msg.setRxFlowControl = 0x01;
2180 msg.rxForwardingLength = 16;
2181 msg.rxForwardingTimeout = 16;
2182 msg.txAckSetting = 0;
2187 if (reset_port == 1) {
2188 msg.portEnabled = 1;
2190 msg.txBreak = (p_priv->break_on);
2193 else if (reset_port == 2) {
2194 msg.portEnabled = 0;
2196 /* Sending intermediate configs */
2198 if (port->open_count)
2199 msg.portEnabled = 1;
2200 msg.txBreak = (p_priv->break_on);
2203 /* Do handshaking outputs */
2205 msg.rts = p_priv->rts_state;
2208 msg.dtr = p_priv->dtr_state;
2210 p_priv->resend_cont = 0;
2211 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2213 /* send the data out the device on control endpoint */
2214 this_urb->transfer_buffer_length = sizeof(msg);
2216 this_urb->dev = serial->dev;
2217 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2218 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2223 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2225 struct usb_serial *serial = port->serial;
2226 struct keyspan_serial_private *s_priv;
2227 const struct keyspan_device_details *d_details;
2229 dbg ("%s", __FUNCTION__);
2231 s_priv = usb_get_serial_data(serial);
2232 d_details = s_priv->device_details;
2234 switch (d_details->msg_format) {
2236 keyspan_usa26_send_setup(serial, port, reset_port);
2239 keyspan_usa28_send_setup(serial, port, reset_port);
2242 keyspan_usa49_send_setup(serial, port, reset_port);
2245 keyspan_usa90_send_setup(serial, port, reset_port);
2251 /* Gets called by the "real" driver (ie once firmware is loaded
2252 and renumeration has taken place. */
2253 static int keyspan_startup (struct usb_serial *serial)
2256 struct usb_serial_port *port;
2257 struct keyspan_serial_private *s_priv;
2258 struct keyspan_port_private *p_priv;
2259 const struct keyspan_device_details *d_details;
2261 dbg("%s", __FUNCTION__);
2263 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2264 if (d_details->product_id == serial->dev->descriptor.idProduct)
2266 if (d_details == NULL) {
2267 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, serial->dev->descriptor.idProduct);
2271 /* Setup private data for serial driver */
2272 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2274 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2277 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2279 s_priv->device_details = d_details;
2280 usb_set_serial_data(serial, s_priv);
2282 /* Now setup per port private data */
2283 for (i = 0; i < serial->num_ports; i++) {
2284 port = serial->port[i];
2285 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2287 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2290 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2291 p_priv->device_details = d_details;
2292 usb_set_serial_port_data(port, p_priv);
2295 keyspan_setup_urbs(serial);
2297 s_priv->instat_urb->dev = serial->dev;
2298 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2299 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2305 static void keyspan_shutdown (struct usb_serial *serial)
2308 struct usb_serial_port *port;
2309 struct keyspan_serial_private *s_priv;
2310 struct keyspan_port_private *p_priv;
2312 dbg("%s", __FUNCTION__);
2314 s_priv = usb_get_serial_data(serial);
2316 /* Stop reading/writing urbs */
2317 stop_urb(s_priv->instat_urb);
2318 stop_urb(s_priv->glocont_urb);
2319 for (i = 0; i < serial->num_ports; ++i) {
2320 port = serial->port[i];
2321 p_priv = usb_get_serial_port_data(port);
2322 stop_urb(p_priv->inack_urb);
2323 stop_urb(p_priv->outcont_urb);
2324 for (j = 0; j < 2; j++) {
2325 stop_urb(p_priv->in_urbs[j]);
2326 stop_urb(p_priv->out_urbs[j]);
2331 if (s_priv->instat_urb)
2332 usb_free_urb(s_priv->instat_urb);
2333 if (s_priv->glocont_urb)
2334 usb_free_urb(s_priv->glocont_urb);
2335 for (i = 0; i < serial->num_ports; ++i) {
2336 port = serial->port[i];
2337 p_priv = usb_get_serial_port_data(port);
2338 if (p_priv->inack_urb)
2339 usb_free_urb(p_priv->inack_urb);
2340 if (p_priv->outcont_urb)
2341 usb_free_urb(p_priv->outcont_urb);
2342 for (j = 0; j < 2; j++) {
2343 if (p_priv->in_urbs[j])
2344 usb_free_urb(p_priv->in_urbs[j]);
2345 if (p_priv->out_urbs[j])
2346 usb_free_urb(p_priv->out_urbs[j]);
2350 /* dbg("Freeing serial->private."); */
2353 /* dbg("Freeing port->private."); */
2354 /* Now free per port private data */
2355 for (i = 0; i < serial->num_ports; i++) {
2356 port = serial->port[i];
2357 kfree(usb_get_serial_port_data(port));
2361 MODULE_AUTHOR( DRIVER_AUTHOR );
2362 MODULE_DESCRIPTION( DRIVER_DESC );
2363 MODULE_LICENSE("GPL");
2365 MODULE_PARM(debug, "i");
2366 MODULE_PARM_DESC(debug, "Debug enabled or not");