| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* SocketCAN driver for Microchip CAN BUS Analyzer Tool |
| 3 |
* |
| 4 |
* Copyright (C) 2017 Mobica Limited |
| 5 |
* |
| 6 |
* This driver is inspired by the 4.6.2 version of net/can/usb/usb_8dev.c |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/unaligned.h> |
| 10 |
#include <linux/can.h> |
| 11 |
#include <linux/can/dev.h> |
| 12 |
#include <linux/can/error.h> |
| 13 |
#include <linux/ethtool.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/netdevice.h> |
| 16 |
#include <linux/signal.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
#include <linux/usb.h> |
| 19 |
|
| 20 |
/* vendor and product id */ |
| 21 |
#define MCBA_MODULE_NAME "mcba_usb" |
| 22 |
#define MCBA_VENDOR_ID 0x04d8 |
| 23 |
#define MCBA_PRODUCT_ID 0x0a30 |
| 24 |
|
| 25 |
/* driver constants */ |
| 26 |
#define MCBA_MAX_RX_URBS 20 |
| 27 |
#define MCBA_MAX_TX_URBS 20 |
| 28 |
#define MCBA_CTX_FREE MCBA_MAX_TX_URBS |
| 29 |
|
| 30 |
/* RX buffer must be bigger than msg size since at the |
| 31 |
* beginning USB messages are stacked. |
| 32 |
*/ |
| 33 |
#define MCBA_USB_RX_BUFF_SIZE 64 |
| 34 |
#define MCBA_USB_TX_BUFF_SIZE (sizeof(struct mcba_usb_msg)) |
| 35 |
|
| 36 |
/* Microchip command id */ |
| 37 |
#define MBCA_CMD_RECEIVE_MESSAGE 0xE3 |
| 38 |
#define MBCA_CMD_I_AM_ALIVE_FROM_CAN 0xF5 |
| 39 |
#define MBCA_CMD_I_AM_ALIVE_FROM_USB 0xF7 |
| 40 |
#define MBCA_CMD_CHANGE_BIT_RATE 0xA1 |
| 41 |
#define MBCA_CMD_TRANSMIT_MESSAGE_EV 0xA3 |
| 42 |
#define MBCA_CMD_SETUP_TERMINATION_RESISTANCE 0xA8 |
| 43 |
#define MBCA_CMD_READ_FW_VERSION 0xA9 |
| 44 |
#define MBCA_CMD_NOTHING_TO_SEND 0xFF |
| 45 |
#define MBCA_CMD_TRANSMIT_MESSAGE_RSP 0xE2 |
| 46 |
|
| 47 |
#define MCBA_VER_REQ_USB 1 |
| 48 |
#define MCBA_VER_REQ_CAN 2 |
| 49 |
|
| 50 |
/* Drive the CAN_RES signal LOW "0" to activate R24 and R25 */ |
| 51 |
#define MCBA_VER_TERMINATION_ON 0 |
| 52 |
#define MCBA_VER_TERMINATION_OFF 1 |
| 53 |
|
| 54 |
#define MCBA_SIDL_EXID_MASK 0x8 |
| 55 |
#define MCBA_DLC_MASK 0xf |
| 56 |
#define MCBA_DLC_RTR_MASK 0x40 |
| 57 |
|
| 58 |
#define MCBA_CAN_STATE_WRN_TH 95 |
| 59 |
#define MCBA_CAN_STATE_ERR_PSV_TH 127 |
| 60 |
|
| 61 |
#define MCBA_TERMINATION_DISABLED CAN_TERMINATION_DISABLED |
| 62 |
#define MCBA_TERMINATION_ENABLED 120 |
| 63 |
|
| 64 |
struct mcba_usb_ctx { |
| 65 |
struct mcba_priv *priv; |
| 66 |
u32 ndx; |
| 67 |
bool can; |
| 68 |
}; |
| 69 |
|
| 70 |
/* Structure to hold all of our device specific stuff */ |
| 71 |
struct mcba_priv { |
| 72 |
struct can_priv can; /* must be the first member */ |
| 73 |
struct sk_buff *echo_skb[MCBA_MAX_TX_URBS]; |
| 74 |
struct mcba_usb_ctx tx_context[MCBA_MAX_TX_URBS]; |
| 75 |
struct usb_device *udev; |
| 76 |
struct net_device *netdev; |
| 77 |
struct usb_anchor tx_submitted; |
| 78 |
struct usb_anchor rx_submitted; |
| 79 |
struct can_berr_counter bec; |
| 80 |
bool usb_ka_first_pass; |
| 81 |
bool can_ka_first_pass; |
| 82 |
bool can_speed_check; |
| 83 |
atomic_t free_ctx_cnt; |
| 84 |
void *rxbuf[MCBA_MAX_RX_URBS]; |
| 85 |
dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS]; |
| 86 |
int rx_pipe; |
| 87 |
int tx_pipe; |
| 88 |
}; |
| 89 |
|
| 90 |
/* CAN frame */ |
| 91 |
struct __packed mcba_usb_msg_can { |
| 92 |
u8 cmd_id; |
| 93 |
__be16 eid; |
| 94 |
__be16 sid; |
| 95 |
u8 dlc; |
| 96 |
u8 data[8]; |
| 97 |
u8 timestamp[4]; |
| 98 |
u8 checksum; |
| 99 |
}; |
| 100 |
|
| 101 |
/* command frame */ |
| 102 |
struct __packed mcba_usb_msg { |
| 103 |
u8 cmd_id; |
| 104 |
u8 unused[18]; |
| 105 |
}; |
| 106 |
|
| 107 |
struct __packed mcba_usb_msg_ka_usb { |
| 108 |
u8 cmd_id; |
| 109 |
u8 termination_state; |
| 110 |
u8 soft_ver_major; |
| 111 |
u8 soft_ver_minor; |
| 112 |
u8 unused[15]; |
| 113 |
}; |
| 114 |
|
| 115 |
struct __packed mcba_usb_msg_ka_can { |
| 116 |
u8 cmd_id; |
| 117 |
u8 tx_err_cnt; |
| 118 |
u8 rx_err_cnt; |
| 119 |
u8 rx_buff_ovfl; |
| 120 |
u8 tx_bus_off; |
| 121 |
__be16 can_bitrate; |
| 122 |
__le16 rx_lost; |
| 123 |
u8 can_stat; |
| 124 |
u8 soft_ver_major; |
| 125 |
u8 soft_ver_minor; |
| 126 |
u8 debug_mode; |
| 127 |
u8 test_complete; |
| 128 |
u8 test_result; |
| 129 |
u8 unused[4]; |
| 130 |
}; |
| 131 |
|
| 132 |
struct __packed mcba_usb_msg_change_bitrate { |
| 133 |
u8 cmd_id; |
| 134 |
__be16 bitrate; |
| 135 |
u8 unused[16]; |
| 136 |
}; |
| 137 |
|
| 138 |
struct __packed mcba_usb_msg_termination { |
| 139 |
u8 cmd_id; |
| 140 |
u8 termination; |
| 141 |
u8 unused[17]; |
| 142 |
}; |
| 143 |
|
| 144 |
struct __packed mcba_usb_msg_fw_ver { |
| 145 |
u8 cmd_id; |
| 146 |
u8 pic; |
| 147 |
u8 unused[17]; |
| 148 |
}; |
| 149 |
|
| 150 |
static const struct usb_device_id mcba_usb_table[] = { |
| 151 |
{ USB_DEVICE(MCBA_VENDOR_ID, MCBA_PRODUCT_ID) }, |
| 152 |
{} /* Terminating entry */ |
| 153 |
}; |
| 154 |
|
| 155 |
MODULE_DEVICE_TABLE(usb, mcba_usb_table); |
| 156 |
|
| 157 |
static const u16 mcba_termination[] = { MCBA_TERMINATION_DISABLED, |
| 158 |
MCBA_TERMINATION_ENABLED }; |
| 159 |
|
| 160 |
static const u32 mcba_bitrate[] = { 20000, 33333, 50000, 80000, 83333, |
| 161 |
100000, 125000, 150000, 175000, 200000, |
| 162 |
225000, 250000, 275000, 300000, 500000, |
| 163 |
625000, 800000, 1000000 }; |
| 164 |
|
| 165 |
static inline void mcba_init_ctx(struct mcba_priv *priv) |
| 166 |
{ |
| 167 |
int i = 0; |
| 168 |
|
| 169 |
for (i = 0; i < MCBA_MAX_TX_URBS; i++) { |
| 170 |
priv->tx_context[i].ndx = MCBA_CTX_FREE; |
| 171 |
priv->tx_context[i].priv = priv; |
| 172 |
} |
| 173 |
|
| 174 |
atomic_set(&priv->free_ctx_cnt, ARRAY_SIZE(priv->tx_context)); |
| 175 |
} |
| 176 |
|
| 177 |
static inline struct mcba_usb_ctx *mcba_usb_get_free_ctx(struct mcba_priv *priv, |
| 178 |
struct can_frame *cf) |
| 179 |
{ |
| 180 |
int i = 0; |
| 181 |
struct mcba_usb_ctx *ctx = NULL; |
| 182 |
|
| 183 |
for (i = 0; i < MCBA_MAX_TX_URBS; i++) { |
| 184 |
if (priv->tx_context[i].ndx == MCBA_CTX_FREE) { |
| 185 |
ctx = &priv->tx_context[i]; |
| 186 |
ctx->ndx = i; |
| 187 |
|
| 188 |
if (cf) |
| 189 |
ctx->can = true; |
| 190 |
else |
| 191 |
ctx->can = false; |
| 192 |
|
| 193 |
atomic_dec(&priv->free_ctx_cnt); |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (!atomic_read(&priv->free_ctx_cnt)) |
| 199 |
/* That was the last free ctx. Slow down tx path */ |
| 200 |
netif_stop_queue(priv->netdev); |
| 201 |
|
| 202 |
return ctx; |
| 203 |
} |
| 204 |
|
| 205 |
/* mcba_usb_free_ctx and mcba_usb_get_free_ctx are executed by different |
| 206 |
* threads. The order of execution in below function is important. |
| 207 |
*/ |
| 208 |
static inline void mcba_usb_free_ctx(struct mcba_usb_ctx *ctx) |
| 209 |
{ |
| 210 |
/* Increase number of free ctxs before freeing ctx */ |
| 211 |
atomic_inc(&ctx->priv->free_ctx_cnt); |
| 212 |
|
| 213 |
ctx->ndx = MCBA_CTX_FREE; |
| 214 |
|
| 215 |
/* Wake up the queue once ctx is marked free */ |
| 216 |
netif_wake_queue(ctx->priv->netdev); |
| 217 |
} |
| 218 |
|
| 219 |
static void mcba_usb_write_bulk_callback(struct urb *urb) |
| 220 |
{ |
| 221 |
struct mcba_usb_ctx *ctx = urb->context; |
| 222 |
struct net_device *netdev; |
| 223 |
|
| 224 |
WARN_ON(!ctx); |
| 225 |
|
| 226 |
netdev = ctx->priv->netdev; |
| 227 |
|
| 228 |
/* free up our allocated buffer */ |
| 229 |
usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
| 230 |
urb->transfer_buffer, urb->transfer_dma); |
| 231 |
|
| 232 |
if (ctx->can) { |
| 233 |
if (!netif_device_present(netdev)) |
| 234 |
return; |
| 235 |
|
| 236 |
netdev->stats.tx_packets++; |
| 237 |
netdev->stats.tx_bytes += can_get_echo_skb(netdev, ctx->ndx, |
| 238 |
NULL); |
| 239 |
} |
| 240 |
|
| 241 |
if (urb->status) |
| 242 |
netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); |
| 243 |
|
| 244 |
/* Release the context */ |
| 245 |
mcba_usb_free_ctx(ctx); |
| 246 |
} |
| 247 |
|
| 248 |
/* Send data to device */ |
| 249 |
static netdev_tx_t mcba_usb_xmit(struct mcba_priv *priv, |
| 250 |
struct mcba_usb_msg *usb_msg, |
| 251 |
struct mcba_usb_ctx *ctx) |
| 252 |
{ |
| 253 |
struct urb *urb; |
| 254 |
u8 *buf; |
| 255 |
int err; |
| 256 |
|
| 257 |
/* create a URB, and a buffer for it, and copy the data to the URB */ |
| 258 |
urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 259 |
if (!urb) |
| 260 |
return -ENOMEM; |
| 261 |
|
| 262 |
buf = usb_alloc_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, GFP_ATOMIC, |
| 263 |
&urb->transfer_dma); |
| 264 |
if (!buf) { |
| 265 |
err = -ENOMEM; |
| 266 |
goto nomembuf; |
| 267 |
} |
| 268 |
|
| 269 |
memcpy(buf, usb_msg, MCBA_USB_TX_BUFF_SIZE); |
| 270 |
|
| 271 |
usb_fill_bulk_urb(urb, priv->udev, priv->tx_pipe, buf, MCBA_USB_TX_BUFF_SIZE, |
| 272 |
mcba_usb_write_bulk_callback, ctx); |
| 273 |
|
| 274 |
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 275 |
usb_anchor_urb(urb, &priv->tx_submitted); |
| 276 |
|
| 277 |
err = usb_submit_urb(urb, GFP_ATOMIC); |
| 278 |
if (unlikely(err)) |
| 279 |
goto failed; |
| 280 |
|
| 281 |
/* Release our reference to this URB, the USB core will eventually free |
| 282 |
* it entirely. |
| 283 |
*/ |
| 284 |
usb_free_urb(urb); |
| 285 |
|
| 286 |
return 0; |
| 287 |
|
| 288 |
failed: |
| 289 |
usb_unanchor_urb(urb); |
| 290 |
usb_free_coherent(priv->udev, MCBA_USB_TX_BUFF_SIZE, buf, |
| 291 |
urb->transfer_dma); |
| 292 |
|
| 293 |
if (err == -ENODEV) |
| 294 |
netif_device_detach(priv->netdev); |
| 295 |
else |
| 296 |
netdev_warn(priv->netdev, "failed tx_urb %d\n", err); |
| 297 |
|
| 298 |
nomembuf: |
| 299 |
usb_free_urb(urb); |
| 300 |
|
| 301 |
return err; |
| 302 |
} |
| 303 |
|
| 304 |
/* Send data to device */ |
| 305 |
static netdev_tx_t mcba_usb_start_xmit(struct sk_buff *skb, |
| 306 |
struct net_device *netdev) |
| 307 |
{ |
| 308 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 309 |
struct can_frame *cf = (struct can_frame *)skb->data; |
| 310 |
struct mcba_usb_ctx *ctx = NULL; |
| 311 |
struct net_device_stats *stats = &priv->netdev->stats; |
| 312 |
u16 sid; |
| 313 |
int err; |
| 314 |
struct mcba_usb_msg_can usb_msg = { |
| 315 |
.cmd_id = MBCA_CMD_TRANSMIT_MESSAGE_EV |
| 316 |
}; |
| 317 |
|
| 318 |
if (can_dev_dropped_skb(netdev, skb)) |
| 319 |
return NETDEV_TX_OK; |
| 320 |
|
| 321 |
ctx = mcba_usb_get_free_ctx(priv, cf); |
| 322 |
if (!ctx) |
| 323 |
return NETDEV_TX_BUSY; |
| 324 |
|
| 325 |
if (cf->can_id & CAN_EFF_FLAG) { |
| 326 |
/* SIDH | SIDL | EIDH | EIDL |
| 327 |
* 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0 |
| 328 |
*/ |
| 329 |
sid = MCBA_SIDL_EXID_MASK; |
| 330 |
/* store 28-18 bits */ |
| 331 |
sid |= (cf->can_id & 0x1ffc0000) >> 13; |
| 332 |
/* store 17-16 bits */ |
| 333 |
sid |= (cf->can_id & 0x30000) >> 16; |
| 334 |
put_unaligned_be16(sid, &usb_msg.sid); |
| 335 |
|
| 336 |
/* store 15-0 bits */ |
| 337 |
put_unaligned_be16(cf->can_id & 0xffff, &usb_msg.eid); |
| 338 |
} else { |
| 339 |
/* SIDH | SIDL |
| 340 |
* 10 - 3 | 2 1 0 x x x x x |
| 341 |
*/ |
| 342 |
put_unaligned_be16((cf->can_id & CAN_SFF_MASK) << 5, |
| 343 |
&usb_msg.sid); |
| 344 |
usb_msg.eid = 0; |
| 345 |
} |
| 346 |
|
| 347 |
usb_msg.dlc = cf->len; |
| 348 |
|
| 349 |
memcpy(usb_msg.data, cf->data, usb_msg.dlc); |
| 350 |
|
| 351 |
if (cf->can_id & CAN_RTR_FLAG) |
| 352 |
usb_msg.dlc |= MCBA_DLC_RTR_MASK; |
| 353 |
|
| 354 |
can_put_echo_skb(skb, priv->netdev, ctx->ndx, 0); |
| 355 |
|
| 356 |
err = mcba_usb_xmit(priv, (struct mcba_usb_msg *)&usb_msg, ctx); |
| 357 |
if (err) |
| 358 |
goto xmit_failed; |
| 359 |
|
| 360 |
return NETDEV_TX_OK; |
| 361 |
|
| 362 |
xmit_failed: |
| 363 |
can_free_echo_skb(priv->netdev, ctx->ndx, NULL); |
| 364 |
mcba_usb_free_ctx(ctx); |
| 365 |
stats->tx_dropped++; |
| 366 |
|
| 367 |
return NETDEV_TX_OK; |
| 368 |
} |
| 369 |
|
| 370 |
/* Send cmd to device */ |
| 371 |
static void mcba_usb_xmit_cmd(struct mcba_priv *priv, |
| 372 |
struct mcba_usb_msg *usb_msg) |
| 373 |
{ |
| 374 |
struct mcba_usb_ctx *ctx = NULL; |
| 375 |
int err; |
| 376 |
|
| 377 |
ctx = mcba_usb_get_free_ctx(priv, NULL); |
| 378 |
if (!ctx) { |
| 379 |
netdev_err(priv->netdev, |
| 380 |
"Lack of free ctx. Sending (%d) cmd aborted", |
| 381 |
usb_msg->cmd_id); |
| 382 |
|
| 383 |
return; |
| 384 |
} |
| 385 |
|
| 386 |
err = mcba_usb_xmit(priv, usb_msg, ctx); |
| 387 |
if (err) |
| 388 |
netdev_err(priv->netdev, "Failed to send cmd (%d)", |
| 389 |
usb_msg->cmd_id); |
| 390 |
} |
| 391 |
|
| 392 |
static void mcba_usb_xmit_change_bitrate(struct mcba_priv *priv, u16 bitrate) |
| 393 |
{ |
| 394 |
struct mcba_usb_msg_change_bitrate usb_msg = { |
| 395 |
.cmd_id = MBCA_CMD_CHANGE_BIT_RATE |
| 396 |
}; |
| 397 |
|
| 398 |
put_unaligned_be16(bitrate, &usb_msg.bitrate); |
| 399 |
|
| 400 |
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg); |
| 401 |
} |
| 402 |
|
| 403 |
static void mcba_usb_xmit_read_fw_ver(struct mcba_priv *priv, u8 pic) |
| 404 |
{ |
| 405 |
struct mcba_usb_msg_fw_ver usb_msg = { |
| 406 |
.cmd_id = MBCA_CMD_READ_FW_VERSION, |
| 407 |
.pic = pic |
| 408 |
}; |
| 409 |
|
| 410 |
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg); |
| 411 |
} |
| 412 |
|
| 413 |
static void mcba_usb_process_can(struct mcba_priv *priv, |
| 414 |
struct mcba_usb_msg_can *msg) |
| 415 |
{ |
| 416 |
struct can_frame *cf; |
| 417 |
struct sk_buff *skb; |
| 418 |
struct net_device_stats *stats = &priv->netdev->stats; |
| 419 |
u16 sid; |
| 420 |
|
| 421 |
skb = alloc_can_skb(priv->netdev, &cf); |
| 422 |
if (!skb) |
| 423 |
return; |
| 424 |
|
| 425 |
sid = get_unaligned_be16(&msg->sid); |
| 426 |
|
| 427 |
if (sid & MCBA_SIDL_EXID_MASK) { |
| 428 |
/* SIDH | SIDL | EIDH | EIDL |
| 429 |
* 28 - 21 | 20 19 18 x x x 17 16 | 15 - 8 | 7 - 0 |
| 430 |
*/ |
| 431 |
cf->can_id = CAN_EFF_FLAG; |
| 432 |
|
| 433 |
/* store 28-18 bits */ |
| 434 |
cf->can_id |= (sid & 0xffe0) << 13; |
| 435 |
/* store 17-16 bits */ |
| 436 |
cf->can_id |= (sid & 3) << 16; |
| 437 |
/* store 15-0 bits */ |
| 438 |
cf->can_id |= get_unaligned_be16(&msg->eid); |
| 439 |
} else { |
| 440 |
/* SIDH | SIDL |
| 441 |
* 10 - 3 | 2 1 0 x x x x x |
| 442 |
*/ |
| 443 |
cf->can_id = (sid & 0xffe0) >> 5; |
| 444 |
} |
| 445 |
|
| 446 |
cf->len = can_cc_dlc2len(msg->dlc & MCBA_DLC_MASK); |
| 447 |
|
| 448 |
if (msg->dlc & MCBA_DLC_RTR_MASK) { |
| 449 |
cf->can_id |= CAN_RTR_FLAG; |
| 450 |
} else { |
| 451 |
memcpy(cf->data, msg->data, cf->len); |
| 452 |
|
| 453 |
stats->rx_bytes += cf->len; |
| 454 |
} |
| 455 |
stats->rx_packets++; |
| 456 |
|
| 457 |
netif_rx(skb); |
| 458 |
} |
| 459 |
|
| 460 |
static void mcba_usb_process_ka_usb(struct mcba_priv *priv, |
| 461 |
struct mcba_usb_msg_ka_usb *msg) |
| 462 |
{ |
| 463 |
if (unlikely(priv->usb_ka_first_pass)) { |
| 464 |
netdev_info(priv->netdev, "PIC USB version %u.%u\n", |
| 465 |
msg->soft_ver_major, msg->soft_ver_minor); |
| 466 |
|
| 467 |
priv->usb_ka_first_pass = false; |
| 468 |
} |
| 469 |
|
| 470 |
if (msg->termination_state == MCBA_VER_TERMINATION_ON) |
| 471 |
priv->can.termination = MCBA_TERMINATION_ENABLED; |
| 472 |
else |
| 473 |
priv->can.termination = MCBA_TERMINATION_DISABLED; |
| 474 |
} |
| 475 |
|
| 476 |
static u32 convert_can2host_bitrate(struct mcba_usb_msg_ka_can *msg) |
| 477 |
{ |
| 478 |
const u32 bitrate = get_unaligned_be16(&msg->can_bitrate); |
| 479 |
|
| 480 |
if ((bitrate == 33) || (bitrate == 83)) |
| 481 |
return bitrate * 1000 + 333; |
| 482 |
else |
| 483 |
return bitrate * 1000; |
| 484 |
} |
| 485 |
|
| 486 |
static void mcba_usb_process_ka_can(struct mcba_priv *priv, |
| 487 |
struct mcba_usb_msg_ka_can *msg) |
| 488 |
{ |
| 489 |
if (unlikely(priv->can_ka_first_pass)) { |
| 490 |
netdev_info(priv->netdev, "PIC CAN version %u.%u\n", |
| 491 |
msg->soft_ver_major, msg->soft_ver_minor); |
| 492 |
|
| 493 |
priv->can_ka_first_pass = false; |
| 494 |
} |
| 495 |
|
| 496 |
if (unlikely(priv->can_speed_check)) { |
| 497 |
const u32 bitrate = convert_can2host_bitrate(msg); |
| 498 |
|
| 499 |
priv->can_speed_check = false; |
| 500 |
|
| 501 |
if (bitrate != priv->can.bittiming.bitrate) |
| 502 |
netdev_err( |
| 503 |
priv->netdev, |
| 504 |
"Wrong bitrate reported by the device (%u). Expected %u", |
| 505 |
bitrate, priv->can.bittiming.bitrate); |
| 506 |
} |
| 507 |
|
| 508 |
priv->bec.txerr = msg->tx_err_cnt; |
| 509 |
priv->bec.rxerr = msg->rx_err_cnt; |
| 510 |
|
| 511 |
if (msg->tx_bus_off) |
| 512 |
priv->can.state = CAN_STATE_BUS_OFF; |
| 513 |
|
| 514 |
else if ((priv->bec.txerr > MCBA_CAN_STATE_ERR_PSV_TH) || |
| 515 |
(priv->bec.rxerr > MCBA_CAN_STATE_ERR_PSV_TH)) |
| 516 |
priv->can.state = CAN_STATE_ERROR_PASSIVE; |
| 517 |
|
| 518 |
else if ((priv->bec.txerr > MCBA_CAN_STATE_WRN_TH) || |
| 519 |
(priv->bec.rxerr > MCBA_CAN_STATE_WRN_TH)) |
| 520 |
priv->can.state = CAN_STATE_ERROR_WARNING; |
| 521 |
} |
| 522 |
|
| 523 |
static void mcba_usb_process_rx(struct mcba_priv *priv, |
| 524 |
struct mcba_usb_msg *msg) |
| 525 |
{ |
| 526 |
switch (msg->cmd_id) { |
| 527 |
case MBCA_CMD_I_AM_ALIVE_FROM_CAN: |
| 528 |
mcba_usb_process_ka_can(priv, |
| 529 |
(struct mcba_usb_msg_ka_can *)msg); |
| 530 |
break; |
| 531 |
|
| 532 |
case MBCA_CMD_I_AM_ALIVE_FROM_USB: |
| 533 |
mcba_usb_process_ka_usb(priv, |
| 534 |
(struct mcba_usb_msg_ka_usb *)msg); |
| 535 |
break; |
| 536 |
|
| 537 |
case MBCA_CMD_RECEIVE_MESSAGE: |
| 538 |
mcba_usb_process_can(priv, (struct mcba_usb_msg_can *)msg); |
| 539 |
break; |
| 540 |
|
| 541 |
case MBCA_CMD_NOTHING_TO_SEND: |
| 542 |
/* Side effect of communication between PIC_USB and PIC_CAN. |
| 543 |
* PIC_CAN is telling us that it has nothing to send |
| 544 |
*/ |
| 545 |
break; |
| 546 |
|
| 547 |
case MBCA_CMD_TRANSMIT_MESSAGE_RSP: |
| 548 |
/* Transmission response from the device containing timestamp */ |
| 549 |
break; |
| 550 |
|
| 551 |
default: |
| 552 |
netdev_warn(priv->netdev, "Unsupported msg (0x%X)", |
| 553 |
msg->cmd_id); |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/* Callback for reading data from device |
| 559 |
* |
| 560 |
* Check urb status, call read function and resubmit urb read operation. |
| 561 |
*/ |
| 562 |
static void mcba_usb_read_bulk_callback(struct urb *urb) |
| 563 |
{ |
| 564 |
struct mcba_priv *priv = urb->context; |
| 565 |
struct net_device *netdev; |
| 566 |
int retval; |
| 567 |
int pos = 0; |
| 568 |
|
| 569 |
netdev = priv->netdev; |
| 570 |
|
| 571 |
if (!netif_device_present(netdev)) |
| 572 |
return; |
| 573 |
|
| 574 |
switch (urb->status) { |
| 575 |
case 0: /* success */ |
| 576 |
break; |
| 577 |
|
| 578 |
case -ENOENT: |
| 579 |
case -EPIPE: |
| 580 |
case -EPROTO: |
| 581 |
case -ESHUTDOWN: |
| 582 |
return; |
| 583 |
|
| 584 |
default: |
| 585 |
netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status); |
| 586 |
|
| 587 |
goto resubmit_urb; |
| 588 |
} |
| 589 |
|
| 590 |
while (pos < urb->actual_length) { |
| 591 |
struct mcba_usb_msg *msg; |
| 592 |
|
| 593 |
if (pos + sizeof(struct mcba_usb_msg) > urb->actual_length) { |
| 594 |
netdev_err(priv->netdev, "format error\n"); |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
msg = (struct mcba_usb_msg *)(urb->transfer_buffer + pos); |
| 599 |
mcba_usb_process_rx(priv, msg); |
| 600 |
|
| 601 |
pos += sizeof(struct mcba_usb_msg); |
| 602 |
} |
| 603 |
|
| 604 |
resubmit_urb: |
| 605 |
|
| 606 |
usb_fill_bulk_urb(urb, priv->udev, |
| 607 |
priv->rx_pipe, |
| 608 |
urb->transfer_buffer, MCBA_USB_RX_BUFF_SIZE, |
| 609 |
mcba_usb_read_bulk_callback, priv); |
| 610 |
|
| 611 |
usb_anchor_urb(urb, &priv->rx_submitted); |
| 612 |
|
| 613 |
retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 614 |
if (!retval) |
| 615 |
return; |
| 616 |
|
| 617 |
usb_unanchor_urb(urb); |
| 618 |
|
| 619 |
if (retval == -ENODEV) |
| 620 |
netif_device_detach(netdev); |
| 621 |
else |
| 622 |
netdev_err(netdev, "failed resubmitting read bulk urb: %d\n", |
| 623 |
retval); |
| 624 |
} |
| 625 |
|
| 626 |
/* Start USB device */ |
| 627 |
static int mcba_usb_start(struct mcba_priv *priv) |
| 628 |
{ |
| 629 |
struct net_device *netdev = priv->netdev; |
| 630 |
int err, i; |
| 631 |
|
| 632 |
mcba_init_ctx(priv); |
| 633 |
|
| 634 |
for (i = 0; i < MCBA_MAX_RX_URBS; i++) { |
| 635 |
struct urb *urb = NULL; |
| 636 |
u8 *buf; |
| 637 |
dma_addr_t buf_dma; |
| 638 |
|
| 639 |
/* create a URB, and a buffer for it */ |
| 640 |
urb = usb_alloc_urb(0, GFP_KERNEL); |
| 641 |
if (!urb) { |
| 642 |
err = -ENOMEM; |
| 643 |
break; |
| 644 |
} |
| 645 |
|
| 646 |
buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, |
| 647 |
GFP_KERNEL, &buf_dma); |
| 648 |
if (!buf) { |
| 649 |
netdev_err(netdev, "No memory left for USB buffer\n"); |
| 650 |
usb_free_urb(urb); |
| 651 |
err = -ENOMEM; |
| 652 |
break; |
| 653 |
} |
| 654 |
|
| 655 |
urb->transfer_dma = buf_dma; |
| 656 |
|
| 657 |
usb_fill_bulk_urb(urb, priv->udev, |
| 658 |
priv->rx_pipe, |
| 659 |
buf, MCBA_USB_RX_BUFF_SIZE, |
| 660 |
mcba_usb_read_bulk_callback, priv); |
| 661 |
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 662 |
usb_anchor_urb(urb, &priv->rx_submitted); |
| 663 |
|
| 664 |
err = usb_submit_urb(urb, GFP_KERNEL); |
| 665 |
if (err) { |
| 666 |
usb_unanchor_urb(urb); |
| 667 |
usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, |
| 668 |
buf, buf_dma); |
| 669 |
usb_free_urb(urb); |
| 670 |
break; |
| 671 |
} |
| 672 |
|
| 673 |
priv->rxbuf[i] = buf; |
| 674 |
priv->rxbuf_dma[i] = buf_dma; |
| 675 |
|
| 676 |
/* Drop reference, USB core will take care of freeing it */ |
| 677 |
usb_free_urb(urb); |
| 678 |
} |
| 679 |
|
| 680 |
/* Did we submit any URBs */ |
| 681 |
if (i == 0) { |
| 682 |
netdev_warn(netdev, "couldn't setup read URBs\n"); |
| 683 |
return err; |
| 684 |
} |
| 685 |
|
| 686 |
/* Warn if we've couldn't transmit all the URBs */ |
| 687 |
if (i < MCBA_MAX_RX_URBS) |
| 688 |
netdev_warn(netdev, "rx performance may be slow\n"); |
| 689 |
|
| 690 |
mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_USB); |
| 691 |
mcba_usb_xmit_read_fw_ver(priv, MCBA_VER_REQ_CAN); |
| 692 |
|
| 693 |
return err; |
| 694 |
} |
| 695 |
|
| 696 |
/* Open USB device */ |
| 697 |
static int mcba_usb_open(struct net_device *netdev) |
| 698 |
{ |
| 699 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 700 |
int err; |
| 701 |
|
| 702 |
/* common open */ |
| 703 |
err = open_candev(netdev); |
| 704 |
if (err) |
| 705 |
return err; |
| 706 |
|
| 707 |
priv->can_speed_check = true; |
| 708 |
priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 709 |
|
| 710 |
netif_start_queue(netdev); |
| 711 |
|
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
static void mcba_urb_unlink(struct mcba_priv *priv) |
| 716 |
{ |
| 717 |
int i; |
| 718 |
|
| 719 |
usb_kill_anchored_urbs(&priv->rx_submitted); |
| 720 |
|
| 721 |
for (i = 0; i < MCBA_MAX_RX_URBS; ++i) |
| 722 |
usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, |
| 723 |
priv->rxbuf[i], priv->rxbuf_dma[i]); |
| 724 |
|
| 725 |
usb_kill_anchored_urbs(&priv->tx_submitted); |
| 726 |
} |
| 727 |
|
| 728 |
/* Close USB device */ |
| 729 |
static int mcba_usb_close(struct net_device *netdev) |
| 730 |
{ |
| 731 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 732 |
|
| 733 |
priv->can.state = CAN_STATE_STOPPED; |
| 734 |
|
| 735 |
netif_stop_queue(netdev); |
| 736 |
|
| 737 |
/* Stop polling */ |
| 738 |
mcba_urb_unlink(priv); |
| 739 |
|
| 740 |
close_candev(netdev); |
| 741 |
|
| 742 |
return 0; |
| 743 |
} |
| 744 |
|
| 745 |
/* Set network device mode |
| 746 |
* |
| 747 |
* Maybe we should leave this function empty, because the device |
| 748 |
* set mode variable with open command. |
| 749 |
*/ |
| 750 |
static int mcba_net_set_mode(struct net_device *netdev, enum can_mode mode) |
| 751 |
{ |
| 752 |
return 0; |
| 753 |
} |
| 754 |
|
| 755 |
static int mcba_net_get_berr_counter(const struct net_device *netdev, |
| 756 |
struct can_berr_counter *bec) |
| 757 |
{ |
| 758 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 759 |
|
| 760 |
bec->txerr = priv->bec.txerr; |
| 761 |
bec->rxerr = priv->bec.rxerr; |
| 762 |
|
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
static const struct net_device_ops mcba_netdev_ops = { |
| 767 |
.ndo_open = mcba_usb_open, |
| 768 |
.ndo_stop = mcba_usb_close, |
| 769 |
.ndo_start_xmit = mcba_usb_start_xmit, |
| 770 |
}; |
| 771 |
|
| 772 |
static const struct ethtool_ops mcba_ethtool_ops = { |
| 773 |
.get_ts_info = ethtool_op_get_ts_info, |
| 774 |
}; |
| 775 |
|
| 776 |
/* Microchip CANBUS has hardcoded bittiming values by default. |
| 777 |
* This function sends request via USB to change the speed and align bittiming |
| 778 |
* values for presentation purposes only |
| 779 |
*/ |
| 780 |
static int mcba_net_set_bittiming(struct net_device *netdev) |
| 781 |
{ |
| 782 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 783 |
const u16 bitrate_kbps = priv->can.bittiming.bitrate / 1000; |
| 784 |
|
| 785 |
mcba_usb_xmit_change_bitrate(priv, bitrate_kbps); |
| 786 |
|
| 787 |
return 0; |
| 788 |
} |
| 789 |
|
| 790 |
static int mcba_set_termination(struct net_device *netdev, u16 term) |
| 791 |
{ |
| 792 |
struct mcba_priv *priv = netdev_priv(netdev); |
| 793 |
struct mcba_usb_msg_termination usb_msg = { |
| 794 |
.cmd_id = MBCA_CMD_SETUP_TERMINATION_RESISTANCE |
| 795 |
}; |
| 796 |
|
| 797 |
if (term == MCBA_TERMINATION_ENABLED) |
| 798 |
usb_msg.termination = MCBA_VER_TERMINATION_ON; |
| 799 |
else |
| 800 |
usb_msg.termination = MCBA_VER_TERMINATION_OFF; |
| 801 |
|
| 802 |
mcba_usb_xmit_cmd(priv, (struct mcba_usb_msg *)&usb_msg); |
| 803 |
|
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
static int mcba_usb_probe(struct usb_interface *intf, |
| 808 |
const struct usb_device_id *id) |
| 809 |
{ |
| 810 |
struct net_device *netdev; |
| 811 |
struct mcba_priv *priv; |
| 812 |
int err; |
| 813 |
struct usb_device *usbdev = interface_to_usbdev(intf); |
| 814 |
struct usb_endpoint_descriptor *in, *out; |
| 815 |
|
| 816 |
err = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, NULL); |
| 817 |
if (err) { |
| 818 |
dev_err(&intf->dev, "Can't find endpoints\n"); |
| 819 |
return err; |
| 820 |
} |
| 821 |
|
| 822 |
netdev = alloc_candev(sizeof(struct mcba_priv), MCBA_MAX_TX_URBS); |
| 823 |
if (!netdev) { |
| 824 |
dev_err(&intf->dev, "Couldn't alloc candev\n"); |
| 825 |
return -ENOMEM; |
| 826 |
} |
| 827 |
|
| 828 |
priv = netdev_priv(netdev); |
| 829 |
|
| 830 |
priv->udev = usbdev; |
| 831 |
priv->netdev = netdev; |
| 832 |
priv->usb_ka_first_pass = true; |
| 833 |
priv->can_ka_first_pass = true; |
| 834 |
priv->can_speed_check = false; |
| 835 |
|
| 836 |
init_usb_anchor(&priv->rx_submitted); |
| 837 |
init_usb_anchor(&priv->tx_submitted); |
| 838 |
|
| 839 |
usb_set_intfdata(intf, priv); |
| 840 |
|
| 841 |
/* Init CAN device */ |
| 842 |
priv->can.state = CAN_STATE_STOPPED; |
| 843 |
priv->can.termination_const = mcba_termination; |
| 844 |
priv->can.termination_const_cnt = ARRAY_SIZE(mcba_termination); |
| 845 |
priv->can.bitrate_const = mcba_bitrate; |
| 846 |
priv->can.bitrate_const_cnt = ARRAY_SIZE(mcba_bitrate); |
| 847 |
|
| 848 |
priv->can.do_set_termination = mcba_set_termination; |
| 849 |
priv->can.do_set_mode = mcba_net_set_mode; |
| 850 |
priv->can.do_get_berr_counter = mcba_net_get_berr_counter; |
| 851 |
priv->can.do_set_bittiming = mcba_net_set_bittiming; |
| 852 |
|
| 853 |
netdev->netdev_ops = &mcba_netdev_ops; |
| 854 |
netdev->ethtool_ops = &mcba_ethtool_ops; |
| 855 |
|
| 856 |
netdev->flags |= IFF_ECHO; /* we support local echo */ |
| 857 |
|
| 858 |
SET_NETDEV_DEV(netdev, &intf->dev); |
| 859 |
|
| 860 |
err = register_candev(netdev); |
| 861 |
if (err) { |
| 862 |
netdev_err(netdev, "couldn't register CAN device: %d\n", err); |
| 863 |
|
| 864 |
goto cleanup_free_candev; |
| 865 |
} |
| 866 |
|
| 867 |
priv->rx_pipe = usb_rcvbulkpipe(priv->udev, in->bEndpointAddress); |
| 868 |
priv->tx_pipe = usb_sndbulkpipe(priv->udev, out->bEndpointAddress); |
| 869 |
|
| 870 |
/* Start USB dev only if we have successfully registered CAN device */ |
| 871 |
err = mcba_usb_start(priv); |
| 872 |
if (err) { |
| 873 |
if (err == -ENODEV) |
| 874 |
netif_device_detach(priv->netdev); |
| 875 |
|
| 876 |
netdev_warn(netdev, "couldn't start device: %d\n", err); |
| 877 |
|
| 878 |
goto cleanup_unregister_candev; |
| 879 |
} |
| 880 |
|
| 881 |
dev_info(&intf->dev, "Microchip CAN BUS Analyzer connected\n"); |
| 882 |
|
| 883 |
return 0; |
| 884 |
|
| 885 |
cleanup_unregister_candev: |
| 886 |
unregister_candev(priv->netdev); |
| 887 |
|
| 888 |
cleanup_free_candev: |
| 889 |
free_candev(netdev); |
| 890 |
|
| 891 |
return err; |
| 892 |
} |
| 893 |
|
| 894 |
/* Called by the usb core when driver is unloaded or device is removed */ |
| 895 |
static void mcba_usb_disconnect(struct usb_interface *intf) |
| 896 |
{ |
| 897 |
struct mcba_priv *priv = usb_get_intfdata(intf); |
| 898 |
|
| 899 |
usb_set_intfdata(intf, NULL); |
| 900 |
|
| 901 |
netdev_info(priv->netdev, "device disconnected\n"); |
| 902 |
|
| 903 |
unregister_candev(priv->netdev); |
| 904 |
mcba_urb_unlink(priv); |
| 905 |
free_candev(priv->netdev); |
| 906 |
} |
| 907 |
|
| 908 |
static struct usb_driver mcba_usb_driver = { |
| 909 |
.name = MCBA_MODULE_NAME, |
| 910 |
.probe = mcba_usb_probe, |
| 911 |
.disconnect = mcba_usb_disconnect, |
| 912 |
.id_table = mcba_usb_table, |
| 913 |
}; |
| 914 |
|
| 915 |
module_usb_driver(mcba_usb_driver); |
| 916 |
|
| 917 |
MODULE_AUTHOR("Remigiusz Kołłątaj <[email protected]>"); |
| 918 |
MODULE_DESCRIPTION("SocketCAN driver for Microchip CAN BUS Analyzer Tool"); |
| 919 |
MODULE_LICENSE("GPL v2"); |