1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CAN driver for PEAK System PCAN-USB Pro adapter 4 * Derived from the PCAN project file driver/src/pcan_usbpro.c 5 * 6 * Copyright (C) 2003-2011 PEAK System-Technik GmbH 7 * Copyright (C) 2011-2012 Stephane Grosjean <s.grosjean@peak-system.com> 8 */ 9 #include <linux/netdevice.h> 10 #include <linux/usb.h> 11 #include <linux/module.h> 12 13 #include <linux/can.h> 14 #include <linux/can/dev.h> 15 #include <linux/can/error.h> 16 17 #include "pcan_usb_core.h" 18 #include "pcan_usb_pro.h" 19 20 #define PCAN_USBPRO_CHANNEL_COUNT 2 21 22 /* PCAN-USB Pro adapter internal clock (MHz) */ 23 #define PCAN_USBPRO_CRYSTAL_HZ 56000000 24 25 /* PCAN-USB Pro command timeout (ms.) */ 26 #define PCAN_USBPRO_COMMAND_TIMEOUT 1000 27 28 /* PCAN-USB Pro rx/tx buffers size */ 29 #define PCAN_USBPRO_RX_BUFFER_SIZE 1024 30 #define PCAN_USBPRO_TX_BUFFER_SIZE 64 31 32 #define PCAN_USBPRO_MSG_HEADER_LEN 4 33 34 /* some commands responses need to be re-submitted */ 35 #define PCAN_USBPRO_RSP_SUBMIT_MAX 2 36 37 #define PCAN_USBPRO_RTR 0x01 38 #define PCAN_USBPRO_EXT 0x02 39 40 #define PCAN_USBPRO_CMD_BUFFER_SIZE 512 41 42 /* handle device specific info used by the netdevices */ 43 struct pcan_usb_pro_interface { 44 struct peak_usb_device *dev[PCAN_USBPRO_CHANNEL_COUNT]; 45 struct peak_time_ref time_ref; 46 int cm_ignore_count; 47 int dev_opened_count; 48 }; 49 50 /* device information */ 51 struct pcan_usb_pro_device { 52 struct peak_usb_device dev; 53 struct pcan_usb_pro_interface *usb_if; 54 u32 cached_ccbt; 55 }; 56 57 /* internal structure used to handle messages sent to bulk urb */ 58 struct pcan_usb_pro_msg { 59 u8 *rec_ptr; 60 int rec_buffer_size; 61 int rec_buffer_len; 62 union { 63 __le16 *rec_cnt_rd; 64 __le32 *rec_cnt; 65 u8 *rec_buffer; 66 } u; 67 }; 68 69 /* records sizes table indexed on message id. (8-bits value) */ 70 static u16 pcan_usb_pro_sizeof_rec[256] = { 71 [PCAN_USBPRO_SETBTR] = sizeof(struct pcan_usb_pro_btr), 72 [PCAN_USBPRO_SETBUSACT] = sizeof(struct pcan_usb_pro_busact), 73 [PCAN_USBPRO_SETSILENT] = sizeof(struct pcan_usb_pro_silent), 74 [PCAN_USBPRO_SETFILTR] = sizeof(struct pcan_usb_pro_filter), 75 [PCAN_USBPRO_SETTS] = sizeof(struct pcan_usb_pro_setts), 76 [PCAN_USBPRO_GETDEVID] = sizeof(struct pcan_usb_pro_devid), 77 [PCAN_USBPRO_SETLED] = sizeof(struct pcan_usb_pro_setled), 78 [PCAN_USBPRO_RXMSG8] = sizeof(struct pcan_usb_pro_rxmsg), 79 [PCAN_USBPRO_RXMSG4] = sizeof(struct pcan_usb_pro_rxmsg) - 4, 80 [PCAN_USBPRO_RXMSG0] = sizeof(struct pcan_usb_pro_rxmsg) - 8, 81 [PCAN_USBPRO_RXRTR] = sizeof(struct pcan_usb_pro_rxmsg) - 8, 82 [PCAN_USBPRO_RXSTATUS] = sizeof(struct pcan_usb_pro_rxstatus), 83 [PCAN_USBPRO_RXTS] = sizeof(struct pcan_usb_pro_rxts), 84 [PCAN_USBPRO_TXMSG8] = sizeof(struct pcan_usb_pro_txmsg), 85 [PCAN_USBPRO_TXMSG4] = sizeof(struct pcan_usb_pro_txmsg) - 4, 86 [PCAN_USBPRO_TXMSG0] = sizeof(struct pcan_usb_pro_txmsg) - 8, 87 }; 88 89 /* 90 * initialize PCAN-USB Pro message data structure 91 */ 92 static u8 *pcan_msg_init(struct pcan_usb_pro_msg *pm, void *buffer_addr, 93 int buffer_size) 94 { 95 if (buffer_size < PCAN_USBPRO_MSG_HEADER_LEN) 96 return NULL; 97 98 pm->u.rec_buffer = (u8 *)buffer_addr; 99 pm->rec_buffer_size = pm->rec_buffer_len = buffer_size; 100 pm->rec_ptr = pm->u.rec_buffer + PCAN_USBPRO_MSG_HEADER_LEN; 101 102 return pm->rec_ptr; 103 } 104 105 static u8 *pcan_msg_init_empty(struct pcan_usb_pro_msg *pm, 106 void *buffer_addr, int buffer_size) 107 { 108 u8 *pr = pcan_msg_init(pm, buffer_addr, buffer_size); 109 110 if (pr) { 111 pm->rec_buffer_len = PCAN_USBPRO_MSG_HEADER_LEN; 112 *pm->u.rec_cnt = 0; 113 } 114 return pr; 115 } 116 117 /* 118 * add one record to a message being built 119 */ 120 static int pcan_msg_add_rec(struct pcan_usb_pro_msg *pm, int id, ...) 121 { 122 int len, i; 123 u8 *pc; 124 va_list ap; 125 126 va_start(ap, id); 127 128 pc = pm->rec_ptr + 1; 129 130 i = 0; 131 switch (id) { 132 case PCAN_USBPRO_TXMSG8: 133 i += 4; 134 fallthrough; 135 case PCAN_USBPRO_TXMSG4: 136 i += 4; 137 fallthrough; 138 case PCAN_USBPRO_TXMSG0: 139 *pc++ = va_arg(ap, int); 140 *pc++ = va_arg(ap, int); 141 *pc++ = va_arg(ap, int); 142 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32)); 143 pc += 4; 144 memcpy(pc, va_arg(ap, int *), i); 145 pc += i; 146 break; 147 148 case PCAN_USBPRO_SETBTR: 149 case PCAN_USBPRO_GETDEVID: 150 *pc++ = va_arg(ap, int); 151 pc += 2; 152 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32)); 153 pc += 4; 154 break; 155 156 case PCAN_USBPRO_SETFILTR: 157 case PCAN_USBPRO_SETBUSACT: 158 case PCAN_USBPRO_SETSILENT: 159 *pc++ = va_arg(ap, int); 160 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int)); 161 pc += 2; 162 break; 163 164 case PCAN_USBPRO_SETLED: 165 *pc++ = va_arg(ap, int); 166 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int)); 167 pc += 2; 168 *(__le32 *)pc = cpu_to_le32(va_arg(ap, u32)); 169 pc += 4; 170 break; 171 172 case PCAN_USBPRO_SETTS: 173 pc++; 174 *(__le16 *)pc = cpu_to_le16(va_arg(ap, int)); 175 pc += 2; 176 break; 177 178 default: 179 pr_err("%s: %s(): unknown data type %02Xh (%d)\n", 180 PCAN_USB_DRIVER_NAME, __func__, id, id); 181 pc--; 182 break; 183 } 184 185 len = pc - pm->rec_ptr; 186 if (len > 0) { 187 le32_add_cpu(pm->u.rec_cnt, 1); 188 *pm->rec_ptr = id; 189 190 pm->rec_ptr = pc; 191 pm->rec_buffer_len += len; 192 } 193 194 va_end(ap); 195 196 return len; 197 } 198 199 /* 200 * send PCAN-USB Pro command synchronously 201 */ 202 static int pcan_usb_pro_send_cmd(struct peak_usb_device *dev, 203 struct pcan_usb_pro_msg *pum) 204 { 205 int actual_length; 206 int err; 207 208 /* usb device unregistered? */ 209 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 210 return 0; 211 212 err = usb_bulk_msg(dev->udev, 213 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT), 214 pum->u.rec_buffer, pum->rec_buffer_len, 215 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT); 216 if (err) 217 netdev_err(dev->netdev, "sending command failure: %d\n", err); 218 219 return err; 220 } 221 222 /* 223 * wait for PCAN-USB Pro command response 224 */ 225 static int pcan_usb_pro_wait_rsp(struct peak_usb_device *dev, 226 struct pcan_usb_pro_msg *pum) 227 { 228 u8 req_data_type, req_channel; 229 int actual_length; 230 int i, err = 0; 231 232 /* usb device unregistered? */ 233 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 234 return 0; 235 236 req_data_type = pum->u.rec_buffer[4]; 237 req_channel = pum->u.rec_buffer[5]; 238 239 *pum->u.rec_cnt = 0; 240 for (i = 0; !err && i < PCAN_USBPRO_RSP_SUBMIT_MAX; i++) { 241 struct pcan_usb_pro_msg rsp; 242 union pcan_usb_pro_rec *pr; 243 u32 r, rec_cnt; 244 u16 rec_len; 245 u8 *pc; 246 247 err = usb_bulk_msg(dev->udev, 248 usb_rcvbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDIN), 249 pum->u.rec_buffer, pum->rec_buffer_len, 250 &actual_length, PCAN_USBPRO_COMMAND_TIMEOUT); 251 if (err) { 252 netdev_err(dev->netdev, "waiting rsp error %d\n", err); 253 break; 254 } 255 256 if (actual_length == 0) 257 continue; 258 259 err = -EBADMSG; 260 if (actual_length < PCAN_USBPRO_MSG_HEADER_LEN) { 261 netdev_err(dev->netdev, 262 "got abnormal too small rsp (len=%d)\n", 263 actual_length); 264 break; 265 } 266 267 pc = pcan_msg_init(&rsp, pum->u.rec_buffer, 268 actual_length); 269 270 rec_cnt = le32_to_cpu(*rsp.u.rec_cnt); 271 272 /* loop on records stored into message */ 273 for (r = 0; r < rec_cnt; r++) { 274 pr = (union pcan_usb_pro_rec *)pc; 275 rec_len = pcan_usb_pro_sizeof_rec[pr->data_type]; 276 if (!rec_len) { 277 netdev_err(dev->netdev, 278 "got unprocessed record in msg\n"); 279 pcan_dump_mem("rcvd rsp msg", pum->u.rec_buffer, 280 actual_length); 281 break; 282 } 283 284 /* check if response corresponds to request */ 285 if (pr->data_type != req_data_type) 286 netdev_err(dev->netdev, 287 "got unwanted rsp %xh: ignored\n", 288 pr->data_type); 289 290 /* check if channel in response corresponds too */ 291 else if ((req_channel != 0xff) && \ 292 (pr->bus_act.channel != req_channel)) 293 netdev_err(dev->netdev, 294 "got rsp %xh but on chan%u: ignored\n", 295 req_data_type, pr->bus_act.channel); 296 297 /* got the response */ 298 else 299 return 0; 300 301 /* otherwise, go on with next record in message */ 302 pc += rec_len; 303 } 304 } 305 306 return (i >= PCAN_USBPRO_RSP_SUBMIT_MAX) ? -ERANGE : err; 307 } 308 309 int pcan_usb_pro_send_req(struct peak_usb_device *dev, int req_id, 310 int req_value, void *req_addr, int req_size) 311 { 312 int err; 313 u8 req_type; 314 unsigned int p; 315 316 /* usb device unregistered? */ 317 if (!(dev->state & PCAN_USB_STATE_CONNECTED)) 318 return 0; 319 320 req_type = USB_TYPE_VENDOR | USB_RECIP_OTHER; 321 322 switch (req_id) { 323 case PCAN_USBPRO_REQ_FCT: 324 p = usb_sndctrlpipe(dev->udev, 0); 325 break; 326 327 default: 328 p = usb_rcvctrlpipe(dev->udev, 0); 329 req_type |= USB_DIR_IN; 330 memset(req_addr, '\0', req_size); 331 break; 332 } 333 334 err = usb_control_msg(dev->udev, p, req_id, req_type, req_value, 0, 335 req_addr, req_size, 2 * USB_CTRL_GET_TIMEOUT); 336 if (err < 0) { 337 netdev_info(dev->netdev, 338 "unable to request usb[type=%d value=%d] err=%d\n", 339 req_id, req_value, err); 340 return err; 341 } 342 343 return 0; 344 } 345 346 static int pcan_usb_pro_set_ts(struct peak_usb_device *dev, u16 onoff) 347 { 348 struct pcan_usb_pro_msg um; 349 350 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 351 pcan_msg_add_rec(&um, PCAN_USBPRO_SETTS, onoff); 352 353 return pcan_usb_pro_send_cmd(dev, &um); 354 } 355 356 static int pcan_usb_pro_set_bitrate(struct peak_usb_device *dev, u32 ccbt) 357 { 358 struct pcan_usb_pro_device *pdev = 359 container_of(dev, struct pcan_usb_pro_device, dev); 360 struct pcan_usb_pro_msg um; 361 362 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 363 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBTR, dev->ctrl_idx, ccbt); 364 365 /* cache the CCBT value to reuse it before next buson */ 366 pdev->cached_ccbt = ccbt; 367 368 return pcan_usb_pro_send_cmd(dev, &um); 369 } 370 371 static int pcan_usb_pro_set_bus(struct peak_usb_device *dev, u8 onoff) 372 { 373 struct pcan_usb_pro_msg um; 374 375 /* if bus=on, be sure the bitrate being set before! */ 376 if (onoff) { 377 struct pcan_usb_pro_device *pdev = 378 container_of(dev, struct pcan_usb_pro_device, dev); 379 380 pcan_usb_pro_set_bitrate(dev, pdev->cached_ccbt); 381 } 382 383 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 384 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, onoff); 385 386 return pcan_usb_pro_send_cmd(dev, &um); 387 } 388 389 static int pcan_usb_pro_set_silent(struct peak_usb_device *dev, u8 onoff) 390 { 391 struct pcan_usb_pro_msg um; 392 393 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 394 pcan_msg_add_rec(&um, PCAN_USBPRO_SETSILENT, dev->ctrl_idx, onoff); 395 396 return pcan_usb_pro_send_cmd(dev, &um); 397 } 398 399 static int pcan_usb_pro_set_filter(struct peak_usb_device *dev, u16 filter_mode) 400 { 401 struct pcan_usb_pro_msg um; 402 403 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 404 pcan_msg_add_rec(&um, PCAN_USBPRO_SETFILTR, dev->ctrl_idx, filter_mode); 405 406 return pcan_usb_pro_send_cmd(dev, &um); 407 } 408 409 static int pcan_usb_pro_set_led(struct peak_usb_device *dev, u8 mode, 410 u32 timeout) 411 { 412 struct pcan_usb_pro_msg um; 413 414 pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 415 pcan_msg_add_rec(&um, PCAN_USBPRO_SETLED, dev->ctrl_idx, mode, timeout); 416 417 return pcan_usb_pro_send_cmd(dev, &um); 418 } 419 420 static int pcan_usb_pro_get_device_id(struct peak_usb_device *dev, 421 u32 *device_id) 422 { 423 struct pcan_usb_pro_devid *pdn; 424 struct pcan_usb_pro_msg um; 425 int err; 426 u8 *pc; 427 428 pc = pcan_msg_init_empty(&um, dev->cmd_buf, PCAN_USB_MAX_CMD_LEN); 429 pcan_msg_add_rec(&um, PCAN_USBPRO_GETDEVID, dev->ctrl_idx); 430 431 err = pcan_usb_pro_send_cmd(dev, &um); 432 if (err) 433 return err; 434 435 err = pcan_usb_pro_wait_rsp(dev, &um); 436 if (err) 437 return err; 438 439 pdn = (struct pcan_usb_pro_devid *)pc; 440 if (device_id) 441 *device_id = le32_to_cpu(pdn->serial_num); 442 443 return err; 444 } 445 446 static int pcan_usb_pro_set_bittiming(struct peak_usb_device *dev, 447 struct can_bittiming *bt) 448 { 449 u32 ccbt; 450 451 ccbt = (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 0x00800000 : 0; 452 ccbt |= (bt->sjw - 1) << 24; 453 ccbt |= (bt->phase_seg2 - 1) << 20; 454 ccbt |= (bt->prop_seg + bt->phase_seg1 - 1) << 16; /* = tseg1 */ 455 ccbt |= bt->brp - 1; 456 457 netdev_info(dev->netdev, "setting ccbt=0x%08x\n", ccbt); 458 459 return pcan_usb_pro_set_bitrate(dev, ccbt); 460 } 461 462 void pcan_usb_pro_restart_complete(struct urb *urb) 463 { 464 /* can delete usb resources */ 465 peak_usb_async_complete(urb); 466 467 /* notify candev and netdev */ 468 peak_usb_restart_complete(urb->context); 469 } 470 471 /* 472 * handle restart but in asynchronously way 473 */ 474 static int pcan_usb_pro_restart_async(struct peak_usb_device *dev, 475 struct urb *urb, u8 *buf) 476 { 477 struct pcan_usb_pro_msg um; 478 479 pcan_msg_init_empty(&um, buf, PCAN_USB_MAX_CMD_LEN); 480 pcan_msg_add_rec(&um, PCAN_USBPRO_SETBUSACT, dev->ctrl_idx, 1); 481 482 usb_fill_bulk_urb(urb, dev->udev, 483 usb_sndbulkpipe(dev->udev, PCAN_USBPRO_EP_CMDOUT), 484 buf, PCAN_USB_MAX_CMD_LEN, 485 pcan_usb_pro_restart_complete, dev); 486 487 return usb_submit_urb(urb, GFP_ATOMIC); 488 } 489 490 static int pcan_usb_pro_drv_loaded(struct peak_usb_device *dev, int loaded) 491 { 492 u8 *buffer; 493 int err; 494 495 buffer = kzalloc(PCAN_USBPRO_FCT_DRVLD_REQ_LEN, GFP_KERNEL); 496 if (!buffer) 497 return -ENOMEM; 498 499 buffer[0] = 0; 500 buffer[1] = !!loaded; 501 502 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_FCT, 503 PCAN_USBPRO_FCT_DRVLD, buffer, 504 PCAN_USBPRO_FCT_DRVLD_REQ_LEN); 505 kfree(buffer); 506 507 return err; 508 } 509 510 static inline 511 struct pcan_usb_pro_interface *pcan_usb_pro_dev_if(struct peak_usb_device *dev) 512 { 513 struct pcan_usb_pro_device *pdev = 514 container_of(dev, struct pcan_usb_pro_device, dev); 515 return pdev->usb_if; 516 } 517 518 static int pcan_usb_pro_handle_canmsg(struct pcan_usb_pro_interface *usb_if, 519 struct pcan_usb_pro_rxmsg *rx) 520 { 521 const unsigned int ctrl_idx = (rx->len >> 4) & 0x0f; 522 struct peak_usb_device *dev = usb_if->dev[ctrl_idx]; 523 struct net_device *netdev = dev->netdev; 524 struct can_frame *can_frame; 525 struct sk_buff *skb; 526 struct skb_shared_hwtstamps *hwts; 527 528 skb = alloc_can_skb(netdev, &can_frame); 529 if (!skb) 530 return -ENOMEM; 531 532 can_frame->can_id = le32_to_cpu(rx->id); 533 can_frame->len = rx->len & 0x0f; 534 535 if (rx->flags & PCAN_USBPRO_EXT) 536 can_frame->can_id |= CAN_EFF_FLAG; 537 538 if (rx->flags & PCAN_USBPRO_RTR) 539 can_frame->can_id |= CAN_RTR_FLAG; 540 else 541 memcpy(can_frame->data, rx->data, can_frame->len); 542 543 hwts = skb_hwtstamps(skb); 544 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(rx->ts32), 545 &hwts->hwtstamp); 546 547 netdev->stats.rx_packets++; 548 netdev->stats.rx_bytes += can_frame->len; 549 netif_rx(skb); 550 551 return 0; 552 } 553 554 static int pcan_usb_pro_handle_error(struct pcan_usb_pro_interface *usb_if, 555 struct pcan_usb_pro_rxstatus *er) 556 { 557 const u16 raw_status = le16_to_cpu(er->status); 558 const unsigned int ctrl_idx = (er->channel >> 4) & 0x0f; 559 struct peak_usb_device *dev = usb_if->dev[ctrl_idx]; 560 struct net_device *netdev = dev->netdev; 561 struct can_frame *can_frame; 562 enum can_state new_state = CAN_STATE_ERROR_ACTIVE; 563 u8 err_mask = 0; 564 struct sk_buff *skb; 565 struct skb_shared_hwtstamps *hwts; 566 567 /* nothing should be sent while in BUS_OFF state */ 568 if (dev->can.state == CAN_STATE_BUS_OFF) 569 return 0; 570 571 if (!raw_status) { 572 /* no error bit (back to active state) */ 573 dev->can.state = CAN_STATE_ERROR_ACTIVE; 574 return 0; 575 } 576 577 if (raw_status & (PCAN_USBPRO_STATUS_OVERRUN | 578 PCAN_USBPRO_STATUS_QOVERRUN)) { 579 /* trick to bypass next comparison and process other errors */ 580 new_state = CAN_STATE_MAX; 581 } 582 583 if (raw_status & PCAN_USBPRO_STATUS_BUS) { 584 new_state = CAN_STATE_BUS_OFF; 585 } else if (raw_status & PCAN_USBPRO_STATUS_ERROR) { 586 u32 rx_err_cnt = (le32_to_cpu(er->err_frm) & 0x00ff0000) >> 16; 587 u32 tx_err_cnt = (le32_to_cpu(er->err_frm) & 0xff000000) >> 24; 588 589 if (rx_err_cnt > 127) 590 err_mask |= CAN_ERR_CRTL_RX_PASSIVE; 591 else if (rx_err_cnt > 96) 592 err_mask |= CAN_ERR_CRTL_RX_WARNING; 593 594 if (tx_err_cnt > 127) 595 err_mask |= CAN_ERR_CRTL_TX_PASSIVE; 596 else if (tx_err_cnt > 96) 597 err_mask |= CAN_ERR_CRTL_TX_WARNING; 598 599 if (err_mask & (CAN_ERR_CRTL_RX_WARNING | 600 CAN_ERR_CRTL_TX_WARNING)) 601 new_state = CAN_STATE_ERROR_WARNING; 602 else if (err_mask & (CAN_ERR_CRTL_RX_PASSIVE | 603 CAN_ERR_CRTL_TX_PASSIVE)) 604 new_state = CAN_STATE_ERROR_PASSIVE; 605 } 606 607 /* donot post any error if current state didn't change */ 608 if (dev->can.state == new_state) 609 return 0; 610 611 /* allocate an skb to store the error frame */ 612 skb = alloc_can_err_skb(netdev, &can_frame); 613 if (!skb) 614 return -ENOMEM; 615 616 switch (new_state) { 617 case CAN_STATE_BUS_OFF: 618 can_frame->can_id |= CAN_ERR_BUSOFF; 619 dev->can.can_stats.bus_off++; 620 can_bus_off(netdev); 621 break; 622 623 case CAN_STATE_ERROR_PASSIVE: 624 can_frame->can_id |= CAN_ERR_CRTL; 625 can_frame->data[1] |= err_mask; 626 dev->can.can_stats.error_passive++; 627 break; 628 629 case CAN_STATE_ERROR_WARNING: 630 can_frame->can_id |= CAN_ERR_CRTL; 631 can_frame->data[1] |= err_mask; 632 dev->can.can_stats.error_warning++; 633 break; 634 635 case CAN_STATE_ERROR_ACTIVE: 636 break; 637 638 default: 639 /* CAN_STATE_MAX (trick to handle other errors) */ 640 if (raw_status & PCAN_USBPRO_STATUS_OVERRUN) { 641 can_frame->can_id |= CAN_ERR_PROT; 642 can_frame->data[2] |= CAN_ERR_PROT_OVERLOAD; 643 netdev->stats.rx_over_errors++; 644 netdev->stats.rx_errors++; 645 } 646 647 if (raw_status & PCAN_USBPRO_STATUS_QOVERRUN) { 648 can_frame->can_id |= CAN_ERR_CRTL; 649 can_frame->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW; 650 netdev->stats.rx_over_errors++; 651 netdev->stats.rx_errors++; 652 } 653 654 new_state = CAN_STATE_ERROR_ACTIVE; 655 break; 656 } 657 658 dev->can.state = new_state; 659 660 hwts = skb_hwtstamps(skb); 661 peak_usb_get_ts_time(&usb_if->time_ref, le32_to_cpu(er->ts32), &hwts->hwtstamp); 662 netdev->stats.rx_packets++; 663 netdev->stats.rx_bytes += can_frame->len; 664 netif_rx(skb); 665 666 return 0; 667 } 668 669 static void pcan_usb_pro_handle_ts(struct pcan_usb_pro_interface *usb_if, 670 struct pcan_usb_pro_rxts *ts) 671 { 672 /* should wait until clock is stabilized */ 673 if (usb_if->cm_ignore_count > 0) 674 usb_if->cm_ignore_count--; 675 else 676 peak_usb_set_ts_now(&usb_if->time_ref, 677 le32_to_cpu(ts->ts64[1])); 678 } 679 680 /* 681 * callback for bulk IN urb 682 */ 683 static int pcan_usb_pro_decode_buf(struct peak_usb_device *dev, struct urb *urb) 684 { 685 struct pcan_usb_pro_interface *usb_if = pcan_usb_pro_dev_if(dev); 686 struct net_device *netdev = dev->netdev; 687 struct pcan_usb_pro_msg usb_msg; 688 u8 *rec_ptr, *msg_end; 689 u16 rec_cnt; 690 int err = 0; 691 692 rec_ptr = pcan_msg_init(&usb_msg, urb->transfer_buffer, 693 urb->actual_length); 694 if (!rec_ptr) { 695 netdev_err(netdev, "bad msg hdr len %d\n", urb->actual_length); 696 return -EINVAL; 697 } 698 699 /* loop reading all the records from the incoming message */ 700 msg_end = urb->transfer_buffer + urb->actual_length; 701 rec_cnt = le16_to_cpu(*usb_msg.u.rec_cnt_rd); 702 for (; rec_cnt > 0; rec_cnt--) { 703 union pcan_usb_pro_rec *pr = (union pcan_usb_pro_rec *)rec_ptr; 704 u16 sizeof_rec = pcan_usb_pro_sizeof_rec[pr->data_type]; 705 706 if (!sizeof_rec) { 707 netdev_err(netdev, 708 "got unsupported rec in usb msg:\n"); 709 err = -ENOTSUPP; 710 break; 711 } 712 713 /* check if the record goes out of current packet */ 714 if (rec_ptr + sizeof_rec > msg_end) { 715 netdev_err(netdev, 716 "got frag rec: should inc usb rx buf size\n"); 717 err = -EBADMSG; 718 break; 719 } 720 721 switch (pr->data_type) { 722 case PCAN_USBPRO_RXMSG8: 723 case PCAN_USBPRO_RXMSG4: 724 case PCAN_USBPRO_RXMSG0: 725 case PCAN_USBPRO_RXRTR: 726 err = pcan_usb_pro_handle_canmsg(usb_if, &pr->rx_msg); 727 if (err < 0) 728 goto fail; 729 break; 730 731 case PCAN_USBPRO_RXSTATUS: 732 err = pcan_usb_pro_handle_error(usb_if, &pr->rx_status); 733 if (err < 0) 734 goto fail; 735 break; 736 737 case PCAN_USBPRO_RXTS: 738 pcan_usb_pro_handle_ts(usb_if, &pr->rx_ts); 739 break; 740 741 default: 742 netdev_err(netdev, 743 "unhandled rec type 0x%02x (%d): ignored\n", 744 pr->data_type, pr->data_type); 745 break; 746 } 747 748 rec_ptr += sizeof_rec; 749 } 750 751 fail: 752 if (err) 753 pcan_dump_mem("received msg", 754 urb->transfer_buffer, urb->actual_length); 755 756 return err; 757 } 758 759 static int pcan_usb_pro_encode_msg(struct peak_usb_device *dev, 760 struct sk_buff *skb, u8 *obuf, size_t *size) 761 { 762 struct can_frame *cf = (struct can_frame *)skb->data; 763 u8 data_type, len, flags; 764 struct pcan_usb_pro_msg usb_msg; 765 766 pcan_msg_init_empty(&usb_msg, obuf, *size); 767 768 if ((cf->can_id & CAN_RTR_FLAG) || (cf->len == 0)) 769 data_type = PCAN_USBPRO_TXMSG0; 770 else if (cf->len <= 4) 771 data_type = PCAN_USBPRO_TXMSG4; 772 else 773 data_type = PCAN_USBPRO_TXMSG8; 774 775 len = (dev->ctrl_idx << 4) | (cf->len & 0x0f); 776 777 flags = 0; 778 if (cf->can_id & CAN_EFF_FLAG) 779 flags |= 0x02; 780 if (cf->can_id & CAN_RTR_FLAG) 781 flags |= 0x01; 782 783 pcan_msg_add_rec(&usb_msg, data_type, 0, flags, len, cf->can_id, 784 cf->data); 785 786 *size = usb_msg.rec_buffer_len; 787 788 return 0; 789 } 790 791 static int pcan_usb_pro_start(struct peak_usb_device *dev) 792 { 793 struct pcan_usb_pro_device *pdev = 794 container_of(dev, struct pcan_usb_pro_device, dev); 795 int err; 796 797 err = pcan_usb_pro_set_silent(dev, 798 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY); 799 if (err) 800 return err; 801 802 /* filter mode: 0-> All OFF; 1->bypass */ 803 err = pcan_usb_pro_set_filter(dev, 1); 804 if (err) 805 return err; 806 807 /* opening first device: */ 808 if (pdev->usb_if->dev_opened_count == 0) { 809 /* reset time_ref */ 810 peak_usb_init_time_ref(&pdev->usb_if->time_ref, &pcan_usb_pro); 811 812 /* ask device to send ts messages */ 813 err = pcan_usb_pro_set_ts(dev, 1); 814 } 815 816 pdev->usb_if->dev_opened_count++; 817 818 return err; 819 } 820 821 /* 822 * stop interface 823 * (last chance before set bus off) 824 */ 825 static int pcan_usb_pro_stop(struct peak_usb_device *dev) 826 { 827 struct pcan_usb_pro_device *pdev = 828 container_of(dev, struct pcan_usb_pro_device, dev); 829 830 /* turn off ts msgs for that interface if no other dev opened */ 831 if (pdev->usb_if->dev_opened_count == 1) 832 pcan_usb_pro_set_ts(dev, 0); 833 834 pdev->usb_if->dev_opened_count--; 835 836 return 0; 837 } 838 839 /* 840 * called when probing to initialize a device object. 841 */ 842 static int pcan_usb_pro_init(struct peak_usb_device *dev) 843 { 844 struct pcan_usb_pro_device *pdev = 845 container_of(dev, struct pcan_usb_pro_device, dev); 846 struct pcan_usb_pro_interface *usb_if = NULL; 847 struct pcan_usb_pro_fwinfo *fi = NULL; 848 struct pcan_usb_pro_blinfo *bi = NULL; 849 int err; 850 851 /* do this for 1st channel only */ 852 if (!dev->prev_siblings) { 853 /* allocate netdevices common structure attached to first one */ 854 usb_if = kzalloc(sizeof(struct pcan_usb_pro_interface), 855 GFP_KERNEL); 856 fi = kmalloc(sizeof(struct pcan_usb_pro_fwinfo), GFP_KERNEL); 857 bi = kmalloc(sizeof(struct pcan_usb_pro_blinfo), GFP_KERNEL); 858 if (!usb_if || !fi || !bi) { 859 err = -ENOMEM; 860 goto err_out; 861 } 862 863 /* number of ts msgs to ignore before taking one into account */ 864 usb_if->cm_ignore_count = 5; 865 866 /* 867 * explicit use of dev_xxx() instead of netdev_xxx() here: 868 * information displayed are related to the device itself, not 869 * to the canx netdevices. 870 */ 871 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO, 872 PCAN_USBPRO_INFO_FW, 873 fi, sizeof(*fi)); 874 if (err) { 875 dev_err(dev->netdev->dev.parent, 876 "unable to read %s firmware info (err %d)\n", 877 pcan_usb_pro.name, err); 878 goto err_out; 879 } 880 881 err = pcan_usb_pro_send_req(dev, PCAN_USBPRO_REQ_INFO, 882 PCAN_USBPRO_INFO_BL, 883 bi, sizeof(*bi)); 884 if (err) { 885 dev_err(dev->netdev->dev.parent, 886 "unable to read %s bootloader info (err %d)\n", 887 pcan_usb_pro.name, err); 888 goto err_out; 889 } 890 891 /* tell the device the can driver is running */ 892 err = pcan_usb_pro_drv_loaded(dev, 1); 893 if (err) 894 goto err_out; 895 896 dev_info(dev->netdev->dev.parent, 897 "PEAK-System %s hwrev %u serial %08X.%08X (%u channels)\n", 898 pcan_usb_pro.name, 899 bi->hw_rev, bi->serial_num_hi, bi->serial_num_lo, 900 pcan_usb_pro.ctrl_count); 901 } else { 902 usb_if = pcan_usb_pro_dev_if(dev->prev_siblings); 903 } 904 905 pdev->usb_if = usb_if; 906 usb_if->dev[dev->ctrl_idx] = dev; 907 908 /* set LED in default state (end of init phase) */ 909 pcan_usb_pro_set_led(dev, 0, 1); 910 911 kfree(bi); 912 kfree(fi); 913 914 return 0; 915 916 err_out: 917 kfree(bi); 918 kfree(fi); 919 kfree(usb_if); 920 921 return err; 922 } 923 924 static void pcan_usb_pro_exit(struct peak_usb_device *dev) 925 { 926 struct pcan_usb_pro_device *pdev = 927 container_of(dev, struct pcan_usb_pro_device, dev); 928 929 /* 930 * when rmmod called before unplug and if down, should reset things 931 * before leaving 932 */ 933 if (dev->can.state != CAN_STATE_STOPPED) { 934 /* set bus off on the corresponding channel */ 935 pcan_usb_pro_set_bus(dev, 0); 936 } 937 938 /* if channel #0 (only) */ 939 if (dev->ctrl_idx == 0) { 940 /* turn off calibration message if any device were opened */ 941 if (pdev->usb_if->dev_opened_count > 0) 942 pcan_usb_pro_set_ts(dev, 0); 943 944 /* tell the PCAN-USB Pro device the driver is being unloaded */ 945 pcan_usb_pro_drv_loaded(dev, 0); 946 } 947 } 948 949 /* 950 * called when PCAN-USB Pro adapter is unplugged 951 */ 952 static void pcan_usb_pro_free(struct peak_usb_device *dev) 953 { 954 /* last device: can free pcan_usb_pro_interface object now */ 955 if (!dev->prev_siblings && !dev->next_siblings) 956 kfree(pcan_usb_pro_dev_if(dev)); 957 } 958 959 /* 960 * probe function for new PCAN-USB Pro usb interface 961 */ 962 int pcan_usb_pro_probe(struct usb_interface *intf) 963 { 964 struct usb_host_interface *if_desc; 965 int i; 966 967 if_desc = intf->altsetting; 968 969 /* check interface endpoint addresses */ 970 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) { 971 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc; 972 973 /* 974 * below is the list of valid ep addresses. Any other ep address 975 * is considered as not-CAN interface address => no dev created 976 */ 977 switch (ep->bEndpointAddress) { 978 case PCAN_USBPRO_EP_CMDOUT: 979 case PCAN_USBPRO_EP_CMDIN: 980 case PCAN_USBPRO_EP_MSGOUT_0: 981 case PCAN_USBPRO_EP_MSGOUT_1: 982 case PCAN_USBPRO_EP_MSGIN: 983 case PCAN_USBPRO_EP_UNUSED: 984 break; 985 default: 986 return -ENODEV; 987 } 988 } 989 990 return 0; 991 } 992 993 /* 994 * describe the PCAN-USB Pro adapter 995 */ 996 static const struct can_bittiming_const pcan_usb_pro_const = { 997 .name = "pcan_usb_pro", 998 .tseg1_min = 1, 999 .tseg1_max = 16, 1000 .tseg2_min = 1, 1001 .tseg2_max = 8, 1002 .sjw_max = 4, 1003 .brp_min = 1, 1004 .brp_max = 1024, 1005 .brp_inc = 1, 1006 }; 1007 1008 const struct peak_usb_adapter pcan_usb_pro = { 1009 .name = "PCAN-USB Pro", 1010 .device_id = PCAN_USBPRO_PRODUCT_ID, 1011 .ctrl_count = PCAN_USBPRO_CHANNEL_COUNT, 1012 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY, 1013 .clock = { 1014 .freq = PCAN_USBPRO_CRYSTAL_HZ, 1015 }, 1016 .bittiming_const = &pcan_usb_pro_const, 1017 1018 /* size of device private data */ 1019 .sizeof_dev_private = sizeof(struct pcan_usb_pro_device), 1020 1021 /* timestamps usage */ 1022 .ts_used_bits = 32, 1023 .ts_period = 1000000, /* calibration period in ts. */ 1024 .us_per_ts_scale = 1, /* us = (ts * scale) >> shift */ 1025 .us_per_ts_shift = 0, 1026 1027 /* give here messages in/out endpoints */ 1028 .ep_msg_in = PCAN_USBPRO_EP_MSGIN, 1029 .ep_msg_out = {PCAN_USBPRO_EP_MSGOUT_0, PCAN_USBPRO_EP_MSGOUT_1}, 1030 1031 /* size of rx/tx usb buffers */ 1032 .rx_buffer_size = PCAN_USBPRO_RX_BUFFER_SIZE, 1033 .tx_buffer_size = PCAN_USBPRO_TX_BUFFER_SIZE, 1034 1035 /* device callbacks */ 1036 .intf_probe = pcan_usb_pro_probe, 1037 .dev_init = pcan_usb_pro_init, 1038 .dev_exit = pcan_usb_pro_exit, 1039 .dev_free = pcan_usb_pro_free, 1040 .dev_set_bus = pcan_usb_pro_set_bus, 1041 .dev_set_bittiming = pcan_usb_pro_set_bittiming, 1042 .dev_get_device_id = pcan_usb_pro_get_device_id, 1043 .dev_decode_buf = pcan_usb_pro_decode_buf, 1044 .dev_encode_msg = pcan_usb_pro_encode_msg, 1045 .dev_start = pcan_usb_pro_start, 1046 .dev_stop = pcan_usb_pro_stop, 1047 .dev_restart_async = pcan_usb_pro_restart_async, 1048 }; 1049