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