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;
281 struct tty_struct *tty = port->tty;
283 dbg("%s", __FUNCTION__);
285 p_priv = usb_get_serial_port_data(port);
286 d_details = p_priv->device_details;
287 cflag = tty->termios->c_cflag;
288 device_port = port->number - port->serial->minor;
290 /* Baud rate calculation takes baud rate as an integer
291 so other rates can be generated if desired. */
292 baud_rate = tty_get_baud_rate(tty);
293 /* If no match or invalid, don't change */
294 if (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 /* FIXME - calcuate exact rate from divisor ? */
298 p_priv->baud = baud_rate;
300 baud_rate = tty_termios_baud_rate(old_termios);
302 tty_encode_baud_rate(tty, baud_rate, baud_rate);
303 /* set CTS/RTS handshake etc. */
304 p_priv->cflag = cflag;
305 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
307 /* Mark/Space not supported */
308 tty->termios->c_cflag &= ~CMSPAR;
310 keyspan_send_setup(port, 0);
313 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
316 struct keyspan_port_private *p_priv;
318 p_priv = usb_get_serial_port_data(port);
320 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
321 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
322 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
323 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
324 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
325 ((p_priv->ri_state) ? TIOCM_RNG : 0);
330 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
331 unsigned int set, unsigned int clear)
333 struct keyspan_port_private *p_priv;
335 p_priv = usb_get_serial_port_data(port);
338 p_priv->rts_state = 1;
340 p_priv->dtr_state = 1;
342 if (clear & TIOCM_RTS)
343 p_priv->rts_state = 0;
344 if (clear & TIOCM_DTR)
345 p_priv->dtr_state = 0;
346 keyspan_send_setup(port, 0);
350 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
351 unsigned int cmd, unsigned long arg)
356 /* Write function is similar for the four protocols used
357 with only a minor change for usa90 (usa19hs) required */
358 static int keyspan_write(struct usb_serial_port *port,
359 const unsigned char *buf, int count)
361 struct keyspan_port_private *p_priv;
362 const struct keyspan_device_details *d_details;
365 struct urb *this_urb;
366 int err, maxDataLen, dataOffset;
368 p_priv = usb_get_serial_port_data(port);
369 d_details = p_priv->device_details;
371 if (d_details->msg_format == msg_usa90) {
379 dbg("%s - for port %d (%d chars), flip=%d",
380 __FUNCTION__, port->number, count, p_priv->out_flip);
382 for (left = count; left > 0; left -= todo) {
384 if (todo > maxDataLen)
387 flip = p_priv->out_flip;
389 /* Check we have a valid urb/endpoint before we use it... */
390 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
391 /* no bulk out, so return 0 bytes written */
392 dbg("%s - no output urb :(", __FUNCTION__);
396 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
398 if (this_urb->status == -EINPROGRESS) {
399 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
401 usb_unlink_urb(this_urb);
405 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
406 for now so set to zero */
407 ((char *)this_urb->transfer_buffer)[0] = 0;
409 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
412 /* send the data out the bulk port */
413 this_urb->transfer_buffer_length = todo + dataOffset;
415 this_urb->dev = port->serial->dev;
416 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
417 dbg("usb_submit_urb(write bulk) failed (%d)", err);
419 p_priv->tx_start_time[flip] = jiffies;
421 /* Flip for next time if usa26 or usa28 interface
422 (not used on usa49) */
423 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
429 static void usa26_indat_callback(struct urb *urb)
433 struct usb_serial_port *port;
434 struct tty_struct *tty;
435 unsigned char *data = urb->transfer_buffer;
436 int status = urb->status;
438 dbg ("%s", __FUNCTION__);
440 endpoint = usb_pipeendpoint(urb->pipe);
443 dbg("%s - nonzero status: %x on endpoint %d.",
444 __FUNCTION__, status, endpoint);
448 port = (struct usb_serial_port *) urb->context;
450 if (tty && urb->actual_length) {
451 /* 0x80 bit is error flag */
452 if ((data[0] & 0x80) == 0) {
453 /* no errors on individual bytes, only possible overrun err*/
454 if (data[0] & RXERROR_OVERRUN)
457 for (i = 1; i < urb->actual_length ; ++i) {
458 tty_insert_flip_char(tty, data[i], err);
461 /* some bytes had errors, every byte has status */
462 dbg("%s - RX error!!!!", __FUNCTION__);
463 for (i = 0; i + 1 < urb->actual_length; i += 2) {
464 int stat = data[i], flag = 0;
465 if (stat & RXERROR_OVERRUN)
467 if (stat & RXERROR_FRAMING)
469 if (stat & RXERROR_PARITY)
471 /* XXX should handle break (0x10) */
472 tty_insert_flip_char(tty, data[i+1], flag);
475 tty_flip_buffer_push(tty);
478 /* Resubmit urb so we continue receiving */
479 urb->dev = port->serial->dev;
480 if (port->open_count)
481 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
482 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
487 /* Outdat handling is common for all devices */
488 static void usa2x_outdat_callback(struct urb *urb)
490 struct usb_serial_port *port;
491 struct keyspan_port_private *p_priv;
493 port = (struct usb_serial_port *) urb->context;
494 p_priv = usb_get_serial_port_data(port);
495 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
497 if (port->open_count)
498 usb_serial_port_softint(port);
501 static void usa26_inack_callback(struct urb *urb)
503 dbg ("%s", __FUNCTION__);
507 static void usa26_outcont_callback(struct urb *urb)
509 struct usb_serial_port *port;
510 struct keyspan_port_private *p_priv;
512 port = (struct usb_serial_port *) urb->context;
513 p_priv = usb_get_serial_port_data(port);
515 if (p_priv->resend_cont) {
516 dbg ("%s - sending setup", __FUNCTION__);
517 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
521 static void usa26_instat_callback(struct urb *urb)
523 unsigned char *data = urb->transfer_buffer;
524 struct keyspan_usa26_portStatusMessage *msg;
525 struct usb_serial *serial;
526 struct usb_serial_port *port;
527 struct keyspan_port_private *p_priv;
528 int old_dcd_state, err;
529 int status = urb->status;
531 serial = (struct usb_serial *) urb->context;
534 dbg("%s - nonzero status: %x", __FUNCTION__, status);
537 if (urb->actual_length != 9) {
538 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
542 msg = (struct keyspan_usa26_portStatusMessage *)data;
545 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
546 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
547 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
550 /* Now do something useful with the data */
553 /* Check port number from message and retrieve private data */
554 if (msg->port >= serial->num_ports) {
555 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
558 port = serial->port[msg->port];
559 p_priv = usb_get_serial_port_data(port);
561 /* Update handshaking pin state information */
562 old_dcd_state = p_priv->dcd_state;
563 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
564 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
565 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
566 p_priv->ri_state = ((msg->ri) ? 1 : 0);
568 if (port->tty && !C_CLOCAL(port->tty)
569 && old_dcd_state != p_priv->dcd_state) {
571 tty_hangup(port->tty);
573 /* wake_up_interruptible(&p_priv->open_wait); */
576 /* Resubmit urb so we continue receiving */
577 urb->dev = serial->dev;
578 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
579 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
584 static void usa26_glocont_callback(struct urb *urb)
586 dbg ("%s", __FUNCTION__);
591 static void usa28_indat_callback(struct urb *urb)
594 struct usb_serial_port *port;
595 struct tty_struct *tty;
597 struct keyspan_port_private *p_priv;
598 int status = urb->status;
600 dbg ("%s", __FUNCTION__);
602 port = (struct usb_serial_port *) urb->context;
603 p_priv = usb_get_serial_port_data(port);
604 data = urb->transfer_buffer;
606 if (urb != p_priv->in_urbs[p_priv->in_flip])
611 dbg("%s - nonzero status: %x on endpoint %d.",
612 __FUNCTION__, status, usb_pipeendpoint(urb->pipe));
616 port = (struct usb_serial_port *) urb->context;
617 p_priv = usb_get_serial_port_data(port);
618 data = urb->transfer_buffer;
621 if (urb->actual_length) {
622 for (i = 0; i < urb->actual_length ; ++i) {
623 tty_insert_flip_char(tty, data[i], 0);
625 tty_flip_buffer_push(tty);
628 /* Resubmit urb so we continue receiving */
629 urb->dev = port->serial->dev;
630 if (port->open_count)
631 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
632 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
634 p_priv->in_flip ^= 1;
636 urb = p_priv->in_urbs[p_priv->in_flip];
637 } while (urb->status != -EINPROGRESS);
640 static void usa28_inack_callback(struct urb *urb)
642 dbg ("%s", __FUNCTION__);
645 static void usa28_outcont_callback(struct urb *urb)
647 struct usb_serial_port *port;
648 struct keyspan_port_private *p_priv;
650 port = (struct usb_serial_port *) urb->context;
651 p_priv = usb_get_serial_port_data(port);
653 if (p_priv->resend_cont) {
654 dbg ("%s - sending setup", __FUNCTION__);
655 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
659 static void usa28_instat_callback(struct urb *urb)
662 unsigned char *data = urb->transfer_buffer;
663 struct keyspan_usa28_portStatusMessage *msg;
664 struct usb_serial *serial;
665 struct usb_serial_port *port;
666 struct keyspan_port_private *p_priv;
668 int status = urb->status;
670 serial = (struct usb_serial *) urb->context;
673 dbg("%s - nonzero status: %x", __FUNCTION__, status);
677 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
678 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
682 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
683 data[0], data[1], data[2], data[3], data[4], data[5],
684 data[6], data[7], data[8], data[9], data[10], data[11]);*/
686 /* Now do something useful with the data */
687 msg = (struct keyspan_usa28_portStatusMessage *)data;
690 /* Check port number from message and retrieve private data */
691 if (msg->port >= serial->num_ports) {
692 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
695 port = serial->port[msg->port];
696 p_priv = usb_get_serial_port_data(port);
698 /* Update handshaking pin state information */
699 old_dcd_state = p_priv->dcd_state;
700 p_priv->cts_state = ((msg->cts) ? 1 : 0);
701 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
702 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
703 p_priv->ri_state = ((msg->ri) ? 1 : 0);
705 if (port->tty && !C_CLOCAL(port->tty)
706 && old_dcd_state != p_priv->dcd_state) {
708 tty_hangup(port->tty);
710 /* wake_up_interruptible(&p_priv->open_wait); */
713 /* Resubmit urb so we continue receiving */
714 urb->dev = serial->dev;
715 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
716 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
721 static void usa28_glocont_callback(struct urb *urb)
723 dbg ("%s", __FUNCTION__);
727 static void usa49_glocont_callback(struct urb *urb)
729 struct usb_serial *serial;
730 struct usb_serial_port *port;
731 struct keyspan_port_private *p_priv;
734 dbg ("%s", __FUNCTION__);
736 serial = (struct usb_serial *) urb->context;
737 for (i = 0; i < serial->num_ports; ++i) {
738 port = serial->port[i];
739 p_priv = usb_get_serial_port_data(port);
741 if (p_priv->resend_cont) {
742 dbg ("%s - sending setup", __FUNCTION__);
743 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
749 /* This is actually called glostat in the Keyspan
751 static void usa49_instat_callback(struct urb *urb)
754 unsigned char *data = urb->transfer_buffer;
755 struct keyspan_usa49_portStatusMessage *msg;
756 struct usb_serial *serial;
757 struct usb_serial_port *port;
758 struct keyspan_port_private *p_priv;
760 int status = urb->status;
762 dbg ("%s", __FUNCTION__);
764 serial = (struct usb_serial *) urb->context;
767 dbg("%s - nonzero status: %x", __FUNCTION__, status);
771 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
772 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
776 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
777 data[0], data[1], data[2], data[3], data[4], data[5],
778 data[6], data[7], data[8], data[9], data[10]);*/
780 /* Now do something useful with the data */
781 msg = (struct keyspan_usa49_portStatusMessage *)data;
783 /* Check port number from message and retrieve private data */
784 if (msg->portNumber >= serial->num_ports) {
785 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
788 port = serial->port[msg->portNumber];
789 p_priv = usb_get_serial_port_data(port);
791 /* Update handshaking pin state information */
792 old_dcd_state = p_priv->dcd_state;
793 p_priv->cts_state = ((msg->cts) ? 1 : 0);
794 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
795 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
796 p_priv->ri_state = ((msg->ri) ? 1 : 0);
798 if (port->tty && !C_CLOCAL(port->tty)
799 && old_dcd_state != p_priv->dcd_state) {
801 tty_hangup(port->tty);
803 /* wake_up_interruptible(&p_priv->open_wait); */
806 /* Resubmit urb so we continue receiving */
807 urb->dev = serial->dev;
809 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
810 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
815 static void usa49_inack_callback(struct urb *urb)
817 dbg ("%s", __FUNCTION__);
820 static void usa49_indat_callback(struct urb *urb)
824 struct usb_serial_port *port;
825 struct tty_struct *tty;
826 unsigned char *data = urb->transfer_buffer;
827 int status = urb->status;
829 dbg ("%s", __FUNCTION__);
831 endpoint = usb_pipeendpoint(urb->pipe);
834 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
839 port = (struct usb_serial_port *) urb->context;
841 if (tty && urb->actual_length) {
842 /* 0x80 bit is error flag */
843 if ((data[0] & 0x80) == 0) {
844 /* no error on any byte */
845 for (i = 1; i < urb->actual_length ; ++i) {
846 tty_insert_flip_char(tty, data[i], 0);
849 /* some bytes had errors, every byte has status */
850 for (i = 0; i + 1 < urb->actual_length; i += 2) {
851 int stat = data[i], flag = 0;
852 if (stat & RXERROR_OVERRUN)
854 if (stat & RXERROR_FRAMING)
856 if (stat & RXERROR_PARITY)
858 /* XXX should handle break (0x10) */
859 tty_insert_flip_char(tty, data[i+1], flag);
862 tty_flip_buffer_push(tty);
865 /* Resubmit urb so we continue receiving */
866 urb->dev = port->serial->dev;
867 if (port->open_count)
868 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
869 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
873 static void usa49wg_indat_callback(struct urb *urb)
876 struct usb_serial *serial;
877 struct usb_serial_port *port;
878 struct tty_struct *tty;
879 unsigned char *data = urb->transfer_buffer;
880 int status = urb->status;
882 dbg ("%s", __FUNCTION__);
884 serial = urb->context;
887 dbg("%s - nonzero status: %x", __FUNCTION__, status);
891 /* inbound data is in the form P#, len, status, data */
895 if (urb->actual_length) {
896 while (i < urb->actual_length) {
898 /* Check port number from message*/
899 if (data[i] >= serial->num_ports) {
900 dbg ("%s - Unexpected port number %d",
901 __FUNCTION__, data[i]);
904 port = serial->port[data[i++]];
908 /* 0x80 bit is error flag */
909 if ((data[i] & 0x80) == 0) {
910 /* no error on any byte */
912 for (x = 1; x < len ; ++x)
913 if (port->open_count)
914 tty_insert_flip_char(tty,
920 * some bytes had errors, every byte has status
922 for (x = 0; x + 1 < len; x += 2) {
923 int stat = data[i], flag = 0;
924 if (stat & RXERROR_OVERRUN)
926 if (stat & RXERROR_FRAMING)
928 if (stat & RXERROR_PARITY)
930 /* XXX should handle break (0x10) */
931 if (port->open_count)
932 tty_insert_flip_char(tty,
937 if (port->open_count)
938 tty_flip_buffer_push(tty);
942 /* Resubmit urb so we continue receiving */
943 urb->dev = serial->dev;
945 err = usb_submit_urb(urb, GFP_ATOMIC);
947 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
950 /* not used, usa-49 doesn't have per-port control endpoints */
951 static void usa49_outcont_callback(struct urb *urb)
953 dbg ("%s", __FUNCTION__);
956 static void usa90_indat_callback(struct urb *urb)
960 struct usb_serial_port *port;
961 struct keyspan_port_private *p_priv;
962 struct tty_struct *tty;
963 unsigned char *data = urb->transfer_buffer;
964 int status = urb->status;
966 dbg ("%s", __FUNCTION__);
968 endpoint = usb_pipeendpoint(urb->pipe);
971 dbg("%s - nonzero status: %x on endpoint %d.",
972 __FUNCTION__, status, endpoint);
976 port = (struct usb_serial_port *) urb->context;
977 p_priv = usb_get_serial_port_data(port);
980 if (urb->actual_length) {
982 /* if current mode is DMA, looks like usa28 format
983 otherwise looks like usa26 data format */
985 if (p_priv->baud > 57600) {
986 for (i = 0; i < urb->actual_length ; ++i)
987 tty_insert_flip_char(tty, data[i], 0);
991 /* 0x80 bit is error flag */
992 if ((data[0] & 0x80) == 0) {
993 /* no errors on individual bytes, only possible overrun err*/
994 if (data[0] & RXERROR_OVERRUN)
997 for (i = 1; i < urb->actual_length ; ++i)
998 tty_insert_flip_char(tty, data[i], err);
1002 /* some bytes had errors, every byte has status */
1003 dbg("%s - RX error!!!!", __FUNCTION__);
1004 for (i = 0; i + 1 < urb->actual_length; i += 2) {
1005 int stat = data[i], flag = 0;
1006 if (stat & RXERROR_OVERRUN)
1007 flag |= TTY_OVERRUN;
1008 if (stat & RXERROR_FRAMING)
1010 if (stat & RXERROR_PARITY)
1012 /* XXX should handle break (0x10) */
1013 tty_insert_flip_char(tty, data[i+1], flag);
1017 tty_flip_buffer_push(tty);
1020 /* Resubmit urb so we continue receiving */
1021 urb->dev = port->serial->dev;
1022 if (port->open_count)
1023 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1024 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1030 static void usa90_instat_callback(struct urb *urb)
1032 unsigned char *data = urb->transfer_buffer;
1033 struct keyspan_usa90_portStatusMessage *msg;
1034 struct usb_serial *serial;
1035 struct usb_serial_port *port;
1036 struct keyspan_port_private *p_priv;
1037 int old_dcd_state, err;
1038 int status = urb->status;
1040 serial = (struct usb_serial *) urb->context;
1043 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1046 if (urb->actual_length < 14) {
1047 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
1051 msg = (struct keyspan_usa90_portStatusMessage *)data;
1053 /* Now do something useful with the data */
1055 port = serial->port[0];
1056 p_priv = usb_get_serial_port_data(port);
1058 /* Update handshaking pin state information */
1059 old_dcd_state = p_priv->dcd_state;
1060 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1061 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1062 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1063 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1065 if (port->tty && !C_CLOCAL(port->tty)
1066 && old_dcd_state != p_priv->dcd_state) {
1068 tty_hangup(port->tty);
1070 /* wake_up_interruptible(&p_priv->open_wait); */
1073 /* Resubmit urb so we continue receiving */
1074 urb->dev = serial->dev;
1075 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1076 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1082 static void usa90_outcont_callback(struct urb *urb)
1084 struct usb_serial_port *port;
1085 struct keyspan_port_private *p_priv;
1087 port = (struct usb_serial_port *) urb->context;
1088 p_priv = usb_get_serial_port_data(port);
1090 if (p_priv->resend_cont) {
1091 dbg ("%s - sending setup", __FUNCTION__);
1092 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1096 /* Status messages from the 28xg */
1097 static void usa67_instat_callback(struct urb *urb)
1100 unsigned char *data = urb->transfer_buffer;
1101 struct keyspan_usa67_portStatusMessage *msg;
1102 struct usb_serial *serial;
1103 struct usb_serial_port *port;
1104 struct keyspan_port_private *p_priv;
1106 int status = urb->status;
1108 dbg ("%s", __FUNCTION__);
1110 serial = urb->context;
1113 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1117 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1118 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
1123 /* Now do something useful with the data */
1124 msg = (struct keyspan_usa67_portStatusMessage *)data;
1126 /* Check port number from message and retrieve private data */
1127 if (msg->port >= serial->num_ports) {
1128 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
1132 port = serial->port[msg->port];
1133 p_priv = usb_get_serial_port_data(port);
1135 /* Update handshaking pin state information */
1136 old_dcd_state = p_priv->dcd_state;
1137 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1138 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1140 if (port->tty && !C_CLOCAL(port->tty)
1141 && old_dcd_state != p_priv->dcd_state) {
1143 tty_hangup(port->tty);
1145 /* wake_up_interruptible(&p_priv->open_wait); */
1148 /* Resubmit urb so we continue receiving */
1149 urb->dev = serial->dev;
1150 err = usb_submit_urb(urb, GFP_ATOMIC);
1152 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1155 static void usa67_glocont_callback(struct urb *urb)
1157 struct usb_serial *serial;
1158 struct usb_serial_port *port;
1159 struct keyspan_port_private *p_priv;
1162 dbg ("%s", __FUNCTION__);
1164 serial = urb->context;
1165 for (i = 0; i < serial->num_ports; ++i) {
1166 port = serial->port[i];
1167 p_priv = usb_get_serial_port_data(port);
1169 if (p_priv->resend_cont) {
1170 dbg ("%s - sending setup", __FUNCTION__);
1171 keyspan_usa67_send_setup(serial, port,
1172 p_priv->resend_cont - 1);
1178 static int keyspan_write_room (struct usb_serial_port *port)
1180 struct keyspan_port_private *p_priv;
1181 const struct keyspan_device_details *d_details;
1184 struct urb *this_urb;
1186 dbg("%s", __FUNCTION__);
1187 p_priv = usb_get_serial_port_data(port);
1188 d_details = p_priv->device_details;
1190 if (d_details->msg_format == msg_usa90)
1195 flip = p_priv->out_flip;
1197 /* Check both endpoints to see if any are available. */
1198 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1199 if (this_urb->status != -EINPROGRESS)
1201 flip = (flip + 1) & d_details->outdat_endp_flip;
1202 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1203 if (this_urb->status != -EINPROGRESS)
1210 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1216 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1218 struct keyspan_port_private *p_priv;
1219 struct keyspan_serial_private *s_priv;
1220 struct usb_serial *serial = port->serial;
1221 const struct keyspan_device_details *d_details;
1223 int baud_rate, device_port;
1227 s_priv = usb_get_serial_data(serial);
1228 p_priv = usb_get_serial_port_data(port);
1229 d_details = p_priv->device_details;
1231 dbg("%s - port%d.", __FUNCTION__, port->number);
1233 /* Set some sane defaults */
1234 p_priv->rts_state = 1;
1235 p_priv->dtr_state = 1;
1236 p_priv->baud = 9600;
1238 /* force baud and lcr to be set on open */
1239 p_priv->old_baud = 0;
1240 p_priv->old_cflag = 0;
1242 p_priv->out_flip = 0;
1243 p_priv->in_flip = 0;
1245 /* Reset low level data toggle and start reading from endpoints */
1246 for (i = 0; i < 2; i++) {
1247 if ((urb = p_priv->in_urbs[i]) == NULL)
1249 urb->dev = serial->dev;
1251 /* make sure endpoint data toggle is synchronized with the device */
1253 usb_clear_halt(urb->dev, urb->pipe);
1255 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1256 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1260 /* Reset low level data toggle on out endpoints */
1261 for (i = 0; i < 2; i++) {
1262 if ((urb = p_priv->out_urbs[i]) == NULL)
1264 urb->dev = serial->dev;
1265 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1268 /* get the terminal config for the setup message now so we don't
1269 * need to send 2 of them */
1271 cflag = port->tty->termios->c_cflag;
1272 device_port = port->number - port->serial->minor;
1274 /* Baud rate calculation takes baud rate as an integer
1275 so other rates can be generated if desired. */
1276 baud_rate = tty_get_baud_rate(port->tty);
1277 /* If no match or invalid, leave as default */
1279 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1280 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1281 p_priv->baud = baud_rate;
1284 /* set CTS/RTS handshake etc. */
1285 p_priv->cflag = cflag;
1286 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1288 keyspan_send_setup(port, 1);
1290 //keyspan_set_termios(port, NULL);
1295 static inline void stop_urb(struct urb *urb)
1297 if (urb && urb->status == -EINPROGRESS)
1301 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1304 struct usb_serial *serial = port->serial;
1305 struct keyspan_serial_private *s_priv;
1306 struct keyspan_port_private *p_priv;
1308 dbg("%s", __FUNCTION__);
1309 s_priv = usb_get_serial_data(serial);
1310 p_priv = usb_get_serial_port_data(port);
1312 p_priv->rts_state = 0;
1313 p_priv->dtr_state = 0;
1316 keyspan_send_setup(port, 2);
1317 /* pilot-xfer seems to work best with this delay */
1319 // keyspan_set_termios(port, NULL);
1322 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1323 dbg("%s - urb in progress", __FUNCTION__);
1326 p_priv->out_flip = 0;
1327 p_priv->in_flip = 0;
1330 /* Stop reading/writing urbs */
1331 stop_urb(p_priv->inack_urb);
1332 /* stop_urb(p_priv->outcont_urb); */
1333 for (i = 0; i < 2; i++) {
1334 stop_urb(p_priv->in_urbs[i]);
1335 stop_urb(p_priv->out_urbs[i]);
1342 /* download the firmware to a pre-renumeration device */
1343 static int keyspan_fake_startup (struct usb_serial *serial)
1346 const struct ezusb_hex_record *record;
1349 dbg("Keyspan startup version %04x product %04x",
1350 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1351 le16_to_cpu(serial->dev->descriptor.idProduct));
1353 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1354 dbg("Firmware already loaded. Quitting.");
1358 /* Select firmware image on the basis of idProduct */
1359 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1360 case keyspan_usa28_pre_product_id:
1361 record = &keyspan_usa28_firmware[0];
1365 case keyspan_usa28x_pre_product_id:
1366 record = &keyspan_usa28x_firmware[0];
1370 case keyspan_usa28xa_pre_product_id:
1371 record = &keyspan_usa28xa_firmware[0];
1372 fw_name = "USA28XA";
1375 case keyspan_usa28xb_pre_product_id:
1376 record = &keyspan_usa28xb_firmware[0];
1377 fw_name = "USA28XB";
1380 case keyspan_usa19_pre_product_id:
1381 record = &keyspan_usa19_firmware[0];
1385 case keyspan_usa19qi_pre_product_id:
1386 record = &keyspan_usa19qi_firmware[0];
1387 fw_name = "USA19QI";
1390 case keyspan_mpr_pre_product_id:
1391 record = &keyspan_mpr_firmware[0];
1395 case keyspan_usa19qw_pre_product_id:
1396 record = &keyspan_usa19qw_firmware[0];
1397 fw_name = "USA19QI";
1400 case keyspan_usa18x_pre_product_id:
1401 record = &keyspan_usa18x_firmware[0];
1405 case keyspan_usa19w_pre_product_id:
1406 record = &keyspan_usa19w_firmware[0];
1410 case keyspan_usa49w_pre_product_id:
1411 record = &keyspan_usa49w_firmware[0];
1415 case keyspan_usa49wlc_pre_product_id:
1416 record = &keyspan_usa49wlc_firmware[0];
1417 fw_name = "USA49WLC";
1422 fw_name = "Unknown";
1426 if (record == NULL) {
1427 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1431 dbg("Uploading Keyspan %s firmware.", fw_name);
1433 /* download the firmware image */
1434 response = ezusb_set_reset(serial, 1);
1436 while(record->address != 0xffff) {
1437 response = ezusb_writememory(serial, record->address,
1438 (unsigned char *)record->data,
1439 record->data_size, 0xa0);
1441 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1442 "firmware (%d %04X %p %d)\n",
1444 record->address, record->data, record->data_size);
1449 /* bring device out of reset. Renumeration will occur in a
1450 moment and the new device will bind to the real driver */
1451 response = ezusb_set_reset(serial, 0);
1453 /* we don't want this device to have a driver assigned to it. */
1457 /* Helper functions used by keyspan_setup_urbs */
1458 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1461 struct usb_host_interface *iface_desc;
1462 struct usb_endpoint_descriptor *ep;
1465 iface_desc = serial->interface->cur_altsetting;
1466 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1467 ep = &iface_desc->endpoint[i].desc;
1468 if (ep->bEndpointAddress == endpoint)
1471 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1472 "endpoint %x\n", endpoint);
1476 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1477 int dir, void *ctx, char *buf, int len,
1478 void (*callback)(struct urb *))
1481 struct usb_endpoint_descriptor const *ep_desc;
1482 char const *ep_type_name;
1485 return NULL; /* endpoint not needed */
1487 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1488 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1490 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1494 if (endpoint == 0) {
1495 /* control EP filled in when used */
1499 ep_desc = find_ep(serial, endpoint);
1501 /* leak the urb, something's wrong and the callers don't care */
1504 if (usb_endpoint_xfer_int(ep_desc)) {
1505 ep_type_name = "INT";
1506 usb_fill_int_urb(urb, serial->dev,
1507 usb_sndintpipe(serial->dev, endpoint) | dir,
1508 buf, len, callback, ctx,
1509 ep_desc->bInterval);
1510 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1511 ep_type_name = "BULK";
1512 usb_fill_bulk_urb(urb, serial->dev,
1513 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1514 buf, len, callback, ctx);
1516 dev_warn(&serial->interface->dev,
1517 "unsupported endpoint type %x\n",
1518 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1523 dbg("%s - using urb %p for %s endpoint %x",
1524 __func__, urb, ep_type_name, endpoint);
1528 static struct callbacks {
1529 void (*instat_callback)(struct urb *);
1530 void (*glocont_callback)(struct urb *);
1531 void (*indat_callback)(struct urb *);
1532 void (*outdat_callback)(struct urb *);
1533 void (*inack_callback)(struct urb *);
1534 void (*outcont_callback)(struct urb *);
1535 } keyspan_callbacks[] = {
1537 /* msg_usa26 callbacks */
1538 .instat_callback = usa26_instat_callback,
1539 .glocont_callback = usa26_glocont_callback,
1540 .indat_callback = usa26_indat_callback,
1541 .outdat_callback = usa2x_outdat_callback,
1542 .inack_callback = usa26_inack_callback,
1543 .outcont_callback = usa26_outcont_callback,
1545 /* msg_usa28 callbacks */
1546 .instat_callback = usa28_instat_callback,
1547 .glocont_callback = usa28_glocont_callback,
1548 .indat_callback = usa28_indat_callback,
1549 .outdat_callback = usa2x_outdat_callback,
1550 .inack_callback = usa28_inack_callback,
1551 .outcont_callback = usa28_outcont_callback,
1553 /* msg_usa49 callbacks */
1554 .instat_callback = usa49_instat_callback,
1555 .glocont_callback = usa49_glocont_callback,
1556 .indat_callback = usa49_indat_callback,
1557 .outdat_callback = usa2x_outdat_callback,
1558 .inack_callback = usa49_inack_callback,
1559 .outcont_callback = usa49_outcont_callback,
1561 /* msg_usa90 callbacks */
1562 .instat_callback = usa90_instat_callback,
1563 .glocont_callback = usa28_glocont_callback,
1564 .indat_callback = usa90_indat_callback,
1565 .outdat_callback = usa2x_outdat_callback,
1566 .inack_callback = usa28_inack_callback,
1567 .outcont_callback = usa90_outcont_callback,
1569 /* msg_usa67 callbacks */
1570 .instat_callback = usa67_instat_callback,
1571 .glocont_callback = usa67_glocont_callback,
1572 .indat_callback = usa26_indat_callback,
1573 .outdat_callback = usa2x_outdat_callback,
1574 .inack_callback = usa26_inack_callback,
1575 .outcont_callback = usa26_outcont_callback,
1579 /* Generic setup urbs function that uses
1580 data in device_details */
1581 static void keyspan_setup_urbs(struct usb_serial *serial)
1584 struct keyspan_serial_private *s_priv;
1585 const struct keyspan_device_details *d_details;
1586 struct usb_serial_port *port;
1587 struct keyspan_port_private *p_priv;
1588 struct callbacks *cback;
1591 dbg ("%s", __FUNCTION__);
1593 s_priv = usb_get_serial_data(serial);
1594 d_details = s_priv->device_details;
1596 /* Setup values for the various callback routines */
1597 cback = &keyspan_callbacks[d_details->msg_format];
1599 /* Allocate and set up urbs for each one that is in use,
1600 starting with instat endpoints */
1601 s_priv->instat_urb = keyspan_setup_urb
1602 (serial, d_details->instat_endpoint, USB_DIR_IN,
1603 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1604 cback->instat_callback);
1606 s_priv->indat_urb = keyspan_setup_urb
1607 (serial, d_details->indat_endpoint, USB_DIR_IN,
1608 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1609 usa49wg_indat_callback);
1611 s_priv->glocont_urb = keyspan_setup_urb
1612 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1613 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1614 cback->glocont_callback);
1616 /* Setup endpoints for each port specific thing */
1617 for (i = 0; i < d_details->num_ports; i ++) {
1618 port = serial->port[i];
1619 p_priv = usb_get_serial_port_data(port);
1621 /* Do indat endpoints first, once for each flip */
1622 endp = d_details->indat_endpoints[i];
1623 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1624 p_priv->in_urbs[j] = keyspan_setup_urb
1625 (serial, endp, USB_DIR_IN, port,
1626 p_priv->in_buffer[j], 64,
1627 cback->indat_callback);
1630 p_priv->in_urbs[j] = NULL;
1632 /* outdat endpoints also have flip */
1633 endp = d_details->outdat_endpoints[i];
1634 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1635 p_priv->out_urbs[j] = keyspan_setup_urb
1636 (serial, endp, USB_DIR_OUT, port,
1637 p_priv->out_buffer[j], 64,
1638 cback->outdat_callback);
1641 p_priv->out_urbs[j] = NULL;
1643 /* inack endpoint */
1644 p_priv->inack_urb = keyspan_setup_urb
1645 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1646 port, p_priv->inack_buffer, 1, cback->inack_callback);
1648 /* outcont endpoint */
1649 p_priv->outcont_urb = keyspan_setup_urb
1650 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1651 port, p_priv->outcont_buffer, 64,
1652 cback->outcont_callback);
1657 /* usa19 function doesn't require prescaler */
1658 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1659 u8 *rate_low, u8 *prescaler, int portnum)
1661 u32 b16, /* baud rate times 16 (actual rate used internally) */
1663 cnt; /* inverse of divisor (programmed into 8051) */
1665 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1667 /* prevent divide by zero... */
1668 if( (b16 = (baud_rate * 16L)) == 0) {
1669 return (KEYSPAN_INVALID_BAUD_RATE);
1672 /* Any "standard" rate over 57k6 is marginal on the USA-19
1673 as we run out of divisor resolution. */
1674 if (baud_rate > 57600) {
1675 return (KEYSPAN_INVALID_BAUD_RATE);
1678 /* calculate the divisor and the counter (its inverse) */
1679 if( (div = (baudclk / b16)) == 0) {
1680 return (KEYSPAN_INVALID_BAUD_RATE);
1687 return (KEYSPAN_INVALID_BAUD_RATE);
1690 /* return the counter values if non-null */
1692 *rate_low = (u8) (cnt & 0xff);
1695 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1697 if (rate_low && rate_hi) {
1698 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1701 return (KEYSPAN_BAUD_RATE_OK);
1704 /* usa19hs function doesn't require prescaler */
1705 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1706 u8 *rate_low, u8 *prescaler, int portnum)
1708 u32 b16, /* baud rate times 16 (actual rate used internally) */
1711 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1713 /* prevent divide by zero... */
1714 if( (b16 = (baud_rate * 16L)) == 0)
1715 return (KEYSPAN_INVALID_BAUD_RATE);
1719 /* calculate the divisor */
1720 if( (div = (baudclk / b16)) == 0)
1721 return (KEYSPAN_INVALID_BAUD_RATE);
1724 return (KEYSPAN_INVALID_BAUD_RATE);
1726 /* return the counter values if non-null */
1728 *rate_low = (u8) (div & 0xff);
1731 *rate_hi = (u8) ((div >> 8) & 0xff);
1733 if (rate_low && rate_hi)
1734 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1736 return (KEYSPAN_BAUD_RATE_OK);
1739 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1740 u8 *rate_low, u8 *prescaler, int portnum)
1742 u32 b16, /* baud rate times 16 (actual rate used internally) */
1743 clk, /* clock with 13/8 prescaler */
1744 div, /* divisor using 13/8 prescaler */
1745 res, /* resulting baud rate using 13/8 prescaler */
1746 diff, /* error using 13/8 prescaler */
1751 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1753 /* prevent divide by zero */
1754 if( (b16 = baud_rate * 16L) == 0) {
1755 return (KEYSPAN_INVALID_BAUD_RATE);
1758 /* Calculate prescaler by trying them all and looking
1761 /* start with largest possible difference */
1762 smallest_diff = 0xffffffff;
1764 /* 0 is an invalid prescaler, used as a flag */
1767 for(i = 8; i <= 0xff; ++i) {
1768 clk = (baudclk * 8) / (u32) i;
1770 if( (div = clk / b16) == 0) {
1775 diff= (res > b16) ? (res-b16) : (b16-res);
1777 if(diff < smallest_diff) {
1779 smallest_diff = diff;
1783 if(best_prescaler == 0) {
1784 return (KEYSPAN_INVALID_BAUD_RATE);
1787 clk = (baudclk * 8) / (u32) best_prescaler;
1790 /* return the divisor and prescaler if non-null */
1792 *rate_low = (u8) (div & 0xff);
1795 *rate_hi = (u8) ((div >> 8) & 0xff);
1798 *prescaler = best_prescaler;
1799 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1801 return (KEYSPAN_BAUD_RATE_OK);
1804 /* USA-28 supports different maximum baud rates on each port */
1805 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1806 u8 *rate_low, u8 *prescaler, int portnum)
1808 u32 b16, /* baud rate times 16 (actual rate used internally) */
1810 cnt; /* inverse of divisor (programmed into 8051) */
1812 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1814 /* prevent divide by zero */
1815 if ((b16 = baud_rate * 16L) == 0)
1816 return (KEYSPAN_INVALID_BAUD_RATE);
1818 /* calculate the divisor and the counter (its inverse) */
1819 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1820 return (KEYSPAN_INVALID_BAUD_RATE);
1826 /* check for out of range, based on portnum,
1827 and return result */
1830 return (KEYSPAN_INVALID_BAUD_RATE);
1835 return (KEYSPAN_INVALID_BAUD_RATE);
1839 return (KEYSPAN_INVALID_BAUD_RATE);
1843 /* return the counter values if not NULL
1844 (port 1 will ignore retHi) */
1846 *rate_low = (u8) (cnt & 0xff);
1849 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1851 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1852 return (KEYSPAN_BAUD_RATE_OK);
1855 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1856 struct usb_serial_port *port,
1859 struct keyspan_usa26_portControlMessage msg;
1860 struct keyspan_serial_private *s_priv;
1861 struct keyspan_port_private *p_priv;
1862 const struct keyspan_device_details *d_details;
1864 struct urb *this_urb;
1865 int device_port, err;
1867 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1869 s_priv = usb_get_serial_data(serial);
1870 p_priv = usb_get_serial_port_data(port);
1871 d_details = s_priv->device_details;
1872 device_port = port->number - port->serial->minor;
1874 outcont_urb = d_details->outcont_endpoints[port->number];
1875 this_urb = p_priv->outcont_urb;
1877 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1879 /* Make sure we have an urb then send the message */
1880 if (this_urb == NULL) {
1881 dbg("%s - oops no urb.", __FUNCTION__);
1885 /* Save reset port val for resend.
1886 Don't overwrite resend for open/close condition. */
1887 if ((reset_port + 1) > p_priv->resend_cont)
1888 p_priv->resend_cont = reset_port + 1;
1889 if (this_urb->status == -EINPROGRESS) {
1890 /* dbg ("%s - already writing", __FUNCTION__); */
1895 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1897 /* Only set baud rate if it's changed */
1898 if (p_priv->old_baud != p_priv->baud) {
1899 p_priv->old_baud = p_priv->baud;
1900 msg.setClocking = 0xff;
1901 if (d_details->calculate_baud_rate
1902 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1903 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1904 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1907 msg.baudHi = 125; /* Values for 9600 baud */
1910 msg.setPrescaler = 0xff;
1913 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1914 switch (p_priv->cflag & CSIZE) {
1916 msg.lcr |= USA_DATABITS_5;
1919 msg.lcr |= USA_DATABITS_6;
1922 msg.lcr |= USA_DATABITS_7;
1925 msg.lcr |= USA_DATABITS_8;
1928 if (p_priv->cflag & PARENB) {
1929 /* note USA_PARITY_NONE == 0 */
1930 msg.lcr |= (p_priv->cflag & PARODD)?
1931 USA_PARITY_ODD: USA_PARITY_EVEN;
1935 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1936 msg.xonFlowControl = 0;
1937 msg.setFlowControl = 0xff;
1938 msg.forwardingLength = 16;
1943 if (reset_port == 1) {
1952 msg.returnStatus = 0;
1953 msg.resetDataToggle = 0xff;
1957 else if (reset_port == 2) {
1966 msg.returnStatus = 0;
1967 msg.resetDataToggle = 0;
1970 /* Sending intermediate configs */
1972 msg._txOn = (! p_priv->break_on);
1975 msg.txBreak = (p_priv->break_on);
1980 msg.returnStatus = 0;
1981 msg.resetDataToggle = 0x0;
1984 /* Do handshaking outputs */
1985 msg.setTxTriState_setRts = 0xff;
1986 msg.txTriState_rts = p_priv->rts_state;
1988 msg.setHskoa_setDtr = 0xff;
1989 msg.hskoa_dtr = p_priv->dtr_state;
1991 p_priv->resend_cont = 0;
1992 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1994 /* send the data out the device on control endpoint */
1995 this_urb->transfer_buffer_length = sizeof(msg);
1997 this_urb->dev = serial->dev;
1998 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1999 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2003 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
2004 outcont_urb, this_urb->transfer_buffer_length,
2005 usb_pipeendpoint(this_urb->pipe));
2012 static int keyspan_usa28_send_setup(struct usb_serial *serial,
2013 struct usb_serial_port *port,
2016 struct keyspan_usa28_portControlMessage msg;
2017 struct keyspan_serial_private *s_priv;
2018 struct keyspan_port_private *p_priv;
2019 const struct keyspan_device_details *d_details;
2020 struct urb *this_urb;
2021 int device_port, err;
2023 dbg ("%s", __FUNCTION__);
2025 s_priv = usb_get_serial_data(serial);
2026 p_priv = usb_get_serial_port_data(port);
2027 d_details = s_priv->device_details;
2028 device_port = port->number - port->serial->minor;
2030 /* only do something if we have a bulk out endpoint */
2031 if ((this_urb = p_priv->outcont_urb) == NULL) {
2032 dbg("%s - oops no urb.", __FUNCTION__);
2036 /* Save reset port val for resend.
2037 Don't overwrite resend for open/close condition. */
2038 if ((reset_port + 1) > p_priv->resend_cont)
2039 p_priv->resend_cont = reset_port + 1;
2040 if (this_urb->status == -EINPROGRESS) {
2041 dbg ("%s already writing", __FUNCTION__);
2046 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2048 msg.setBaudRate = 1;
2049 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2050 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2051 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
2053 msg.baudHi = 0xb2; /* Values for 9600 baud */
2056 /* If parity is enabled, we must calculate it ourselves. */
2057 msg.parity = 0; /* XXX for now */
2059 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2060 msg.xonFlowControl = 0;
2062 /* Do handshaking outputs, DTR is inverted relative to RTS */
2063 msg.rts = p_priv->rts_state;
2064 msg.dtr = p_priv->dtr_state;
2066 msg.forwardingLength = 16;
2068 msg.breakThreshold = 45;
2072 /*msg.returnStatus = 1;
2073 msg.resetDataToggle = 0xff;*/
2075 if (reset_port == 1) {
2079 msg.txForceXoff = 0;
2085 msg.returnStatus = 0;
2086 msg.resetDataToggle = 0xff;
2089 else if (reset_port == 2) {
2093 msg.txForceXoff = 0;
2099 msg.returnStatus = 0;
2100 msg.resetDataToggle = 0;
2102 /* Sending intermediate configs */
2104 msg._txOn = (! p_priv->break_on);
2107 msg.txForceXoff = 0;
2108 msg.txBreak = (p_priv->break_on);
2113 msg.returnStatus = 0;
2114 msg.resetDataToggle = 0x0;
2117 p_priv->resend_cont = 0;
2118 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2120 /* send the data out the device on control endpoint */
2121 this_urb->transfer_buffer_length = sizeof(msg);
2123 this_urb->dev = serial->dev;
2124 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2125 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
2129 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
2130 this_urb->transfer_buffer_length);
2137 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2138 struct usb_serial_port *port,
2141 struct keyspan_usa49_portControlMessage msg;
2142 struct usb_ctrlrequest *dr = NULL;
2143 struct keyspan_serial_private *s_priv;
2144 struct keyspan_port_private *p_priv;
2145 const struct keyspan_device_details *d_details;
2146 struct urb *this_urb;
2147 int err, device_port;
2149 dbg ("%s", __FUNCTION__);
2151 s_priv = usb_get_serial_data(serial);
2152 p_priv = usb_get_serial_port_data(port);
2153 d_details = s_priv->device_details;
2155 this_urb = s_priv->glocont_urb;
2157 /* Work out which port within the device is being setup */
2158 device_port = port->number - port->serial->minor;
2160 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2162 /* Make sure we have an urb then send the message */
2163 if (this_urb == NULL) {
2164 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
2168 /* Save reset port val for resend.
2169 Don't overwrite resend for open/close condition. */
2170 if ((reset_port + 1) > p_priv->resend_cont)
2171 p_priv->resend_cont = reset_port + 1;
2173 if (this_urb->status == -EINPROGRESS) {
2174 /* dbg ("%s - already writing", __FUNCTION__); */
2179 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2181 /*msg.portNumber = port->number;*/
2182 msg.portNumber = device_port;
2184 /* Only set baud rate if it's changed */
2185 if (p_priv->old_baud != p_priv->baud) {
2186 p_priv->old_baud = p_priv->baud;
2187 msg.setClocking = 0xff;
2188 if (d_details->calculate_baud_rate
2189 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2190 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2191 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2194 msg.baudHi = 125; /* Values for 9600 baud */
2197 //msg.setPrescaler = 0xff;
2200 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2201 switch (p_priv->cflag & CSIZE) {
2203 msg.lcr |= USA_DATABITS_5;
2206 msg.lcr |= USA_DATABITS_6;
2209 msg.lcr |= USA_DATABITS_7;
2212 msg.lcr |= USA_DATABITS_8;
2215 if (p_priv->cflag & PARENB) {
2216 /* note USA_PARITY_NONE == 0 */
2217 msg.lcr |= (p_priv->cflag & PARODD)?
2218 USA_PARITY_ODD: USA_PARITY_EVEN;
2222 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2223 msg.xonFlowControl = 0;
2224 msg.setFlowControl = 0xff;
2226 msg.forwardingLength = 16;
2231 if (reset_port == 1) {
2240 msg.returnStatus = 0;
2241 msg.resetDataToggle = 0xff;
2243 msg.disablePort = 0;
2246 else if (reset_port == 2) {
2255 msg.returnStatus = 0;
2256 msg.resetDataToggle = 0;
2258 msg.disablePort = 1;
2260 /* Sending intermediate configs */
2262 msg._txOn = (! p_priv->break_on);
2265 msg.txBreak = (p_priv->break_on);
2270 msg.returnStatus = 0;
2271 msg.resetDataToggle = 0x0;
2273 msg.disablePort = 0;
2276 /* Do handshaking outputs */
2278 msg.rts = p_priv->rts_state;
2281 msg.dtr = p_priv->dtr_state;
2283 p_priv->resend_cont = 0;
2285 /* if the device is a 49wg, we send control message on usb control EP 0 */
2287 if (d_details->product_id == keyspan_usa49wg_product_id) {
2288 dr = (void *)(s_priv->ctrl_buf);
2289 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2290 dr->bRequest = 0xB0; /* 49wg control message */;
2293 dr->wLength = cpu_to_le16(sizeof(msg));
2295 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2297 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2298 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2299 usa49_glocont_callback, serial);
2302 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2304 /* send the data out the device on control endpoint */
2305 this_urb->transfer_buffer_length = sizeof(msg);
2307 this_urb->dev = serial->dev;
2309 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2310 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2314 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2315 outcont_urb, this_urb->transfer_buffer_length,
2316 usb_pipeendpoint(this_urb->pipe));
2323 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2324 struct usb_serial_port *port,
2327 struct keyspan_usa90_portControlMessage msg;
2328 struct keyspan_serial_private *s_priv;
2329 struct keyspan_port_private *p_priv;
2330 const struct keyspan_device_details *d_details;
2331 struct urb *this_urb;
2335 dbg ("%s", __FUNCTION__);
2337 s_priv = usb_get_serial_data(serial);
2338 p_priv = usb_get_serial_port_data(port);
2339 d_details = s_priv->device_details;
2341 /* only do something if we have a bulk out endpoint */
2342 if ((this_urb = p_priv->outcont_urb) == NULL) {
2343 dbg("%s - oops no urb.", __FUNCTION__);
2347 /* Save reset port val for resend.
2348 Don't overwrite resend for open/close condition. */
2349 if ((reset_port + 1) > p_priv->resend_cont)
2350 p_priv->resend_cont = reset_port + 1;
2351 if (this_urb->status == -EINPROGRESS) {
2352 dbg ("%s already writing", __FUNCTION__);
2357 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2359 /* Only set baud rate if it's changed */
2360 if (p_priv->old_baud != p_priv->baud) {
2361 p_priv->old_baud = p_priv->baud;
2362 msg.setClocking = 0x01;
2363 if (d_details->calculate_baud_rate
2364 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2365 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2366 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2368 p_priv->baud = 9600;
2369 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2370 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2376 /* modes must always be correctly specified */
2377 if (p_priv->baud > 57600)
2379 msg.rxMode = RXMODE_DMA;
2380 msg.txMode = TXMODE_DMA;
2384 msg.rxMode = RXMODE_BYHAND;
2385 msg.txMode = TXMODE_BYHAND;
2388 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2389 switch (p_priv->cflag & CSIZE) {
2391 msg.lcr |= USA_DATABITS_5;
2394 msg.lcr |= USA_DATABITS_6;
2397 msg.lcr |= USA_DATABITS_7;
2400 msg.lcr |= USA_DATABITS_8;
2403 if (p_priv->cflag & PARENB) {
2404 /* note USA_PARITY_NONE == 0 */
2405 msg.lcr |= (p_priv->cflag & PARODD)?
2406 USA_PARITY_ODD: USA_PARITY_EVEN;
2408 if (p_priv->old_cflag != p_priv->cflag) {
2409 p_priv->old_cflag = p_priv->cflag;
2413 if (p_priv->flow_control == flow_cts)
2414 msg.txFlowControl = TXFLOW_CTS;
2415 msg.setTxFlowControl = 0x01;
2416 msg.setRxFlowControl = 0x01;
2418 msg.rxForwardingLength = 16;
2419 msg.rxForwardingTimeout = 16;
2420 msg.txAckSetting = 0;
2425 if (reset_port == 1) {
2426 msg.portEnabled = 1;
2428 msg.txBreak = (p_priv->break_on);
2431 else if (reset_port == 2) {
2432 msg.portEnabled = 0;
2434 /* Sending intermediate configs */
2436 if (port->open_count)
2437 msg.portEnabled = 1;
2438 msg.txBreak = (p_priv->break_on);
2441 /* Do handshaking outputs */
2443 msg.rts = p_priv->rts_state;
2446 msg.dtr = p_priv->dtr_state;
2448 p_priv->resend_cont = 0;
2449 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2451 /* send the data out the device on control endpoint */
2452 this_urb->transfer_buffer_length = sizeof(msg);
2454 this_urb->dev = serial->dev;
2455 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2456 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2461 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2462 struct usb_serial_port *port,
2465 struct keyspan_usa67_portControlMessage msg;
2466 struct keyspan_serial_private *s_priv;
2467 struct keyspan_port_private *p_priv;
2468 const struct keyspan_device_details *d_details;
2469 struct urb *this_urb;
2470 int err, device_port;
2472 dbg ("%s", __FUNCTION__);
2474 s_priv = usb_get_serial_data(serial);
2475 p_priv = usb_get_serial_port_data(port);
2476 d_details = s_priv->device_details;
2478 this_urb = s_priv->glocont_urb;
2480 /* Work out which port within the device is being setup */
2481 device_port = port->number - port->serial->minor;
2483 /* Make sure we have an urb then send the message */
2484 if (this_urb == NULL) {
2485 dbg("%s - oops no urb for port %d.", __FUNCTION__,
2490 /* Save reset port val for resend.
2491 Don't overwrite resend for open/close condition. */
2492 if ((reset_port + 1) > p_priv->resend_cont)
2493 p_priv->resend_cont = reset_port + 1;
2494 if (this_urb->status == -EINPROGRESS) {
2495 /* dbg ("%s - already writing", __FUNCTION__); */
2500 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2502 msg.port = device_port;
2504 /* Only set baud rate if it's changed */
2505 if (p_priv->old_baud != p_priv->baud) {
2506 p_priv->old_baud = p_priv->baud;
2507 msg.setClocking = 0xff;
2508 if (d_details->calculate_baud_rate
2509 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2510 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2511 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2514 msg.baudHi = 125; /* Values for 9600 baud */
2517 msg.setPrescaler = 0xff;
2520 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2521 switch (p_priv->cflag & CSIZE) {
2523 msg.lcr |= USA_DATABITS_5;
2526 msg.lcr |= USA_DATABITS_6;
2529 msg.lcr |= USA_DATABITS_7;
2532 msg.lcr |= USA_DATABITS_8;
2535 if (p_priv->cflag & PARENB) {
2536 /* note USA_PARITY_NONE == 0 */
2537 msg.lcr |= (p_priv->cflag & PARODD)?
2538 USA_PARITY_ODD: USA_PARITY_EVEN;
2542 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2543 msg.xonFlowControl = 0;
2544 msg.setFlowControl = 0xff;
2545 msg.forwardingLength = 16;
2549 if (reset_port == 1) {
2559 msg.returnStatus = 0;
2560 msg.resetDataToggle = 0xff;
2561 } else if (reset_port == 2) {
2571 msg.returnStatus = 0;
2572 msg.resetDataToggle = 0;
2574 /* Sending intermediate configs */
2575 msg._txOn = (! p_priv->break_on);
2578 msg.txBreak = (p_priv->break_on);
2583 msg.returnStatus = 0;
2584 msg.resetDataToggle = 0x0;
2587 /* Do handshaking outputs */
2588 msg.setTxTriState_setRts = 0xff;
2589 msg.txTriState_rts = p_priv->rts_state;
2591 msg.setHskoa_setDtr = 0xff;
2592 msg.hskoa_dtr = p_priv->dtr_state;
2594 p_priv->resend_cont = 0;
2596 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2598 /* send the data out the device on control endpoint */
2599 this_urb->transfer_buffer_length = sizeof(msg);
2600 this_urb->dev = serial->dev;
2602 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2604 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__,
2609 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2611 struct usb_serial *serial = port->serial;
2612 struct keyspan_serial_private *s_priv;
2613 const struct keyspan_device_details *d_details;
2615 dbg ("%s", __FUNCTION__);
2617 s_priv = usb_get_serial_data(serial);
2618 d_details = s_priv->device_details;
2620 switch (d_details->msg_format) {
2622 keyspan_usa26_send_setup(serial, port, reset_port);
2625 keyspan_usa28_send_setup(serial, port, reset_port);
2628 keyspan_usa49_send_setup(serial, port, reset_port);
2631 keyspan_usa90_send_setup(serial, port, reset_port);
2634 keyspan_usa67_send_setup(serial, port, reset_port);
2640 /* Gets called by the "real" driver (ie once firmware is loaded
2641 and renumeration has taken place. */
2642 static int keyspan_startup (struct usb_serial *serial)
2645 struct usb_serial_port *port;
2646 struct keyspan_serial_private *s_priv;
2647 struct keyspan_port_private *p_priv;
2648 const struct keyspan_device_details *d_details;
2650 dbg("%s", __FUNCTION__);
2652 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2653 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2655 if (d_details == NULL) {
2656 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2660 /* Setup private data for serial driver */
2661 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2663 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2667 s_priv->device_details = d_details;
2668 usb_set_serial_data(serial, s_priv);
2670 /* Now setup per port private data */
2671 for (i = 0; i < serial->num_ports; i++) {
2672 port = serial->port[i];
2673 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2675 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2678 p_priv->device_details = d_details;
2679 usb_set_serial_port_data(port, p_priv);
2682 keyspan_setup_urbs(serial);
2684 if (s_priv->instat_urb != NULL) {
2685 s_priv->instat_urb->dev = serial->dev;
2686 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2688 dbg("%s - submit instat urb failed %d", __FUNCTION__,
2691 if (s_priv->indat_urb != NULL) {
2692 s_priv->indat_urb->dev = serial->dev;
2693 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2695 dbg("%s - submit indat urb failed %d", __FUNCTION__,
2702 static void keyspan_shutdown (struct usb_serial *serial)
2705 struct usb_serial_port *port;
2706 struct keyspan_serial_private *s_priv;
2707 struct keyspan_port_private *p_priv;
2709 dbg("%s", __FUNCTION__);
2711 s_priv = usb_get_serial_data(serial);
2713 /* Stop reading/writing urbs */
2714 stop_urb(s_priv->instat_urb);
2715 stop_urb(s_priv->glocont_urb);
2716 stop_urb(s_priv->indat_urb);
2717 for (i = 0; i < serial->num_ports; ++i) {
2718 port = serial->port[i];
2719 p_priv = usb_get_serial_port_data(port);
2720 stop_urb(p_priv->inack_urb);
2721 stop_urb(p_priv->outcont_urb);
2722 for (j = 0; j < 2; j++) {
2723 stop_urb(p_priv->in_urbs[j]);
2724 stop_urb(p_priv->out_urbs[j]);
2729 usb_free_urb(s_priv->instat_urb);
2730 usb_free_urb(s_priv->indat_urb);
2731 usb_free_urb(s_priv->glocont_urb);
2732 for (i = 0; i < serial->num_ports; ++i) {
2733 port = serial->port[i];
2734 p_priv = usb_get_serial_port_data(port);
2735 usb_free_urb(p_priv->inack_urb);
2736 usb_free_urb(p_priv->outcont_urb);
2737 for (j = 0; j < 2; j++) {
2738 usb_free_urb(p_priv->in_urbs[j]);
2739 usb_free_urb(p_priv->out_urbs[j]);
2743 /* dbg("Freeing serial->private."); */
2746 /* dbg("Freeing port->private."); */
2747 /* Now free per port private data */
2748 for (i = 0; i < serial->num_ports; i++) {
2749 port = serial->port[i];
2750 kfree(usb_get_serial_port_data(port));
2754 MODULE_AUTHOR( DRIVER_AUTHOR );
2755 MODULE_DESCRIPTION( DRIVER_DESC );
2756 MODULE_LICENSE("GPL");
2758 module_param(debug, bool, S_IRUGO | S_IWUSR);
2759 MODULE_PARM_DESC(debug, "Debug enabled or not");