2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 See http://misc.nu/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
31 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
32 Improve setup message handling for all devices.
34 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36 Linux source tree. The Linux tree lacked support for the 49WLC and
37 others. The Keyspan patches didn't work with the current kernel.
39 2003jan30 LPM add support for the 49WLC and MPR
41 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
43 now supported (including QI and QW). Modified port open, port
44 close, and send setup() logic to fix various data and endpoint
45 synchronization bugs and device LED status bugs. Changed keyspan_
46 write_room() to accurately return transmit buffer availability.
47 Changed forwardingLength from 1 to 16 for all adapters.
49 Fri Oct 12 16:45:00 EST 2001
50 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
52 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
54 now supported (including QI and QW). Modified port open, port
55 close, and send setup() logic to fix various data and endpoint
56 synchronization bugs and device LED status bugs. Changed keyspan_
57 write_room() to accurately return transmit buffer availability.
58 Changed forwardingLength from 1 to 16 for all adapters.
60 Fri Oct 12 16:45:00 EST 2001
61 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
63 Mon Oct 8 14:29:00 EST 2001 hugh
64 Fixed bug that prevented mulitport devices operating correctly
65 if they weren't the first unit attached.
67 Sat Oct 6 12:31:21 EST 2001 hugh
68 Added support for USA-28XA and -28XB, misc cleanups, break support
69 for usa26 based models thanks to David Gibson.
71 Thu May 31 11:56:42 PDT 2001 gkh
72 switched from using spinlock to a semaphore
75 Identify version on module load.
77 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
80 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
84 Wed Jul 19 14:00:42 EST 2000 gkh
85 Added module_init and module_exit functions to handle the fact that
86 this driver is a loadable module now.
88 Tue Jul 18 16:14:52 EST 2000 Hugh
89 Basic character input/output for USA-19 now mostly works,
90 fixed at 9600 baud for the moment.
92 Sat Jul 8 11:11:48 EST 2000 Hugh
93 First public release - nothing works except the firmware upload.
94 Tested on PPC and x86 architectures, seems to behave...
98 #include <linux/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <asm/uaccess.h>
109 #include <linux/usb.h>
110 #include <linux/usb/serial.h>
116 * Version Information
118 #define DRIVER_VERSION "v1.1.5"
119 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
120 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
122 #define INSTAT_BUFLEN 32
123 #define GLOCONT_BUFLEN 64
124 #define INDAT49W_BUFLEN 512
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 /* added to support 49wg, where data from all 4 ports comes in on 1 EP */
134 /* and high-speed supported */
135 struct urb *indat_urb;
136 char indat_buf[INDAT49W_BUFLEN];
138 /* XXX this one probably will need a lock */
139 struct urb *glocont_urb;
140 char glocont_buf[GLOCONT_BUFLEN];
141 char ctrl_buf[8]; // for EP0 control message
144 struct keyspan_port_private {
145 /* Keep track of which input & output endpoints to use */
149 /* Keep duplicate of device details in each port
150 structure as well - simplifies some of the
151 callback functions etc. */
152 const struct keyspan_device_details *device_details;
154 /* Input endpoints and buffer for this port */
155 struct urb *in_urbs[2];
156 char in_buffer[2][64];
157 /* Output endpoints and buffer for this port */
158 struct urb *out_urbs[2];
159 char out_buffer[2][64];
161 /* Input ack endpoint */
162 struct urb *inack_urb;
163 char inack_buffer[1];
165 /* Output control endpoint */
166 struct urb *outcont_urb;
167 char outcont_buffer[64];
169 /* Settings for the port */
173 unsigned int old_cflag;
174 enum {flow_none, flow_cts, flow_xon} flow_control;
175 int rts_state; /* Handshaking pins (outputs) */
177 int cts_state; /* Handshaking pins (inputs) */
183 unsigned long tx_start_time[2];
184 int resend_cont; /* need to resend control packet */
188 /* Include Keyspan message headers. All current Keyspan Adapters
189 make use of one of five message formats which are referred
190 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and within this driver. */
191 #include "keyspan_usa26msg.h"
192 #include "keyspan_usa28msg.h"
193 #include "keyspan_usa49msg.h"
194 #include "keyspan_usa90msg.h"
195 #include "keyspan_usa67msg.h"
198 /* Functions used by new usb-serial code. */
199 static int __init keyspan_init (void)
202 retval = usb_serial_register(&keyspan_pre_device);
204 goto failed_pre_device_register;
205 retval = usb_serial_register(&keyspan_1port_device);
207 goto failed_1port_device_register;
208 retval = usb_serial_register(&keyspan_2port_device);
210 goto failed_2port_device_register;
211 retval = usb_serial_register(&keyspan_4port_device);
213 goto failed_4port_device_register;
214 retval = usb_register(&keyspan_driver);
216 goto failed_usb_register;
218 info(DRIVER_VERSION ":" DRIVER_DESC);
222 usb_serial_deregister(&keyspan_4port_device);
223 failed_4port_device_register:
224 usb_serial_deregister(&keyspan_2port_device);
225 failed_2port_device_register:
226 usb_serial_deregister(&keyspan_1port_device);
227 failed_1port_device_register:
228 usb_serial_deregister(&keyspan_pre_device);
229 failed_pre_device_register:
233 static void __exit keyspan_exit (void)
235 usb_deregister (&keyspan_driver);
236 usb_serial_deregister (&keyspan_pre_device);
237 usb_serial_deregister (&keyspan_1port_device);
238 usb_serial_deregister (&keyspan_2port_device);
239 usb_serial_deregister (&keyspan_4port_device);
242 module_init(keyspan_init);
243 module_exit(keyspan_exit);
245 static void keyspan_rx_throttle (struct usb_serial_port *port)
247 dbg("%s - port %d", __FUNCTION__, port->number);
251 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
253 dbg("%s - port %d", __FUNCTION__, port->number);
257 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
259 struct keyspan_port_private *p_priv;
261 dbg("%s", __FUNCTION__);
263 p_priv = usb_get_serial_port_data(port);
265 if (break_state == -1)
266 p_priv->break_on = 1;
268 p_priv->break_on = 0;
270 keyspan_send_setup(port, 0);
274 static void keyspan_set_termios (struct usb_serial_port *port,
275 struct ktermios *old_termios)
277 int baud_rate, device_port;
278 struct keyspan_port_private *p_priv;
279 const struct keyspan_device_details *d_details;
282 dbg("%s", __FUNCTION__);
284 p_priv = usb_get_serial_port_data(port);
285 d_details = p_priv->device_details;
286 cflag = port->tty->termios->c_cflag;
287 device_port = port->number - port->serial->minor;
289 /* Baud rate calculation takes baud rate as an integer
290 so other rates can be generated if desired. */
291 baud_rate = tty_get_baud_rate(port->tty);
292 /* If no match or invalid, don't change */
294 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
295 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
296 /* FIXME - more to do here to ensure rate changes cleanly */
297 p_priv->baud = baud_rate;
300 /* set CTS/RTS handshake etc. */
301 p_priv->cflag = cflag;
302 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
304 keyspan_send_setup(port, 0);
307 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
310 struct keyspan_port_private *p_priv;
312 p_priv = usb_get_serial_port_data(port);
314 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
315 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
316 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
317 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
318 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
319 ((p_priv->ri_state) ? TIOCM_RNG : 0);
324 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
325 unsigned int set, unsigned int clear)
327 struct keyspan_port_private *p_priv;
329 p_priv = usb_get_serial_port_data(port);
332 p_priv->rts_state = 1;
334 p_priv->dtr_state = 1;
336 if (clear & TIOCM_RTS)
337 p_priv->rts_state = 0;
338 if (clear & TIOCM_DTR)
339 p_priv->dtr_state = 0;
340 keyspan_send_setup(port, 0);
344 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
345 unsigned int cmd, unsigned long arg)
350 /* Write function is similar for the four protocols used
351 with only a minor change for usa90 (usa19hs) required */
352 static int keyspan_write(struct usb_serial_port *port,
353 const unsigned char *buf, int count)
355 struct keyspan_port_private *p_priv;
356 const struct keyspan_device_details *d_details;
359 struct urb *this_urb;
360 int err, maxDataLen, dataOffset;
362 p_priv = usb_get_serial_port_data(port);
363 d_details = p_priv->device_details;
365 if (d_details->msg_format == msg_usa90) {
373 dbg("%s - for port %d (%d chars), flip=%d",
374 __FUNCTION__, port->number, count, p_priv->out_flip);
376 for (left = count; left > 0; left -= todo) {
378 if (todo > maxDataLen)
381 flip = p_priv->out_flip;
383 /* Check we have a valid urb/endpoint before we use it... */
384 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
385 /* no bulk out, so return 0 bytes written */
386 dbg("%s - no output urb :(", __FUNCTION__);
390 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
392 if (this_urb->status == -EINPROGRESS) {
393 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
395 usb_unlink_urb(this_urb);
399 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
400 for now so set to zero */
401 ((char *)this_urb->transfer_buffer)[0] = 0;
403 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
406 /* send the data out the bulk port */
407 this_urb->transfer_buffer_length = todo + dataOffset;
409 this_urb->dev = port->serial->dev;
410 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
411 dbg("usb_submit_urb(write bulk) failed (%d)", err);
413 p_priv->tx_start_time[flip] = jiffies;
415 /* Flip for next time if usa26 or usa28 interface
416 (not used on usa49) */
417 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
423 static void usa26_indat_callback(struct urb *urb)
427 struct usb_serial_port *port;
428 struct tty_struct *tty;
429 unsigned char *data = urb->transfer_buffer;
431 dbg ("%s", __FUNCTION__);
433 endpoint = usb_pipeendpoint(urb->pipe);
436 dbg("%s - nonzero status: %x on endpoint %d.",
437 __FUNCTION__, urb->status, endpoint);
441 port = (struct usb_serial_port *) urb->context;
443 if (urb->actual_length) {
444 /* 0x80 bit is error flag */
445 if ((data[0] & 0x80) == 0) {
446 /* no errors on individual bytes, only possible overrun err*/
447 if (data[0] & RXERROR_OVERRUN)
450 for (i = 1; i < urb->actual_length ; ++i) {
451 tty_insert_flip_char(tty, data[i], err);
454 /* some bytes had errors, every byte has status */
455 dbg("%s - RX error!!!!", __FUNCTION__);
456 for (i = 0; i + 1 < urb->actual_length; i += 2) {
457 int stat = data[i], flag = 0;
458 if (stat & RXERROR_OVERRUN)
460 if (stat & RXERROR_FRAMING)
462 if (stat & RXERROR_PARITY)
464 /* XXX should handle break (0x10) */
465 tty_insert_flip_char(tty, data[i+1], flag);
468 tty_flip_buffer_push(tty);
471 /* Resubmit urb so we continue receiving */
472 urb->dev = port->serial->dev;
473 if (port->open_count)
474 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
475 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
480 /* Outdat handling is common for all devices */
481 static void usa2x_outdat_callback(struct urb *urb)
483 struct usb_serial_port *port;
484 struct keyspan_port_private *p_priv;
486 port = (struct usb_serial_port *) urb->context;
487 p_priv = usb_get_serial_port_data(port);
488 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
490 if (port->open_count)
491 usb_serial_port_softint(port);
494 static void usa26_inack_callback(struct urb *urb)
496 dbg ("%s", __FUNCTION__);
500 static void usa26_outcont_callback(struct urb *urb)
502 struct usb_serial_port *port;
503 struct keyspan_port_private *p_priv;
505 port = (struct usb_serial_port *) urb->context;
506 p_priv = usb_get_serial_port_data(port);
508 if (p_priv->resend_cont) {
509 dbg ("%s - sending setup", __FUNCTION__);
510 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
514 static void usa26_instat_callback(struct urb *urb)
516 unsigned char *data = urb->transfer_buffer;
517 struct keyspan_usa26_portStatusMessage *msg;
518 struct usb_serial *serial;
519 struct usb_serial_port *port;
520 struct keyspan_port_private *p_priv;
521 int old_dcd_state, err;
523 serial = (struct usb_serial *) urb->context;
526 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
529 if (urb->actual_length != 9) {
530 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
534 msg = (struct keyspan_usa26_portStatusMessage *)data;
537 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
538 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
539 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
542 /* Now do something useful with the data */
545 /* Check port number from message and retrieve private data */
546 if (msg->port >= serial->num_ports) {
547 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
550 port = serial->port[msg->port];
551 p_priv = usb_get_serial_port_data(port);
553 /* Update handshaking pin state information */
554 old_dcd_state = p_priv->dcd_state;
555 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
556 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
557 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
558 p_priv->ri_state = ((msg->ri) ? 1 : 0);
560 if (port->tty && !C_CLOCAL(port->tty)
561 && old_dcd_state != p_priv->dcd_state) {
563 tty_hangup(port->tty);
565 /* wake_up_interruptible(&p_priv->open_wait); */
568 /* Resubmit urb so we continue receiving */
569 urb->dev = serial->dev;
570 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
571 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
576 static void usa26_glocont_callback(struct urb *urb)
578 dbg ("%s", __FUNCTION__);
583 static void usa28_indat_callback(struct urb *urb)
586 struct usb_serial_port *port;
587 struct tty_struct *tty;
589 struct keyspan_port_private *p_priv;
591 dbg ("%s", __FUNCTION__);
593 port = (struct usb_serial_port *) urb->context;
594 p_priv = usb_get_serial_port_data(port);
595 data = urb->transfer_buffer;
597 if (urb != p_priv->in_urbs[p_priv->in_flip])
602 dbg("%s - nonzero status: %x on endpoint %d.",
603 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
607 port = (struct usb_serial_port *) urb->context;
608 p_priv = usb_get_serial_port_data(port);
609 data = urb->transfer_buffer;
612 if (urb->actual_length) {
613 for (i = 0; i < urb->actual_length ; ++i) {
614 tty_insert_flip_char(tty, data[i], 0);
616 tty_flip_buffer_push(tty);
619 /* Resubmit urb so we continue receiving */
620 urb->dev = port->serial->dev;
621 if (port->open_count)
622 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
623 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
625 p_priv->in_flip ^= 1;
627 urb = p_priv->in_urbs[p_priv->in_flip];
628 } while (urb->status != -EINPROGRESS);
631 static void usa28_inack_callback(struct urb *urb)
633 dbg ("%s", __FUNCTION__);
636 static void usa28_outcont_callback(struct urb *urb)
638 struct usb_serial_port *port;
639 struct keyspan_port_private *p_priv;
641 port = (struct usb_serial_port *) urb->context;
642 p_priv = usb_get_serial_port_data(port);
644 if (p_priv->resend_cont) {
645 dbg ("%s - sending setup", __FUNCTION__);
646 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
650 static void usa28_instat_callback(struct urb *urb)
653 unsigned char *data = urb->transfer_buffer;
654 struct keyspan_usa28_portStatusMessage *msg;
655 struct usb_serial *serial;
656 struct usb_serial_port *port;
657 struct keyspan_port_private *p_priv;
660 serial = (struct usb_serial *) urb->context;
663 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
667 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
668 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
672 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
673 data[0], data[1], data[2], data[3], data[4], data[5],
674 data[6], data[7], data[8], data[9], data[10], data[11]);*/
676 /* Now do something useful with the data */
677 msg = (struct keyspan_usa28_portStatusMessage *)data;
680 /* Check port number from message and retrieve private data */
681 if (msg->port >= serial->num_ports) {
682 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
685 port = serial->port[msg->port];
686 p_priv = usb_get_serial_port_data(port);
688 /* Update handshaking pin state information */
689 old_dcd_state = p_priv->dcd_state;
690 p_priv->cts_state = ((msg->cts) ? 1 : 0);
691 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
692 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
693 p_priv->ri_state = ((msg->ri) ? 1 : 0);
695 if (port->tty && !C_CLOCAL(port->tty)
696 && old_dcd_state != p_priv->dcd_state) {
698 tty_hangup(port->tty);
700 /* wake_up_interruptible(&p_priv->open_wait); */
703 /* Resubmit urb so we continue receiving */
704 urb->dev = serial->dev;
705 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
706 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
711 static void usa28_glocont_callback(struct urb *urb)
713 dbg ("%s", __FUNCTION__);
717 static void usa49_glocont_callback(struct urb *urb)
719 struct usb_serial *serial;
720 struct usb_serial_port *port;
721 struct keyspan_port_private *p_priv;
724 dbg ("%s", __FUNCTION__);
726 serial = (struct usb_serial *) urb->context;
727 for (i = 0; i < serial->num_ports; ++i) {
728 port = serial->port[i];
729 p_priv = usb_get_serial_port_data(port);
731 if (p_priv->resend_cont) {
732 dbg ("%s - sending setup", __FUNCTION__);
733 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
739 /* This is actually called glostat in the Keyspan
741 static void usa49_instat_callback(struct urb *urb)
744 unsigned char *data = urb->transfer_buffer;
745 struct keyspan_usa49_portStatusMessage *msg;
746 struct usb_serial *serial;
747 struct usb_serial_port *port;
748 struct keyspan_port_private *p_priv;
751 dbg ("%s", __FUNCTION__);
753 serial = (struct usb_serial *) urb->context;
756 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
760 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
761 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
765 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
766 data[0], data[1], data[2], data[3], data[4], data[5],
767 data[6], data[7], data[8], data[9], data[10]);*/
769 /* Now do something useful with the data */
770 msg = (struct keyspan_usa49_portStatusMessage *)data;
772 /* Check port number from message and retrieve private data */
773 if (msg->portNumber >= serial->num_ports) {
774 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
777 port = serial->port[msg->portNumber];
778 p_priv = usb_get_serial_port_data(port);
780 /* Update handshaking pin state information */
781 old_dcd_state = p_priv->dcd_state;
782 p_priv->cts_state = ((msg->cts) ? 1 : 0);
783 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
784 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
785 p_priv->ri_state = ((msg->ri) ? 1 : 0);
787 if (port->tty && !C_CLOCAL(port->tty)
788 && old_dcd_state != p_priv->dcd_state) {
790 tty_hangup(port->tty);
792 /* wake_up_interruptible(&p_priv->open_wait); */
795 /* Resubmit urb so we continue receiving */
796 urb->dev = serial->dev;
798 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
799 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
804 static void usa49_inack_callback(struct urb *urb)
806 dbg ("%s", __FUNCTION__);
809 static void usa49_indat_callback(struct urb *urb)
813 struct usb_serial_port *port;
814 struct tty_struct *tty;
815 unsigned char *data = urb->transfer_buffer;
817 dbg ("%s", __FUNCTION__);
819 endpoint = usb_pipeendpoint(urb->pipe);
822 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
823 urb->status, endpoint);
827 port = (struct usb_serial_port *) urb->context;
829 if (urb->actual_length) {
830 /* 0x80 bit is error flag */
831 if ((data[0] & 0x80) == 0) {
832 /* no error on any byte */
833 for (i = 1; i < urb->actual_length ; ++i) {
834 tty_insert_flip_char(tty, data[i], 0);
837 /* some bytes had errors, every byte has status */
838 for (i = 0; i + 1 < urb->actual_length; i += 2) {
839 int stat = data[i], flag = 0;
840 if (stat & RXERROR_OVERRUN)
842 if (stat & RXERROR_FRAMING)
844 if (stat & RXERROR_PARITY)
846 /* XXX should handle break (0x10) */
847 tty_insert_flip_char(tty, data[i+1], flag);
850 tty_flip_buffer_push(tty);
853 /* Resubmit urb so we continue receiving */
854 urb->dev = port->serial->dev;
855 if (port->open_count)
856 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
857 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
861 static void usa49wg_indat_callback(struct urb *urb)
864 struct usb_serial *serial;
865 struct usb_serial_port *port;
866 struct tty_struct *tty;
867 unsigned char *data = urb->transfer_buffer;
869 dbg ("%s", __FUNCTION__);
871 serial = urb->context;
874 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
878 /* inbound data is in the form P#, len, status, data */
882 if (urb->actual_length) {
883 while (i < urb->actual_length) {
885 /* Check port number from message*/
886 if (data[i] >= serial->num_ports) {
887 dbg ("%s - Unexpected port number %d",
888 __FUNCTION__, data[i]);
891 port = serial->port[data[i++]];
895 /* 0x80 bit is error flag */
896 if ((data[i] & 0x80) == 0) {
897 /* no error on any byte */
899 for (x = 1; x < len ; ++x)
900 if (port->open_count)
901 tty_insert_flip_char(tty,
907 * some bytes had errors, every byte has status
909 for (x = 0; x + 1 < len; x += 2) {
910 int stat = data[i], flag = 0;
911 if (stat & RXERROR_OVERRUN)
913 if (stat & RXERROR_FRAMING)
915 if (stat & RXERROR_PARITY)
917 /* XXX should handle break (0x10) */
918 if (port->open_count)
919 tty_insert_flip_char(tty,
924 if (port->open_count)
925 tty_flip_buffer_push(tty);
929 /* Resubmit urb so we continue receiving */
930 urb->dev = serial->dev;
932 err = usb_submit_urb(urb, GFP_ATOMIC);
934 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
937 /* not used, usa-49 doesn't have per-port control endpoints */
938 static void usa49_outcont_callback(struct urb *urb)
940 dbg ("%s", __FUNCTION__);
943 static void usa90_indat_callback(struct urb *urb)
947 struct usb_serial_port *port;
948 struct keyspan_port_private *p_priv;
949 struct tty_struct *tty;
950 unsigned char *data = urb->transfer_buffer;
952 dbg ("%s", __FUNCTION__);
954 endpoint = usb_pipeendpoint(urb->pipe);
957 dbg("%s - nonzero status: %x on endpoint %d.",
958 __FUNCTION__, urb->status, endpoint);
962 port = (struct usb_serial_port *) urb->context;
963 p_priv = usb_get_serial_port_data(port);
966 if (urb->actual_length) {
968 /* if current mode is DMA, looks like usa28 format
969 otherwise looks like usa26 data format */
971 if (p_priv->baud > 57600) {
972 for (i = 0; i < urb->actual_length ; ++i)
973 tty_insert_flip_char(tty, data[i], 0);
977 /* 0x80 bit is error flag */
978 if ((data[0] & 0x80) == 0) {
979 /* no errors on individual bytes, only possible overrun err*/
980 if (data[0] & RXERROR_OVERRUN)
983 for (i = 1; i < urb->actual_length ; ++i)
984 tty_insert_flip_char(tty, data[i], err);
988 /* some bytes had errors, every byte has status */
989 dbg("%s - RX error!!!!", __FUNCTION__);
990 for (i = 0; i + 1 < urb->actual_length; i += 2) {
991 int stat = data[i], flag = 0;
992 if (stat & RXERROR_OVERRUN)
994 if (stat & RXERROR_FRAMING)
996 if (stat & RXERROR_PARITY)
998 /* XXX should handle break (0x10) */
999 tty_insert_flip_char(tty, data[i+1], flag);
1003 tty_flip_buffer_push(tty);
1006 /* Resubmit urb so we continue receiving */
1007 urb->dev = port->serial->dev;
1008 if (port->open_count)
1009 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1010 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1016 static void usa90_instat_callback(struct urb *urb)
1018 unsigned char *data = urb->transfer_buffer;
1019 struct keyspan_usa90_portStatusMessage *msg;
1020 struct usb_serial *serial;
1021 struct usb_serial_port *port;
1022 struct keyspan_port_private *p_priv;
1023 int old_dcd_state, err;
1025 serial = (struct usb_serial *) urb->context;
1028 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
1031 if (urb->actual_length < 14) {
1032 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
1036 msg = (struct keyspan_usa90_portStatusMessage *)data;
1038 /* Now do something useful with the data */
1040 port = serial->port[0];
1041 p_priv = usb_get_serial_port_data(port);
1043 /* Update handshaking pin state information */
1044 old_dcd_state = p_priv->dcd_state;
1045 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1046 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1047 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1048 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1050 if (port->tty && !C_CLOCAL(port->tty)
1051 && old_dcd_state != p_priv->dcd_state) {
1053 tty_hangup(port->tty);
1055 /* wake_up_interruptible(&p_priv->open_wait); */
1058 /* Resubmit urb so we continue receiving */
1059 urb->dev = serial->dev;
1060 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1061 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1067 static void usa90_outcont_callback(struct urb *urb)
1069 struct usb_serial_port *port;
1070 struct keyspan_port_private *p_priv;
1072 port = (struct usb_serial_port *) urb->context;
1073 p_priv = usb_get_serial_port_data(port);
1075 if (p_priv->resend_cont) {
1076 dbg ("%s - sending setup", __FUNCTION__);
1077 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1081 /* Status messages from the 28xg */
1082 static void usa67_instat_callback(struct urb *urb)
1085 unsigned char *data = urb->transfer_buffer;
1086 struct keyspan_usa67_portStatusMessage *msg;
1087 struct usb_serial *serial;
1088 struct usb_serial_port *port;
1089 struct keyspan_port_private *p_priv;
1092 dbg ("%s", __FUNCTION__);
1094 serial = urb->context;
1097 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
1101 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1102 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
1107 /* Now do something useful with the data */
1108 msg = (struct keyspan_usa67_portStatusMessage *)data;
1110 /* Check port number from message and retrieve private data */
1111 if (msg->port >= serial->num_ports) {
1112 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
1116 port = serial->port[msg->port];
1117 p_priv = usb_get_serial_port_data(port);
1119 /* Update handshaking pin state information */
1120 old_dcd_state = p_priv->dcd_state;
1121 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1122 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1124 if (port->tty && !C_CLOCAL(port->tty)
1125 && old_dcd_state != p_priv->dcd_state) {
1127 tty_hangup(port->tty);
1129 /* wake_up_interruptible(&p_priv->open_wait); */
1132 /* Resubmit urb so we continue receiving */
1133 urb->dev = serial->dev;
1134 err = usb_submit_urb(urb, GFP_ATOMIC);
1136 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1139 static void usa67_glocont_callback(struct urb *urb)
1141 struct usb_serial *serial;
1142 struct usb_serial_port *port;
1143 struct keyspan_port_private *p_priv;
1146 dbg ("%s", __FUNCTION__);
1148 serial = urb->context;
1149 for (i = 0; i < serial->num_ports; ++i) {
1150 port = serial->port[i];
1151 p_priv = usb_get_serial_port_data(port);
1153 if (p_priv->resend_cont) {
1154 dbg ("%s - sending setup", __FUNCTION__);
1155 keyspan_usa67_send_setup(serial, port,
1156 p_priv->resend_cont - 1);
1162 static int keyspan_write_room (struct usb_serial_port *port)
1164 struct keyspan_port_private *p_priv;
1165 const struct keyspan_device_details *d_details;
1168 struct urb *this_urb;
1170 dbg("%s", __FUNCTION__);
1171 p_priv = usb_get_serial_port_data(port);
1172 d_details = p_priv->device_details;
1174 if (d_details->msg_format == msg_usa90)
1179 flip = p_priv->out_flip;
1181 /* Check both endpoints to see if any are available. */
1182 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1183 if (this_urb->status != -EINPROGRESS)
1185 flip = (flip + 1) & d_details->outdat_endp_flip;
1186 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1187 if (this_urb->status != -EINPROGRESS)
1194 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1200 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1202 struct keyspan_port_private *p_priv;
1203 struct keyspan_serial_private *s_priv;
1204 struct usb_serial *serial = port->serial;
1205 const struct keyspan_device_details *d_details;
1207 int baud_rate, device_port;
1211 s_priv = usb_get_serial_data(serial);
1212 p_priv = usb_get_serial_port_data(port);
1213 d_details = p_priv->device_details;
1215 dbg("%s - port%d.", __FUNCTION__, port->number);
1217 /* Set some sane defaults */
1218 p_priv->rts_state = 1;
1219 p_priv->dtr_state = 1;
1220 p_priv->baud = 9600;
1222 /* force baud and lcr to be set on open */
1223 p_priv->old_baud = 0;
1224 p_priv->old_cflag = 0;
1226 p_priv->out_flip = 0;
1227 p_priv->in_flip = 0;
1229 /* Reset low level data toggle and start reading from endpoints */
1230 for (i = 0; i < 2; i++) {
1231 if ((urb = p_priv->in_urbs[i]) == NULL)
1233 urb->dev = serial->dev;
1235 /* make sure endpoint data toggle is synchronized with the device */
1237 usb_clear_halt(urb->dev, urb->pipe);
1239 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1240 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1244 /* Reset low level data toggle on out endpoints */
1245 for (i = 0; i < 2; i++) {
1246 if ((urb = p_priv->out_urbs[i]) == NULL)
1248 urb->dev = serial->dev;
1249 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1252 /* get the terminal config for the setup message now so we don't
1253 * need to send 2 of them */
1255 cflag = port->tty->termios->c_cflag;
1256 device_port = port->number - port->serial->minor;
1258 /* Baud rate calculation takes baud rate as an integer
1259 so other rates can be generated if desired. */
1260 baud_rate = tty_get_baud_rate(port->tty);
1261 /* If no match or invalid, leave as default */
1263 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1264 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1265 p_priv->baud = baud_rate;
1268 /* set CTS/RTS handshake etc. */
1269 p_priv->cflag = cflag;
1270 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1272 keyspan_send_setup(port, 1);
1274 //keyspan_set_termios(port, NULL);
1279 static inline void stop_urb(struct urb *urb)
1281 if (urb && urb->status == -EINPROGRESS)
1285 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1288 struct usb_serial *serial = port->serial;
1289 struct keyspan_serial_private *s_priv;
1290 struct keyspan_port_private *p_priv;
1292 dbg("%s", __FUNCTION__);
1293 s_priv = usb_get_serial_data(serial);
1294 p_priv = usb_get_serial_port_data(port);
1296 p_priv->rts_state = 0;
1297 p_priv->dtr_state = 0;
1300 keyspan_send_setup(port, 2);
1301 /* pilot-xfer seems to work best with this delay */
1303 // keyspan_set_termios(port, NULL);
1306 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1307 dbg("%s - urb in progress", __FUNCTION__);
1310 p_priv->out_flip = 0;
1311 p_priv->in_flip = 0;
1314 /* Stop reading/writing urbs */
1315 stop_urb(p_priv->inack_urb);
1316 /* stop_urb(p_priv->outcont_urb); */
1317 for (i = 0; i < 2; i++) {
1318 stop_urb(p_priv->in_urbs[i]);
1319 stop_urb(p_priv->out_urbs[i]);
1326 /* download the firmware to a pre-renumeration device */
1327 static int keyspan_fake_startup (struct usb_serial *serial)
1330 const struct ezusb_hex_record *record;
1333 dbg("Keyspan startup version %04x product %04x",
1334 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1335 le16_to_cpu(serial->dev->descriptor.idProduct));
1337 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1338 dbg("Firmware already loaded. Quitting.");
1342 /* Select firmware image on the basis of idProduct */
1343 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1344 case keyspan_usa28_pre_product_id:
1345 record = &keyspan_usa28_firmware[0];
1349 case keyspan_usa28x_pre_product_id:
1350 record = &keyspan_usa28x_firmware[0];
1354 case keyspan_usa28xa_pre_product_id:
1355 record = &keyspan_usa28xa_firmware[0];
1356 fw_name = "USA28XA";
1359 case keyspan_usa28xb_pre_product_id:
1360 record = &keyspan_usa28xb_firmware[0];
1361 fw_name = "USA28XB";
1364 case keyspan_usa19_pre_product_id:
1365 record = &keyspan_usa19_firmware[0];
1369 case keyspan_usa19qi_pre_product_id:
1370 record = &keyspan_usa19qi_firmware[0];
1371 fw_name = "USA19QI";
1374 case keyspan_mpr_pre_product_id:
1375 record = &keyspan_mpr_firmware[0];
1379 case keyspan_usa19qw_pre_product_id:
1380 record = &keyspan_usa19qw_firmware[0];
1381 fw_name = "USA19QI";
1384 case keyspan_usa18x_pre_product_id:
1385 record = &keyspan_usa18x_firmware[0];
1389 case keyspan_usa19w_pre_product_id:
1390 record = &keyspan_usa19w_firmware[0];
1394 case keyspan_usa49w_pre_product_id:
1395 record = &keyspan_usa49w_firmware[0];
1399 case keyspan_usa49wlc_pre_product_id:
1400 record = &keyspan_usa49wlc_firmware[0];
1401 fw_name = "USA49WLC";
1406 fw_name = "Unknown";
1410 if (record == NULL) {
1411 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1415 dbg("Uploading Keyspan %s firmware.", fw_name);
1417 /* download the firmware image */
1418 response = ezusb_set_reset(serial, 1);
1420 while(record->address != 0xffff) {
1421 response = ezusb_writememory(serial, record->address,
1422 (unsigned char *)record->data,
1423 record->data_size, 0xa0);
1425 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1426 "firmware (%d %04X %p %d)\n",
1428 record->address, record->data, record->data_size);
1433 /* bring device out of reset. Renumeration will occur in a
1434 moment and the new device will bind to the real driver */
1435 response = ezusb_set_reset(serial, 0);
1437 /* we don't want this device to have a driver assigned to it. */
1441 /* Helper functions used by keyspan_setup_urbs */
1442 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1445 struct usb_host_interface *iface_desc;
1446 struct usb_endpoint_descriptor *ep;
1449 iface_desc = serial->interface->cur_altsetting;
1450 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1451 ep = &iface_desc->endpoint[i].desc;
1452 if (ep->bEndpointAddress == endpoint)
1455 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1456 "endpoint %x\n", endpoint);
1460 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1461 int dir, void *ctx, char *buf, int len,
1462 void (*callback)(struct urb *))
1465 struct usb_endpoint_descriptor const *ep_desc;
1466 char const *ep_type_name;
1469 return NULL; /* endpoint not needed */
1471 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1472 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1474 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1478 if (endpoint == 0) {
1479 /* control EP filled in when used */
1483 ep_desc = find_ep(serial, endpoint);
1485 /* leak the urb, something's wrong and the callers don't care */
1488 if (usb_endpoint_xfer_int(ep_desc)) {
1489 ep_type_name = "INT";
1490 usb_fill_int_urb(urb, serial->dev,
1491 usb_sndintpipe(serial->dev, endpoint) | dir,
1492 buf, len, callback, ctx,
1493 ep_desc->bInterval);
1494 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1495 ep_type_name = "BULK";
1496 usb_fill_bulk_urb(urb, serial->dev,
1497 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1498 buf, len, callback, ctx);
1500 dev_warn(&serial->interface->dev,
1501 "unsupported endpoint type %x\n",
1502 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1507 dbg("%s - using urb %p for %s endpoint %x",
1508 __func__, urb, ep_type_name, endpoint);
1512 static struct callbacks {
1513 void (*instat_callback)(struct urb *);
1514 void (*glocont_callback)(struct urb *);
1515 void (*indat_callback)(struct urb *);
1516 void (*outdat_callback)(struct urb *);
1517 void (*inack_callback)(struct urb *);
1518 void (*outcont_callback)(struct urb *);
1519 } keyspan_callbacks[] = {
1521 /* msg_usa26 callbacks */
1522 .instat_callback = usa26_instat_callback,
1523 .glocont_callback = usa26_glocont_callback,
1524 .indat_callback = usa26_indat_callback,
1525 .outdat_callback = usa2x_outdat_callback,
1526 .inack_callback = usa26_inack_callback,
1527 .outcont_callback = usa26_outcont_callback,
1529 /* msg_usa28 callbacks */
1530 .instat_callback = usa28_instat_callback,
1531 .glocont_callback = usa28_glocont_callback,
1532 .indat_callback = usa28_indat_callback,
1533 .outdat_callback = usa2x_outdat_callback,
1534 .inack_callback = usa28_inack_callback,
1535 .outcont_callback = usa28_outcont_callback,
1537 /* msg_usa49 callbacks */
1538 .instat_callback = usa49_instat_callback,
1539 .glocont_callback = usa49_glocont_callback,
1540 .indat_callback = usa49_indat_callback,
1541 .outdat_callback = usa2x_outdat_callback,
1542 .inack_callback = usa49_inack_callback,
1543 .outcont_callback = usa49_outcont_callback,
1545 /* msg_usa90 callbacks */
1546 .instat_callback = usa90_instat_callback,
1547 .glocont_callback = usa28_glocont_callback,
1548 .indat_callback = usa90_indat_callback,
1549 .outdat_callback = usa2x_outdat_callback,
1550 .inack_callback = usa28_inack_callback,
1551 .outcont_callback = usa90_outcont_callback,
1553 /* msg_usa67 callbacks */
1554 .instat_callback = usa67_instat_callback,
1555 .glocont_callback = usa67_glocont_callback,
1556 .indat_callback = usa26_indat_callback,
1557 .outdat_callback = usa2x_outdat_callback,
1558 .inack_callback = usa26_inack_callback,
1559 .outcont_callback = usa26_outcont_callback,
1563 /* Generic setup urbs function that uses
1564 data in device_details */
1565 static void keyspan_setup_urbs(struct usb_serial *serial)
1568 struct keyspan_serial_private *s_priv;
1569 const struct keyspan_device_details *d_details;
1570 struct usb_serial_port *port;
1571 struct keyspan_port_private *p_priv;
1572 struct callbacks *cback;
1575 dbg ("%s", __FUNCTION__);
1577 s_priv = usb_get_serial_data(serial);
1578 d_details = s_priv->device_details;
1580 /* Setup values for the various callback routines */
1581 cback = &keyspan_callbacks[d_details->msg_format];
1583 /* Allocate and set up urbs for each one that is in use,
1584 starting with instat endpoints */
1585 s_priv->instat_urb = keyspan_setup_urb
1586 (serial, d_details->instat_endpoint, USB_DIR_IN,
1587 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1588 cback->instat_callback);
1590 s_priv->indat_urb = keyspan_setup_urb
1591 (serial, d_details->indat_endpoint, USB_DIR_IN,
1592 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1593 usa49wg_indat_callback);
1595 s_priv->glocont_urb = keyspan_setup_urb
1596 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1597 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1598 cback->glocont_callback);
1600 /* Setup endpoints for each port specific thing */
1601 for (i = 0; i < d_details->num_ports; i ++) {
1602 port = serial->port[i];
1603 p_priv = usb_get_serial_port_data(port);
1605 /* Do indat endpoints first, once for each flip */
1606 endp = d_details->indat_endpoints[i];
1607 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1608 p_priv->in_urbs[j] = keyspan_setup_urb
1609 (serial, endp, USB_DIR_IN, port,
1610 p_priv->in_buffer[j], 64,
1611 cback->indat_callback);
1614 p_priv->in_urbs[j] = NULL;
1616 /* outdat endpoints also have flip */
1617 endp = d_details->outdat_endpoints[i];
1618 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1619 p_priv->out_urbs[j] = keyspan_setup_urb
1620 (serial, endp, USB_DIR_OUT, port,
1621 p_priv->out_buffer[j], 64,
1622 cback->outdat_callback);
1625 p_priv->out_urbs[j] = NULL;
1627 /* inack endpoint */
1628 p_priv->inack_urb = keyspan_setup_urb
1629 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1630 port, p_priv->inack_buffer, 1, cback->inack_callback);
1632 /* outcont endpoint */
1633 p_priv->outcont_urb = keyspan_setup_urb
1634 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1635 port, p_priv->outcont_buffer, 64,
1636 cback->outcont_callback);
1641 /* usa19 function doesn't require prescaler */
1642 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1643 u8 *rate_low, u8 *prescaler, int portnum)
1645 u32 b16, /* baud rate times 16 (actual rate used internally) */
1647 cnt; /* inverse of divisor (programmed into 8051) */
1649 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1651 /* prevent divide by zero... */
1652 if( (b16 = (baud_rate * 16L)) == 0) {
1653 return (KEYSPAN_INVALID_BAUD_RATE);
1656 /* Any "standard" rate over 57k6 is marginal on the USA-19
1657 as we run out of divisor resolution. */
1658 if (baud_rate > 57600) {
1659 return (KEYSPAN_INVALID_BAUD_RATE);
1662 /* calculate the divisor and the counter (its inverse) */
1663 if( (div = (baudclk / b16)) == 0) {
1664 return (KEYSPAN_INVALID_BAUD_RATE);
1671 return (KEYSPAN_INVALID_BAUD_RATE);
1674 /* return the counter values if non-null */
1676 *rate_low = (u8) (cnt & 0xff);
1679 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1681 if (rate_low && rate_hi) {
1682 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1685 return (KEYSPAN_BAUD_RATE_OK);
1688 /* usa19hs function doesn't require prescaler */
1689 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1690 u8 *rate_low, u8 *prescaler, int portnum)
1692 u32 b16, /* baud rate times 16 (actual rate used internally) */
1695 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1697 /* prevent divide by zero... */
1698 if( (b16 = (baud_rate * 16L)) == 0)
1699 return (KEYSPAN_INVALID_BAUD_RATE);
1703 /* calculate the divisor */
1704 if( (div = (baudclk / b16)) == 0)
1705 return (KEYSPAN_INVALID_BAUD_RATE);
1708 return (KEYSPAN_INVALID_BAUD_RATE);
1710 /* return the counter values if non-null */
1712 *rate_low = (u8) (div & 0xff);
1715 *rate_hi = (u8) ((div >> 8) & 0xff);
1717 if (rate_low && rate_hi)
1718 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1720 return (KEYSPAN_BAUD_RATE_OK);
1723 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1724 u8 *rate_low, u8 *prescaler, int portnum)
1726 u32 b16, /* baud rate times 16 (actual rate used internally) */
1727 clk, /* clock with 13/8 prescaler */
1728 div, /* divisor using 13/8 prescaler */
1729 res, /* resulting baud rate using 13/8 prescaler */
1730 diff, /* error using 13/8 prescaler */
1735 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1737 /* prevent divide by zero */
1738 if( (b16 = baud_rate * 16L) == 0) {
1739 return (KEYSPAN_INVALID_BAUD_RATE);
1742 /* Calculate prescaler by trying them all and looking
1745 /* start with largest possible difference */
1746 smallest_diff = 0xffffffff;
1748 /* 0 is an invalid prescaler, used as a flag */
1751 for(i = 8; i <= 0xff; ++i) {
1752 clk = (baudclk * 8) / (u32) i;
1754 if( (div = clk / b16) == 0) {
1759 diff= (res > b16) ? (res-b16) : (b16-res);
1761 if(diff < smallest_diff) {
1763 smallest_diff = diff;
1767 if(best_prescaler == 0) {
1768 return (KEYSPAN_INVALID_BAUD_RATE);
1771 clk = (baudclk * 8) / (u32) best_prescaler;
1774 /* return the divisor and prescaler if non-null */
1776 *rate_low = (u8) (div & 0xff);
1779 *rate_hi = (u8) ((div >> 8) & 0xff);
1782 *prescaler = best_prescaler;
1783 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1785 return (KEYSPAN_BAUD_RATE_OK);
1788 /* USA-28 supports different maximum baud rates on each port */
1789 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1790 u8 *rate_low, u8 *prescaler, int portnum)
1792 u32 b16, /* baud rate times 16 (actual rate used internally) */
1794 cnt; /* inverse of divisor (programmed into 8051) */
1796 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1798 /* prevent divide by zero */
1799 if ((b16 = baud_rate * 16L) == 0)
1800 return (KEYSPAN_INVALID_BAUD_RATE);
1802 /* calculate the divisor and the counter (its inverse) */
1803 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1804 return (KEYSPAN_INVALID_BAUD_RATE);
1810 /* check for out of range, based on portnum,
1811 and return result */
1814 return (KEYSPAN_INVALID_BAUD_RATE);
1819 return (KEYSPAN_INVALID_BAUD_RATE);
1823 return (KEYSPAN_INVALID_BAUD_RATE);
1827 /* return the counter values if not NULL
1828 (port 1 will ignore retHi) */
1830 *rate_low = (u8) (cnt & 0xff);
1833 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1835 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1836 return (KEYSPAN_BAUD_RATE_OK);
1839 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1840 struct usb_serial_port *port,
1843 struct keyspan_usa26_portControlMessage msg;
1844 struct keyspan_serial_private *s_priv;
1845 struct keyspan_port_private *p_priv;
1846 const struct keyspan_device_details *d_details;
1848 struct urb *this_urb;
1849 int device_port, err;
1851 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1853 s_priv = usb_get_serial_data(serial);
1854 p_priv = usb_get_serial_port_data(port);
1855 d_details = s_priv->device_details;
1856 device_port = port->number - port->serial->minor;
1858 outcont_urb = d_details->outcont_endpoints[port->number];
1859 this_urb = p_priv->outcont_urb;
1861 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1863 /* Make sure we have an urb then send the message */
1864 if (this_urb == NULL) {
1865 dbg("%s - oops no urb.", __FUNCTION__);
1869 /* Save reset port val for resend.
1870 Don't overwrite resend for open/close condition. */
1871 if ((reset_port + 1) > p_priv->resend_cont)
1872 p_priv->resend_cont = reset_port + 1;
1873 if (this_urb->status == -EINPROGRESS) {
1874 /* dbg ("%s - already writing", __FUNCTION__); */
1879 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1881 /* Only set baud rate if it's changed */
1882 if (p_priv->old_baud != p_priv->baud) {
1883 p_priv->old_baud = p_priv->baud;
1884 msg.setClocking = 0xff;
1885 if (d_details->calculate_baud_rate
1886 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1887 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1888 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1891 msg.baudHi = 125; /* Values for 9600 baud */
1894 msg.setPrescaler = 0xff;
1897 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1898 switch (p_priv->cflag & CSIZE) {
1900 msg.lcr |= USA_DATABITS_5;
1903 msg.lcr |= USA_DATABITS_6;
1906 msg.lcr |= USA_DATABITS_7;
1909 msg.lcr |= USA_DATABITS_8;
1912 if (p_priv->cflag & PARENB) {
1913 /* note USA_PARITY_NONE == 0 */
1914 msg.lcr |= (p_priv->cflag & PARODD)?
1915 USA_PARITY_ODD: USA_PARITY_EVEN;
1919 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1920 msg.xonFlowControl = 0;
1921 msg.setFlowControl = 0xff;
1922 msg.forwardingLength = 16;
1927 if (reset_port == 1) {
1936 msg.returnStatus = 0;
1937 msg.resetDataToggle = 0xff;
1941 else if (reset_port == 2) {
1950 msg.returnStatus = 0;
1951 msg.resetDataToggle = 0;
1954 /* Sending intermediate configs */
1956 msg._txOn = (! p_priv->break_on);
1959 msg.txBreak = (p_priv->break_on);
1964 msg.returnStatus = 0;
1965 msg.resetDataToggle = 0x0;
1968 /* Do handshaking outputs */
1969 msg.setTxTriState_setRts = 0xff;
1970 msg.txTriState_rts = p_priv->rts_state;
1972 msg.setHskoa_setDtr = 0xff;
1973 msg.hskoa_dtr = p_priv->dtr_state;
1975 p_priv->resend_cont = 0;
1976 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1978 /* send the data out the device on control endpoint */
1979 this_urb->transfer_buffer_length = sizeof(msg);
1981 this_urb->dev = serial->dev;
1982 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1983 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1987 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1988 outcont_urb, this_urb->transfer_buffer_length,
1989 usb_pipeendpoint(this_urb->pipe));
1996 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1997 struct usb_serial_port *port,
2000 struct keyspan_usa28_portControlMessage msg;
2001 struct keyspan_serial_private *s_priv;
2002 struct keyspan_port_private *p_priv;
2003 const struct keyspan_device_details *d_details;
2004 struct urb *this_urb;
2005 int device_port, err;
2007 dbg ("%s", __FUNCTION__);
2009 s_priv = usb_get_serial_data(serial);
2010 p_priv = usb_get_serial_port_data(port);
2011 d_details = s_priv->device_details;
2012 device_port = port->number - port->serial->minor;
2014 /* only do something if we have a bulk out endpoint */
2015 if ((this_urb = p_priv->outcont_urb) == NULL) {
2016 dbg("%s - oops no urb.", __FUNCTION__);
2020 /* Save reset port val for resend.
2021 Don't overwrite resend for open/close condition. */
2022 if ((reset_port + 1) > p_priv->resend_cont)
2023 p_priv->resend_cont = reset_port + 1;
2024 if (this_urb->status == -EINPROGRESS) {
2025 dbg ("%s already writing", __FUNCTION__);
2030 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2032 msg.setBaudRate = 1;
2033 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2034 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2035 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
2037 msg.baudHi = 0xb2; /* Values for 9600 baud */
2040 /* If parity is enabled, we must calculate it ourselves. */
2041 msg.parity = 0; /* XXX for now */
2043 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2044 msg.xonFlowControl = 0;
2046 /* Do handshaking outputs, DTR is inverted relative to RTS */
2047 msg.rts = p_priv->rts_state;
2048 msg.dtr = p_priv->dtr_state;
2050 msg.forwardingLength = 16;
2052 msg.breakThreshold = 45;
2056 /*msg.returnStatus = 1;
2057 msg.resetDataToggle = 0xff;*/
2059 if (reset_port == 1) {
2063 msg.txForceXoff = 0;
2069 msg.returnStatus = 0;
2070 msg.resetDataToggle = 0xff;
2073 else if (reset_port == 2) {
2077 msg.txForceXoff = 0;
2083 msg.returnStatus = 0;
2084 msg.resetDataToggle = 0;
2086 /* Sending intermediate configs */
2088 msg._txOn = (! p_priv->break_on);
2091 msg.txForceXoff = 0;
2092 msg.txBreak = (p_priv->break_on);
2097 msg.returnStatus = 0;
2098 msg.resetDataToggle = 0x0;
2101 p_priv->resend_cont = 0;
2102 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2104 /* send the data out the device on control endpoint */
2105 this_urb->transfer_buffer_length = sizeof(msg);
2107 this_urb->dev = serial->dev;
2108 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2109 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
2113 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
2114 this_urb->transfer_buffer_length);
2121 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2122 struct usb_serial_port *port,
2125 struct keyspan_usa49_portControlMessage msg;
2126 struct usb_ctrlrequest *dr = NULL;
2127 struct keyspan_serial_private *s_priv;
2128 struct keyspan_port_private *p_priv;
2129 const struct keyspan_device_details *d_details;
2130 struct urb *this_urb;
2131 int err, device_port;
2133 dbg ("%s", __FUNCTION__);
2135 s_priv = usb_get_serial_data(serial);
2136 p_priv = usb_get_serial_port_data(port);
2137 d_details = s_priv->device_details;
2139 this_urb = s_priv->glocont_urb;
2141 /* Work out which port within the device is being setup */
2142 device_port = port->number - port->serial->minor;
2144 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2146 /* Make sure we have an urb then send the message */
2147 if (this_urb == NULL) {
2148 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
2152 /* Save reset port val for resend.
2153 Don't overwrite resend for open/close condition. */
2154 if ((reset_port + 1) > p_priv->resend_cont)
2155 p_priv->resend_cont = reset_port + 1;
2157 if (this_urb->status == -EINPROGRESS) {
2158 /* dbg ("%s - already writing", __FUNCTION__); */
2163 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2165 /*msg.portNumber = port->number;*/
2166 msg.portNumber = device_port;
2168 /* Only set baud rate if it's changed */
2169 if (p_priv->old_baud != p_priv->baud) {
2170 p_priv->old_baud = p_priv->baud;
2171 msg.setClocking = 0xff;
2172 if (d_details->calculate_baud_rate
2173 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2174 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2175 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2178 msg.baudHi = 125; /* Values for 9600 baud */
2181 //msg.setPrescaler = 0xff;
2184 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2185 switch (p_priv->cflag & CSIZE) {
2187 msg.lcr |= USA_DATABITS_5;
2190 msg.lcr |= USA_DATABITS_6;
2193 msg.lcr |= USA_DATABITS_7;
2196 msg.lcr |= USA_DATABITS_8;
2199 if (p_priv->cflag & PARENB) {
2200 /* note USA_PARITY_NONE == 0 */
2201 msg.lcr |= (p_priv->cflag & PARODD)?
2202 USA_PARITY_ODD: USA_PARITY_EVEN;
2206 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2207 msg.xonFlowControl = 0;
2208 msg.setFlowControl = 0xff;
2210 msg.forwardingLength = 16;
2215 if (reset_port == 1) {
2224 msg.returnStatus = 0;
2225 msg.resetDataToggle = 0xff;
2227 msg.disablePort = 0;
2230 else if (reset_port == 2) {
2239 msg.returnStatus = 0;
2240 msg.resetDataToggle = 0;
2242 msg.disablePort = 1;
2244 /* Sending intermediate configs */
2246 msg._txOn = (! p_priv->break_on);
2249 msg.txBreak = (p_priv->break_on);
2254 msg.returnStatus = 0;
2255 msg.resetDataToggle = 0x0;
2257 msg.disablePort = 0;
2260 /* Do handshaking outputs */
2262 msg.rts = p_priv->rts_state;
2265 msg.dtr = p_priv->dtr_state;
2267 p_priv->resend_cont = 0;
2269 /* if the device is a 49wg, we send control message on usb control EP 0 */
2271 if (d_details->product_id == keyspan_usa49wg_product_id) {
2272 dr = (void *)(s_priv->ctrl_buf);
2273 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2274 dr->bRequest = 0xB0; /* 49wg control message */;
2277 dr->wLength = cpu_to_le16(sizeof(msg));
2279 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2281 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2282 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2283 usa49_glocont_callback, serial);
2286 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2288 /* send the data out the device on control endpoint */
2289 this_urb->transfer_buffer_length = sizeof(msg);
2291 this_urb->dev = serial->dev;
2293 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2294 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2298 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2299 outcont_urb, this_urb->transfer_buffer_length,
2300 usb_pipeendpoint(this_urb->pipe));
2307 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2308 struct usb_serial_port *port,
2311 struct keyspan_usa90_portControlMessage msg;
2312 struct keyspan_serial_private *s_priv;
2313 struct keyspan_port_private *p_priv;
2314 const struct keyspan_device_details *d_details;
2315 struct urb *this_urb;
2319 dbg ("%s", __FUNCTION__);
2321 s_priv = usb_get_serial_data(serial);
2322 p_priv = usb_get_serial_port_data(port);
2323 d_details = s_priv->device_details;
2325 /* only do something if we have a bulk out endpoint */
2326 if ((this_urb = p_priv->outcont_urb) == NULL) {
2327 dbg("%s - oops no urb.", __FUNCTION__);
2331 /* Save reset port val for resend.
2332 Don't overwrite resend for open/close condition. */
2333 if ((reset_port + 1) > p_priv->resend_cont)
2334 p_priv->resend_cont = reset_port + 1;
2335 if (this_urb->status == -EINPROGRESS) {
2336 dbg ("%s already writing", __FUNCTION__);
2341 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2343 /* Only set baud rate if it's changed */
2344 if (p_priv->old_baud != p_priv->baud) {
2345 p_priv->old_baud = p_priv->baud;
2346 msg.setClocking = 0x01;
2347 if (d_details->calculate_baud_rate
2348 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2349 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2350 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2352 p_priv->baud = 9600;
2353 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2354 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2360 /* modes must always be correctly specified */
2361 if (p_priv->baud > 57600)
2363 msg.rxMode = RXMODE_DMA;
2364 msg.txMode = TXMODE_DMA;
2368 msg.rxMode = RXMODE_BYHAND;
2369 msg.txMode = TXMODE_BYHAND;
2372 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2373 switch (p_priv->cflag & CSIZE) {
2375 msg.lcr |= USA_DATABITS_5;
2378 msg.lcr |= USA_DATABITS_6;
2381 msg.lcr |= USA_DATABITS_7;
2384 msg.lcr |= USA_DATABITS_8;
2387 if (p_priv->cflag & PARENB) {
2388 /* note USA_PARITY_NONE == 0 */
2389 msg.lcr |= (p_priv->cflag & PARODD)?
2390 USA_PARITY_ODD: USA_PARITY_EVEN;
2392 if (p_priv->old_cflag != p_priv->cflag) {
2393 p_priv->old_cflag = p_priv->cflag;
2397 if (p_priv->flow_control == flow_cts)
2398 msg.txFlowControl = TXFLOW_CTS;
2399 msg.setTxFlowControl = 0x01;
2400 msg.setRxFlowControl = 0x01;
2402 msg.rxForwardingLength = 16;
2403 msg.rxForwardingTimeout = 16;
2404 msg.txAckSetting = 0;
2409 if (reset_port == 1) {
2410 msg.portEnabled = 1;
2412 msg.txBreak = (p_priv->break_on);
2415 else if (reset_port == 2) {
2416 msg.portEnabled = 0;
2418 /* Sending intermediate configs */
2420 if (port->open_count)
2421 msg.portEnabled = 1;
2422 msg.txBreak = (p_priv->break_on);
2425 /* Do handshaking outputs */
2427 msg.rts = p_priv->rts_state;
2430 msg.dtr = p_priv->dtr_state;
2432 p_priv->resend_cont = 0;
2433 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2435 /* send the data out the device on control endpoint */
2436 this_urb->transfer_buffer_length = sizeof(msg);
2438 this_urb->dev = serial->dev;
2439 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2440 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2445 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2446 struct usb_serial_port *port,
2449 struct keyspan_usa67_portControlMessage msg;
2450 struct keyspan_serial_private *s_priv;
2451 struct keyspan_port_private *p_priv;
2452 const struct keyspan_device_details *d_details;
2453 struct urb *this_urb;
2454 int err, device_port;
2456 dbg ("%s", __FUNCTION__);
2458 s_priv = usb_get_serial_data(serial);
2459 p_priv = usb_get_serial_port_data(port);
2460 d_details = s_priv->device_details;
2462 this_urb = s_priv->glocont_urb;
2464 /* Work out which port within the device is being setup */
2465 device_port = port->number - port->serial->minor;
2467 /* Make sure we have an urb then send the message */
2468 if (this_urb == NULL) {
2469 dbg("%s - oops no urb for port %d.", __FUNCTION__,
2474 /* Save reset port val for resend.
2475 Don't overwrite resend for open/close condition. */
2476 if ((reset_port + 1) > p_priv->resend_cont)
2477 p_priv->resend_cont = reset_port + 1;
2478 if (this_urb->status == -EINPROGRESS) {
2479 /* dbg ("%s - already writing", __FUNCTION__); */
2484 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2486 msg.port = device_port;
2488 /* Only set baud rate if it's changed */
2489 if (p_priv->old_baud != p_priv->baud) {
2490 p_priv->old_baud = p_priv->baud;
2491 msg.setClocking = 0xff;
2492 if (d_details->calculate_baud_rate
2493 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2494 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2495 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2498 msg.baudHi = 125; /* Values for 9600 baud */
2501 msg.setPrescaler = 0xff;
2504 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2505 switch (p_priv->cflag & CSIZE) {
2507 msg.lcr |= USA_DATABITS_5;
2510 msg.lcr |= USA_DATABITS_6;
2513 msg.lcr |= USA_DATABITS_7;
2516 msg.lcr |= USA_DATABITS_8;
2519 if (p_priv->cflag & PARENB) {
2520 /* note USA_PARITY_NONE == 0 */
2521 msg.lcr |= (p_priv->cflag & PARODD)?
2522 USA_PARITY_ODD: USA_PARITY_EVEN;
2526 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2527 msg.xonFlowControl = 0;
2528 msg.setFlowControl = 0xff;
2529 msg.forwardingLength = 16;
2533 if (reset_port == 1) {
2543 msg.returnStatus = 0;
2544 msg.resetDataToggle = 0xff;
2545 } else if (reset_port == 2) {
2555 msg.returnStatus = 0;
2556 msg.resetDataToggle = 0;
2558 /* Sending intermediate configs */
2559 msg._txOn = (! p_priv->break_on);
2562 msg.txBreak = (p_priv->break_on);
2567 msg.returnStatus = 0;
2568 msg.resetDataToggle = 0x0;
2571 /* Do handshaking outputs */
2572 msg.setTxTriState_setRts = 0xff;
2573 msg.txTriState_rts = p_priv->rts_state;
2575 msg.setHskoa_setDtr = 0xff;
2576 msg.hskoa_dtr = p_priv->dtr_state;
2578 p_priv->resend_cont = 0;
2580 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2582 /* send the data out the device on control endpoint */
2583 this_urb->transfer_buffer_length = sizeof(msg);
2584 this_urb->dev = serial->dev;
2586 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2588 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__,
2593 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2595 struct usb_serial *serial = port->serial;
2596 struct keyspan_serial_private *s_priv;
2597 const struct keyspan_device_details *d_details;
2599 dbg ("%s", __FUNCTION__);
2601 s_priv = usb_get_serial_data(serial);
2602 d_details = s_priv->device_details;
2604 switch (d_details->msg_format) {
2606 keyspan_usa26_send_setup(serial, port, reset_port);
2609 keyspan_usa28_send_setup(serial, port, reset_port);
2612 keyspan_usa49_send_setup(serial, port, reset_port);
2615 keyspan_usa90_send_setup(serial, port, reset_port);
2618 keyspan_usa67_send_setup(serial, port, reset_port);
2624 /* Gets called by the "real" driver (ie once firmware is loaded
2625 and renumeration has taken place. */
2626 static int keyspan_startup (struct usb_serial *serial)
2629 struct usb_serial_port *port;
2630 struct keyspan_serial_private *s_priv;
2631 struct keyspan_port_private *p_priv;
2632 const struct keyspan_device_details *d_details;
2634 dbg("%s", __FUNCTION__);
2636 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2637 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2639 if (d_details == NULL) {
2640 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2644 /* Setup private data for serial driver */
2645 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2647 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2651 s_priv->device_details = d_details;
2652 usb_set_serial_data(serial, s_priv);
2654 /* Now setup per port private data */
2655 for (i = 0; i < serial->num_ports; i++) {
2656 port = serial->port[i];
2657 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2659 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2662 p_priv->device_details = d_details;
2663 usb_set_serial_port_data(port, p_priv);
2666 keyspan_setup_urbs(serial);
2668 if (s_priv->instat_urb != NULL) {
2669 s_priv->instat_urb->dev = serial->dev;
2670 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2672 dbg("%s - submit instat urb failed %d", __FUNCTION__,
2675 if (s_priv->indat_urb != NULL) {
2676 s_priv->indat_urb->dev = serial->dev;
2677 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2679 dbg("%s - submit indat urb failed %d", __FUNCTION__,
2686 static void keyspan_shutdown (struct usb_serial *serial)
2689 struct usb_serial_port *port;
2690 struct keyspan_serial_private *s_priv;
2691 struct keyspan_port_private *p_priv;
2693 dbg("%s", __FUNCTION__);
2695 s_priv = usb_get_serial_data(serial);
2697 /* Stop reading/writing urbs */
2698 stop_urb(s_priv->instat_urb);
2699 stop_urb(s_priv->glocont_urb);
2700 stop_urb(s_priv->indat_urb);
2701 for (i = 0; i < serial->num_ports; ++i) {
2702 port = serial->port[i];
2703 p_priv = usb_get_serial_port_data(port);
2704 stop_urb(p_priv->inack_urb);
2705 stop_urb(p_priv->outcont_urb);
2706 for (j = 0; j < 2; j++) {
2707 stop_urb(p_priv->in_urbs[j]);
2708 stop_urb(p_priv->out_urbs[j]);
2713 usb_free_urb(s_priv->instat_urb);
2714 usb_free_urb(s_priv->indat_urb);
2715 usb_free_urb(s_priv->glocont_urb);
2716 for (i = 0; i < serial->num_ports; ++i) {
2717 port = serial->port[i];
2718 p_priv = usb_get_serial_port_data(port);
2719 usb_free_urb(p_priv->inack_urb);
2720 usb_free_urb(p_priv->outcont_urb);
2721 for (j = 0; j < 2; j++) {
2722 usb_free_urb(p_priv->in_urbs[j]);
2723 usb_free_urb(p_priv->out_urbs[j]);
2727 /* dbg("Freeing serial->private."); */
2730 /* dbg("Freeing port->private."); */
2731 /* Now free per port private data */
2732 for (i = 0; i < serial->num_ports; i++) {
2733 port = serial->port[i];
2734 kfree(usb_get_serial_port_data(port));
2738 MODULE_AUTHOR( DRIVER_AUTHOR );
2739 MODULE_DESCRIPTION( DRIVER_DESC );
2740 MODULE_LICENSE("GPL");
2742 module_param(debug, bool, S_IRUGO | S_IWUSR);
2743 MODULE_PARM_DESC(debug, "Debug enabled or not");