📄 keyspan.c
字号:
}static struct 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; char const *ep_type_name; if (endpoint == -1) return NULL; /* endpoint not needed */ dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint); urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ if (urb == NULL) { dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint); return NULL; } if (endpoint == 0) { /* control EP filled in when used */ return urb; } ep_desc = find_ep(serial, endpoint); if (!ep_desc) { /* leak the urb, something's wrong and the callers don't care */ return urb; } if (usb_endpoint_xfer_int(ep_desc)) { ep_type_name = "INT"; usb_fill_int_urb(urb, serial->dev, usb_sndintpipe(serial->dev, endpoint) | dir, buf, len, callback, ctx, ep_desc->bInterval); } else if (usb_endpoint_xfer_bulk(ep_desc)) { ep_type_name = "BULK"; usb_fill_bulk_urb(urb, serial->dev, usb_sndbulkpipe(serial->dev, endpoint) | dir, buf, len, callback, ctx); } else { dev_warn(&serial->interface->dev, "unsupported endpoint type %x\n", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); usb_free_urb(urb); return NULL; } dbg("%s - using urb %p for %s endpoint %x", __func__, urb, ep_type_name, endpoint); return urb;}static struct callbacks { void (*instat_callback)(struct urb *); void (*glocont_callback)(struct urb *); void (*indat_callback)(struct urb *); void (*outdat_callback)(struct urb *); void (*inack_callback)(struct urb *); void (*outcont_callback)(struct urb *);} keyspan_callbacks[] = { { /* msg_usa26 callbacks */ .instat_callback = usa26_instat_callback, .glocont_callback = usa26_glocont_callback, .indat_callback = usa26_indat_callback, .outdat_callback = usa2x_outdat_callback, .inack_callback = usa26_inack_callback, .outcont_callback = usa26_outcont_callback, }, { /* msg_usa28 callbacks */ .instat_callback = usa28_instat_callback, .glocont_callback = usa28_glocont_callback, .indat_callback = usa28_indat_callback, .outdat_callback = usa2x_outdat_callback, .inack_callback = usa28_inack_callback, .outcont_callback = usa28_outcont_callback, }, { /* msg_usa49 callbacks */ .instat_callback = usa49_instat_callback, .glocont_callback = usa49_glocont_callback, .indat_callback = usa49_indat_callback, .outdat_callback = usa2x_outdat_callback, .inack_callback = usa49_inack_callback, .outcont_callback = usa49_outcont_callback, }, { /* msg_usa90 callbacks */ .instat_callback = usa90_instat_callback, .glocont_callback = usa28_glocont_callback, .indat_callback = usa90_indat_callback, .outdat_callback = usa2x_outdat_callback, .inack_callback = usa28_inack_callback, .outcont_callback = usa90_outcont_callback, }, { /* msg_usa67 callbacks */ .instat_callback = usa67_instat_callback, .glocont_callback = usa67_glocont_callback, .indat_callback = usa26_indat_callback, .outdat_callback = usa2x_outdat_callback, .inack_callback = usa26_inack_callback, .outcont_callback = usa26_outcont_callback, }}; /* Generic setup urbs function that uses data in device_details */static void keyspan_setup_urbs(struct usb_serial *serial){ int i, j; struct keyspan_serial_private *s_priv; const struct keyspan_device_details *d_details; struct usb_serial_port *port; struct keyspan_port_private *p_priv; struct callbacks *cback; int endp; dbg ("%s", __FUNCTION__); s_priv = usb_get_serial_data(serial); d_details = s_priv->device_details; /* 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); s_priv->indat_urb = keyspan_setup_urb (serial, d_details->indat_endpoint, USB_DIR_IN, serial, s_priv->indat_buf, INDAT49W_BUFLEN, usa49wg_indat_callback); s_priv->glocont_urb = keyspan_setup_urb (serial, d_details->glocont_endpoint, USB_DIR_OUT, serial, s_priv->glocont_buf, GLOCONT_BUFLEN, cback->glocont_callback); /* Setup endpoints for each port specific thing */ for (i = 0; i < d_details->num_ports; i ++) { port = serial->port[i]; p_priv = usb_get_serial_port_data(port); /* Do indat endpoints first, once for each flip */ endp = d_details->indat_endpoints[i]; for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) { p_priv->in_urbs[j] = keyspan_setup_urb (serial, endp, USB_DIR_IN, port, p_priv->in_buffer[j], 64, cback->indat_callback); } for (; j < 2; ++j) p_priv->in_urbs[j] = NULL; /* outdat endpoints also have flip */ endp = d_details->outdat_endpoints[i]; for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) { p_priv->out_urbs[j] = keyspan_setup_urb (serial, endp, USB_DIR_OUT, port, p_priv->out_buffer[j], 64, cback->outdat_callback); } for (; j < 2; ++j) p_priv->out_urbs[j] = NULL; /* inack endpoint */ p_priv->inack_urb = keyspan_setup_urb (serial, d_details->inack_endpoints[i], USB_DIR_IN, port, p_priv->inack_buffer, 1, cback->inack_callback); /* outcont endpoint */ p_priv->outcont_urb = keyspan_setup_urb (serial, d_details->outcont_endpoints[i], USB_DIR_OUT, port, p_priv->outcont_buffer, 64, cback->outcont_callback); } }/* usa19 function doesn't require prescaler */static int keyspan_usa19_calc_baud(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 */ cnt; /* inverse of divisor (programmed into 8051) */ dbg ("%s - %d.", __FUNCTION__, baud_rate); /* prevent divide by zero... */ if( (b16 = (baud_rate * 16L)) == 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) */ if( (div = (baudclk / b16)) == 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) { dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low); } return (KEYSPAN_BAUD_RATE_OK);}/* usa19hs function doesn't require prescaler */static int keyspan_usa19hs_calc_baud(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 */ dbg ("%s - %d.", __FUNCTION__, baud_rate); /* prevent divide by zero... */ if( (b16 = (baud_rate * 16L)) == 0) return (KEYSPAN_INVALID_BAUD_RATE); /* calculate the divisor */ if( (div = (baudclk / b16)) == 0) return (KEYSPAN_INVALID_BAUD_RATE); if(div > 0xffff) return (KEYSPAN_INVALID_BAUD_RATE); /* return the counter values if non-null */ if (rate_low) *rate_low = (u8) (div & 0xff); if (rate_hi) *rate_hi = (u8) ((div >> 8) & 0xff); if (rate_low && rate_hi) dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low); return (KEYSPAN_BAUD_RATE_OK);}static int keyspan_usa19w_calc_baud(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) */ clk, /* clock with 13/8 prescaler */ div, /* divisor using 13/8 prescaler */ res, /* resulting baud rate using 13/8 prescaler */ diff, /* error using 13/8 prescaler */ smallest_diff; u8 best_prescaler; int i; dbg ("%s - %d.", __FUNCTION__, baud_rate); /* prevent divide by zero */ if( (b16 = baud_rate * 16L) == 0) { return (KEYSPAN_INVALID_BAUD_RATE); } /* Calculate prescaler by trying them all and looking for best fit */ /* start with largest possible difference */ smallest_diff = 0xffffffff; /* 0 is an invalid prescaler, used as a flag */ best_prescaler = 0; for(i = 8; i <= 0xff; ++i) { clk = (baudclk * 8) / (u32) i; if( (div = clk / b16) == 0) { continue; } res = clk / div; diff= (res > b16) ? (res-b16) : (b16-res); if(diff < smallest_diff) { best_prescaler = i; smallest_diff = diff; } } if(best_prescaler == 0) { return (KEYSPAN_INVALID_BAUD_RATE); } clk = (baudclk * 8) / (u32) best_prescaler; div = clk / b16; /* return the divisor and prescaler if non-null */ if (rate_low) { *rate_low = (u8) (div & 0xff); } if (rate_hi) { *rate_hi = (u8) ((div >> 8) & 0xff); } if (prescaler) { *prescaler = best_prescaler; /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */ } return (KEYSPAN_BAUD_RATE_OK);} /* USA-28 supports different maximum baud rates on each port */static int keyspan_usa28_calc_baud(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 */ cnt; /* inverse of divisor (programmed into 8051) */ dbg ("%s - %d.", __FUNCTION__, baud_rate); /* prevent divide by zero */ if ((b16 = baud_rate * 16L) == 0) return (KEYSPAN_INVALID_BAUD_RATE); /* calculate the divisor and the counter (its inverse) */ if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 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); } dbg ("%s - %d OK.", __FUNCTION__, baud_rate); return (KEYSPAN_BAUD_RATE_OK);}static int keyspan_usa26_send_setup(struct usb_serial *serial, struct usb_serial_port *port, int reset_port){ struct keyspan_usa26_portControlMessage msg; struct keyspan_serial_private *s_priv; struct keyspan_port_private *p_priv; const struct keyspan_device_details *d_details; int outcont_urb; struct urb *this_urb; int device_port, err; dbg ("%s reset=%d", __FUNCTION__, reset_port); s_priv = usb_get_serial_data(serial); p_priv = usb_get_serial_port_data(port); d_details = s_priv->device_details; device_port = port->number - port->serial->minor; outcont_urb = d_details->outcont_endpoints[port->number]; this_urb = p_priv->outcont_urb; dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe)); /* Make sure we have an urb then send the message */ if (this_urb == NULL) { dbg("%s - oops no urb.", __FUNCTION__); 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) { /* dbg ("%s - already writing", __FUNCTION__); */ mdelay(5); return(-1); } memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage)); /* Only set baud rate if it's changed */ if (p_priv->old_baud != p_priv->baud) { p_priv->old_baud = p_priv->baud; msg.setClocking = 0xff; if (d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk, &msg.baudHi, &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) { dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__, p_priv->baud); msg.baudLo = 0; msg.baudHi = 125; /* Values for 9600 baud */ msg.prescaler = 10; } msg.setPrescaler = 0xff; } msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1; switch (p_priv->cflag & CSIZE) { case CS5: msg.lcr |= USA_DATABITS_5; break; case CS6: msg.lcr |= USA_DATABITS_6; break; case CS7: msg.lcr |= USA_DATABITS_7; break; case CS8: msg.lcr |= USA_DATABITS_8; break; } if (p_priv->cflag & PARENB) { /* note USA_PARITY_NONE == 0 */ msg.lcr |= (p_priv->cflag & PARODD)? USA_PARITY_ODD: USA_PARITY_EVEN; } msg.setLcr = 0xff; msg.ctsFlowControl = (p_priv->flow_control == flow_cts); msg.xonFlowControl = 0; msg.setFlowControl = 0xff; msg.forwardingLength = 16; msg.xonChar = 17; msg.xoffChar = 19; /* Opening port */ if (reset_port == 1) { msg._txOn = 1; msg._txOff = 0; msg.txFlush = 0; msg.txBreak = 0; msg.rxOn = 1; msg.rxOff = 0; msg.rxFlush = 1; msg.rxForward = 0; msg.returnStatus = 0; msg.resetDataToggle = 0xff; } /* Closing port */ else if (reset_port == 2) { msg._txOn = 0; msg._txOff = 1; msg.txFlush = 0; msg.txBreak = 0; msg.rxOn = 0; msg.rxOff = 1; msg.rxFlush = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -