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 /* number of active ports */
137 atomic_t active_count;
139 const struct keyspan_device_details *device_details;
141 struct urb *instat_urb;
142 char instat_buf[INSTAT_BUFLEN];
144 /* XXX this one probably will need a lock */
145 struct urb *glocont_urb;
146 char glocont_buf[GLOCONT_BUFLEN];
149 struct keyspan_port_private {
150 /* Keep track of which input & output endpoints to use */
154 /* Keep duplicate of device details in each port
155 structure as well - simplifies some of the
156 callback functions etc. */
157 const struct keyspan_device_details *device_details;
159 /* Input endpoints and buffer for this port */
160 struct urb *in_urbs[2];
161 char in_buffer[2][64];
162 /* Output endpoints and buffer for this port */
163 struct urb *out_urbs[2];
164 char out_buffer[2][64];
166 /* Input ack endpoint */
167 struct urb *inack_urb;
168 char inack_buffer[1];
170 /* Output control endpoint */
171 struct urb *outcont_urb;
172 char outcont_buffer[64];
174 /* Settings for the port */
178 unsigned int old_cflag;
179 enum {flow_none, flow_cts, flow_xon} flow_control;
180 int rts_state; /* Handshaking pins (outputs) */
182 int cts_state; /* Handshaking pins (inputs) */
188 unsigned long tx_start_time[2];
189 int resend_cont; /* need to resend control packet */
193 /* Include Keyspan message headers. All current Keyspan Adapters
194 make use of one of four message formats which are referred
195 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
196 #include "keyspan_usa26msg.h"
197 #include "keyspan_usa28msg.h"
198 #include "keyspan_usa49msg.h"
199 #include "keyspan_usa90msg.h"
202 /* Functions used by new usb-serial code. */
203 static int __init keyspan_init (void)
206 retval = usb_serial_register(&keyspan_pre_device);
208 goto failed_pre_device_register;
209 retval = usb_serial_register(&keyspan_1port_device);
211 goto failed_1port_device_register;
212 retval = usb_serial_register(&keyspan_2port_device);
214 goto failed_2port_device_register;
215 retval = usb_serial_register(&keyspan_4port_device);
217 goto failed_4port_device_register;
218 retval = usb_register(&keyspan_driver);
220 goto failed_usb_register;
222 info(DRIVER_VERSION ":" DRIVER_DESC);
226 usb_serial_deregister(&keyspan_4port_device);
227 failed_4port_device_register:
228 usb_serial_deregister(&keyspan_2port_device);
229 failed_2port_device_register:
230 usb_serial_deregister(&keyspan_1port_device);
231 failed_1port_device_register:
232 usb_serial_deregister(&keyspan_pre_device);
233 failed_pre_device_register:
237 static void __exit keyspan_exit (void)
239 usb_deregister (&keyspan_driver);
240 usb_serial_deregister (&keyspan_pre_device);
241 usb_serial_deregister (&keyspan_1port_device);
242 usb_serial_deregister (&keyspan_2port_device);
243 usb_serial_deregister (&keyspan_4port_device);
246 module_init(keyspan_init);
247 module_exit(keyspan_exit);
249 static void keyspan_rx_throttle (struct usb_serial_port *port)
251 dbg("%s - port %d", __FUNCTION__, port->number);
255 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
257 dbg("%s - port %d", __FUNCTION__, port->number);
261 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
263 struct keyspan_port_private *p_priv;
265 dbg("%s", __FUNCTION__);
267 p_priv = usb_get_serial_port_data(port);
269 if (break_state == -1)
270 p_priv->break_on = 1;
272 p_priv->break_on = 0;
274 keyspan_send_setup(port, 0);
278 static void keyspan_set_termios (struct usb_serial_port *port,
279 struct termios *old_termios)
281 int baud_rate, device_port;
282 struct keyspan_port_private *p_priv;
283 const struct keyspan_device_details *d_details;
286 dbg("%s", __FUNCTION__);
288 p_priv = usb_get_serial_port_data(port);
289 d_details = p_priv->device_details;
290 cflag = port->tty->termios->c_cflag;
291 device_port = port->number - port->serial->minor;
293 /* Baud rate calculation takes baud rate as an integer
294 so other rates can be generated if desired. */
295 baud_rate = tty_get_baud_rate(port->tty);
296 /* If no match or invalid, don't change */
298 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
299 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
300 /* FIXME - more to do here to ensure rate changes cleanly */
301 p_priv->baud = baud_rate;
304 /* set CTS/RTS handshake etc. */
305 p_priv->cflag = cflag;
306 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
308 keyspan_send_setup(port, 0);
311 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
314 struct keyspan_port_private *p_priv;
316 p_priv = usb_get_serial_port_data(port);
318 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
319 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
320 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
321 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
322 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
323 ((p_priv->ri_state) ? TIOCM_RNG : 0);
328 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
329 unsigned int set, unsigned int clear)
331 struct keyspan_port_private *p_priv;
333 p_priv = usb_get_serial_port_data(port);
336 p_priv->rts_state = 1;
338 p_priv->dtr_state = 1;
340 if (clear & TIOCM_RTS)
341 p_priv->rts_state = 0;
342 if (clear & TIOCM_DTR)
343 p_priv->dtr_state = 0;
344 keyspan_send_setup(port, 0);
348 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
349 unsigned int cmd, unsigned long arg)
354 /* Write function is similar for the four protocols used
355 with only a minor change for usa90 (usa19hs) required */
356 static int keyspan_write(struct usb_serial_port *port, int from_user,
357 const unsigned char *buf, int count)
359 struct keyspan_port_private *p_priv;
360 const struct keyspan_device_details *d_details;
363 struct urb *this_urb;
364 int err, maxDataLen, dataOffset;
366 p_priv = usb_get_serial_port_data(port);
367 d_details = p_priv->device_details;
369 if (d_details->msg_format == msg_usa90) {
377 dbg("%s - for port %d (%d chars), flip=%d",
378 __FUNCTION__, port->number, count, p_priv->out_flip);
380 for (left = count; left > 0; left -= todo) {
382 if (todo > maxDataLen)
385 flip = p_priv->out_flip;
387 /* Check we have a valid urb/endpoint before we use it... */
388 if ((this_urb = p_priv->out_urbs[flip]) == 0) {
389 /* no bulk out, so return 0 bytes written */
390 dbg("%s - no output urb :(", __FUNCTION__);
394 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
396 if (this_urb->status == -EINPROGRESS) {
397 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
399 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
401 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
402 usb_unlink_urb(this_urb);
406 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
407 for now so set to zero */
408 ((char *)this_urb->transfer_buffer)[0] = 0;
411 if (copy_from_user(this_urb->transfer_buffer + dataOffset, buf, todo))
414 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
418 /* send the data out the bulk port */
419 this_urb->transfer_buffer_length = todo + dataOffset;
421 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
422 this_urb->dev = port->serial->dev;
423 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
424 dbg("usb_submit_urb(write bulk) failed (%d)", err);
426 p_priv->tx_start_time[flip] = jiffies;
428 /* Flip for next time if usa26 or usa28 interface
429 (not used on usa49) */
430 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
436 static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
440 struct usb_serial_port *port;
441 struct tty_struct *tty;
442 unsigned char *data = urb->transfer_buffer;
444 dbg ("%s", __FUNCTION__);
446 endpoint = usb_pipeendpoint(urb->pipe);
449 dbg("%s - nonzero status: %x on endpoint %d.",
450 __FUNCTION__, urb->status, endpoint);
454 port = (struct usb_serial_port *) urb->context;
456 if (urb->actual_length) {
457 /* 0x80 bit is error flag */
458 if ((data[0] & 0x80) == 0) {
459 /* no errors on individual bytes, only possible overrun err*/
460 if (data[0] & RXERROR_OVERRUN)
463 for (i = 1; i < urb->actual_length ; ++i) {
464 tty_insert_flip_char(tty, data[i], err);
467 /* some bytes had errors, every byte has status */
468 dbg("%s - RX error!!!!", __FUNCTION__);
469 for (i = 0; i + 1 < urb->actual_length; i += 2) {
470 int stat = data[i], flag = 0;
471 if (stat & RXERROR_OVERRUN)
473 if (stat & RXERROR_FRAMING)
475 if (stat & RXERROR_PARITY)
477 /* XXX should handle break (0x10) */
478 tty_insert_flip_char(tty, data[i+1], flag);
481 tty_flip_buffer_push(tty);
484 /* Resubmit urb so we continue receiving */
485 urb->dev = port->serial->dev;
486 if (port->open_count)
487 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
488 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
493 /* Outdat handling is common for all devices */
494 static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
496 struct usb_serial_port *port;
497 struct keyspan_port_private *p_priv;
499 port = (struct usb_serial_port *) urb->context;
500 p_priv = usb_get_serial_port_data(port);
501 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
503 if (port->open_count)
504 schedule_work(&port->work);
507 static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
509 dbg ("%s", __FUNCTION__);
513 static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
515 struct usb_serial_port *port;
516 struct keyspan_port_private *p_priv;
518 port = (struct usb_serial_port *) urb->context;
519 p_priv = usb_get_serial_port_data(port);
521 if (p_priv->resend_cont) {
522 dbg ("%s - sending setup", __FUNCTION__);
523 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
527 static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
529 unsigned char *data = urb->transfer_buffer;
530 struct keyspan_usa26_portStatusMessage *msg;
531 struct usb_serial *serial;
532 struct usb_serial_port *port;
533 struct keyspan_port_private *p_priv;
534 int old_dcd_state, err;
536 serial = (struct usb_serial *) urb->context;
539 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
542 if (urb->actual_length != 9) {
543 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
547 msg = (struct keyspan_usa26_portStatusMessage *)data;
550 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
551 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
552 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
555 /* Now do something useful with the data */
558 /* Check port number from message and retrieve private data */
559 if (msg->port >= serial->num_ports) {
560 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
563 port = serial->port[msg->port];
564 p_priv = usb_get_serial_port_data(port);
566 /* Update handshaking pin state information */
567 old_dcd_state = p_priv->dcd_state;
568 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
569 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
570 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
571 p_priv->ri_state = ((msg->ri) ? 1 : 0);
573 if (port->tty && !C_CLOCAL(port->tty)
574 && old_dcd_state != p_priv->dcd_state) {
576 tty_hangup(port->tty);
578 /* wake_up_interruptible(&p_priv->open_wait); */
581 /* Resubmit urb so we continue receiving */
582 urb->dev = serial->dev;
583 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
584 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
589 static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
591 dbg ("%s", __FUNCTION__);
596 static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
599 struct usb_serial_port *port;
600 struct tty_struct *tty;
602 struct keyspan_port_private *p_priv;
604 dbg ("%s", __FUNCTION__);
606 port = (struct usb_serial_port *) urb->context;
607 p_priv = usb_get_serial_port_data(port);
608 data = urb->transfer_buffer;
610 if (urb != p_priv->in_urbs[p_priv->in_flip])
615 dbg("%s - nonzero status: %x on endpoint %d.",
616 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
620 port = (struct usb_serial_port *) urb->context;
621 p_priv = usb_get_serial_port_data(port);
622 data = urb->transfer_buffer;
625 if (urb->actual_length) {
626 for (i = 0; i < urb->actual_length ; ++i) {
627 tty_insert_flip_char(tty, data[i], 0);
629 tty_flip_buffer_push(tty);
632 /* Resubmit urb so we continue receiving */
633 urb->dev = port->serial->dev;
634 if (port->open_count)
635 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
636 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
638 p_priv->in_flip ^= 1;
640 urb = p_priv->in_urbs[p_priv->in_flip];
641 } while (urb->status != -EINPROGRESS);
644 static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
646 dbg ("%s", __FUNCTION__);
649 static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
651 struct usb_serial_port *port;
652 struct keyspan_port_private *p_priv;
654 port = (struct usb_serial_port *) urb->context;
655 p_priv = usb_get_serial_port_data(port);
657 if (p_priv->resend_cont) {
658 dbg ("%s - sending setup", __FUNCTION__);
659 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
663 static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
666 unsigned char *data = urb->transfer_buffer;
667 struct keyspan_usa28_portStatusMessage *msg;
668 struct usb_serial *serial;
669 struct usb_serial_port *port;
670 struct keyspan_port_private *p_priv;
673 serial = (struct usb_serial *) urb->context;
676 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
680 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
681 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
685 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
686 data[0], data[1], data[2], data[3], data[4], data[5],
687 data[6], data[7], data[8], data[9], data[10], data[11]);*/
689 /* Now do something useful with the data */
690 msg = (struct keyspan_usa28_portStatusMessage *)data;
693 /* Check port number from message and retrieve private data */
694 if (msg->port >= serial->num_ports) {
695 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
698 port = serial->port[msg->port];
699 p_priv = usb_get_serial_port_data(port);
701 /* Update handshaking pin state information */
702 old_dcd_state = p_priv->dcd_state;
703 p_priv->cts_state = ((msg->cts) ? 1 : 0);
704 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
705 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
706 p_priv->ri_state = ((msg->ri) ? 1 : 0);
708 if (port->tty && !C_CLOCAL(port->tty)
709 && old_dcd_state != p_priv->dcd_state) {
711 tty_hangup(port->tty);
713 /* wake_up_interruptible(&p_priv->open_wait); */
716 /* Resubmit urb so we continue receiving */
717 urb->dev = serial->dev;
718 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
719 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
724 static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
726 dbg ("%s", __FUNCTION__);
730 static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
732 struct usb_serial *serial;
733 struct usb_serial_port *port;
734 struct keyspan_port_private *p_priv;
737 dbg ("%s", __FUNCTION__);
739 serial = (struct usb_serial *) urb->context;
740 for (i = 0; i < serial->num_ports; ++i) {
741 port = serial->port[i];
742 p_priv = usb_get_serial_port_data(port);
744 if (p_priv->resend_cont) {
745 dbg ("%s - sending setup", __FUNCTION__);
746 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
752 /* This is actually called glostat in the Keyspan
754 static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
757 unsigned char *data = urb->transfer_buffer;
758 struct keyspan_usa49_portStatusMessage *msg;
759 struct usb_serial *serial;
760 struct usb_serial_port *port;
761 struct keyspan_port_private *p_priv;
764 dbg ("%s", __FUNCTION__);
766 serial = (struct usb_serial *) urb->context;
769 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
773 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
774 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
778 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
779 data[0], data[1], data[2], data[3], data[4], data[5],
780 data[6], data[7], data[8], data[9], data[10]);*/
782 /* Now do something useful with the data */
783 msg = (struct keyspan_usa49_portStatusMessage *)data;
785 /* Check port number from message and retrieve private data */
786 if (msg->portNumber >= serial->num_ports) {
787 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
790 port = serial->port[msg->portNumber];
791 p_priv = usb_get_serial_port_data(port);
793 /* Update handshaking pin state information */
794 old_dcd_state = p_priv->dcd_state;
795 p_priv->cts_state = ((msg->cts) ? 1 : 0);
796 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
797 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
798 p_priv->ri_state = ((msg->ri) ? 1 : 0);
800 if (port->tty && !C_CLOCAL(port->tty)
801 && old_dcd_state != p_priv->dcd_state) {
803 tty_hangup(port->tty);
805 /* wake_up_interruptible(&p_priv->open_wait); */
808 /* Resubmit urb so we continue receiving */
809 urb->dev = serial->dev;
811 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
812 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
817 static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
819 dbg ("%s", __FUNCTION__);
822 static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
826 struct usb_serial_port *port;
827 struct tty_struct *tty;
828 unsigned char *data = urb->transfer_buffer;
830 dbg ("%s", __FUNCTION__);
832 endpoint = usb_pipeendpoint(urb->pipe);
835 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
836 urb->status, endpoint);
840 port = (struct usb_serial_port *) urb->context;
842 if (urb->actual_length) {
843 /* 0x80 bit is error flag */
844 if ((data[0] & 0x80) == 0) {
845 /* no error on any byte */
846 for (i = 1; i < urb->actual_length ; ++i) {
847 tty_insert_flip_char(tty, data[i], 0);
850 /* some bytes had errors, every byte has status */
851 for (i = 0; i + 1 < urb->actual_length; i += 2) {
852 int stat = data[i], flag = 0;
853 if (stat & RXERROR_OVERRUN)
855 if (stat & RXERROR_FRAMING)
857 if (stat & RXERROR_PARITY)
859 /* XXX should handle break (0x10) */
860 tty_insert_flip_char(tty, data[i+1], flag);
863 tty_flip_buffer_push(tty);
866 /* Resubmit urb so we continue receiving */
867 urb->dev = port->serial->dev;
868 if (port->open_count)
869 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
870 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
874 /* not used, usa-49 doesn't have per-port control endpoints */
875 static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
877 dbg ("%s", __FUNCTION__);
880 static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
884 struct usb_serial_port *port;
885 struct keyspan_port_private *p_priv;
886 struct tty_struct *tty;
887 unsigned char *data = urb->transfer_buffer;
889 dbg ("%s", __FUNCTION__);
891 endpoint = usb_pipeendpoint(urb->pipe);
895 dbg("%s - nonzero status: %x on endpoint %d.",
896 __FUNCTION__, urb->status, endpoint);
900 port = (struct usb_serial_port *) urb->context;
901 p_priv = usb_get_serial_port_data(port);
904 if (urb->actual_length) {
906 /* if current mode is DMA, looks like usa28 format
907 otherwise looks like usa26 data format */
909 if (p_priv->baud > 57600) {
910 for (i = 0; i < urb->actual_length ; ++i)
911 tty_insert_flip_char(tty, data[i], 0);
915 /* 0x80 bit is error flag */
916 if ((data[0] & 0x80) == 0) {
917 /* no errors on individual bytes, only possible overrun err*/
918 if (data[0] & RXERROR_OVERRUN)
921 for (i = 1; i < urb->actual_length ; ++i)
922 tty_insert_flip_char(tty, data[i], err);
926 /* some bytes had errors, every byte has status */
927 dbg("%s - RX error!!!!", __FUNCTION__);
928 for (i = 0; i + 1 < urb->actual_length; i += 2) {
929 int stat = data[i], flag = 0;
930 if (stat & RXERROR_OVERRUN)
932 if (stat & RXERROR_FRAMING)
934 if (stat & RXERROR_PARITY)
936 /* XXX should handle break (0x10) */
937 tty_insert_flip_char(tty, data[i+1], flag);
941 tty_flip_buffer_push(tty);
944 /* Resubmit urb so we continue receiving */
945 urb->dev = port->serial->dev;
946 if (port->open_count)
947 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
948 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
954 static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
956 unsigned char *data = urb->transfer_buffer;
957 struct keyspan_usa90_portStatusMessage *msg;
958 struct usb_serial *serial;
959 struct usb_serial_port *port;
960 struct keyspan_port_private *p_priv;
961 int old_dcd_state, err;
963 serial = (struct usb_serial *) urb->context;
966 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
969 if (urb->actual_length < 14) {
970 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
974 msg = (struct keyspan_usa90_portStatusMessage *)data;
976 /* Now do something useful with the data */
978 port = serial->port[0];
979 p_priv = usb_get_serial_port_data(port);
981 /* Update handshaking pin state information */
982 old_dcd_state = p_priv->dcd_state;
983 p_priv->cts_state = ((msg->cts) ? 1 : 0);
984 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
985 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
986 p_priv->ri_state = ((msg->ri) ? 1 : 0);
988 if (port->tty && !C_CLOCAL(port->tty)
989 && old_dcd_state != p_priv->dcd_state) {
991 tty_hangup(port->tty);
993 /* wake_up_interruptible(&p_priv->open_wait); */
996 /* Resubmit urb so we continue receiving */
997 urb->dev = serial->dev;
998 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
999 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1005 static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
1007 struct usb_serial_port *port;
1008 struct keyspan_port_private *p_priv;
1010 port = (struct usb_serial_port *) urb->context;
1011 p_priv = usb_get_serial_port_data(port);
1013 if (p_priv->resend_cont) {
1014 dbg ("%s - sending setup", __FUNCTION__);
1015 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1019 static int keyspan_write_room (struct usb_serial_port *port)
1021 struct keyspan_port_private *p_priv;
1022 const struct keyspan_device_details *d_details;
1025 struct urb *this_urb;
1027 dbg("%s", __FUNCTION__);
1028 p_priv = usb_get_serial_port_data(port);
1029 d_details = p_priv->device_details;
1031 if (d_details->msg_format == msg_usa90)
1036 flip = p_priv->out_flip;
1038 /* Check both endpoints to see if any are available. */
1039 if ((this_urb = p_priv->out_urbs[flip]) != 0) {
1040 if (this_urb->status != -EINPROGRESS)
1042 flip = (flip + 1) & d_details->outdat_endp_flip;
1043 if ((this_urb = p_priv->out_urbs[flip]) != 0)
1044 if (this_urb->status != -EINPROGRESS)
1051 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1057 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1059 struct keyspan_port_private *p_priv;
1060 struct keyspan_serial_private *s_priv;
1061 struct usb_serial *serial = port->serial;
1062 const struct keyspan_device_details *d_details;
1064 int baud_rate, device_port;
1068 s_priv = usb_get_serial_data(serial);
1069 p_priv = usb_get_serial_port_data(port);
1070 d_details = p_priv->device_details;
1072 dbg("%s - port%d.", __FUNCTION__, port->number);
1074 /* Set some sane defaults */
1075 p_priv->rts_state = 1;
1076 p_priv->dtr_state = 1;
1077 p_priv->baud = 9600;
1079 /* force baud and lcr to be set on open */
1080 p_priv->old_baud = 0;
1081 p_priv->old_cflag = 0;
1083 p_priv->out_flip = 0;
1084 p_priv->in_flip = 0;
1086 /* Reset low level data toggle and start reading from endpoints */
1087 for (i = 0; i < 2; i++) {
1088 if ((urb = p_priv->in_urbs[i]) == NULL)
1090 urb->dev = serial->dev;
1092 /* make sure endpoint data toggle is synchronized with the device */
1094 usb_clear_halt(urb->dev, urb->pipe);
1096 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1097 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1101 /* Reset low level data toggle on out endpoints */
1102 for (i = 0; i < 2; i++) {
1103 if ((urb = p_priv->out_urbs[i]) == NULL)
1105 urb->dev = serial->dev;
1106 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1109 /* get the terminal config for the setup message now so we don't
1110 * need to send 2 of them */
1112 cflag = port->tty->termios->c_cflag;
1113 device_port = port->number - port->serial->minor;
1115 /* Baud rate calculation takes baud rate as an integer
1116 so other rates can be generated if desired. */
1117 baud_rate = tty_get_baud_rate(port->tty);
1118 /* If no match or invalid, leave as default */
1120 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1121 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1122 p_priv->baud = baud_rate;
1125 /* set CTS/RTS handshake etc. */
1126 p_priv->cflag = cflag;
1127 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1129 keyspan_send_setup(port, 1);
1131 //keyspan_set_termios(port, NULL);
1136 static inline void stop_urb(struct urb *urb)
1138 if (urb && urb->status == -EINPROGRESS) {
1139 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1140 usb_unlink_urb(urb);
1144 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1147 struct usb_serial *serial;
1148 struct keyspan_serial_private *s_priv;
1149 struct keyspan_port_private *p_priv;
1151 serial = get_usb_serial (port, __FUNCTION__);
1155 dbg("%s", __FUNCTION__);
1156 s_priv = usb_get_serial_data(serial);
1157 p_priv = usb_get_serial_port_data(port);
1159 p_priv->rts_state = 0;
1160 p_priv->dtr_state = 0;
1163 keyspan_send_setup(port, 2);
1164 /* pilot-xfer seems to work best with this delay */
1166 // keyspan_set_termios(port, NULL);
1169 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1170 dbg("%s - urb in progress", __FUNCTION__);
1173 p_priv->out_flip = 0;
1174 p_priv->in_flip = 0;
1177 /* Stop reading/writing urbs */
1178 stop_urb(p_priv->inack_urb);
1179 /* stop_urb(p_priv->outcont_urb); */
1180 for (i = 0; i < 2; i++) {
1181 stop_urb(p_priv->in_urbs[i]);
1182 stop_urb(p_priv->out_urbs[i]);
1189 /* download the firmware to a pre-renumeration device */
1190 static int keyspan_fake_startup (struct usb_serial *serial)
1193 const struct ezusb_hex_record *record;
1196 dbg("Keyspan startup version %04x product %04x",
1197 serial->dev->descriptor.bcdDevice,
1198 serial->dev->descriptor.idProduct);
1200 if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
1201 dbg("Firmware already loaded. Quitting.");
1205 /* Select firmware image on the basis of idProduct */
1206 switch (serial->dev->descriptor.idProduct) {
1207 case keyspan_usa28_pre_product_id:
1208 record = &keyspan_usa28_firmware[0];
1212 case keyspan_usa28x_pre_product_id:
1213 record = &keyspan_usa28x_firmware[0];
1217 case keyspan_usa28xa_pre_product_id:
1218 record = &keyspan_usa28xa_firmware[0];
1219 fw_name = "USA28XA";
1222 case keyspan_usa28xb_pre_product_id:
1223 record = &keyspan_usa28xb_firmware[0];
1224 fw_name = "USA28XB";
1227 case keyspan_usa19_pre_product_id:
1228 record = &keyspan_usa19_firmware[0];
1232 case keyspan_usa19qi_pre_product_id:
1233 record = &keyspan_usa19qi_firmware[0];
1234 fw_name = "USA19QI";
1237 case keyspan_mpr_pre_product_id:
1238 record = &keyspan_mpr_firmware[0];
1242 case keyspan_usa19qw_pre_product_id:
1243 record = &keyspan_usa19qw_firmware[0];
1244 fw_name = "USA19QI";
1247 case keyspan_usa18x_pre_product_id:
1248 record = &keyspan_usa18x_firmware[0];
1252 case keyspan_usa19w_pre_product_id:
1253 record = &keyspan_usa19w_firmware[0];
1257 case keyspan_usa49w_pre_product_id:
1258 record = &keyspan_usa49w_firmware[0];
1262 case keyspan_usa49wlc_pre_product_id:
1263 record = &keyspan_usa49wlc_firmware[0];
1264 fw_name = "USA49WLC";
1269 fw_name = "Unknown";
1273 if (record == NULL) {
1274 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1278 dbg("Uploading Keyspan %s firmware.", fw_name);
1280 /* download the firmware image */
1281 response = ezusb_set_reset(serial, 1);
1283 while(record->address != 0xffff) {
1284 response = ezusb_writememory(serial, record->address,
1285 (unsigned char *)record->data,
1286 record->data_size, 0xa0);
1288 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1289 "firmware (%d %04X %p %d)\n",
1291 record->address, record->data, record->data_size);
1296 /* bring device out of reset. Renumeration will occur in a
1297 moment and the new device will bind to the real driver */
1298 response = ezusb_set_reset(serial, 0);
1300 /* we don't want this device to have a driver assigned to it. */
1304 /* Helper functions used by keyspan_setup_urbs */
1305 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1306 int dir, void *ctx, char *buf, int len,
1307 void (*callback)(struct urb *, struct pt_regs *regs))
1312 return NULL; /* endpoint not needed */
1314 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1315 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1317 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1321 /* Fill URB using supplied data. */
1322 usb_fill_bulk_urb(urb, serial->dev,
1323 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1324 buf, len, callback, ctx);
1329 static struct callbacks {
1330 void (*instat_callback)(struct urb *, struct pt_regs *regs);
1331 void (*glocont_callback)(struct urb *, struct pt_regs *regs);
1332 void (*indat_callback)(struct urb *, struct pt_regs *regs);
1333 void (*outdat_callback)(struct urb *, struct pt_regs *regs);
1334 void (*inack_callback)(struct urb *, struct pt_regs *regs);
1335 void (*outcont_callback)(struct urb *, struct pt_regs *regs);
1336 } keyspan_callbacks[] = {
1338 /* msg_usa26 callbacks */
1339 .instat_callback = usa26_instat_callback,
1340 .glocont_callback = usa26_glocont_callback,
1341 .indat_callback = usa26_indat_callback,
1342 .outdat_callback = usa2x_outdat_callback,
1343 .inack_callback = usa26_inack_callback,
1344 .outcont_callback = usa26_outcont_callback,
1346 /* msg_usa28 callbacks */
1347 .instat_callback = usa28_instat_callback,
1348 .glocont_callback = usa28_glocont_callback,
1349 .indat_callback = usa28_indat_callback,
1350 .outdat_callback = usa2x_outdat_callback,
1351 .inack_callback = usa28_inack_callback,
1352 .outcont_callback = usa28_outcont_callback,
1354 /* msg_usa49 callbacks */
1355 .instat_callback = usa49_instat_callback,
1356 .glocont_callback = usa49_glocont_callback,
1357 .indat_callback = usa49_indat_callback,
1358 .outdat_callback = usa2x_outdat_callback,
1359 .inack_callback = usa49_inack_callback,
1360 .outcont_callback = usa49_outcont_callback,
1362 /* msg_usa90 callbacks */
1363 .instat_callback = usa90_instat_callback,
1364 .glocont_callback = usa28_glocont_callback,
1365 .indat_callback = usa90_indat_callback,
1366 .outdat_callback = usa2x_outdat_callback,
1367 .inack_callback = usa28_inack_callback,
1368 .outcont_callback = usa90_outcont_callback,
1372 /* Generic setup urbs function that uses
1373 data in device_details */
1374 static void keyspan_setup_urbs(struct usb_serial *serial)
1377 struct keyspan_serial_private *s_priv;
1378 const struct keyspan_device_details *d_details;
1379 struct usb_serial_port *port;
1380 struct keyspan_port_private *p_priv;
1381 struct callbacks *cback;
1384 dbg ("%s", __FUNCTION__);
1386 s_priv = usb_get_serial_data(serial);
1387 d_details = s_priv->device_details;
1389 /* Setup values for the various callback routines */
1390 cback = &keyspan_callbacks[d_details->msg_format];
1392 /* Allocate and set up urbs for each one that is in use,
1393 starting with instat endpoints */
1394 s_priv->instat_urb = keyspan_setup_urb
1395 (serial, d_details->instat_endpoint, USB_DIR_IN,
1396 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1397 cback->instat_callback);
1399 s_priv->glocont_urb = keyspan_setup_urb
1400 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1401 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1402 cback->glocont_callback);
1404 /* Setup endpoints for each port specific thing */
1405 for (i = 0; i < d_details->num_ports; i ++) {
1406 port = serial->port[i];
1407 p_priv = usb_get_serial_port_data(port);
1409 /* Do indat endpoints first, once for each flip */
1410 endp = d_details->indat_endpoints[i];
1411 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1412 p_priv->in_urbs[j] = keyspan_setup_urb
1413 (serial, endp, USB_DIR_IN, port,
1414 p_priv->in_buffer[j], 64,
1415 cback->indat_callback);
1418 p_priv->in_urbs[j] = NULL;
1420 /* outdat endpoints also have flip */
1421 endp = d_details->outdat_endpoints[i];
1422 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1423 p_priv->out_urbs[j] = keyspan_setup_urb
1424 (serial, endp, USB_DIR_OUT, port,
1425 p_priv->out_buffer[j], 64,
1426 cback->outdat_callback);
1429 p_priv->out_urbs[j] = NULL;
1431 /* inack endpoint */
1432 p_priv->inack_urb = keyspan_setup_urb
1433 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1434 port, p_priv->inack_buffer, 1, cback->inack_callback);
1436 /* outcont endpoint */
1437 p_priv->outcont_urb = keyspan_setup_urb
1438 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1439 port, p_priv->outcont_buffer, 64,
1440 cback->outcont_callback);
1445 /* usa19 function doesn't require prescaler */
1446 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1447 u8 *rate_low, u8 *prescaler, int portnum)
1449 u32 b16, /* baud rate times 16 (actual rate used internally) */
1451 cnt; /* inverse of divisor (programmed into 8051) */
1453 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1455 /* prevent divide by zero... */
1456 if( (b16 = (baud_rate * 16L)) == 0) {
1457 return (KEYSPAN_INVALID_BAUD_RATE);
1460 /* Any "standard" rate over 57k6 is marginal on the USA-19
1461 as we run out of divisor resolution. */
1462 if (baud_rate > 57600) {
1463 return (KEYSPAN_INVALID_BAUD_RATE);
1466 /* calculate the divisor and the counter (its inverse) */
1467 if( (div = (baudclk / b16)) == 0) {
1468 return (KEYSPAN_INVALID_BAUD_RATE);
1475 return (KEYSPAN_INVALID_BAUD_RATE);
1478 /* return the counter values if non-null */
1480 *rate_low = (u8) (cnt & 0xff);
1483 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1485 if (rate_low && rate_hi) {
1486 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1489 return (KEYSPAN_BAUD_RATE_OK);
1492 /* usa19hs function doesn't require prescaler */
1493 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1494 u8 *rate_low, u8 *prescaler, int portnum)
1496 u32 b16, /* baud rate times 16 (actual rate used internally) */
1499 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1501 /* prevent divide by zero... */
1502 if( (b16 = (baud_rate * 16L)) == 0)
1503 return (KEYSPAN_INVALID_BAUD_RATE);
1507 /* calculate the divisor */
1508 if( (div = (baudclk / b16)) == 0)
1509 return (KEYSPAN_INVALID_BAUD_RATE);
1512 return (KEYSPAN_INVALID_BAUD_RATE);
1514 /* return the counter values if non-null */
1516 *rate_low = (u8) (div & 0xff);
1519 *rate_hi = (u8) ((div >> 8) & 0xff);
1521 if (rate_low && rate_hi)
1522 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1524 return (KEYSPAN_BAUD_RATE_OK);
1527 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1528 u8 *rate_low, u8 *prescaler, int portnum)
1530 u32 b16, /* baud rate times 16 (actual rate used internally) */
1531 clk, /* clock with 13/8 prescaler */
1532 div, /* divisor using 13/8 prescaler */
1533 res, /* resulting baud rate using 13/8 prescaler */
1534 diff, /* error using 13/8 prescaler */
1539 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1541 /* prevent divide by zero */
1542 if( (b16 = baud_rate * 16L) == 0) {
1543 return (KEYSPAN_INVALID_BAUD_RATE);
1546 /* Calculate prescaler by trying them all and looking
1549 /* start with largest possible difference */
1550 smallest_diff = 0xffffffff;
1552 /* 0 is an invalid prescaler, used as a flag */
1555 for(i = 8; i <= 0xff; ++i) {
1556 clk = (baudclk * 8) / (u32) i;
1558 if( (div = clk / b16) == 0) {
1563 diff= (res > b16) ? (res-b16) : (b16-res);
1565 if(diff < smallest_diff) {
1567 smallest_diff = diff;
1571 if(best_prescaler == 0) {
1572 return (KEYSPAN_INVALID_BAUD_RATE);
1575 clk = (baudclk * 8) / (u32) best_prescaler;
1578 /* return the divisor and prescaler if non-null */
1580 *rate_low = (u8) (div & 0xff);
1583 *rate_hi = (u8) ((div >> 8) & 0xff);
1586 *prescaler = best_prescaler;
1587 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1589 return (KEYSPAN_BAUD_RATE_OK);
1592 /* USA-28 supports different maximum baud rates on each port */
1593 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1594 u8 *rate_low, u8 *prescaler, int portnum)
1596 u32 b16, /* baud rate times 16 (actual rate used internally) */
1598 cnt; /* inverse of divisor (programmed into 8051) */
1600 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1602 /* prevent divide by zero */
1603 if ((b16 = baud_rate * 16L) == 0)
1604 return (KEYSPAN_INVALID_BAUD_RATE);
1606 /* calculate the divisor and the counter (its inverse) */
1607 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1608 return (KEYSPAN_INVALID_BAUD_RATE);
1614 /* check for out of range, based on portnum,
1615 and return result */
1618 return (KEYSPAN_INVALID_BAUD_RATE);
1623 return (KEYSPAN_INVALID_BAUD_RATE);
1627 return (KEYSPAN_INVALID_BAUD_RATE);
1631 /* return the counter values if not NULL
1632 (port 1 will ignore retHi) */
1634 *rate_low = (u8) (cnt & 0xff);
1637 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1639 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1640 return (KEYSPAN_BAUD_RATE_OK);
1643 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1644 struct usb_serial_port *port,
1647 struct keyspan_usa26_portControlMessage msg;
1648 struct keyspan_serial_private *s_priv;
1649 struct keyspan_port_private *p_priv;
1650 const struct keyspan_device_details *d_details;
1652 struct urb *this_urb;
1653 int device_port, err;
1655 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1657 s_priv = usb_get_serial_data(serial);
1658 p_priv = usb_get_serial_port_data(port);
1659 d_details = s_priv->device_details;
1660 device_port = port->number - port->serial->minor;
1662 outcont_urb = d_details->outcont_endpoints[port->number];
1663 this_urb = p_priv->outcont_urb;
1665 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1667 /* Make sure we have an urb then send the message */
1668 if (this_urb == NULL) {
1669 dbg("%s - oops no urb.", __FUNCTION__);
1673 /* Save reset port val for resend.
1674 Don't overwrite resend for close condition. */
1675 if (p_priv->resend_cont != 3)
1676 p_priv->resend_cont = reset_port + 1;
1677 if (this_urb->status == -EINPROGRESS) {
1678 /* dbg ("%s - already writing", __FUNCTION__); */
1683 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1685 /* Only set baud rate if it's changed */
1686 if (p_priv->old_baud != p_priv->baud) {
1687 p_priv->old_baud = p_priv->baud;
1688 msg.setClocking = 0xff;
1689 if (d_details->calculate_baud_rate
1690 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1691 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1692 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1695 msg.baudHi = 125; /* Values for 9600 baud */
1698 msg.setPrescaler = 0xff;
1701 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1702 switch (p_priv->cflag & CSIZE) {
1704 msg.lcr |= USA_DATABITS_5;
1707 msg.lcr |= USA_DATABITS_6;
1710 msg.lcr |= USA_DATABITS_7;
1713 msg.lcr |= USA_DATABITS_8;
1716 if (p_priv->cflag & PARENB) {
1717 /* note USA_PARITY_NONE == 0 */
1718 msg.lcr |= (p_priv->cflag & PARODD)?
1719 USA_PARITY_ODD: USA_PARITY_EVEN;
1723 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1724 msg.xonFlowControl = 0;
1725 msg.setFlowControl = 0xff;
1726 msg.forwardingLength = 16;
1731 if (reset_port == 1) {
1740 msg.returnStatus = 0;
1741 msg.resetDataToggle = 0xff;
1745 else if (reset_port == 2) {
1754 msg.returnStatus = 0;
1755 msg.resetDataToggle = 0;
1758 /* Sending intermediate configs */
1760 msg._txOn = (! p_priv->break_on);
1763 msg.txBreak = (p_priv->break_on);
1768 msg.returnStatus = 0;
1769 msg.resetDataToggle = 0x0;
1772 /* Do handshaking outputs */
1773 msg.setTxTriState_setRts = 0xff;
1774 msg.txTriState_rts = p_priv->rts_state;
1776 msg.setHskoa_setDtr = 0xff;
1777 msg.hskoa_dtr = p_priv->dtr_state;
1779 p_priv->resend_cont = 0;
1780 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1782 /* send the data out the device on control endpoint */
1783 this_urb->transfer_buffer_length = sizeof(msg);
1785 this_urb->dev = serial->dev;
1786 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1787 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1791 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1792 outcont_urb, this_urb->transfer_buffer_length,
1793 usb_pipeendpoint(this_urb->pipe));
1800 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1801 struct usb_serial_port *port,
1804 struct keyspan_usa28_portControlMessage msg;
1805 struct keyspan_serial_private *s_priv;
1806 struct keyspan_port_private *p_priv;
1807 const struct keyspan_device_details *d_details;
1808 struct urb *this_urb;
1809 int device_port, err;
1811 dbg ("%s", __FUNCTION__);
1813 s_priv = usb_get_serial_data(serial);
1814 p_priv = usb_get_serial_port_data(port);
1815 d_details = s_priv->device_details;
1816 device_port = port->number - port->serial->minor;
1818 /* only do something if we have a bulk out endpoint */
1819 if ((this_urb = p_priv->outcont_urb) == NULL) {
1820 dbg("%s - oops no urb.", __FUNCTION__);
1824 /* Save reset port val for resend.
1825 Don't overwrite resend for close condition. */
1826 if (p_priv->resend_cont != 3)
1827 p_priv->resend_cont = reset_port + 1;
1828 if (this_urb->status == -EINPROGRESS) {
1829 dbg ("%s already writing", __FUNCTION__);
1834 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1836 msg.setBaudRate = 1;
1837 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1838 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1839 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1841 msg.baudHi = 0xb2; /* Values for 9600 baud */
1844 /* If parity is enabled, we must calculate it ourselves. */
1845 msg.parity = 0; /* XXX for now */
1847 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1848 msg.xonFlowControl = 0;
1850 /* Do handshaking outputs, DTR is inverted relative to RTS */
1851 msg.rts = p_priv->rts_state;
1852 msg.dtr = p_priv->dtr_state;
1854 msg.forwardingLength = 16;
1856 msg.breakThreshold = 45;
1860 /*msg.returnStatus = 1;
1861 msg.resetDataToggle = 0xff;*/
1863 if (reset_port == 1) {
1867 msg.txForceXoff = 0;
1873 msg.returnStatus = 0;
1874 msg.resetDataToggle = 0xff;
1877 else if (reset_port == 2) {
1881 msg.txForceXoff = 0;
1887 msg.returnStatus = 0;
1888 msg.resetDataToggle = 0;
1890 /* Sending intermediate configs */
1892 msg._txOn = (! p_priv->break_on);
1895 msg.txForceXoff = 0;
1896 msg.txBreak = (p_priv->break_on);
1901 msg.returnStatus = 0;
1902 msg.resetDataToggle = 0x0;
1905 p_priv->resend_cont = 0;
1906 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1908 /* send the data out the device on control endpoint */
1909 this_urb->transfer_buffer_length = sizeof(msg);
1911 this_urb->dev = serial->dev;
1912 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1913 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1917 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1918 this_urb->transfer_buffer_length);
1925 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1926 struct usb_serial_port *port,
1929 struct keyspan_usa49_portControlMessage msg;
1930 struct keyspan_serial_private *s_priv;
1931 struct keyspan_port_private *p_priv;
1932 const struct keyspan_device_details *d_details;
1934 struct urb *this_urb;
1935 int err, device_port;
1937 dbg ("%s", __FUNCTION__);
1939 s_priv = usb_get_serial_data(serial);
1940 p_priv = usb_get_serial_port_data(port);
1941 d_details = s_priv->device_details;
1943 glocont_urb = d_details->glocont_endpoint;
1944 this_urb = s_priv->glocont_urb;
1946 /* Work out which port within the device is being setup */
1947 device_port = port->number - port->serial->minor;
1949 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1951 /* Make sure we have an urb then send the message */
1952 if (this_urb == NULL) {
1953 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1957 /* Save reset port val for resend.
1958 Don't overwrite resend for close condition. */
1959 if (p_priv->resend_cont != 3)
1960 p_priv->resend_cont = reset_port + 1;
1961 if (this_urb->status == -EINPROGRESS) {
1962 /* dbg ("%s - already writing", __FUNCTION__); */
1967 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1969 /*msg.portNumber = port->number;*/
1970 msg.portNumber = device_port;
1972 /* Only set baud rate if it's changed */
1973 if (p_priv->old_baud != p_priv->baud) {
1974 p_priv->old_baud = p_priv->baud;
1975 msg.setClocking = 0xff;
1976 if (d_details->calculate_baud_rate
1977 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1978 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1979 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1982 msg.baudHi = 125; /* Values for 9600 baud */
1985 //msg.setPrescaler = 0xff;
1988 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1989 switch (p_priv->cflag & CSIZE) {
1991 msg.lcr |= USA_DATABITS_5;
1994 msg.lcr |= USA_DATABITS_6;
1997 msg.lcr |= USA_DATABITS_7;
2000 msg.lcr |= USA_DATABITS_8;
2003 if (p_priv->cflag & PARENB) {
2004 /* note USA_PARITY_NONE == 0 */
2005 msg.lcr |= (p_priv->cflag & PARODD)?
2006 USA_PARITY_ODD: USA_PARITY_EVEN;
2010 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2011 msg.xonFlowControl = 0;
2012 msg.setFlowControl = 0xff;
2014 msg.forwardingLength = 16;
2019 if (reset_port == 1) {
2028 msg.returnStatus = 0;
2029 msg.resetDataToggle = 0xff;
2031 msg.disablePort = 0;
2034 else if (reset_port == 2) {
2043 msg.returnStatus = 0;
2044 msg.resetDataToggle = 0;
2046 msg.disablePort = 1;
2048 /* Sending intermediate configs */
2050 msg._txOn = (! p_priv->break_on);
2053 msg.txBreak = (p_priv->break_on);
2058 msg.returnStatus = 0;
2059 msg.resetDataToggle = 0x0;
2061 msg.disablePort = 0;
2064 /* Do handshaking outputs */
2066 msg.rts = p_priv->rts_state;
2069 msg.dtr = p_priv->dtr_state;
2071 p_priv->resend_cont = 0;
2072 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2074 /* send the data out the device on control endpoint */
2075 this_urb->transfer_buffer_length = sizeof(msg);
2077 this_urb->dev = serial->dev;
2078 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2079 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2083 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2084 outcont_urb, this_urb->transfer_buffer_length,
2085 usb_pipeendpoint(this_urb->pipe));
2092 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2093 struct usb_serial_port *port,
2096 struct keyspan_usa90_portControlMessage msg;
2097 struct keyspan_serial_private *s_priv;
2098 struct keyspan_port_private *p_priv;
2099 const struct keyspan_device_details *d_details;
2100 struct urb *this_urb;
2104 dbg ("%s", __FUNCTION__);
2106 s_priv = usb_get_serial_data(serial);
2107 p_priv = usb_get_serial_port_data(port);
2108 d_details = s_priv->device_details;
2110 /* only do something if we have a bulk out endpoint */
2111 if ((this_urb = p_priv->outcont_urb) == NULL) {
2112 dbg("%s - oops no urb.", __FUNCTION__);
2116 /* Save reset port val for resend.
2117 Don't overwrite resend for open/close condition. */
2118 if ((reset_port + 1) > p_priv->resend_cont)
2119 p_priv->resend_cont = reset_port + 1;
2120 if (this_urb->status == -EINPROGRESS) {
2121 dbg ("%s already writing", __FUNCTION__);
2126 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2128 /* Only set baud rate if it's changed */
2129 if (p_priv->old_baud != p_priv->baud) {
2130 p_priv->old_baud = p_priv->baud;
2131 msg.setClocking = 0x01;
2132 if (d_details->calculate_baud_rate
2133 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2134 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2135 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2137 p_priv->baud = 9600;
2138 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2139 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2145 /* modes must always be correctly specified */
2146 if (p_priv->baud > 57600)
2148 msg.rxMode = RXMODE_DMA;
2149 msg.txMode = TXMODE_DMA;
2153 msg.rxMode = RXMODE_BYHAND;
2154 msg.txMode = TXMODE_BYHAND;
2157 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2158 switch (p_priv->cflag & CSIZE) {
2160 msg.lcr |= USA_DATABITS_5;
2163 msg.lcr |= USA_DATABITS_6;
2166 msg.lcr |= USA_DATABITS_7;
2169 msg.lcr |= USA_DATABITS_8;
2172 if (p_priv->cflag & PARENB) {
2173 /* note USA_PARITY_NONE == 0 */
2174 msg.lcr |= (p_priv->cflag & PARODD)?
2175 USA_PARITY_ODD: USA_PARITY_EVEN;
2177 if (p_priv->old_cflag != p_priv->cflag) {
2178 p_priv->old_cflag = p_priv->cflag;
2182 if (p_priv->flow_control == flow_cts)
2183 msg.txFlowControl = TXFLOW_CTS;
2184 msg.setTxFlowControl = 0x01;
2185 msg.setRxFlowControl = 0x01;
2187 msg.rxForwardingLength = 16;
2188 msg.rxForwardingTimeout = 16;
2189 msg.txAckSetting = 0;
2194 if (reset_port == 1) {
2195 msg.portEnabled = 1;
2197 msg.txBreak = (p_priv->break_on);
2200 else if (reset_port == 2) {
2201 msg.portEnabled = 0;
2203 /* Sending intermediate configs */
2205 if (port->open_count)
2206 msg.portEnabled = 1;
2207 msg.txBreak = (p_priv->break_on);
2210 /* Do handshaking outputs */
2212 msg.rts = p_priv->rts_state;
2215 msg.dtr = p_priv->dtr_state;
2217 p_priv->resend_cont = 0;
2218 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2220 /* send the data out the device on control endpoint */
2221 this_urb->transfer_buffer_length = sizeof(msg);
2223 this_urb->dev = serial->dev;
2224 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2225 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2230 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2232 struct usb_serial *serial = port->serial;
2233 struct keyspan_serial_private *s_priv;
2234 const struct keyspan_device_details *d_details;
2236 dbg ("%s", __FUNCTION__);
2238 s_priv = usb_get_serial_data(serial);
2239 d_details = s_priv->device_details;
2241 switch (d_details->msg_format) {
2243 keyspan_usa26_send_setup(serial, port, reset_port);
2246 keyspan_usa28_send_setup(serial, port, reset_port);
2249 keyspan_usa49_send_setup(serial, port, reset_port);
2252 keyspan_usa90_send_setup(serial, port, reset_port);
2258 /* Gets called by the "real" driver (ie once firmware is loaded
2259 and renumeration has taken place. */
2260 static int keyspan_startup (struct usb_serial *serial)
2263 struct usb_serial_port *port;
2264 struct keyspan_serial_private *s_priv;
2265 struct keyspan_port_private *p_priv;
2266 const struct keyspan_device_details *d_details;
2268 dbg("%s", __FUNCTION__);
2270 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2271 if (d_details->product_id == serial->dev->descriptor.idProduct)
2273 if (d_details == NULL) {
2274 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, serial->dev->descriptor.idProduct);
2278 /* Setup private data for serial driver */
2279 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2281 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2284 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2286 s_priv->device_details = d_details;
2287 usb_set_serial_data(serial, s_priv);
2289 /* Now setup per port private data */
2290 for (i = 0; i < serial->num_ports; i++) {
2291 port = serial->port[i];
2292 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2294 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2297 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2298 p_priv->device_details = d_details;
2299 usb_set_serial_port_data(port, p_priv);
2302 keyspan_setup_urbs(serial);
2304 s_priv->instat_urb->dev = serial->dev;
2305 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2306 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2312 static void keyspan_shutdown (struct usb_serial *serial)
2315 struct usb_serial_port *port;
2316 struct keyspan_serial_private *s_priv;
2317 struct keyspan_port_private *p_priv;
2319 dbg("%s", __FUNCTION__);
2321 s_priv = usb_get_serial_data(serial);
2323 /* Stop reading/writing urbs */
2324 stop_urb(s_priv->instat_urb);
2325 stop_urb(s_priv->glocont_urb);
2326 for (i = 0; i < serial->num_ports; ++i) {
2327 port = serial->port[i];
2328 p_priv = usb_get_serial_port_data(port);
2329 stop_urb(p_priv->inack_urb);
2330 stop_urb(p_priv->outcont_urb);
2331 for (j = 0; j < 2; j++) {
2332 stop_urb(p_priv->in_urbs[j]);
2333 stop_urb(p_priv->out_urbs[j]);
2338 if (s_priv->instat_urb)
2339 usb_free_urb(s_priv->instat_urb);
2340 if (s_priv->glocont_urb)
2341 usb_free_urb(s_priv->glocont_urb);
2342 for (i = 0; i < serial->num_ports; ++i) {
2343 port = serial->port[i];
2344 p_priv = usb_get_serial_port_data(port);
2345 if (p_priv->inack_urb)
2346 usb_free_urb(p_priv->inack_urb);
2347 if (p_priv->outcont_urb)
2348 usb_free_urb(p_priv->outcont_urb);
2349 for (j = 0; j < 2; j++) {
2350 if (p_priv->in_urbs[j])
2351 usb_free_urb(p_priv->in_urbs[j]);
2352 if (p_priv->out_urbs[j])
2353 usb_free_urb(p_priv->out_urbs[j]);
2357 /* dbg("Freeing serial->private."); */
2360 /* dbg("Freeing port->private."); */
2361 /* Now free per port private data */
2362 for (i = 0; i < serial->num_ports; i++) {
2363 port = serial->port[i];
2364 kfree(usb_get_serial_port_data(port));
2368 MODULE_AUTHOR( DRIVER_AUTHOR );
2369 MODULE_DESCRIPTION( DRIVER_DESC );
2370 MODULE_LICENSE("GPL");
2372 MODULE_PARM(debug, "i");
2373 MODULE_PARM_DESC(debug, "Debug enabled or not");