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