Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License as |
| 4 | * published by the Free Software Foundation version 2. |
| 5 | * |
| 6 | * Parts of this driver are based on the following: |
| 7 | * - Kvaser linux leaf driver (version 4.78) |
| 8 | * - CAN driver for esd CAN-USB/2 |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 9 | * - Kvaser linux usbcanII driver (version 5.3) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2002-2006 KVASER AB, Sweden. All rights reserved. |
| 12 | * Copyright (C) 2010 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh |
| 13 | * Copyright (C) 2012 Olivier Sobrie <olivier@sobrie.be> |
Ahmed S. Darwish | a9ca6e1 | 2015-02-02 15:15:55 -0500 | [diff] [blame] | 14 | * Copyright (C) 2015 Valeo S.A. |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 19 | #include <linux/completion.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/netdevice.h> |
| 22 | #include <linux/usb.h> |
| 23 | |
| 24 | #include <linux/can.h> |
| 25 | #include <linux/can/dev.h> |
| 26 | #include <linux/can/error.h> |
| 27 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 28 | #define MAX_RX_URBS 4 |
| 29 | #define START_TIMEOUT 1000 /* msecs */ |
| 30 | #define STOP_TIMEOUT 1000 /* msecs */ |
| 31 | #define USB_SEND_TIMEOUT 1000 /* msecs */ |
| 32 | #define USB_RECV_TIMEOUT 1000 /* msecs */ |
| 33 | #define RX_BUFFER_SIZE 3072 |
| 34 | #define CAN_USB_CLOCK 8000000 |
| 35 | #define MAX_NET_DEVICES 3 |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 36 | #define MAX_USBCAN_NET_DEVICES 2 |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 37 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 38 | /* Kvaser Leaf USB devices */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 39 | #define KVASER_VENDOR_ID 0x0bfd |
| 40 | #define USB_LEAF_DEVEL_PRODUCT_ID 10 |
| 41 | #define USB_LEAF_LITE_PRODUCT_ID 11 |
| 42 | #define USB_LEAF_PRO_PRODUCT_ID 12 |
| 43 | #define USB_LEAF_SPRO_PRODUCT_ID 14 |
| 44 | #define USB_LEAF_PRO_LS_PRODUCT_ID 15 |
| 45 | #define USB_LEAF_PRO_SWC_PRODUCT_ID 16 |
| 46 | #define USB_LEAF_PRO_LIN_PRODUCT_ID 17 |
| 47 | #define USB_LEAF_SPRO_LS_PRODUCT_ID 18 |
| 48 | #define USB_LEAF_SPRO_SWC_PRODUCT_ID 19 |
| 49 | #define USB_MEMO2_DEVEL_PRODUCT_ID 22 |
| 50 | #define USB_MEMO2_HSHS_PRODUCT_ID 23 |
| 51 | #define USB_UPRO_HSHS_PRODUCT_ID 24 |
| 52 | #define USB_LEAF_LITE_GI_PRODUCT_ID 25 |
| 53 | #define USB_LEAF_PRO_OBDII_PRODUCT_ID 26 |
| 54 | #define USB_MEMO2_HSLS_PRODUCT_ID 27 |
| 55 | #define USB_LEAF_LITE_CH_PRODUCT_ID 28 |
| 56 | #define USB_BLACKBIRD_SPRO_PRODUCT_ID 29 |
| 57 | #define USB_OEM_MERCURY_PRODUCT_ID 34 |
| 58 | #define USB_OEM_LEAF_PRODUCT_ID 35 |
| 59 | #define USB_CAN_R_PRODUCT_ID 39 |
Olivier Sobrie | b4f2013 | 2014-04-10 21:44:22 +0200 | [diff] [blame] | 60 | #define USB_LEAF_LITE_V2_PRODUCT_ID 288 |
| 61 | #define USB_MINI_PCIE_HS_PRODUCT_ID 289 |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 62 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 63 | static inline bool kvaser_is_leaf(const struct usb_device_id *id) |
| 64 | { |
| 65 | return id->idProduct >= USB_LEAF_DEVEL_PRODUCT_ID && |
| 66 | id->idProduct <= USB_MINI_PCIE_HS_PRODUCT_ID; |
| 67 | } |
| 68 | |
| 69 | /* Kvaser USBCan-II devices */ |
| 70 | #define USB_USBCAN_REVB_PRODUCT_ID 2 |
| 71 | #define USB_VCI2_PRODUCT_ID 3 |
| 72 | #define USB_USBCAN2_PRODUCT_ID 4 |
| 73 | #define USB_MEMORATOR_PRODUCT_ID 5 |
| 74 | |
| 75 | static inline bool kvaser_is_usbcan(const struct usb_device_id *id) |
| 76 | { |
| 77 | return id->idProduct >= USB_USBCAN_REVB_PRODUCT_ID && |
| 78 | id->idProduct <= USB_MEMORATOR_PRODUCT_ID; |
| 79 | } |
| 80 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 81 | /* USB devices features */ |
| 82 | #define KVASER_HAS_SILENT_MODE BIT(0) |
| 83 | #define KVASER_HAS_TXRX_ERRORS BIT(1) |
| 84 | |
| 85 | /* Message header size */ |
| 86 | #define MSG_HEADER_LEN 2 |
| 87 | |
| 88 | /* Can message flags */ |
| 89 | #define MSG_FLAG_ERROR_FRAME BIT(0) |
| 90 | #define MSG_FLAG_OVERRUN BIT(1) |
| 91 | #define MSG_FLAG_NERR BIT(2) |
| 92 | #define MSG_FLAG_WAKEUP BIT(3) |
| 93 | #define MSG_FLAG_REMOTE_FRAME BIT(4) |
| 94 | #define MSG_FLAG_RESERVED BIT(5) |
| 95 | #define MSG_FLAG_TX_ACK BIT(6) |
| 96 | #define MSG_FLAG_TX_REQUEST BIT(7) |
| 97 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 98 | /* Can states (M16C CxSTRH register) */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 99 | #define M16C_STATE_BUS_RESET BIT(0) |
| 100 | #define M16C_STATE_BUS_ERROR BIT(4) |
| 101 | #define M16C_STATE_BUS_PASSIVE BIT(5) |
| 102 | #define M16C_STATE_BUS_OFF BIT(6) |
| 103 | |
| 104 | /* Can msg ids */ |
| 105 | #define CMD_RX_STD_MESSAGE 12 |
| 106 | #define CMD_TX_STD_MESSAGE 13 |
| 107 | #define CMD_RX_EXT_MESSAGE 14 |
| 108 | #define CMD_TX_EXT_MESSAGE 15 |
| 109 | #define CMD_SET_BUS_PARAMS 16 |
| 110 | #define CMD_GET_BUS_PARAMS 17 |
| 111 | #define CMD_GET_BUS_PARAMS_REPLY 18 |
| 112 | #define CMD_GET_CHIP_STATE 19 |
| 113 | #define CMD_CHIP_STATE_EVENT 20 |
| 114 | #define CMD_SET_CTRL_MODE 21 |
| 115 | #define CMD_GET_CTRL_MODE 22 |
| 116 | #define CMD_GET_CTRL_MODE_REPLY 23 |
| 117 | #define CMD_RESET_CHIP 24 |
| 118 | #define CMD_RESET_CARD 25 |
| 119 | #define CMD_START_CHIP 26 |
| 120 | #define CMD_START_CHIP_REPLY 27 |
| 121 | #define CMD_STOP_CHIP 28 |
| 122 | #define CMD_STOP_CHIP_REPLY 29 |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 123 | |
| 124 | #define CMD_LEAF_GET_CARD_INFO2 32 |
| 125 | #define CMD_USBCAN_RESET_CLOCK 32 |
| 126 | #define CMD_USBCAN_CLOCK_OVERFLOW_EVENT 33 |
| 127 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 128 | #define CMD_GET_CARD_INFO 34 |
| 129 | #define CMD_GET_CARD_INFO_REPLY 35 |
| 130 | #define CMD_GET_SOFTWARE_INFO 38 |
| 131 | #define CMD_GET_SOFTWARE_INFO_REPLY 39 |
| 132 | #define CMD_ERROR_EVENT 45 |
| 133 | #define CMD_FLUSH_QUEUE 48 |
| 134 | #define CMD_RESET_ERROR_COUNTER 49 |
| 135 | #define CMD_TX_ACKNOWLEDGE 50 |
| 136 | #define CMD_CAN_ERROR_EVENT 51 |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 137 | |
| 138 | #define CMD_LEAF_USB_THROTTLE 77 |
| 139 | #define CMD_LEAF_LOG_MESSAGE 106 |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 140 | |
| 141 | /* error factors */ |
| 142 | #define M16C_EF_ACKE BIT(0) |
| 143 | #define M16C_EF_CRCE BIT(1) |
| 144 | #define M16C_EF_FORME BIT(2) |
| 145 | #define M16C_EF_STFE BIT(3) |
| 146 | #define M16C_EF_BITE0 BIT(4) |
| 147 | #define M16C_EF_BITE1 BIT(5) |
| 148 | #define M16C_EF_RCVE BIT(6) |
| 149 | #define M16C_EF_TRE BIT(7) |
| 150 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 151 | /* Only Leaf-based devices can report M16C error factors, |
| 152 | * thus define our own error status flags for USBCANII |
| 153 | */ |
| 154 | #define USBCAN_ERROR_STATE_NONE 0 |
| 155 | #define USBCAN_ERROR_STATE_TX_ERROR BIT(0) |
| 156 | #define USBCAN_ERROR_STATE_RX_ERROR BIT(1) |
| 157 | #define USBCAN_ERROR_STATE_BUSERROR BIT(2) |
| 158 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 159 | /* bittiming parameters */ |
| 160 | #define KVASER_USB_TSEG1_MIN 1 |
| 161 | #define KVASER_USB_TSEG1_MAX 16 |
| 162 | #define KVASER_USB_TSEG2_MIN 1 |
| 163 | #define KVASER_USB_TSEG2_MAX 8 |
| 164 | #define KVASER_USB_SJW_MAX 4 |
| 165 | #define KVASER_USB_BRP_MIN 1 |
| 166 | #define KVASER_USB_BRP_MAX 64 |
| 167 | #define KVASER_USB_BRP_INC 1 |
| 168 | |
| 169 | /* ctrl modes */ |
| 170 | #define KVASER_CTRL_MODE_NORMAL 1 |
| 171 | #define KVASER_CTRL_MODE_SILENT 2 |
| 172 | #define KVASER_CTRL_MODE_SELFRECEPTION 3 |
| 173 | #define KVASER_CTRL_MODE_OFF 4 |
| 174 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 175 | /* Extended CAN identifier flag */ |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 176 | #define KVASER_EXTENDED_FRAME BIT(31) |
| 177 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 178 | /* Kvaser USB CAN dongles are divided into two major families: |
| 179 | * - Leaf: Based on Renesas M32C, running firmware labeled as 'filo' |
| 180 | * - UsbcanII: Based on Renesas M16C, running firmware labeled as 'helios' |
| 181 | */ |
| 182 | enum kvaser_usb_family { |
| 183 | KVASER_LEAF, |
| 184 | KVASER_USBCAN, |
| 185 | }; |
| 186 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 187 | struct kvaser_msg_simple { |
| 188 | u8 tid; |
| 189 | u8 channel; |
| 190 | } __packed; |
| 191 | |
| 192 | struct kvaser_msg_cardinfo { |
| 193 | u8 tid; |
| 194 | u8 nchannels; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 195 | union { |
| 196 | struct { |
| 197 | __le32 serial_number; |
| 198 | __le32 padding; |
| 199 | } __packed leaf0; |
| 200 | struct { |
| 201 | __le32 serial_number_low; |
| 202 | __le32 serial_number_high; |
| 203 | } __packed usbcan0; |
| 204 | } __packed; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 205 | __le32 clock_resolution; |
| 206 | __le32 mfgdate; |
| 207 | u8 ean[8]; |
| 208 | u8 hw_revision; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 209 | union { |
| 210 | struct { |
| 211 | u8 usb_hs_mode; |
| 212 | } __packed leaf1; |
| 213 | struct { |
| 214 | u8 padding; |
| 215 | } __packed usbcan1; |
| 216 | } __packed; |
| 217 | __le16 padding; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 218 | } __packed; |
| 219 | |
| 220 | struct kvaser_msg_cardinfo2 { |
| 221 | u8 tid; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 222 | u8 reserved; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 223 | u8 pcb_id[24]; |
| 224 | __le32 oem_unlock_code; |
| 225 | } __packed; |
| 226 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 227 | struct leaf_msg_softinfo { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 228 | u8 tid; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 229 | u8 padding0; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 230 | __le32 sw_options; |
| 231 | __le32 fw_version; |
| 232 | __le16 max_outstanding_tx; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 233 | __le16 padding1[9]; |
| 234 | } __packed; |
| 235 | |
| 236 | struct usbcan_msg_softinfo { |
| 237 | u8 tid; |
| 238 | u8 fw_name[5]; |
| 239 | __le16 max_outstanding_tx; |
| 240 | u8 padding[6]; |
| 241 | __le32 fw_version; |
| 242 | __le16 checksum; |
| 243 | __le16 sw_options; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 244 | } __packed; |
| 245 | |
| 246 | struct kvaser_msg_busparams { |
| 247 | u8 tid; |
| 248 | u8 channel; |
| 249 | __le32 bitrate; |
| 250 | u8 tseg1; |
| 251 | u8 tseg2; |
| 252 | u8 sjw; |
| 253 | u8 no_samp; |
| 254 | } __packed; |
| 255 | |
| 256 | struct kvaser_msg_tx_can { |
| 257 | u8 channel; |
| 258 | u8 tid; |
| 259 | u8 msg[14]; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 260 | union { |
| 261 | struct { |
| 262 | u8 padding; |
| 263 | u8 flags; |
| 264 | } __packed leaf; |
| 265 | struct { |
| 266 | u8 flags; |
| 267 | u8 padding; |
| 268 | } __packed usbcan; |
| 269 | } __packed; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 270 | } __packed; |
| 271 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 272 | struct kvaser_msg_rx_can_header { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 273 | u8 channel; |
| 274 | u8 flag; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 275 | } __packed; |
| 276 | |
| 277 | struct leaf_msg_rx_can { |
| 278 | u8 channel; |
| 279 | u8 flag; |
| 280 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 281 | __le16 time[3]; |
| 282 | u8 msg[14]; |
| 283 | } __packed; |
| 284 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 285 | struct usbcan_msg_rx_can { |
| 286 | u8 channel; |
| 287 | u8 flag; |
| 288 | |
| 289 | u8 msg[14]; |
| 290 | __le16 time; |
| 291 | } __packed; |
| 292 | |
| 293 | struct leaf_msg_chip_state_event { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 294 | u8 tid; |
| 295 | u8 channel; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 296 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 297 | __le16 time[3]; |
| 298 | u8 tx_errors_count; |
| 299 | u8 rx_errors_count; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 300 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 301 | u8 status; |
| 302 | u8 padding[3]; |
| 303 | } __packed; |
| 304 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 305 | struct usbcan_msg_chip_state_event { |
| 306 | u8 tid; |
| 307 | u8 channel; |
| 308 | |
| 309 | u8 tx_errors_count; |
| 310 | u8 rx_errors_count; |
| 311 | __le16 time; |
| 312 | |
| 313 | u8 status; |
| 314 | u8 padding[3]; |
| 315 | } __packed; |
| 316 | |
| 317 | struct kvaser_msg_tx_acknowledge_header { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 318 | u8 channel; |
| 319 | u8 tid; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 320 | } __packed; |
| 321 | |
| 322 | struct leaf_msg_tx_acknowledge { |
| 323 | u8 channel; |
| 324 | u8 tid; |
| 325 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 326 | __le16 time[3]; |
| 327 | u8 flags; |
| 328 | u8 time_offset; |
| 329 | } __packed; |
| 330 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 331 | struct usbcan_msg_tx_acknowledge { |
| 332 | u8 channel; |
| 333 | u8 tid; |
| 334 | |
| 335 | __le16 time; |
| 336 | __le16 padding; |
| 337 | } __packed; |
| 338 | |
| 339 | struct leaf_msg_error_event { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 340 | u8 tid; |
| 341 | u8 flags; |
| 342 | __le16 time[3]; |
| 343 | u8 channel; |
| 344 | u8 padding; |
| 345 | u8 tx_errors_count; |
| 346 | u8 rx_errors_count; |
| 347 | u8 status; |
| 348 | u8 error_factor; |
| 349 | } __packed; |
| 350 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 351 | struct usbcan_msg_error_event { |
| 352 | u8 tid; |
| 353 | u8 padding; |
| 354 | u8 tx_errors_count_ch0; |
| 355 | u8 rx_errors_count_ch0; |
| 356 | u8 tx_errors_count_ch1; |
| 357 | u8 rx_errors_count_ch1; |
| 358 | u8 status_ch0; |
| 359 | u8 status_ch1; |
| 360 | __le16 time; |
| 361 | } __packed; |
| 362 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 363 | struct kvaser_msg_ctrl_mode { |
| 364 | u8 tid; |
| 365 | u8 channel; |
| 366 | u8 ctrl_mode; |
| 367 | u8 padding[3]; |
| 368 | } __packed; |
| 369 | |
| 370 | struct kvaser_msg_flush_queue { |
| 371 | u8 tid; |
| 372 | u8 channel; |
| 373 | u8 flags; |
| 374 | u8 padding[3]; |
| 375 | } __packed; |
| 376 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 377 | struct leaf_msg_log_message { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 378 | u8 channel; |
| 379 | u8 flags; |
| 380 | __le16 time[3]; |
| 381 | u8 dlc; |
| 382 | u8 time_offset; |
| 383 | __le32 id; |
| 384 | u8 data[8]; |
| 385 | } __packed; |
| 386 | |
| 387 | struct kvaser_msg { |
| 388 | u8 len; |
| 389 | u8 id; |
| 390 | union { |
| 391 | struct kvaser_msg_simple simple; |
| 392 | struct kvaser_msg_cardinfo cardinfo; |
| 393 | struct kvaser_msg_cardinfo2 cardinfo2; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 394 | struct kvaser_msg_busparams busparams; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 395 | |
| 396 | struct kvaser_msg_rx_can_header rx_can_header; |
| 397 | struct kvaser_msg_tx_acknowledge_header tx_acknowledge_header; |
| 398 | |
| 399 | union { |
| 400 | struct leaf_msg_softinfo softinfo; |
| 401 | struct leaf_msg_rx_can rx_can; |
| 402 | struct leaf_msg_chip_state_event chip_state_event; |
| 403 | struct leaf_msg_tx_acknowledge tx_acknowledge; |
| 404 | struct leaf_msg_error_event error_event; |
| 405 | struct leaf_msg_log_message log_message; |
| 406 | } __packed leaf; |
| 407 | |
| 408 | union { |
| 409 | struct usbcan_msg_softinfo softinfo; |
| 410 | struct usbcan_msg_rx_can rx_can; |
| 411 | struct usbcan_msg_chip_state_event chip_state_event; |
| 412 | struct usbcan_msg_tx_acknowledge tx_acknowledge; |
| 413 | struct usbcan_msg_error_event error_event; |
| 414 | } __packed usbcan; |
| 415 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 416 | struct kvaser_msg_tx_can tx_can; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 417 | struct kvaser_msg_ctrl_mode ctrl_mode; |
| 418 | struct kvaser_msg_flush_queue flush_queue; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 419 | } u; |
| 420 | } __packed; |
| 421 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 422 | /* Summary of a kvaser error event, for a unified Leaf/Usbcan error |
| 423 | * handling. Some discrepancies between the two families exist: |
| 424 | * |
| 425 | * - USBCAN firmware does not report M16C "error factors" |
| 426 | * - USBCAN controllers has difficulties reporting if the raised error |
| 427 | * event is for ch0 or ch1. They leave such arbitration to the OS |
| 428 | * driver by letting it compare error counters with previous values |
| 429 | * and decide the error event's channel. Thus for USBCAN, the channel |
| 430 | * field is only advisory. |
| 431 | */ |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 432 | struct kvaser_usb_error_summary { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 433 | u8 channel, status, txerr, rxerr; |
| 434 | union { |
| 435 | struct { |
| 436 | u8 error_factor; |
| 437 | } leaf; |
| 438 | struct { |
| 439 | u8 other_ch_status; |
| 440 | u8 error_state; |
| 441 | } usbcan; |
| 442 | }; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 443 | }; |
| 444 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 445 | /* Context for an outstanding, not yet ACKed, transmission */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 446 | struct kvaser_usb_tx_urb_context { |
| 447 | struct kvaser_usb_net_priv *priv; |
| 448 | u32 echo_index; |
| 449 | int dlc; |
| 450 | }; |
| 451 | |
| 452 | struct kvaser_usb { |
| 453 | struct usb_device *udev; |
| 454 | struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; |
| 455 | |
| 456 | struct usb_endpoint_descriptor *bulk_in, *bulk_out; |
| 457 | struct usb_anchor rx_submitted; |
| 458 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 459 | /* @max_tx_urbs: Firmware-reported maximum number of oustanding, |
| 460 | * not yet ACKed, transmissions on this device. This value is |
| 461 | * also used as a sentinel for marking free tx contexts. |
| 462 | */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 463 | u32 fw_version; |
| 464 | unsigned int nchannels; |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 465 | unsigned int max_tx_urbs; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 466 | enum kvaser_usb_family family; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 467 | |
| 468 | bool rxinitdone; |
| 469 | void *rxbuf[MAX_RX_URBS]; |
| 470 | dma_addr_t rxbuf_dma[MAX_RX_URBS]; |
| 471 | }; |
| 472 | |
| 473 | struct kvaser_usb_net_priv { |
| 474 | struct can_priv can; |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 475 | struct can_berr_counter bec; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 476 | |
| 477 | struct kvaser_usb *dev; |
| 478 | struct net_device *netdev; |
| 479 | int channel; |
| 480 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 481 | struct completion start_comp, stop_comp; |
| 482 | struct usb_anchor tx_submitted; |
| 483 | |
| 484 | spinlock_t tx_contexts_lock; |
| 485 | int active_tx_contexts; |
| 486 | struct kvaser_usb_tx_urb_context tx_contexts[]; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | static const struct usb_device_id kvaser_usb_table[] = { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 490 | /* Leaf family IDs */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 491 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, |
| 492 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, |
| 493 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), |
| 494 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 495 | KVASER_HAS_SILENT_MODE }, |
| 496 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), |
| 497 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 498 | KVASER_HAS_SILENT_MODE }, |
| 499 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), |
| 500 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 501 | KVASER_HAS_SILENT_MODE }, |
| 502 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), |
| 503 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 504 | KVASER_HAS_SILENT_MODE }, |
| 505 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), |
| 506 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 507 | KVASER_HAS_SILENT_MODE }, |
| 508 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), |
| 509 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 510 | KVASER_HAS_SILENT_MODE }, |
| 511 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), |
| 512 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 513 | KVASER_HAS_SILENT_MODE }, |
| 514 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), |
| 515 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 516 | KVASER_HAS_SILENT_MODE }, |
| 517 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), |
| 518 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 519 | KVASER_HAS_SILENT_MODE }, |
| 520 | { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), |
| 521 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 522 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, |
| 523 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), |
| 524 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 525 | KVASER_HAS_SILENT_MODE }, |
| 526 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), |
| 527 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 528 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), |
| 529 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 530 | { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), |
| 531 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 532 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), |
| 533 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 534 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), |
| 535 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 536 | { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), |
| 537 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
Olivier Sobrie | b4f2013 | 2014-04-10 21:44:22 +0200 | [diff] [blame] | 538 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, |
| 539 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 540 | |
| 541 | /* USBCANII family IDs */ |
| 542 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), |
| 543 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 544 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), |
| 545 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 546 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), |
| 547 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 548 | { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), |
| 549 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 550 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 551 | { } |
| 552 | }; |
| 553 | MODULE_DEVICE_TABLE(usb, kvaser_usb_table); |
| 554 | |
| 555 | static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev, |
| 556 | struct kvaser_msg *msg) |
| 557 | { |
| 558 | int actual_len; |
| 559 | |
| 560 | return usb_bulk_msg(dev->udev, |
| 561 | usb_sndbulkpipe(dev->udev, |
| 562 | dev->bulk_out->bEndpointAddress), |
| 563 | msg, msg->len, &actual_len, |
| 564 | USB_SEND_TIMEOUT); |
| 565 | } |
| 566 | |
| 567 | static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id, |
| 568 | struct kvaser_msg *msg) |
| 569 | { |
| 570 | struct kvaser_msg *tmp; |
| 571 | void *buf; |
| 572 | int actual_len; |
| 573 | int err; |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 574 | int pos; |
| 575 | unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 576 | |
| 577 | buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL); |
| 578 | if (!buf) |
| 579 | return -ENOMEM; |
| 580 | |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 581 | do { |
| 582 | err = usb_bulk_msg(dev->udev, |
| 583 | usb_rcvbulkpipe(dev->udev, |
| 584 | dev->bulk_in->bEndpointAddress), |
| 585 | buf, RX_BUFFER_SIZE, &actual_len, |
| 586 | USB_RECV_TIMEOUT); |
| 587 | if (err < 0) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 588 | goto end; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 589 | |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 590 | pos = 0; |
| 591 | while (pos <= actual_len - MSG_HEADER_LEN) { |
| 592 | tmp = buf + pos; |
| 593 | |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 594 | /* Handle messages crossing the USB endpoint max packet |
| 595 | * size boundary. Check kvaser_usb_read_bulk_callback() |
| 596 | * for further details. |
| 597 | */ |
| 598 | if (tmp->len == 0) { |
Ahmed S. Darwish | 7e184c2 | 2015-03-15 11:10:49 -0400 | [diff] [blame] | 599 | pos = round_up(pos, le16_to_cpu(dev->bulk_in-> |
| 600 | wMaxPacketSize)); |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 601 | continue; |
| 602 | } |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 603 | |
| 604 | if (pos + tmp->len > actual_len) { |
| 605 | dev_err(dev->udev->dev.parent, |
| 606 | "Format error\n"); |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | if (tmp->id == id) { |
| 611 | memcpy(msg, tmp, tmp->len); |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | pos += tmp->len; |
| 616 | } |
| 617 | } while (time_before(jiffies, to)); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 618 | |
| 619 | err = -EINVAL; |
| 620 | |
| 621 | end: |
| 622 | kfree(buf); |
| 623 | |
| 624 | return err; |
| 625 | } |
| 626 | |
| 627 | static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev, |
| 628 | u8 msg_id, int channel) |
| 629 | { |
| 630 | struct kvaser_msg *msg; |
| 631 | int rc; |
| 632 | |
| 633 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 634 | if (!msg) |
| 635 | return -ENOMEM; |
| 636 | |
| 637 | msg->id = msg_id; |
| 638 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); |
| 639 | msg->u.simple.channel = channel; |
| 640 | msg->u.simple.tid = 0xff; |
| 641 | |
| 642 | rc = kvaser_usb_send_msg(dev, msg); |
| 643 | |
| 644 | kfree(msg); |
| 645 | return rc; |
| 646 | } |
| 647 | |
| 648 | static int kvaser_usb_get_software_info(struct kvaser_usb *dev) |
| 649 | { |
| 650 | struct kvaser_msg msg; |
| 651 | int err; |
| 652 | |
| 653 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0); |
| 654 | if (err) |
| 655 | return err; |
| 656 | |
| 657 | err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg); |
| 658 | if (err) |
| 659 | return err; |
| 660 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 661 | switch (dev->family) { |
| 662 | case KVASER_LEAF: |
| 663 | dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version); |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 664 | dev->max_tx_urbs = |
| 665 | le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx); |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 666 | break; |
| 667 | case KVASER_USBCAN: |
| 668 | dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 669 | dev->max_tx_urbs = |
| 670 | le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx); |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 671 | break; |
| 672 | } |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int kvaser_usb_get_card_info(struct kvaser_usb *dev) |
| 678 | { |
| 679 | struct kvaser_msg msg; |
| 680 | int err; |
| 681 | |
| 682 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0); |
| 683 | if (err) |
| 684 | return err; |
| 685 | |
| 686 | err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg); |
| 687 | if (err) |
| 688 | return err; |
| 689 | |
| 690 | dev->nchannels = msg.u.cardinfo.nchannels; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 691 | if ((dev->nchannels > MAX_NET_DEVICES) || |
| 692 | (dev->family == KVASER_USBCAN && |
| 693 | dev->nchannels > MAX_USBCAN_NET_DEVICES)) |
Olivier Sobrie | 862474f8 | 2014-02-11 11:01:23 +0100 | [diff] [blame] | 694 | return -EINVAL; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, |
| 700 | const struct kvaser_msg *msg) |
| 701 | { |
| 702 | struct net_device_stats *stats; |
| 703 | struct kvaser_usb_tx_urb_context *context; |
| 704 | struct kvaser_usb_net_priv *priv; |
| 705 | struct sk_buff *skb; |
| 706 | struct can_frame *cf; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 707 | unsigned long flags; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 708 | u8 channel, tid; |
| 709 | |
| 710 | channel = msg->u.tx_acknowledge_header.channel; |
| 711 | tid = msg->u.tx_acknowledge_header.tid; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 712 | |
| 713 | if (channel >= dev->nchannels) { |
| 714 | dev_err(dev->udev->dev.parent, |
| 715 | "Invalid channel number (%d)\n", channel); |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | priv = dev->nets[channel]; |
| 720 | |
| 721 | if (!netif_device_present(priv->netdev)) |
| 722 | return; |
| 723 | |
| 724 | stats = &priv->netdev->stats; |
| 725 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 726 | context = &priv->tx_contexts[tid % dev->max_tx_urbs]; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 727 | |
| 728 | /* Sometimes the state change doesn't come after a bus-off event */ |
| 729 | if (priv->can.restart_ms && |
| 730 | (priv->can.state >= CAN_STATE_BUS_OFF)) { |
| 731 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 732 | if (skb) { |
| 733 | cf->can_id |= CAN_ERR_RESTARTED; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 734 | |
| 735 | stats->rx_packets++; |
| 736 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 737 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 738 | } else { |
| 739 | netdev_err(priv->netdev, |
| 740 | "No memory left for err_skb\n"); |
| 741 | } |
| 742 | |
| 743 | priv->can.can_stats.restarts++; |
| 744 | netif_carrier_on(priv->netdev); |
| 745 | |
| 746 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 747 | } |
| 748 | |
| 749 | stats->tx_packets++; |
| 750 | stats->tx_bytes += context->dlc; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 751 | |
| 752 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
| 753 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 754 | can_get_echo_skb(priv->netdev, context->echo_index); |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 755 | context->echo_index = dev->max_tx_urbs; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 756 | --priv->active_tx_contexts; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 757 | netif_wake_queue(priv->netdev); |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 758 | |
| 759 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static void kvaser_usb_simple_msg_callback(struct urb *urb) |
| 763 | { |
| 764 | struct net_device *netdev = urb->context; |
| 765 | |
| 766 | kfree(urb->transfer_buffer); |
| 767 | |
| 768 | if (urb->status) |
| 769 | netdev_warn(netdev, "urb status received: %d\n", |
| 770 | urb->status); |
| 771 | } |
| 772 | |
| 773 | static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv, |
| 774 | u8 msg_id) |
| 775 | { |
| 776 | struct kvaser_usb *dev = priv->dev; |
| 777 | struct net_device *netdev = priv->netdev; |
| 778 | struct kvaser_msg *msg; |
| 779 | struct urb *urb; |
| 780 | void *buf; |
| 781 | int err; |
| 782 | |
| 783 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 784 | if (!urb) { |
| 785 | netdev_err(netdev, "No memory left for URBs\n"); |
| 786 | return -ENOMEM; |
| 787 | } |
| 788 | |
| 789 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); |
| 790 | if (!buf) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 791 | usb_free_urb(urb); |
| 792 | return -ENOMEM; |
| 793 | } |
| 794 | |
| 795 | msg = (struct kvaser_msg *)buf; |
| 796 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); |
| 797 | msg->id = msg_id; |
| 798 | msg->u.simple.channel = priv->channel; |
| 799 | |
| 800 | usb_fill_bulk_urb(urb, dev->udev, |
| 801 | usb_sndbulkpipe(dev->udev, |
| 802 | dev->bulk_out->bEndpointAddress), |
| 803 | buf, msg->len, |
Ahmed S. Darwish | 3803fa6 | 2015-01-26 07:22:54 +0200 | [diff] [blame] | 804 | kvaser_usb_simple_msg_callback, netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 805 | usb_anchor_urb(urb, &priv->tx_submitted); |
| 806 | |
| 807 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 808 | if (err) { |
| 809 | netdev_err(netdev, "Error transmitting URB\n"); |
| 810 | usb_unanchor_urb(urb); |
| 811 | usb_free_urb(urb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 812 | return err; |
| 813 | } |
| 814 | |
| 815 | usb_free_urb(urb); |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 820 | static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 821 | const struct kvaser_usb_error_summary *es, |
| 822 | struct can_frame *cf) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 823 | { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 824 | struct kvaser_usb *dev = priv->dev; |
| 825 | struct net_device_stats *stats = &priv->netdev->stats; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 826 | enum can_state cur_state, new_state, tx_state, rx_state; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 827 | |
| 828 | netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); |
| 829 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 830 | new_state = cur_state = priv->can.state; |
| 831 | |
| 832 | if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 833 | new_state = CAN_STATE_BUS_OFF; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 834 | else if (es->status & M16C_STATE_BUS_PASSIVE) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 835 | new_state = CAN_STATE_ERROR_PASSIVE; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 836 | else if (es->status & M16C_STATE_BUS_ERROR) { |
Ahmed S. Darwish | a9ca6e1 | 2015-02-02 15:15:55 -0500 | [diff] [blame] | 837 | /* Guard against spurious error events after a busoff */ |
| 838 | if (cur_state < CAN_STATE_BUS_OFF) { |
| 839 | if ((es->txerr >= 128) || (es->rxerr >= 128)) |
| 840 | new_state = CAN_STATE_ERROR_PASSIVE; |
| 841 | else if ((es->txerr >= 96) || (es->rxerr >= 96)) |
| 842 | new_state = CAN_STATE_ERROR_WARNING; |
| 843 | else if (cur_state > CAN_STATE_ERROR_ACTIVE) |
| 844 | new_state = CAN_STATE_ERROR_ACTIVE; |
| 845 | } |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (!es->status) |
| 849 | new_state = CAN_STATE_ERROR_ACTIVE; |
| 850 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 851 | if (new_state != cur_state) { |
| 852 | tx_state = (es->txerr >= es->rxerr) ? new_state : 0; |
| 853 | rx_state = (es->txerr <= es->rxerr) ? new_state : 0; |
| 854 | |
| 855 | can_change_state(priv->netdev, cf, tx_state, rx_state); |
| 856 | } |
| 857 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 858 | if (priv->can.restart_ms && |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 859 | (cur_state >= CAN_STATE_BUS_OFF) && |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 860 | (new_state < CAN_STATE_BUS_OFF)) { |
| 861 | priv->can.can_stats.restarts++; |
| 862 | } |
| 863 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 864 | switch (dev->family) { |
| 865 | case KVASER_LEAF: |
| 866 | if (es->leaf.error_factor) { |
| 867 | priv->can.can_stats.bus_error++; |
| 868 | stats->rx_errors++; |
| 869 | } |
| 870 | break; |
| 871 | case KVASER_USBCAN: |
| 872 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) |
| 873 | stats->tx_errors++; |
| 874 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) |
| 875 | stats->rx_errors++; |
| 876 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { |
| 877 | priv->can.can_stats.bus_error++; |
| 878 | } |
| 879 | break; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | priv->bec.txerr = es->txerr; |
| 883 | priv->bec.rxerr = es->rxerr; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 886 | static void kvaser_usb_rx_error(const struct kvaser_usb *dev, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 887 | const struct kvaser_usb_error_summary *es) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 888 | { |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 889 | struct can_frame *cf, tmp_cf = { .can_id = CAN_ERR_FLAG, .can_dlc = CAN_ERR_DLC }; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 890 | struct sk_buff *skb; |
| 891 | struct net_device_stats *stats; |
| 892 | struct kvaser_usb_net_priv *priv; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 893 | enum can_state old_state, new_state; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 894 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 895 | if (es->channel >= dev->nchannels) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 896 | dev_err(dev->udev->dev.parent, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 897 | "Invalid channel number (%d)\n", es->channel); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 901 | priv = dev->nets[es->channel]; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 902 | stats = &priv->netdev->stats; |
| 903 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 904 | /* Update all of the can interface's state and error counters before |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 905 | * trying any memory allocation that can actually fail with -ENOMEM. |
| 906 | * |
| 907 | * We send a temporary stack-allocated error can frame to |
| 908 | * can_change_state() for the very same reason. |
| 909 | * |
| 910 | * TODO: Split can_change_state() responsibility between updating the |
| 911 | * can interface's state and counters, and the setting up of can error |
| 912 | * frame ID and data to userspace. Remove stack allocation afterwards. |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 913 | */ |
| 914 | old_state = priv->can.state; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 915 | kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf); |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 916 | new_state = priv->can.state; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 917 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 918 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 919 | if (!skb) { |
| 920 | stats->rx_dropped++; |
| 921 | return; |
| 922 | } |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 923 | memcpy(cf, &tmp_cf, sizeof(*cf)); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 924 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 925 | if (new_state != old_state) { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 926 | if (es->status & |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 927 | (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { |
| 928 | if (!priv->can.restart_ms) |
| 929 | kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP); |
| 930 | netif_carrier_off(priv->netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 931 | } |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 932 | |
| 933 | if (priv->can.restart_ms && |
| 934 | (old_state >= CAN_STATE_BUS_OFF) && |
| 935 | (new_state < CAN_STATE_BUS_OFF)) { |
| 936 | cf->can_id |= CAN_ERR_RESTARTED; |
| 937 | netif_carrier_on(priv->netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 941 | switch (dev->family) { |
| 942 | case KVASER_LEAF: |
| 943 | if (es->leaf.error_factor) { |
| 944 | cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 945 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 946 | if (es->leaf.error_factor & M16C_EF_ACKE) |
| 947 | cf->data[3] |= (CAN_ERR_PROT_LOC_ACK); |
| 948 | if (es->leaf.error_factor & M16C_EF_CRCE) |
| 949 | cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ | |
| 950 | CAN_ERR_PROT_LOC_CRC_DEL); |
| 951 | if (es->leaf.error_factor & M16C_EF_FORME) |
| 952 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 953 | if (es->leaf.error_factor & M16C_EF_STFE) |
| 954 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 955 | if (es->leaf.error_factor & M16C_EF_BITE0) |
| 956 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
| 957 | if (es->leaf.error_factor & M16C_EF_BITE1) |
| 958 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
| 959 | if (es->leaf.error_factor & M16C_EF_TRE) |
| 960 | cf->data[2] |= CAN_ERR_PROT_TX; |
| 961 | } |
| 962 | break; |
| 963 | case KVASER_USBCAN: |
| 964 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { |
| 965 | cf->can_id |= CAN_ERR_BUSERROR; |
| 966 | } |
| 967 | break; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 970 | cf->data[6] = es->txerr; |
| 971 | cf->data[7] = es->rxerr; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 972 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 973 | stats->rx_packets++; |
| 974 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 975 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 978 | /* For USBCAN, report error to userspace iff the channels's errors counter |
| 979 | * has changed, or we're the only channel seeing a bus error state. |
| 980 | */ |
| 981 | static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, |
| 982 | struct kvaser_usb_error_summary *es) |
| 983 | { |
| 984 | struct kvaser_usb_net_priv *priv; |
| 985 | int channel; |
| 986 | bool report_error; |
| 987 | |
| 988 | channel = es->channel; |
| 989 | if (channel >= dev->nchannels) { |
| 990 | dev_err(dev->udev->dev.parent, |
| 991 | "Invalid channel number (%d)\n", channel); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | priv = dev->nets[channel]; |
| 996 | report_error = false; |
| 997 | |
| 998 | if (es->txerr != priv->bec.txerr) { |
| 999 | es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; |
| 1000 | report_error = true; |
| 1001 | } |
| 1002 | if (es->rxerr != priv->bec.rxerr) { |
| 1003 | es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; |
| 1004 | report_error = true; |
| 1005 | } |
| 1006 | if ((es->status & M16C_STATE_BUS_ERROR) && |
| 1007 | !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { |
| 1008 | es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; |
| 1009 | report_error = true; |
| 1010 | } |
| 1011 | |
| 1012 | if (report_error) |
| 1013 | kvaser_usb_rx_error(dev, es); |
| 1014 | } |
| 1015 | |
| 1016 | static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev, |
| 1017 | const struct kvaser_msg *msg) |
| 1018 | { |
| 1019 | struct kvaser_usb_error_summary es = { }; |
| 1020 | |
| 1021 | switch (msg->id) { |
| 1022 | /* Sometimes errors are sent as unsolicited chip state events */ |
| 1023 | case CMD_CHIP_STATE_EVENT: |
| 1024 | es.channel = msg->u.usbcan.chip_state_event.channel; |
| 1025 | es.status = msg->u.usbcan.chip_state_event.status; |
| 1026 | es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count; |
| 1027 | es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count; |
| 1028 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1029 | break; |
| 1030 | |
| 1031 | case CMD_CAN_ERROR_EVENT: |
| 1032 | es.channel = 0; |
| 1033 | es.status = msg->u.usbcan.error_event.status_ch0; |
| 1034 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0; |
| 1035 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0; |
| 1036 | es.usbcan.other_ch_status = |
| 1037 | msg->u.usbcan.error_event.status_ch1; |
| 1038 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1039 | |
| 1040 | /* The USBCAN firmware supports up to 2 channels. |
| 1041 | * Now that ch0 was checked, check if ch1 has any errors. |
| 1042 | */ |
| 1043 | if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { |
| 1044 | es.channel = 1; |
| 1045 | es.status = msg->u.usbcan.error_event.status_ch1; |
| 1046 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1; |
| 1047 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1; |
| 1048 | es.usbcan.other_ch_status = |
| 1049 | msg->u.usbcan.error_event.status_ch0; |
| 1050 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1051 | } |
| 1052 | break; |
| 1053 | |
| 1054 | default: |
| 1055 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", |
| 1056 | msg->id); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | static void kvaser_leaf_rx_error(const struct kvaser_usb *dev, |
| 1061 | const struct kvaser_msg *msg) |
| 1062 | { |
| 1063 | struct kvaser_usb_error_summary es = { }; |
| 1064 | |
| 1065 | switch (msg->id) { |
| 1066 | case CMD_CAN_ERROR_EVENT: |
| 1067 | es.channel = msg->u.leaf.error_event.channel; |
| 1068 | es.status = msg->u.leaf.error_event.status; |
| 1069 | es.txerr = msg->u.leaf.error_event.tx_errors_count; |
| 1070 | es.rxerr = msg->u.leaf.error_event.rx_errors_count; |
| 1071 | es.leaf.error_factor = msg->u.leaf.error_event.error_factor; |
| 1072 | break; |
| 1073 | case CMD_LEAF_LOG_MESSAGE: |
| 1074 | es.channel = msg->u.leaf.log_message.channel; |
| 1075 | es.status = msg->u.leaf.log_message.data[0]; |
| 1076 | es.txerr = msg->u.leaf.log_message.data[2]; |
| 1077 | es.rxerr = msg->u.leaf.log_message.data[3]; |
| 1078 | es.leaf.error_factor = msg->u.leaf.log_message.data[1]; |
| 1079 | break; |
| 1080 | case CMD_CHIP_STATE_EVENT: |
| 1081 | es.channel = msg->u.leaf.chip_state_event.channel; |
| 1082 | es.status = msg->u.leaf.chip_state_event.status; |
| 1083 | es.txerr = msg->u.leaf.chip_state_event.tx_errors_count; |
| 1084 | es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count; |
| 1085 | es.leaf.error_factor = 0; |
| 1086 | break; |
| 1087 | default: |
| 1088 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", |
| 1089 | msg->id); |
| 1090 | return; |
| 1091 | } |
| 1092 | |
| 1093 | kvaser_usb_rx_error(dev, &es); |
| 1094 | } |
| 1095 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1096 | static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv, |
| 1097 | const struct kvaser_msg *msg) |
| 1098 | { |
| 1099 | struct can_frame *cf; |
| 1100 | struct sk_buff *skb; |
| 1101 | struct net_device_stats *stats = &priv->netdev->stats; |
| 1102 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1103 | if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1104 | MSG_FLAG_NERR)) { |
| 1105 | netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n", |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1106 | msg->u.rx_can_header.flag); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1107 | |
| 1108 | stats->rx_errors++; |
| 1109 | return; |
| 1110 | } |
| 1111 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1112 | if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) { |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 1113 | stats->rx_over_errors++; |
| 1114 | stats->rx_errors++; |
| 1115 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1116 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 1117 | if (!skb) { |
| 1118 | stats->rx_dropped++; |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | cf->can_id |= CAN_ERR_CRTL; |
| 1123 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 1124 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1125 | stats->rx_packets++; |
| 1126 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 1127 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, |
| 1132 | const struct kvaser_msg *msg) |
| 1133 | { |
| 1134 | struct kvaser_usb_net_priv *priv; |
| 1135 | struct can_frame *cf; |
| 1136 | struct sk_buff *skb; |
| 1137 | struct net_device_stats *stats; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1138 | u8 channel = msg->u.rx_can_header.channel; |
| 1139 | const u8 *rx_msg = NULL; /* GCC */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1140 | |
| 1141 | if (channel >= dev->nchannels) { |
| 1142 | dev_err(dev->udev->dev.parent, |
| 1143 | "Invalid channel number (%d)\n", channel); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | priv = dev->nets[channel]; |
| 1148 | stats = &priv->netdev->stats; |
| 1149 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1150 | if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && |
| 1151 | (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) { |
| 1152 | kvaser_leaf_rx_error(dev, msg); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1153 | return; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1154 | } else if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | |
| 1155 | MSG_FLAG_NERR | |
| 1156 | MSG_FLAG_OVERRUN)) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1157 | kvaser_usb_rx_can_err(priv, msg); |
| 1158 | return; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1159 | } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1160 | netdev_warn(priv->netdev, |
| 1161 | "Unhandled frame (flags: 0x%02x)", |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1162 | msg->u.rx_can_header.flag); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1163 | return; |
| 1164 | } |
| 1165 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1166 | switch (dev->family) { |
| 1167 | case KVASER_LEAF: |
| 1168 | rx_msg = msg->u.leaf.rx_can.msg; |
| 1169 | break; |
| 1170 | case KVASER_USBCAN: |
| 1171 | rx_msg = msg->u.usbcan.rx_can.msg; |
| 1172 | break; |
| 1173 | } |
| 1174 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1175 | skb = alloc_can_skb(priv->netdev, &cf); |
| 1176 | if (!skb) { |
| 1177 | stats->tx_dropped++; |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1181 | if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) { |
| 1182 | cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1183 | if (cf->can_id & KVASER_EXTENDED_FRAME) |
| 1184 | cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; |
| 1185 | else |
| 1186 | cf->can_id &= CAN_SFF_MASK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1187 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1188 | cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1189 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1190 | if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1191 | cf->can_id |= CAN_RTR_FLAG; |
| 1192 | else |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1193 | memcpy(cf->data, &msg->u.leaf.log_message.data, |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1194 | cf->can_dlc); |
| 1195 | } else { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1196 | cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1197 | |
| 1198 | if (msg->id == CMD_RX_EXT_MESSAGE) { |
| 1199 | cf->can_id <<= 18; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1200 | cf->can_id |= ((rx_msg[2] & 0x0f) << 14) | |
| 1201 | ((rx_msg[3] & 0xff) << 6) | |
| 1202 | (rx_msg[4] & 0x3f); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1203 | cf->can_id |= CAN_EFF_FLAG; |
| 1204 | } |
| 1205 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1206 | cf->can_dlc = get_can_dlc(rx_msg[5]); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1207 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1208 | if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1209 | cf->can_id |= CAN_RTR_FLAG; |
| 1210 | else |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1211 | memcpy(cf->data, &rx_msg[6], |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1212 | cf->can_dlc); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1215 | stats->rx_packets++; |
| 1216 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 1217 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev, |
| 1221 | const struct kvaser_msg *msg) |
| 1222 | { |
| 1223 | struct kvaser_usb_net_priv *priv; |
| 1224 | u8 channel = msg->u.simple.channel; |
| 1225 | |
| 1226 | if (channel >= dev->nchannels) { |
| 1227 | dev_err(dev->udev->dev.parent, |
| 1228 | "Invalid channel number (%d)\n", channel); |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | priv = dev->nets[channel]; |
| 1233 | |
| 1234 | if (completion_done(&priv->start_comp) && |
| 1235 | netif_queue_stopped(priv->netdev)) { |
| 1236 | netif_wake_queue(priv->netdev); |
| 1237 | } else { |
| 1238 | netif_start_queue(priv->netdev); |
| 1239 | complete(&priv->start_comp); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev, |
| 1244 | const struct kvaser_msg *msg) |
| 1245 | { |
| 1246 | struct kvaser_usb_net_priv *priv; |
| 1247 | u8 channel = msg->u.simple.channel; |
| 1248 | |
| 1249 | if (channel >= dev->nchannels) { |
| 1250 | dev_err(dev->udev->dev.parent, |
| 1251 | "Invalid channel number (%d)\n", channel); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | priv = dev->nets[channel]; |
| 1256 | |
| 1257 | complete(&priv->stop_comp); |
| 1258 | } |
| 1259 | |
| 1260 | static void kvaser_usb_handle_message(const struct kvaser_usb *dev, |
| 1261 | const struct kvaser_msg *msg) |
| 1262 | { |
| 1263 | switch (msg->id) { |
| 1264 | case CMD_START_CHIP_REPLY: |
| 1265 | kvaser_usb_start_chip_reply(dev, msg); |
| 1266 | break; |
| 1267 | |
| 1268 | case CMD_STOP_CHIP_REPLY: |
| 1269 | kvaser_usb_stop_chip_reply(dev, msg); |
| 1270 | break; |
| 1271 | |
| 1272 | case CMD_RX_STD_MESSAGE: |
| 1273 | case CMD_RX_EXT_MESSAGE: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1274 | kvaser_usb_rx_can_msg(dev, msg); |
| 1275 | break; |
| 1276 | |
| 1277 | case CMD_LEAF_LOG_MESSAGE: |
| 1278 | if (dev->family != KVASER_LEAF) |
| 1279 | goto warn; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1280 | kvaser_usb_rx_can_msg(dev, msg); |
| 1281 | break; |
| 1282 | |
| 1283 | case CMD_CHIP_STATE_EVENT: |
| 1284 | case CMD_CAN_ERROR_EVENT: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1285 | if (dev->family == KVASER_LEAF) |
| 1286 | kvaser_leaf_rx_error(dev, msg); |
| 1287 | else |
| 1288 | kvaser_usbcan_rx_error(dev, msg); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1289 | break; |
| 1290 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1291 | case CMD_TX_ACKNOWLEDGE: |
| 1292 | kvaser_usb_tx_acknowledge(dev, msg); |
| 1293 | break; |
| 1294 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1295 | /* Ignored messages */ |
| 1296 | case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: |
| 1297 | if (dev->family != KVASER_USBCAN) |
| 1298 | goto warn; |
| 1299 | break; |
| 1300 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1301 | default: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1302 | warn: dev_warn(dev->udev->dev.parent, |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1303 | "Unhandled message (%d)\n", msg->id); |
| 1304 | break; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | static void kvaser_usb_read_bulk_callback(struct urb *urb) |
| 1309 | { |
| 1310 | struct kvaser_usb *dev = urb->context; |
| 1311 | struct kvaser_msg *msg; |
| 1312 | int pos = 0; |
| 1313 | int err, i; |
| 1314 | |
| 1315 | switch (urb->status) { |
| 1316 | case 0: |
| 1317 | break; |
| 1318 | case -ENOENT: |
| 1319 | case -ESHUTDOWN: |
| 1320 | return; |
| 1321 | default: |
| 1322 | dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n", |
| 1323 | urb->status); |
| 1324 | goto resubmit_urb; |
| 1325 | } |
| 1326 | |
| 1327 | while (pos <= urb->actual_length - MSG_HEADER_LEN) { |
| 1328 | msg = urb->transfer_buffer + pos; |
| 1329 | |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 1330 | /* The Kvaser firmware can only read and write messages that |
| 1331 | * does not cross the USB's endpoint wMaxPacketSize boundary. |
| 1332 | * If a follow-up command crosses such boundary, firmware puts |
| 1333 | * a placeholder zero-length command in its place then aligns |
| 1334 | * the real command to the next max packet size. |
| 1335 | * |
| 1336 | * Handle such cases or we're going to miss a significant |
| 1337 | * number of events in case of a heavy rx load on the bus. |
| 1338 | */ |
| 1339 | if (msg->len == 0) { |
Ahmed S. Darwish | 7e184c2 | 2015-03-15 11:10:49 -0400 | [diff] [blame] | 1340 | pos = round_up(pos, le16_to_cpu(dev->bulk_in-> |
| 1341 | wMaxPacketSize)); |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 1342 | continue; |
| 1343 | } |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1344 | |
| 1345 | if (pos + msg->len > urb->actual_length) { |
| 1346 | dev_err(dev->udev->dev.parent, "Format error\n"); |
| 1347 | break; |
| 1348 | } |
| 1349 | |
| 1350 | kvaser_usb_handle_message(dev, msg); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1351 | pos += msg->len; |
| 1352 | } |
| 1353 | |
| 1354 | resubmit_urb: |
| 1355 | usb_fill_bulk_urb(urb, dev->udev, |
| 1356 | usb_rcvbulkpipe(dev->udev, |
| 1357 | dev->bulk_in->bEndpointAddress), |
| 1358 | urb->transfer_buffer, RX_BUFFER_SIZE, |
| 1359 | kvaser_usb_read_bulk_callback, dev); |
| 1360 | |
| 1361 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1362 | if (err == -ENODEV) { |
| 1363 | for (i = 0; i < dev->nchannels; i++) { |
| 1364 | if (!dev->nets[i]) |
| 1365 | continue; |
| 1366 | |
| 1367 | netif_device_detach(dev->nets[i]->netdev); |
| 1368 | } |
| 1369 | } else if (err) { |
| 1370 | dev_err(dev->udev->dev.parent, |
| 1371 | "Failed resubmitting read bulk urb: %d\n", err); |
| 1372 | } |
| 1373 | |
| 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev) |
| 1378 | { |
| 1379 | int i, err = 0; |
| 1380 | |
| 1381 | if (dev->rxinitdone) |
| 1382 | return 0; |
| 1383 | |
| 1384 | for (i = 0; i < MAX_RX_URBS; i++) { |
| 1385 | struct urb *urb = NULL; |
| 1386 | u8 *buf = NULL; |
| 1387 | dma_addr_t buf_dma; |
| 1388 | |
| 1389 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1390 | if (!urb) { |
| 1391 | dev_warn(dev->udev->dev.parent, |
| 1392 | "No memory left for URBs\n"); |
| 1393 | err = -ENOMEM; |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, |
| 1398 | GFP_KERNEL, &buf_dma); |
| 1399 | if (!buf) { |
| 1400 | dev_warn(dev->udev->dev.parent, |
| 1401 | "No memory left for USB buffer\n"); |
| 1402 | usb_free_urb(urb); |
| 1403 | err = -ENOMEM; |
| 1404 | break; |
| 1405 | } |
| 1406 | |
| 1407 | usb_fill_bulk_urb(urb, dev->udev, |
| 1408 | usb_rcvbulkpipe(dev->udev, |
| 1409 | dev->bulk_in->bEndpointAddress), |
| 1410 | buf, RX_BUFFER_SIZE, |
| 1411 | kvaser_usb_read_bulk_callback, |
| 1412 | dev); |
| 1413 | urb->transfer_dma = buf_dma; |
| 1414 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1415 | usb_anchor_urb(urb, &dev->rx_submitted); |
| 1416 | |
| 1417 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 1418 | if (err) { |
| 1419 | usb_unanchor_urb(urb); |
| 1420 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, |
| 1421 | buf_dma); |
| 1422 | usb_free_urb(urb); |
| 1423 | break; |
| 1424 | } |
| 1425 | |
| 1426 | dev->rxbuf[i] = buf; |
| 1427 | dev->rxbuf_dma[i] = buf_dma; |
| 1428 | |
| 1429 | usb_free_urb(urb); |
| 1430 | } |
| 1431 | |
| 1432 | if (i == 0) { |
| 1433 | dev_warn(dev->udev->dev.parent, |
| 1434 | "Cannot setup read URBs, error %d\n", err); |
| 1435 | return err; |
| 1436 | } else if (i < MAX_RX_URBS) { |
| 1437 | dev_warn(dev->udev->dev.parent, |
| 1438 | "RX performances may be slow\n"); |
| 1439 | } |
| 1440 | |
| 1441 | dev->rxinitdone = true; |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv) |
| 1447 | { |
| 1448 | struct kvaser_msg *msg; |
| 1449 | int rc; |
| 1450 | |
| 1451 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1452 | if (!msg) |
| 1453 | return -ENOMEM; |
| 1454 | |
| 1455 | msg->id = CMD_SET_CTRL_MODE; |
| 1456 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode); |
| 1457 | msg->u.ctrl_mode.tid = 0xff; |
| 1458 | msg->u.ctrl_mode.channel = priv->channel; |
| 1459 | |
| 1460 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 1461 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; |
| 1462 | else |
| 1463 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; |
| 1464 | |
| 1465 | rc = kvaser_usb_send_msg(priv->dev, msg); |
| 1466 | |
| 1467 | kfree(msg); |
| 1468 | return rc; |
| 1469 | } |
| 1470 | |
| 1471 | static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv) |
| 1472 | { |
| 1473 | int err; |
| 1474 | |
| 1475 | init_completion(&priv->start_comp); |
| 1476 | |
| 1477 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP, |
| 1478 | priv->channel); |
| 1479 | if (err) |
| 1480 | return err; |
| 1481 | |
| 1482 | if (!wait_for_completion_timeout(&priv->start_comp, |
| 1483 | msecs_to_jiffies(START_TIMEOUT))) |
| 1484 | return -ETIMEDOUT; |
| 1485 | |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | static int kvaser_usb_open(struct net_device *netdev) |
| 1490 | { |
| 1491 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1492 | struct kvaser_usb *dev = priv->dev; |
| 1493 | int err; |
| 1494 | |
| 1495 | err = open_candev(netdev); |
| 1496 | if (err) |
| 1497 | return err; |
| 1498 | |
| 1499 | err = kvaser_usb_setup_rx_urbs(dev); |
| 1500 | if (err) |
| 1501 | goto error; |
| 1502 | |
| 1503 | err = kvaser_usb_set_opt_mode(priv); |
| 1504 | if (err) |
| 1505 | goto error; |
| 1506 | |
| 1507 | err = kvaser_usb_start_chip(priv); |
| 1508 | if (err) { |
| 1509 | netdev_warn(netdev, "Cannot start device, error %d\n", err); |
| 1510 | goto error; |
| 1511 | } |
| 1512 | |
| 1513 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1514 | |
| 1515 | return 0; |
| 1516 | |
| 1517 | error: |
| 1518 | close_candev(netdev); |
| 1519 | return err; |
| 1520 | } |
| 1521 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1522 | static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv) |
| 1523 | { |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1524 | int i, max_tx_urbs; |
| 1525 | |
| 1526 | max_tx_urbs = priv->dev->max_tx_urbs; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1527 | |
| 1528 | priv->active_tx_contexts = 0; |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1529 | for (i = 0; i < max_tx_urbs; i++) |
| 1530 | priv->tx_contexts[i].echo_index = max_tx_urbs; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* This method might sleep. Do not call it in the atomic context |
| 1534 | * of URB completions. |
| 1535 | */ |
| 1536 | static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv) |
| 1537 | { |
| 1538 | usb_kill_anchored_urbs(&priv->tx_submitted); |
| 1539 | kvaser_usb_reset_tx_urb_contexts(priv); |
| 1540 | } |
| 1541 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1542 | static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) |
| 1543 | { |
| 1544 | int i; |
| 1545 | |
| 1546 | usb_kill_anchored_urbs(&dev->rx_submitted); |
| 1547 | |
| 1548 | for (i = 0; i < MAX_RX_URBS; i++) |
| 1549 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, |
| 1550 | dev->rxbuf[i], |
| 1551 | dev->rxbuf_dma[i]); |
| 1552 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1553 | for (i = 0; i < dev->nchannels; i++) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1554 | struct kvaser_usb_net_priv *priv = dev->nets[i]; |
| 1555 | |
| 1556 | if (priv) |
| 1557 | kvaser_usb_unlink_tx_urbs(priv); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv) |
| 1562 | { |
| 1563 | int err; |
| 1564 | |
| 1565 | init_completion(&priv->stop_comp); |
| 1566 | |
| 1567 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP, |
| 1568 | priv->channel); |
| 1569 | if (err) |
| 1570 | return err; |
| 1571 | |
| 1572 | if (!wait_for_completion_timeout(&priv->stop_comp, |
| 1573 | msecs_to_jiffies(STOP_TIMEOUT))) |
| 1574 | return -ETIMEDOUT; |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv) |
| 1580 | { |
| 1581 | struct kvaser_msg *msg; |
| 1582 | int rc; |
| 1583 | |
| 1584 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1585 | if (!msg) |
| 1586 | return -ENOMEM; |
| 1587 | |
| 1588 | msg->id = CMD_FLUSH_QUEUE; |
| 1589 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue); |
| 1590 | msg->u.flush_queue.channel = priv->channel; |
| 1591 | msg->u.flush_queue.flags = 0x00; |
| 1592 | |
| 1593 | rc = kvaser_usb_send_msg(priv->dev, msg); |
| 1594 | |
| 1595 | kfree(msg); |
| 1596 | return rc; |
| 1597 | } |
| 1598 | |
| 1599 | static int kvaser_usb_close(struct net_device *netdev) |
| 1600 | { |
| 1601 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1602 | struct kvaser_usb *dev = priv->dev; |
| 1603 | int err; |
| 1604 | |
| 1605 | netif_stop_queue(netdev); |
| 1606 | |
| 1607 | err = kvaser_usb_flush_queue(priv); |
| 1608 | if (err) |
| 1609 | netdev_warn(netdev, "Cannot flush queue, error %d\n", err); |
| 1610 | |
| 1611 | if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel)) |
| 1612 | netdev_warn(netdev, "Cannot reset card, error %d\n", err); |
| 1613 | |
| 1614 | err = kvaser_usb_stop_chip(priv); |
| 1615 | if (err) |
| 1616 | netdev_warn(netdev, "Cannot stop device, error %d\n", err); |
| 1617 | |
Ahmed S. Darwish | 889b77f | 2015-01-05 12:52:06 -0500 | [diff] [blame] | 1618 | /* reset tx contexts */ |
| 1619 | kvaser_usb_unlink_tx_urbs(priv); |
| 1620 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1621 | priv->can.state = CAN_STATE_STOPPED; |
| 1622 | close_candev(priv->netdev); |
| 1623 | |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | static void kvaser_usb_write_bulk_callback(struct urb *urb) |
| 1628 | { |
| 1629 | struct kvaser_usb_tx_urb_context *context = urb->context; |
| 1630 | struct kvaser_usb_net_priv *priv; |
| 1631 | struct net_device *netdev; |
| 1632 | |
| 1633 | if (WARN_ON(!context)) |
| 1634 | return; |
| 1635 | |
| 1636 | priv = context->priv; |
| 1637 | netdev = priv->netdev; |
| 1638 | |
| 1639 | kfree(urb->transfer_buffer); |
| 1640 | |
| 1641 | if (!netif_device_present(netdev)) |
| 1642 | return; |
| 1643 | |
| 1644 | if (urb->status) |
| 1645 | netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); |
| 1646 | } |
| 1647 | |
| 1648 | static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, |
| 1649 | struct net_device *netdev) |
| 1650 | { |
| 1651 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1652 | struct kvaser_usb *dev = priv->dev; |
| 1653 | struct net_device_stats *stats = &netdev->stats; |
| 1654 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 1655 | struct kvaser_usb_tx_urb_context *context = NULL; |
| 1656 | struct urb *urb; |
| 1657 | void *buf; |
| 1658 | struct kvaser_msg *msg; |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1659 | int i, err, ret = NETDEV_TX_OK; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1660 | u8 *msg_tx_can_flags = NULL; /* GCC */ |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1661 | unsigned long flags; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1662 | |
| 1663 | if (can_dropped_invalid_skb(netdev, skb)) |
| 1664 | return NETDEV_TX_OK; |
| 1665 | |
| 1666 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1667 | if (!urb) { |
| 1668 | netdev_err(netdev, "No memory left for URBs\n"); |
| 1669 | stats->tx_dropped++; |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1670 | dev_kfree_skb(skb); |
| 1671 | return NETDEV_TX_OK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); |
| 1675 | if (!buf) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1676 | stats->tx_dropped++; |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1677 | dev_kfree_skb(skb); |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1678 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | msg = buf; |
| 1682 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1683 | msg->u.tx_can.channel = priv->channel; |
| 1684 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1685 | switch (dev->family) { |
| 1686 | case KVASER_LEAF: |
| 1687 | msg_tx_can_flags = &msg->u.tx_can.leaf.flags; |
| 1688 | break; |
| 1689 | case KVASER_USBCAN: |
| 1690 | msg_tx_can_flags = &msg->u.tx_can.usbcan.flags; |
| 1691 | break; |
| 1692 | } |
| 1693 | |
| 1694 | *msg_tx_can_flags = 0; |
| 1695 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1696 | if (cf->can_id & CAN_EFF_FLAG) { |
| 1697 | msg->id = CMD_TX_EXT_MESSAGE; |
| 1698 | msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f; |
| 1699 | msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f; |
| 1700 | msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f; |
| 1701 | msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff; |
| 1702 | msg->u.tx_can.msg[4] = cf->can_id & 0x3f; |
| 1703 | } else { |
| 1704 | msg->id = CMD_TX_STD_MESSAGE; |
| 1705 | msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f; |
| 1706 | msg->u.tx_can.msg[1] = cf->can_id & 0x3f; |
| 1707 | } |
| 1708 | |
| 1709 | msg->u.tx_can.msg[5] = cf->can_dlc; |
| 1710 | memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc); |
| 1711 | |
| 1712 | if (cf->can_id & CAN_RTR_FLAG) |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1713 | *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1714 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1715 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1716 | for (i = 0; i < dev->max_tx_urbs; i++) { |
| 1717 | if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1718 | context = &priv->tx_contexts[i]; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1719 | |
| 1720 | context->echo_index = i; |
| 1721 | can_put_echo_skb(skb, netdev, context->echo_index); |
| 1722 | ++priv->active_tx_contexts; |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1723 | if (priv->active_tx_contexts >= dev->max_tx_urbs) |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1724 | netif_stop_queue(netdev); |
| 1725 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1726 | break; |
| 1727 | } |
| 1728 | } |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1729 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1730 | |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1731 | /* This should never happen; it implies a flow control bug */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1732 | if (!context) { |
| 1733 | netdev_warn(netdev, "cannot find free context\n"); |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1734 | |
| 1735 | kfree(buf); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1736 | ret = NETDEV_TX_BUSY; |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1737 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | context->priv = priv; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1741 | context->dlc = cf->can_dlc; |
| 1742 | |
| 1743 | msg->u.tx_can.tid = context->echo_index; |
| 1744 | |
| 1745 | usb_fill_bulk_urb(urb, dev->udev, |
| 1746 | usb_sndbulkpipe(dev->udev, |
| 1747 | dev->bulk_out->bEndpointAddress), |
| 1748 | buf, msg->len, |
| 1749 | kvaser_usb_write_bulk_callback, context); |
| 1750 | usb_anchor_urb(urb, &priv->tx_submitted); |
| 1751 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1752 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1753 | if (unlikely(err)) { |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1754 | spin_lock_irqsave(&priv->tx_contexts_lock, flags); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1755 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1756 | can_free_echo_skb(netdev, context->echo_index); |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1757 | context->echo_index = dev->max_tx_urbs; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1758 | --priv->active_tx_contexts; |
| 1759 | netif_wake_queue(netdev); |
| 1760 | |
| 1761 | spin_unlock_irqrestore(&priv->tx_contexts_lock, flags); |
| 1762 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1763 | usb_unanchor_urb(urb); |
| 1764 | |
| 1765 | stats->tx_dropped++; |
| 1766 | |
| 1767 | if (err == -ENODEV) |
| 1768 | netif_device_detach(netdev); |
| 1769 | else |
| 1770 | netdev_warn(netdev, "Failed tx_urb %d\n", err); |
| 1771 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1772 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1773 | } |
| 1774 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1775 | ret = NETDEV_TX_OK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1776 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1777 | freeurb: |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1778 | usb_free_urb(urb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1779 | return ret; |
| 1780 | } |
| 1781 | |
| 1782 | static const struct net_device_ops kvaser_usb_netdev_ops = { |
| 1783 | .ndo_open = kvaser_usb_open, |
| 1784 | .ndo_stop = kvaser_usb_close, |
| 1785 | .ndo_start_xmit = kvaser_usb_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1786 | .ndo_change_mtu = can_change_mtu, |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1787 | }; |
| 1788 | |
| 1789 | static const struct can_bittiming_const kvaser_usb_bittiming_const = { |
| 1790 | .name = "kvaser_usb", |
| 1791 | .tseg1_min = KVASER_USB_TSEG1_MIN, |
| 1792 | .tseg1_max = KVASER_USB_TSEG1_MAX, |
| 1793 | .tseg2_min = KVASER_USB_TSEG2_MIN, |
| 1794 | .tseg2_max = KVASER_USB_TSEG2_MAX, |
| 1795 | .sjw_max = KVASER_USB_SJW_MAX, |
| 1796 | .brp_min = KVASER_USB_BRP_MIN, |
| 1797 | .brp_max = KVASER_USB_BRP_MAX, |
| 1798 | .brp_inc = KVASER_USB_BRP_INC, |
| 1799 | }; |
| 1800 | |
| 1801 | static int kvaser_usb_set_bittiming(struct net_device *netdev) |
| 1802 | { |
| 1803 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1804 | struct can_bittiming *bt = &priv->can.bittiming; |
| 1805 | struct kvaser_usb *dev = priv->dev; |
| 1806 | struct kvaser_msg *msg; |
| 1807 | int rc; |
| 1808 | |
| 1809 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1810 | if (!msg) |
| 1811 | return -ENOMEM; |
| 1812 | |
| 1813 | msg->id = CMD_SET_BUS_PARAMS; |
| 1814 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams); |
| 1815 | msg->u.busparams.channel = priv->channel; |
| 1816 | msg->u.busparams.tid = 0xff; |
| 1817 | msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate); |
| 1818 | msg->u.busparams.sjw = bt->sjw; |
| 1819 | msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; |
| 1820 | msg->u.busparams.tseg2 = bt->phase_seg2; |
| 1821 | |
| 1822 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 1823 | msg->u.busparams.no_samp = 3; |
| 1824 | else |
| 1825 | msg->u.busparams.no_samp = 1; |
| 1826 | |
| 1827 | rc = kvaser_usb_send_msg(dev, msg); |
| 1828 | |
| 1829 | kfree(msg); |
| 1830 | return rc; |
| 1831 | } |
| 1832 | |
| 1833 | static int kvaser_usb_set_mode(struct net_device *netdev, |
| 1834 | enum can_mode mode) |
| 1835 | { |
| 1836 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1837 | int err; |
| 1838 | |
| 1839 | switch (mode) { |
| 1840 | case CAN_MODE_START: |
| 1841 | err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP); |
| 1842 | if (err) |
| 1843 | return err; |
| 1844 | break; |
| 1845 | default: |
| 1846 | return -EOPNOTSUPP; |
| 1847 | } |
| 1848 | |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | static int kvaser_usb_get_berr_counter(const struct net_device *netdev, |
| 1853 | struct can_berr_counter *bec) |
| 1854 | { |
| 1855 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1856 | |
| 1857 | *bec = priv->bec; |
| 1858 | |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
| 1862 | static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) |
| 1863 | { |
| 1864 | int i; |
| 1865 | |
| 1866 | for (i = 0; i < dev->nchannels; i++) { |
| 1867 | if (!dev->nets[i]) |
| 1868 | continue; |
| 1869 | |
Ahmed S. Darwish | c637aab | 2015-03-14 09:11:47 -0400 | [diff] [blame] | 1870 | unregister_candev(dev->nets[i]->netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | kvaser_usb_unlink_all_urbs(dev); |
| 1874 | |
| 1875 | for (i = 0; i < dev->nchannels; i++) { |
| 1876 | if (!dev->nets[i]) |
| 1877 | continue; |
| 1878 | |
| 1879 | free_candev(dev->nets[i]->netdev); |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | static int kvaser_usb_init_one(struct usb_interface *intf, |
| 1884 | const struct usb_device_id *id, int channel) |
| 1885 | { |
| 1886 | struct kvaser_usb *dev = usb_get_intfdata(intf); |
| 1887 | struct net_device *netdev; |
| 1888 | struct kvaser_usb_net_priv *priv; |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1889 | int err; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1890 | |
Ahmed S. Darwish | 5e7e6e0 | 2015-01-05 12:57:13 -0500 | [diff] [blame] | 1891 | err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); |
| 1892 | if (err) |
| 1893 | return err; |
| 1894 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 1895 | netdev = alloc_candev(sizeof(*priv) + |
| 1896 | dev->max_tx_urbs * sizeof(*priv->tx_contexts), |
| 1897 | dev->max_tx_urbs); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1898 | if (!netdev) { |
| 1899 | dev_err(&intf->dev, "Cannot alloc candev\n"); |
| 1900 | return -ENOMEM; |
| 1901 | } |
| 1902 | |
| 1903 | priv = netdev_priv(netdev); |
| 1904 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1905 | init_usb_anchor(&priv->tx_submitted); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1906 | init_completion(&priv->start_comp); |
| 1907 | init_completion(&priv->stop_comp); |
| 1908 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1909 | priv->dev = dev; |
| 1910 | priv->netdev = netdev; |
| 1911 | priv->channel = channel; |
| 1912 | |
Ahmed S. Darwish | a9dc960 | 2015-03-14 09:02:49 -0400 | [diff] [blame] | 1913 | spin_lock_init(&priv->tx_contexts_lock); |
| 1914 | kvaser_usb_reset_tx_urb_contexts(priv); |
| 1915 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1916 | priv->can.state = CAN_STATE_STOPPED; |
| 1917 | priv->can.clock.freq = CAN_USB_CLOCK; |
| 1918 | priv->can.bittiming_const = &kvaser_usb_bittiming_const; |
| 1919 | priv->can.do_set_bittiming = kvaser_usb_set_bittiming; |
| 1920 | priv->can.do_set_mode = kvaser_usb_set_mode; |
| 1921 | if (id->driver_info & KVASER_HAS_TXRX_ERRORS) |
| 1922 | priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter; |
| 1923 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; |
| 1924 | if (id->driver_info & KVASER_HAS_SILENT_MODE) |
| 1925 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; |
| 1926 | |
| 1927 | netdev->flags |= IFF_ECHO; |
| 1928 | |
| 1929 | netdev->netdev_ops = &kvaser_usb_netdev_ops; |
| 1930 | |
| 1931 | SET_NETDEV_DEV(netdev, &intf->dev); |
Christopher R. Baker | 3e66d01 | 2014-03-08 11:00:20 -0500 | [diff] [blame] | 1932 | netdev->dev_id = channel; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1933 | |
| 1934 | dev->nets[channel] = priv; |
| 1935 | |
| 1936 | err = register_candev(netdev); |
| 1937 | if (err) { |
| 1938 | dev_err(&intf->dev, "Failed to register can device\n"); |
| 1939 | free_candev(netdev); |
| 1940 | dev->nets[channel] = NULL; |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
| 1944 | netdev_dbg(netdev, "device registered\n"); |
| 1945 | |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1949 | static int kvaser_usb_get_endpoints(const struct usb_interface *intf, |
| 1950 | struct usb_endpoint_descriptor **in, |
| 1951 | struct usb_endpoint_descriptor **out) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1952 | { |
| 1953 | const struct usb_host_interface *iface_desc; |
| 1954 | struct usb_endpoint_descriptor *endpoint; |
| 1955 | int i; |
| 1956 | |
| 1957 | iface_desc = &intf->altsetting[0]; |
| 1958 | |
| 1959 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 1960 | endpoint = &iface_desc->endpoint[i].desc; |
| 1961 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1962 | if (!*in && usb_endpoint_is_bulk_in(endpoint)) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1963 | *in = endpoint; |
| 1964 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1965 | if (!*out && usb_endpoint_is_bulk_out(endpoint)) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1966 | *out = endpoint; |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* use first bulk endpoint for in and out */ |
| 1969 | if (*in && *out) |
| 1970 | return 0; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1971 | } |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1972 | |
| 1973 | return -ENODEV; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | static int kvaser_usb_probe(struct usb_interface *intf, |
| 1977 | const struct usb_device_id *id) |
| 1978 | { |
| 1979 | struct kvaser_usb *dev; |
| 1980 | int err = -ENOMEM; |
Ahmed S. Darwish | 14c10c2 | 2015-01-26 07:24:06 +0200 | [diff] [blame] | 1981 | int i, retry = 3; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1982 | |
| 1983 | dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); |
| 1984 | if (!dev) |
| 1985 | return -ENOMEM; |
| 1986 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1987 | if (kvaser_is_leaf(id)) { |
| 1988 | dev->family = KVASER_LEAF; |
| 1989 | } else if (kvaser_is_usbcan(id)) { |
| 1990 | dev->family = KVASER_USBCAN; |
| 1991 | } else { |
| 1992 | dev_err(&intf->dev, |
| 1993 | "Product ID (%d) does not belong to any known Kvaser USB family", |
| 1994 | id->idProduct); |
| 1995 | return -ENODEV; |
| 1996 | } |
| 1997 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1998 | err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out); |
| 1999 | if (err) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 2000 | dev_err(&intf->dev, "Cannot get usb endpoint(s)"); |
| 2001 | return err; |
| 2002 | } |
| 2003 | |
| 2004 | dev->udev = interface_to_usbdev(intf); |
| 2005 | |
| 2006 | init_usb_anchor(&dev->rx_submitted); |
| 2007 | |
| 2008 | usb_set_intfdata(intf, dev); |
| 2009 | |
Ahmed S. Darwish | 14c10c2 | 2015-01-26 07:24:06 +0200 | [diff] [blame] | 2010 | /* On some x86 laptops, plugging a Kvaser device again after |
| 2011 | * an unplug makes the firmware always ignore the very first |
| 2012 | * command. For such a case, provide some room for retries |
| 2013 | * instead of completely exiting the driver. |
| 2014 | */ |
| 2015 | do { |
| 2016 | err = kvaser_usb_get_software_info(dev); |
| 2017 | } while (--retry && err == -ETIMEDOUT); |
| 2018 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 2019 | if (err) { |
| 2020 | dev_err(&intf->dev, |
| 2021 | "Cannot get software infos, error %d\n", err); |
| 2022 | return err; |
| 2023 | } |
| 2024 | |
Ahmed S. Darwish | 082d70b | 2015-03-15 11:03:38 -0400 | [diff] [blame] | 2025 | dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n", |
| 2026 | ((dev->fw_version >> 24) & 0xff), |
| 2027 | ((dev->fw_version >> 16) & 0xff), |
| 2028 | (dev->fw_version & 0xffff)); |
| 2029 | |
| 2030 | dev_dbg(&intf->dev, "Max oustanding tx = %d URBs\n", dev->max_tx_urbs); |
| 2031 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 2032 | err = kvaser_usb_get_card_info(dev); |
| 2033 | if (err) { |
| 2034 | dev_err(&intf->dev, |
| 2035 | "Cannot get card infos, error %d\n", err); |
| 2036 | return err; |
| 2037 | } |
| 2038 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 2039 | for (i = 0; i < dev->nchannels; i++) { |
| 2040 | err = kvaser_usb_init_one(intf, id, i); |
| 2041 | if (err) { |
| 2042 | kvaser_usb_remove_interfaces(dev); |
| 2043 | return err; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | static void kvaser_usb_disconnect(struct usb_interface *intf) |
| 2051 | { |
| 2052 | struct kvaser_usb *dev = usb_get_intfdata(intf); |
| 2053 | |
| 2054 | usb_set_intfdata(intf, NULL); |
| 2055 | |
| 2056 | if (!dev) |
| 2057 | return; |
| 2058 | |
| 2059 | kvaser_usb_remove_interfaces(dev); |
| 2060 | } |
| 2061 | |
| 2062 | static struct usb_driver kvaser_usb_driver = { |
| 2063 | .name = "kvaser_usb", |
| 2064 | .probe = kvaser_usb_probe, |
| 2065 | .disconnect = kvaser_usb_disconnect, |
| 2066 | .id_table = kvaser_usb_table, |
| 2067 | }; |
| 2068 | |
| 2069 | module_usb_driver(kvaser_usb_driver); |
| 2070 | |
| 2071 | MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); |
| 2072 | MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices"); |
| 2073 | MODULE_LICENSE("GPL v2"); |