1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CAN driver for PEAK System PCAN-USB adapter 4 * Derived from the PCAN project file driver/src/pcan_usb.c 5 * 6 * Copyright (C) 2003-2010 PEAK System-Technik GmbH 7 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com> 8 * 9 * Many thanks to Klaus Hitschler <klaus.hitschler@gmx.de> 10 */ 11 #include <linux/netdevice.h> 12 #include <linux/usb.h> 13 #include <linux/module.h> 14 15 #include <linux/can.h> 16 #include <linux/can/dev.h> 17 #include <linux/can/error.h> 18 19 #include "pcan_usb_core.h" 20 21 /* PCAN-USB Endpoints */ 22 #define PCAN_USB_EP_CMDOUT 1 23 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN) 24 #define PCAN_USB_EP_MSGOUT 2 25 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN) 26 27 /* PCAN-USB command struct */ 28 #define PCAN_USB_CMD_FUNC 0 29 #define PCAN_USB_CMD_NUM 1 30 #define PCAN_USB_CMD_ARGS 2 31 #define PCAN_USB_CMD_ARGS_LEN 14 32 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \ 33 PCAN_USB_CMD_ARGS_LEN) 34 35 /* PCAN-USB commands */ 36 #define PCAN_USB_CMD_BITRATE 1 37 #define PCAN_USB_CMD_SET_BUS 3 38 #define PCAN_USB_CMD_DEVID 4 39 #define PCAN_USB_CMD_SN 6 40 #define PCAN_USB_CMD_REGISTER 9 41 #define PCAN_USB_CMD_EXT_VCC 10 42 #define PCAN_USB_CMD_ERR_FR 11 43 44 /* PCAN_USB_CMD_SET_BUS number arg */ 45 #define PCAN_USB_BUS_XCVER 2 46 #define PCAN_USB_BUS_SILENT_MODE 3 47 48 /* PCAN_USB_CMD_xxx functions */ 49 #define PCAN_USB_GET 1 50 #define PCAN_USB_SET 2 51 52 /* PCAN-USB command timeout (ms.) */ 53 #define PCAN_USB_COMMAND_TIMEOUT 1000 54 55 /* PCAN-USB startup timeout (ms.) */ 56 #define PCAN_USB_STARTUP_TIMEOUT 10 57 58 /* PCAN-USB rx/tx buffers size */ 59 #define PCAN_USB_RX_BUFFER_SIZE 64 60 #define PCAN_USB_TX_BUFFER_SIZE 64 61 62 #define PCAN_USB_MSG_HEADER_LEN 2 63 64 /* PCAN-USB adapter internal clock (MHz) */ 65 #define PCAN_USB_CRYSTAL_HZ 16000000 66 67 /* PCAN-USB USB message record status/len field */ 68 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7) 69 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6) 70 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5) 71 #define PCAN_USB_STATUSLEN_RTR (1 << 4) 72 #define PCAN_USB_STATUSLEN_DLC (0xf) 73 74 /* PCAN-USB error flags */ 75 #define PCAN_USB_ERROR_TXFULL 0x01 76 #define PCAN_USB_ERROR_RXQOVR 0x02 77 #define PCAN_USB_ERROR_BUS_LIGHT 0x04 78 #define PCAN_USB_ERROR_BUS_HEAVY 0x08 79 #define PCAN_USB_ERROR_BUS_OFF 0x10 80 #define PCAN_USB_ERROR_RXQEMPTY 0x20 81 #define PCAN_USB_ERROR_QOVR 0x40 82 #define PCAN_USB_ERROR_TXQFULL 0x80 83 84 #define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \ 85 PCAN_USB_ERROR_BUS_HEAVY | \ 86 PCAN_USB_ERROR_BUS_OFF) 87 88 /* SJA1000 modes */ 89 #define SJA1000_MODE_NORMAL 0x00 90 #define SJA1000_MODE_INIT 0x01 91 92 /* 93 * tick duration = 42.666 us => 94 * (tick_number * 44739243) >> 20 ~ (tick_number * 42666) / 1000 95 * accuracy = 10^-7 96 */ 97 #define PCAN_USB_TS_DIV_SHIFTER 20 98 #define PCAN_USB_TS_US_PER_TICK 44739243 99 100 /* PCAN-USB messages record types */ 101 #define PCAN_USB_REC_ERROR 1 102 #define PCAN_USB_REC_ANALOG 2 103 #define PCAN_USB_REC_BUSLOAD 3 104 #define PCAN_USB_REC_TS 4 105 #define PCAN_USB_REC_BUSEVT 5 106 107 /* CAN bus events notifications selection mask */ 108 #define PCAN_USB_ERR_RXERR 0x02 /* ask for rxerr counter */ 109 #define PCAN_USB_ERR_TXERR 0x04 /* ask for txerr counter */ 110 111 /* This mask generates an usb packet each time the state of the bus changes. 112 * In other words, its interest is to know which side among rx and tx is 113 * responsible of the change of the bus state. 114 */ 115 #define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR) 116 117 /* identify bus event packets with rx/tx error counters */ 118 #define PCAN_USB_ERR_CNT 0x80 119 120 /* private to PCAN-USB adapter */ 121 struct pcan_usb { 122 struct peak_usb_device dev; 123 struct peak_time_ref time_ref; 124 struct timer_list restart_timer; 125 struct can_berr_counter bec; 126 }; 127 128 /* incoming message context for decoding */ 129 struct pcan_usb_msg_context { 130 u16 ts16; 131 u8 prev_ts8; 132 u8 *ptr; 133 u8 *end; 134 u8 rec_cnt; 135 u8 rec_idx; 136 u8 rec_ts_idx; 137 struct net_device *netdev; 138 struct pcan_usb *pdev; 139 }; 140 141 /* 142 * send a command 143 */ 144 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p) 145 { 146 int err; 147 int actual_length; 148 149 /* usb device unregistered? */ 150 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 151 return 0; 152 153 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f; 154 dev->cmd_buf[PCAN_USB_CMD_NUM] = n; 155 156 if (p) 157 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS, 158 p, PCAN_USB_CMD_ARGS_LEN); 159 160 err = usb_bulk_msg(dev->udev, 161 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT), 162 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length, 163 PCAN_USB_COMMAND_TIMEOUT); 164 if (err) 165 netdev_err(dev->netdev, 166 "sending cmd f=0x%x n=0x%x failure: %d\n", 167 f, n, err); 168 return err; 169 } 170 171 /* 172 * send a command then wait for its response 173 */ 174 static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p) 175 { 176 int err; 177 int actual_length; 178 179 /* usb device unregistered? */ 180 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 181 return 0; 182 183 /* first, send command */ 184 err = pcan_usb_send_cmd(dev, f, n, NULL); 185 if (err) 186 return err; 187 188 err = usb_bulk_msg(dev->udev, 189 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN), 190 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length, 191 PCAN_USB_COMMAND_TIMEOUT); 192 if (err) 193 netdev_err(dev->netdev, 194 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err); 195 else if (p) 196 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS, 197 PCAN_USB_CMD_ARGS_LEN); 198 199 return err; 200 } 201 202 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode) 203 { 204 u8 args[PCAN_USB_CMD_ARGS_LEN] = { 205 [1] = mode, 206 }; 207 208 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_REGISTER, PCAN_USB_SET, 209 args); 210 } 211 212 static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff) 213 { 214 u8 args[PCAN_USB_CMD_ARGS_LEN] = { 215 [0] = !!onoff, 216 }; 217 218 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, PCAN_USB_BUS_XCVER, 219 args); 220 } 221 222 static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff) 223 { 224 u8 args[PCAN_USB_CMD_ARGS_LEN] = { 225 [0] = !!onoff, 226 }; 227 228 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, 229 PCAN_USB_BUS_SILENT_MODE, args); 230 } 231 232 /* send the cmd to be notified from bus errors */ 233 static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask) 234 { 235 u8 args[PCAN_USB_CMD_ARGS_LEN] = { 236 [0] = err_mask, 237 }; 238 239 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args); 240 } 241 242 static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff) 243 { 244 u8 args[PCAN_USB_CMD_ARGS_LEN] = { 245 [0] = !!onoff, 246 }; 247 248 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_EXT_VCC, PCAN_USB_SET, args); 249 } 250 251 /* 252 * set bittiming value to can 253 */ 254 static int pcan_usb_set_bittiming(struct peak_usb_device *dev, 255 struct can_bittiming *bt) 256 { 257 u8 args[PCAN_USB_CMD_ARGS_LEN]; 258 u8 btr0, btr1; 259 260 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); 261 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | 262 (((bt->phase_seg2 - 1) & 0x7) << 4); 263 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 264 btr1 |= 0x80; 265 266 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", 267 btr0, btr1); 268 269 args[0] = btr1; 270 args[1] = btr0; 271 272 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_BITRATE, PCAN_USB_SET, args); 273 } 274 275 /* 276 * init/reset can 277 */ 278 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff) 279 { 280 int err; 281 282 err = pcan_usb_set_bus(dev, onoff); 283 if (err) 284 return err; 285 286 if (!onoff) { 287 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT); 288 } else { 289 /* the PCAN-USB needs time to init */ 290 set_current_state(TASK_INTERRUPTIBLE); 291 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT)); 292 } 293 294 return err; 295 } 296 297 /* 298 * handle end of waiting for the device to reset 299 */ 300 static void pcan_usb_restart(struct timer_list *t) 301 { 302 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer); 303 struct peak_usb_device *dev = &pdev->dev; 304 305 /* notify candev and netdev */ 306 peak_usb_restart_complete(dev); 307 } 308 309 /* 310 * handle the submission of the restart urb 311 */ 312 static void pcan_usb_restart_pending(struct urb *urb) 313 { 314 struct pcan_usb *pdev = urb->context; 315 316 /* the PCAN-USB needs time to restart */ 317 mod_timer(&pdev->restart_timer, 318 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT)); 319 320 /* can delete usb resources */ 321 peak_usb_async_complete(urb); 322 } 323 324 /* 325 * handle asynchronous restart 326 */ 327 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb, 328 u8 *buf) 329 { 330 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev); 331 332 if (timer_pending(&pdev->restart_timer)) 333 return -EBUSY; 334 335 /* set bus on */ 336 buf[PCAN_USB_CMD_FUNC] = 3; 337 buf[PCAN_USB_CMD_NUM] = 2; 338 buf[PCAN_USB_CMD_ARGS] = 1; 339 340 usb_fill_bulk_urb(urb, dev->udev, 341 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT), 342 buf, PCAN_USB_CMD_LEN, 343 pcan_usb_restart_pending, pdev); 344 345 return usb_submit_urb(urb, GFP_ATOMIC); 346 } 347 348 /* 349 * read serial number from device 350 */ 351 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number) 352 { 353 u8 args[PCAN_USB_CMD_ARGS_LEN]; 354 int err; 355 356 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_SN, PCAN_USB_GET, args); 357 if (err) { 358 netdev_err(dev->netdev, "getting serial failure: %d\n", err); 359 } else if (serial_number) { 360 __le32 tmp32; 361 362 memcpy(&tmp32, args, 4); 363 *serial_number = le32_to_cpu(tmp32); 364 } 365 366 return err; 367 } 368 369 /* 370 * read device id from device 371 */ 372 static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id) 373 { 374 u8 args[PCAN_USB_CMD_ARGS_LEN]; 375 int err; 376 377 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_DEVID, PCAN_USB_GET, args); 378 if (err) 379 netdev_err(dev->netdev, "getting device id failure: %d\n", err); 380 else if (device_id) 381 *device_id = args[0]; 382 383 return err; 384 } 385 386 /* 387 * update current time ref with received timestamp 388 */ 389 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc) 390 { 391 __le16 tmp16; 392 393 if ((mc->ptr+2) > mc->end) 394 return -EINVAL; 395 396 memcpy(&tmp16, mc->ptr, 2); 397 398 mc->ts16 = le16_to_cpu(tmp16); 399 400 if (mc->rec_idx > 0) 401 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16); 402 else 403 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16); 404 405 return 0; 406 } 407 408 /* 409 * decode received timestamp 410 */ 411 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet) 412 { 413 /* only 1st packet supplies a word timestamp */ 414 if (first_packet) { 415 __le16 tmp16; 416 417 if ((mc->ptr + 2) > mc->end) 418 return -EINVAL; 419 420 memcpy(&tmp16, mc->ptr, 2); 421 mc->ptr += 2; 422 423 mc->ts16 = le16_to_cpu(tmp16); 424 mc->prev_ts8 = mc->ts16 & 0x00ff; 425 } else { 426 u8 ts8; 427 428 if ((mc->ptr + 1) > mc->end) 429 return -EINVAL; 430 431 ts8 = *mc->ptr++; 432 433 if (ts8 < mc->prev_ts8) 434 mc->ts16 += 0x100; 435 436 mc->ts16 &= 0xff00; 437 mc->ts16 |= ts8; 438 mc->prev_ts8 = ts8; 439 } 440 441 return 0; 442 } 443 444 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n, 445 u8 status_len) 446 { 447 struct sk_buff *skb; 448 struct can_frame *cf; 449 enum can_state new_state; 450 451 /* ignore this error until 1st ts received */ 452 if (n == PCAN_USB_ERROR_QOVR) 453 if (!mc->pdev->time_ref.tick_count) 454 return 0; 455 456 new_state = mc->pdev->dev.can.state; 457 458 switch (mc->pdev->dev.can.state) { 459 case CAN_STATE_ERROR_ACTIVE: 460 if (n & PCAN_USB_ERROR_BUS_LIGHT) { 461 new_state = CAN_STATE_ERROR_WARNING; 462 break; 463 } 464 fallthrough; 465 466 case CAN_STATE_ERROR_WARNING: 467 if (n & PCAN_USB_ERROR_BUS_HEAVY) { 468 new_state = CAN_STATE_ERROR_PASSIVE; 469 break; 470 } 471 if (n & PCAN_USB_ERROR_BUS_OFF) { 472 new_state = CAN_STATE_BUS_OFF; 473 break; 474 } 475 if (n & ~PCAN_USB_ERROR_BUS) { 476 /* 477 * trick to bypass next comparison and process other 478 * errors 479 */ 480 new_state = CAN_STATE_MAX; 481 break; 482 } 483 if ((n & PCAN_USB_ERROR_BUS_LIGHT) == 0) { 484 /* no error (back to active state) */ 485 new_state = CAN_STATE_ERROR_ACTIVE; 486 break; 487 } 488 break; 489 490 case CAN_STATE_ERROR_PASSIVE: 491 if (n & PCAN_USB_ERROR_BUS_OFF) { 492 new_state = CAN_STATE_BUS_OFF; 493 break; 494 } 495 if (n & PCAN_USB_ERROR_BUS_LIGHT) { 496 new_state = CAN_STATE_ERROR_WARNING; 497 break; 498 } 499 if (n & ~PCAN_USB_ERROR_BUS) { 500 /* 501 * trick to bypass next comparison and process other 502 * errors 503 */ 504 new_state = CAN_STATE_MAX; 505 break; 506 } 507 508 if ((n & PCAN_USB_ERROR_BUS_HEAVY) == 0) { 509 /* no error (back to warning state) */ 510 new_state = CAN_STATE_ERROR_WARNING; 511 break; 512 } 513 break; 514 515 default: 516 /* do nothing waiting for restart */ 517 return 0; 518 } 519 520 /* donot post any error if current state didn't change */ 521 if (mc->pdev->dev.can.state == new_state) 522 return 0; 523 524 /* allocate an skb to store the error frame */ 525 skb = alloc_can_err_skb(mc->netdev, &cf); 526 if (!skb) 527 return -ENOMEM; 528 529 switch (new_state) { 530 case CAN_STATE_BUS_OFF: 531 cf->can_id |= CAN_ERR_BUSOFF; 532 mc->pdev->dev.can.can_stats.bus_off++; 533 can_bus_off(mc->netdev); 534 break; 535 536 case CAN_STATE_ERROR_PASSIVE: 537 cf->can_id |= CAN_ERR_CRTL; 538 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ? 539 CAN_ERR_CRTL_TX_PASSIVE : 540 CAN_ERR_CRTL_RX_PASSIVE; 541 cf->data[6] = mc->pdev->bec.txerr; 542 cf->data[7] = mc->pdev->bec.rxerr; 543 544 mc->pdev->dev.can.can_stats.error_passive++; 545 break; 546 547 case CAN_STATE_ERROR_WARNING: 548 cf->can_id |= CAN_ERR_CRTL; 549 cf->data[1] = (mc->pdev->bec.txerr > mc->pdev->bec.rxerr) ? 550 CAN_ERR_CRTL_TX_WARNING : 551 CAN_ERR_CRTL_RX_WARNING; 552 cf->data[6] = mc->pdev->bec.txerr; 553 cf->data[7] = mc->pdev->bec.rxerr; 554 555 mc->pdev->dev.can.can_stats.error_warning++; 556 break; 557 558 case CAN_STATE_ERROR_ACTIVE: 559 cf->can_id |= CAN_ERR_CRTL; 560 cf->data[1] = CAN_ERR_CRTL_ACTIVE; 561 562 /* sync local copies of rxerr/txerr counters */ 563 mc->pdev->bec.txerr = 0; 564 mc->pdev->bec.rxerr = 0; 565 break; 566 567 default: 568 /* CAN_STATE_MAX (trick to handle other errors) */ 569 if (n & PCAN_USB_ERROR_TXQFULL) 570 netdev_dbg(mc->netdev, "device Tx queue full)\n"); 571 572 if (n & PCAN_USB_ERROR_RXQOVR) { 573 netdev_dbg(mc->netdev, "data overrun interrupt\n"); 574 cf->can_id |= CAN_ERR_CRTL; 575 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW; 576 mc->netdev->stats.rx_over_errors++; 577 mc->netdev->stats.rx_errors++; 578 } 579 580 cf->data[6] = mc->pdev->bec.txerr; 581 cf->data[7] = mc->pdev->bec.rxerr; 582 583 new_state = mc->pdev->dev.can.state; 584 break; 585 } 586 587 mc->pdev->dev.can.state = new_state; 588 589 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) { 590 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb); 591 592 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, 593 &hwts->hwtstamp); 594 } 595 596 mc->netdev->stats.rx_packets++; 597 mc->netdev->stats.rx_bytes += cf->len; 598 netif_rx(skb); 599 600 return 0; 601 } 602 603 /* decode bus event usb packet: first byte contains rxerr while 2nd one contains 604 * txerr. 605 */ 606 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir) 607 { 608 struct pcan_usb *pdev = mc->pdev; 609 610 /* acccording to the content of the packet */ 611 switch (ir) { 612 case PCAN_USB_ERR_CNT: 613 614 /* save rx/tx error counters from in the device context */ 615 pdev->bec.rxerr = mc->ptr[0]; 616 pdev->bec.txerr = mc->ptr[1]; 617 break; 618 619 default: 620 /* reserved */ 621 break; 622 } 623 624 return 0; 625 } 626 627 /* 628 * decode non-data usb message 629 */ 630 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc, 631 u8 status_len) 632 { 633 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC; 634 u8 f, n; 635 int err; 636 637 /* check whether function and number can be read */ 638 if ((mc->ptr + 2) > mc->end) 639 return -EINVAL; 640 641 f = mc->ptr[PCAN_USB_CMD_FUNC]; 642 n = mc->ptr[PCAN_USB_CMD_NUM]; 643 mc->ptr += PCAN_USB_CMD_ARGS; 644 645 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) { 646 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx); 647 648 if (err) 649 return err; 650 651 /* Next packet in the buffer will have a timestamp on a single 652 * byte 653 */ 654 mc->rec_ts_idx++; 655 } 656 657 switch (f) { 658 case PCAN_USB_REC_ERROR: 659 err = pcan_usb_decode_error(mc, n, status_len); 660 if (err) 661 return err; 662 break; 663 664 case PCAN_USB_REC_ANALOG: 665 /* analog values (ignored) */ 666 rec_len = 2; 667 break; 668 669 case PCAN_USB_REC_BUSLOAD: 670 /* bus load (ignored) */ 671 rec_len = 1; 672 break; 673 674 case PCAN_USB_REC_TS: 675 /* only timestamp */ 676 if (pcan_usb_update_ts(mc)) 677 return -EINVAL; 678 break; 679 680 case PCAN_USB_REC_BUSEVT: 681 /* bus event notifications (get rxerr/txerr) */ 682 err = pcan_usb_handle_bus_evt(mc, n); 683 if (err) 684 return err; 685 break; 686 default: 687 netdev_err(mc->netdev, "unexpected function %u\n", f); 688 break; 689 } 690 691 if ((mc->ptr + rec_len) > mc->end) 692 return -EINVAL; 693 694 mc->ptr += rec_len; 695 696 return 0; 697 } 698 699 /* 700 * decode data usb message 701 */ 702 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len) 703 { 704 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC; 705 struct sk_buff *skb; 706 struct can_frame *cf; 707 struct skb_shared_hwtstamps *hwts; 708 709 skb = alloc_can_skb(mc->netdev, &cf); 710 if (!skb) 711 return -ENOMEM; 712 713 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) { 714 __le32 tmp32; 715 716 if ((mc->ptr + 4) > mc->end) 717 goto decode_failed; 718 719 memcpy(&tmp32, mc->ptr, 4); 720 mc->ptr += 4; 721 722 cf->can_id = (le32_to_cpu(tmp32) >> 3) | CAN_EFF_FLAG; 723 } else { 724 __le16 tmp16; 725 726 if ((mc->ptr + 2) > mc->end) 727 goto decode_failed; 728 729 memcpy(&tmp16, mc->ptr, 2); 730 mc->ptr += 2; 731 732 cf->can_id = le16_to_cpu(tmp16) >> 5; 733 } 734 735 can_frame_set_cc_len(cf, rec_len, mc->pdev->dev.can.ctrlmode); 736 737 /* Only first packet timestamp is a word */ 738 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx)) 739 goto decode_failed; 740 741 /* Next packet in the buffer will have a timestamp on a single byte */ 742 mc->rec_ts_idx++; 743 744 /* read data */ 745 memset(cf->data, 0x0, sizeof(cf->data)); 746 if (status_len & PCAN_USB_STATUSLEN_RTR) { 747 cf->can_id |= CAN_RTR_FLAG; 748 } else { 749 if ((mc->ptr + rec_len) > mc->end) 750 goto decode_failed; 751 752 memcpy(cf->data, mc->ptr, cf->len); 753 mc->ptr += rec_len; 754 } 755 756 /* convert timestamp into kernel time */ 757 hwts = skb_hwtstamps(skb); 758 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp); 759 760 /* update statistics */ 761 mc->netdev->stats.rx_packets++; 762 mc->netdev->stats.rx_bytes += cf->len; 763 /* push the skb */ 764 netif_rx(skb); 765 766 return 0; 767 768 decode_failed: 769 dev_kfree_skb(skb); 770 return -EINVAL; 771 } 772 773 /* 774 * process incoming message 775 */ 776 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf) 777 { 778 struct pcan_usb_msg_context mc = { 779 .rec_cnt = ibuf[1], 780 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN, 781 .end = ibuf + lbuf, 782 .netdev = dev->netdev, 783 .pdev = container_of(dev, struct pcan_usb, dev), 784 }; 785 int err; 786 787 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) { 788 u8 sl = *mc.ptr++; 789 790 /* handle status and error frames here */ 791 if (sl & PCAN_USB_STATUSLEN_INTERNAL) { 792 err = pcan_usb_decode_status(&mc, sl); 793 /* handle normal can frames here */ 794 } else { 795 err = pcan_usb_decode_data(&mc, sl); 796 } 797 } 798 799 return err; 800 } 801 802 /* 803 * process any incoming buffer 804 */ 805 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb) 806 { 807 int err = 0; 808 809 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) { 810 err = pcan_usb_decode_msg(dev, urb->transfer_buffer, 811 urb->actual_length); 812 813 } else if (urb->actual_length > 0) { 814 netdev_err(dev->netdev, "usb message length error (%u)\n", 815 urb->actual_length); 816 err = -EINVAL; 817 } 818 819 return err; 820 } 821 822 /* 823 * process outgoing packet 824 */ 825 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb, 826 u8 *obuf, size_t *size) 827 { 828 struct net_device *netdev = dev->netdev; 829 struct net_device_stats *stats = &netdev->stats; 830 struct can_frame *cf = (struct can_frame *)skb->data; 831 u8 *pc; 832 833 obuf[0] = 2; 834 obuf[1] = 1; 835 836 pc = obuf + PCAN_USB_MSG_HEADER_LEN; 837 838 /* status/len byte */ 839 *pc = can_get_cc_dlc(cf, dev->can.ctrlmode); 840 841 if (cf->can_id & CAN_RTR_FLAG) 842 *pc |= PCAN_USB_STATUSLEN_RTR; 843 844 /* can id */ 845 if (cf->can_id & CAN_EFF_FLAG) { 846 __le32 tmp32 = cpu_to_le32((cf->can_id & CAN_ERR_MASK) << 3); 847 848 *pc |= PCAN_USB_STATUSLEN_EXT_ID; 849 memcpy(++pc, &tmp32, 4); 850 pc += 4; 851 } else { 852 __le16 tmp16 = cpu_to_le16((cf->can_id & CAN_ERR_MASK) << 5); 853 854 memcpy(++pc, &tmp16, 2); 855 pc += 2; 856 } 857 858 /* can data */ 859 if (!(cf->can_id & CAN_RTR_FLAG)) { 860 memcpy(pc, cf->data, cf->len); 861 pc += cf->len; 862 } 863 864 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff); 865 866 return 0; 867 } 868 869 /* socket callback used to copy berr counters values received through USB */ 870 static int pcan_usb_get_berr_counter(const struct net_device *netdev, 871 struct can_berr_counter *bec) 872 { 873 struct peak_usb_device *dev = netdev_priv(netdev); 874 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev); 875 876 *bec = pdev->bec; 877 878 /* must return 0 */ 879 return 0; 880 } 881 882 /* 883 * start interface 884 */ 885 static int pcan_usb_start(struct peak_usb_device *dev) 886 { 887 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev); 888 int err; 889 890 /* number of bits used in timestamps read from adapter struct */ 891 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb); 892 893 pdev->bec.rxerr = 0; 894 pdev->bec.txerr = 0; 895 896 /* be notified on error counter changes (if requested by user) */ 897 if (dev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) { 898 err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK); 899 if (err) 900 netdev_warn(dev->netdev, 901 "Asking for BERR reporting error %u\n", 902 err); 903 } 904 905 /* if revision greater than 3, can put silent mode on/off */ 906 if (dev->device_rev > 3) { 907 err = pcan_usb_set_silent(dev, 908 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY); 909 if (err) 910 return err; 911 } 912 913 return pcan_usb_set_ext_vcc(dev, 0); 914 } 915 916 static int pcan_usb_init(struct peak_usb_device *dev) 917 { 918 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev); 919 u32 serial_number; 920 int err; 921 922 /* initialize a timer needed to wait for hardware restart */ 923 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0); 924 925 /* 926 * explicit use of dev_xxx() instead of netdev_xxx() here: 927 * information displayed are related to the device itself, not 928 * to the canx netdevice. 929 */ 930 err = pcan_usb_get_serial(dev, &serial_number); 931 if (err) { 932 dev_err(dev->netdev->dev.parent, 933 "unable to read %s serial number (err %d)\n", 934 pcan_usb.name, err); 935 return err; 936 } 937 938 dev_info(dev->netdev->dev.parent, 939 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n", 940 pcan_usb.name, dev->device_rev, serial_number, 941 pcan_usb.ctrl_count); 942 943 return 0; 944 } 945 946 /* 947 * probe function for new PCAN-USB usb interface 948 */ 949 static int pcan_usb_probe(struct usb_interface *intf) 950 { 951 struct usb_host_interface *if_desc; 952 int i; 953 954 if_desc = intf->altsetting; 955 956 /* check interface endpoint addresses */ 957 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) { 958 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc; 959 960 switch (ep->bEndpointAddress) { 961 case PCAN_USB_EP_CMDOUT: 962 case PCAN_USB_EP_CMDIN: 963 case PCAN_USB_EP_MSGOUT: 964 case PCAN_USB_EP_MSGIN: 965 break; 966 default: 967 return -ENODEV; 968 } 969 } 970 971 return 0; 972 } 973 974 /* 975 * describe the PCAN-USB adapter 976 */ 977 static const struct can_bittiming_const pcan_usb_const = { 978 .name = "pcan_usb", 979 .tseg1_min = 1, 980 .tseg1_max = 16, 981 .tseg2_min = 1, 982 .tseg2_max = 8, 983 .sjw_max = 4, 984 .brp_min = 1, 985 .brp_max = 64, 986 .brp_inc = 1, 987 }; 988 989 const struct peak_usb_adapter pcan_usb = { 990 .name = "PCAN-USB", 991 .device_id = PCAN_USB_PRODUCT_ID, 992 .ctrl_count = 1, 993 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY | 994 CAN_CTRLMODE_BERR_REPORTING | 995 CAN_CTRLMODE_CC_LEN8_DLC, 996 .clock = { 997 .freq = PCAN_USB_CRYSTAL_HZ / 2 , 998 }, 999 .bittiming_const = &pcan_usb_const, 1000 1001 /* size of device private data */ 1002 .sizeof_dev_private = sizeof(struct pcan_usb), 1003 1004 /* timestamps usage */ 1005 .ts_used_bits = 16, 1006 .ts_period = 24575, /* calibration period in ts. */ 1007 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK, /* us=(ts*scale) */ 1008 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER, /* >> shift */ 1009 1010 /* give here messages in/out endpoints */ 1011 .ep_msg_in = PCAN_USB_EP_MSGIN, 1012 .ep_msg_out = {PCAN_USB_EP_MSGOUT}, 1013 1014 /* size of rx/tx usb buffers */ 1015 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE, 1016 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE, 1017 1018 /* device callbacks */ 1019 .intf_probe = pcan_usb_probe, 1020 .dev_init = pcan_usb_init, 1021 .dev_set_bus = pcan_usb_write_mode, 1022 .dev_set_bittiming = pcan_usb_set_bittiming, 1023 .dev_get_device_id = pcan_usb_get_device_id, 1024 .dev_decode_buf = pcan_usb_decode_buf, 1025 .dev_encode_msg = pcan_usb_encode_msg, 1026 .dev_start = pcan_usb_start, 1027 .dev_restart_async = pcan_usb_restart_async, 1028 .do_get_berr_counter = pcan_usb_get_berr_counter, 1029 }; 1030