Code in this driver inspired by and in a number of places taken from Brian Warner's original Keyspan-PDA driver.
This driver has been put together with the support of Innosys, Inc. and Keyspan, Inc the manufacturers of the Keyspan USB-serial products. Thanks Guys :)
Thanks to Paulus for miscellaneous tidy ups, some largish chunks of much nicer and/or completely new code and (perhaps most uniquely) having the patience to sit down and explain why and where he'd changed stuff.
Tip 'o the hat to IBM (and previously Linuxcare :) for supporting staff in their work on open source projects.
*/
/* * Product IDs post-renumeration. Note that the 28x and 28xb have the same * id's post-renumeration but behave identically so it's not an issue. As * such, the 28xb is not listed in any of the device tables.
*/ #define keyspan_usa18x_product_id 0x0112 #define keyspan_usa19_product_id 0x0107 #define keyspan_usa19qi_product_id 0x010c #define keyspan_usa19hs_product_id 0x0121 #define keyspan_mpr_product_id 0x011c #define keyspan_usa19qw_product_id 0x0119 #define keyspan_usa19w_product_id 0x0108 #define keyspan_usa28_product_id 0x010f #define keyspan_usa28x_product_id 0x0110 #define keyspan_usa28xa_product_id 0x0115 #define keyspan_usa28xb_product_id 0x0110 #define keyspan_usa28xg_product_id 0x0135 #define keyspan_usa49w_product_id 0x010a #define keyspan_usa49wlc_product_id 0x012a #define keyspan_usa49wg_product_id 0x0131
struct keyspan_device_details { /* product ID value */ int product_id;
/* Per device and per port private data */ struct keyspan_serial_private { conststruct keyspan_device_details *device_details;
struct urb *instat_urb; char *instat_buf;
/* added to support 49wg, where data from all 4 ports comes in
on 1 EP and high-speed supported */ struct urb *indat_urb; char *indat_buf;
/* XXX this one probably will need a lock */ struct urb *glocont_urb; char *glocont_buf; char *ctrl_buf; /* for EP0 control message */
};
struct keyspan_port_private { /* Keep track of which input & output endpoints to use */ int in_flip; int out_flip;
/* Keep duplicate of device details in each port structure as well - simplifies some of the
callback functions etc. */ conststruct keyspan_device_details *device_details;
/* Input endpoints and buffer for this port */ struct urb *in_urbs[2]; char *in_buffer[2]; /* Output endpoints and buffer for this port */ struct urb *out_urbs[2]; char *out_buffer[2];
/* Output control endpoint */ struct urb *outcont_urb; char *outcont_buffer;
/* Settings for the port */ int baud; int old_baud; unsignedint cflag; unsignedint old_cflag; enum {flow_none, flow_cts, flow_xon} flow_control; int rts_state; /* Handshaking pins (outputs) */ int dtr_state; int cts_state; /* Handshaking pins (inputs) */ int dsr_state; int dcd_state; int ri_state; int break_on;
unsignedlong tx_start_time[2]; int resend_cont; /* need to resend control packet */
};
/* Include Keyspan message headers. All current Keyspan Adapters make use of one of five message formats which are referred to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
within this driver. */ #include"keyspan_usa26msg.h" #include"keyspan_usa28msg.h" #include"keyspan_usa49msg.h" #include"keyspan_usa90msg.h" #include"keyspan_usa67msg.h"
/* Baud rate calculation takes baud rate as an integer
so other rates can be generated if desired. */
baud_rate = tty_get_baud_rate(tty); /* If no match or invalid, don't change */ if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) { /* FIXME - more to do here to ensure rate changes cleanly */ /* FIXME - calculate exact rate from divisor ? */
p_priv->baud = baud_rate;
} else
baud_rate = tty_termios_baud_rate(old_termios);
if (set & TIOCM_RTS)
p_priv->rts_state = 1; if (set & TIOCM_DTR)
p_priv->dtr_state = 1; if (clear & TIOCM_RTS)
p_priv->rts_state = 0; if (clear & TIOCM_DTR)
p_priv->dtr_state = 0;
keyspan_send_setup(port, 0); return 0;
}
/* Write function is similar for the four protocols used
with only a minor change for usa90 (usa19hs) required */ staticint keyspan_write(struct tty_struct *tty, struct usb_serial_port *port, constunsignedchar *buf, int count)
{ struct keyspan_port_private *p_priv; conststruct keyspan_device_details *d_details; int flip; int left, todo; struct urb *this_urb; int err, maxDataLen, dataOffset;
for (left = count; left > 0; left -= todo) {
todo = left; if (todo > maxDataLen)
todo = maxDataLen;
flip = p_priv->out_flip;
/* Check we have a valid urb/endpoint before we use it... */
this_urb = p_priv->out_urbs[flip]; if (this_urb == NULL) { /* no bulk out, so return 0 bytes written */
dev_dbg(&port->dev, "%s - no output urb :(\n", __func__); return count;
}
/* Flip for next time if usa26 or usa28 interface
(not used on usa49) */
p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
}
return count - left;
}
staticvoid usa26_indat_callback(struct urb *urb)
{ int i, err; int endpoint; struct usb_serial_port *port; unsignedchar *data = urb->transfer_buffer; int status = urb->status;
endpoint = usb_pipeendpoint(urb->pipe);
if (status) {
dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
__func__, status, endpoint); return;
}
port = urb->context; if (urb->actual_length) { /* 0x80 bit is error flag */ if ((data[0] & 0x80) == 0) { /* no errors on individual bytes, only
possible overrun err */ if (data[0] & RXERROR_OVERRUN) {
tty_insert_flip_char(&port->port, 0,
TTY_OVERRUN);
} for (i = 1; i < urb->actual_length ; ++i)
tty_insert_flip_char(&port->port, data[i],
TTY_NORMAL);
} else { /* some bytes had errors, every byte has status */
dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__); for (i = 0; i + 1 < urb->actual_length; i += 2) { int stat = data[i]; int flag = TTY_NORMAL;
if (stat & RXERROR_OVERRUN) {
tty_insert_flip_char(&port->port, 0,
TTY_OVERRUN);
} /* XXX should handle break (0x10) */ if (stat & RXERROR_PARITY)
flag = TTY_PARITY; elseif (stat & RXERROR_FRAMING)
flag = TTY_FRAME;
/* Resubmit urb so we continue receiving */
err = usb_submit_urb(urb, GFP_ATOMIC); if (err != 0)
dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
}
/* Outdat handling is common for all devices */ staticvoid usa2x_outdat_callback(struct urb *urb)
{ struct usb_serial_port *port; struct keyspan_port_private *p_priv;
/* Check port number from message and retrieve private data */ if (msg->port >= serial->num_ports) {
dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port); gotoexit;
}
port = serial->port[msg->port];
p_priv = usb_get_serial_port_data(port); if (!p_priv) goto resubmit;
/* Check port number from message and retrieve private data */ if (msg->port >= serial->num_ports) {
dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port); gotoexit;
}
port = serial->port[msg->port];
p_priv = usb_get_serial_port_data(port); if (!p_priv) goto resubmit;
serial = urb->context; for (i = 0; i < serial->num_ports; ++i) {
port = serial->port[i];
p_priv = usb_get_serial_port_data(port); if (!p_priv) continue;
/* This is actually called glostat in the Keyspan
doco */ staticvoid usa49_instat_callback(struct urb *urb)
{ int err; unsignedchar *data = urb->transfer_buffer; struct keyspan_usa49_portStatusMessage *msg; struct usb_serial *serial; struct usb_serial_port *port; struct keyspan_port_private *p_priv; int old_dcd_state; int status = urb->status;
/* Check port number from message and retrieve private data */ if (msg->portNumber >= serial->num_ports) {
dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
__func__, msg->portNumber); gotoexit;
}
port = serial->port[msg->portNumber];
p_priv = usb_get_serial_port_data(port); if (!p_priv) goto resubmit;
staticvoid usa49_indat_callback(struct urb *urb)
{ int i, err; int endpoint; struct usb_serial_port *port; unsignedchar *data = urb->transfer_buffer; int status = urb->status;
endpoint = usb_pipeendpoint(urb->pipe);
if (status) {
dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
__func__, status, endpoint); return;
}
port = urb->context; if (urb->actual_length) { /* 0x80 bit is error flag */ if ((data[0] & 0x80) == 0) { /* no error on any byte */
tty_insert_flip_string(&port->port, data + 1,
urb->actual_length - 1);
} else { /* some bytes had errors, every byte has status */ for (i = 0; i + 1 < urb->actual_length; i += 2) { int stat = data[i]; int flag = TTY_NORMAL;
if (stat & RXERROR_OVERRUN) {
tty_insert_flip_char(&port->port, 0,
TTY_OVERRUN);
} /* XXX should handle break (0x10) */ if (stat & RXERROR_PARITY)
flag = TTY_PARITY; elseif (stat & RXERROR_FRAMING)
flag = TTY_FRAME;
/* inbound data is in the form P#, len, status, data */
i = 0;
len = 0;
while (i < urb->actual_length) {
/* Check port number from message */ if (data[i] >= serial->num_ports) {
dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
__func__, data[i]); return;
}
port = serial->port[data[i++]];
len = data[i++];
/* 0x80 bit is error flag */ if ((data[i] & 0x80) == 0) { /* no error on any byte */
i++; for (x = 1; x < len && i < urb->actual_length; ++x)
tty_insert_flip_char(&port->port,
data[i++], 0);
} else { /* * some bytes had errors, every byte has status
*/ for (x = 0; x + 1 < len &&
i + 1 < urb->actual_length; x += 2) { int stat = data[i]; int flag = TTY_NORMAL;
if (stat & RXERROR_OVERRUN) {
tty_insert_flip_char(&port->port, 0,
TTY_OVERRUN);
} /* XXX should handle break (0x10) */ if (stat & RXERROR_PARITY)
flag = TTY_PARITY; elseif (stat & RXERROR_FRAMING)
flag = TTY_FRAME;
tty_insert_flip_char(&port->port, data[i+1],
flag);
i += 2;
}
}
tty_flip_buffer_push(&port->port);
}
/* Resubmit urb so we continue receiving */
err = usb_submit_urb(urb, GFP_ATOMIC); if (err != 0)
dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
}
/* not used, usa-49 doesn't have per-port control endpoints */ staticvoid usa49_outcont_callback(struct urb *urb)
{
}
staticvoid usa90_indat_callback(struct urb *urb)
{ int i, err; int endpoint; struct usb_serial_port *port; struct keyspan_port_private *p_priv; unsignedchar *data = urb->transfer_buffer; int status = urb->status;
endpoint = usb_pipeendpoint(urb->pipe);
if (status) {
dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
__func__, status, endpoint); return;
}
port = urb->context;
p_priv = usb_get_serial_port_data(port);
if (urb->actual_length) { /* if current mode is DMA, looks like usa28 format
otherwise looks like usa26 data format */
if (p_priv->baud > 57600)
tty_insert_flip_string(&port->port, data,
urb->actual_length); else { /* 0x80 bit is error flag */ if ((data[0] & 0x80) == 0) { /* no errors on individual bytes, only
possible overrun err*/ if (data[0] & RXERROR_OVERRUN) {
tty_insert_flip_char(&port->port, 0,
TTY_OVERRUN);
} for (i = 1; i < urb->actual_length ; ++i)
tty_insert_flip_char(&port->port,
data[i], TTY_NORMAL);
} else { /* some bytes had errors, every byte has status */
dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__); for (i = 0; i + 1 < urb->actual_length; i += 2) { int stat = data[i]; int flag = TTY_NORMAL;
if (stat & RXERROR_OVERRUN) {
tty_insert_flip_char(
&port->port, 0,
TTY_OVERRUN);
} /* XXX should handle break (0x10) */ if (stat & RXERROR_PARITY)
flag = TTY_PARITY; elseif (stat & RXERROR_FRAMING)
flag = TTY_FRAME;
if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length); return;
}
/* Now do something useful with the data */
msg = (struct keyspan_usa67_portStatusMessage *)data;
/* Check port number from message and retrieve private data */ if (msg->port >= serial->num_ports) {
dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port); return;
}
port = serial->port[msg->port];
p_priv = usb_get_serial_port_data(port); if (!p_priv) goto resubmit;
serial = urb->context; for (i = 0; i < serial->num_ports; ++i) {
port = serial->port[i];
p_priv = usb_get_serial_port_data(port); if (!p_priv) continue;
/* Set some sane defaults */
p_priv->rts_state = 1;
p_priv->dtr_state = 1;
p_priv->baud = 9600;
/* force baud and lcr to be set on open */
p_priv->old_baud = 0;
p_priv->old_cflag = 0;
p_priv->out_flip = 0;
p_priv->in_flip = 0;
/* Reset low level data toggle and start reading from endpoints */ for (i = 0; i < 2; i++) {
urb = p_priv->in_urbs[i]; if (urb == NULL) continue;
/* make sure endpoint data toggle is synchronized
with the device */
usb_clear_halt(urb->dev, urb->pipe);
err = usb_submit_urb(urb, GFP_KERNEL); if (err != 0)
dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
}
/* Reset low level data toggle on out endpoints */ for (i = 0; i < 2; i++) {
urb = p_priv->out_urbs[i]; if (urb == NULL) continue; /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
usb_pipeout(urb->pipe), 0); */
}
/* get the terminal config for the setup message now so we don't
* need to send 2 of them */
device_port = port->port_number; if (tty) {
cflag = tty->termios.c_cflag; /* Baud rate calculation takes baud rate as an integer
so other rates can be generated if desired. */
baud_rate = tty_get_baud_rate(tty); /* If no match or invalid, leave as default */ if (baud_rate >= 0
&& d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
p_priv->baud = baud_rate;
}
} /* set CTS/RTS handshake etc. */
p_priv->cflag = cflag;
p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
/* Select firmware image on the basis of idProduct */ switch (le16_to_cpu(serial->dev->descriptor.idProduct)) { case keyspan_usa28_pre_product_id:
fw_name = "keyspan/usa28.fw"; break;
case keyspan_usa28x_pre_product_id:
fw_name = "keyspan/usa28x.fw"; break;
case keyspan_usa28xa_pre_product_id:
fw_name = "keyspan/usa28xa.fw"; break;
case keyspan_usa28xb_pre_product_id:
fw_name = "keyspan/usa28xb.fw"; break;
case keyspan_usa19_pre_product_id:
fw_name = "keyspan/usa19.fw"; break;
case keyspan_usa19qi_pre_product_id:
fw_name = "keyspan/usa19qi.fw"; break;
case keyspan_mpr_pre_product_id:
fw_name = "keyspan/mpr.fw"; break;
case keyspan_usa19qw_pre_product_id:
fw_name = "keyspan/usa19qw.fw"; break;
case keyspan_usa18x_pre_product_id:
fw_name = "keyspan/usa18x.fw"; break;
case keyspan_usa19w_pre_product_id:
fw_name = "keyspan/usa19w.fw"; break;
case keyspan_usa49w_pre_product_id:
fw_name = "keyspan/usa49w.fw"; break;
case keyspan_usa49wlc_pre_product_id:
fw_name = "keyspan/usa49wlc.fw"; break;
default:
dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
le16_to_cpu(serial->dev->descriptor.idProduct)); return 1;
}
if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
fw_name); return -ENOENT;
}
/* after downloading firmware Renumeration will occur in a
moment and the new device will bind to the real driver */
/* we don't want this device to have a driver assigned to it. */ return 1;
}
/* Helper functions used by keyspan_setup_urbs */ staticstruct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial, int endpoint)
{ struct usb_host_interface *iface_desc; struct usb_endpoint_descriptor *ep; int i;
iface_desc = serial->interface->cur_altsetting; for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
ep = &iface_desc->endpoint[i].desc; if (ep->bEndpointAddress == endpoint) return ep;
}
dev_warn(&serial->interface->dev, "found no endpoint descriptor for endpoint %x\n",
endpoint); return NULL;
}
staticstruct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint, int dir, void *ctx, char *buf, int len, void (*callback)(struct urb *))
{ struct urb *urb; struct usb_endpoint_descriptor const *ep_desc; charconst *ep_type_name;
if (endpoint == -1) return NULL; /* endpoint not needed */
dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %x\n",
__func__, endpoint);
urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ if (!urb) return NULL;
if (endpoint == 0) { /* control EP filled in when used */ return urb;
}
/* Generic setup urbs function that uses
data in device_details */ staticvoid keyspan_setup_urbs(struct usb_serial *serial)
{ struct keyspan_serial_private *s_priv; conststruct keyspan_device_details *d_details; struct callbacks *cback;
/* Setup values for the various callback routines */
cback = &keyspan_callbacks[d_details->msg_format];
/* Allocate and set up urbs for each one that is in use,
starting with instat endpoints */
s_priv->instat_urb = keyspan_setup_urb
(serial, d_details->instat_endpoint, USB_DIR_IN,
serial, s_priv->instat_buf, INSTAT_BUFLEN,
cback->instat_callback);
/* prevent divide by zero... */
b16 = baud_rate * 16L; if (b16 == 0) return KEYSPAN_INVALID_BAUD_RATE; /* Any "standard" rate over 57k6 is marginal on the USA-19
as we run out of divisor resolution. */ if (baud_rate > 57600) return KEYSPAN_INVALID_BAUD_RATE;
/* calculate the divisor and the counter (its inverse) */
div = baudclk / b16; if (div == 0) return KEYSPAN_INVALID_BAUD_RATE; else
cnt = 0 - div;
if (div > 0xffff) return KEYSPAN_INVALID_BAUD_RATE;
/* return the counter values if non-null */ if (rate_low)
*rate_low = (u8) (cnt & 0xff); if (rate_hi)
*rate_hi = (u8) ((cnt >> 8) & 0xff); if (rate_low && rate_hi)
dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
__func__, baud_rate, *rate_hi, *rate_low); return KEYSPAN_BAUD_RATE_OK;
}
/* usa19hs function doesn't require prescaler */ staticint keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
u32 baud_rate, u32 baudclk, u8 *rate_hi,
u8 *rate_low, u8 *prescaler, int portnum)
{
u32 b16, /* baud rate times 16 (actual rate used internally) */
div; /* divisor */
/* prevent divide by zero */
b16 = baud_rate * 16L; if (b16 == 0) return KEYSPAN_INVALID_BAUD_RATE;
/* calculate the divisor and the counter (its inverse) */
div = KEYSPAN_USA28_BAUDCLK / b16; if (div == 0) return KEYSPAN_INVALID_BAUD_RATE; else
cnt = 0 - div;
/* check for out of range, based on portnum,
and return result */ if (portnum == 0) { if (div > 0xffff) return KEYSPAN_INVALID_BAUD_RATE;
} else { if (portnum == 1) { if (div > 0xff) return KEYSPAN_INVALID_BAUD_RATE;
} else return KEYSPAN_INVALID_BAUD_RATE;
}
/* return the counter values if not NULL
(port 1 will ignore retHi) */ if (rate_low)
*rate_low = (u8) (cnt & 0xff); if (rate_hi)
*rate_hi = (u8) ((cnt >> 8) & 0xff);
dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate); return KEYSPAN_BAUD_RATE_OK;
}
/* only do something if we have a bulk out endpoint */
this_urb = p_priv->outcont_urb; if (this_urb == NULL) {
dev_dbg(&port->dev, "%s - oops no urb.\n", __func__); return -1;
}
/* Save reset port val for resend.
Don't overwrite resend for open/close condition. */ if ((reset_port + 1) > p_priv->resend_cont)
p_priv->resend_cont = reset_port + 1; if (this_urb->status == -EINPROGRESS) {
dev_dbg(&port->dev, "%s already writing\n", __func__);
mdelay(5); return -1;
}
/* Work out which port within the device is being setup */
device_port = port->port_number;
/* Make sure we have an urb then send the message */ if (this_urb == NULL) {
dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__); return -1;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.