2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 See http://misc.nu/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
31 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
32 Improve setup message handling for all devices.
34 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36 Linux source tree. The Linux tree lacked support for the 49WLC and
37 others. The Keyspan patches didn't work with the current kernel.
39 2003jan30 LPM add support for the 49WLC and MPR
41 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
43 now supported (including QI and QW). Modified port open, port
44 close, and send setup() logic to fix various data and endpoint
45 synchronization bugs and device LED status bugs. Changed keyspan_
46 write_room() to accurately return transmit buffer availability.
47 Changed forwardingLength from 1 to 16 for all adapters.
49 Fri Oct 12 16:45:00 EST 2001
50 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
52 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
54 now supported (including QI and QW). Modified port open, port
55 close, and send setup() logic to fix various data and endpoint
56 synchronization bugs and device LED status bugs. Changed keyspan_
57 write_room() to accurately return transmit buffer availability.
58 Changed forwardingLength from 1 to 16 for all adapters.
60 Fri Oct 12 16:45:00 EST 2001
61 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
63 Mon Oct 8 14:29:00 EST 2001 hugh
64 Fixed bug that prevented mulitport devices operating correctly
65 if they weren't the first unit attached.
67 Sat Oct 6 12:31:21 EST 2001 hugh
68 Added support for USA-28XA and -28XB, misc cleanups, break support
69 for usa26 based models thanks to David Gibson.
71 Thu May 31 11:56:42 PDT 2001 gkh
72 switched from using spinlock to a semaphore
75 Identify version on module load.
77 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
80 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
84 Wed Jul 19 14:00:42 EST 2000 gkh
85 Added module_init and module_exit functions to handle the fact that
86 this driver is a loadable module now.
88 Tue Jul 18 16:14:52 EST 2000 Hugh
89 Basic character input/output for USA-19 now mostly works,
90 fixed at 9600 baud for the moment.
92 Sat Jul 8 11:11:48 EST 2000 Hugh
93 First public release - nothing works except the firmware upload.
94 Tested on PPC and x86 architectures, seems to behave...
98 #include <linux/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <asm/uaccess.h>
109 #include <linux/usb.h>
110 #include <linux/usb/serial.h>
116 * Version Information
118 #define DRIVER_VERSION "v1.1.4"
119 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
120 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
122 #define INSTAT_BUFLEN 32
123 #define GLOCONT_BUFLEN 64
125 /* Per device and per port private data */
126 struct keyspan_serial_private {
127 const struct keyspan_device_details *device_details;
129 struct urb *instat_urb;
130 char instat_buf[INSTAT_BUFLEN];
132 /* XXX this one probably will need a lock */
133 struct urb *glocont_urb;
134 char glocont_buf[GLOCONT_BUFLEN];
137 struct keyspan_port_private {
138 /* Keep track of which input & output endpoints to use */
142 /* Keep duplicate of device details in each port
143 structure as well - simplifies some of the
144 callback functions etc. */
145 const struct keyspan_device_details *device_details;
147 /* Input endpoints and buffer for this port */
148 struct urb *in_urbs[2];
149 char in_buffer[2][64];
150 /* Output endpoints and buffer for this port */
151 struct urb *out_urbs[2];
152 char out_buffer[2][64];
154 /* Input ack endpoint */
155 struct urb *inack_urb;
156 char inack_buffer[1];
158 /* Output control endpoint */
159 struct urb *outcont_urb;
160 char outcont_buffer[64];
162 /* Settings for the port */
166 unsigned int old_cflag;
167 enum {flow_none, flow_cts, flow_xon} flow_control;
168 int rts_state; /* Handshaking pins (outputs) */
170 int cts_state; /* Handshaking pins (inputs) */
176 unsigned long tx_start_time[2];
177 int resend_cont; /* need to resend control packet */
181 /* Include Keyspan message headers. All current Keyspan Adapters
182 make use of one of four message formats which are referred
183 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
184 #include "keyspan_usa26msg.h"
185 #include "keyspan_usa28msg.h"
186 #include "keyspan_usa49msg.h"
187 #include "keyspan_usa90msg.h"
190 /* Functions used by new usb-serial code. */
191 static int __init keyspan_init (void)
194 retval = usb_serial_register(&keyspan_pre_device);
196 goto failed_pre_device_register;
197 retval = usb_serial_register(&keyspan_1port_device);
199 goto failed_1port_device_register;
200 retval = usb_serial_register(&keyspan_2port_device);
202 goto failed_2port_device_register;
203 retval = usb_serial_register(&keyspan_4port_device);
205 goto failed_4port_device_register;
206 retval = usb_register(&keyspan_driver);
208 goto failed_usb_register;
210 info(DRIVER_VERSION ":" DRIVER_DESC);
214 usb_serial_deregister(&keyspan_4port_device);
215 failed_4port_device_register:
216 usb_serial_deregister(&keyspan_2port_device);
217 failed_2port_device_register:
218 usb_serial_deregister(&keyspan_1port_device);
219 failed_1port_device_register:
220 usb_serial_deregister(&keyspan_pre_device);
221 failed_pre_device_register:
225 static void __exit keyspan_exit (void)
227 usb_deregister (&keyspan_driver);
228 usb_serial_deregister (&keyspan_pre_device);
229 usb_serial_deregister (&keyspan_1port_device);
230 usb_serial_deregister (&keyspan_2port_device);
231 usb_serial_deregister (&keyspan_4port_device);
234 module_init(keyspan_init);
235 module_exit(keyspan_exit);
237 static void keyspan_rx_throttle (struct usb_serial_port *port)
239 dbg("%s - port %d", __FUNCTION__, port->number);
243 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
245 dbg("%s - port %d", __FUNCTION__, port->number);
249 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
251 struct keyspan_port_private *p_priv;
253 dbg("%s", __FUNCTION__);
255 p_priv = usb_get_serial_port_data(port);
257 if (break_state == -1)
258 p_priv->break_on = 1;
260 p_priv->break_on = 0;
262 keyspan_send_setup(port, 0);
266 static void keyspan_set_termios (struct usb_serial_port *port,
267 struct ktermios *old_termios)
269 int baud_rate, device_port;
270 struct keyspan_port_private *p_priv;
271 const struct keyspan_device_details *d_details;
274 dbg("%s", __FUNCTION__);
276 p_priv = usb_get_serial_port_data(port);
277 d_details = p_priv->device_details;
278 cflag = port->tty->termios->c_cflag;
279 device_port = port->number - port->serial->minor;
281 /* Baud rate calculation takes baud rate as an integer
282 so other rates can be generated if desired. */
283 baud_rate = tty_get_baud_rate(port->tty);
284 /* If no match or invalid, don't change */
286 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
287 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
288 /* FIXME - more to do here to ensure rate changes cleanly */
289 p_priv->baud = baud_rate;
292 /* set CTS/RTS handshake etc. */
293 p_priv->cflag = cflag;
294 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
296 keyspan_send_setup(port, 0);
299 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
302 struct keyspan_port_private *p_priv;
304 p_priv = usb_get_serial_port_data(port);
306 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
307 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
308 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
309 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
310 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
311 ((p_priv->ri_state) ? TIOCM_RNG : 0);
316 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
317 unsigned int set, unsigned int clear)
319 struct keyspan_port_private *p_priv;
321 p_priv = usb_get_serial_port_data(port);
324 p_priv->rts_state = 1;
326 p_priv->dtr_state = 1;
328 if (clear & TIOCM_RTS)
329 p_priv->rts_state = 0;
330 if (clear & TIOCM_DTR)
331 p_priv->dtr_state = 0;
332 keyspan_send_setup(port, 0);
336 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
337 unsigned int cmd, unsigned long arg)
342 /* Write function is similar for the four protocols used
343 with only a minor change for usa90 (usa19hs) required */
344 static int keyspan_write(struct usb_serial_port *port,
345 const unsigned char *buf, int count)
347 struct keyspan_port_private *p_priv;
348 const struct keyspan_device_details *d_details;
351 struct urb *this_urb;
352 int err, maxDataLen, dataOffset;
354 p_priv = usb_get_serial_port_data(port);
355 d_details = p_priv->device_details;
357 if (d_details->msg_format == msg_usa90) {
365 dbg("%s - for port %d (%d chars), flip=%d",
366 __FUNCTION__, port->number, count, p_priv->out_flip);
368 for (left = count; left > 0; left -= todo) {
370 if (todo > maxDataLen)
373 flip = p_priv->out_flip;
375 /* Check we have a valid urb/endpoint before we use it... */
376 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
377 /* no bulk out, so return 0 bytes written */
378 dbg("%s - no output urb :(", __FUNCTION__);
382 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
384 if (this_urb->status == -EINPROGRESS) {
385 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
387 usb_unlink_urb(this_urb);
391 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
392 for now so set to zero */
393 ((char *)this_urb->transfer_buffer)[0] = 0;
395 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
398 /* send the data out the bulk port */
399 this_urb->transfer_buffer_length = todo + dataOffset;
401 this_urb->dev = port->serial->dev;
402 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
403 dbg("usb_submit_urb(write bulk) failed (%d)", err);
405 p_priv->tx_start_time[flip] = jiffies;
407 /* Flip for next time if usa26 or usa28 interface
408 (not used on usa49) */
409 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
415 static void usa26_indat_callback(struct urb *urb)
419 struct usb_serial_port *port;
420 struct tty_struct *tty;
421 unsigned char *data = urb->transfer_buffer;
423 dbg ("%s", __FUNCTION__);
425 endpoint = usb_pipeendpoint(urb->pipe);
428 dbg("%s - nonzero status: %x on endpoint %d.",
429 __FUNCTION__, urb->status, endpoint);
433 port = (struct usb_serial_port *) urb->context;
435 if (urb->actual_length) {
436 /* 0x80 bit is error flag */
437 if ((data[0] & 0x80) == 0) {
438 /* no errors on individual bytes, only possible overrun err*/
439 if (data[0] & RXERROR_OVERRUN)
442 for (i = 1; i < urb->actual_length ; ++i) {
443 tty_insert_flip_char(tty, data[i], err);
446 /* some bytes had errors, every byte has status */
447 dbg("%s - RX error!!!!", __FUNCTION__);
448 for (i = 0; i + 1 < urb->actual_length; i += 2) {
449 int stat = data[i], flag = 0;
450 if (stat & RXERROR_OVERRUN)
452 if (stat & RXERROR_FRAMING)
454 if (stat & RXERROR_PARITY)
456 /* XXX should handle break (0x10) */
457 tty_insert_flip_char(tty, data[i+1], flag);
460 tty_flip_buffer_push(tty);
463 /* Resubmit urb so we continue receiving */
464 urb->dev = port->serial->dev;
465 if (port->open_count)
466 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
467 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
472 /* Outdat handling is common for all devices */
473 static void usa2x_outdat_callback(struct urb *urb)
475 struct usb_serial_port *port;
476 struct keyspan_port_private *p_priv;
478 port = (struct usb_serial_port *) urb->context;
479 p_priv = usb_get_serial_port_data(port);
480 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
482 if (port->open_count)
483 usb_serial_port_softint(port);
486 static void usa26_inack_callback(struct urb *urb)
488 dbg ("%s", __FUNCTION__);
492 static void usa26_outcont_callback(struct urb *urb)
494 struct usb_serial_port *port;
495 struct keyspan_port_private *p_priv;
497 port = (struct usb_serial_port *) urb->context;
498 p_priv = usb_get_serial_port_data(port);
500 if (p_priv->resend_cont) {
501 dbg ("%s - sending setup", __FUNCTION__);
502 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
506 static void usa26_instat_callback(struct urb *urb)
508 unsigned char *data = urb->transfer_buffer;
509 struct keyspan_usa26_portStatusMessage *msg;
510 struct usb_serial *serial;
511 struct usb_serial_port *port;
512 struct keyspan_port_private *p_priv;
513 int old_dcd_state, err;
515 serial = (struct usb_serial *) urb->context;
518 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
521 if (urb->actual_length != 9) {
522 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
526 msg = (struct keyspan_usa26_portStatusMessage *)data;
529 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
530 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
531 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
534 /* Now do something useful with the data */
537 /* Check port number from message and retrieve private data */
538 if (msg->port >= serial->num_ports) {
539 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
542 port = serial->port[msg->port];
543 p_priv = usb_get_serial_port_data(port);
545 /* Update handshaking pin state information */
546 old_dcd_state = p_priv->dcd_state;
547 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
548 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
549 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
550 p_priv->ri_state = ((msg->ri) ? 1 : 0);
552 if (port->tty && !C_CLOCAL(port->tty)
553 && old_dcd_state != p_priv->dcd_state) {
555 tty_hangup(port->tty);
557 /* wake_up_interruptible(&p_priv->open_wait); */
560 /* Resubmit urb so we continue receiving */
561 urb->dev = serial->dev;
562 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
563 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
568 static void usa26_glocont_callback(struct urb *urb)
570 dbg ("%s", __FUNCTION__);
575 static void usa28_indat_callback(struct urb *urb)
578 struct usb_serial_port *port;
579 struct tty_struct *tty;
581 struct keyspan_port_private *p_priv;
583 dbg ("%s", __FUNCTION__);
585 port = (struct usb_serial_port *) urb->context;
586 p_priv = usb_get_serial_port_data(port);
587 data = urb->transfer_buffer;
589 if (urb != p_priv->in_urbs[p_priv->in_flip])
594 dbg("%s - nonzero status: %x on endpoint %d.",
595 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
599 port = (struct usb_serial_port *) urb->context;
600 p_priv = usb_get_serial_port_data(port);
601 data = urb->transfer_buffer;
604 if (urb->actual_length) {
605 for (i = 0; i < urb->actual_length ; ++i) {
606 tty_insert_flip_char(tty, data[i], 0);
608 tty_flip_buffer_push(tty);
611 /* Resubmit urb so we continue receiving */
612 urb->dev = port->serial->dev;
613 if (port->open_count)
614 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
615 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
617 p_priv->in_flip ^= 1;
619 urb = p_priv->in_urbs[p_priv->in_flip];
620 } while (urb->status != -EINPROGRESS);
623 static void usa28_inack_callback(struct urb *urb)
625 dbg ("%s", __FUNCTION__);
628 static void usa28_outcont_callback(struct urb *urb)
630 struct usb_serial_port *port;
631 struct keyspan_port_private *p_priv;
633 port = (struct usb_serial_port *) urb->context;
634 p_priv = usb_get_serial_port_data(port);
636 if (p_priv->resend_cont) {
637 dbg ("%s - sending setup", __FUNCTION__);
638 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
642 static void usa28_instat_callback(struct urb *urb)
645 unsigned char *data = urb->transfer_buffer;
646 struct keyspan_usa28_portStatusMessage *msg;
647 struct usb_serial *serial;
648 struct usb_serial_port *port;
649 struct keyspan_port_private *p_priv;
652 serial = (struct usb_serial *) urb->context;
655 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
659 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
660 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
664 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
665 data[0], data[1], data[2], data[3], data[4], data[5],
666 data[6], data[7], data[8], data[9], data[10], data[11]);*/
668 /* Now do something useful with the data */
669 msg = (struct keyspan_usa28_portStatusMessage *)data;
672 /* Check port number from message and retrieve private data */
673 if (msg->port >= serial->num_ports) {
674 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
677 port = serial->port[msg->port];
678 p_priv = usb_get_serial_port_data(port);
680 /* Update handshaking pin state information */
681 old_dcd_state = p_priv->dcd_state;
682 p_priv->cts_state = ((msg->cts) ? 1 : 0);
683 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
684 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
685 p_priv->ri_state = ((msg->ri) ? 1 : 0);
687 if (port->tty && !C_CLOCAL(port->tty)
688 && old_dcd_state != p_priv->dcd_state) {
690 tty_hangup(port->tty);
692 /* wake_up_interruptible(&p_priv->open_wait); */
695 /* Resubmit urb so we continue receiving */
696 urb->dev = serial->dev;
697 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
698 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
703 static void usa28_glocont_callback(struct urb *urb)
705 dbg ("%s", __FUNCTION__);
709 static void usa49_glocont_callback(struct urb *urb)
711 struct usb_serial *serial;
712 struct usb_serial_port *port;
713 struct keyspan_port_private *p_priv;
716 dbg ("%s", __FUNCTION__);
718 serial = (struct usb_serial *) urb->context;
719 for (i = 0; i < serial->num_ports; ++i) {
720 port = serial->port[i];
721 p_priv = usb_get_serial_port_data(port);
723 if (p_priv->resend_cont) {
724 dbg ("%s - sending setup", __FUNCTION__);
725 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
731 /* This is actually called glostat in the Keyspan
733 static void usa49_instat_callback(struct urb *urb)
736 unsigned char *data = urb->transfer_buffer;
737 struct keyspan_usa49_portStatusMessage *msg;
738 struct usb_serial *serial;
739 struct usb_serial_port *port;
740 struct keyspan_port_private *p_priv;
743 dbg ("%s", __FUNCTION__);
745 serial = (struct usb_serial *) urb->context;
748 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
752 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
753 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
757 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
758 data[0], data[1], data[2], data[3], data[4], data[5],
759 data[6], data[7], data[8], data[9], data[10]);*/
761 /* Now do something useful with the data */
762 msg = (struct keyspan_usa49_portStatusMessage *)data;
764 /* Check port number from message and retrieve private data */
765 if (msg->portNumber >= serial->num_ports) {
766 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
769 port = serial->port[msg->portNumber];
770 p_priv = usb_get_serial_port_data(port);
772 /* Update handshaking pin state information */
773 old_dcd_state = p_priv->dcd_state;
774 p_priv->cts_state = ((msg->cts) ? 1 : 0);
775 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
776 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
777 p_priv->ri_state = ((msg->ri) ? 1 : 0);
779 if (port->tty && !C_CLOCAL(port->tty)
780 && old_dcd_state != p_priv->dcd_state) {
782 tty_hangup(port->tty);
784 /* wake_up_interruptible(&p_priv->open_wait); */
787 /* Resubmit urb so we continue receiving */
788 urb->dev = serial->dev;
790 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
791 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
796 static void usa49_inack_callback(struct urb *urb)
798 dbg ("%s", __FUNCTION__);
801 static void usa49_indat_callback(struct urb *urb)
805 struct usb_serial_port *port;
806 struct tty_struct *tty;
807 unsigned char *data = urb->transfer_buffer;
809 dbg ("%s", __FUNCTION__);
811 endpoint = usb_pipeendpoint(urb->pipe);
814 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
815 urb->status, endpoint);
819 port = (struct usb_serial_port *) urb->context;
821 if (urb->actual_length) {
822 /* 0x80 bit is error flag */
823 if ((data[0] & 0x80) == 0) {
824 /* no error on any byte */
825 for (i = 1; i < urb->actual_length ; ++i) {
826 tty_insert_flip_char(tty, data[i], 0);
829 /* some bytes had errors, every byte has status */
830 for (i = 0; i + 1 < urb->actual_length; i += 2) {
831 int stat = data[i], flag = 0;
832 if (stat & RXERROR_OVERRUN)
834 if (stat & RXERROR_FRAMING)
836 if (stat & RXERROR_PARITY)
838 /* XXX should handle break (0x10) */
839 tty_insert_flip_char(tty, data[i+1], flag);
842 tty_flip_buffer_push(tty);
845 /* Resubmit urb so we continue receiving */
846 urb->dev = port->serial->dev;
847 if (port->open_count)
848 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
849 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
853 /* not used, usa-49 doesn't have per-port control endpoints */
854 static void usa49_outcont_callback(struct urb *urb)
856 dbg ("%s", __FUNCTION__);
859 static void usa90_indat_callback(struct urb *urb)
863 struct usb_serial_port *port;
864 struct keyspan_port_private *p_priv;
865 struct tty_struct *tty;
866 unsigned char *data = urb->transfer_buffer;
868 dbg ("%s", __FUNCTION__);
870 endpoint = usb_pipeendpoint(urb->pipe);
874 dbg("%s - nonzero status: %x on endpoint %d.",
875 __FUNCTION__, urb->status, endpoint);
879 port = (struct usb_serial_port *) urb->context;
880 p_priv = usb_get_serial_port_data(port);
883 if (urb->actual_length) {
885 /* if current mode is DMA, looks like usa28 format
886 otherwise looks like usa26 data format */
888 if (p_priv->baud > 57600) {
889 for (i = 0; i < urb->actual_length ; ++i)
890 tty_insert_flip_char(tty, data[i], 0);
894 /* 0x80 bit is error flag */
895 if ((data[0] & 0x80) == 0) {
896 /* no errors on individual bytes, only possible overrun err*/
897 if (data[0] & RXERROR_OVERRUN)
900 for (i = 1; i < urb->actual_length ; ++i)
901 tty_insert_flip_char(tty, data[i], err);
905 /* some bytes had errors, every byte has status */
906 dbg("%s - RX error!!!!", __FUNCTION__);
907 for (i = 0; i + 1 < urb->actual_length; i += 2) {
908 int stat = data[i], flag = 0;
909 if (stat & RXERROR_OVERRUN)
911 if (stat & RXERROR_FRAMING)
913 if (stat & RXERROR_PARITY)
915 /* XXX should handle break (0x10) */
916 tty_insert_flip_char(tty, data[i+1], flag);
920 tty_flip_buffer_push(tty);
923 /* Resubmit urb so we continue receiving */
924 urb->dev = port->serial->dev;
925 if (port->open_count)
926 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
927 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
933 static void usa90_instat_callback(struct urb *urb)
935 unsigned char *data = urb->transfer_buffer;
936 struct keyspan_usa90_portStatusMessage *msg;
937 struct usb_serial *serial;
938 struct usb_serial_port *port;
939 struct keyspan_port_private *p_priv;
940 int old_dcd_state, err;
942 serial = (struct usb_serial *) urb->context;
945 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
948 if (urb->actual_length < 14) {
949 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
953 msg = (struct keyspan_usa90_portStatusMessage *)data;
955 /* Now do something useful with the data */
957 port = serial->port[0];
958 p_priv = usb_get_serial_port_data(port);
960 /* Update handshaking pin state information */
961 old_dcd_state = p_priv->dcd_state;
962 p_priv->cts_state = ((msg->cts) ? 1 : 0);
963 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
964 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
965 p_priv->ri_state = ((msg->ri) ? 1 : 0);
967 if (port->tty && !C_CLOCAL(port->tty)
968 && old_dcd_state != p_priv->dcd_state) {
970 tty_hangup(port->tty);
972 /* wake_up_interruptible(&p_priv->open_wait); */
975 /* Resubmit urb so we continue receiving */
976 urb->dev = serial->dev;
977 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
978 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
984 static void usa90_outcont_callback(struct urb *urb)
986 struct usb_serial_port *port;
987 struct keyspan_port_private *p_priv;
989 port = (struct usb_serial_port *) urb->context;
990 p_priv = usb_get_serial_port_data(port);
992 if (p_priv->resend_cont) {
993 dbg ("%s - sending setup", __FUNCTION__);
994 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
998 static int keyspan_write_room (struct usb_serial_port *port)
1000 struct keyspan_port_private *p_priv;
1001 const struct keyspan_device_details *d_details;
1004 struct urb *this_urb;
1006 dbg("%s", __FUNCTION__);
1007 p_priv = usb_get_serial_port_data(port);
1008 d_details = p_priv->device_details;
1010 if (d_details->msg_format == msg_usa90)
1015 flip = p_priv->out_flip;
1017 /* Check both endpoints to see if any are available. */
1018 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1019 if (this_urb->status != -EINPROGRESS)
1021 flip = (flip + 1) & d_details->outdat_endp_flip;
1022 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1023 if (this_urb->status != -EINPROGRESS)
1030 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1036 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1038 struct keyspan_port_private *p_priv;
1039 struct keyspan_serial_private *s_priv;
1040 struct usb_serial *serial = port->serial;
1041 const struct keyspan_device_details *d_details;
1043 int baud_rate, device_port;
1047 s_priv = usb_get_serial_data(serial);
1048 p_priv = usb_get_serial_port_data(port);
1049 d_details = p_priv->device_details;
1051 dbg("%s - port%d.", __FUNCTION__, port->number);
1053 /* Set some sane defaults */
1054 p_priv->rts_state = 1;
1055 p_priv->dtr_state = 1;
1056 p_priv->baud = 9600;
1058 /* force baud and lcr to be set on open */
1059 p_priv->old_baud = 0;
1060 p_priv->old_cflag = 0;
1062 p_priv->out_flip = 0;
1063 p_priv->in_flip = 0;
1065 /* Reset low level data toggle and start reading from endpoints */
1066 for (i = 0; i < 2; i++) {
1067 if ((urb = p_priv->in_urbs[i]) == NULL)
1069 urb->dev = serial->dev;
1071 /* make sure endpoint data toggle is synchronized with the device */
1073 usb_clear_halt(urb->dev, urb->pipe);
1075 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1076 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1080 /* Reset low level data toggle on out endpoints */
1081 for (i = 0; i < 2; i++) {
1082 if ((urb = p_priv->out_urbs[i]) == NULL)
1084 urb->dev = serial->dev;
1085 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1088 /* get the terminal config for the setup message now so we don't
1089 * need to send 2 of them */
1091 cflag = port->tty->termios->c_cflag;
1092 device_port = port->number - port->serial->minor;
1094 /* Baud rate calculation takes baud rate as an integer
1095 so other rates can be generated if desired. */
1096 baud_rate = tty_get_baud_rate(port->tty);
1097 /* If no match or invalid, leave as default */
1099 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1100 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1101 p_priv->baud = baud_rate;
1104 /* set CTS/RTS handshake etc. */
1105 p_priv->cflag = cflag;
1106 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1108 keyspan_send_setup(port, 1);
1110 //keyspan_set_termios(port, NULL);
1115 static inline void stop_urb(struct urb *urb)
1117 if (urb && urb->status == -EINPROGRESS)
1121 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1124 struct usb_serial *serial = port->serial;
1125 struct keyspan_serial_private *s_priv;
1126 struct keyspan_port_private *p_priv;
1128 dbg("%s", __FUNCTION__);
1129 s_priv = usb_get_serial_data(serial);
1130 p_priv = usb_get_serial_port_data(port);
1132 p_priv->rts_state = 0;
1133 p_priv->dtr_state = 0;
1136 keyspan_send_setup(port, 2);
1137 /* pilot-xfer seems to work best with this delay */
1139 // keyspan_set_termios(port, NULL);
1142 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1143 dbg("%s - urb in progress", __FUNCTION__);
1146 p_priv->out_flip = 0;
1147 p_priv->in_flip = 0;
1150 /* Stop reading/writing urbs */
1151 stop_urb(p_priv->inack_urb);
1152 /* stop_urb(p_priv->outcont_urb); */
1153 for (i = 0; i < 2; i++) {
1154 stop_urb(p_priv->in_urbs[i]);
1155 stop_urb(p_priv->out_urbs[i]);
1162 /* download the firmware to a pre-renumeration device */
1163 static int keyspan_fake_startup (struct usb_serial *serial)
1166 const struct ezusb_hex_record *record;
1169 dbg("Keyspan startup version %04x product %04x",
1170 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1171 le16_to_cpu(serial->dev->descriptor.idProduct));
1173 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1174 dbg("Firmware already loaded. Quitting.");
1178 /* Select firmware image on the basis of idProduct */
1179 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1180 case keyspan_usa28_pre_product_id:
1181 record = &keyspan_usa28_firmware[0];
1185 case keyspan_usa28x_pre_product_id:
1186 record = &keyspan_usa28x_firmware[0];
1190 case keyspan_usa28xa_pre_product_id:
1191 record = &keyspan_usa28xa_firmware[0];
1192 fw_name = "USA28XA";
1195 case keyspan_usa28xb_pre_product_id:
1196 record = &keyspan_usa28xb_firmware[0];
1197 fw_name = "USA28XB";
1200 case keyspan_usa19_pre_product_id:
1201 record = &keyspan_usa19_firmware[0];
1205 case keyspan_usa19qi_pre_product_id:
1206 record = &keyspan_usa19qi_firmware[0];
1207 fw_name = "USA19QI";
1210 case keyspan_mpr_pre_product_id:
1211 record = &keyspan_mpr_firmware[0];
1215 case keyspan_usa19qw_pre_product_id:
1216 record = &keyspan_usa19qw_firmware[0];
1217 fw_name = "USA19QI";
1220 case keyspan_usa18x_pre_product_id:
1221 record = &keyspan_usa18x_firmware[0];
1225 case keyspan_usa19w_pre_product_id:
1226 record = &keyspan_usa19w_firmware[0];
1230 case keyspan_usa49w_pre_product_id:
1231 record = &keyspan_usa49w_firmware[0];
1235 case keyspan_usa49wlc_pre_product_id:
1236 record = &keyspan_usa49wlc_firmware[0];
1237 fw_name = "USA49WLC";
1242 fw_name = "Unknown";
1246 if (record == NULL) {
1247 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1251 dbg("Uploading Keyspan %s firmware.", fw_name);
1253 /* download the firmware image */
1254 response = ezusb_set_reset(serial, 1);
1256 while(record->address != 0xffff) {
1257 response = ezusb_writememory(serial, record->address,
1258 (unsigned char *)record->data,
1259 record->data_size, 0xa0);
1261 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1262 "firmware (%d %04X %p %d)\n",
1264 record->address, record->data, record->data_size);
1269 /* bring device out of reset. Renumeration will occur in a
1270 moment and the new device will bind to the real driver */
1271 response = ezusb_set_reset(serial, 0);
1273 /* we don't want this device to have a driver assigned to it. */
1277 /* Helper functions used by keyspan_setup_urbs */
1278 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1281 struct usb_host_interface *iface_desc;
1282 struct usb_endpoint_descriptor *ep;
1285 iface_desc = serial->interface->cur_altsetting;
1286 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1287 ep = &iface_desc->endpoint[i].desc;
1288 if (ep->bEndpointAddress == endpoint)
1291 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1292 "endpoint %x\n", endpoint);
1296 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1297 int dir, void *ctx, char *buf, int len,
1298 void (*callback)(struct urb *))
1301 struct usb_endpoint_descriptor const *ep_desc;
1302 char const *ep_type_name;
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 ep_desc = find_ep(serial, endpoint);
1316 /* leak the urb, something's wrong and the callers don't care */
1319 if (usb_endpoint_xfer_int(ep_desc)) {
1320 ep_type_name = "INT";
1321 usb_fill_int_urb(urb, serial->dev,
1322 usb_sndintpipe(serial->dev, endpoint) | dir,
1323 buf, len, callback, ctx,
1324 ep_desc->bInterval);
1325 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1326 ep_type_name = "BULK";
1327 usb_fill_bulk_urb(urb, serial->dev,
1328 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1329 buf, len, callback, ctx);
1331 dev_warn(&serial->interface->dev,
1332 "unsupported endpoint type %x\n",
1333 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1338 dbg("%s - using urb %p for %s endpoint %x",
1339 __func__, urb, ep_type_name, endpoint);
1343 static struct callbacks {
1344 void (*instat_callback)(struct urb *);
1345 void (*glocont_callback)(struct urb *);
1346 void (*indat_callback)(struct urb *);
1347 void (*outdat_callback)(struct urb *);
1348 void (*inack_callback)(struct urb *);
1349 void (*outcont_callback)(struct urb *);
1350 } keyspan_callbacks[] = {
1352 /* msg_usa26 callbacks */
1353 .instat_callback = usa26_instat_callback,
1354 .glocont_callback = usa26_glocont_callback,
1355 .indat_callback = usa26_indat_callback,
1356 .outdat_callback = usa2x_outdat_callback,
1357 .inack_callback = usa26_inack_callback,
1358 .outcont_callback = usa26_outcont_callback,
1360 /* msg_usa28 callbacks */
1361 .instat_callback = usa28_instat_callback,
1362 .glocont_callback = usa28_glocont_callback,
1363 .indat_callback = usa28_indat_callback,
1364 .outdat_callback = usa2x_outdat_callback,
1365 .inack_callback = usa28_inack_callback,
1366 .outcont_callback = usa28_outcont_callback,
1368 /* msg_usa49 callbacks */
1369 .instat_callback = usa49_instat_callback,
1370 .glocont_callback = usa49_glocont_callback,
1371 .indat_callback = usa49_indat_callback,
1372 .outdat_callback = usa2x_outdat_callback,
1373 .inack_callback = usa49_inack_callback,
1374 .outcont_callback = usa49_outcont_callback,
1376 /* msg_usa90 callbacks */
1377 .instat_callback = usa90_instat_callback,
1378 .glocont_callback = usa28_glocont_callback,
1379 .indat_callback = usa90_indat_callback,
1380 .outdat_callback = usa2x_outdat_callback,
1381 .inack_callback = usa28_inack_callback,
1382 .outcont_callback = usa90_outcont_callback,
1386 /* Generic setup urbs function that uses
1387 data in device_details */
1388 static void keyspan_setup_urbs(struct usb_serial *serial)
1391 struct keyspan_serial_private *s_priv;
1392 const struct keyspan_device_details *d_details;
1393 struct usb_serial_port *port;
1394 struct keyspan_port_private *p_priv;
1395 struct callbacks *cback;
1398 dbg ("%s", __FUNCTION__);
1400 s_priv = usb_get_serial_data(serial);
1401 d_details = s_priv->device_details;
1403 /* Setup values for the various callback routines */
1404 cback = &keyspan_callbacks[d_details->msg_format];
1406 /* Allocate and set up urbs for each one that is in use,
1407 starting with instat endpoints */
1408 s_priv->instat_urb = keyspan_setup_urb
1409 (serial, d_details->instat_endpoint, USB_DIR_IN,
1410 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1411 cback->instat_callback);
1413 s_priv->glocont_urb = keyspan_setup_urb
1414 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1415 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1416 cback->glocont_callback);
1418 /* Setup endpoints for each port specific thing */
1419 for (i = 0; i < d_details->num_ports; i ++) {
1420 port = serial->port[i];
1421 p_priv = usb_get_serial_port_data(port);
1423 /* Do indat endpoints first, once for each flip */
1424 endp = d_details->indat_endpoints[i];
1425 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1426 p_priv->in_urbs[j] = keyspan_setup_urb
1427 (serial, endp, USB_DIR_IN, port,
1428 p_priv->in_buffer[j], 64,
1429 cback->indat_callback);
1432 p_priv->in_urbs[j] = NULL;
1434 /* outdat endpoints also have flip */
1435 endp = d_details->outdat_endpoints[i];
1436 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1437 p_priv->out_urbs[j] = keyspan_setup_urb
1438 (serial, endp, USB_DIR_OUT, port,
1439 p_priv->out_buffer[j], 64,
1440 cback->outdat_callback);
1443 p_priv->out_urbs[j] = NULL;
1445 /* inack endpoint */
1446 p_priv->inack_urb = keyspan_setup_urb
1447 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1448 port, p_priv->inack_buffer, 1, cback->inack_callback);
1450 /* outcont endpoint */
1451 p_priv->outcont_urb = keyspan_setup_urb
1452 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1453 port, p_priv->outcont_buffer, 64,
1454 cback->outcont_callback);
1459 /* usa19 function doesn't require prescaler */
1460 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1461 u8 *rate_low, u8 *prescaler, int portnum)
1463 u32 b16, /* baud rate times 16 (actual rate used internally) */
1465 cnt; /* inverse of divisor (programmed into 8051) */
1467 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1469 /* prevent divide by zero... */
1470 if( (b16 = (baud_rate * 16L)) == 0) {
1471 return (KEYSPAN_INVALID_BAUD_RATE);
1474 /* Any "standard" rate over 57k6 is marginal on the USA-19
1475 as we run out of divisor resolution. */
1476 if (baud_rate > 57600) {
1477 return (KEYSPAN_INVALID_BAUD_RATE);
1480 /* calculate the divisor and the counter (its inverse) */
1481 if( (div = (baudclk / b16)) == 0) {
1482 return (KEYSPAN_INVALID_BAUD_RATE);
1489 return (KEYSPAN_INVALID_BAUD_RATE);
1492 /* return the counter values if non-null */
1494 *rate_low = (u8) (cnt & 0xff);
1497 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1499 if (rate_low && rate_hi) {
1500 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1503 return (KEYSPAN_BAUD_RATE_OK);
1506 /* usa19hs function doesn't require prescaler */
1507 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1508 u8 *rate_low, u8 *prescaler, int portnum)
1510 u32 b16, /* baud rate times 16 (actual rate used internally) */
1513 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1515 /* prevent divide by zero... */
1516 if( (b16 = (baud_rate * 16L)) == 0)
1517 return (KEYSPAN_INVALID_BAUD_RATE);
1521 /* calculate the divisor */
1522 if( (div = (baudclk / b16)) == 0)
1523 return (KEYSPAN_INVALID_BAUD_RATE);
1526 return (KEYSPAN_INVALID_BAUD_RATE);
1528 /* return the counter values if non-null */
1530 *rate_low = (u8) (div & 0xff);
1533 *rate_hi = (u8) ((div >> 8) & 0xff);
1535 if (rate_low && rate_hi)
1536 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1538 return (KEYSPAN_BAUD_RATE_OK);
1541 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1542 u8 *rate_low, u8 *prescaler, int portnum)
1544 u32 b16, /* baud rate times 16 (actual rate used internally) */
1545 clk, /* clock with 13/8 prescaler */
1546 div, /* divisor using 13/8 prescaler */
1547 res, /* resulting baud rate using 13/8 prescaler */
1548 diff, /* error using 13/8 prescaler */
1553 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1555 /* prevent divide by zero */
1556 if( (b16 = baud_rate * 16L) == 0) {
1557 return (KEYSPAN_INVALID_BAUD_RATE);
1560 /* Calculate prescaler by trying them all and looking
1563 /* start with largest possible difference */
1564 smallest_diff = 0xffffffff;
1566 /* 0 is an invalid prescaler, used as a flag */
1569 for(i = 8; i <= 0xff; ++i) {
1570 clk = (baudclk * 8) / (u32) i;
1572 if( (div = clk / b16) == 0) {
1577 diff= (res > b16) ? (res-b16) : (b16-res);
1579 if(diff < smallest_diff) {
1581 smallest_diff = diff;
1585 if(best_prescaler == 0) {
1586 return (KEYSPAN_INVALID_BAUD_RATE);
1589 clk = (baudclk * 8) / (u32) best_prescaler;
1592 /* return the divisor and prescaler if non-null */
1594 *rate_low = (u8) (div & 0xff);
1597 *rate_hi = (u8) ((div >> 8) & 0xff);
1600 *prescaler = best_prescaler;
1601 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1603 return (KEYSPAN_BAUD_RATE_OK);
1606 /* USA-28 supports different maximum baud rates on each port */
1607 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1608 u8 *rate_low, u8 *prescaler, int portnum)
1610 u32 b16, /* baud rate times 16 (actual rate used internally) */
1612 cnt; /* inverse of divisor (programmed into 8051) */
1614 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1616 /* prevent divide by zero */
1617 if ((b16 = baud_rate * 16L) == 0)
1618 return (KEYSPAN_INVALID_BAUD_RATE);
1620 /* calculate the divisor and the counter (its inverse) */
1621 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1622 return (KEYSPAN_INVALID_BAUD_RATE);
1628 /* check for out of range, based on portnum,
1629 and return result */
1632 return (KEYSPAN_INVALID_BAUD_RATE);
1637 return (KEYSPAN_INVALID_BAUD_RATE);
1641 return (KEYSPAN_INVALID_BAUD_RATE);
1645 /* return the counter values if not NULL
1646 (port 1 will ignore retHi) */
1648 *rate_low = (u8) (cnt & 0xff);
1651 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1653 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1654 return (KEYSPAN_BAUD_RATE_OK);
1657 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1658 struct usb_serial_port *port,
1661 struct keyspan_usa26_portControlMessage msg;
1662 struct keyspan_serial_private *s_priv;
1663 struct keyspan_port_private *p_priv;
1664 const struct keyspan_device_details *d_details;
1666 struct urb *this_urb;
1667 int device_port, err;
1669 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1671 s_priv = usb_get_serial_data(serial);
1672 p_priv = usb_get_serial_port_data(port);
1673 d_details = s_priv->device_details;
1674 device_port = port->number - port->serial->minor;
1676 outcont_urb = d_details->outcont_endpoints[port->number];
1677 this_urb = p_priv->outcont_urb;
1679 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1681 /* Make sure we have an urb then send the message */
1682 if (this_urb == NULL) {
1683 dbg("%s - oops no urb.", __FUNCTION__);
1687 /* Save reset port val for resend.
1688 Don't overwrite resend for close condition. */
1689 if (p_priv->resend_cont != 3)
1690 p_priv->resend_cont = reset_port + 1;
1691 if (this_urb->status == -EINPROGRESS) {
1692 /* dbg ("%s - already writing", __FUNCTION__); */
1697 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1699 /* Only set baud rate if it's changed */
1700 if (p_priv->old_baud != p_priv->baud) {
1701 p_priv->old_baud = p_priv->baud;
1702 msg.setClocking = 0xff;
1703 if (d_details->calculate_baud_rate
1704 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1705 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1706 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1709 msg.baudHi = 125; /* Values for 9600 baud */
1712 msg.setPrescaler = 0xff;
1715 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1716 switch (p_priv->cflag & CSIZE) {
1718 msg.lcr |= USA_DATABITS_5;
1721 msg.lcr |= USA_DATABITS_6;
1724 msg.lcr |= USA_DATABITS_7;
1727 msg.lcr |= USA_DATABITS_8;
1730 if (p_priv->cflag & PARENB) {
1731 /* note USA_PARITY_NONE == 0 */
1732 msg.lcr |= (p_priv->cflag & PARODD)?
1733 USA_PARITY_ODD: USA_PARITY_EVEN;
1737 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1738 msg.xonFlowControl = 0;
1739 msg.setFlowControl = 0xff;
1740 msg.forwardingLength = 16;
1745 if (reset_port == 1) {
1754 msg.returnStatus = 0;
1755 msg.resetDataToggle = 0xff;
1759 else if (reset_port == 2) {
1768 msg.returnStatus = 0;
1769 msg.resetDataToggle = 0;
1772 /* Sending intermediate configs */
1774 msg._txOn = (! p_priv->break_on);
1777 msg.txBreak = (p_priv->break_on);
1782 msg.returnStatus = 0;
1783 msg.resetDataToggle = 0x0;
1786 /* Do handshaking outputs */
1787 msg.setTxTriState_setRts = 0xff;
1788 msg.txTriState_rts = p_priv->rts_state;
1790 msg.setHskoa_setDtr = 0xff;
1791 msg.hskoa_dtr = p_priv->dtr_state;
1793 p_priv->resend_cont = 0;
1794 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1796 /* send the data out the device on control endpoint */
1797 this_urb->transfer_buffer_length = sizeof(msg);
1799 this_urb->dev = serial->dev;
1800 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1801 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1805 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1806 outcont_urb, this_urb->transfer_buffer_length,
1807 usb_pipeendpoint(this_urb->pipe));
1814 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1815 struct usb_serial_port *port,
1818 struct keyspan_usa28_portControlMessage msg;
1819 struct keyspan_serial_private *s_priv;
1820 struct keyspan_port_private *p_priv;
1821 const struct keyspan_device_details *d_details;
1822 struct urb *this_urb;
1823 int device_port, err;
1825 dbg ("%s", __FUNCTION__);
1827 s_priv = usb_get_serial_data(serial);
1828 p_priv = usb_get_serial_port_data(port);
1829 d_details = s_priv->device_details;
1830 device_port = port->number - port->serial->minor;
1832 /* only do something if we have a bulk out endpoint */
1833 if ((this_urb = p_priv->outcont_urb) == NULL) {
1834 dbg("%s - oops no urb.", __FUNCTION__);
1838 /* Save reset port val for resend.
1839 Don't overwrite resend for close condition. */
1840 if (p_priv->resend_cont != 3)
1841 p_priv->resend_cont = reset_port + 1;
1842 if (this_urb->status == -EINPROGRESS) {
1843 dbg ("%s already writing", __FUNCTION__);
1848 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1850 msg.setBaudRate = 1;
1851 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1852 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1853 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1855 msg.baudHi = 0xb2; /* Values for 9600 baud */
1858 /* If parity is enabled, we must calculate it ourselves. */
1859 msg.parity = 0; /* XXX for now */
1861 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1862 msg.xonFlowControl = 0;
1864 /* Do handshaking outputs, DTR is inverted relative to RTS */
1865 msg.rts = p_priv->rts_state;
1866 msg.dtr = p_priv->dtr_state;
1868 msg.forwardingLength = 16;
1870 msg.breakThreshold = 45;
1874 /*msg.returnStatus = 1;
1875 msg.resetDataToggle = 0xff;*/
1877 if (reset_port == 1) {
1881 msg.txForceXoff = 0;
1887 msg.returnStatus = 0;
1888 msg.resetDataToggle = 0xff;
1891 else if (reset_port == 2) {
1895 msg.txForceXoff = 0;
1901 msg.returnStatus = 0;
1902 msg.resetDataToggle = 0;
1904 /* Sending intermediate configs */
1906 msg._txOn = (! p_priv->break_on);
1909 msg.txForceXoff = 0;
1910 msg.txBreak = (p_priv->break_on);
1915 msg.returnStatus = 0;
1916 msg.resetDataToggle = 0x0;
1919 p_priv->resend_cont = 0;
1920 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1922 /* send the data out the device on control endpoint */
1923 this_urb->transfer_buffer_length = sizeof(msg);
1925 this_urb->dev = serial->dev;
1926 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1927 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1931 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1932 this_urb->transfer_buffer_length);
1939 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1940 struct usb_serial_port *port,
1943 struct keyspan_usa49_portControlMessage msg;
1944 struct keyspan_serial_private *s_priv;
1945 struct keyspan_port_private *p_priv;
1946 const struct keyspan_device_details *d_details;
1948 struct urb *this_urb;
1949 int err, device_port;
1951 dbg ("%s", __FUNCTION__);
1953 s_priv = usb_get_serial_data(serial);
1954 p_priv = usb_get_serial_port_data(port);
1955 d_details = s_priv->device_details;
1957 glocont_urb = d_details->glocont_endpoint;
1958 this_urb = s_priv->glocont_urb;
1960 /* Work out which port within the device is being setup */
1961 device_port = port->number - port->serial->minor;
1963 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1965 /* Make sure we have an urb then send the message */
1966 if (this_urb == NULL) {
1967 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1971 /* Save reset port val for resend.
1972 Don't overwrite resend for close condition. */
1973 if (p_priv->resend_cont != 3)
1974 p_priv->resend_cont = reset_port + 1;
1975 if (this_urb->status == -EINPROGRESS) {
1976 /* dbg ("%s - already writing", __FUNCTION__); */
1981 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1983 /*msg.portNumber = port->number;*/
1984 msg.portNumber = device_port;
1986 /* Only set baud rate if it's changed */
1987 if (p_priv->old_baud != p_priv->baud) {
1988 p_priv->old_baud = p_priv->baud;
1989 msg.setClocking = 0xff;
1990 if (d_details->calculate_baud_rate
1991 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1992 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1993 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1996 msg.baudHi = 125; /* Values for 9600 baud */
1999 //msg.setPrescaler = 0xff;
2002 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2003 switch (p_priv->cflag & CSIZE) {
2005 msg.lcr |= USA_DATABITS_5;
2008 msg.lcr |= USA_DATABITS_6;
2011 msg.lcr |= USA_DATABITS_7;
2014 msg.lcr |= USA_DATABITS_8;
2017 if (p_priv->cflag & PARENB) {
2018 /* note USA_PARITY_NONE == 0 */
2019 msg.lcr |= (p_priv->cflag & PARODD)?
2020 USA_PARITY_ODD: USA_PARITY_EVEN;
2024 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2025 msg.xonFlowControl = 0;
2026 msg.setFlowControl = 0xff;
2028 msg.forwardingLength = 16;
2033 if (reset_port == 1) {
2042 msg.returnStatus = 0;
2043 msg.resetDataToggle = 0xff;
2045 msg.disablePort = 0;
2048 else if (reset_port == 2) {
2057 msg.returnStatus = 0;
2058 msg.resetDataToggle = 0;
2060 msg.disablePort = 1;
2062 /* Sending intermediate configs */
2064 msg._txOn = (! p_priv->break_on);
2067 msg.txBreak = (p_priv->break_on);
2072 msg.returnStatus = 0;
2073 msg.resetDataToggle = 0x0;
2075 msg.disablePort = 0;
2078 /* Do handshaking outputs */
2080 msg.rts = p_priv->rts_state;
2083 msg.dtr = p_priv->dtr_state;
2085 p_priv->resend_cont = 0;
2086 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2088 /* send the data out the device on control endpoint */
2089 this_urb->transfer_buffer_length = sizeof(msg);
2091 this_urb->dev = serial->dev;
2092 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2093 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2097 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2098 outcont_urb, this_urb->transfer_buffer_length,
2099 usb_pipeendpoint(this_urb->pipe));
2106 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2107 struct usb_serial_port *port,
2110 struct keyspan_usa90_portControlMessage msg;
2111 struct keyspan_serial_private *s_priv;
2112 struct keyspan_port_private *p_priv;
2113 const struct keyspan_device_details *d_details;
2114 struct urb *this_urb;
2118 dbg ("%s", __FUNCTION__);
2120 s_priv = usb_get_serial_data(serial);
2121 p_priv = usb_get_serial_port_data(port);
2122 d_details = s_priv->device_details;
2124 /* only do something if we have a bulk out endpoint */
2125 if ((this_urb = p_priv->outcont_urb) == NULL) {
2126 dbg("%s - oops no urb.", __FUNCTION__);
2130 /* Save reset port val for resend.
2131 Don't overwrite resend for open/close condition. */
2132 if ((reset_port + 1) > p_priv->resend_cont)
2133 p_priv->resend_cont = reset_port + 1;
2134 if (this_urb->status == -EINPROGRESS) {
2135 dbg ("%s already writing", __FUNCTION__);
2140 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2142 /* Only set baud rate if it's changed */
2143 if (p_priv->old_baud != p_priv->baud) {
2144 p_priv->old_baud = p_priv->baud;
2145 msg.setClocking = 0x01;
2146 if (d_details->calculate_baud_rate
2147 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2148 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2149 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2151 p_priv->baud = 9600;
2152 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2153 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2159 /* modes must always be correctly specified */
2160 if (p_priv->baud > 57600)
2162 msg.rxMode = RXMODE_DMA;
2163 msg.txMode = TXMODE_DMA;
2167 msg.rxMode = RXMODE_BYHAND;
2168 msg.txMode = TXMODE_BYHAND;
2171 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2172 switch (p_priv->cflag & CSIZE) {
2174 msg.lcr |= USA_DATABITS_5;
2177 msg.lcr |= USA_DATABITS_6;
2180 msg.lcr |= USA_DATABITS_7;
2183 msg.lcr |= USA_DATABITS_8;
2186 if (p_priv->cflag & PARENB) {
2187 /* note USA_PARITY_NONE == 0 */
2188 msg.lcr |= (p_priv->cflag & PARODD)?
2189 USA_PARITY_ODD: USA_PARITY_EVEN;
2191 if (p_priv->old_cflag != p_priv->cflag) {
2192 p_priv->old_cflag = p_priv->cflag;
2196 if (p_priv->flow_control == flow_cts)
2197 msg.txFlowControl = TXFLOW_CTS;
2198 msg.setTxFlowControl = 0x01;
2199 msg.setRxFlowControl = 0x01;
2201 msg.rxForwardingLength = 16;
2202 msg.rxForwardingTimeout = 16;
2203 msg.txAckSetting = 0;
2208 if (reset_port == 1) {
2209 msg.portEnabled = 1;
2211 msg.txBreak = (p_priv->break_on);
2214 else if (reset_port == 2) {
2215 msg.portEnabled = 0;
2217 /* Sending intermediate configs */
2219 if (port->open_count)
2220 msg.portEnabled = 1;
2221 msg.txBreak = (p_priv->break_on);
2224 /* Do handshaking outputs */
2226 msg.rts = p_priv->rts_state;
2229 msg.dtr = p_priv->dtr_state;
2231 p_priv->resend_cont = 0;
2232 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2234 /* send the data out the device on control endpoint */
2235 this_urb->transfer_buffer_length = sizeof(msg);
2237 this_urb->dev = serial->dev;
2238 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2239 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2244 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2246 struct usb_serial *serial = port->serial;
2247 struct keyspan_serial_private *s_priv;
2248 const struct keyspan_device_details *d_details;
2250 dbg ("%s", __FUNCTION__);
2252 s_priv = usb_get_serial_data(serial);
2253 d_details = s_priv->device_details;
2255 switch (d_details->msg_format) {
2257 keyspan_usa26_send_setup(serial, port, reset_port);
2260 keyspan_usa28_send_setup(serial, port, reset_port);
2263 keyspan_usa49_send_setup(serial, port, reset_port);
2266 keyspan_usa90_send_setup(serial, port, reset_port);
2272 /* Gets called by the "real" driver (ie once firmware is loaded
2273 and renumeration has taken place. */
2274 static int keyspan_startup (struct usb_serial *serial)
2277 struct usb_serial_port *port;
2278 struct keyspan_serial_private *s_priv;
2279 struct keyspan_port_private *p_priv;
2280 const struct keyspan_device_details *d_details;
2282 dbg("%s", __FUNCTION__);
2284 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2285 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2287 if (d_details == NULL) {
2288 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2292 /* Setup private data for serial driver */
2293 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2295 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2299 s_priv->device_details = d_details;
2300 usb_set_serial_data(serial, s_priv);
2302 /* Now setup per port private data */
2303 for (i = 0; i < serial->num_ports; i++) {
2304 port = serial->port[i];
2305 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2307 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2310 p_priv->device_details = d_details;
2311 usb_set_serial_port_data(port, p_priv);
2314 keyspan_setup_urbs(serial);
2316 s_priv->instat_urb->dev = serial->dev;
2317 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2318 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2324 static void keyspan_shutdown (struct usb_serial *serial)
2327 struct usb_serial_port *port;
2328 struct keyspan_serial_private *s_priv;
2329 struct keyspan_port_private *p_priv;
2331 dbg("%s", __FUNCTION__);
2333 s_priv = usb_get_serial_data(serial);
2335 /* Stop reading/writing urbs */
2336 stop_urb(s_priv->instat_urb);
2337 stop_urb(s_priv->glocont_urb);
2338 for (i = 0; i < serial->num_ports; ++i) {
2339 port = serial->port[i];
2340 p_priv = usb_get_serial_port_data(port);
2341 stop_urb(p_priv->inack_urb);
2342 stop_urb(p_priv->outcont_urb);
2343 for (j = 0; j < 2; j++) {
2344 stop_urb(p_priv->in_urbs[j]);
2345 stop_urb(p_priv->out_urbs[j]);
2350 usb_free_urb(s_priv->instat_urb);
2351 usb_free_urb(s_priv->glocont_urb);
2352 for (i = 0; i < serial->num_ports; ++i) {
2353 port = serial->port[i];
2354 p_priv = usb_get_serial_port_data(port);
2355 usb_free_urb(p_priv->inack_urb);
2356 usb_free_urb(p_priv->outcont_urb);
2357 for (j = 0; j < 2; j++) {
2358 usb_free_urb(p_priv->in_urbs[j]);
2359 usb_free_urb(p_priv->out_urbs[j]);
2363 /* dbg("Freeing serial->private."); */
2366 /* dbg("Freeing port->private."); */
2367 /* Now free per port private data */
2368 for (i = 0; i < serial->num_ports; i++) {
2369 port = serial->port[i];
2370 kfree(usb_get_serial_port_data(port));
2374 MODULE_AUTHOR( DRIVER_AUTHOR );
2375 MODULE_DESCRIPTION( DRIVER_DESC );
2376 MODULE_LICENSE("GPL");
2378 module_param(debug, bool, S_IRUGO | S_IWUSR);
2379 MODULE_PARM_DESC(debug, "Debug enabled or not");