0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <asm/unaligned.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/usb.h>
0014 #include <linux/module.h>
0015 #include <linux/ethtool.h>
0016
0017 #include <linux/can.h>
0018 #include <linux/can/dev.h>
0019 #include <linux/can/error.h>
0020
0021 #include "pcan_usb_core.h"
0022
0023
0024 #define PCAN_USB_EP_CMDOUT 1
0025 #define PCAN_USB_EP_CMDIN (PCAN_USB_EP_CMDOUT | USB_DIR_IN)
0026 #define PCAN_USB_EP_MSGOUT 2
0027 #define PCAN_USB_EP_MSGIN (PCAN_USB_EP_MSGOUT | USB_DIR_IN)
0028
0029
0030 #define PCAN_USB_CMD_FUNC 0
0031 #define PCAN_USB_CMD_NUM 1
0032 #define PCAN_USB_CMD_ARGS 2
0033 #define PCAN_USB_CMD_ARGS_LEN 14
0034 #define PCAN_USB_CMD_LEN (PCAN_USB_CMD_ARGS + \
0035 PCAN_USB_CMD_ARGS_LEN)
0036
0037
0038 #define PCAN_USB_CMD_BITRATE 1
0039 #define PCAN_USB_CMD_SET_BUS 3
0040 #define PCAN_USB_CMD_DEVID 4
0041 #define PCAN_USB_CMD_SN 6
0042 #define PCAN_USB_CMD_REGISTER 9
0043 #define PCAN_USB_CMD_EXT_VCC 10
0044 #define PCAN_USB_CMD_ERR_FR 11
0045 #define PCAN_USB_CMD_LED 12
0046
0047
0048 #define PCAN_USB_BUS_XCVER 2
0049 #define PCAN_USB_BUS_SILENT_MODE 3
0050
0051
0052 #define PCAN_USB_GET 1
0053 #define PCAN_USB_SET 2
0054
0055
0056 #define PCAN_USB_COMMAND_TIMEOUT 1000
0057
0058
0059 #define PCAN_USB_STARTUP_TIMEOUT 10
0060
0061
0062 #define PCAN_USB_RX_BUFFER_SIZE 64
0063 #define PCAN_USB_TX_BUFFER_SIZE 64
0064
0065 #define PCAN_USB_MSG_HEADER_LEN 2
0066
0067 #define PCAN_USB_MSG_TX_CAN 2
0068
0069
0070 #define PCAN_USB_CRYSTAL_HZ 16000000
0071
0072
0073 #define PCAN_USB_STATUSLEN_TIMESTAMP (1 << 7)
0074 #define PCAN_USB_STATUSLEN_INTERNAL (1 << 6)
0075 #define PCAN_USB_STATUSLEN_EXT_ID (1 << 5)
0076 #define PCAN_USB_STATUSLEN_RTR (1 << 4)
0077 #define PCAN_USB_STATUSLEN_DLC (0xf)
0078
0079
0080 #define PCAN_USB_TX_SRR 0x01
0081 #define PCAN_USB_TX_AT 0x02
0082
0083
0084 #define PCAN_USB_ERROR_TXFULL 0x01
0085 #define PCAN_USB_ERROR_RXQOVR 0x02
0086 #define PCAN_USB_ERROR_BUS_LIGHT 0x04
0087 #define PCAN_USB_ERROR_BUS_HEAVY 0x08
0088 #define PCAN_USB_ERROR_BUS_OFF 0x10
0089 #define PCAN_USB_ERROR_RXQEMPTY 0x20
0090 #define PCAN_USB_ERROR_QOVR 0x40
0091 #define PCAN_USB_ERROR_TXQFULL 0x80
0092
0093 #define PCAN_USB_ERROR_BUS (PCAN_USB_ERROR_BUS_LIGHT | \
0094 PCAN_USB_ERROR_BUS_HEAVY | \
0095 PCAN_USB_ERROR_BUS_OFF)
0096
0097
0098 #define SJA1000_MODE_NORMAL 0x00
0099 #define SJA1000_MODE_INIT 0x01
0100
0101
0102
0103
0104
0105
0106 #define PCAN_USB_TS_DIV_SHIFTER 20
0107 #define PCAN_USB_TS_US_PER_TICK 44739243
0108
0109
0110 #define PCAN_USB_REC_ERROR 1
0111 #define PCAN_USB_REC_ANALOG 2
0112 #define PCAN_USB_REC_BUSLOAD 3
0113 #define PCAN_USB_REC_TS 4
0114 #define PCAN_USB_REC_BUSEVT 5
0115
0116
0117 #define PCAN_USB_ERR_RXERR 0x02
0118 #define PCAN_USB_ERR_TXERR 0x04
0119
0120
0121
0122
0123
0124 #define PCAN_USB_BERR_MASK (PCAN_USB_ERR_RXERR | PCAN_USB_ERR_TXERR)
0125
0126
0127 #define PCAN_USB_ERR_CNT_DEC 0x00
0128 #define PCAN_USB_ERR_CNT_INC 0x80
0129
0130
0131 struct pcan_usb {
0132 struct peak_usb_device dev;
0133 struct peak_time_ref time_ref;
0134 struct timer_list restart_timer;
0135 struct can_berr_counter bec;
0136 };
0137
0138
0139 struct pcan_usb_msg_context {
0140 u16 ts16;
0141 u8 prev_ts8;
0142 u8 *ptr;
0143 u8 *end;
0144 u8 rec_cnt;
0145 u8 rec_idx;
0146 u8 rec_ts_idx;
0147 struct net_device *netdev;
0148 struct pcan_usb *pdev;
0149 };
0150
0151
0152
0153
0154 static int pcan_usb_send_cmd(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
0155 {
0156 int err;
0157 int actual_length;
0158
0159
0160 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
0161 return 0;
0162
0163 dev->cmd_buf[PCAN_USB_CMD_FUNC] = f;
0164 dev->cmd_buf[PCAN_USB_CMD_NUM] = n;
0165
0166 if (p)
0167 memcpy(dev->cmd_buf + PCAN_USB_CMD_ARGS,
0168 p, PCAN_USB_CMD_ARGS_LEN);
0169
0170 err = usb_bulk_msg(dev->udev,
0171 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
0172 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
0173 PCAN_USB_COMMAND_TIMEOUT);
0174 if (err)
0175 netdev_err(dev->netdev,
0176 "sending cmd f=0x%x n=0x%x failure: %d\n",
0177 f, n, err);
0178 return err;
0179 }
0180
0181
0182
0183
0184 static int pcan_usb_wait_rsp(struct peak_usb_device *dev, u8 f, u8 n, u8 *p)
0185 {
0186 int err;
0187 int actual_length;
0188
0189
0190 if (!(dev->state & PCAN_USB_STATE_CONNECTED))
0191 return 0;
0192
0193
0194 err = pcan_usb_send_cmd(dev, f, n, NULL);
0195 if (err)
0196 return err;
0197
0198 err = usb_bulk_msg(dev->udev,
0199 usb_rcvbulkpipe(dev->udev, PCAN_USB_EP_CMDIN),
0200 dev->cmd_buf, PCAN_USB_CMD_LEN, &actual_length,
0201 PCAN_USB_COMMAND_TIMEOUT);
0202 if (err)
0203 netdev_err(dev->netdev,
0204 "waiting rsp f=0x%x n=0x%x failure: %d\n", f, n, err);
0205 else if (p)
0206 memcpy(p, dev->cmd_buf + PCAN_USB_CMD_ARGS,
0207 PCAN_USB_CMD_ARGS_LEN);
0208
0209 return err;
0210 }
0211
0212 static int pcan_usb_set_sja1000(struct peak_usb_device *dev, u8 mode)
0213 {
0214 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0215 [1] = mode,
0216 };
0217
0218 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_REGISTER, PCAN_USB_SET,
0219 args);
0220 }
0221
0222 static int pcan_usb_set_bus(struct peak_usb_device *dev, u8 onoff)
0223 {
0224 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0225 [0] = !!onoff,
0226 };
0227
0228 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS, PCAN_USB_BUS_XCVER,
0229 args);
0230 }
0231
0232 static int pcan_usb_set_silent(struct peak_usb_device *dev, u8 onoff)
0233 {
0234 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0235 [0] = !!onoff,
0236 };
0237
0238 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_SET_BUS,
0239 PCAN_USB_BUS_SILENT_MODE, args);
0240 }
0241
0242
0243 static int pcan_usb_set_err_frame(struct peak_usb_device *dev, u8 err_mask)
0244 {
0245 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0246 [0] = err_mask,
0247 };
0248
0249 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_ERR_FR, PCAN_USB_SET, args);
0250 }
0251
0252 static int pcan_usb_set_ext_vcc(struct peak_usb_device *dev, u8 onoff)
0253 {
0254 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0255 [0] = !!onoff,
0256 };
0257
0258 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_EXT_VCC, PCAN_USB_SET, args);
0259 }
0260
0261 static int pcan_usb_set_led(struct peak_usb_device *dev, u8 onoff)
0262 {
0263 u8 args[PCAN_USB_CMD_ARGS_LEN] = {
0264 [0] = !!onoff,
0265 };
0266
0267 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_LED, PCAN_USB_SET, args);
0268 }
0269
0270
0271
0272
0273 static int pcan_usb_set_bittiming(struct peak_usb_device *dev,
0274 struct can_bittiming *bt)
0275 {
0276 u8 args[PCAN_USB_CMD_ARGS_LEN];
0277 u8 btr0, btr1;
0278
0279 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
0280 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
0281 (((bt->phase_seg2 - 1) & 0x7) << 4);
0282 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
0283 btr1 |= 0x80;
0284
0285 netdev_info(dev->netdev, "setting BTR0=0x%02x BTR1=0x%02x\n",
0286 btr0, btr1);
0287
0288 args[0] = btr1;
0289 args[1] = btr0;
0290
0291 return pcan_usb_send_cmd(dev, PCAN_USB_CMD_BITRATE, PCAN_USB_SET, args);
0292 }
0293
0294
0295
0296
0297 static int pcan_usb_write_mode(struct peak_usb_device *dev, u8 onoff)
0298 {
0299 int err;
0300
0301 err = pcan_usb_set_bus(dev, onoff);
0302 if (err)
0303 return err;
0304
0305 if (!onoff) {
0306 err = pcan_usb_set_sja1000(dev, SJA1000_MODE_INIT);
0307 } else {
0308
0309 set_current_state(TASK_INTERRUPTIBLE);
0310 schedule_timeout(msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
0311 }
0312
0313 return err;
0314 }
0315
0316
0317
0318
0319 static void pcan_usb_restart(struct timer_list *t)
0320 {
0321 struct pcan_usb *pdev = from_timer(pdev, t, restart_timer);
0322 struct peak_usb_device *dev = &pdev->dev;
0323
0324
0325 peak_usb_restart_complete(dev);
0326 }
0327
0328
0329
0330
0331 static void pcan_usb_restart_pending(struct urb *urb)
0332 {
0333 struct pcan_usb *pdev = urb->context;
0334
0335
0336 mod_timer(&pdev->restart_timer,
0337 jiffies + msecs_to_jiffies(PCAN_USB_STARTUP_TIMEOUT));
0338
0339
0340 peak_usb_async_complete(urb);
0341 }
0342
0343
0344
0345
0346 static int pcan_usb_restart_async(struct peak_usb_device *dev, struct urb *urb,
0347 u8 *buf)
0348 {
0349 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
0350
0351 if (timer_pending(&pdev->restart_timer))
0352 return -EBUSY;
0353
0354
0355 buf[PCAN_USB_CMD_FUNC] = 3;
0356 buf[PCAN_USB_CMD_NUM] = 2;
0357 buf[PCAN_USB_CMD_ARGS] = 1;
0358
0359 usb_fill_bulk_urb(urb, dev->udev,
0360 usb_sndbulkpipe(dev->udev, PCAN_USB_EP_CMDOUT),
0361 buf, PCAN_USB_CMD_LEN,
0362 pcan_usb_restart_pending, pdev);
0363
0364 return usb_submit_urb(urb, GFP_ATOMIC);
0365 }
0366
0367
0368
0369
0370 static int pcan_usb_get_serial(struct peak_usb_device *dev, u32 *serial_number)
0371 {
0372 u8 args[PCAN_USB_CMD_ARGS_LEN];
0373 int err;
0374
0375 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_SN, PCAN_USB_GET, args);
0376 if (err)
0377 return err;
0378 *serial_number = le32_to_cpup((__le32 *)args);
0379
0380 return 0;
0381 }
0382
0383
0384
0385
0386 static int pcan_usb_get_device_id(struct peak_usb_device *dev, u32 *device_id)
0387 {
0388 u8 args[PCAN_USB_CMD_ARGS_LEN];
0389 int err;
0390
0391 err = pcan_usb_wait_rsp(dev, PCAN_USB_CMD_DEVID, PCAN_USB_GET, args);
0392 if (err)
0393 netdev_err(dev->netdev, "getting device id failure: %d\n", err);
0394
0395 else
0396 *device_id = args[0];
0397
0398 return err;
0399 }
0400
0401
0402
0403
0404 static int pcan_usb_update_ts(struct pcan_usb_msg_context *mc)
0405 {
0406 if ((mc->ptr + 2) > mc->end)
0407 return -EINVAL;
0408
0409 mc->ts16 = get_unaligned_le16(mc->ptr);
0410
0411 if (mc->rec_idx > 0)
0412 peak_usb_update_ts_now(&mc->pdev->time_ref, mc->ts16);
0413 else
0414 peak_usb_set_ts_now(&mc->pdev->time_ref, mc->ts16);
0415
0416 return 0;
0417 }
0418
0419
0420
0421
0422 static int pcan_usb_decode_ts(struct pcan_usb_msg_context *mc, u8 first_packet)
0423 {
0424
0425 if (first_packet) {
0426 if ((mc->ptr + 2) > mc->end)
0427 return -EINVAL;
0428
0429 mc->ts16 = get_unaligned_le16(mc->ptr);
0430 mc->prev_ts8 = mc->ts16 & 0x00ff;
0431
0432 mc->ptr += 2;
0433 } else {
0434 u8 ts8;
0435
0436 if ((mc->ptr + 1) > mc->end)
0437 return -EINVAL;
0438
0439 ts8 = *mc->ptr++;
0440
0441 if (ts8 < mc->prev_ts8)
0442 mc->ts16 += 0x100;
0443
0444 mc->ts16 &= 0xff00;
0445 mc->ts16 |= ts8;
0446 mc->prev_ts8 = ts8;
0447 }
0448
0449 return 0;
0450 }
0451
0452 static int pcan_usb_decode_error(struct pcan_usb_msg_context *mc, u8 n,
0453 u8 status_len)
0454 {
0455 struct sk_buff *skb;
0456 struct can_frame *cf;
0457 enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
0458
0459
0460 if (n == PCAN_USB_ERROR_QOVR)
0461 if (!mc->pdev->time_ref.tick_count)
0462 return 0;
0463
0464
0465 skb = alloc_can_err_skb(mc->netdev, &cf);
0466
0467 if (n & PCAN_USB_ERROR_RXQOVR) {
0468
0469 netdev_dbg(mc->netdev, "data overrun interrupt\n");
0470 mc->netdev->stats.rx_over_errors++;
0471 mc->netdev->stats.rx_errors++;
0472 if (cf) {
0473 cf->can_id |= CAN_ERR_CRTL;
0474 cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
0475 }
0476 }
0477
0478 if (n & PCAN_USB_ERROR_TXQFULL)
0479 netdev_dbg(mc->netdev, "device Tx queue full)\n");
0480
0481 if (n & PCAN_USB_ERROR_BUS_OFF) {
0482 new_state = CAN_STATE_BUS_OFF;
0483 } else if (n & PCAN_USB_ERROR_BUS_HEAVY) {
0484 new_state = ((mc->pdev->bec.txerr >= 128) ||
0485 (mc->pdev->bec.rxerr >= 128)) ?
0486 CAN_STATE_ERROR_PASSIVE :
0487 CAN_STATE_ERROR_WARNING;
0488 } else {
0489 new_state = CAN_STATE_ERROR_ACTIVE;
0490 }
0491
0492
0493 if (new_state != mc->pdev->dev.can.state) {
0494 enum can_state tx_state =
0495 (mc->pdev->bec.txerr >= mc->pdev->bec.rxerr) ?
0496 new_state : 0;
0497 enum can_state rx_state =
0498 (mc->pdev->bec.txerr <= mc->pdev->bec.rxerr) ?
0499 new_state : 0;
0500
0501 can_change_state(mc->netdev, cf, tx_state, rx_state);
0502
0503 if (new_state == CAN_STATE_BUS_OFF) {
0504 can_bus_off(mc->netdev);
0505 } else if (cf && (cf->can_id & CAN_ERR_CRTL)) {
0506
0507
0508
0509 cf->can_id = CAN_ERR_CNT;
0510 cf->data[6] = mc->pdev->bec.txerr;
0511 cf->data[7] = mc->pdev->bec.rxerr;
0512 }
0513 }
0514
0515 if (!skb)
0516 return -ENOMEM;
0517
0518 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
0519 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
0520
0521 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16,
0522 &hwts->hwtstamp);
0523 }
0524
0525 netif_rx(skb);
0526
0527 return 0;
0528 }
0529
0530
0531
0532
0533 static int pcan_usb_handle_bus_evt(struct pcan_usb_msg_context *mc, u8 ir)
0534 {
0535 struct pcan_usb *pdev = mc->pdev;
0536
0537
0538 switch (ir) {
0539 case PCAN_USB_ERR_CNT_DEC:
0540 case PCAN_USB_ERR_CNT_INC:
0541
0542
0543 pdev->bec.rxerr = mc->ptr[1];
0544 pdev->bec.txerr = mc->ptr[2];
0545 break;
0546
0547 default:
0548
0549 break;
0550 }
0551
0552 return 0;
0553 }
0554
0555
0556
0557
0558 static int pcan_usb_decode_status(struct pcan_usb_msg_context *mc,
0559 u8 status_len)
0560 {
0561 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
0562 u8 f, n;
0563 int err;
0564
0565
0566 if ((mc->ptr + 2) > mc->end)
0567 return -EINVAL;
0568
0569 f = mc->ptr[PCAN_USB_CMD_FUNC];
0570 n = mc->ptr[PCAN_USB_CMD_NUM];
0571 mc->ptr += PCAN_USB_CMD_ARGS;
0572
0573 if (status_len & PCAN_USB_STATUSLEN_TIMESTAMP) {
0574 int err = pcan_usb_decode_ts(mc, !mc->rec_ts_idx);
0575
0576 if (err)
0577 return err;
0578
0579
0580
0581
0582 mc->rec_ts_idx++;
0583 }
0584
0585 switch (f) {
0586 case PCAN_USB_REC_ERROR:
0587 err = pcan_usb_decode_error(mc, n, status_len);
0588 if (err)
0589 return err;
0590 break;
0591
0592 case PCAN_USB_REC_ANALOG:
0593
0594 rec_len = 2;
0595 break;
0596
0597 case PCAN_USB_REC_BUSLOAD:
0598
0599 rec_len = 1;
0600 break;
0601
0602 case PCAN_USB_REC_TS:
0603
0604 if (pcan_usb_update_ts(mc))
0605 return -EINVAL;
0606 break;
0607
0608 case PCAN_USB_REC_BUSEVT:
0609
0610 err = pcan_usb_handle_bus_evt(mc, n);
0611 if (err)
0612 return err;
0613 break;
0614 default:
0615 netdev_err(mc->netdev, "unexpected function %u\n", f);
0616 break;
0617 }
0618
0619 if ((mc->ptr + rec_len) > mc->end)
0620 return -EINVAL;
0621
0622 mc->ptr += rec_len;
0623
0624 return 0;
0625 }
0626
0627
0628
0629
0630 static int pcan_usb_decode_data(struct pcan_usb_msg_context *mc, u8 status_len)
0631 {
0632 u8 rec_len = status_len & PCAN_USB_STATUSLEN_DLC;
0633 struct sk_buff *skb;
0634 struct can_frame *cf;
0635 struct skb_shared_hwtstamps *hwts;
0636 u32 can_id_flags;
0637
0638 skb = alloc_can_skb(mc->netdev, &cf);
0639 if (!skb)
0640 return -ENOMEM;
0641
0642 if (status_len & PCAN_USB_STATUSLEN_EXT_ID) {
0643 if ((mc->ptr + 4) > mc->end)
0644 goto decode_failed;
0645
0646 can_id_flags = get_unaligned_le32(mc->ptr);
0647 cf->can_id = can_id_flags >> 3 | CAN_EFF_FLAG;
0648 mc->ptr += 4;
0649 } else {
0650 if ((mc->ptr + 2) > mc->end)
0651 goto decode_failed;
0652
0653 can_id_flags = get_unaligned_le16(mc->ptr);
0654 cf->can_id = can_id_flags >> 5;
0655 mc->ptr += 2;
0656 }
0657
0658 can_frame_set_cc_len(cf, rec_len, mc->pdev->dev.can.ctrlmode);
0659
0660
0661 if (pcan_usb_decode_ts(mc, !mc->rec_ts_idx))
0662 goto decode_failed;
0663
0664
0665 mc->rec_ts_idx++;
0666
0667
0668 memset(cf->data, 0x0, sizeof(cf->data));
0669 if (status_len & PCAN_USB_STATUSLEN_RTR) {
0670 cf->can_id |= CAN_RTR_FLAG;
0671 } else {
0672 if ((mc->ptr + rec_len) > mc->end)
0673 goto decode_failed;
0674
0675 memcpy(cf->data, mc->ptr, cf->len);
0676 mc->ptr += rec_len;
0677
0678
0679 if (can_id_flags & PCAN_USB_TX_SRR)
0680 mc->ptr++;
0681
0682
0683 mc->netdev->stats.rx_bytes += cf->len;
0684 }
0685 mc->netdev->stats.rx_packets++;
0686
0687
0688 hwts = skb_hwtstamps(skb);
0689 peak_usb_get_ts_time(&mc->pdev->time_ref, mc->ts16, &hwts->hwtstamp);
0690
0691
0692 netif_rx(skb);
0693
0694 return 0;
0695
0696 decode_failed:
0697 dev_kfree_skb(skb);
0698 return -EINVAL;
0699 }
0700
0701
0702
0703
0704 static int pcan_usb_decode_msg(struct peak_usb_device *dev, u8 *ibuf, u32 lbuf)
0705 {
0706 struct pcan_usb_msg_context mc = {
0707 .rec_cnt = ibuf[1],
0708 .ptr = ibuf + PCAN_USB_MSG_HEADER_LEN,
0709 .end = ibuf + lbuf,
0710 .netdev = dev->netdev,
0711 .pdev = container_of(dev, struct pcan_usb, dev),
0712 };
0713 int err;
0714
0715 for (err = 0; mc.rec_idx < mc.rec_cnt && !err; mc.rec_idx++) {
0716 u8 sl = *mc.ptr++;
0717
0718
0719 if (sl & PCAN_USB_STATUSLEN_INTERNAL) {
0720 err = pcan_usb_decode_status(&mc, sl);
0721
0722 } else {
0723 err = pcan_usb_decode_data(&mc, sl);
0724 }
0725 }
0726
0727 return err;
0728 }
0729
0730
0731
0732
0733 static int pcan_usb_decode_buf(struct peak_usb_device *dev, struct urb *urb)
0734 {
0735 int err = 0;
0736
0737 if (urb->actual_length > PCAN_USB_MSG_HEADER_LEN) {
0738 err = pcan_usb_decode_msg(dev, urb->transfer_buffer,
0739 urb->actual_length);
0740
0741 } else if (urb->actual_length > 0) {
0742 netdev_err(dev->netdev, "usb message length error (%u)\n",
0743 urb->actual_length);
0744 err = -EINVAL;
0745 }
0746
0747 return err;
0748 }
0749
0750
0751
0752
0753 static int pcan_usb_encode_msg(struct peak_usb_device *dev, struct sk_buff *skb,
0754 u8 *obuf, size_t *size)
0755 {
0756 struct net_device *netdev = dev->netdev;
0757 struct net_device_stats *stats = &netdev->stats;
0758 struct can_frame *cf = (struct can_frame *)skb->data;
0759 u32 can_id_flags = cf->can_id & CAN_ERR_MASK;
0760 u8 *pc;
0761
0762 obuf[0] = PCAN_USB_MSG_TX_CAN;
0763 obuf[1] = 1;
0764
0765 pc = obuf + PCAN_USB_MSG_HEADER_LEN;
0766
0767
0768 *pc = can_get_cc_dlc(cf, dev->can.ctrlmode);
0769
0770 if (cf->can_id & CAN_RTR_FLAG)
0771 *pc |= PCAN_USB_STATUSLEN_RTR;
0772
0773
0774 if (cf->can_id & CAN_EFF_FLAG) {
0775 *pc |= PCAN_USB_STATUSLEN_EXT_ID;
0776 pc++;
0777
0778 can_id_flags <<= 3;
0779
0780 if (dev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
0781 can_id_flags |= PCAN_USB_TX_SRR;
0782
0783 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
0784 can_id_flags |= PCAN_USB_TX_AT;
0785
0786 put_unaligned_le32(can_id_flags, pc);
0787 pc += 4;
0788 } else {
0789 pc++;
0790
0791 can_id_flags <<= 5;
0792
0793 if (dev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
0794 can_id_flags |= PCAN_USB_TX_SRR;
0795
0796 if (dev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
0797 can_id_flags |= PCAN_USB_TX_AT;
0798
0799 put_unaligned_le16(can_id_flags, pc);
0800 pc += 2;
0801 }
0802
0803
0804 if (!(cf->can_id & CAN_RTR_FLAG)) {
0805 memcpy(pc, cf->data, cf->len);
0806 pc += cf->len;
0807 }
0808
0809
0810 if (can_id_flags & PCAN_USB_TX_SRR)
0811 *pc++ = 0x80;
0812
0813 obuf[(*size)-1] = (u8)(stats->tx_packets & 0xff);
0814
0815 return 0;
0816 }
0817
0818
0819 static int pcan_usb_get_berr_counter(const struct net_device *netdev,
0820 struct can_berr_counter *bec)
0821 {
0822 struct peak_usb_device *dev = netdev_priv(netdev);
0823 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
0824
0825 *bec = pdev->bec;
0826
0827
0828 return 0;
0829 }
0830
0831
0832
0833
0834 static int pcan_usb_start(struct peak_usb_device *dev)
0835 {
0836 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
0837 int err;
0838
0839
0840 peak_usb_init_time_ref(&pdev->time_ref, &pcan_usb);
0841
0842 pdev->bec.rxerr = 0;
0843 pdev->bec.txerr = 0;
0844
0845
0846
0847
0848 err = pcan_usb_set_err_frame(dev, PCAN_USB_BERR_MASK);
0849 if (err)
0850 netdev_warn(dev->netdev,
0851 "Asking for BERR reporting error %u\n",
0852 err);
0853
0854
0855 if (dev->device_rev > 3) {
0856 err = pcan_usb_set_silent(dev,
0857 dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY);
0858 if (err)
0859 return err;
0860 }
0861
0862 return pcan_usb_set_ext_vcc(dev, 0);
0863 }
0864
0865 static int pcan_usb_init(struct peak_usb_device *dev)
0866 {
0867 struct pcan_usb *pdev = container_of(dev, struct pcan_usb, dev);
0868 u32 serial_number;
0869 int err;
0870
0871
0872 timer_setup(&pdev->restart_timer, pcan_usb_restart, 0);
0873
0874
0875
0876
0877
0878
0879 err = pcan_usb_get_serial(dev, &serial_number);
0880 if (err) {
0881 dev_err(dev->netdev->dev.parent,
0882 "unable to read %s serial number (err %d)\n",
0883 pcan_usb.name, err);
0884 return err;
0885 }
0886
0887 dev_info(dev->netdev->dev.parent,
0888 "PEAK-System %s adapter hwrev %u serial %08X (%u channel)\n",
0889 pcan_usb.name, dev->device_rev, serial_number,
0890 pcan_usb.ctrl_count);
0891
0892
0893
0894
0895 if (dev->device_rev >= 41) {
0896 struct can_priv *priv = netdev_priv(dev->netdev);
0897
0898 priv->ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT |
0899 CAN_CTRLMODE_LOOPBACK;
0900 } else {
0901 dev_info(dev->netdev->dev.parent,
0902 "Firmware update available. Please contact support@peak-system.com\n");
0903 }
0904
0905 return 0;
0906 }
0907
0908
0909
0910
0911 static int pcan_usb_probe(struct usb_interface *intf)
0912 {
0913 struct usb_host_interface *if_desc;
0914 int i;
0915
0916 if_desc = intf->altsetting;
0917
0918
0919 for (i = 0; i < if_desc->desc.bNumEndpoints; i++) {
0920 struct usb_endpoint_descriptor *ep = &if_desc->endpoint[i].desc;
0921
0922 switch (ep->bEndpointAddress) {
0923 case PCAN_USB_EP_CMDOUT:
0924 case PCAN_USB_EP_CMDIN:
0925 case PCAN_USB_EP_MSGOUT:
0926 case PCAN_USB_EP_MSGIN:
0927 break;
0928 default:
0929 return -ENODEV;
0930 }
0931 }
0932
0933 return 0;
0934 }
0935
0936 static int pcan_usb_set_phys_id(struct net_device *netdev,
0937 enum ethtool_phys_id_state state)
0938 {
0939 struct peak_usb_device *dev = netdev_priv(netdev);
0940 int err = 0;
0941
0942 switch (state) {
0943 case ETHTOOL_ID_ACTIVE:
0944
0945 return 2;
0946
0947 case ETHTOOL_ID_OFF:
0948 err = pcan_usb_set_led(dev, 0);
0949 break;
0950
0951 case ETHTOOL_ID_ON:
0952 fallthrough;
0953
0954 case ETHTOOL_ID_INACTIVE:
0955
0956 err = pcan_usb_set_led(dev, 1);
0957 break;
0958
0959 default:
0960 break;
0961 }
0962
0963 return err;
0964 }
0965
0966 static const struct ethtool_ops pcan_usb_ethtool_ops = {
0967 .set_phys_id = pcan_usb_set_phys_id,
0968 .get_ts_info = pcan_get_ts_info,
0969 };
0970
0971
0972
0973
0974 static const struct can_bittiming_const pcan_usb_const = {
0975 .name = "pcan_usb",
0976 .tseg1_min = 1,
0977 .tseg1_max = 16,
0978 .tseg2_min = 1,
0979 .tseg2_max = 8,
0980 .sjw_max = 4,
0981 .brp_min = 1,
0982 .brp_max = 64,
0983 .brp_inc = 1,
0984 };
0985
0986 const struct peak_usb_adapter pcan_usb = {
0987 .name = "PCAN-USB",
0988 .device_id = PCAN_USB_PRODUCT_ID,
0989 .ctrl_count = 1,
0990 .ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_LISTENONLY |
0991 CAN_CTRLMODE_CC_LEN8_DLC,
0992 .clock = {
0993 .freq = PCAN_USB_CRYSTAL_HZ / 2,
0994 },
0995 .bittiming_const = &pcan_usb_const,
0996
0997
0998 .sizeof_dev_private = sizeof(struct pcan_usb),
0999
1000 .ethtool_ops = &pcan_usb_ethtool_ops,
1001
1002
1003 .ts_used_bits = 16,
1004 .us_per_ts_scale = PCAN_USB_TS_US_PER_TICK,
1005 .us_per_ts_shift = PCAN_USB_TS_DIV_SHIFTER,
1006
1007
1008 .ep_msg_in = PCAN_USB_EP_MSGIN,
1009 .ep_msg_out = {PCAN_USB_EP_MSGOUT},
1010
1011
1012 .rx_buffer_size = PCAN_USB_RX_BUFFER_SIZE,
1013 .tx_buffer_size = PCAN_USB_TX_BUFFER_SIZE,
1014
1015
1016 .intf_probe = pcan_usb_probe,
1017 .dev_init = pcan_usb_init,
1018 .dev_set_bus = pcan_usb_write_mode,
1019 .dev_set_bittiming = pcan_usb_set_bittiming,
1020 .dev_get_device_id = pcan_usb_get_device_id,
1021 .dev_decode_buf = pcan_usb_decode_buf,
1022 .dev_encode_msg = pcan_usb_encode_msg,
1023 .dev_start = pcan_usb_start,
1024 .dev_restart_async = pcan_usb_restart_async,
1025 .do_get_berr_counter = pcan_usb_get_berr_counter,
1026 };