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 | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 18 | #include <linux/completion.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/usb.h> |
| 22 | |
| 23 | #include <linux/can.h> |
| 24 | #include <linux/can/dev.h> |
| 25 | #include <linux/can/error.h> |
| 26 | |
| 27 | #define MAX_TX_URBS 16 |
| 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 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 445 | struct kvaser_usb_tx_urb_context { |
| 446 | struct kvaser_usb_net_priv *priv; |
| 447 | u32 echo_index; |
| 448 | int dlc; |
| 449 | }; |
| 450 | |
| 451 | struct kvaser_usb { |
| 452 | struct usb_device *udev; |
| 453 | struct kvaser_usb_net_priv *nets[MAX_NET_DEVICES]; |
| 454 | |
| 455 | struct usb_endpoint_descriptor *bulk_in, *bulk_out; |
| 456 | struct usb_anchor rx_submitted; |
| 457 | |
| 458 | u32 fw_version; |
| 459 | unsigned int nchannels; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 460 | enum kvaser_usb_family family; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 461 | |
| 462 | bool rxinitdone; |
| 463 | void *rxbuf[MAX_RX_URBS]; |
| 464 | dma_addr_t rxbuf_dma[MAX_RX_URBS]; |
| 465 | }; |
| 466 | |
| 467 | struct kvaser_usb_net_priv { |
| 468 | struct can_priv can; |
| 469 | |
| 470 | atomic_t active_tx_urbs; |
| 471 | struct usb_anchor tx_submitted; |
| 472 | struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS]; |
| 473 | |
| 474 | struct completion start_comp, stop_comp; |
| 475 | |
| 476 | struct kvaser_usb *dev; |
| 477 | struct net_device *netdev; |
| 478 | int channel; |
| 479 | |
| 480 | struct can_berr_counter bec; |
| 481 | }; |
| 482 | |
| 483 | static const struct usb_device_id kvaser_usb_table[] = { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 484 | /* Leaf family IDs */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 485 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_DEVEL_PRODUCT_ID) }, |
| 486 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_PRODUCT_ID) }, |
| 487 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_PRODUCT_ID), |
| 488 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 489 | KVASER_HAS_SILENT_MODE }, |
| 490 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_PRODUCT_ID), |
| 491 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 492 | KVASER_HAS_SILENT_MODE }, |
| 493 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LS_PRODUCT_ID), |
| 494 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 495 | KVASER_HAS_SILENT_MODE }, |
| 496 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_SWC_PRODUCT_ID), |
| 497 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 498 | KVASER_HAS_SILENT_MODE }, |
| 499 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_LIN_PRODUCT_ID), |
| 500 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 501 | KVASER_HAS_SILENT_MODE }, |
| 502 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_LS_PRODUCT_ID), |
| 503 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 504 | KVASER_HAS_SILENT_MODE }, |
| 505 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_SPRO_SWC_PRODUCT_ID), |
| 506 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 507 | KVASER_HAS_SILENT_MODE }, |
| 508 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_DEVEL_PRODUCT_ID), |
| 509 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 510 | KVASER_HAS_SILENT_MODE }, |
| 511 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSHS_PRODUCT_ID), |
| 512 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 513 | KVASER_HAS_SILENT_MODE }, |
| 514 | { USB_DEVICE(KVASER_VENDOR_ID, USB_UPRO_HSHS_PRODUCT_ID), |
| 515 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 516 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_GI_PRODUCT_ID) }, |
| 517 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_PRO_OBDII_PRODUCT_ID), |
| 518 | .driver_info = KVASER_HAS_TXRX_ERRORS | |
| 519 | KVASER_HAS_SILENT_MODE }, |
| 520 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMO2_HSLS_PRODUCT_ID), |
| 521 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 522 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_CH_PRODUCT_ID), |
| 523 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 524 | { USB_DEVICE(KVASER_VENDOR_ID, USB_BLACKBIRD_SPRO_PRODUCT_ID), |
| 525 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 526 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_MERCURY_PRODUCT_ID), |
| 527 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 528 | { USB_DEVICE(KVASER_VENDOR_ID, USB_OEM_LEAF_PRODUCT_ID), |
| 529 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 530 | { USB_DEVICE(KVASER_VENDOR_ID, USB_CAN_R_PRODUCT_ID), |
| 531 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
Olivier Sobrie | b4f2013 | 2014-04-10 21:44:22 +0200 | [diff] [blame] | 532 | { USB_DEVICE(KVASER_VENDOR_ID, USB_LEAF_LITE_V2_PRODUCT_ID) }, |
| 533 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MINI_PCIE_HS_PRODUCT_ID) }, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 534 | |
| 535 | /* USBCANII family IDs */ |
| 536 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN2_PRODUCT_ID), |
| 537 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 538 | { USB_DEVICE(KVASER_VENDOR_ID, USB_USBCAN_REVB_PRODUCT_ID), |
| 539 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 540 | { USB_DEVICE(KVASER_VENDOR_ID, USB_MEMORATOR_PRODUCT_ID), |
| 541 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 542 | { USB_DEVICE(KVASER_VENDOR_ID, USB_VCI2_PRODUCT_ID), |
| 543 | .driver_info = KVASER_HAS_TXRX_ERRORS }, |
| 544 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 545 | { } |
| 546 | }; |
| 547 | MODULE_DEVICE_TABLE(usb, kvaser_usb_table); |
| 548 | |
| 549 | static inline int kvaser_usb_send_msg(const struct kvaser_usb *dev, |
| 550 | struct kvaser_msg *msg) |
| 551 | { |
| 552 | int actual_len; |
| 553 | |
| 554 | return usb_bulk_msg(dev->udev, |
| 555 | usb_sndbulkpipe(dev->udev, |
| 556 | dev->bulk_out->bEndpointAddress), |
| 557 | msg, msg->len, &actual_len, |
| 558 | USB_SEND_TIMEOUT); |
| 559 | } |
| 560 | |
| 561 | static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id, |
| 562 | struct kvaser_msg *msg) |
| 563 | { |
| 564 | struct kvaser_msg *tmp; |
| 565 | void *buf; |
| 566 | int actual_len; |
| 567 | int err; |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 568 | int pos; |
| 569 | unsigned long to = jiffies + msecs_to_jiffies(USB_RECV_TIMEOUT); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 570 | |
| 571 | buf = kzalloc(RX_BUFFER_SIZE, GFP_KERNEL); |
| 572 | if (!buf) |
| 573 | return -ENOMEM; |
| 574 | |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 575 | do { |
| 576 | err = usb_bulk_msg(dev->udev, |
| 577 | usb_rcvbulkpipe(dev->udev, |
| 578 | dev->bulk_in->bEndpointAddress), |
| 579 | buf, RX_BUFFER_SIZE, &actual_len, |
| 580 | USB_RECV_TIMEOUT); |
| 581 | if (err < 0) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 582 | goto end; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 583 | |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 584 | pos = 0; |
| 585 | while (pos <= actual_len - MSG_HEADER_LEN) { |
| 586 | tmp = buf + pos; |
| 587 | |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 588 | /* Handle messages crossing the USB endpoint max packet |
| 589 | * size boundary. Check kvaser_usb_read_bulk_callback() |
| 590 | * for further details. |
| 591 | */ |
| 592 | if (tmp->len == 0) { |
| 593 | pos = round_up(pos, |
| 594 | dev->bulk_in->wMaxPacketSize); |
| 595 | continue; |
| 596 | } |
Olivier Sobrie | e59e36e | 2014-04-10 21:44:21 +0200 | [diff] [blame] | 597 | |
| 598 | if (pos + tmp->len > actual_len) { |
| 599 | dev_err(dev->udev->dev.parent, |
| 600 | "Format error\n"); |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | if (tmp->id == id) { |
| 605 | memcpy(msg, tmp, tmp->len); |
| 606 | goto end; |
| 607 | } |
| 608 | |
| 609 | pos += tmp->len; |
| 610 | } |
| 611 | } while (time_before(jiffies, to)); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 612 | |
| 613 | err = -EINVAL; |
| 614 | |
| 615 | end: |
| 616 | kfree(buf); |
| 617 | |
| 618 | return err; |
| 619 | } |
| 620 | |
| 621 | static int kvaser_usb_send_simple_msg(const struct kvaser_usb *dev, |
| 622 | u8 msg_id, int channel) |
| 623 | { |
| 624 | struct kvaser_msg *msg; |
| 625 | int rc; |
| 626 | |
| 627 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 628 | if (!msg) |
| 629 | return -ENOMEM; |
| 630 | |
| 631 | msg->id = msg_id; |
| 632 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); |
| 633 | msg->u.simple.channel = channel; |
| 634 | msg->u.simple.tid = 0xff; |
| 635 | |
| 636 | rc = kvaser_usb_send_msg(dev, msg); |
| 637 | |
| 638 | kfree(msg); |
| 639 | return rc; |
| 640 | } |
| 641 | |
| 642 | static int kvaser_usb_get_software_info(struct kvaser_usb *dev) |
| 643 | { |
| 644 | struct kvaser_msg msg; |
| 645 | int err; |
| 646 | |
| 647 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_SOFTWARE_INFO, 0); |
| 648 | if (err) |
| 649 | return err; |
| 650 | |
| 651 | err = kvaser_usb_wait_msg(dev, CMD_GET_SOFTWARE_INFO_REPLY, &msg); |
| 652 | if (err) |
| 653 | return err; |
| 654 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 655 | switch (dev->family) { |
| 656 | case KVASER_LEAF: |
| 657 | dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version); |
| 658 | break; |
| 659 | case KVASER_USBCAN: |
| 660 | dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); |
| 661 | break; |
| 662 | } |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int kvaser_usb_get_card_info(struct kvaser_usb *dev) |
| 668 | { |
| 669 | struct kvaser_msg msg; |
| 670 | int err; |
| 671 | |
| 672 | err = kvaser_usb_send_simple_msg(dev, CMD_GET_CARD_INFO, 0); |
| 673 | if (err) |
| 674 | return err; |
| 675 | |
| 676 | err = kvaser_usb_wait_msg(dev, CMD_GET_CARD_INFO_REPLY, &msg); |
| 677 | if (err) |
| 678 | return err; |
| 679 | |
| 680 | dev->nchannels = msg.u.cardinfo.nchannels; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 681 | if ((dev->nchannels > MAX_NET_DEVICES) || |
| 682 | (dev->family == KVASER_USBCAN && |
| 683 | dev->nchannels > MAX_USBCAN_NET_DEVICES)) |
Olivier Sobrie | 862474f8 | 2014-02-11 11:01:23 +0100 | [diff] [blame] | 684 | return -EINVAL; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev, |
| 690 | const struct kvaser_msg *msg) |
| 691 | { |
| 692 | struct net_device_stats *stats; |
| 693 | struct kvaser_usb_tx_urb_context *context; |
| 694 | struct kvaser_usb_net_priv *priv; |
| 695 | struct sk_buff *skb; |
| 696 | struct can_frame *cf; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 697 | u8 channel, tid; |
| 698 | |
| 699 | channel = msg->u.tx_acknowledge_header.channel; |
| 700 | tid = msg->u.tx_acknowledge_header.tid; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 701 | |
| 702 | if (channel >= dev->nchannels) { |
| 703 | dev_err(dev->udev->dev.parent, |
| 704 | "Invalid channel number (%d)\n", channel); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | priv = dev->nets[channel]; |
| 709 | |
| 710 | if (!netif_device_present(priv->netdev)) |
| 711 | return; |
| 712 | |
| 713 | stats = &priv->netdev->stats; |
| 714 | |
| 715 | context = &priv->tx_contexts[tid % MAX_TX_URBS]; |
| 716 | |
| 717 | /* Sometimes the state change doesn't come after a bus-off event */ |
| 718 | if (priv->can.restart_ms && |
| 719 | (priv->can.state >= CAN_STATE_BUS_OFF)) { |
| 720 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 721 | if (skb) { |
| 722 | cf->can_id |= CAN_ERR_RESTARTED; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 723 | |
| 724 | stats->rx_packets++; |
| 725 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 726 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 727 | } else { |
| 728 | netdev_err(priv->netdev, |
| 729 | "No memory left for err_skb\n"); |
| 730 | } |
| 731 | |
| 732 | priv->can.can_stats.restarts++; |
| 733 | netif_carrier_on(priv->netdev); |
| 734 | |
| 735 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 736 | } |
| 737 | |
| 738 | stats->tx_packets++; |
| 739 | stats->tx_bytes += context->dlc; |
| 740 | can_get_echo_skb(priv->netdev, context->echo_index); |
| 741 | |
| 742 | context->echo_index = MAX_TX_URBS; |
| 743 | atomic_dec(&priv->active_tx_urbs); |
| 744 | |
| 745 | netif_wake_queue(priv->netdev); |
| 746 | } |
| 747 | |
| 748 | static void kvaser_usb_simple_msg_callback(struct urb *urb) |
| 749 | { |
| 750 | struct net_device *netdev = urb->context; |
| 751 | |
| 752 | kfree(urb->transfer_buffer); |
| 753 | |
| 754 | if (urb->status) |
| 755 | netdev_warn(netdev, "urb status received: %d\n", |
| 756 | urb->status); |
| 757 | } |
| 758 | |
| 759 | static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv, |
| 760 | u8 msg_id) |
| 761 | { |
| 762 | struct kvaser_usb *dev = priv->dev; |
| 763 | struct net_device *netdev = priv->netdev; |
| 764 | struct kvaser_msg *msg; |
| 765 | struct urb *urb; |
| 766 | void *buf; |
| 767 | int err; |
| 768 | |
| 769 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 770 | if (!urb) { |
| 771 | netdev_err(netdev, "No memory left for URBs\n"); |
| 772 | return -ENOMEM; |
| 773 | } |
| 774 | |
| 775 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); |
| 776 | if (!buf) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 777 | usb_free_urb(urb); |
| 778 | return -ENOMEM; |
| 779 | } |
| 780 | |
| 781 | msg = (struct kvaser_msg *)buf; |
| 782 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_simple); |
| 783 | msg->id = msg_id; |
| 784 | msg->u.simple.channel = priv->channel; |
| 785 | |
| 786 | usb_fill_bulk_urb(urb, dev->udev, |
| 787 | usb_sndbulkpipe(dev->udev, |
| 788 | dev->bulk_out->bEndpointAddress), |
| 789 | buf, msg->len, |
Ahmed S. Darwish | 3803fa6 | 2015-01-26 07:22:54 +0200 | [diff] [blame] | 790 | kvaser_usb_simple_msg_callback, netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 791 | usb_anchor_urb(urb, &priv->tx_submitted); |
| 792 | |
| 793 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 794 | if (err) { |
| 795 | netdev_err(netdev, "Error transmitting URB\n"); |
| 796 | usb_unanchor_urb(urb); |
| 797 | usb_free_urb(urb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 798 | return err; |
| 799 | } |
| 800 | |
| 801 | usb_free_urb(urb); |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv) |
| 807 | { |
| 808 | int i; |
| 809 | |
| 810 | usb_kill_anchored_urbs(&priv->tx_submitted); |
| 811 | atomic_set(&priv->active_tx_urbs, 0); |
| 812 | |
| 813 | for (i = 0; i < MAX_TX_URBS; i++) |
| 814 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; |
| 815 | } |
| 816 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 817 | 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] | 818 | const struct kvaser_usb_error_summary *es, |
| 819 | struct can_frame *cf) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 820 | { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 821 | struct kvaser_usb *dev = priv->dev; |
| 822 | struct net_device_stats *stats = &priv->netdev->stats; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 823 | enum can_state cur_state, new_state, tx_state, rx_state; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 824 | |
| 825 | netdev_dbg(priv->netdev, "Error status: 0x%02x\n", es->status); |
| 826 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 827 | new_state = cur_state = priv->can.state; |
| 828 | |
| 829 | if (es->status & (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 830 | new_state = CAN_STATE_BUS_OFF; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 831 | else if (es->status & M16C_STATE_BUS_PASSIVE) |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 832 | new_state = CAN_STATE_ERROR_PASSIVE; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 833 | else if (es->status & M16C_STATE_BUS_ERROR) { |
Ahmed S. Darwish | a9ca6e1 | 2015-02-02 15:15:55 -0500 | [diff] [blame] | 834 | /* Guard against spurious error events after a busoff */ |
| 835 | if (cur_state < CAN_STATE_BUS_OFF) { |
| 836 | if ((es->txerr >= 128) || (es->rxerr >= 128)) |
| 837 | new_state = CAN_STATE_ERROR_PASSIVE; |
| 838 | else if ((es->txerr >= 96) || (es->rxerr >= 96)) |
| 839 | new_state = CAN_STATE_ERROR_WARNING; |
| 840 | else if (cur_state > CAN_STATE_ERROR_ACTIVE) |
| 841 | new_state = CAN_STATE_ERROR_ACTIVE; |
| 842 | } |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | if (!es->status) |
| 846 | new_state = CAN_STATE_ERROR_ACTIVE; |
| 847 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 848 | if (new_state != cur_state) { |
| 849 | tx_state = (es->txerr >= es->rxerr) ? new_state : 0; |
| 850 | rx_state = (es->txerr <= es->rxerr) ? new_state : 0; |
| 851 | |
| 852 | can_change_state(priv->netdev, cf, tx_state, rx_state); |
| 853 | } |
| 854 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 855 | if (priv->can.restart_ms && |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 856 | (cur_state >= CAN_STATE_BUS_OFF) && |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 857 | (new_state < CAN_STATE_BUS_OFF)) { |
| 858 | priv->can.can_stats.restarts++; |
| 859 | } |
| 860 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 861 | switch (dev->family) { |
| 862 | case KVASER_LEAF: |
| 863 | if (es->leaf.error_factor) { |
| 864 | priv->can.can_stats.bus_error++; |
| 865 | stats->rx_errors++; |
| 866 | } |
| 867 | break; |
| 868 | case KVASER_USBCAN: |
| 869 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_TX_ERROR) |
| 870 | stats->tx_errors++; |
| 871 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_RX_ERROR) |
| 872 | stats->rx_errors++; |
| 873 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { |
| 874 | priv->can.can_stats.bus_error++; |
| 875 | } |
| 876 | break; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | priv->bec.txerr = es->txerr; |
| 880 | priv->bec.rxerr = es->rxerr; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 883 | static void kvaser_usb_rx_error(const struct kvaser_usb *dev, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 884 | const struct kvaser_usb_error_summary *es) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 885 | { |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 886 | 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] | 887 | struct sk_buff *skb; |
| 888 | struct net_device_stats *stats; |
| 889 | struct kvaser_usb_net_priv *priv; |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 890 | enum can_state old_state, new_state; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 891 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 892 | if (es->channel >= dev->nchannels) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 893 | dev_err(dev->udev->dev.parent, |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 894 | "Invalid channel number (%d)\n", es->channel); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 895 | return; |
| 896 | } |
| 897 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 898 | priv = dev->nets[es->channel]; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 899 | stats = &priv->netdev->stats; |
| 900 | |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 901 | /* 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] | 902 | * trying any memory allocation that can actually fail with -ENOMEM. |
| 903 | * |
| 904 | * We send a temporary stack-allocated error can frame to |
| 905 | * can_change_state() for the very same reason. |
| 906 | * |
| 907 | * TODO: Split can_change_state() responsibility between updating the |
| 908 | * can interface's state and counters, and the setting up of can error |
| 909 | * frame ID and data to userspace. Remove stack allocation afterwards. |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 910 | */ |
| 911 | old_state = priv->can.state; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 912 | kvaser_usb_rx_error_update_can_state(priv, es, &tmp_cf); |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 913 | new_state = priv->can.state; |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 914 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 915 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 916 | if (!skb) { |
| 917 | stats->rx_dropped++; |
| 918 | return; |
| 919 | } |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 920 | memcpy(cf, &tmp_cf, sizeof(*cf)); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 921 | |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 922 | if (new_state != old_state) { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 923 | if (es->status & |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 924 | (M16C_STATE_BUS_OFF | M16C_STATE_BUS_RESET)) { |
| 925 | if (!priv->can.restart_ms) |
| 926 | kvaser_usb_simple_msg_async(priv, CMD_STOP_CHIP); |
| 927 | netif_carrier_off(priv->netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 928 | } |
Ahmed S. Darwish | 96d7f10 | 2015-01-26 07:29:15 +0200 | [diff] [blame] | 929 | |
| 930 | if (priv->can.restart_ms && |
| 931 | (old_state >= CAN_STATE_BUS_OFF) && |
| 932 | (new_state < CAN_STATE_BUS_OFF)) { |
| 933 | cf->can_id |= CAN_ERR_RESTARTED; |
| 934 | netif_carrier_on(priv->netdev); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 938 | switch (dev->family) { |
| 939 | case KVASER_LEAF: |
| 940 | if (es->leaf.error_factor) { |
| 941 | cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 942 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 943 | if (es->leaf.error_factor & M16C_EF_ACKE) |
| 944 | cf->data[3] |= (CAN_ERR_PROT_LOC_ACK); |
| 945 | if (es->leaf.error_factor & M16C_EF_CRCE) |
| 946 | cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ | |
| 947 | CAN_ERR_PROT_LOC_CRC_DEL); |
| 948 | if (es->leaf.error_factor & M16C_EF_FORME) |
| 949 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 950 | if (es->leaf.error_factor & M16C_EF_STFE) |
| 951 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 952 | if (es->leaf.error_factor & M16C_EF_BITE0) |
| 953 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
| 954 | if (es->leaf.error_factor & M16C_EF_BITE1) |
| 955 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
| 956 | if (es->leaf.error_factor & M16C_EF_TRE) |
| 957 | cf->data[2] |= CAN_ERR_PROT_TX; |
| 958 | } |
| 959 | break; |
| 960 | case KVASER_USBCAN: |
| 961 | if (es->usbcan.error_state & USBCAN_ERROR_STATE_BUSERROR) { |
| 962 | cf->can_id |= CAN_ERR_BUSERROR; |
| 963 | } |
| 964 | break; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 967 | cf->data[6] = es->txerr; |
| 968 | cf->data[7] = es->rxerr; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 969 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 970 | stats->rx_packets++; |
| 971 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 972 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 975 | /* For USBCAN, report error to userspace iff the channels's errors counter |
| 976 | * has changed, or we're the only channel seeing a bus error state. |
| 977 | */ |
| 978 | static void kvaser_usbcan_conditionally_rx_error(const struct kvaser_usb *dev, |
| 979 | struct kvaser_usb_error_summary *es) |
| 980 | { |
| 981 | struct kvaser_usb_net_priv *priv; |
| 982 | int channel; |
| 983 | bool report_error; |
| 984 | |
| 985 | channel = es->channel; |
| 986 | if (channel >= dev->nchannels) { |
| 987 | dev_err(dev->udev->dev.parent, |
| 988 | "Invalid channel number (%d)\n", channel); |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | priv = dev->nets[channel]; |
| 993 | report_error = false; |
| 994 | |
| 995 | if (es->txerr != priv->bec.txerr) { |
| 996 | es->usbcan.error_state |= USBCAN_ERROR_STATE_TX_ERROR; |
| 997 | report_error = true; |
| 998 | } |
| 999 | if (es->rxerr != priv->bec.rxerr) { |
| 1000 | es->usbcan.error_state |= USBCAN_ERROR_STATE_RX_ERROR; |
| 1001 | report_error = true; |
| 1002 | } |
| 1003 | if ((es->status & M16C_STATE_BUS_ERROR) && |
| 1004 | !(es->usbcan.other_ch_status & M16C_STATE_BUS_ERROR)) { |
| 1005 | es->usbcan.error_state |= USBCAN_ERROR_STATE_BUSERROR; |
| 1006 | report_error = true; |
| 1007 | } |
| 1008 | |
| 1009 | if (report_error) |
| 1010 | kvaser_usb_rx_error(dev, es); |
| 1011 | } |
| 1012 | |
| 1013 | static void kvaser_usbcan_rx_error(const struct kvaser_usb *dev, |
| 1014 | const struct kvaser_msg *msg) |
| 1015 | { |
| 1016 | struct kvaser_usb_error_summary es = { }; |
| 1017 | |
| 1018 | switch (msg->id) { |
| 1019 | /* Sometimes errors are sent as unsolicited chip state events */ |
| 1020 | case CMD_CHIP_STATE_EVENT: |
| 1021 | es.channel = msg->u.usbcan.chip_state_event.channel; |
| 1022 | es.status = msg->u.usbcan.chip_state_event.status; |
| 1023 | es.txerr = msg->u.usbcan.chip_state_event.tx_errors_count; |
| 1024 | es.rxerr = msg->u.usbcan.chip_state_event.rx_errors_count; |
| 1025 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1026 | break; |
| 1027 | |
| 1028 | case CMD_CAN_ERROR_EVENT: |
| 1029 | es.channel = 0; |
| 1030 | es.status = msg->u.usbcan.error_event.status_ch0; |
| 1031 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch0; |
| 1032 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch0; |
| 1033 | es.usbcan.other_ch_status = |
| 1034 | msg->u.usbcan.error_event.status_ch1; |
| 1035 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1036 | |
| 1037 | /* The USBCAN firmware supports up to 2 channels. |
| 1038 | * Now that ch0 was checked, check if ch1 has any errors. |
| 1039 | */ |
| 1040 | if (dev->nchannels == MAX_USBCAN_NET_DEVICES) { |
| 1041 | es.channel = 1; |
| 1042 | es.status = msg->u.usbcan.error_event.status_ch1; |
| 1043 | es.txerr = msg->u.usbcan.error_event.tx_errors_count_ch1; |
| 1044 | es.rxerr = msg->u.usbcan.error_event.rx_errors_count_ch1; |
| 1045 | es.usbcan.other_ch_status = |
| 1046 | msg->u.usbcan.error_event.status_ch0; |
| 1047 | kvaser_usbcan_conditionally_rx_error(dev, &es); |
| 1048 | } |
| 1049 | break; |
| 1050 | |
| 1051 | default: |
| 1052 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", |
| 1053 | msg->id); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static void kvaser_leaf_rx_error(const struct kvaser_usb *dev, |
| 1058 | const struct kvaser_msg *msg) |
| 1059 | { |
| 1060 | struct kvaser_usb_error_summary es = { }; |
| 1061 | |
| 1062 | switch (msg->id) { |
| 1063 | case CMD_CAN_ERROR_EVENT: |
| 1064 | es.channel = msg->u.leaf.error_event.channel; |
| 1065 | es.status = msg->u.leaf.error_event.status; |
| 1066 | es.txerr = msg->u.leaf.error_event.tx_errors_count; |
| 1067 | es.rxerr = msg->u.leaf.error_event.rx_errors_count; |
| 1068 | es.leaf.error_factor = msg->u.leaf.error_event.error_factor; |
| 1069 | break; |
| 1070 | case CMD_LEAF_LOG_MESSAGE: |
| 1071 | es.channel = msg->u.leaf.log_message.channel; |
| 1072 | es.status = msg->u.leaf.log_message.data[0]; |
| 1073 | es.txerr = msg->u.leaf.log_message.data[2]; |
| 1074 | es.rxerr = msg->u.leaf.log_message.data[3]; |
| 1075 | es.leaf.error_factor = msg->u.leaf.log_message.data[1]; |
| 1076 | break; |
| 1077 | case CMD_CHIP_STATE_EVENT: |
| 1078 | es.channel = msg->u.leaf.chip_state_event.channel; |
| 1079 | es.status = msg->u.leaf.chip_state_event.status; |
| 1080 | es.txerr = msg->u.leaf.chip_state_event.tx_errors_count; |
| 1081 | es.rxerr = msg->u.leaf.chip_state_event.rx_errors_count; |
| 1082 | es.leaf.error_factor = 0; |
| 1083 | break; |
| 1084 | default: |
| 1085 | dev_err(dev->udev->dev.parent, "Invalid msg id (%d)\n", |
| 1086 | msg->id); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | kvaser_usb_rx_error(dev, &es); |
| 1091 | } |
| 1092 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1093 | static void kvaser_usb_rx_can_err(const struct kvaser_usb_net_priv *priv, |
| 1094 | const struct kvaser_msg *msg) |
| 1095 | { |
| 1096 | struct can_frame *cf; |
| 1097 | struct sk_buff *skb; |
| 1098 | struct net_device_stats *stats = &priv->netdev->stats; |
| 1099 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1100 | if (msg->u.rx_can_header.flag & (MSG_FLAG_ERROR_FRAME | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1101 | MSG_FLAG_NERR)) { |
| 1102 | netdev_err(priv->netdev, "Unknow error (flags: 0x%02x)\n", |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1103 | msg->u.rx_can_header.flag); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1104 | |
| 1105 | stats->rx_errors++; |
| 1106 | return; |
| 1107 | } |
| 1108 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1109 | if (msg->u.rx_can_header.flag & MSG_FLAG_OVERRUN) { |
Ahmed S. Darwish | 3b07a44 | 2015-01-26 07:27:19 +0200 | [diff] [blame] | 1110 | stats->rx_over_errors++; |
| 1111 | stats->rx_errors++; |
| 1112 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1113 | skb = alloc_can_err_skb(priv->netdev, &cf); |
| 1114 | if (!skb) { |
| 1115 | stats->rx_dropped++; |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | cf->can_id |= CAN_ERR_CRTL; |
| 1120 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 1121 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1122 | stats->rx_packets++; |
| 1123 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 1124 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | static void kvaser_usb_rx_can_msg(const struct kvaser_usb *dev, |
| 1129 | const struct kvaser_msg *msg) |
| 1130 | { |
| 1131 | struct kvaser_usb_net_priv *priv; |
| 1132 | struct can_frame *cf; |
| 1133 | struct sk_buff *skb; |
| 1134 | struct net_device_stats *stats; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1135 | u8 channel = msg->u.rx_can_header.channel; |
| 1136 | const u8 *rx_msg = NULL; /* GCC */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1137 | |
| 1138 | if (channel >= dev->nchannels) { |
| 1139 | dev_err(dev->udev->dev.parent, |
| 1140 | "Invalid channel number (%d)\n", channel); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | priv = dev->nets[channel]; |
| 1145 | stats = &priv->netdev->stats; |
| 1146 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1147 | if ((msg->u.rx_can_header.flag & MSG_FLAG_ERROR_FRAME) && |
| 1148 | (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE)) { |
| 1149 | kvaser_leaf_rx_error(dev, msg); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 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_ERROR_FRAME | |
| 1152 | MSG_FLAG_NERR | |
| 1153 | MSG_FLAG_OVERRUN)) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1154 | kvaser_usb_rx_can_err(priv, msg); |
| 1155 | return; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1156 | } else if (msg->u.rx_can_header.flag & ~MSG_FLAG_REMOTE_FRAME) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1157 | netdev_warn(priv->netdev, |
| 1158 | "Unhandled frame (flags: 0x%02x)", |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1159 | msg->u.rx_can_header.flag); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1160 | return; |
| 1161 | } |
| 1162 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1163 | switch (dev->family) { |
| 1164 | case KVASER_LEAF: |
| 1165 | rx_msg = msg->u.leaf.rx_can.msg; |
| 1166 | break; |
| 1167 | case KVASER_USBCAN: |
| 1168 | rx_msg = msg->u.usbcan.rx_can.msg; |
| 1169 | break; |
| 1170 | } |
| 1171 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1172 | skb = alloc_can_skb(priv->netdev, &cf); |
| 1173 | if (!skb) { |
| 1174 | stats->tx_dropped++; |
| 1175 | return; |
| 1176 | } |
| 1177 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1178 | if (dev->family == KVASER_LEAF && msg->id == CMD_LEAF_LOG_MESSAGE) { |
| 1179 | cf->can_id = le32_to_cpu(msg->u.leaf.log_message.id); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1180 | if (cf->can_id & KVASER_EXTENDED_FRAME) |
| 1181 | cf->can_id &= CAN_EFF_MASK | CAN_EFF_FLAG; |
| 1182 | else |
| 1183 | cf->can_id &= CAN_SFF_MASK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1184 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1185 | cf->can_dlc = get_can_dlc(msg->u.leaf.log_message.dlc); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1186 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1187 | if (msg->u.leaf.log_message.flags & MSG_FLAG_REMOTE_FRAME) |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1188 | cf->can_id |= CAN_RTR_FLAG; |
| 1189 | else |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1190 | memcpy(cf->data, &msg->u.leaf.log_message.data, |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1191 | cf->can_dlc); |
| 1192 | } else { |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1193 | cf->can_id = ((rx_msg[0] & 0x1f) << 6) | (rx_msg[1] & 0x3f); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1194 | |
| 1195 | if (msg->id == CMD_RX_EXT_MESSAGE) { |
| 1196 | cf->can_id <<= 18; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1197 | cf->can_id |= ((rx_msg[2] & 0x0f) << 14) | |
| 1198 | ((rx_msg[3] & 0xff) << 6) | |
| 1199 | (rx_msg[4] & 0x3f); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1200 | cf->can_id |= CAN_EFF_FLAG; |
| 1201 | } |
| 1202 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1203 | cf->can_dlc = get_can_dlc(rx_msg[5]); |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1204 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1205 | if (msg->u.rx_can_header.flag & MSG_FLAG_REMOTE_FRAME) |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1206 | cf->can_id |= CAN_RTR_FLAG; |
| 1207 | else |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1208 | memcpy(cf->data, &rx_msg[6], |
Jonas Peterson | a90f13b | 2013-05-07 22:05:23 +0200 | [diff] [blame] | 1209 | cf->can_dlc); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1210 | } |
| 1211 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1212 | stats->rx_packets++; |
| 1213 | stats->rx_bytes += cf->can_dlc; |
Ahmed S. Darwish | a58518c | 2015-01-11 15:49:52 -0500 | [diff] [blame] | 1214 | netif_rx(skb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static void kvaser_usb_start_chip_reply(const struct kvaser_usb *dev, |
| 1218 | const struct kvaser_msg *msg) |
| 1219 | { |
| 1220 | struct kvaser_usb_net_priv *priv; |
| 1221 | u8 channel = msg->u.simple.channel; |
| 1222 | |
| 1223 | if (channel >= dev->nchannels) { |
| 1224 | dev_err(dev->udev->dev.parent, |
| 1225 | "Invalid channel number (%d)\n", channel); |
| 1226 | return; |
| 1227 | } |
| 1228 | |
| 1229 | priv = dev->nets[channel]; |
| 1230 | |
| 1231 | if (completion_done(&priv->start_comp) && |
| 1232 | netif_queue_stopped(priv->netdev)) { |
| 1233 | netif_wake_queue(priv->netdev); |
| 1234 | } else { |
| 1235 | netif_start_queue(priv->netdev); |
| 1236 | complete(&priv->start_comp); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | static void kvaser_usb_stop_chip_reply(const struct kvaser_usb *dev, |
| 1241 | const struct kvaser_msg *msg) |
| 1242 | { |
| 1243 | struct kvaser_usb_net_priv *priv; |
| 1244 | u8 channel = msg->u.simple.channel; |
| 1245 | |
| 1246 | if (channel >= dev->nchannels) { |
| 1247 | dev_err(dev->udev->dev.parent, |
| 1248 | "Invalid channel number (%d)\n", channel); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | priv = dev->nets[channel]; |
| 1253 | |
| 1254 | complete(&priv->stop_comp); |
| 1255 | } |
| 1256 | |
| 1257 | static void kvaser_usb_handle_message(const struct kvaser_usb *dev, |
| 1258 | const struct kvaser_msg *msg) |
| 1259 | { |
| 1260 | switch (msg->id) { |
| 1261 | case CMD_START_CHIP_REPLY: |
| 1262 | kvaser_usb_start_chip_reply(dev, msg); |
| 1263 | break; |
| 1264 | |
| 1265 | case CMD_STOP_CHIP_REPLY: |
| 1266 | kvaser_usb_stop_chip_reply(dev, msg); |
| 1267 | break; |
| 1268 | |
| 1269 | case CMD_RX_STD_MESSAGE: |
| 1270 | case CMD_RX_EXT_MESSAGE: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1271 | kvaser_usb_rx_can_msg(dev, msg); |
| 1272 | break; |
| 1273 | |
| 1274 | case CMD_LEAF_LOG_MESSAGE: |
| 1275 | if (dev->family != KVASER_LEAF) |
| 1276 | goto warn; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1277 | kvaser_usb_rx_can_msg(dev, msg); |
| 1278 | break; |
| 1279 | |
| 1280 | case CMD_CHIP_STATE_EVENT: |
| 1281 | case CMD_CAN_ERROR_EVENT: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1282 | if (dev->family == KVASER_LEAF) |
| 1283 | kvaser_leaf_rx_error(dev, msg); |
| 1284 | else |
| 1285 | kvaser_usbcan_rx_error(dev, msg); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1286 | break; |
| 1287 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1288 | case CMD_TX_ACKNOWLEDGE: |
| 1289 | kvaser_usb_tx_acknowledge(dev, msg); |
| 1290 | break; |
| 1291 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1292 | /* Ignored messages */ |
| 1293 | case CMD_USBCAN_CLOCK_OVERFLOW_EVENT: |
| 1294 | if (dev->family != KVASER_USBCAN) |
| 1295 | goto warn; |
| 1296 | break; |
| 1297 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1298 | default: |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1299 | warn: dev_warn(dev->udev->dev.parent, |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1300 | "Unhandled message (%d)\n", msg->id); |
| 1301 | break; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | static void kvaser_usb_read_bulk_callback(struct urb *urb) |
| 1306 | { |
| 1307 | struct kvaser_usb *dev = urb->context; |
| 1308 | struct kvaser_msg *msg; |
| 1309 | int pos = 0; |
| 1310 | int err, i; |
| 1311 | |
| 1312 | switch (urb->status) { |
| 1313 | case 0: |
| 1314 | break; |
| 1315 | case -ENOENT: |
| 1316 | case -ESHUTDOWN: |
| 1317 | return; |
| 1318 | default: |
| 1319 | dev_info(dev->udev->dev.parent, "Rx URB aborted (%d)\n", |
| 1320 | urb->status); |
| 1321 | goto resubmit_urb; |
| 1322 | } |
| 1323 | |
| 1324 | while (pos <= urb->actual_length - MSG_HEADER_LEN) { |
| 1325 | msg = urb->transfer_buffer + pos; |
| 1326 | |
Ahmed S. Darwish | 2fec510 | 2015-02-26 10:22:02 -0500 | [diff] [blame] | 1327 | /* The Kvaser firmware can only read and write messages that |
| 1328 | * does not cross the USB's endpoint wMaxPacketSize boundary. |
| 1329 | * If a follow-up command crosses such boundary, firmware puts |
| 1330 | * a placeholder zero-length command in its place then aligns |
| 1331 | * the real command to the next max packet size. |
| 1332 | * |
| 1333 | * Handle such cases or we're going to miss a significant |
| 1334 | * number of events in case of a heavy rx load on the bus. |
| 1335 | */ |
| 1336 | if (msg->len == 0) { |
| 1337 | pos = round_up(pos, dev->bulk_in->wMaxPacketSize); |
| 1338 | continue; |
| 1339 | } |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1340 | |
| 1341 | if (pos + msg->len > urb->actual_length) { |
| 1342 | dev_err(dev->udev->dev.parent, "Format error\n"); |
| 1343 | break; |
| 1344 | } |
| 1345 | |
| 1346 | kvaser_usb_handle_message(dev, msg); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1347 | pos += msg->len; |
| 1348 | } |
| 1349 | |
| 1350 | resubmit_urb: |
| 1351 | usb_fill_bulk_urb(urb, dev->udev, |
| 1352 | usb_rcvbulkpipe(dev->udev, |
| 1353 | dev->bulk_in->bEndpointAddress), |
| 1354 | urb->transfer_buffer, RX_BUFFER_SIZE, |
| 1355 | kvaser_usb_read_bulk_callback, dev); |
| 1356 | |
| 1357 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1358 | if (err == -ENODEV) { |
| 1359 | for (i = 0; i < dev->nchannels; i++) { |
| 1360 | if (!dev->nets[i]) |
| 1361 | continue; |
| 1362 | |
| 1363 | netif_device_detach(dev->nets[i]->netdev); |
| 1364 | } |
| 1365 | } else if (err) { |
| 1366 | dev_err(dev->udev->dev.parent, |
| 1367 | "Failed resubmitting read bulk urb: %d\n", err); |
| 1368 | } |
| 1369 | |
| 1370 | return; |
| 1371 | } |
| 1372 | |
| 1373 | static int kvaser_usb_setup_rx_urbs(struct kvaser_usb *dev) |
| 1374 | { |
| 1375 | int i, err = 0; |
| 1376 | |
| 1377 | if (dev->rxinitdone) |
| 1378 | return 0; |
| 1379 | |
| 1380 | for (i = 0; i < MAX_RX_URBS; i++) { |
| 1381 | struct urb *urb = NULL; |
| 1382 | u8 *buf = NULL; |
| 1383 | dma_addr_t buf_dma; |
| 1384 | |
| 1385 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1386 | if (!urb) { |
| 1387 | dev_warn(dev->udev->dev.parent, |
| 1388 | "No memory left for URBs\n"); |
| 1389 | err = -ENOMEM; |
| 1390 | break; |
| 1391 | } |
| 1392 | |
| 1393 | buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, |
| 1394 | GFP_KERNEL, &buf_dma); |
| 1395 | if (!buf) { |
| 1396 | dev_warn(dev->udev->dev.parent, |
| 1397 | "No memory left for USB buffer\n"); |
| 1398 | usb_free_urb(urb); |
| 1399 | err = -ENOMEM; |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | usb_fill_bulk_urb(urb, dev->udev, |
| 1404 | usb_rcvbulkpipe(dev->udev, |
| 1405 | dev->bulk_in->bEndpointAddress), |
| 1406 | buf, RX_BUFFER_SIZE, |
| 1407 | kvaser_usb_read_bulk_callback, |
| 1408 | dev); |
| 1409 | urb->transfer_dma = buf_dma; |
| 1410 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1411 | usb_anchor_urb(urb, &dev->rx_submitted); |
| 1412 | |
| 1413 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 1414 | if (err) { |
| 1415 | usb_unanchor_urb(urb); |
| 1416 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, |
| 1417 | buf_dma); |
| 1418 | usb_free_urb(urb); |
| 1419 | break; |
| 1420 | } |
| 1421 | |
| 1422 | dev->rxbuf[i] = buf; |
| 1423 | dev->rxbuf_dma[i] = buf_dma; |
| 1424 | |
| 1425 | usb_free_urb(urb); |
| 1426 | } |
| 1427 | |
| 1428 | if (i == 0) { |
| 1429 | dev_warn(dev->udev->dev.parent, |
| 1430 | "Cannot setup read URBs, error %d\n", err); |
| 1431 | return err; |
| 1432 | } else if (i < MAX_RX_URBS) { |
| 1433 | dev_warn(dev->udev->dev.parent, |
| 1434 | "RX performances may be slow\n"); |
| 1435 | } |
| 1436 | |
| 1437 | dev->rxinitdone = true; |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static int kvaser_usb_set_opt_mode(const struct kvaser_usb_net_priv *priv) |
| 1443 | { |
| 1444 | struct kvaser_msg *msg; |
| 1445 | int rc; |
| 1446 | |
| 1447 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1448 | if (!msg) |
| 1449 | return -ENOMEM; |
| 1450 | |
| 1451 | msg->id = CMD_SET_CTRL_MODE; |
| 1452 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_ctrl_mode); |
| 1453 | msg->u.ctrl_mode.tid = 0xff; |
| 1454 | msg->u.ctrl_mode.channel = priv->channel; |
| 1455 | |
| 1456 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 1457 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_SILENT; |
| 1458 | else |
| 1459 | msg->u.ctrl_mode.ctrl_mode = KVASER_CTRL_MODE_NORMAL; |
| 1460 | |
| 1461 | rc = kvaser_usb_send_msg(priv->dev, msg); |
| 1462 | |
| 1463 | kfree(msg); |
| 1464 | return rc; |
| 1465 | } |
| 1466 | |
| 1467 | static int kvaser_usb_start_chip(struct kvaser_usb_net_priv *priv) |
| 1468 | { |
| 1469 | int err; |
| 1470 | |
| 1471 | init_completion(&priv->start_comp); |
| 1472 | |
| 1473 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_START_CHIP, |
| 1474 | priv->channel); |
| 1475 | if (err) |
| 1476 | return err; |
| 1477 | |
| 1478 | if (!wait_for_completion_timeout(&priv->start_comp, |
| 1479 | msecs_to_jiffies(START_TIMEOUT))) |
| 1480 | return -ETIMEDOUT; |
| 1481 | |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static int kvaser_usb_open(struct net_device *netdev) |
| 1486 | { |
| 1487 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1488 | struct kvaser_usb *dev = priv->dev; |
| 1489 | int err; |
| 1490 | |
| 1491 | err = open_candev(netdev); |
| 1492 | if (err) |
| 1493 | return err; |
| 1494 | |
| 1495 | err = kvaser_usb_setup_rx_urbs(dev); |
| 1496 | if (err) |
| 1497 | goto error; |
| 1498 | |
| 1499 | err = kvaser_usb_set_opt_mode(priv); |
| 1500 | if (err) |
| 1501 | goto error; |
| 1502 | |
| 1503 | err = kvaser_usb_start_chip(priv); |
| 1504 | if (err) { |
| 1505 | netdev_warn(netdev, "Cannot start device, error %d\n", err); |
| 1506 | goto error; |
| 1507 | } |
| 1508 | |
| 1509 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1510 | |
| 1511 | return 0; |
| 1512 | |
| 1513 | error: |
| 1514 | close_candev(netdev); |
| 1515 | return err; |
| 1516 | } |
| 1517 | |
| 1518 | static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) |
| 1519 | { |
| 1520 | int i; |
| 1521 | |
| 1522 | usb_kill_anchored_urbs(&dev->rx_submitted); |
| 1523 | |
| 1524 | for (i = 0; i < MAX_RX_URBS; i++) |
| 1525 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, |
| 1526 | dev->rxbuf[i], |
| 1527 | dev->rxbuf_dma[i]); |
| 1528 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1529 | for (i = 0; i < dev->nchannels; i++) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1530 | struct kvaser_usb_net_priv *priv = dev->nets[i]; |
| 1531 | |
| 1532 | if (priv) |
| 1533 | kvaser_usb_unlink_tx_urbs(priv); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | static int kvaser_usb_stop_chip(struct kvaser_usb_net_priv *priv) |
| 1538 | { |
| 1539 | int err; |
| 1540 | |
| 1541 | init_completion(&priv->stop_comp); |
| 1542 | |
| 1543 | err = kvaser_usb_send_simple_msg(priv->dev, CMD_STOP_CHIP, |
| 1544 | priv->channel); |
| 1545 | if (err) |
| 1546 | return err; |
| 1547 | |
| 1548 | if (!wait_for_completion_timeout(&priv->stop_comp, |
| 1549 | msecs_to_jiffies(STOP_TIMEOUT))) |
| 1550 | return -ETIMEDOUT; |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | static int kvaser_usb_flush_queue(struct kvaser_usb_net_priv *priv) |
| 1556 | { |
| 1557 | struct kvaser_msg *msg; |
| 1558 | int rc; |
| 1559 | |
| 1560 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1561 | if (!msg) |
| 1562 | return -ENOMEM; |
| 1563 | |
| 1564 | msg->id = CMD_FLUSH_QUEUE; |
| 1565 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_flush_queue); |
| 1566 | msg->u.flush_queue.channel = priv->channel; |
| 1567 | msg->u.flush_queue.flags = 0x00; |
| 1568 | |
| 1569 | rc = kvaser_usb_send_msg(priv->dev, msg); |
| 1570 | |
| 1571 | kfree(msg); |
| 1572 | return rc; |
| 1573 | } |
| 1574 | |
| 1575 | static int kvaser_usb_close(struct net_device *netdev) |
| 1576 | { |
| 1577 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1578 | struct kvaser_usb *dev = priv->dev; |
| 1579 | int err; |
| 1580 | |
| 1581 | netif_stop_queue(netdev); |
| 1582 | |
| 1583 | err = kvaser_usb_flush_queue(priv); |
| 1584 | if (err) |
| 1585 | netdev_warn(netdev, "Cannot flush queue, error %d\n", err); |
| 1586 | |
| 1587 | if (kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, priv->channel)) |
| 1588 | netdev_warn(netdev, "Cannot reset card, error %d\n", err); |
| 1589 | |
| 1590 | err = kvaser_usb_stop_chip(priv); |
| 1591 | if (err) |
| 1592 | netdev_warn(netdev, "Cannot stop device, error %d\n", err); |
| 1593 | |
Ahmed S. Darwish | 889b77f | 2015-01-05 12:52:06 -0500 | [diff] [blame] | 1594 | /* reset tx contexts */ |
| 1595 | kvaser_usb_unlink_tx_urbs(priv); |
| 1596 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1597 | priv->can.state = CAN_STATE_STOPPED; |
| 1598 | close_candev(priv->netdev); |
| 1599 | |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | static void kvaser_usb_write_bulk_callback(struct urb *urb) |
| 1604 | { |
| 1605 | struct kvaser_usb_tx_urb_context *context = urb->context; |
| 1606 | struct kvaser_usb_net_priv *priv; |
| 1607 | struct net_device *netdev; |
| 1608 | |
| 1609 | if (WARN_ON(!context)) |
| 1610 | return; |
| 1611 | |
| 1612 | priv = context->priv; |
| 1613 | netdev = priv->netdev; |
| 1614 | |
| 1615 | kfree(urb->transfer_buffer); |
| 1616 | |
| 1617 | if (!netif_device_present(netdev)) |
| 1618 | return; |
| 1619 | |
| 1620 | if (urb->status) |
| 1621 | netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); |
| 1622 | } |
| 1623 | |
| 1624 | static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb, |
| 1625 | struct net_device *netdev) |
| 1626 | { |
| 1627 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1628 | struct kvaser_usb *dev = priv->dev; |
| 1629 | struct net_device_stats *stats = &netdev->stats; |
| 1630 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 1631 | struct kvaser_usb_tx_urb_context *context = NULL; |
| 1632 | struct urb *urb; |
| 1633 | void *buf; |
| 1634 | struct kvaser_msg *msg; |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1635 | int i, err, ret = NETDEV_TX_OK; |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1636 | u8 *msg_tx_can_flags = NULL; /* GCC */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1637 | |
| 1638 | if (can_dropped_invalid_skb(netdev, skb)) |
| 1639 | return NETDEV_TX_OK; |
| 1640 | |
| 1641 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1642 | if (!urb) { |
| 1643 | netdev_err(netdev, "No memory left for URBs\n"); |
| 1644 | stats->tx_dropped++; |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1645 | dev_kfree_skb(skb); |
| 1646 | return NETDEV_TX_OK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | buf = kmalloc(sizeof(struct kvaser_msg), GFP_ATOMIC); |
| 1650 | if (!buf) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1651 | stats->tx_dropped++; |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1652 | dev_kfree_skb(skb); |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1653 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | msg = buf; |
| 1657 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_tx_can); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1658 | msg->u.tx_can.channel = priv->channel; |
| 1659 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1660 | switch (dev->family) { |
| 1661 | case KVASER_LEAF: |
| 1662 | msg_tx_can_flags = &msg->u.tx_can.leaf.flags; |
| 1663 | break; |
| 1664 | case KVASER_USBCAN: |
| 1665 | msg_tx_can_flags = &msg->u.tx_can.usbcan.flags; |
| 1666 | break; |
| 1667 | } |
| 1668 | |
| 1669 | *msg_tx_can_flags = 0; |
| 1670 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1671 | if (cf->can_id & CAN_EFF_FLAG) { |
| 1672 | msg->id = CMD_TX_EXT_MESSAGE; |
| 1673 | msg->u.tx_can.msg[0] = (cf->can_id >> 24) & 0x1f; |
| 1674 | msg->u.tx_can.msg[1] = (cf->can_id >> 18) & 0x3f; |
| 1675 | msg->u.tx_can.msg[2] = (cf->can_id >> 14) & 0x0f; |
| 1676 | msg->u.tx_can.msg[3] = (cf->can_id >> 6) & 0xff; |
| 1677 | msg->u.tx_can.msg[4] = cf->can_id & 0x3f; |
| 1678 | } else { |
| 1679 | msg->id = CMD_TX_STD_MESSAGE; |
| 1680 | msg->u.tx_can.msg[0] = (cf->can_id >> 6) & 0x1f; |
| 1681 | msg->u.tx_can.msg[1] = cf->can_id & 0x3f; |
| 1682 | } |
| 1683 | |
| 1684 | msg->u.tx_can.msg[5] = cf->can_dlc; |
| 1685 | memcpy(&msg->u.tx_can.msg[6], cf->data, cf->can_dlc); |
| 1686 | |
| 1687 | if (cf->can_id & CAN_RTR_FLAG) |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1688 | *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1689 | |
| 1690 | for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) { |
| 1691 | if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { |
| 1692 | context = &priv->tx_contexts[i]; |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Ahmed S. Darwish | b442723 | 2015-01-05 12:49:10 -0500 | [diff] [blame] | 1697 | /* This should never happen; it implies a flow control bug */ |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1698 | if (!context) { |
| 1699 | netdev_warn(netdev, "cannot find free context\n"); |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1700 | |
| 1701 | kfree(buf); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1702 | ret = NETDEV_TX_BUSY; |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1703 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | context->priv = priv; |
| 1707 | context->echo_index = i; |
| 1708 | context->dlc = cf->can_dlc; |
| 1709 | |
| 1710 | msg->u.tx_can.tid = context->echo_index; |
| 1711 | |
| 1712 | usb_fill_bulk_urb(urb, dev->udev, |
| 1713 | usb_sndbulkpipe(dev->udev, |
| 1714 | dev->bulk_out->bEndpointAddress), |
| 1715 | buf, msg->len, |
| 1716 | kvaser_usb_write_bulk_callback, context); |
| 1717 | usb_anchor_urb(urb, &priv->tx_submitted); |
| 1718 | |
| 1719 | can_put_echo_skb(skb, netdev, context->echo_index); |
| 1720 | |
| 1721 | atomic_inc(&priv->active_tx_urbs); |
| 1722 | |
| 1723 | if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS) |
| 1724 | netif_stop_queue(netdev); |
| 1725 | |
| 1726 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1727 | if (unlikely(err)) { |
| 1728 | can_free_echo_skb(netdev, context->echo_index); |
| 1729 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1730 | atomic_dec(&priv->active_tx_urbs); |
| 1731 | usb_unanchor_urb(urb); |
| 1732 | |
| 1733 | stats->tx_dropped++; |
| 1734 | |
| 1735 | if (err == -ENODEV) |
| 1736 | netif_device_detach(netdev); |
| 1737 | else |
| 1738 | netdev_warn(netdev, "Failed tx_urb %d\n", err); |
| 1739 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1740 | goto freeurb; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1743 | ret = NETDEV_TX_OK; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1744 | |
Ahmed S. Darwish | deb2701 | 2015-02-26 10:20:11 -0500 | [diff] [blame] | 1745 | freeurb: |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1746 | usb_free_urb(urb); |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1747 | return ret; |
| 1748 | } |
| 1749 | |
| 1750 | static const struct net_device_ops kvaser_usb_netdev_ops = { |
| 1751 | .ndo_open = kvaser_usb_open, |
| 1752 | .ndo_stop = kvaser_usb_close, |
| 1753 | .ndo_start_xmit = kvaser_usb_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1754 | .ndo_change_mtu = can_change_mtu, |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1755 | }; |
| 1756 | |
| 1757 | static const struct can_bittiming_const kvaser_usb_bittiming_const = { |
| 1758 | .name = "kvaser_usb", |
| 1759 | .tseg1_min = KVASER_USB_TSEG1_MIN, |
| 1760 | .tseg1_max = KVASER_USB_TSEG1_MAX, |
| 1761 | .tseg2_min = KVASER_USB_TSEG2_MIN, |
| 1762 | .tseg2_max = KVASER_USB_TSEG2_MAX, |
| 1763 | .sjw_max = KVASER_USB_SJW_MAX, |
| 1764 | .brp_min = KVASER_USB_BRP_MIN, |
| 1765 | .brp_max = KVASER_USB_BRP_MAX, |
| 1766 | .brp_inc = KVASER_USB_BRP_INC, |
| 1767 | }; |
| 1768 | |
| 1769 | static int kvaser_usb_set_bittiming(struct net_device *netdev) |
| 1770 | { |
| 1771 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1772 | struct can_bittiming *bt = &priv->can.bittiming; |
| 1773 | struct kvaser_usb *dev = priv->dev; |
| 1774 | struct kvaser_msg *msg; |
| 1775 | int rc; |
| 1776 | |
| 1777 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
| 1778 | if (!msg) |
| 1779 | return -ENOMEM; |
| 1780 | |
| 1781 | msg->id = CMD_SET_BUS_PARAMS; |
| 1782 | msg->len = MSG_HEADER_LEN + sizeof(struct kvaser_msg_busparams); |
| 1783 | msg->u.busparams.channel = priv->channel; |
| 1784 | msg->u.busparams.tid = 0xff; |
| 1785 | msg->u.busparams.bitrate = cpu_to_le32(bt->bitrate); |
| 1786 | msg->u.busparams.sjw = bt->sjw; |
| 1787 | msg->u.busparams.tseg1 = bt->prop_seg + bt->phase_seg1; |
| 1788 | msg->u.busparams.tseg2 = bt->phase_seg2; |
| 1789 | |
| 1790 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 1791 | msg->u.busparams.no_samp = 3; |
| 1792 | else |
| 1793 | msg->u.busparams.no_samp = 1; |
| 1794 | |
| 1795 | rc = kvaser_usb_send_msg(dev, msg); |
| 1796 | |
| 1797 | kfree(msg); |
| 1798 | return rc; |
| 1799 | } |
| 1800 | |
| 1801 | static int kvaser_usb_set_mode(struct net_device *netdev, |
| 1802 | enum can_mode mode) |
| 1803 | { |
| 1804 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1805 | int err; |
| 1806 | |
| 1807 | switch (mode) { |
| 1808 | case CAN_MODE_START: |
| 1809 | err = kvaser_usb_simple_msg_async(priv, CMD_START_CHIP); |
| 1810 | if (err) |
| 1811 | return err; |
| 1812 | break; |
| 1813 | default: |
| 1814 | return -EOPNOTSUPP; |
| 1815 | } |
| 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
| 1820 | static int kvaser_usb_get_berr_counter(const struct net_device *netdev, |
| 1821 | struct can_berr_counter *bec) |
| 1822 | { |
| 1823 | struct kvaser_usb_net_priv *priv = netdev_priv(netdev); |
| 1824 | |
| 1825 | *bec = priv->bec; |
| 1826 | |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | static void kvaser_usb_remove_interfaces(struct kvaser_usb *dev) |
| 1831 | { |
| 1832 | int i; |
| 1833 | |
| 1834 | for (i = 0; i < dev->nchannels; i++) { |
| 1835 | if (!dev->nets[i]) |
| 1836 | continue; |
| 1837 | |
| 1838 | unregister_netdev(dev->nets[i]->netdev); |
| 1839 | } |
| 1840 | |
| 1841 | kvaser_usb_unlink_all_urbs(dev); |
| 1842 | |
| 1843 | for (i = 0; i < dev->nchannels; i++) { |
| 1844 | if (!dev->nets[i]) |
| 1845 | continue; |
| 1846 | |
| 1847 | free_candev(dev->nets[i]->netdev); |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | static int kvaser_usb_init_one(struct usb_interface *intf, |
| 1852 | const struct usb_device_id *id, int channel) |
| 1853 | { |
| 1854 | struct kvaser_usb *dev = usb_get_intfdata(intf); |
| 1855 | struct net_device *netdev; |
| 1856 | struct kvaser_usb_net_priv *priv; |
| 1857 | int i, err; |
| 1858 | |
Ahmed S. Darwish | 5e7e6e0 | 2015-01-05 12:57:13 -0500 | [diff] [blame] | 1859 | err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); |
| 1860 | if (err) |
| 1861 | return err; |
| 1862 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1863 | netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS); |
| 1864 | if (!netdev) { |
| 1865 | dev_err(&intf->dev, "Cannot alloc candev\n"); |
| 1866 | return -ENOMEM; |
| 1867 | } |
| 1868 | |
| 1869 | priv = netdev_priv(netdev); |
| 1870 | |
| 1871 | init_completion(&priv->start_comp); |
| 1872 | init_completion(&priv->stop_comp); |
| 1873 | |
| 1874 | init_usb_anchor(&priv->tx_submitted); |
| 1875 | atomic_set(&priv->active_tx_urbs, 0); |
| 1876 | |
| 1877 | for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) |
| 1878 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; |
| 1879 | |
| 1880 | priv->dev = dev; |
| 1881 | priv->netdev = netdev; |
| 1882 | priv->channel = channel; |
| 1883 | |
| 1884 | priv->can.state = CAN_STATE_STOPPED; |
| 1885 | priv->can.clock.freq = CAN_USB_CLOCK; |
| 1886 | priv->can.bittiming_const = &kvaser_usb_bittiming_const; |
| 1887 | priv->can.do_set_bittiming = kvaser_usb_set_bittiming; |
| 1888 | priv->can.do_set_mode = kvaser_usb_set_mode; |
| 1889 | if (id->driver_info & KVASER_HAS_TXRX_ERRORS) |
| 1890 | priv->can.do_get_berr_counter = kvaser_usb_get_berr_counter; |
| 1891 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; |
| 1892 | if (id->driver_info & KVASER_HAS_SILENT_MODE) |
| 1893 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; |
| 1894 | |
| 1895 | netdev->flags |= IFF_ECHO; |
| 1896 | |
| 1897 | netdev->netdev_ops = &kvaser_usb_netdev_ops; |
| 1898 | |
| 1899 | SET_NETDEV_DEV(netdev, &intf->dev); |
Christopher R. Baker | 3e66d01 | 2014-03-08 11:00:20 -0500 | [diff] [blame] | 1900 | netdev->dev_id = channel; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1901 | |
| 1902 | dev->nets[channel] = priv; |
| 1903 | |
| 1904 | err = register_candev(netdev); |
| 1905 | if (err) { |
| 1906 | dev_err(&intf->dev, "Failed to register can device\n"); |
| 1907 | free_candev(netdev); |
| 1908 | dev->nets[channel] = NULL; |
| 1909 | return err; |
| 1910 | } |
| 1911 | |
| 1912 | netdev_dbg(netdev, "device registered\n"); |
| 1913 | |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1917 | static int kvaser_usb_get_endpoints(const struct usb_interface *intf, |
| 1918 | struct usb_endpoint_descriptor **in, |
| 1919 | struct usb_endpoint_descriptor **out) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1920 | { |
| 1921 | const struct usb_host_interface *iface_desc; |
| 1922 | struct usb_endpoint_descriptor *endpoint; |
| 1923 | int i; |
| 1924 | |
| 1925 | iface_desc = &intf->altsetting[0]; |
| 1926 | |
| 1927 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 1928 | endpoint = &iface_desc->endpoint[i].desc; |
| 1929 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1930 | if (!*in && usb_endpoint_is_bulk_in(endpoint)) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1931 | *in = endpoint; |
| 1932 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1933 | if (!*out && usb_endpoint_is_bulk_out(endpoint)) |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1934 | *out = endpoint; |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1935 | |
| 1936 | /* use first bulk endpoint for in and out */ |
| 1937 | if (*in && *out) |
| 1938 | return 0; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1939 | } |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1940 | |
| 1941 | return -ENODEV; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | static int kvaser_usb_probe(struct usb_interface *intf, |
| 1945 | const struct usb_device_id *id) |
| 1946 | { |
| 1947 | struct kvaser_usb *dev; |
| 1948 | int err = -ENOMEM; |
Ahmed S. Darwish | 14c10c2 | 2015-01-26 07:24:06 +0200 | [diff] [blame] | 1949 | int i, retry = 3; |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1950 | |
| 1951 | dev = devm_kzalloc(&intf->dev, sizeof(*dev), GFP_KERNEL); |
| 1952 | if (!dev) |
| 1953 | return -ENOMEM; |
| 1954 | |
Ahmed S. Darwish | f5d4abea | 2015-01-26 07:33:10 +0200 | [diff] [blame] | 1955 | if (kvaser_is_leaf(id)) { |
| 1956 | dev->family = KVASER_LEAF; |
| 1957 | } else if (kvaser_is_usbcan(id)) { |
| 1958 | dev->family = KVASER_USBCAN; |
| 1959 | } else { |
| 1960 | dev_err(&intf->dev, |
| 1961 | "Product ID (%d) does not belong to any known Kvaser USB family", |
| 1962 | id->idProduct); |
| 1963 | return -ENODEV; |
| 1964 | } |
| 1965 | |
Olivier Sobrie | 896e23b | 2013-10-27 22:07:53 +0100 | [diff] [blame] | 1966 | err = kvaser_usb_get_endpoints(intf, &dev->bulk_in, &dev->bulk_out); |
| 1967 | if (err) { |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1968 | dev_err(&intf->dev, "Cannot get usb endpoint(s)"); |
| 1969 | return err; |
| 1970 | } |
| 1971 | |
| 1972 | dev->udev = interface_to_usbdev(intf); |
| 1973 | |
| 1974 | init_usb_anchor(&dev->rx_submitted); |
| 1975 | |
| 1976 | usb_set_intfdata(intf, dev); |
| 1977 | |
Ahmed S. Darwish | 14c10c2 | 2015-01-26 07:24:06 +0200 | [diff] [blame] | 1978 | /* On some x86 laptops, plugging a Kvaser device again after |
| 1979 | * an unplug makes the firmware always ignore the very first |
| 1980 | * command. For such a case, provide some room for retries |
| 1981 | * instead of completely exiting the driver. |
| 1982 | */ |
| 1983 | do { |
| 1984 | err = kvaser_usb_get_software_info(dev); |
| 1985 | } while (--retry && err == -ETIMEDOUT); |
| 1986 | |
Olivier Sobrie | 080f40a | 2012-11-21 08:11:13 +0100 | [diff] [blame] | 1987 | if (err) { |
| 1988 | dev_err(&intf->dev, |
| 1989 | "Cannot get software infos, error %d\n", err); |
| 1990 | return err; |
| 1991 | } |
| 1992 | |
| 1993 | err = kvaser_usb_get_card_info(dev); |
| 1994 | if (err) { |
| 1995 | dev_err(&intf->dev, |
| 1996 | "Cannot get card infos, error %d\n", err); |
| 1997 | return err; |
| 1998 | } |
| 1999 | |
| 2000 | dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n", |
| 2001 | ((dev->fw_version >> 24) & 0xff), |
| 2002 | ((dev->fw_version >> 16) & 0xff), |
| 2003 | (dev->fw_version & 0xffff)); |
| 2004 | |
| 2005 | for (i = 0; i < dev->nchannels; i++) { |
| 2006 | err = kvaser_usb_init_one(intf, id, i); |
| 2007 | if (err) { |
| 2008 | kvaser_usb_remove_interfaces(dev); |
| 2009 | return err; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | static void kvaser_usb_disconnect(struct usb_interface *intf) |
| 2017 | { |
| 2018 | struct kvaser_usb *dev = usb_get_intfdata(intf); |
| 2019 | |
| 2020 | usb_set_intfdata(intf, NULL); |
| 2021 | |
| 2022 | if (!dev) |
| 2023 | return; |
| 2024 | |
| 2025 | kvaser_usb_remove_interfaces(dev); |
| 2026 | } |
| 2027 | |
| 2028 | static struct usb_driver kvaser_usb_driver = { |
| 2029 | .name = "kvaser_usb", |
| 2030 | .probe = kvaser_usb_probe, |
| 2031 | .disconnect = kvaser_usb_disconnect, |
| 2032 | .id_table = kvaser_usb_table, |
| 2033 | }; |
| 2034 | |
| 2035 | module_usb_driver(kvaser_usb_driver); |
| 2036 | |
| 2037 | MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); |
| 2038 | MODULE_DESCRIPTION("CAN driver for Kvaser CAN/USB devices"); |
| 2039 | MODULE_LICENSE("GPL v2"); |