Added struct and defines to handle errors reported from CPC-USB/FD accordingly. Added needed code for that purpose Signed-off-by: Gerhard Uttenthaler <uttenthaler@xxxxxxxxxxxxxxxx> --- drivers/net/can/usb/ems_usb.c | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/drivers/net/can/usb/ems_usb.c b/drivers/net/can/usb/ems_usb.c index 677ed4bc47b9..b94364d8f390 100644 --- a/drivers/net/can/usb/ems_usb.c +++ b/drivers/net/can/usb/ems_usb.c @@ -62,7 +62,9 @@ MODULE_LICENSE("GPL v2"); /* CPC-USB/FD * Initialization is done with a generic CAN controller representation + * However error reporting brings two registers of the used controller type */ +#define CPC_CC_TYPE_LPC546XX 5 #define CPC_CC_TYPE_GENERIC 6 #define CPC_CAN_ECODE_ERRFRAME 0x01 /* Ecode type */ @@ -108,6 +110,25 @@ MODULE_LICENSE("GPL v2"); #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA +/* CPC-USB/FD PSR */ +#define CPCFD_LEC_MASK 0x07 +#define CPCFD_LEC_STUFF 0x01 +#define CPCFD_LEC_FORM 0x02 +#define CPCFD_LEC_ACK 0x03 +#define CPCFD_LEC_BIT1 0x04 +#define CPCFD_LEC_BIT0 0x05 +#define CPCFD_LEC_CRC 0x06 + +#define CPCFD_ERR_EP BIT(5) +#define CPCFD_ERR_EW BIT(6) +#define CPCFD_ERR_BO BIT(7) + +#define CPCFD_ACT_MASK 0x18 +#define CPCFD_ACT_SYNC 0x00 +#define CPCFD_ACT_IDLE 0x08 +#define CPCFD_ACT_RX 0x10 +#define CPCFD_ACT_TX 0x18 + /* CPC-USB/ARM7 actually uses a 16MHz clock to generate the CAN clock * but it expects SJA1000 bit settings based on 8MHz (is internally * converted). @@ -230,6 +251,11 @@ struct __packed cpc_sja1000_can_error { u8 txerr; }; +struct __packed cpc_lpc546xx_can_error { + __le32 psr; /* protocol status register */ + __le32 ecr; /* error counter register */ +}; + /* structure for CAN error conditions */ struct __packed cpc_can_error { u8 ecode; @@ -240,6 +266,7 @@ struct __packed cpc_can_error { /* Other controllers may also provide error code capture regs */ union { struct cpc_sja1000_can_error sja1000; + struct cpc_lpc546xx_can_error lpc546xx; } regs; } cc; }; @@ -504,6 +531,84 @@ static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg) CAN_ERR_CRTL_RX_PASSIVE; } } + /* CPC-USB/FD */ + else if (msg->msg.error.cc.cc_type == CPC_CC_TYPE_LPC546XX) { + struct net_device *netdev = dev->netdev; + u32 psr = le32_to_cpu(msg->msg.error.cc.regs.lpc546xx.psr); + u8 txerr = le32_to_cpu(msg->msg.error.cc.regs.lpc546xx.ecr); + u8 rxerr = le32_to_cpu(msg->msg.error.cc.regs.lpc546xx.ecr) >> 8; + + /* bus error interrupt */ + dev->can.can_stats.bus_error++; + + cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; + + switch (psr & CPCFD_LEC_MASK) { + case CPCFD_LEC_STUFF: + netdev_dbg(netdev, "stuff error\n"); + cf->data[2] |= CAN_ERR_PROT_STUFF; + break; + case CPCFD_LEC_FORM: + netdev_dbg(netdev, "form error\n"); + cf->data[2] |= CAN_ERR_PROT_FORM; + break; + case CPCFD_LEC_ACK: + netdev_dbg(netdev, "ack error\n"); + cf->data[3] = CAN_ERR_PROT_LOC_ACK; + break; + case CPCFD_LEC_BIT1: + netdev_dbg(netdev, "bit1 error\n"); + cf->data[2] |= CAN_ERR_PROT_BIT1; + break; + case CPCFD_LEC_BIT0: + netdev_dbg(netdev, "bit0 error\n"); + cf->data[2] |= CAN_ERR_PROT_BIT0; + break; + case CPCFD_LEC_CRC: + netdev_dbg(netdev, "CRC error\n"); + cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; + break; + default: + break; + } + + /* Let activity flags decide direction */ + switch (psr & CPCFD_ACT_MASK) { + case CPCFD_ACT_SYNC: + /* fallthrough */ + case CPCFD_ACT_IDLE: + /* fallthrough */ + case CPCFD_ACT_RX: + stats->rx_errors++; + break; + case CPCFD_ACT_TX: + stats->tx_errors++; + break; + } + + /* Error warning status */ + if (psr & CPCFD_ERR_EW) { + cf->data[1] = (txerr > rxerr) ? + CAN_ERR_CRTL_TX_WARNING : + CAN_ERR_CRTL_RX_WARNING; + cf->can_id |= CAN_ERR_CRTL; + } + + /* Error passive status */ + if (psr & CPCFD_ERR_EP) { + cf->data[1] |= (txerr > rxerr) ? + CAN_ERR_CRTL_TX_PASSIVE : + CAN_ERR_CRTL_RX_PASSIVE; + cf->can_id |= CAN_ERR_CRTL; + } + + /* Error bus off status */ + if (psr & CPCFD_ERR_BO) + cf->can_id |= CAN_ERR_BUSOFF; + + cf->data[6] = txerr; + cf->data[7] = rxerr; + } } else if (msg->type == CPC_MSG_TYPE_OVERRUN) { cf->can_id |= CAN_ERR_CRTL; cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; -- 2.26.2 -- EMS Dr. Thomas Wuensche e.K. Sonnenhang 3 85304 Ilmmuenster HR Ingolstadt, HRA 170106 Phone: +49-8441-490260 Fax : +49-8441-81860 http://www.ems-wuensche.com