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