| .. | .. |
|---|
| 20 | 20 | #include <linux/string.h> |
|---|
| 21 | 21 | #include <linux/types.h> |
|---|
| 22 | 22 | #include <linux/usb.h> |
|---|
| 23 | +#include <linux/workqueue.h> |
|---|
| 23 | 24 | |
|---|
| 24 | 25 | #include <linux/can.h> |
|---|
| 25 | 26 | #include <linux/can/dev.h> |
|---|
| .. | .. |
|---|
| 55 | 56 | #define CMD_RX_EXT_MESSAGE 14 |
|---|
| 56 | 57 | #define CMD_TX_EXT_MESSAGE 15 |
|---|
| 57 | 58 | #define CMD_SET_BUS_PARAMS 16 |
|---|
| 59 | +#define CMD_GET_BUS_PARAMS 17 |
|---|
| 60 | +#define CMD_GET_BUS_PARAMS_REPLY 18 |
|---|
| 61 | +#define CMD_GET_CHIP_STATE 19 |
|---|
| 58 | 62 | #define CMD_CHIP_STATE_EVENT 20 |
|---|
| 59 | 63 | #define CMD_SET_CTRL_MODE 21 |
|---|
| 60 | 64 | #define CMD_RESET_CHIP 24 |
|---|
| .. | .. |
|---|
| 69 | 73 | #define CMD_GET_CARD_INFO_REPLY 35 |
|---|
| 70 | 74 | #define CMD_GET_SOFTWARE_INFO 38 |
|---|
| 71 | 75 | #define CMD_GET_SOFTWARE_INFO_REPLY 39 |
|---|
| 76 | +#define CMD_ERROR_EVENT 45 |
|---|
| 72 | 77 | #define CMD_FLUSH_QUEUE 48 |
|---|
| 73 | 78 | #define CMD_TX_ACKNOWLEDGE 50 |
|---|
| 74 | 79 | #define CMD_CAN_ERROR_EVENT 51 |
|---|
| 75 | 80 | #define CMD_FLUSH_QUEUE_REPLY 68 |
|---|
| 81 | +#define CMD_GET_CAPABILITIES_REQ 95 |
|---|
| 82 | +#define CMD_GET_CAPABILITIES_RESP 96 |
|---|
| 76 | 83 | |
|---|
| 77 | 84 | #define CMD_LEAF_LOG_MESSAGE 106 |
|---|
| 78 | 85 | |
|---|
| .. | .. |
|---|
| 81 | 88 | #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0 |
|---|
| 82 | 89 | #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5) |
|---|
| 83 | 90 | #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6) |
|---|
| 91 | + |
|---|
| 92 | +#define KVASER_USB_LEAF_SWOPTION_EXT_CAP BIT(12) |
|---|
| 84 | 93 | |
|---|
| 85 | 94 | /* error factors */ |
|---|
| 86 | 95 | #define M16C_EF_ACKE BIT(0) |
|---|
| .. | .. |
|---|
| 156 | 165 | struct kvaser_cmd_busparams { |
|---|
| 157 | 166 | u8 tid; |
|---|
| 158 | 167 | u8 channel; |
|---|
| 159 | | - __le32 bitrate; |
|---|
| 160 | | - u8 tseg1; |
|---|
| 161 | | - u8 tseg2; |
|---|
| 162 | | - u8 sjw; |
|---|
| 163 | | - u8 no_samp; |
|---|
| 168 | + struct kvaser_usb_busparams busparams; |
|---|
| 164 | 169 | } __packed; |
|---|
| 165 | 170 | |
|---|
| 166 | 171 | struct kvaser_cmd_tx_can { |
|---|
| .. | .. |
|---|
| 229 | 234 | u8 tid; |
|---|
| 230 | 235 | } __packed; |
|---|
| 231 | 236 | |
|---|
| 232 | | -struct leaf_cmd_error_event { |
|---|
| 237 | +struct leaf_cmd_can_error_event { |
|---|
| 233 | 238 | u8 tid; |
|---|
| 234 | 239 | u8 flags; |
|---|
| 235 | 240 | __le16 time[3]; |
|---|
| .. | .. |
|---|
| 241 | 246 | u8 error_factor; |
|---|
| 242 | 247 | } __packed; |
|---|
| 243 | 248 | |
|---|
| 244 | | -struct usbcan_cmd_error_event { |
|---|
| 249 | +struct usbcan_cmd_can_error_event { |
|---|
| 245 | 250 | u8 tid; |
|---|
| 246 | 251 | u8 padding; |
|---|
| 247 | 252 | u8 tx_errors_count_ch0; |
|---|
| .. | .. |
|---|
| 251 | 256 | u8 status_ch0; |
|---|
| 252 | 257 | u8 status_ch1; |
|---|
| 253 | 258 | __le16 time; |
|---|
| 259 | +} __packed; |
|---|
| 260 | + |
|---|
| 261 | +/* CMD_ERROR_EVENT error codes */ |
|---|
| 262 | +#define KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL 0x8 |
|---|
| 263 | +#define KVASER_USB_LEAF_ERROR_EVENT_PARAM 0x9 |
|---|
| 264 | + |
|---|
| 265 | +struct leaf_cmd_error_event { |
|---|
| 266 | + u8 tid; |
|---|
| 267 | + u8 error_code; |
|---|
| 268 | + __le16 timestamp[3]; |
|---|
| 269 | + __le16 padding; |
|---|
| 270 | + __le16 info1; |
|---|
| 271 | + __le16 info2; |
|---|
| 272 | +} __packed; |
|---|
| 273 | + |
|---|
| 274 | +struct usbcan_cmd_error_event { |
|---|
| 275 | + u8 tid; |
|---|
| 276 | + u8 error_code; |
|---|
| 277 | + __le16 info1; |
|---|
| 278 | + __le16 info2; |
|---|
| 279 | + __le16 timestamp; |
|---|
| 280 | + __le16 padding; |
|---|
| 254 | 281 | } __packed; |
|---|
| 255 | 282 | |
|---|
| 256 | 283 | struct kvaser_cmd_ctrl_mode { |
|---|
| .. | .. |
|---|
| 277 | 304 | u8 data[8]; |
|---|
| 278 | 305 | } __packed; |
|---|
| 279 | 306 | |
|---|
| 307 | +/* Sub commands for cap_req and cap_res */ |
|---|
| 308 | +#define KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE 0x02 |
|---|
| 309 | +#define KVASER_USB_LEAF_CAP_CMD_ERR_REPORT 0x05 |
|---|
| 310 | +struct kvaser_cmd_cap_req { |
|---|
| 311 | + __le16 padding0; |
|---|
| 312 | + __le16 cap_cmd; |
|---|
| 313 | + __le16 padding1; |
|---|
| 314 | + __le16 channel; |
|---|
| 315 | +} __packed; |
|---|
| 316 | + |
|---|
| 317 | +/* Status codes for cap_res */ |
|---|
| 318 | +#define KVASER_USB_LEAF_CAP_STAT_OK 0x00 |
|---|
| 319 | +#define KVASER_USB_LEAF_CAP_STAT_NOT_IMPL 0x01 |
|---|
| 320 | +#define KVASER_USB_LEAF_CAP_STAT_UNAVAIL 0x02 |
|---|
| 321 | +struct kvaser_cmd_cap_res { |
|---|
| 322 | + __le16 padding; |
|---|
| 323 | + __le16 cap_cmd; |
|---|
| 324 | + __le16 status; |
|---|
| 325 | + __le32 mask; |
|---|
| 326 | + __le32 value; |
|---|
| 327 | +} __packed; |
|---|
| 328 | + |
|---|
| 280 | 329 | struct kvaser_cmd { |
|---|
| 281 | 330 | u8 len; |
|---|
| 282 | 331 | u8 id; |
|---|
| .. | .. |
|---|
| 292 | 341 | struct leaf_cmd_softinfo softinfo; |
|---|
| 293 | 342 | struct leaf_cmd_rx_can rx_can; |
|---|
| 294 | 343 | struct leaf_cmd_chip_state_event chip_state_event; |
|---|
| 295 | | - struct leaf_cmd_error_event error_event; |
|---|
| 344 | + struct leaf_cmd_can_error_event can_error_event; |
|---|
| 296 | 345 | struct leaf_cmd_log_message log_message; |
|---|
| 346 | + struct leaf_cmd_error_event error_event; |
|---|
| 347 | + struct kvaser_cmd_cap_req cap_req; |
|---|
| 348 | + struct kvaser_cmd_cap_res cap_res; |
|---|
| 297 | 349 | } __packed leaf; |
|---|
| 298 | 350 | |
|---|
| 299 | 351 | union { |
|---|
| 300 | 352 | struct usbcan_cmd_softinfo softinfo; |
|---|
| 301 | 353 | struct usbcan_cmd_rx_can rx_can; |
|---|
| 302 | 354 | struct usbcan_cmd_chip_state_event chip_state_event; |
|---|
| 355 | + struct usbcan_cmd_can_error_event can_error_event; |
|---|
| 303 | 356 | struct usbcan_cmd_error_event error_event; |
|---|
| 304 | 357 | } __packed usbcan; |
|---|
| 305 | 358 | |
|---|
| .. | .. |
|---|
| 322 | 375 | [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.leaf.rx_can), |
|---|
| 323 | 376 | [CMD_LEAF_LOG_MESSAGE] = kvaser_fsize(u.leaf.log_message), |
|---|
| 324 | 377 | [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.leaf.chip_state_event), |
|---|
| 325 | | - [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.leaf.error_event), |
|---|
| 378 | + [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.leaf.can_error_event), |
|---|
| 379 | + [CMD_GET_CAPABILITIES_RESP] = kvaser_fsize(u.leaf.cap_res), |
|---|
| 380 | + [CMD_GET_BUS_PARAMS_REPLY] = kvaser_fsize(u.busparams), |
|---|
| 381 | + [CMD_ERROR_EVENT] = kvaser_fsize(u.leaf.error_event), |
|---|
| 326 | 382 | /* ignored events: */ |
|---|
| 327 | 383 | [CMD_FLUSH_QUEUE_REPLY] = CMD_SIZE_ANY, |
|---|
| 328 | 384 | }; |
|---|
| .. | .. |
|---|
| 336 | 392 | [CMD_RX_STD_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), |
|---|
| 337 | 393 | [CMD_RX_EXT_MESSAGE] = kvaser_fsize(u.usbcan.rx_can), |
|---|
| 338 | 394 | [CMD_CHIP_STATE_EVENT] = kvaser_fsize(u.usbcan.chip_state_event), |
|---|
| 339 | | - [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.usbcan.error_event), |
|---|
| 395 | + [CMD_CAN_ERROR_EVENT] = kvaser_fsize(u.usbcan.can_error_event), |
|---|
| 396 | + [CMD_ERROR_EVENT] = kvaser_fsize(u.usbcan.error_event), |
|---|
| 340 | 397 | /* ignored events: */ |
|---|
| 341 | 398 | [CMD_USBCAN_CLOCK_OVERFLOW_EVENT] = CMD_SIZE_ANY, |
|---|
| 342 | 399 | }; |
|---|
| .. | .. |
|---|
| 362 | 419 | u8 error_state; |
|---|
| 363 | 420 | } usbcan; |
|---|
| 364 | 421 | }; |
|---|
| 422 | +}; |
|---|
| 423 | + |
|---|
| 424 | +struct kvaser_usb_net_leaf_priv { |
|---|
| 425 | + struct kvaser_usb_net_priv *net; |
|---|
| 426 | + |
|---|
| 427 | + struct delayed_work chip_state_req_work; |
|---|
| 365 | 428 | }; |
|---|
| 366 | 429 | |
|---|
| 367 | 430 | static const struct can_bittiming_const kvaser_usb_leaf_m16c_bittiming_const = { |
|---|
| .. | .. |
|---|
| 607 | 670 | dev->fw_version = le32_to_cpu(softinfo->fw_version); |
|---|
| 608 | 671 | dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx); |
|---|
| 609 | 672 | |
|---|
| 673 | + if (sw_options & KVASER_USB_LEAF_SWOPTION_EXT_CAP) |
|---|
| 674 | + dev->card_data.capabilities |= KVASER_USB_CAP_EXT_CAP; |
|---|
| 675 | + |
|---|
| 610 | 676 | if (dev->driver_info->quirks & KVASER_USB_QUIRK_IGNORE_CLK_FREQ) { |
|---|
| 611 | 677 | /* Firmware expects bittiming parameters calculated for 16MHz |
|---|
| 612 | 678 | * clock, regardless of the actual clock |
|---|
| .. | .. |
|---|
| 694 | 760 | return 0; |
|---|
| 695 | 761 | } |
|---|
| 696 | 762 | |
|---|
| 763 | +static int kvaser_usb_leaf_get_single_capability(struct kvaser_usb *dev, |
|---|
| 764 | + u16 cap_cmd_req, u16 *status) |
|---|
| 765 | +{ |
|---|
| 766 | + struct kvaser_usb_dev_card_data *card_data = &dev->card_data; |
|---|
| 767 | + struct kvaser_cmd *cmd; |
|---|
| 768 | + u32 value = 0; |
|---|
| 769 | + u32 mask = 0; |
|---|
| 770 | + u16 cap_cmd_res; |
|---|
| 771 | + int err; |
|---|
| 772 | + int i; |
|---|
| 773 | + |
|---|
| 774 | + cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
|---|
| 775 | + if (!cmd) |
|---|
| 776 | + return -ENOMEM; |
|---|
| 777 | + |
|---|
| 778 | + cmd->id = CMD_GET_CAPABILITIES_REQ; |
|---|
| 779 | + cmd->u.leaf.cap_req.cap_cmd = cpu_to_le16(cap_cmd_req); |
|---|
| 780 | + cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_cap_req); |
|---|
| 781 | + |
|---|
| 782 | + err = kvaser_usb_send_cmd(dev, cmd, cmd->len); |
|---|
| 783 | + if (err) |
|---|
| 784 | + goto end; |
|---|
| 785 | + |
|---|
| 786 | + err = kvaser_usb_leaf_wait_cmd(dev, CMD_GET_CAPABILITIES_RESP, cmd); |
|---|
| 787 | + if (err) |
|---|
| 788 | + goto end; |
|---|
| 789 | + |
|---|
| 790 | + *status = le16_to_cpu(cmd->u.leaf.cap_res.status); |
|---|
| 791 | + |
|---|
| 792 | + if (*status != KVASER_USB_LEAF_CAP_STAT_OK) |
|---|
| 793 | + goto end; |
|---|
| 794 | + |
|---|
| 795 | + cap_cmd_res = le16_to_cpu(cmd->u.leaf.cap_res.cap_cmd); |
|---|
| 796 | + switch (cap_cmd_res) { |
|---|
| 797 | + case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE: |
|---|
| 798 | + case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT: |
|---|
| 799 | + value = le32_to_cpu(cmd->u.leaf.cap_res.value); |
|---|
| 800 | + mask = le32_to_cpu(cmd->u.leaf.cap_res.mask); |
|---|
| 801 | + break; |
|---|
| 802 | + default: |
|---|
| 803 | + dev_warn(&dev->intf->dev, "Unknown capability command %u\n", |
|---|
| 804 | + cap_cmd_res); |
|---|
| 805 | + break; |
|---|
| 806 | + } |
|---|
| 807 | + |
|---|
| 808 | + for (i = 0; i < dev->nchannels; i++) { |
|---|
| 809 | + if (BIT(i) & (value & mask)) { |
|---|
| 810 | + switch (cap_cmd_res) { |
|---|
| 811 | + case KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE: |
|---|
| 812 | + card_data->ctrlmode_supported |= |
|---|
| 813 | + CAN_CTRLMODE_LISTENONLY; |
|---|
| 814 | + break; |
|---|
| 815 | + case KVASER_USB_LEAF_CAP_CMD_ERR_REPORT: |
|---|
| 816 | + card_data->capabilities |= |
|---|
| 817 | + KVASER_USB_CAP_BERR_CAP; |
|---|
| 818 | + break; |
|---|
| 819 | + } |
|---|
| 820 | + } |
|---|
| 821 | + } |
|---|
| 822 | + |
|---|
| 823 | +end: |
|---|
| 824 | + kfree(cmd); |
|---|
| 825 | + |
|---|
| 826 | + return err; |
|---|
| 827 | +} |
|---|
| 828 | + |
|---|
| 829 | +static int kvaser_usb_leaf_get_capabilities_leaf(struct kvaser_usb *dev) |
|---|
| 830 | +{ |
|---|
| 831 | + int err; |
|---|
| 832 | + u16 status; |
|---|
| 833 | + |
|---|
| 834 | + if (!(dev->card_data.capabilities & KVASER_USB_CAP_EXT_CAP)) { |
|---|
| 835 | + dev_info(&dev->intf->dev, |
|---|
| 836 | + "No extended capability support. Upgrade device firmware.\n"); |
|---|
| 837 | + return 0; |
|---|
| 838 | + } |
|---|
| 839 | + |
|---|
| 840 | + err = kvaser_usb_leaf_get_single_capability(dev, |
|---|
| 841 | + KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE, |
|---|
| 842 | + &status); |
|---|
| 843 | + if (err) |
|---|
| 844 | + return err; |
|---|
| 845 | + if (status) |
|---|
| 846 | + dev_info(&dev->intf->dev, |
|---|
| 847 | + "KVASER_USB_LEAF_CAP_CMD_LISTEN_MODE failed %u\n", |
|---|
| 848 | + status); |
|---|
| 849 | + |
|---|
| 850 | + err = kvaser_usb_leaf_get_single_capability(dev, |
|---|
| 851 | + KVASER_USB_LEAF_CAP_CMD_ERR_REPORT, |
|---|
| 852 | + &status); |
|---|
| 853 | + if (err) |
|---|
| 854 | + return err; |
|---|
| 855 | + if (status) |
|---|
| 856 | + dev_info(&dev->intf->dev, |
|---|
| 857 | + "KVASER_USB_LEAF_CAP_CMD_ERR_REPORT failed %u\n", |
|---|
| 858 | + status); |
|---|
| 859 | + |
|---|
| 860 | + return 0; |
|---|
| 861 | +} |
|---|
| 862 | + |
|---|
| 863 | +static int kvaser_usb_leaf_get_capabilities(struct kvaser_usb *dev) |
|---|
| 864 | +{ |
|---|
| 865 | + int err = 0; |
|---|
| 866 | + |
|---|
| 867 | + if (dev->driver_info->family == KVASER_LEAF) |
|---|
| 868 | + err = kvaser_usb_leaf_get_capabilities_leaf(dev); |
|---|
| 869 | + |
|---|
| 870 | + return err; |
|---|
| 871 | +} |
|---|
| 872 | + |
|---|
| 697 | 873 | static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev, |
|---|
| 698 | 874 | const struct kvaser_cmd *cmd) |
|---|
| 699 | 875 | { |
|---|
| .. | .. |
|---|
| 722 | 898 | context = &priv->tx_contexts[tid % dev->max_tx_urbs]; |
|---|
| 723 | 899 | |
|---|
| 724 | 900 | /* Sometimes the state change doesn't come after a bus-off event */ |
|---|
| 725 | | - if (priv->can.restart_ms && priv->can.state >= CAN_STATE_BUS_OFF) { |
|---|
| 901 | + if (priv->can.restart_ms && priv->can.state == CAN_STATE_BUS_OFF) { |
|---|
| 726 | 902 | struct sk_buff *skb; |
|---|
| 727 | 903 | struct can_frame *cf; |
|---|
| 728 | 904 | |
|---|
| .. | .. |
|---|
| 778 | 954 | return err; |
|---|
| 779 | 955 | } |
|---|
| 780 | 956 | |
|---|
| 957 | +static void kvaser_usb_leaf_chip_state_req_work(struct work_struct *work) |
|---|
| 958 | +{ |
|---|
| 959 | + struct kvaser_usb_net_leaf_priv *leaf = |
|---|
| 960 | + container_of(work, struct kvaser_usb_net_leaf_priv, |
|---|
| 961 | + chip_state_req_work.work); |
|---|
| 962 | + struct kvaser_usb_net_priv *priv = leaf->net; |
|---|
| 963 | + |
|---|
| 964 | + kvaser_usb_leaf_simple_cmd_async(priv, CMD_GET_CHIP_STATE); |
|---|
| 965 | +} |
|---|
| 966 | + |
|---|
| 781 | 967 | static void |
|---|
| 782 | 968 | kvaser_usb_leaf_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, |
|---|
| 783 | 969 | const struct kvaser_usb_err_summary *es, |
|---|
| .. | .. |
|---|
| 796 | 982 | new_state = CAN_STATE_BUS_OFF; |
|---|
| 797 | 983 | } else if (es->status & M16C_STATE_BUS_PASSIVE) { |
|---|
| 798 | 984 | new_state = CAN_STATE_ERROR_PASSIVE; |
|---|
| 799 | | - } else if (es->status & M16C_STATE_BUS_ERROR) { |
|---|
| 985 | + } else if ((es->status & M16C_STATE_BUS_ERROR) && |
|---|
| 986 | + cur_state >= CAN_STATE_BUS_OFF) { |
|---|
| 800 | 987 | /* Guard against spurious error events after a busoff */ |
|---|
| 801 | | - if (cur_state < CAN_STATE_BUS_OFF) { |
|---|
| 802 | | - if (es->txerr >= 128 || es->rxerr >= 128) |
|---|
| 803 | | - new_state = CAN_STATE_ERROR_PASSIVE; |
|---|
| 804 | | - else if (es->txerr >= 96 || es->rxerr >= 96) |
|---|
| 805 | | - new_state = CAN_STATE_ERROR_WARNING; |
|---|
| 806 | | - else if (cur_state > CAN_STATE_ERROR_ACTIVE) |
|---|
| 807 | | - new_state = CAN_STATE_ERROR_ACTIVE; |
|---|
| 808 | | - } |
|---|
| 809 | | - } |
|---|
| 810 | | - |
|---|
| 811 | | - if (!es->status) |
|---|
| 988 | + } else if (es->txerr >= 128 || es->rxerr >= 128) { |
|---|
| 989 | + new_state = CAN_STATE_ERROR_PASSIVE; |
|---|
| 990 | + } else if (es->txerr >= 96 || es->rxerr >= 96) { |
|---|
| 991 | + new_state = CAN_STATE_ERROR_WARNING; |
|---|
| 992 | + } else { |
|---|
| 812 | 993 | new_state = CAN_STATE_ERROR_ACTIVE; |
|---|
| 994 | + } |
|---|
| 813 | 995 | |
|---|
| 814 | 996 | if (new_state != cur_state) { |
|---|
| 815 | 997 | tx_state = (es->txerr >= es->rxerr) ? new_state : 0; |
|---|
| .. | .. |
|---|
| 819 | 1001 | } |
|---|
| 820 | 1002 | |
|---|
| 821 | 1003 | if (priv->can.restart_ms && |
|---|
| 822 | | - cur_state >= CAN_STATE_BUS_OFF && |
|---|
| 1004 | + cur_state == CAN_STATE_BUS_OFF && |
|---|
| 823 | 1005 | new_state < CAN_STATE_BUS_OFF) |
|---|
| 824 | 1006 | priv->can.can_stats.restarts++; |
|---|
| 825 | 1007 | |
|---|
| .. | .. |
|---|
| 853 | 1035 | struct sk_buff *skb; |
|---|
| 854 | 1036 | struct net_device_stats *stats; |
|---|
| 855 | 1037 | struct kvaser_usb_net_priv *priv; |
|---|
| 1038 | + struct kvaser_usb_net_leaf_priv *leaf; |
|---|
| 856 | 1039 | enum can_state old_state, new_state; |
|---|
| 857 | 1040 | |
|---|
| 858 | 1041 | if (es->channel >= dev->nchannels) { |
|---|
| .. | .. |
|---|
| 862 | 1045 | } |
|---|
| 863 | 1046 | |
|---|
| 864 | 1047 | priv = dev->nets[es->channel]; |
|---|
| 1048 | + leaf = priv->sub_priv; |
|---|
| 865 | 1049 | stats = &priv->netdev->stats; |
|---|
| 1050 | + |
|---|
| 1051 | + /* Ignore e.g. state change to bus-off reported just after stopping */ |
|---|
| 1052 | + if (!netif_running(priv->netdev)) |
|---|
| 1053 | + return; |
|---|
| 866 | 1054 | |
|---|
| 867 | 1055 | /* Update all of the CAN interface's state and error counters before |
|---|
| 868 | 1056 | * trying any memory allocation that can actually fail with -ENOMEM. |
|---|
| .. | .. |
|---|
| 877 | 1065 | old_state = priv->can.state; |
|---|
| 878 | 1066 | kvaser_usb_leaf_rx_error_update_can_state(priv, es, &tmp_cf); |
|---|
| 879 | 1067 | new_state = priv->can.state; |
|---|
| 1068 | + |
|---|
| 1069 | + /* If there are errors, request status updates periodically as we do |
|---|
| 1070 | + * not get automatic notifications of improved state. |
|---|
| 1071 | + */ |
|---|
| 1072 | + if (new_state < CAN_STATE_BUS_OFF && |
|---|
| 1073 | + (es->rxerr || es->txerr || new_state == CAN_STATE_ERROR_PASSIVE)) |
|---|
| 1074 | + schedule_delayed_work(&leaf->chip_state_req_work, |
|---|
| 1075 | + msecs_to_jiffies(500)); |
|---|
| 880 | 1076 | |
|---|
| 881 | 1077 | skb = alloc_can_err_skb(priv->netdev, &cf); |
|---|
| 882 | 1078 | if (!skb) { |
|---|
| .. | .. |
|---|
| 895 | 1091 | } |
|---|
| 896 | 1092 | |
|---|
| 897 | 1093 | if (priv->can.restart_ms && |
|---|
| 898 | | - old_state >= CAN_STATE_BUS_OFF && |
|---|
| 1094 | + old_state == CAN_STATE_BUS_OFF && |
|---|
| 899 | 1095 | new_state < CAN_STATE_BUS_OFF) { |
|---|
| 900 | 1096 | cf->can_id |= CAN_ERR_RESTARTED; |
|---|
| 901 | 1097 | netif_carrier_on(priv->netdev); |
|---|
| .. | .. |
|---|
| 995 | 1191 | |
|---|
| 996 | 1192 | case CMD_CAN_ERROR_EVENT: |
|---|
| 997 | 1193 | es.channel = 0; |
|---|
| 998 | | - es.status = cmd->u.usbcan.error_event.status_ch0; |
|---|
| 999 | | - es.txerr = cmd->u.usbcan.error_event.tx_errors_count_ch0; |
|---|
| 1000 | | - es.rxerr = cmd->u.usbcan.error_event.rx_errors_count_ch0; |
|---|
| 1194 | + es.status = cmd->u.usbcan.can_error_event.status_ch0; |
|---|
| 1195 | + es.txerr = cmd->u.usbcan.can_error_event.tx_errors_count_ch0; |
|---|
| 1196 | + es.rxerr = cmd->u.usbcan.can_error_event.rx_errors_count_ch0; |
|---|
| 1001 | 1197 | es.usbcan.other_ch_status = |
|---|
| 1002 | | - cmd->u.usbcan.error_event.status_ch1; |
|---|
| 1198 | + cmd->u.usbcan.can_error_event.status_ch1; |
|---|
| 1003 | 1199 | kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); |
|---|
| 1004 | 1200 | |
|---|
| 1005 | 1201 | /* The USBCAN firmware supports up to 2 channels. |
|---|
| .. | .. |
|---|
| 1007 | 1203 | */ |
|---|
| 1008 | 1204 | if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { |
|---|
| 1009 | 1205 | es.channel = 1; |
|---|
| 1010 | | - es.status = cmd->u.usbcan.error_event.status_ch1; |
|---|
| 1206 | + es.status = cmd->u.usbcan.can_error_event.status_ch1; |
|---|
| 1011 | 1207 | es.txerr = |
|---|
| 1012 | | - cmd->u.usbcan.error_event.tx_errors_count_ch1; |
|---|
| 1208 | + cmd->u.usbcan.can_error_event.tx_errors_count_ch1; |
|---|
| 1013 | 1209 | es.rxerr = |
|---|
| 1014 | | - cmd->u.usbcan.error_event.rx_errors_count_ch1; |
|---|
| 1210 | + cmd->u.usbcan.can_error_event.rx_errors_count_ch1; |
|---|
| 1015 | 1211 | es.usbcan.other_ch_status = |
|---|
| 1016 | | - cmd->u.usbcan.error_event.status_ch0; |
|---|
| 1212 | + cmd->u.usbcan.can_error_event.status_ch0; |
|---|
| 1017 | 1213 | kvaser_usb_leaf_usbcan_conditionally_rx_error(dev, &es); |
|---|
| 1018 | 1214 | } |
|---|
| 1019 | 1215 | break; |
|---|
| .. | .. |
|---|
| 1030 | 1226 | |
|---|
| 1031 | 1227 | switch (cmd->id) { |
|---|
| 1032 | 1228 | case CMD_CAN_ERROR_EVENT: |
|---|
| 1033 | | - es.channel = cmd->u.leaf.error_event.channel; |
|---|
| 1034 | | - es.status = cmd->u.leaf.error_event.status; |
|---|
| 1035 | | - es.txerr = cmd->u.leaf.error_event.tx_errors_count; |
|---|
| 1036 | | - es.rxerr = cmd->u.leaf.error_event.rx_errors_count; |
|---|
| 1037 | | - es.leaf.error_factor = cmd->u.leaf.error_event.error_factor; |
|---|
| 1229 | + es.channel = cmd->u.leaf.can_error_event.channel; |
|---|
| 1230 | + es.status = cmd->u.leaf.can_error_event.status; |
|---|
| 1231 | + es.txerr = cmd->u.leaf.can_error_event.tx_errors_count; |
|---|
| 1232 | + es.rxerr = cmd->u.leaf.can_error_event.rx_errors_count; |
|---|
| 1233 | + es.leaf.error_factor = cmd->u.leaf.can_error_event.error_factor; |
|---|
| 1038 | 1234 | break; |
|---|
| 1039 | 1235 | case CMD_LEAF_LOG_MESSAGE: |
|---|
| 1040 | 1236 | es.channel = cmd->u.leaf.log_message.channel; |
|---|
| .. | .. |
|---|
| 1166 | 1362 | netif_rx(skb); |
|---|
| 1167 | 1363 | } |
|---|
| 1168 | 1364 | |
|---|
| 1365 | +static void kvaser_usb_leaf_error_event_parameter(const struct kvaser_usb *dev, |
|---|
| 1366 | + const struct kvaser_cmd *cmd) |
|---|
| 1367 | +{ |
|---|
| 1368 | + u16 info1 = 0; |
|---|
| 1369 | + |
|---|
| 1370 | + switch (dev->driver_info->family) { |
|---|
| 1371 | + case KVASER_LEAF: |
|---|
| 1372 | + info1 = le16_to_cpu(cmd->u.leaf.error_event.info1); |
|---|
| 1373 | + break; |
|---|
| 1374 | + case KVASER_USBCAN: |
|---|
| 1375 | + info1 = le16_to_cpu(cmd->u.usbcan.error_event.info1); |
|---|
| 1376 | + break; |
|---|
| 1377 | + } |
|---|
| 1378 | + |
|---|
| 1379 | + /* info1 will contain the offending cmd_no */ |
|---|
| 1380 | + switch (info1) { |
|---|
| 1381 | + case CMD_SET_CTRL_MODE: |
|---|
| 1382 | + dev_warn(&dev->intf->dev, |
|---|
| 1383 | + "CMD_SET_CTRL_MODE error in parameter\n"); |
|---|
| 1384 | + break; |
|---|
| 1385 | + |
|---|
| 1386 | + case CMD_SET_BUS_PARAMS: |
|---|
| 1387 | + dev_warn(&dev->intf->dev, |
|---|
| 1388 | + "CMD_SET_BUS_PARAMS error in parameter\n"); |
|---|
| 1389 | + break; |
|---|
| 1390 | + |
|---|
| 1391 | + default: |
|---|
| 1392 | + dev_warn(&dev->intf->dev, |
|---|
| 1393 | + "Unhandled parameter error event cmd_no (%u)\n", |
|---|
| 1394 | + info1); |
|---|
| 1395 | + break; |
|---|
| 1396 | + } |
|---|
| 1397 | +} |
|---|
| 1398 | + |
|---|
| 1399 | +static void kvaser_usb_leaf_error_event(const struct kvaser_usb *dev, |
|---|
| 1400 | + const struct kvaser_cmd *cmd) |
|---|
| 1401 | +{ |
|---|
| 1402 | + u8 error_code = 0; |
|---|
| 1403 | + |
|---|
| 1404 | + switch (dev->driver_info->family) { |
|---|
| 1405 | + case KVASER_LEAF: |
|---|
| 1406 | + error_code = cmd->u.leaf.error_event.error_code; |
|---|
| 1407 | + break; |
|---|
| 1408 | + case KVASER_USBCAN: |
|---|
| 1409 | + error_code = cmd->u.usbcan.error_event.error_code; |
|---|
| 1410 | + break; |
|---|
| 1411 | + } |
|---|
| 1412 | + |
|---|
| 1413 | + switch (error_code) { |
|---|
| 1414 | + case KVASER_USB_LEAF_ERROR_EVENT_TX_QUEUE_FULL: |
|---|
| 1415 | + /* Received additional CAN message, when firmware TX queue is |
|---|
| 1416 | + * already full. Something is wrong with the driver. |
|---|
| 1417 | + * This should never happen! |
|---|
| 1418 | + */ |
|---|
| 1419 | + dev_err(&dev->intf->dev, |
|---|
| 1420 | + "Received error event TX_QUEUE_FULL\n"); |
|---|
| 1421 | + break; |
|---|
| 1422 | + case KVASER_USB_LEAF_ERROR_EVENT_PARAM: |
|---|
| 1423 | + kvaser_usb_leaf_error_event_parameter(dev, cmd); |
|---|
| 1424 | + break; |
|---|
| 1425 | + |
|---|
| 1426 | + default: |
|---|
| 1427 | + dev_warn(&dev->intf->dev, |
|---|
| 1428 | + "Unhandled error event (%d)\n", error_code); |
|---|
| 1429 | + break; |
|---|
| 1430 | + } |
|---|
| 1431 | +} |
|---|
| 1432 | + |
|---|
| 1169 | 1433 | static void kvaser_usb_leaf_start_chip_reply(const struct kvaser_usb *dev, |
|---|
| 1170 | 1434 | const struct kvaser_cmd *cmd) |
|---|
| 1171 | 1435 | { |
|---|
| .. | .. |
|---|
| 1206 | 1470 | complete(&priv->stop_comp); |
|---|
| 1207 | 1471 | } |
|---|
| 1208 | 1472 | |
|---|
| 1473 | +static void kvaser_usb_leaf_get_busparams_reply(const struct kvaser_usb *dev, |
|---|
| 1474 | + const struct kvaser_cmd *cmd) |
|---|
| 1475 | +{ |
|---|
| 1476 | + struct kvaser_usb_net_priv *priv; |
|---|
| 1477 | + u8 channel = cmd->u.busparams.channel; |
|---|
| 1478 | + |
|---|
| 1479 | + if (channel >= dev->nchannels) { |
|---|
| 1480 | + dev_err(&dev->intf->dev, |
|---|
| 1481 | + "Invalid channel number (%d)\n", channel); |
|---|
| 1482 | + return; |
|---|
| 1483 | + } |
|---|
| 1484 | + |
|---|
| 1485 | + priv = dev->nets[channel]; |
|---|
| 1486 | + memcpy(&priv->busparams_nominal, &cmd->u.busparams.busparams, |
|---|
| 1487 | + sizeof(priv->busparams_nominal)); |
|---|
| 1488 | + |
|---|
| 1489 | + complete(&priv->get_busparams_comp); |
|---|
| 1490 | +} |
|---|
| 1491 | + |
|---|
| 1209 | 1492 | static void kvaser_usb_leaf_handle_command(const struct kvaser_usb *dev, |
|---|
| 1210 | 1493 | const struct kvaser_cmd *cmd) |
|---|
| 1211 | 1494 | { |
|---|
| .. | .. |
|---|
| 1242 | 1525 | |
|---|
| 1243 | 1526 | case CMD_TX_ACKNOWLEDGE: |
|---|
| 1244 | 1527 | kvaser_usb_leaf_tx_acknowledge(dev, cmd); |
|---|
| 1528 | + break; |
|---|
| 1529 | + |
|---|
| 1530 | + case CMD_ERROR_EVENT: |
|---|
| 1531 | + kvaser_usb_leaf_error_event(dev, cmd); |
|---|
| 1532 | + break; |
|---|
| 1533 | + |
|---|
| 1534 | + case CMD_GET_BUS_PARAMS_REPLY: |
|---|
| 1535 | + kvaser_usb_leaf_get_busparams_reply(dev, cmd); |
|---|
| 1245 | 1536 | break; |
|---|
| 1246 | 1537 | |
|---|
| 1247 | 1538 | /* Ignored commands */ |
|---|
| .. | .. |
|---|
| 1340 | 1631 | |
|---|
| 1341 | 1632 | static int kvaser_usb_leaf_stop_chip(struct kvaser_usb_net_priv *priv) |
|---|
| 1342 | 1633 | { |
|---|
| 1634 | + struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv; |
|---|
| 1343 | 1635 | int err; |
|---|
| 1344 | 1636 | |
|---|
| 1345 | 1637 | reinit_completion(&priv->stop_comp); |
|---|
| 1638 | + |
|---|
| 1639 | + cancel_delayed_work(&leaf->chip_state_req_work); |
|---|
| 1346 | 1640 | |
|---|
| 1347 | 1641 | err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_STOP_CHIP, |
|---|
| 1348 | 1642 | priv->channel); |
|---|
| .. | .. |
|---|
| 1390 | 1684 | return 0; |
|---|
| 1391 | 1685 | } |
|---|
| 1392 | 1686 | |
|---|
| 1393 | | -static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev) |
|---|
| 1687 | +static int kvaser_usb_leaf_init_channel(struct kvaser_usb_net_priv *priv) |
|---|
| 1688 | +{ |
|---|
| 1689 | + struct kvaser_usb_net_leaf_priv *leaf; |
|---|
| 1690 | + |
|---|
| 1691 | + leaf = devm_kzalloc(&priv->dev->intf->dev, sizeof(*leaf), GFP_KERNEL); |
|---|
| 1692 | + if (!leaf) |
|---|
| 1693 | + return -ENOMEM; |
|---|
| 1694 | + |
|---|
| 1695 | + leaf->net = priv; |
|---|
| 1696 | + INIT_DELAYED_WORK(&leaf->chip_state_req_work, |
|---|
| 1697 | + kvaser_usb_leaf_chip_state_req_work); |
|---|
| 1698 | + |
|---|
| 1699 | + priv->sub_priv = leaf; |
|---|
| 1700 | + |
|---|
| 1701 | + return 0; |
|---|
| 1702 | +} |
|---|
| 1703 | + |
|---|
| 1704 | +static void kvaser_usb_leaf_remove_channel(struct kvaser_usb_net_priv *priv) |
|---|
| 1705 | +{ |
|---|
| 1706 | + struct kvaser_usb_net_leaf_priv *leaf = priv->sub_priv; |
|---|
| 1707 | + |
|---|
| 1708 | + if (leaf) |
|---|
| 1709 | + cancel_delayed_work_sync(&leaf->chip_state_req_work); |
|---|
| 1710 | +} |
|---|
| 1711 | + |
|---|
| 1712 | +static int kvaser_usb_leaf_set_bittiming(const struct net_device *netdev, |
|---|
| 1713 | + const struct kvaser_usb_busparams *busparams) |
|---|
| 1394 | 1714 | { |
|---|
| 1395 | 1715 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
|---|
| 1396 | | - struct can_bittiming *bt = &priv->can.bittiming; |
|---|
| 1397 | 1716 | struct kvaser_usb *dev = priv->dev; |
|---|
| 1398 | 1717 | struct kvaser_cmd *cmd; |
|---|
| 1399 | 1718 | int rc; |
|---|
| .. | .. |
|---|
| 1406 | 1725 | cmd->len = CMD_HEADER_LEN + sizeof(struct kvaser_cmd_busparams); |
|---|
| 1407 | 1726 | cmd->u.busparams.channel = priv->channel; |
|---|
| 1408 | 1727 | cmd->u.busparams.tid = 0xff; |
|---|
| 1409 | | - cmd->u.busparams.bitrate = cpu_to_le32(bt->bitrate); |
|---|
| 1410 | | - cmd->u.busparams.sjw = bt->sjw; |
|---|
| 1411 | | - cmd->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; |
|---|
| 1412 | | - cmd->u.busparams.tseg2 = bt->phase_seg2; |
|---|
| 1413 | | - |
|---|
| 1414 | | - if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
|---|
| 1415 | | - cmd->u.busparams.no_samp = 3; |
|---|
| 1416 | | - else |
|---|
| 1417 | | - cmd->u.busparams.no_samp = 1; |
|---|
| 1728 | + memcpy(&cmd->u.busparams.busparams, busparams, |
|---|
| 1729 | + sizeof(cmd->u.busparams.busparams)); |
|---|
| 1418 | 1730 | |
|---|
| 1419 | 1731 | rc = kvaser_usb_send_cmd(dev, cmd, cmd->len); |
|---|
| 1420 | 1732 | |
|---|
| 1421 | 1733 | kfree(cmd); |
|---|
| 1422 | 1734 | return rc; |
|---|
| 1735 | +} |
|---|
| 1736 | + |
|---|
| 1737 | +static int kvaser_usb_leaf_get_busparams(struct kvaser_usb_net_priv *priv) |
|---|
| 1738 | +{ |
|---|
| 1739 | + int err; |
|---|
| 1740 | + |
|---|
| 1741 | + if (priv->dev->driver_info->family == KVASER_USBCAN) |
|---|
| 1742 | + return -EOPNOTSUPP; |
|---|
| 1743 | + |
|---|
| 1744 | + reinit_completion(&priv->get_busparams_comp); |
|---|
| 1745 | + |
|---|
| 1746 | + err = kvaser_usb_leaf_send_simple_cmd(priv->dev, CMD_GET_BUS_PARAMS, |
|---|
| 1747 | + priv->channel); |
|---|
| 1748 | + if (err) |
|---|
| 1749 | + return err; |
|---|
| 1750 | + |
|---|
| 1751 | + if (!wait_for_completion_timeout(&priv->get_busparams_comp, |
|---|
| 1752 | + msecs_to_jiffies(KVASER_USB_TIMEOUT))) |
|---|
| 1753 | + return -ETIMEDOUT; |
|---|
| 1754 | + |
|---|
| 1755 | + return 0; |
|---|
| 1423 | 1756 | } |
|---|
| 1424 | 1757 | |
|---|
| 1425 | 1758 | static int kvaser_usb_leaf_set_mode(struct net_device *netdev, |
|---|
| .. | .. |
|---|
| 1483 | 1816 | const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = { |
|---|
| 1484 | 1817 | .dev_set_mode = kvaser_usb_leaf_set_mode, |
|---|
| 1485 | 1818 | .dev_set_bittiming = kvaser_usb_leaf_set_bittiming, |
|---|
| 1819 | + .dev_get_busparams = kvaser_usb_leaf_get_busparams, |
|---|
| 1486 | 1820 | .dev_set_data_bittiming = NULL, |
|---|
| 1821 | + .dev_get_data_busparams = NULL, |
|---|
| 1487 | 1822 | .dev_get_berr_counter = kvaser_usb_leaf_get_berr_counter, |
|---|
| 1488 | 1823 | .dev_setup_endpoints = kvaser_usb_leaf_setup_endpoints, |
|---|
| 1489 | 1824 | .dev_init_card = kvaser_usb_leaf_init_card, |
|---|
| 1825 | + .dev_init_channel = kvaser_usb_leaf_init_channel, |
|---|
| 1826 | + .dev_remove_channel = kvaser_usb_leaf_remove_channel, |
|---|
| 1490 | 1827 | .dev_get_software_info = kvaser_usb_leaf_get_software_info, |
|---|
| 1491 | 1828 | .dev_get_software_details = NULL, |
|---|
| 1492 | 1829 | .dev_get_card_info = kvaser_usb_leaf_get_card_info, |
|---|
| 1493 | | - .dev_get_capabilities = NULL, |
|---|
| 1830 | + .dev_get_capabilities = kvaser_usb_leaf_get_capabilities, |
|---|
| 1494 | 1831 | .dev_set_opt_mode = kvaser_usb_leaf_set_opt_mode, |
|---|
| 1495 | 1832 | .dev_start_chip = kvaser_usb_leaf_start_chip, |
|---|
| 1496 | 1833 | .dev_stop_chip = kvaser_usb_leaf_stop_chip, |
|---|