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