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>
110 #include <linux/usb.h>
111 #include "usb-serial.h"
117 * Version Information
119 #define DRIVER_VERSION "v1.1.4"
120 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
121 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
123 #define INSTAT_BUFLEN 32
124 #define GLOCONT_BUFLEN 64
126 /* Per device and per port private data */
127 struct keyspan_serial_private {
128 const struct keyspan_device_details *device_details;
130 struct urb *instat_urb;
131 char instat_buf[INSTAT_BUFLEN];
133 /* XXX this one probably will need a lock */
134 struct urb *glocont_urb;
135 char glocont_buf[GLOCONT_BUFLEN];
138 struct keyspan_port_private {
139 /* Keep track of which input & output endpoints to use */
143 /* Keep duplicate of device details in each port
144 structure as well - simplifies some of the
145 callback functions etc. */
146 const struct keyspan_device_details *device_details;
148 /* Input endpoints and buffer for this port */
149 struct urb *in_urbs[2];
150 char in_buffer[2][64];
151 /* Output endpoints and buffer for this port */
152 struct urb *out_urbs[2];
153 char out_buffer[2][64];
155 /* Input ack endpoint */
156 struct urb *inack_urb;
157 char inack_buffer[1];
159 /* Output control endpoint */
160 struct urb *outcont_urb;
161 char outcont_buffer[64];
163 /* Settings for the port */
167 unsigned int old_cflag;
168 enum {flow_none, flow_cts, flow_xon} flow_control;
169 int rts_state; /* Handshaking pins (outputs) */
171 int cts_state; /* Handshaking pins (inputs) */
177 unsigned long tx_start_time[2];
178 int resend_cont; /* need to resend control packet */
182 /* Include Keyspan message headers. All current Keyspan Adapters
183 make use of one of four message formats which are referred
184 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
185 #include "keyspan_usa26msg.h"
186 #include "keyspan_usa28msg.h"
187 #include "keyspan_usa49msg.h"
188 #include "keyspan_usa90msg.h"
191 /* Functions used by new usb-serial code. */
192 static int __init keyspan_init (void)
195 retval = usb_serial_register(&keyspan_pre_device);
197 goto failed_pre_device_register;
198 retval = usb_serial_register(&keyspan_1port_device);
200 goto failed_1port_device_register;
201 retval = usb_serial_register(&keyspan_2port_device);
203 goto failed_2port_device_register;
204 retval = usb_serial_register(&keyspan_4port_device);
206 goto failed_4port_device_register;
207 retval = usb_register(&keyspan_driver);
209 goto failed_usb_register;
211 info(DRIVER_VERSION ":" DRIVER_DESC);
215 usb_serial_deregister(&keyspan_4port_device);
216 failed_4port_device_register:
217 usb_serial_deregister(&keyspan_2port_device);
218 failed_2port_device_register:
219 usb_serial_deregister(&keyspan_1port_device);
220 failed_1port_device_register:
221 usb_serial_deregister(&keyspan_pre_device);
222 failed_pre_device_register:
226 static void __exit keyspan_exit (void)
228 usb_deregister (&keyspan_driver);
229 usb_serial_deregister (&keyspan_pre_device);
230 usb_serial_deregister (&keyspan_1port_device);
231 usb_serial_deregister (&keyspan_2port_device);
232 usb_serial_deregister (&keyspan_4port_device);
235 module_init(keyspan_init);
236 module_exit(keyspan_exit);
238 static void keyspan_rx_throttle (struct usb_serial_port *port)
240 dbg("%s - port %d", __FUNCTION__, port->number);
244 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
246 dbg("%s - port %d", __FUNCTION__, port->number);
250 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
252 struct keyspan_port_private *p_priv;
254 dbg("%s", __FUNCTION__);
256 p_priv = usb_get_serial_port_data(port);
258 if (break_state == -1)
259 p_priv->break_on = 1;
261 p_priv->break_on = 0;
263 keyspan_send_setup(port, 0);
267 static void keyspan_set_termios (struct usb_serial_port *port,
268 struct termios *old_termios)
270 int baud_rate, device_port;
271 struct keyspan_port_private *p_priv;
272 const struct keyspan_device_details *d_details;
275 dbg("%s", __FUNCTION__);
277 p_priv = usb_get_serial_port_data(port);
278 d_details = p_priv->device_details;
279 cflag = port->tty->termios->c_cflag;
280 device_port = port->number - port->serial->minor;
282 /* Baud rate calculation takes baud rate as an integer
283 so other rates can be generated if desired. */
284 baud_rate = tty_get_baud_rate(port->tty);
285 /* If no match or invalid, don't change */
287 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
288 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
289 /* FIXME - more to do here to ensure rate changes cleanly */
290 p_priv->baud = baud_rate;
293 /* set CTS/RTS handshake etc. */
294 p_priv->cflag = cflag;
295 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
297 keyspan_send_setup(port, 0);
300 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
303 struct keyspan_port_private *p_priv;
305 p_priv = usb_get_serial_port_data(port);
307 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
308 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
309 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
310 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
311 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
312 ((p_priv->ri_state) ? TIOCM_RNG : 0);
317 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
318 unsigned int set, unsigned int clear)
320 struct keyspan_port_private *p_priv;
322 p_priv = usb_get_serial_port_data(port);
325 p_priv->rts_state = 1;
327 p_priv->dtr_state = 1;
329 if (clear & TIOCM_RTS)
330 p_priv->rts_state = 0;
331 if (clear & TIOCM_DTR)
332 p_priv->dtr_state = 0;
333 keyspan_send_setup(port, 0);
337 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
338 unsigned int cmd, unsigned long arg)
343 /* Write function is similar for the four protocols used
344 with only a minor change for usa90 (usa19hs) required */
345 static int keyspan_write(struct usb_serial_port *port,
346 const unsigned char *buf, int count)
348 struct keyspan_port_private *p_priv;
349 const struct keyspan_device_details *d_details;
352 struct urb *this_urb;
353 int err, maxDataLen, dataOffset;
355 p_priv = usb_get_serial_port_data(port);
356 d_details = p_priv->device_details;
358 if (d_details->msg_format == msg_usa90) {
366 dbg("%s - for port %d (%d chars), flip=%d",
367 __FUNCTION__, port->number, count, p_priv->out_flip);
369 for (left = count; left > 0; left -= todo) {
371 if (todo > maxDataLen)
374 flip = p_priv->out_flip;
376 /* Check we have a valid urb/endpoint before we use it... */
377 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
378 /* no bulk out, so return 0 bytes written */
379 dbg("%s - no output urb :(", __FUNCTION__);
383 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
385 if (this_urb->status == -EINPROGRESS) {
386 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
388 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
390 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
391 usb_unlink_urb(this_urb);
395 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
396 for now so set to zero */
397 ((char *)this_urb->transfer_buffer)[0] = 0;
399 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
402 /* send the data out the bulk port */
403 this_urb->transfer_buffer_length = todo + dataOffset;
405 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
406 this_urb->dev = port->serial->dev;
407 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
408 dbg("usb_submit_urb(write bulk) failed (%d)", err);
410 p_priv->tx_start_time[flip] = jiffies;
412 /* Flip for next time if usa26 or usa28 interface
413 (not used on usa49) */
414 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
420 static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
424 struct usb_serial_port *port;
425 struct tty_struct *tty;
426 unsigned char *data = urb->transfer_buffer;
428 dbg ("%s", __FUNCTION__);
430 endpoint = usb_pipeendpoint(urb->pipe);
433 dbg("%s - nonzero status: %x on endpoint %d.",
434 __FUNCTION__, urb->status, endpoint);
438 port = (struct usb_serial_port *) urb->context;
440 if (urb->actual_length) {
441 /* 0x80 bit is error flag */
442 if ((data[0] & 0x80) == 0) {
443 /* no errors on individual bytes, only possible overrun err*/
444 if (data[0] & RXERROR_OVERRUN)
447 for (i = 1; i < urb->actual_length ; ++i) {
448 tty_insert_flip_char(tty, data[i], err);
451 /* some bytes had errors, every byte has status */
452 dbg("%s - RX error!!!!", __FUNCTION__);
453 for (i = 0; i + 1 < urb->actual_length; i += 2) {
454 int stat = data[i], flag = 0;
455 if (stat & RXERROR_OVERRUN)
457 if (stat & RXERROR_FRAMING)
459 if (stat & RXERROR_PARITY)
461 /* XXX should handle break (0x10) */
462 tty_insert_flip_char(tty, data[i+1], flag);
465 tty_flip_buffer_push(tty);
468 /* Resubmit urb so we continue receiving */
469 urb->dev = port->serial->dev;
470 if (port->open_count)
471 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
472 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
477 /* Outdat handling is common for all devices */
478 static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
480 struct usb_serial_port *port;
481 struct keyspan_port_private *p_priv;
483 port = (struct usb_serial_port *) urb->context;
484 p_priv = usb_get_serial_port_data(port);
485 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
487 if (port->open_count)
488 schedule_work(&port->work);
491 static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
493 dbg ("%s", __FUNCTION__);
497 static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
499 struct usb_serial_port *port;
500 struct keyspan_port_private *p_priv;
502 port = (struct usb_serial_port *) urb->context;
503 p_priv = usb_get_serial_port_data(port);
505 if (p_priv->resend_cont) {
506 dbg ("%s - sending setup", __FUNCTION__);
507 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
511 static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
513 unsigned char *data = urb->transfer_buffer;
514 struct keyspan_usa26_portStatusMessage *msg;
515 struct usb_serial *serial;
516 struct usb_serial_port *port;
517 struct keyspan_port_private *p_priv;
518 int old_dcd_state, err;
520 serial = (struct usb_serial *) urb->context;
523 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
526 if (urb->actual_length != 9) {
527 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
531 msg = (struct keyspan_usa26_portStatusMessage *)data;
534 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
535 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
536 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
539 /* Now do something useful with the data */
542 /* Check port number from message and retrieve private data */
543 if (msg->port >= serial->num_ports) {
544 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
547 port = serial->port[msg->port];
548 p_priv = usb_get_serial_port_data(port);
550 /* Update handshaking pin state information */
551 old_dcd_state = p_priv->dcd_state;
552 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
553 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
554 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
555 p_priv->ri_state = ((msg->ri) ? 1 : 0);
557 if (port->tty && !C_CLOCAL(port->tty)
558 && old_dcd_state != p_priv->dcd_state) {
560 tty_hangup(port->tty);
562 /* wake_up_interruptible(&p_priv->open_wait); */
565 /* Resubmit urb so we continue receiving */
566 urb->dev = serial->dev;
567 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
568 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
573 static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
575 dbg ("%s", __FUNCTION__);
580 static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
583 struct usb_serial_port *port;
584 struct tty_struct *tty;
586 struct keyspan_port_private *p_priv;
588 dbg ("%s", __FUNCTION__);
590 port = (struct usb_serial_port *) urb->context;
591 p_priv = usb_get_serial_port_data(port);
592 data = urb->transfer_buffer;
594 if (urb != p_priv->in_urbs[p_priv->in_flip])
599 dbg("%s - nonzero status: %x on endpoint %d.",
600 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
604 port = (struct usb_serial_port *) urb->context;
605 p_priv = usb_get_serial_port_data(port);
606 data = urb->transfer_buffer;
609 if (urb->actual_length) {
610 for (i = 0; i < urb->actual_length ; ++i) {
611 tty_insert_flip_char(tty, data[i], 0);
613 tty_flip_buffer_push(tty);
616 /* Resubmit urb so we continue receiving */
617 urb->dev = port->serial->dev;
618 if (port->open_count)
619 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
620 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
622 p_priv->in_flip ^= 1;
624 urb = p_priv->in_urbs[p_priv->in_flip];
625 } while (urb->status != -EINPROGRESS);
628 static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
630 dbg ("%s", __FUNCTION__);
633 static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
635 struct usb_serial_port *port;
636 struct keyspan_port_private *p_priv;
638 port = (struct usb_serial_port *) urb->context;
639 p_priv = usb_get_serial_port_data(port);
641 if (p_priv->resend_cont) {
642 dbg ("%s - sending setup", __FUNCTION__);
643 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
647 static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
650 unsigned char *data = urb->transfer_buffer;
651 struct keyspan_usa28_portStatusMessage *msg;
652 struct usb_serial *serial;
653 struct usb_serial_port *port;
654 struct keyspan_port_private *p_priv;
657 serial = (struct usb_serial *) urb->context;
660 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
664 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
665 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
669 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
670 data[0], data[1], data[2], data[3], data[4], data[5],
671 data[6], data[7], data[8], data[9], data[10], data[11]);*/
673 /* Now do something useful with the data */
674 msg = (struct keyspan_usa28_portStatusMessage *)data;
677 /* Check port number from message and retrieve private data */
678 if (msg->port >= serial->num_ports) {
679 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
682 port = serial->port[msg->port];
683 p_priv = usb_get_serial_port_data(port);
685 /* Update handshaking pin state information */
686 old_dcd_state = p_priv->dcd_state;
687 p_priv->cts_state = ((msg->cts) ? 1 : 0);
688 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
689 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
690 p_priv->ri_state = ((msg->ri) ? 1 : 0);
692 if (port->tty && !C_CLOCAL(port->tty)
693 && old_dcd_state != p_priv->dcd_state) {
695 tty_hangup(port->tty);
697 /* wake_up_interruptible(&p_priv->open_wait); */
700 /* Resubmit urb so we continue receiving */
701 urb->dev = serial->dev;
702 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
703 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
708 static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
710 dbg ("%s", __FUNCTION__);
714 static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
716 struct usb_serial *serial;
717 struct usb_serial_port *port;
718 struct keyspan_port_private *p_priv;
721 dbg ("%s", __FUNCTION__);
723 serial = (struct usb_serial *) urb->context;
724 for (i = 0; i < serial->num_ports; ++i) {
725 port = serial->port[i];
726 p_priv = usb_get_serial_port_data(port);
728 if (p_priv->resend_cont) {
729 dbg ("%s - sending setup", __FUNCTION__);
730 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
736 /* This is actually called glostat in the Keyspan
738 static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
741 unsigned char *data = urb->transfer_buffer;
742 struct keyspan_usa49_portStatusMessage *msg;
743 struct usb_serial *serial;
744 struct usb_serial_port *port;
745 struct keyspan_port_private *p_priv;
748 dbg ("%s", __FUNCTION__);
750 serial = (struct usb_serial *) urb->context;
753 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
757 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
758 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
762 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
763 data[0], data[1], data[2], data[3], data[4], data[5],
764 data[6], data[7], data[8], data[9], data[10]);*/
766 /* Now do something useful with the data */
767 msg = (struct keyspan_usa49_portStatusMessage *)data;
769 /* Check port number from message and retrieve private data */
770 if (msg->portNumber >= serial->num_ports) {
771 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
774 port = serial->port[msg->portNumber];
775 p_priv = usb_get_serial_port_data(port);
777 /* Update handshaking pin state information */
778 old_dcd_state = p_priv->dcd_state;
779 p_priv->cts_state = ((msg->cts) ? 1 : 0);
780 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
781 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
782 p_priv->ri_state = ((msg->ri) ? 1 : 0);
784 if (port->tty && !C_CLOCAL(port->tty)
785 && old_dcd_state != p_priv->dcd_state) {
787 tty_hangup(port->tty);
789 /* wake_up_interruptible(&p_priv->open_wait); */
792 /* Resubmit urb so we continue receiving */
793 urb->dev = serial->dev;
795 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
796 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
801 static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
803 dbg ("%s", __FUNCTION__);
806 static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
810 struct usb_serial_port *port;
811 struct tty_struct *tty;
812 unsigned char *data = urb->transfer_buffer;
814 dbg ("%s", __FUNCTION__);
816 endpoint = usb_pipeendpoint(urb->pipe);
819 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
820 urb->status, endpoint);
824 port = (struct usb_serial_port *) urb->context;
826 if (urb->actual_length) {
827 /* 0x80 bit is error flag */
828 if ((data[0] & 0x80) == 0) {
829 /* no error on any byte */
830 for (i = 1; i < urb->actual_length ; ++i) {
831 tty_insert_flip_char(tty, data[i], 0);
834 /* some bytes had errors, every byte has status */
835 for (i = 0; i + 1 < urb->actual_length; i += 2) {
836 int stat = data[i], flag = 0;
837 if (stat & RXERROR_OVERRUN)
839 if (stat & RXERROR_FRAMING)
841 if (stat & RXERROR_PARITY)
843 /* XXX should handle break (0x10) */
844 tty_insert_flip_char(tty, data[i+1], flag);
847 tty_flip_buffer_push(tty);
850 /* Resubmit urb so we continue receiving */
851 urb->dev = port->serial->dev;
852 if (port->open_count)
853 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
854 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
858 /* not used, usa-49 doesn't have per-port control endpoints */
859 static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
861 dbg ("%s", __FUNCTION__);
864 static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
868 struct usb_serial_port *port;
869 struct keyspan_port_private *p_priv;
870 struct tty_struct *tty;
871 unsigned char *data = urb->transfer_buffer;
873 dbg ("%s", __FUNCTION__);
875 endpoint = usb_pipeendpoint(urb->pipe);
879 dbg("%s - nonzero status: %x on endpoint %d.",
880 __FUNCTION__, urb->status, endpoint);
884 port = (struct usb_serial_port *) urb->context;
885 p_priv = usb_get_serial_port_data(port);
888 if (urb->actual_length) {
890 /* if current mode is DMA, looks like usa28 format
891 otherwise looks like usa26 data format */
893 if (p_priv->baud > 57600) {
894 for (i = 0; i < urb->actual_length ; ++i)
895 tty_insert_flip_char(tty, data[i], 0);
899 /* 0x80 bit is error flag */
900 if ((data[0] & 0x80) == 0) {
901 /* no errors on individual bytes, only possible overrun err*/
902 if (data[0] & RXERROR_OVERRUN)
905 for (i = 1; i < urb->actual_length ; ++i)
906 tty_insert_flip_char(tty, data[i], err);
910 /* some bytes had errors, every byte has status */
911 dbg("%s - RX error!!!!", __FUNCTION__);
912 for (i = 0; i + 1 < urb->actual_length; i += 2) {
913 int stat = data[i], flag = 0;
914 if (stat & RXERROR_OVERRUN)
916 if (stat & RXERROR_FRAMING)
918 if (stat & RXERROR_PARITY)
920 /* XXX should handle break (0x10) */
921 tty_insert_flip_char(tty, data[i+1], flag);
925 tty_flip_buffer_push(tty);
928 /* Resubmit urb so we continue receiving */
929 urb->dev = port->serial->dev;
930 if (port->open_count)
931 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
932 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
938 static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
940 unsigned char *data = urb->transfer_buffer;
941 struct keyspan_usa90_portStatusMessage *msg;
942 struct usb_serial *serial;
943 struct usb_serial_port *port;
944 struct keyspan_port_private *p_priv;
945 int old_dcd_state, err;
947 serial = (struct usb_serial *) urb->context;
950 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
953 if (urb->actual_length < 14) {
954 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
958 msg = (struct keyspan_usa90_portStatusMessage *)data;
960 /* Now do something useful with the data */
962 port = serial->port[0];
963 p_priv = usb_get_serial_port_data(port);
965 /* Update handshaking pin state information */
966 old_dcd_state = p_priv->dcd_state;
967 p_priv->cts_state = ((msg->cts) ? 1 : 0);
968 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
969 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
970 p_priv->ri_state = ((msg->ri) ? 1 : 0);
972 if (port->tty && !C_CLOCAL(port->tty)
973 && old_dcd_state != p_priv->dcd_state) {
975 tty_hangup(port->tty);
977 /* wake_up_interruptible(&p_priv->open_wait); */
980 /* Resubmit urb so we continue receiving */
981 urb->dev = serial->dev;
982 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
983 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
989 static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
991 struct usb_serial_port *port;
992 struct keyspan_port_private *p_priv;
994 port = (struct usb_serial_port *) urb->context;
995 p_priv = usb_get_serial_port_data(port);
997 if (p_priv->resend_cont) {
998 dbg ("%s - sending setup", __FUNCTION__);
999 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1003 static int keyspan_write_room (struct usb_serial_port *port)
1005 struct keyspan_port_private *p_priv;
1006 const struct keyspan_device_details *d_details;
1009 struct urb *this_urb;
1011 dbg("%s", __FUNCTION__);
1012 p_priv = usb_get_serial_port_data(port);
1013 d_details = p_priv->device_details;
1015 if (d_details->msg_format == msg_usa90)
1020 flip = p_priv->out_flip;
1022 /* Check both endpoints to see if any are available. */
1023 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1024 if (this_urb->status != -EINPROGRESS)
1026 flip = (flip + 1) & d_details->outdat_endp_flip;
1027 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1028 if (this_urb->status != -EINPROGRESS)
1035 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1041 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1043 struct keyspan_port_private *p_priv;
1044 struct keyspan_serial_private *s_priv;
1045 struct usb_serial *serial = port->serial;
1046 const struct keyspan_device_details *d_details;
1048 int baud_rate, device_port;
1052 s_priv = usb_get_serial_data(serial);
1053 p_priv = usb_get_serial_port_data(port);
1054 d_details = p_priv->device_details;
1056 dbg("%s - port%d.", __FUNCTION__, port->number);
1058 /* Set some sane defaults */
1059 p_priv->rts_state = 1;
1060 p_priv->dtr_state = 1;
1061 p_priv->baud = 9600;
1063 /* force baud and lcr to be set on open */
1064 p_priv->old_baud = 0;
1065 p_priv->old_cflag = 0;
1067 p_priv->out_flip = 0;
1068 p_priv->in_flip = 0;
1070 /* Reset low level data toggle and start reading from endpoints */
1071 for (i = 0; i < 2; i++) {
1072 if ((urb = p_priv->in_urbs[i]) == NULL)
1074 urb->dev = serial->dev;
1076 /* make sure endpoint data toggle is synchronized with the device */
1078 usb_clear_halt(urb->dev, urb->pipe);
1080 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1081 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1085 /* Reset low level data toggle on out endpoints */
1086 for (i = 0; i < 2; i++) {
1087 if ((urb = p_priv->out_urbs[i]) == NULL)
1089 urb->dev = serial->dev;
1090 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1093 /* get the terminal config for the setup message now so we don't
1094 * need to send 2 of them */
1096 cflag = port->tty->termios->c_cflag;
1097 device_port = port->number - port->serial->minor;
1099 /* Baud rate calculation takes baud rate as an integer
1100 so other rates can be generated if desired. */
1101 baud_rate = tty_get_baud_rate(port->tty);
1102 /* If no match or invalid, leave as default */
1104 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1105 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1106 p_priv->baud = baud_rate;
1109 /* set CTS/RTS handshake etc. */
1110 p_priv->cflag = cflag;
1111 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1113 keyspan_send_setup(port, 1);
1115 //keyspan_set_termios(port, NULL);
1120 static inline void stop_urb(struct urb *urb)
1122 if (urb && urb->status == -EINPROGRESS) {
1123 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1128 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1131 struct usb_serial *serial = port->serial;
1132 struct keyspan_serial_private *s_priv;
1133 struct keyspan_port_private *p_priv;
1135 dbg("%s", __FUNCTION__);
1136 s_priv = usb_get_serial_data(serial);
1137 p_priv = usb_get_serial_port_data(port);
1139 p_priv->rts_state = 0;
1140 p_priv->dtr_state = 0;
1143 keyspan_send_setup(port, 2);
1144 /* pilot-xfer seems to work best with this delay */
1146 // keyspan_set_termios(port, NULL);
1149 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1150 dbg("%s - urb in progress", __FUNCTION__);
1153 p_priv->out_flip = 0;
1154 p_priv->in_flip = 0;
1157 /* Stop reading/writing urbs */
1158 stop_urb(p_priv->inack_urb);
1159 /* stop_urb(p_priv->outcont_urb); */
1160 for (i = 0; i < 2; i++) {
1161 stop_urb(p_priv->in_urbs[i]);
1162 stop_urb(p_priv->out_urbs[i]);
1169 /* download the firmware to a pre-renumeration device */
1170 static int keyspan_fake_startup (struct usb_serial *serial)
1173 const struct ezusb_hex_record *record;
1176 dbg("Keyspan startup version %04x product %04x",
1177 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1178 le16_to_cpu(serial->dev->descriptor.idProduct));
1180 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1181 dbg("Firmware already loaded. Quitting.");
1185 /* Select firmware image on the basis of idProduct */
1186 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1187 case keyspan_usa28_pre_product_id:
1188 record = &keyspan_usa28_firmware[0];
1192 case keyspan_usa28x_pre_product_id:
1193 record = &keyspan_usa28x_firmware[0];
1197 case keyspan_usa28xa_pre_product_id:
1198 record = &keyspan_usa28xa_firmware[0];
1199 fw_name = "USA28XA";
1202 case keyspan_usa28xb_pre_product_id:
1203 record = &keyspan_usa28xb_firmware[0];
1204 fw_name = "USA28XB";
1207 case keyspan_usa19_pre_product_id:
1208 record = &keyspan_usa19_firmware[0];
1212 case keyspan_usa19qi_pre_product_id:
1213 record = &keyspan_usa19qi_firmware[0];
1214 fw_name = "USA19QI";
1217 case keyspan_mpr_pre_product_id:
1218 record = &keyspan_mpr_firmware[0];
1222 case keyspan_usa19qw_pre_product_id:
1223 record = &keyspan_usa19qw_firmware[0];
1224 fw_name = "USA19QI";
1227 case keyspan_usa18x_pre_product_id:
1228 record = &keyspan_usa18x_firmware[0];
1232 case keyspan_usa19w_pre_product_id:
1233 record = &keyspan_usa19w_firmware[0];
1237 case keyspan_usa49w_pre_product_id:
1238 record = &keyspan_usa49w_firmware[0];
1242 case keyspan_usa49wlc_pre_product_id:
1243 record = &keyspan_usa49wlc_firmware[0];
1244 fw_name = "USA49WLC";
1249 fw_name = "Unknown";
1253 if (record == NULL) {
1254 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1258 dbg("Uploading Keyspan %s firmware.", fw_name);
1260 /* download the firmware image */
1261 response = ezusb_set_reset(serial, 1);
1263 while(record->address != 0xffff) {
1264 response = ezusb_writememory(serial, record->address,
1265 (unsigned char *)record->data,
1266 record->data_size, 0xa0);
1268 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1269 "firmware (%d %04X %p %d)\n",
1271 record->address, record->data, record->data_size);
1276 /* bring device out of reset. Renumeration will occur in a
1277 moment and the new device will bind to the real driver */
1278 response = ezusb_set_reset(serial, 0);
1280 /* we don't want this device to have a driver assigned to it. */
1284 /* Helper functions used by keyspan_setup_urbs */
1285 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1286 int dir, void *ctx, char *buf, int len,
1287 void (*callback)(struct urb *, struct pt_regs *regs))
1292 return NULL; /* endpoint not needed */
1294 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1295 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1297 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1301 /* Fill URB using supplied data. */
1302 usb_fill_bulk_urb(urb, serial->dev,
1303 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1304 buf, len, callback, ctx);
1309 static struct callbacks {
1310 void (*instat_callback)(struct urb *, struct pt_regs *regs);
1311 void (*glocont_callback)(struct urb *, struct pt_regs *regs);
1312 void (*indat_callback)(struct urb *, struct pt_regs *regs);
1313 void (*outdat_callback)(struct urb *, struct pt_regs *regs);
1314 void (*inack_callback)(struct urb *, struct pt_regs *regs);
1315 void (*outcont_callback)(struct urb *, struct pt_regs *regs);
1316 } keyspan_callbacks[] = {
1318 /* msg_usa26 callbacks */
1319 .instat_callback = usa26_instat_callback,
1320 .glocont_callback = usa26_glocont_callback,
1321 .indat_callback = usa26_indat_callback,
1322 .outdat_callback = usa2x_outdat_callback,
1323 .inack_callback = usa26_inack_callback,
1324 .outcont_callback = usa26_outcont_callback,
1326 /* msg_usa28 callbacks */
1327 .instat_callback = usa28_instat_callback,
1328 .glocont_callback = usa28_glocont_callback,
1329 .indat_callback = usa28_indat_callback,
1330 .outdat_callback = usa2x_outdat_callback,
1331 .inack_callback = usa28_inack_callback,
1332 .outcont_callback = usa28_outcont_callback,
1334 /* msg_usa49 callbacks */
1335 .instat_callback = usa49_instat_callback,
1336 .glocont_callback = usa49_glocont_callback,
1337 .indat_callback = usa49_indat_callback,
1338 .outdat_callback = usa2x_outdat_callback,
1339 .inack_callback = usa49_inack_callback,
1340 .outcont_callback = usa49_outcont_callback,
1342 /* msg_usa90 callbacks */
1343 .instat_callback = usa90_instat_callback,
1344 .glocont_callback = usa28_glocont_callback,
1345 .indat_callback = usa90_indat_callback,
1346 .outdat_callback = usa2x_outdat_callback,
1347 .inack_callback = usa28_inack_callback,
1348 .outcont_callback = usa90_outcont_callback,
1352 /* Generic setup urbs function that uses
1353 data in device_details */
1354 static void keyspan_setup_urbs(struct usb_serial *serial)
1357 struct keyspan_serial_private *s_priv;
1358 const struct keyspan_device_details *d_details;
1359 struct usb_serial_port *port;
1360 struct keyspan_port_private *p_priv;
1361 struct callbacks *cback;
1364 dbg ("%s", __FUNCTION__);
1366 s_priv = usb_get_serial_data(serial);
1367 d_details = s_priv->device_details;
1369 /* Setup values for the various callback routines */
1370 cback = &keyspan_callbacks[d_details->msg_format];
1372 /* Allocate and set up urbs for each one that is in use,
1373 starting with instat endpoints */
1374 s_priv->instat_urb = keyspan_setup_urb
1375 (serial, d_details->instat_endpoint, USB_DIR_IN,
1376 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1377 cback->instat_callback);
1379 s_priv->glocont_urb = keyspan_setup_urb
1380 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1381 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1382 cback->glocont_callback);
1384 /* Setup endpoints for each port specific thing */
1385 for (i = 0; i < d_details->num_ports; i ++) {
1386 port = serial->port[i];
1387 p_priv = usb_get_serial_port_data(port);
1389 /* Do indat endpoints first, once for each flip */
1390 endp = d_details->indat_endpoints[i];
1391 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1392 p_priv->in_urbs[j] = keyspan_setup_urb
1393 (serial, endp, USB_DIR_IN, port,
1394 p_priv->in_buffer[j], 64,
1395 cback->indat_callback);
1398 p_priv->in_urbs[j] = NULL;
1400 /* outdat endpoints also have flip */
1401 endp = d_details->outdat_endpoints[i];
1402 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1403 p_priv->out_urbs[j] = keyspan_setup_urb
1404 (serial, endp, USB_DIR_OUT, port,
1405 p_priv->out_buffer[j], 64,
1406 cback->outdat_callback);
1409 p_priv->out_urbs[j] = NULL;
1411 /* inack endpoint */
1412 p_priv->inack_urb = keyspan_setup_urb
1413 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1414 port, p_priv->inack_buffer, 1, cback->inack_callback);
1416 /* outcont endpoint */
1417 p_priv->outcont_urb = keyspan_setup_urb
1418 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1419 port, p_priv->outcont_buffer, 64,
1420 cback->outcont_callback);
1425 /* usa19 function doesn't require prescaler */
1426 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1427 u8 *rate_low, u8 *prescaler, int portnum)
1429 u32 b16, /* baud rate times 16 (actual rate used internally) */
1431 cnt; /* inverse of divisor (programmed into 8051) */
1433 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1435 /* prevent divide by zero... */
1436 if( (b16 = (baud_rate * 16L)) == 0) {
1437 return (KEYSPAN_INVALID_BAUD_RATE);
1440 /* Any "standard" rate over 57k6 is marginal on the USA-19
1441 as we run out of divisor resolution. */
1442 if (baud_rate > 57600) {
1443 return (KEYSPAN_INVALID_BAUD_RATE);
1446 /* calculate the divisor and the counter (its inverse) */
1447 if( (div = (baudclk / b16)) == 0) {
1448 return (KEYSPAN_INVALID_BAUD_RATE);
1455 return (KEYSPAN_INVALID_BAUD_RATE);
1458 /* return the counter values if non-null */
1460 *rate_low = (u8) (cnt & 0xff);
1463 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1465 if (rate_low && rate_hi) {
1466 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1469 return (KEYSPAN_BAUD_RATE_OK);
1472 /* usa19hs function doesn't require prescaler */
1473 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1474 u8 *rate_low, u8 *prescaler, int portnum)
1476 u32 b16, /* baud rate times 16 (actual rate used internally) */
1479 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1481 /* prevent divide by zero... */
1482 if( (b16 = (baud_rate * 16L)) == 0)
1483 return (KEYSPAN_INVALID_BAUD_RATE);
1487 /* calculate the divisor */
1488 if( (div = (baudclk / b16)) == 0)
1489 return (KEYSPAN_INVALID_BAUD_RATE);
1492 return (KEYSPAN_INVALID_BAUD_RATE);
1494 /* return the counter values if non-null */
1496 *rate_low = (u8) (div & 0xff);
1499 *rate_hi = (u8) ((div >> 8) & 0xff);
1501 if (rate_low && rate_hi)
1502 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1504 return (KEYSPAN_BAUD_RATE_OK);
1507 static int keyspan_usa19w_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) */
1511 clk, /* clock with 13/8 prescaler */
1512 div, /* divisor using 13/8 prescaler */
1513 res, /* resulting baud rate using 13/8 prescaler */
1514 diff, /* error using 13/8 prescaler */
1519 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1521 /* prevent divide by zero */
1522 if( (b16 = baud_rate * 16L) == 0) {
1523 return (KEYSPAN_INVALID_BAUD_RATE);
1526 /* Calculate prescaler by trying them all and looking
1529 /* start with largest possible difference */
1530 smallest_diff = 0xffffffff;
1532 /* 0 is an invalid prescaler, used as a flag */
1535 for(i = 8; i <= 0xff; ++i) {
1536 clk = (baudclk * 8) / (u32) i;
1538 if( (div = clk / b16) == 0) {
1543 diff= (res > b16) ? (res-b16) : (b16-res);
1545 if(diff < smallest_diff) {
1547 smallest_diff = diff;
1551 if(best_prescaler == 0) {
1552 return (KEYSPAN_INVALID_BAUD_RATE);
1555 clk = (baudclk * 8) / (u32) best_prescaler;
1558 /* return the divisor and prescaler if non-null */
1560 *rate_low = (u8) (div & 0xff);
1563 *rate_hi = (u8) ((div >> 8) & 0xff);
1566 *prescaler = best_prescaler;
1567 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1569 return (KEYSPAN_BAUD_RATE_OK);
1572 /* USA-28 supports different maximum baud rates on each port */
1573 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1574 u8 *rate_low, u8 *prescaler, int portnum)
1576 u32 b16, /* baud rate times 16 (actual rate used internally) */
1578 cnt; /* inverse of divisor (programmed into 8051) */
1580 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1582 /* prevent divide by zero */
1583 if ((b16 = baud_rate * 16L) == 0)
1584 return (KEYSPAN_INVALID_BAUD_RATE);
1586 /* calculate the divisor and the counter (its inverse) */
1587 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1588 return (KEYSPAN_INVALID_BAUD_RATE);
1594 /* check for out of range, based on portnum,
1595 and return result */
1598 return (KEYSPAN_INVALID_BAUD_RATE);
1603 return (KEYSPAN_INVALID_BAUD_RATE);
1607 return (KEYSPAN_INVALID_BAUD_RATE);
1611 /* return the counter values if not NULL
1612 (port 1 will ignore retHi) */
1614 *rate_low = (u8) (cnt & 0xff);
1617 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1619 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1620 return (KEYSPAN_BAUD_RATE_OK);
1623 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1624 struct usb_serial_port *port,
1627 struct keyspan_usa26_portControlMessage msg;
1628 struct keyspan_serial_private *s_priv;
1629 struct keyspan_port_private *p_priv;
1630 const struct keyspan_device_details *d_details;
1632 struct urb *this_urb;
1633 int device_port, err;
1635 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1637 s_priv = usb_get_serial_data(serial);
1638 p_priv = usb_get_serial_port_data(port);
1639 d_details = s_priv->device_details;
1640 device_port = port->number - port->serial->minor;
1642 outcont_urb = d_details->outcont_endpoints[port->number];
1643 this_urb = p_priv->outcont_urb;
1645 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1647 /* Make sure we have an urb then send the message */
1648 if (this_urb == NULL) {
1649 dbg("%s - oops no urb.", __FUNCTION__);
1653 /* Save reset port val for resend.
1654 Don't overwrite resend for close condition. */
1655 if (p_priv->resend_cont != 3)
1656 p_priv->resend_cont = reset_port + 1;
1657 if (this_urb->status == -EINPROGRESS) {
1658 /* dbg ("%s - already writing", __FUNCTION__); */
1663 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1665 /* Only set baud rate if it's changed */
1666 if (p_priv->old_baud != p_priv->baud) {
1667 p_priv->old_baud = p_priv->baud;
1668 msg.setClocking = 0xff;
1669 if (d_details->calculate_baud_rate
1670 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1671 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1672 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1675 msg.baudHi = 125; /* Values for 9600 baud */
1678 msg.setPrescaler = 0xff;
1681 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1682 switch (p_priv->cflag & CSIZE) {
1684 msg.lcr |= USA_DATABITS_5;
1687 msg.lcr |= USA_DATABITS_6;
1690 msg.lcr |= USA_DATABITS_7;
1693 msg.lcr |= USA_DATABITS_8;
1696 if (p_priv->cflag & PARENB) {
1697 /* note USA_PARITY_NONE == 0 */
1698 msg.lcr |= (p_priv->cflag & PARODD)?
1699 USA_PARITY_ODD: USA_PARITY_EVEN;
1703 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1704 msg.xonFlowControl = 0;
1705 msg.setFlowControl = 0xff;
1706 msg.forwardingLength = 16;
1711 if (reset_port == 1) {
1720 msg.returnStatus = 0;
1721 msg.resetDataToggle = 0xff;
1725 else if (reset_port == 2) {
1734 msg.returnStatus = 0;
1735 msg.resetDataToggle = 0;
1738 /* Sending intermediate configs */
1740 msg._txOn = (! p_priv->break_on);
1743 msg.txBreak = (p_priv->break_on);
1748 msg.returnStatus = 0;
1749 msg.resetDataToggle = 0x0;
1752 /* Do handshaking outputs */
1753 msg.setTxTriState_setRts = 0xff;
1754 msg.txTriState_rts = p_priv->rts_state;
1756 msg.setHskoa_setDtr = 0xff;
1757 msg.hskoa_dtr = p_priv->dtr_state;
1759 p_priv->resend_cont = 0;
1760 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1762 /* send the data out the device on control endpoint */
1763 this_urb->transfer_buffer_length = sizeof(msg);
1765 this_urb->dev = serial->dev;
1766 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1767 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1771 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1772 outcont_urb, this_urb->transfer_buffer_length,
1773 usb_pipeendpoint(this_urb->pipe));
1780 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1781 struct usb_serial_port *port,
1784 struct keyspan_usa28_portControlMessage msg;
1785 struct keyspan_serial_private *s_priv;
1786 struct keyspan_port_private *p_priv;
1787 const struct keyspan_device_details *d_details;
1788 struct urb *this_urb;
1789 int device_port, err;
1791 dbg ("%s", __FUNCTION__);
1793 s_priv = usb_get_serial_data(serial);
1794 p_priv = usb_get_serial_port_data(port);
1795 d_details = s_priv->device_details;
1796 device_port = port->number - port->serial->minor;
1798 /* only do something if we have a bulk out endpoint */
1799 if ((this_urb = p_priv->outcont_urb) == NULL) {
1800 dbg("%s - oops no urb.", __FUNCTION__);
1804 /* Save reset port val for resend.
1805 Don't overwrite resend for close condition. */
1806 if (p_priv->resend_cont != 3)
1807 p_priv->resend_cont = reset_port + 1;
1808 if (this_urb->status == -EINPROGRESS) {
1809 dbg ("%s already writing", __FUNCTION__);
1814 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1816 msg.setBaudRate = 1;
1817 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1818 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1819 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1821 msg.baudHi = 0xb2; /* Values for 9600 baud */
1824 /* If parity is enabled, we must calculate it ourselves. */
1825 msg.parity = 0; /* XXX for now */
1827 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1828 msg.xonFlowControl = 0;
1830 /* Do handshaking outputs, DTR is inverted relative to RTS */
1831 msg.rts = p_priv->rts_state;
1832 msg.dtr = p_priv->dtr_state;
1834 msg.forwardingLength = 16;
1836 msg.breakThreshold = 45;
1840 /*msg.returnStatus = 1;
1841 msg.resetDataToggle = 0xff;*/
1843 if (reset_port == 1) {
1847 msg.txForceXoff = 0;
1853 msg.returnStatus = 0;
1854 msg.resetDataToggle = 0xff;
1857 else if (reset_port == 2) {
1861 msg.txForceXoff = 0;
1867 msg.returnStatus = 0;
1868 msg.resetDataToggle = 0;
1870 /* Sending intermediate configs */
1872 msg._txOn = (! p_priv->break_on);
1875 msg.txForceXoff = 0;
1876 msg.txBreak = (p_priv->break_on);
1881 msg.returnStatus = 0;
1882 msg.resetDataToggle = 0x0;
1885 p_priv->resend_cont = 0;
1886 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1888 /* send the data out the device on control endpoint */
1889 this_urb->transfer_buffer_length = sizeof(msg);
1891 this_urb->dev = serial->dev;
1892 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1893 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1897 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1898 this_urb->transfer_buffer_length);
1905 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1906 struct usb_serial_port *port,
1909 struct keyspan_usa49_portControlMessage msg;
1910 struct keyspan_serial_private *s_priv;
1911 struct keyspan_port_private *p_priv;
1912 const struct keyspan_device_details *d_details;
1914 struct urb *this_urb;
1915 int err, device_port;
1917 dbg ("%s", __FUNCTION__);
1919 s_priv = usb_get_serial_data(serial);
1920 p_priv = usb_get_serial_port_data(port);
1921 d_details = s_priv->device_details;
1923 glocont_urb = d_details->glocont_endpoint;
1924 this_urb = s_priv->glocont_urb;
1926 /* Work out which port within the device is being setup */
1927 device_port = port->number - port->serial->minor;
1929 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1931 /* Make sure we have an urb then send the message */
1932 if (this_urb == NULL) {
1933 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1937 /* Save reset port val for resend.
1938 Don't overwrite resend for close condition. */
1939 if (p_priv->resend_cont != 3)
1940 p_priv->resend_cont = reset_port + 1;
1941 if (this_urb->status == -EINPROGRESS) {
1942 /* dbg ("%s - already writing", __FUNCTION__); */
1947 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1949 /*msg.portNumber = port->number;*/
1950 msg.portNumber = device_port;
1952 /* Only set baud rate if it's changed */
1953 if (p_priv->old_baud != p_priv->baud) {
1954 p_priv->old_baud = p_priv->baud;
1955 msg.setClocking = 0xff;
1956 if (d_details->calculate_baud_rate
1957 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1958 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1959 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1962 msg.baudHi = 125; /* Values for 9600 baud */
1965 //msg.setPrescaler = 0xff;
1968 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1969 switch (p_priv->cflag & CSIZE) {
1971 msg.lcr |= USA_DATABITS_5;
1974 msg.lcr |= USA_DATABITS_6;
1977 msg.lcr |= USA_DATABITS_7;
1980 msg.lcr |= USA_DATABITS_8;
1983 if (p_priv->cflag & PARENB) {
1984 /* note USA_PARITY_NONE == 0 */
1985 msg.lcr |= (p_priv->cflag & PARODD)?
1986 USA_PARITY_ODD: USA_PARITY_EVEN;
1990 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1991 msg.xonFlowControl = 0;
1992 msg.setFlowControl = 0xff;
1994 msg.forwardingLength = 16;
1999 if (reset_port == 1) {
2008 msg.returnStatus = 0;
2009 msg.resetDataToggle = 0xff;
2011 msg.disablePort = 0;
2014 else if (reset_port == 2) {
2023 msg.returnStatus = 0;
2024 msg.resetDataToggle = 0;
2026 msg.disablePort = 1;
2028 /* Sending intermediate configs */
2030 msg._txOn = (! p_priv->break_on);
2033 msg.txBreak = (p_priv->break_on);
2038 msg.returnStatus = 0;
2039 msg.resetDataToggle = 0x0;
2041 msg.disablePort = 0;
2044 /* Do handshaking outputs */
2046 msg.rts = p_priv->rts_state;
2049 msg.dtr = p_priv->dtr_state;
2051 p_priv->resend_cont = 0;
2052 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2054 /* send the data out the device on control endpoint */
2055 this_urb->transfer_buffer_length = sizeof(msg);
2057 this_urb->dev = serial->dev;
2058 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2059 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2063 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2064 outcont_urb, this_urb->transfer_buffer_length,
2065 usb_pipeendpoint(this_urb->pipe));
2072 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2073 struct usb_serial_port *port,
2076 struct keyspan_usa90_portControlMessage msg;
2077 struct keyspan_serial_private *s_priv;
2078 struct keyspan_port_private *p_priv;
2079 const struct keyspan_device_details *d_details;
2080 struct urb *this_urb;
2084 dbg ("%s", __FUNCTION__);
2086 s_priv = usb_get_serial_data(serial);
2087 p_priv = usb_get_serial_port_data(port);
2088 d_details = s_priv->device_details;
2090 /* only do something if we have a bulk out endpoint */
2091 if ((this_urb = p_priv->outcont_urb) == NULL) {
2092 dbg("%s - oops no urb.", __FUNCTION__);
2096 /* Save reset port val for resend.
2097 Don't overwrite resend for open/close condition. */
2098 if ((reset_port + 1) > p_priv->resend_cont)
2099 p_priv->resend_cont = reset_port + 1;
2100 if (this_urb->status == -EINPROGRESS) {
2101 dbg ("%s already writing", __FUNCTION__);
2106 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2108 /* Only set baud rate if it's changed */
2109 if (p_priv->old_baud != p_priv->baud) {
2110 p_priv->old_baud = p_priv->baud;
2111 msg.setClocking = 0x01;
2112 if (d_details->calculate_baud_rate
2113 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2114 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2115 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2117 p_priv->baud = 9600;
2118 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2119 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2125 /* modes must always be correctly specified */
2126 if (p_priv->baud > 57600)
2128 msg.rxMode = RXMODE_DMA;
2129 msg.txMode = TXMODE_DMA;
2133 msg.rxMode = RXMODE_BYHAND;
2134 msg.txMode = TXMODE_BYHAND;
2137 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2138 switch (p_priv->cflag & CSIZE) {
2140 msg.lcr |= USA_DATABITS_5;
2143 msg.lcr |= USA_DATABITS_6;
2146 msg.lcr |= USA_DATABITS_7;
2149 msg.lcr |= USA_DATABITS_8;
2152 if (p_priv->cflag & PARENB) {
2153 /* note USA_PARITY_NONE == 0 */
2154 msg.lcr |= (p_priv->cflag & PARODD)?
2155 USA_PARITY_ODD: USA_PARITY_EVEN;
2157 if (p_priv->old_cflag != p_priv->cflag) {
2158 p_priv->old_cflag = p_priv->cflag;
2162 if (p_priv->flow_control == flow_cts)
2163 msg.txFlowControl = TXFLOW_CTS;
2164 msg.setTxFlowControl = 0x01;
2165 msg.setRxFlowControl = 0x01;
2167 msg.rxForwardingLength = 16;
2168 msg.rxForwardingTimeout = 16;
2169 msg.txAckSetting = 0;
2174 if (reset_port == 1) {
2175 msg.portEnabled = 1;
2177 msg.txBreak = (p_priv->break_on);
2180 else if (reset_port == 2) {
2181 msg.portEnabled = 0;
2183 /* Sending intermediate configs */
2185 if (port->open_count)
2186 msg.portEnabled = 1;
2187 msg.txBreak = (p_priv->break_on);
2190 /* Do handshaking outputs */
2192 msg.rts = p_priv->rts_state;
2195 msg.dtr = p_priv->dtr_state;
2197 p_priv->resend_cont = 0;
2198 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2200 /* send the data out the device on control endpoint */
2201 this_urb->transfer_buffer_length = sizeof(msg);
2203 this_urb->dev = serial->dev;
2204 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2205 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2210 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2212 struct usb_serial *serial = port->serial;
2213 struct keyspan_serial_private *s_priv;
2214 const struct keyspan_device_details *d_details;
2216 dbg ("%s", __FUNCTION__);
2218 s_priv = usb_get_serial_data(serial);
2219 d_details = s_priv->device_details;
2221 switch (d_details->msg_format) {
2223 keyspan_usa26_send_setup(serial, port, reset_port);
2226 keyspan_usa28_send_setup(serial, port, reset_port);
2229 keyspan_usa49_send_setup(serial, port, reset_port);
2232 keyspan_usa90_send_setup(serial, port, reset_port);
2238 /* Gets called by the "real" driver (ie once firmware is loaded
2239 and renumeration has taken place. */
2240 static int keyspan_startup (struct usb_serial *serial)
2243 struct usb_serial_port *port;
2244 struct keyspan_serial_private *s_priv;
2245 struct keyspan_port_private *p_priv;
2246 const struct keyspan_device_details *d_details;
2248 dbg("%s", __FUNCTION__);
2250 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2251 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2253 if (d_details == NULL) {
2254 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2258 /* Setup private data for serial driver */
2259 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2261 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2264 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2266 s_priv->device_details = d_details;
2267 usb_set_serial_data(serial, s_priv);
2269 /* Now setup per port private data */
2270 for (i = 0; i < serial->num_ports; i++) {
2271 port = serial->port[i];
2272 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2274 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2277 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2278 p_priv->device_details = d_details;
2279 usb_set_serial_port_data(port, p_priv);
2282 keyspan_setup_urbs(serial);
2284 s_priv->instat_urb->dev = serial->dev;
2285 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2286 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2292 static void keyspan_shutdown (struct usb_serial *serial)
2295 struct usb_serial_port *port;
2296 struct keyspan_serial_private *s_priv;
2297 struct keyspan_port_private *p_priv;
2299 dbg("%s", __FUNCTION__);
2301 s_priv = usb_get_serial_data(serial);
2303 /* Stop reading/writing urbs */
2304 stop_urb(s_priv->instat_urb);
2305 stop_urb(s_priv->glocont_urb);
2306 for (i = 0; i < serial->num_ports; ++i) {
2307 port = serial->port[i];
2308 p_priv = usb_get_serial_port_data(port);
2309 stop_urb(p_priv->inack_urb);
2310 stop_urb(p_priv->outcont_urb);
2311 for (j = 0; j < 2; j++) {
2312 stop_urb(p_priv->in_urbs[j]);
2313 stop_urb(p_priv->out_urbs[j]);
2318 if (s_priv->instat_urb)
2319 usb_free_urb(s_priv->instat_urb);
2320 if (s_priv->glocont_urb)
2321 usb_free_urb(s_priv->glocont_urb);
2322 for (i = 0; i < serial->num_ports; ++i) {
2323 port = serial->port[i];
2324 p_priv = usb_get_serial_port_data(port);
2325 if (p_priv->inack_urb)
2326 usb_free_urb(p_priv->inack_urb);
2327 if (p_priv->outcont_urb)
2328 usb_free_urb(p_priv->outcont_urb);
2329 for (j = 0; j < 2; j++) {
2330 if (p_priv->in_urbs[j])
2331 usb_free_urb(p_priv->in_urbs[j]);
2332 if (p_priv->out_urbs[j])
2333 usb_free_urb(p_priv->out_urbs[j]);
2337 /* dbg("Freeing serial->private."); */
2340 /* dbg("Freeing port->private."); */
2341 /* Now free per port private data */
2342 for (i = 0; i < serial->num_ports; i++) {
2343 port = serial->port[i];
2344 kfree(usb_get_serial_port_data(port));
2348 MODULE_AUTHOR( DRIVER_AUTHOR );
2349 MODULE_DESCRIPTION( DRIVER_DESC );
2350 MODULE_LICENSE("GPL");
2352 module_param(debug, bool, S_IRUGO | S_IWUSR);
2353 MODULE_PARM_DESC(debug, "Debug enabled or not");