1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/usb.h>
16#include <linux/crc16.h>
17#include <asm/unaligned.h>
18
19#include "es58x_core.h"
20
21#define DRV_VERSION "1.00"
22MODULE_AUTHOR("Mailhol Vincent <mailhol.vincent@wanadoo.fr>");
23MODULE_AUTHOR("Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>");
24MODULE_DESCRIPTION("Socket CAN driver for ETAS ES58X USB adapters");
25MODULE_VERSION(DRV_VERSION);
26MODULE_LICENSE("GPL v2");
27
28#define ES58X_MODULE_NAME "etas_es58x"
29#define ES58X_VENDOR_ID 0x108C
30#define ES581_4_PRODUCT_ID 0x0159
31#define ES582_1_PRODUCT_ID 0x0168
32#define ES584_1_PRODUCT_ID 0x0169
33
34
35#define ES58X_FD_INTERFACE_PROTOCOL 0
36
37
38static const struct usb_device_id es58x_id_table[] = {
39 {
40
41 USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID),
42 .driver_info = ES58X_DUAL_CHANNEL
43 }, {
44
45 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID,
46 ES58X_FD_INTERFACE_PROTOCOL),
47 .driver_info = ES58X_DUAL_CHANNEL | ES58X_FD_FAMILY
48 }, {
49
50 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID,
51 ES58X_FD_INTERFACE_PROTOCOL),
52 .driver_info = ES58X_FD_FAMILY
53 }, {
54
55 }
56};
57
58MODULE_DEVICE_TABLE(usb, es58x_id_table);
59
60#define es58x_print_hex_dump(buf, len) \
61 print_hex_dump(KERN_DEBUG, \
62 ES58X_MODULE_NAME " " __stringify(buf) ": ", \
63 DUMP_PREFIX_NONE, 16, 1, buf, len, false)
64
65#define es58x_print_hex_dump_debug(buf, len) \
66 print_hex_dump_debug(ES58X_MODULE_NAME " " __stringify(buf) ": ",\
67 DUMP_PREFIX_NONE, 16, 1, buf, len, false)
68
69
70
71
72
73#define ES58X_CRC_CALC_OFFSET 2
74
75
76
77
78
79
80
81
82
83static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
84{
85 u16 crc;
86 ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);
87
88 crc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len);
89 return crc;
90}
91
92
93
94
95
96
97
98
99
100static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
101{
102 u16 crc;
103 const __le16 *crc_addr;
104
105 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
106 crc = get_unaligned_le16(crc_addr);
107 return crc;
108}
109
110
111
112
113
114
115
116static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len)
117{
118 u16 crc;
119 __le16 *crc_addr;
120
121 crc = es58x_calculate_crc(urb_cmd, urb_len);
122 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
123 put_unaligned_le16(crc, crc_addr);
124}
125
126
127
128
129
130
131
132
133
134
135static int es58x_check_crc(struct es58x_device *es58x_dev,
136 const union es58x_urb_cmd *urb_cmd, u16 urb_len)
137{
138 u16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len);
139 u16 expected_crc = es58x_get_crc(urb_cmd, urb_len);
140
141 if (expected_crc != calculated_crc) {
142 dev_err_ratelimited(es58x_dev->dev,
143 "%s: Bad CRC, urb_len: %d\n",
144 __func__, urb_len);
145 return -EBADMSG;
146 }
147
148 return 0;
149}
150
151
152
153
154
155
156
157
158
159
160
161static u64 es58x_timestamp_to_ns(u64 timestamp)
162{
163 const u64 es58x_timestamp_ns_mult_coef = 500ULL;
164
165 return es58x_timestamp_ns_mult_coef * timestamp;
166}
167
168
169
170
171
172
173
174
175
176static void es58x_set_skb_timestamp(struct net_device *netdev,
177 struct sk_buff *skb, u64 timestamp)
178{
179 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
180 struct skb_shared_hwtstamps *hwts;
181
182 hwts = skb_hwtstamps(skb);
183
184
185
186 hwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) +
187 es58x_dev->realtime_diff_ns);
188}
189
190
191
192
193
194
195
196
197
198
199
200void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp)
201{
202 u64 ktime_real_ns = ktime_get_real_ns();
203 u64 device_timestamp = es58x_timestamp_to_ns(timestamp);
204
205 dev_dbg(es58x_dev->dev, "%s: request round-trip time: %llu ns\n",
206 __func__, ktime_real_ns - es58x_dev->ktime_req_ns);
207
208 es58x_dev->realtime_diff_ns =
209 (es58x_dev->ktime_req_ns + ktime_real_ns) / 2 - device_timestamp;
210 es58x_dev->ktime_req_ns = 0;
211
212 dev_dbg(es58x_dev->dev,
213 "%s: Device timestamp: %llu, diff with kernel: %llu\n",
214 __func__, device_timestamp, es58x_dev->realtime_diff_ns);
215}
216
217
218
219
220
221
222
223
224
225
226
227
228static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev)
229{
230 if (es58x_dev->ktime_req_ns) {
231 dev_warn(es58x_dev->dev,
232 "%s: Previous request to set timestamp has not completed yet\n",
233 __func__);
234 return -EBUSY;
235 }
236
237 es58x_dev->ktime_req_ns = ktime_get_real_ns();
238 return es58x_dev->ops->get_timestamp(es58x_dev);
239}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260static bool es58x_is_can_state_active(struct net_device *netdev)
261{
262 return es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF;
263}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278static bool es58x_is_echo_skb_threshold_reached(struct es58x_priv *priv)
279{
280 u32 num_echo_skb = priv->tx_head - priv->tx_tail;
281 u32 threshold = priv->can.echo_skb_max -
282 priv->es58x_dev->param->tx_bulk_max + 1;
283
284 return num_echo_skb >= threshold;
285}
286
287
288
289
290
291
292
293
294
295static void es58x_can_free_echo_skb_tail(struct net_device *netdev)
296{
297 struct es58x_priv *priv = es58x_priv(netdev);
298 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
299 unsigned int frame_len = 0;
300
301 can_free_echo_skb(netdev, priv->tx_tail & fifo_mask, &frame_len);
302 netdev_completed_queue(netdev, 1, frame_len);
303
304 priv->tx_tail++;
305
306 netdev->stats.tx_dropped++;
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321static int es58x_can_get_echo_skb_recovery(struct net_device *netdev,
322 u32 rcv_packet_idx)
323{
324 struct es58x_priv *priv = es58x_priv(netdev);
325 int ret = 0;
326
327 netdev->stats.tx_errors++;
328
329 if (net_ratelimit())
330 netdev_warn(netdev,
331 "Bad echo packet index: %u. First index: %u, end index %u, num_echo_skb: %02u/%02u\n",
332 rcv_packet_idx, priv->tx_tail, priv->tx_head,
333 priv->tx_head - priv->tx_tail,
334 priv->can.echo_skb_max);
335
336 if ((s32)(rcv_packet_idx - priv->tx_tail) < 0) {
337 if (net_ratelimit())
338 netdev_warn(netdev,
339 "Received echo index is from the past. Ignoring it\n");
340 ret = -EINVAL;
341 } else if ((s32)(rcv_packet_idx - priv->tx_head) >= 0) {
342 if (net_ratelimit())
343 netdev_err(netdev,
344 "Received echo index is from the future. Ignoring it\n");
345 ret = -EINVAL;
346 } else {
347 if (net_ratelimit())
348 netdev_warn(netdev,
349 "Recovery: dropping %u echo skb from index %u to %u\n",
350 rcv_packet_idx - priv->tx_tail,
351 priv->tx_tail, rcv_packet_idx - 1);
352 while (priv->tx_tail != rcv_packet_idx) {
353 if (priv->tx_tail == priv->tx_head)
354 return -EINVAL;
355 es58x_can_free_echo_skb_tail(netdev);
356 }
357 }
358 return ret;
359}
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383int es58x_can_get_echo_skb(struct net_device *netdev, u32 rcv_packet_idx,
384 u64 *tstamps, unsigned int pkts)
385{
386 struct es58x_priv *priv = es58x_priv(netdev);
387 unsigned int rx_total_frame_len = 0;
388 unsigned int num_echo_skb = priv->tx_head - priv->tx_tail;
389 int i;
390 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
391
392 if (!netif_running(netdev)) {
393 if (net_ratelimit())
394 netdev_info(netdev,
395 "%s: %s is down, dropping %d echo packets\n",
396 __func__, netdev->name, pkts);
397 netdev->stats.tx_dropped += pkts;
398 return 0;
399 } else if (!es58x_is_can_state_active(netdev)) {
400 if (net_ratelimit())
401 netdev_dbg(netdev,
402 "Bus is off or device is restarting. Ignoring %u echo packets from index %u\n",
403 pkts, rcv_packet_idx);
404
405
406
407
408 return 0;
409 } else if (num_echo_skb == 0) {
410 if (net_ratelimit())
411 netdev_warn(netdev,
412 "Received %u echo packets from index: %u but echo skb queue is empty.\n",
413 pkts, rcv_packet_idx);
414 netdev->stats.tx_dropped += pkts;
415 return 0;
416 }
417
418 if (priv->tx_tail != rcv_packet_idx) {
419 if (es58x_can_get_echo_skb_recovery(netdev, rcv_packet_idx) < 0) {
420 if (net_ratelimit())
421 netdev_warn(netdev,
422 "Could not find echo skb for echo packet index: %u\n",
423 rcv_packet_idx);
424 return 0;
425 }
426 }
427 if (num_echo_skb < pkts) {
428 int pkts_drop = pkts - num_echo_skb;
429
430 if (net_ratelimit())
431 netdev_err(netdev,
432 "Received %u echo packets but have only %d echo skb. Dropping %d echo skb\n",
433 pkts, num_echo_skb, pkts_drop);
434 netdev->stats.tx_dropped += pkts_drop;
435 pkts -= pkts_drop;
436 }
437
438 for (i = 0; i < pkts; i++) {
439 unsigned int skb_idx = priv->tx_tail & fifo_mask;
440 struct sk_buff *skb = priv->can.echo_skb[skb_idx];
441 unsigned int frame_len = 0;
442
443 if (skb)
444 es58x_set_skb_timestamp(netdev, skb, tstamps[i]);
445
446 netdev->stats.tx_bytes += can_get_echo_skb(netdev, skb_idx,
447 &frame_len);
448 rx_total_frame_len += frame_len;
449
450 priv->tx_tail++;
451 }
452
453 netdev_completed_queue(netdev, pkts, rx_total_frame_len);
454 netdev->stats.tx_packets += pkts;
455
456 priv->err_passive_before_rtx_success = 0;
457 if (!es58x_is_echo_skb_threshold_reached(priv))
458 netif_wake_queue(netdev);
459
460 return 0;
461}
462
463
464
465
466
467
468
469
470
471
472static void es58x_can_reset_echo_fifo(struct net_device *netdev)
473{
474 struct es58x_priv *priv = es58x_priv(netdev);
475
476 priv->tx_tail = 0;
477 priv->tx_head = 0;
478 priv->tx_urb = NULL;
479 priv->err_passive_before_rtx_success = 0;
480 netdev_reset_queue(netdev);
481}
482
483
484
485
486
487
488
489
490
491
492
493static void es58x_flush_pending_tx_msg(struct net_device *netdev)
494{
495 struct es58x_priv *priv = es58x_priv(netdev);
496 struct es58x_device *es58x_dev = priv->es58x_dev;
497
498 if (priv->tx_urb) {
499 netdev_warn(netdev, "%s: dropping %d TX messages\n",
500 __func__, priv->tx_can_msg_cnt);
501 netdev->stats.tx_dropped += priv->tx_can_msg_cnt;
502 while (priv->tx_can_msg_cnt > 0) {
503 unsigned int frame_len = 0;
504 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
505
506 priv->tx_head--;
507 priv->tx_can_msg_cnt--;
508 can_free_echo_skb(netdev, priv->tx_head & fifo_mask,
509 &frame_len);
510 netdev_completed_queue(netdev, 1, frame_len);
511 }
512 usb_anchor_urb(priv->tx_urb, &priv->es58x_dev->tx_urbs_idle);
513 atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
514 usb_free_urb(priv->tx_urb);
515 }
516 priv->tx_urb = NULL;
517}
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
535 enum es58x_ret_u32 rx_cmd_ret_u32)
536{
537 struct es58x_priv *priv = es58x_priv(netdev);
538
539 if (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) {
540 if (net_ratelimit())
541 netdev_err(netdev,
542 "Only %d entries left in device queue, num_echo_skb: %d/%d\n",
543 tx_free_entries,
544 priv->tx_head - priv->tx_tail,
545 priv->can.echo_skb_max);
546 netif_stop_queue(netdev);
547 }
548
549 return es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG,
550 rx_cmd_ret_u32);
551}
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,
571 canid_t can_id, enum es58x_flag es58x_flags, u8 dlc)
572{
573 struct canfd_frame *cfd;
574 struct can_frame *ccf;
575 struct sk_buff *skb;
576 u8 len;
577 bool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA);
578
579 if (dlc > CAN_MAX_RAW_DLC) {
580 netdev_err(netdev,
581 "%s: DLC is %d but maximum should be %d\n",
582 __func__, dlc, CAN_MAX_RAW_DLC);
583 return -EMSGSIZE;
584 }
585
586 if (is_can_fd) {
587 len = can_fd_dlc2len(dlc);
588 skb = alloc_canfd_skb(netdev, &cfd);
589 } else {
590 len = can_cc_dlc2len(dlc);
591 skb = alloc_can_skb(netdev, &ccf);
592 cfd = (struct canfd_frame *)ccf;
593 }
594 if (!skb) {
595 netdev->stats.rx_dropped++;
596 return 0;
597 }
598
599 cfd->can_id = can_id;
600 if (es58x_flags & ES58X_FLAG_EFF)
601 cfd->can_id |= CAN_EFF_FLAG;
602 if (is_can_fd) {
603 cfd->len = len;
604 if (es58x_flags & ES58X_FLAG_FD_BRS)
605 cfd->flags |= CANFD_BRS;
606 if (es58x_flags & ES58X_FLAG_FD_ESI)
607 cfd->flags |= CANFD_ESI;
608 } else {
609 can_frame_set_cc_len(ccf, dlc, es58x_priv(netdev)->can.ctrlmode);
610 if (es58x_flags & ES58X_FLAG_RTR) {
611 ccf->can_id |= CAN_RTR_FLAG;
612 len = 0;
613 }
614 }
615 memcpy(cfd->data, data, len);
616 netdev->stats.rx_packets++;
617 netdev->stats.rx_bytes += len;
618
619 es58x_set_skb_timestamp(netdev, skb, timestamp);
620 netif_rx(skb);
621
622 es58x_priv(netdev)->err_passive_before_rtx_success = 0;
623
624 return 0;
625}
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
660 enum es58x_event event, u64 timestamp)
661{
662 struct es58x_priv *priv = es58x_priv(netdev);
663 struct can_priv *can = netdev_priv(netdev);
664 struct can_device_stats *can_stats = &can->can_stats;
665 struct can_frame *cf = NULL;
666 struct sk_buff *skb;
667 int ret;
668
669 if (!netif_running(netdev)) {
670 if (net_ratelimit())
671 netdev_info(netdev, "%s: %s is down, dropping packet\n",
672 __func__, netdev->name);
673 netdev->stats.rx_dropped++;
674 return 0;
675 }
676
677 if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
678 netdev_err(netdev, "%s: Both error and event are zero\n",
679 __func__);
680 return -EINVAL;
681 }
682
683 skb = alloc_can_err_skb(netdev, &cf);
684
685 switch (error) {
686 case ES58X_ERR_OK:
687 break;
688
689 case ES58X_ERR_PROT_STUFF:
690 if (net_ratelimit())
691 netdev_dbg(netdev, "Error BITSTUFF\n");
692 if (cf)
693 cf->data[2] |= CAN_ERR_PROT_STUFF;
694 break;
695
696 case ES58X_ERR_PROT_FORM:
697 if (net_ratelimit())
698 netdev_dbg(netdev, "Error FORMAT\n");
699 if (cf)
700 cf->data[2] |= CAN_ERR_PROT_FORM;
701 break;
702
703 case ES58X_ERR_ACK:
704 if (net_ratelimit())
705 netdev_dbg(netdev, "Error ACK\n");
706 if (cf)
707 cf->can_id |= CAN_ERR_ACK;
708 break;
709
710 case ES58X_ERR_PROT_BIT:
711 if (net_ratelimit())
712 netdev_dbg(netdev, "Error BIT\n");
713 if (cf)
714 cf->data[2] |= CAN_ERR_PROT_BIT;
715 break;
716
717 case ES58X_ERR_PROT_CRC:
718 if (net_ratelimit())
719 netdev_dbg(netdev, "Error CRC\n");
720 if (cf)
721 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
722 break;
723
724 case ES58X_ERR_PROT_BIT1:
725 if (net_ratelimit())
726 netdev_dbg(netdev,
727 "Error: expected a recessive bit but monitored a dominant one\n");
728 if (cf)
729 cf->data[2] |= CAN_ERR_PROT_BIT1;
730 break;
731
732 case ES58X_ERR_PROT_BIT0:
733 if (net_ratelimit())
734 netdev_dbg(netdev,
735 "Error expected a dominant bit but monitored a recessive one\n");
736 if (cf)
737 cf->data[2] |= CAN_ERR_PROT_BIT0;
738 break;
739
740 case ES58X_ERR_PROT_OVERLOAD:
741 if (net_ratelimit())
742 netdev_dbg(netdev, "Error OVERLOAD\n");
743 if (cf)
744 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
745 break;
746
747 case ES58X_ERR_PROT_UNSPEC:
748 if (net_ratelimit())
749 netdev_dbg(netdev, "Unspecified error\n");
750 if (cf)
751 cf->can_id |= CAN_ERR_PROT;
752 break;
753
754 default:
755 if (net_ratelimit())
756 netdev_err(netdev,
757 "%s: Unspecified error code 0x%04X\n",
758 __func__, (int)error);
759 if (cf)
760 cf->can_id |= CAN_ERR_PROT;
761 break;
762 }
763
764 switch (event) {
765 case ES58X_EVENT_OK:
766 break;
767
768 case ES58X_EVENT_CRTL_ACTIVE:
769 if (can->state == CAN_STATE_BUS_OFF) {
770 netdev_err(netdev,
771 "%s: state transition: BUS OFF -> ACTIVE\n",
772 __func__);
773 }
774 if (net_ratelimit())
775 netdev_dbg(netdev, "Event CAN BUS ACTIVE\n");
776 if (cf)
777 cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
778 can->state = CAN_STATE_ERROR_ACTIVE;
779 break;
780
781 case ES58X_EVENT_CRTL_PASSIVE:
782 if (net_ratelimit())
783 netdev_dbg(netdev, "Event CAN BUS PASSIVE\n");
784
785
786
787
788 if (cf) {
789 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
790 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
791 }
792 if (can->state < CAN_STATE_BUS_OFF)
793 can->state = CAN_STATE_ERROR_PASSIVE;
794 can_stats->error_passive++;
795 if (priv->err_passive_before_rtx_success < U8_MAX)
796 priv->err_passive_before_rtx_success++;
797 break;
798
799 case ES58X_EVENT_CRTL_WARNING:
800 if (net_ratelimit())
801 netdev_dbg(netdev, "Event CAN BUS WARNING\n");
802
803
804
805
806 if (cf) {
807 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
808 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
809 }
810 if (can->state < CAN_STATE_BUS_OFF)
811 can->state = CAN_STATE_ERROR_WARNING;
812 can_stats->error_warning++;
813 break;
814
815 case ES58X_EVENT_BUSOFF:
816 if (net_ratelimit())
817 netdev_dbg(netdev, "Event CAN BUS OFF\n");
818 if (cf)
819 cf->can_id |= CAN_ERR_BUSOFF;
820 can_stats->bus_off++;
821 netif_stop_queue(netdev);
822 if (can->state != CAN_STATE_BUS_OFF) {
823 can->state = CAN_STATE_BUS_OFF;
824 can_bus_off(netdev);
825 ret = can->do_set_mode(netdev, CAN_MODE_STOP);
826 if (ret)
827 return ret;
828 }
829 break;
830
831 case ES58X_EVENT_SINGLE_WIRE:
832 if (net_ratelimit())
833 netdev_warn(netdev,
834 "Lost connection on either CAN high or CAN low\n");
835
836
837
838 if (cf) {
839 cf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE;
840 cf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE;
841 }
842 break;
843
844 default:
845 if (net_ratelimit())
846 netdev_err(netdev,
847 "%s: Unspecified event code 0x%04X\n",
848 __func__, (int)event);
849 if (cf)
850 cf->can_id |= CAN_ERR_CRTL;
851 break;
852 }
853
854
855
856
857
858 netdev->stats.rx_packets++;
859 netdev->stats.rx_bytes += CAN_ERR_DLC;
860
861 if (cf) {
862 if (cf->data[1])
863 cf->can_id |= CAN_ERR_CRTL;
864 if (cf->data[2] || cf->data[3]) {
865 cf->can_id |= CAN_ERR_PROT;
866 can_stats->bus_error++;
867 }
868 if (cf->data[4])
869 cf->can_id |= CAN_ERR_TRX;
870
871 es58x_set_skb_timestamp(netdev, skb, timestamp);
872 netif_rx(skb);
873 }
874
875 if ((event & ES58X_EVENT_CRTL_PASSIVE) &&
876 priv->err_passive_before_rtx_success == ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) {
877 netdev_info(netdev,
878 "Got %d consecutive warning events with no successful RX or TX. Forcing bus-off\n",
879 priv->err_passive_before_rtx_success);
880 return es58x_rx_err_msg(netdev, ES58X_ERR_OK,
881 ES58X_EVENT_BUSOFF, timestamp);
882 }
883
884 return 0;
885}
886
887
888
889
890
891
892
893
894
895
896static const char *es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)
897{
898 switch (cmd_ret_type) {
899 case ES58X_RET_TYPE_SET_BITTIMING:
900 return "Set bittiming";
901 case ES58X_RET_TYPE_ENABLE_CHANNEL:
902 return "Enable channel";
903 case ES58X_RET_TYPE_DISABLE_CHANNEL:
904 return "Disable channel";
905 case ES58X_RET_TYPE_TX_MSG:
906 return "Transmit message";
907 case ES58X_RET_TYPE_RESET_RX:
908 return "Reset RX";
909 case ES58X_RET_TYPE_RESET_TX:
910 return "Reset TX";
911 case ES58X_RET_TYPE_DEVICE_ERR:
912 return "Device error";
913 }
914
915 return "<unknown>";
916};
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935int es58x_rx_cmd_ret_u8(struct device *dev,
936 enum es58x_ret_type cmd_ret_type,
937 enum es58x_ret_u8 rx_cmd_ret_u8)
938{
939 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
940
941 switch (rx_cmd_ret_u8) {
942 case ES58X_RET_U8_OK:
943 dev_dbg_ratelimited(dev, "%s: OK\n", ret_desc);
944 return 0;
945
946 case ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE:
947 dev_err(dev, "%s: unspecified failure\n", ret_desc);
948 return -EBADMSG;
949
950 case ES58X_RET_U8_ERR_NO_MEM:
951 dev_err(dev, "%s: device ran out of memory\n", ret_desc);
952 return -ENOMEM;
953
954 case ES58X_RET_U8_ERR_BAD_CRC:
955 dev_err(dev, "%s: CRC of previous command is incorrect\n",
956 ret_desc);
957 return -EIO;
958
959 default:
960 dev_err(dev, "%s: returned unknown value: 0x%02X\n",
961 ret_desc, rx_cmd_ret_u8);
962 return -EBADMSG;
963 }
964}
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980int es58x_rx_cmd_ret_u32(struct net_device *netdev,
981 enum es58x_ret_type cmd_ret_type,
982 enum es58x_ret_u32 rx_cmd_ret_u32)
983{
984 struct es58x_priv *priv = es58x_priv(netdev);
985 const struct es58x_operators *ops = priv->es58x_dev->ops;
986 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
987
988 switch (rx_cmd_ret_u32) {
989 case ES58X_RET_U32_OK:
990 switch (cmd_ret_type) {
991 case ES58X_RET_TYPE_ENABLE_CHANNEL:
992 es58x_can_reset_echo_fifo(netdev);
993 priv->can.state = CAN_STATE_ERROR_ACTIVE;
994 netif_wake_queue(netdev);
995 netdev_info(netdev,
996 "%s: %s (Serial Number %s): CAN%d channel becomes ready\n",
997 ret_desc, priv->es58x_dev->udev->product,
998 priv->es58x_dev->udev->serial,
999 priv->channel_idx + 1);
1000 break;
1001
1002 case ES58X_RET_TYPE_TX_MSG:
1003 if (IS_ENABLED(CONFIG_VERBOSE_DEBUG) && net_ratelimit())
1004 netdev_vdbg(netdev, "%s: OK\n", ret_desc);
1005 break;
1006
1007 default:
1008 netdev_dbg(netdev, "%s: OK\n", ret_desc);
1009 break;
1010 }
1011 return 0;
1012
1013 case ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE:
1014 if (cmd_ret_type == ES58X_RET_TYPE_ENABLE_CHANNEL) {
1015 int ret;
1016
1017 netdev_warn(netdev,
1018 "%s: channel is already opened, closing and re-opening it to reflect new configuration\n",
1019 ret_desc);
1020 ret = ops->disable_channel(es58x_priv(netdev));
1021 if (ret)
1022 return ret;
1023 return ops->enable_channel(es58x_priv(netdev));
1024 }
1025 if (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL) {
1026 netdev_info(netdev,
1027 "%s: channel is already closed\n", ret_desc);
1028 return 0;
1029 }
1030 netdev_err(netdev,
1031 "%s: unspecified failure\n", ret_desc);
1032 return -EBADMSG;
1033
1034 case ES58X_RET_U32_ERR_NO_MEM:
1035 netdev_err(netdev, "%s: device ran out of memory\n", ret_desc);
1036 return -ENOMEM;
1037
1038 case ES58X_RET_U32_WARN_PARAM_ADJUSTED:
1039 netdev_warn(netdev,
1040 "%s: some incompatible parameters have been adjusted\n",
1041 ret_desc);
1042 return 0;
1043
1044 case ES58X_RET_U32_WARN_TX_MAYBE_REORDER:
1045 netdev_warn(netdev,
1046 "%s: TX messages might have been reordered\n",
1047 ret_desc);
1048 return 0;
1049
1050 case ES58X_RET_U32_ERR_TIMEDOUT:
1051 netdev_err(netdev, "%s: command timed out\n", ret_desc);
1052 return -ETIMEDOUT;
1053
1054 case ES58X_RET_U32_ERR_FIFO_FULL:
1055 netdev_warn(netdev, "%s: fifo is full\n", ret_desc);
1056 return 0;
1057
1058 case ES58X_RET_U32_ERR_BAD_CONFIG:
1059 netdev_err(netdev, "%s: bad configuration\n", ret_desc);
1060 return -EINVAL;
1061
1062 case ES58X_RET_U32_ERR_NO_RESOURCE:
1063 netdev_err(netdev, "%s: no resource available\n", ret_desc);
1064 return -EBUSY;
1065
1066 default:
1067 netdev_err(netdev, "%s returned unknown value: 0x%08X\n",
1068 ret_desc, rx_cmd_ret_u32);
1069 return -EBADMSG;
1070 }
1071}
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083static void es58x_increment_rx_errors(struct es58x_device *es58x_dev)
1084{
1085 int i;
1086
1087 for (i = 0; i < es58x_dev->num_can_ch; i++)
1088 if (es58x_dev->netdev[i])
1089 es58x_dev->netdev[i]->stats.rx_errors++;
1090}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev,
1102 const union es58x_urb_cmd *urb_cmd)
1103{
1104 const struct es58x_operators *ops = es58x_dev->ops;
1105 size_t cmd_len;
1106 int i, ret;
1107
1108 ret = ops->handle_urb_cmd(es58x_dev, urb_cmd);
1109 switch (ret) {
1110 case 0:
1111 return;
1112
1113 case -ENODEV:
1114 dev_err_ratelimited(es58x_dev->dev, "Device is not ready\n");
1115 break;
1116
1117 case -EINVAL:
1118 case -EMSGSIZE:
1119 case -EBADRQC:
1120 case -EBADMSG:
1121 case -ECHRNG:
1122 case -ETIMEDOUT:
1123 cmd_len = es58x_get_urb_cmd_len(es58x_dev,
1124 ops->get_msg_len(urb_cmd));
1125 dev_err(es58x_dev->dev,
1126 "ops->handle_urb_cmd() returned error %pe",
1127 ERR_PTR(ret));
1128 es58x_print_hex_dump(urb_cmd, cmd_len);
1129 break;
1130
1131 case -EFAULT:
1132 case -ENOMEM:
1133 case -EIO:
1134 default:
1135 dev_crit(es58x_dev->dev,
1136 "ops->handle_urb_cmd() returned error %pe, detaching all network devices\n",
1137 ERR_PTR(ret));
1138 for (i = 0; i < es58x_dev->num_can_ch; i++)
1139 if (es58x_dev->netdev[i])
1140 netif_device_detach(es58x_dev->netdev[i]);
1141 if (es58x_dev->ops->reset_device)
1142 es58x_dev->ops->reset_device(es58x_dev);
1143 break;
1144 }
1145
1146
1147
1148
1149
1150 es58x_increment_rx_errors(es58x_dev);
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev,
1175 const union es58x_urb_cmd *urb_cmd,
1176 u32 urb_actual_len)
1177{
1178 const struct device *dev = es58x_dev->dev;
1179 const struct es58x_parameters *param = es58x_dev->param;
1180 u16 sof, msg_len;
1181 signed int urb_cmd_len, ret;
1182
1183 if (urb_actual_len < param->urb_cmd_header_len) {
1184 dev_vdbg(dev,
1185 "%s: Received %d bytes [%*ph]: header incomplete\n",
1186 __func__, urb_actual_len, urb_actual_len,
1187 urb_cmd->raw_cmd);
1188 return -ENODATA;
1189 }
1190
1191 sof = get_unaligned_le16(&urb_cmd->sof);
1192 if (sof != param->rx_start_of_frame) {
1193 dev_err_ratelimited(es58x_dev->dev,
1194 "%s: Expected sequence 0x%04X for start of frame but got 0x%04X.\n",
1195 __func__, param->rx_start_of_frame, sof);
1196 return -EBADRQC;
1197 }
1198
1199 msg_len = es58x_dev->ops->get_msg_len(urb_cmd);
1200 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1201 if (urb_cmd_len > param->rx_urb_cmd_max_len) {
1202 dev_err_ratelimited(es58x_dev->dev,
1203 "%s: Biggest expected size for rx urb_cmd is %u but receive a command of size %d\n",
1204 __func__,
1205 param->rx_urb_cmd_max_len, urb_cmd_len);
1206 return -EOVERFLOW;
1207 } else if (urb_actual_len < urb_cmd_len) {
1208 dev_vdbg(dev, "%s: Received %02d/%02d bytes\n",
1209 __func__, urb_actual_len, urb_cmd_len);
1210 return -ENODATA;
1211 }
1212
1213 ret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len);
1214 if (ret)
1215 return ret;
1216
1217 return urb_cmd_len;
1218}
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev,
1233 u8 *raw_cmd, int raw_cmd_len)
1234{
1235 if (es58x_dev->rx_cmd_buf_len + raw_cmd_len >
1236 es58x_dev->param->rx_urb_cmd_max_len)
1237 return -EMSGSIZE;
1238
1239 memcpy(&es58x_dev->rx_cmd_buf.raw_cmd[es58x_dev->rx_cmd_buf_len],
1240 raw_cmd, raw_cmd_len);
1241 es58x_dev->rx_cmd_buf_len += raw_cmd_len;
1242
1243 return 0;
1244}
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev,
1265 u8 *raw_cmd, size_t raw_cmd_len)
1266{
1267 union es58x_urb_cmd *urb_cmd;
1268 signed int urb_cmd_len;
1269 u16 sof;
1270 int dropped_bytes = 0;
1271
1272 es58x_increment_rx_errors(es58x_dev);
1273
1274 while (raw_cmd_len > sizeof(sof)) {
1275 urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1276 sof = get_unaligned_le16(&urb_cmd->sof);
1277
1278 if (sof == es58x_dev->param->rx_start_of_frame) {
1279 urb_cmd_len = es58x_check_rx_urb(es58x_dev,
1280 urb_cmd, raw_cmd_len);
1281 if ((urb_cmd_len == -ENODATA) || urb_cmd_len > 0) {
1282 dev_info_ratelimited(es58x_dev->dev,
1283 "Recovery successful! Dropped %d bytes (urb_cmd_len: %d)\n",
1284 dropped_bytes,
1285 urb_cmd_len);
1286 return dropped_bytes;
1287 }
1288 }
1289 raw_cmd++;
1290 raw_cmd_len--;
1291 dropped_bytes++;
1292 }
1293
1294 dev_warn_ratelimited(es58x_dev->dev, "%s: Recovery failed\n", __func__);
1295 return -EBADMSG;
1296}
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev,
1316 struct urb *urb)
1317{
1318 size_t cpy_len;
1319 signed int urb_cmd_len, tmp_cmd_buf_len, ret;
1320
1321 tmp_cmd_buf_len = es58x_dev->rx_cmd_buf_len;
1322 cpy_len = min_t(int, es58x_dev->param->rx_urb_cmd_max_len -
1323 es58x_dev->rx_cmd_buf_len, urb->actual_length);
1324 ret = es58x_copy_to_cmd_buf(es58x_dev, urb->transfer_buffer, cpy_len);
1325 if (ret < 0)
1326 return ret;
1327
1328 urb_cmd_len = es58x_check_rx_urb(es58x_dev, &es58x_dev->rx_cmd_buf,
1329 es58x_dev->rx_cmd_buf_len);
1330 if (urb_cmd_len == -ENODATA) {
1331 return -ENODATA;
1332 } else if (urb_cmd_len < 0) {
1333 dev_err_ratelimited(es58x_dev->dev,
1334 "Could not reconstitute incomplete command from previous URB, dropping %d bytes\n",
1335 tmp_cmd_buf_len + urb->actual_length);
1336 dev_err_ratelimited(es58x_dev->dev,
1337 "Error code: %pe, es58x_dev->rx_cmd_buf_len: %d, urb->actual_length: %u\n",
1338 ERR_PTR(urb_cmd_len),
1339 tmp_cmd_buf_len, urb->actual_length);
1340 es58x_print_hex_dump(&es58x_dev->rx_cmd_buf, tmp_cmd_buf_len);
1341 es58x_print_hex_dump(urb->transfer_buffer, urb->actual_length);
1342 return urb->actual_length;
1343 }
1344
1345 es58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf);
1346 return urb_cmd_len - tmp_cmd_buf_len;
1347}
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365static signed int es58x_split_urb(struct es58x_device *es58x_dev,
1366 struct urb *urb)
1367{
1368 union es58x_urb_cmd *urb_cmd;
1369 u8 *raw_cmd = urb->transfer_buffer;
1370 s32 raw_cmd_len = urb->actual_length;
1371 int ret;
1372
1373 if (es58x_dev->rx_cmd_buf_len != 0) {
1374 ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
1375 if (ret != -ENODATA)
1376 es58x_dev->rx_cmd_buf_len = 0;
1377 if (ret < 0)
1378 return ret;
1379
1380 raw_cmd += ret;
1381 raw_cmd_len -= ret;
1382 }
1383
1384 while (raw_cmd_len > 0) {
1385 if (raw_cmd[0] == ES58X_HEARTBEAT) {
1386 raw_cmd++;
1387 raw_cmd_len--;
1388 continue;
1389 }
1390 urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1391 ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
1392 if (ret > 0) {
1393 es58x_handle_urb_cmd(es58x_dev, urb_cmd);
1394 } else if (ret == -ENODATA) {
1395 es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);
1396 return -ENODATA;
1397 } else if (ret < 0) {
1398 ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
1399 raw_cmd_len);
1400 if (ret < 0)
1401 return ret;
1402 }
1403 raw_cmd += ret;
1404 raw_cmd_len -= ret;
1405 }
1406
1407 return 0;
1408}
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420static void es58x_read_bulk_callback(struct urb *urb)
1421{
1422 struct es58x_device *es58x_dev = urb->context;
1423 const struct device *dev = es58x_dev->dev;
1424 int i, ret;
1425
1426 switch (urb->status) {
1427 case 0:
1428 break;
1429
1430 case -EOVERFLOW:
1431 dev_err_ratelimited(dev, "%s: error %pe\n",
1432 __func__, ERR_PTR(urb->status));
1433 es58x_print_hex_dump_debug(urb->transfer_buffer,
1434 urb->transfer_buffer_length);
1435 goto resubmit_urb;
1436
1437 case -EPROTO:
1438 dev_warn_ratelimited(dev, "%s: error %pe. Device unplugged?\n",
1439 __func__, ERR_PTR(urb->status));
1440 goto free_urb;
1441
1442 case -ENOENT:
1443 case -EPIPE:
1444 dev_err_ratelimited(dev, "%s: error %pe\n",
1445 __func__, ERR_PTR(urb->status));
1446 goto free_urb;
1447
1448 case -ESHUTDOWN:
1449 dev_dbg_ratelimited(dev, "%s: error %pe\n",
1450 __func__, ERR_PTR(urb->status));
1451 goto free_urb;
1452
1453 default:
1454 dev_err_ratelimited(dev, "%s: error %pe\n",
1455 __func__, ERR_PTR(urb->status));
1456 goto resubmit_urb;
1457 }
1458
1459 ret = es58x_split_urb(es58x_dev, urb);
1460 if ((ret != -ENODATA) && ret < 0) {
1461 dev_err(es58x_dev->dev, "es58x_split_urb() returned error %pe",
1462 ERR_PTR(ret));
1463 es58x_print_hex_dump_debug(urb->transfer_buffer,
1464 urb->actual_length);
1465
1466
1467
1468
1469
1470 es58x_increment_rx_errors(es58x_dev);
1471 }
1472
1473 resubmit_urb:
1474 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1475 urb->transfer_buffer, urb->transfer_buffer_length,
1476 es58x_read_bulk_callback, es58x_dev);
1477
1478 ret = usb_submit_urb(urb, GFP_ATOMIC);
1479 if (ret == -ENODEV) {
1480 for (i = 0; i < es58x_dev->num_can_ch; i++)
1481 if (es58x_dev->netdev[i])
1482 netif_device_detach(es58x_dev->netdev[i]);
1483 } else if (ret)
1484 dev_err_ratelimited(dev,
1485 "Failed resubmitting read bulk urb: %pe\n",
1486 ERR_PTR(ret));
1487 return;
1488
1489 free_urb:
1490 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1491 urb->transfer_buffer, urb->transfer_dma);
1492}
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504static void es58x_write_bulk_callback(struct urb *urb)
1505{
1506 struct net_device *netdev = urb->context;
1507 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1508
1509 switch (urb->status) {
1510 case 0:
1511 break;
1512
1513 case -EOVERFLOW:
1514 if (net_ratelimit())
1515 netdev_err(netdev, "%s: error %pe\n",
1516 __func__, ERR_PTR(urb->status));
1517 es58x_print_hex_dump(urb->transfer_buffer,
1518 urb->transfer_buffer_length);
1519 break;
1520
1521 case -ENOENT:
1522 if (net_ratelimit())
1523 netdev_dbg(netdev, "%s: error %pe\n",
1524 __func__, ERR_PTR(urb->status));
1525 usb_free_coherent(urb->dev,
1526 es58x_dev->param->tx_urb_cmd_max_len,
1527 urb->transfer_buffer, urb->transfer_dma);
1528 return;
1529
1530 default:
1531 if (net_ratelimit())
1532 netdev_info(netdev, "%s: error %pe\n",
1533 __func__, ERR_PTR(urb->status));
1534 break;
1535 }
1536
1537 usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);
1538 atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
1539}
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,
1559 u8 **buf, size_t buf_len, gfp_t mem_flags)
1560{
1561 *urb = usb_alloc_urb(0, mem_flags);
1562 if (!*urb) {
1563 dev_err(es58x_dev->dev, "No memory left for URBs\n");
1564 return -ENOMEM;
1565 }
1566
1567 *buf = usb_alloc_coherent(es58x_dev->udev, buf_len,
1568 mem_flags, &(*urb)->transfer_dma);
1569 if (!*buf) {
1570 dev_err(es58x_dev->dev, "No memory left for USB buffer\n");
1571 usb_free_urb(*urb);
1572 return -ENOMEM;
1573 }
1574
1575 (*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1576
1577 return 0;
1578}
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev)
1597{
1598 atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt;
1599 struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1600
1601 if (!urb) {
1602 size_t tx_buf_len;
1603 u8 *buf;
1604
1605 tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;
1606 if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,
1607 GFP_ATOMIC))
1608 return NULL;
1609
1610 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1611 buf, tx_buf_len, NULL, NULL);
1612 return urb;
1613 }
1614
1615 while (atomic_dec_return(idle_cnt) > ES58X_TX_URBS_MAX) {
1616
1617 struct urb *tmp = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1618
1619 if (!tmp)
1620 break;
1621 usb_free_coherent(tmp->dev,
1622 es58x_dev->param->tx_urb_cmd_max_len,
1623 tmp->transfer_buffer, tmp->transfer_dma);
1624 usb_free_urb(tmp);
1625 }
1626
1627 return urb;
1628}
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,
1639 struct net_device *netdev)
1640{
1641 int ret;
1642
1643 es58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length);
1644 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1645 urb->transfer_buffer, urb->transfer_buffer_length,
1646 es58x_write_bulk_callback, netdev);
1647 usb_anchor_urb(urb, &es58x_dev->tx_urbs_busy);
1648 ret = usb_submit_urb(urb, GFP_ATOMIC);
1649 if (ret) {
1650 netdev_err(netdev, "%s: USB send urb failure: %pe\n",
1651 __func__, ERR_PTR(ret));
1652 usb_unanchor_urb(urb);
1653 usb_free_coherent(urb->dev,
1654 es58x_dev->param->tx_urb_cmd_max_len,
1655 urb->transfer_buffer, urb->transfer_dma);
1656 }
1657 usb_free_urb(urb);
1658
1659 return ret;
1660}
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
1677 const void *msg, u16 msg_len, int channel_idx)
1678{
1679 struct net_device *netdev;
1680 union es58x_urb_cmd *urb_cmd;
1681 struct urb *urb;
1682 int urb_cmd_len;
1683
1684 if (channel_idx == ES58X_CHANNEL_IDX_NA)
1685 netdev = es58x_dev->netdev[0];
1686 else
1687 netdev = es58x_dev->netdev[channel_idx];
1688
1689 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1690 if (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len)
1691 return -EOVERFLOW;
1692
1693 urb = es58x_get_tx_urb(es58x_dev);
1694 if (!urb)
1695 return -ENOMEM;
1696
1697 urb_cmd = urb->transfer_buffer;
1698 es58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id,
1699 channel_idx, msg_len);
1700 memcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len],
1701 msg, msg_len);
1702 urb->transfer_buffer_length = urb_cmd_len;
1703
1704 return es58x_submit_urb(es58x_dev, urb, netdev);
1705}
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev)
1716{
1717 const struct device *dev = es58x_dev->dev;
1718 const struct es58x_parameters *param = es58x_dev->param;
1719 size_t rx_buf_len = es58x_dev->rx_max_packet_size;
1720 struct urb *urb;
1721 u8 *buf;
1722 int i;
1723 int ret = -EINVAL;
1724
1725 for (i = 0; i < param->rx_urb_max; i++) {
1726 ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,
1727 GFP_KERNEL);
1728 if (ret)
1729 break;
1730
1731 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1732 buf, rx_buf_len, es58x_read_bulk_callback,
1733 es58x_dev);
1734 usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1735
1736 ret = usb_submit_urb(urb, GFP_KERNEL);
1737 if (ret) {
1738 usb_unanchor_urb(urb);
1739 usb_free_coherent(es58x_dev->udev, rx_buf_len,
1740 buf, urb->transfer_dma);
1741 usb_free_urb(urb);
1742 break;
1743 }
1744 usb_free_urb(urb);
1745 }
1746
1747 if (i == 0) {
1748 dev_err(dev, "%s: Could not setup any rx URBs\n", __func__);
1749 return ret;
1750 }
1751 dev_dbg(dev, "%s: Allocated %d rx URBs each of size %zu\n",
1752 __func__, i, rx_buf_len);
1753
1754 return ret;
1755}
1756
1757
1758
1759
1760
1761static void es58x_free_urbs(struct es58x_device *es58x_dev)
1762{
1763 struct urb *urb;
1764
1765 if (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) {
1766 dev_err(es58x_dev->dev, "%s: Timeout, some TX urbs still remain\n",
1767 __func__);
1768 usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);
1769 }
1770
1771 while ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) {
1772 usb_free_coherent(urb->dev, es58x_dev->param->tx_urb_cmd_max_len,
1773 urb->transfer_buffer, urb->transfer_dma);
1774 usb_free_urb(urb);
1775 atomic_dec(&es58x_dev->tx_urbs_idle_cnt);
1776 }
1777 if (atomic_read(&es58x_dev->tx_urbs_idle_cnt))
1778 dev_err(es58x_dev->dev,
1779 "All idle urbs were freed but tx_urb_idle_cnt is %d\n",
1780 atomic_read(&es58x_dev->tx_urbs_idle_cnt));
1781
1782 usb_kill_anchored_urbs(&es58x_dev->rx_urbs);
1783}
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794static int es58x_open(struct net_device *netdev)
1795{
1796 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1797 int ret;
1798
1799 if (atomic_inc_return(&es58x_dev->opened_channel_cnt) == 1) {
1800 ret = es58x_alloc_rx_urbs(es58x_dev);
1801 if (ret)
1802 return ret;
1803
1804 ret = es58x_set_realtime_diff_ns(es58x_dev);
1805 if (ret)
1806 goto free_urbs;
1807 }
1808
1809 ret = open_candev(netdev);
1810 if (ret)
1811 goto free_urbs;
1812
1813 ret = es58x_dev->ops->enable_channel(es58x_priv(netdev));
1814 if (ret)
1815 goto free_urbs;
1816
1817 netif_start_queue(netdev);
1818
1819 return ret;
1820
1821 free_urbs:
1822 if (atomic_dec_and_test(&es58x_dev->opened_channel_cnt))
1823 es58x_free_urbs(es58x_dev);
1824 netdev_err(netdev, "%s: Could not open the network device: %pe\n",
1825 __func__, ERR_PTR(ret));
1826
1827 return ret;
1828}
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840static int es58x_stop(struct net_device *netdev)
1841{
1842 struct es58x_priv *priv = es58x_priv(netdev);
1843 struct es58x_device *es58x_dev = priv->es58x_dev;
1844 int ret;
1845
1846 netif_stop_queue(netdev);
1847 ret = es58x_dev->ops->disable_channel(priv);
1848 if (ret)
1849 return ret;
1850
1851 priv->can.state = CAN_STATE_STOPPED;
1852 es58x_can_reset_echo_fifo(netdev);
1853 close_candev(netdev);
1854
1855 es58x_flush_pending_tx_msg(netdev);
1856
1857 if (atomic_dec_and_test(&es58x_dev->opened_channel_cnt))
1858 es58x_free_urbs(es58x_dev);
1859
1860 return 0;
1861}
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872static int es58x_xmit_commit(struct net_device *netdev)
1873{
1874 struct es58x_priv *priv = es58x_priv(netdev);
1875 int ret;
1876
1877 if (!es58x_is_can_state_active(netdev))
1878 return -ENETDOWN;
1879
1880 if (es58x_is_echo_skb_threshold_reached(priv))
1881 netif_stop_queue(netdev);
1882
1883 ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);
1884 if (ret == 0)
1885 priv->tx_urb = NULL;
1886
1887 return ret;
1888}
1889
1890
1891
1892
1893
1894
1895
1896static bool es58x_xmit_more(struct es58x_priv *priv)
1897{
1898 unsigned int free_slots =
1899 priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail);
1900
1901 return netdev_xmit_more() && free_slots > 0 &&
1902 priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max;
1903}
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
1923 struct net_device *netdev)
1924{
1925 struct es58x_priv *priv = es58x_priv(netdev);
1926 struct es58x_device *es58x_dev = priv->es58x_dev;
1927 unsigned int frame_len;
1928 int ret;
1929
1930 if (can_dropped_invalid_skb(netdev, skb)) {
1931 if (priv->tx_urb)
1932 goto xmit_commit;
1933 return NETDEV_TX_OK;
1934 }
1935
1936 if (priv->tx_urb && priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
1937
1938 ret = es58x_xmit_commit(netdev);
1939 if (ret)
1940 goto drop_skb;
1941 }
1942
1943 if (!priv->tx_urb) {
1944 priv->tx_urb = es58x_get_tx_urb(es58x_dev);
1945 if (!priv->tx_urb) {
1946 ret = -ENOMEM;
1947 goto drop_skb;
1948 }
1949 priv->tx_can_msg_cnt = 0;
1950 priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
1951 }
1952
1953 ret = es58x_dev->ops->tx_can_msg(priv, skb);
1954 if (ret)
1955 goto drop_skb;
1956
1957 frame_len = can_skb_get_frame_len(skb);
1958 ret = can_put_echo_skb(skb, netdev,
1959 priv->tx_head & es58x_dev->param->fifo_mask,
1960 frame_len);
1961 if (ret)
1962 goto xmit_failure;
1963 netdev_sent_queue(netdev, frame_len);
1964
1965 priv->tx_head++;
1966 priv->tx_can_msg_cnt++;
1967
1968 xmit_commit:
1969 if (!es58x_xmit_more(priv)) {
1970 ret = es58x_xmit_commit(netdev);
1971 if (ret)
1972 goto xmit_failure;
1973 }
1974
1975 return NETDEV_TX_OK;
1976
1977 drop_skb:
1978 dev_kfree_skb(skb);
1979 netdev->stats.tx_dropped++;
1980 xmit_failure:
1981 netdev_warn(netdev, "%s: send message failure: %pe\n",
1982 __func__, ERR_PTR(ret));
1983 netdev->stats.tx_errors++;
1984 es58x_flush_pending_tx_msg(netdev);
1985 return NETDEV_TX_OK;
1986}
1987
1988static const struct net_device_ops es58x_netdev_ops = {
1989 .ndo_open = es58x_open,
1990 .ndo_stop = es58x_stop,
1991 .ndo_start_xmit = es58x_start_xmit
1992};
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
2007{
2008 struct es58x_priv *priv = es58x_priv(netdev);
2009
2010 switch (mode) {
2011 case CAN_MODE_START:
2012 switch (priv->can.state) {
2013 case CAN_STATE_BUS_OFF:
2014 return priv->es58x_dev->ops->enable_channel(priv);
2015
2016 case CAN_STATE_STOPPED:
2017 return es58x_open(netdev);
2018
2019 case CAN_STATE_ERROR_ACTIVE:
2020 case CAN_STATE_ERROR_WARNING:
2021 case CAN_STATE_ERROR_PASSIVE:
2022 default:
2023 return 0;
2024 }
2025
2026 case CAN_MODE_STOP:
2027 switch (priv->can.state) {
2028 case CAN_STATE_STOPPED:
2029 return 0;
2030
2031 case CAN_STATE_ERROR_ACTIVE:
2032 case CAN_STATE_ERROR_WARNING:
2033 case CAN_STATE_ERROR_PASSIVE:
2034 case CAN_STATE_BUS_OFF:
2035 default:
2036 return priv->es58x_dev->ops->disable_channel(priv);
2037 }
2038
2039 case CAN_MODE_SLEEP:
2040 default:
2041 return -EOPNOTSUPP;
2042 }
2043}
2044
2045
2046
2047
2048
2049
2050
2051static void es58x_init_priv(struct es58x_device *es58x_dev,
2052 struct es58x_priv *priv, int channel_idx)
2053{
2054 const struct es58x_parameters *param = es58x_dev->param;
2055 struct can_priv *can = &priv->can;
2056
2057 priv->es58x_dev = es58x_dev;
2058 priv->channel_idx = channel_idx;
2059 priv->tx_urb = NULL;
2060 priv->tx_can_msg_cnt = 0;
2061
2062 can->bittiming_const = param->bittiming_const;
2063 if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
2064 can->data_bittiming_const = param->data_bittiming_const;
2065 can->tdc_const = param->tdc_const;
2066 }
2067 can->bitrate_max = param->bitrate_max;
2068 can->clock = param->clock;
2069 can->state = CAN_STATE_STOPPED;
2070 can->ctrlmode_supported = param->ctrlmode_supported;
2071 can->do_set_mode = es58x_set_mode;
2072}
2073
2074
2075
2076
2077
2078
2079
2080
2081static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
2082{
2083 struct net_device *netdev;
2084 struct device *dev = es58x_dev->dev;
2085 int ret;
2086
2087 netdev = alloc_candev(sizeof(struct es58x_priv),
2088 es58x_dev->param->fifo_mask + 1);
2089 if (!netdev) {
2090 dev_err(dev, "Could not allocate candev\n");
2091 return -ENOMEM;
2092 }
2093 SET_NETDEV_DEV(netdev, dev);
2094 es58x_dev->netdev[channel_idx] = netdev;
2095 es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
2096
2097 netdev->netdev_ops = &es58x_netdev_ops;
2098 netdev->flags |= IFF_ECHO;
2099
2100 ret = register_candev(netdev);
2101 if (ret)
2102 return ret;
2103
2104 netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
2105 es58x_dev->param->dql_min_limit);
2106
2107 return ret;
2108}
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118static int es58x_get_product_info(struct es58x_device *es58x_dev)
2119{
2120 struct usb_device *udev = es58x_dev->udev;
2121 const int es58x_prod_info_idx = 6;
2122
2123
2124
2125 const size_t prod_info_len = 127;
2126 char *prod_info;
2127 int ret;
2128
2129 prod_info = kmalloc(prod_info_len, GFP_KERNEL);
2130 if (!prod_info)
2131 return -ENOMEM;
2132
2133 ret = usb_string(udev, es58x_prod_info_idx, prod_info, prod_info_len);
2134 if (ret < 0) {
2135 dev_err(es58x_dev->dev,
2136 "%s: Could not read the product info: %pe\n",
2137 __func__, ERR_PTR(ret));
2138 goto out_free;
2139 }
2140 if (ret >= prod_info_len - 1) {
2141 dev_warn(es58x_dev->dev,
2142 "%s: Buffer is too small, result might be truncated\n",
2143 __func__);
2144 }
2145 dev_info(es58x_dev->dev, "Product info: %s\n", prod_info);
2146
2147 out_free:
2148 kfree(prod_info);
2149 return ret < 0 ? ret : 0;
2150}
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160static int es58x_init_es58x_dev(struct usb_interface *intf,
2161 struct es58x_device **p_es58x_dev,
2162 kernel_ulong_t driver_info)
2163{
2164 struct device *dev = &intf->dev;
2165 struct es58x_device *es58x_dev;
2166 const struct es58x_parameters *param;
2167 const struct es58x_operators *ops;
2168 struct usb_device *udev = interface_to_usbdev(intf);
2169 struct usb_endpoint_descriptor *ep_in, *ep_out;
2170 int ret;
2171
2172 dev_info(dev,
2173 "Starting %s %s (Serial Number %s) driver version %s\n",
2174 udev->manufacturer, udev->product, udev->serial, DRV_VERSION);
2175
2176 ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
2177 NULL, NULL);
2178 if (ret)
2179 return ret;
2180
2181 if (driver_info & ES58X_FD_FAMILY) {
2182 param = &es58x_fd_param;
2183 ops = &es58x_fd_ops;
2184 } else {
2185 param = &es581_4_param;
2186 ops = &es581_4_ops;
2187 }
2188
2189 es58x_dev = kzalloc(es58x_sizeof_es58x_device(param), GFP_KERNEL);
2190 if (!es58x_dev)
2191 return -ENOMEM;
2192
2193 es58x_dev->param = param;
2194 es58x_dev->ops = ops;
2195 es58x_dev->dev = dev;
2196 es58x_dev->udev = udev;
2197
2198 if (driver_info & ES58X_DUAL_CHANNEL)
2199 es58x_dev->num_can_ch = 2;
2200 else
2201 es58x_dev->num_can_ch = 1;
2202
2203 init_usb_anchor(&es58x_dev->rx_urbs);
2204 init_usb_anchor(&es58x_dev->tx_urbs_idle);
2205 init_usb_anchor(&es58x_dev->tx_urbs_busy);
2206 atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0);
2207 atomic_set(&es58x_dev->opened_channel_cnt, 0);
2208 usb_set_intfdata(intf, es58x_dev);
2209
2210 es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,
2211 ep_in->bEndpointAddress);
2212 es58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev,
2213 ep_out->bEndpointAddress);
2214 es58x_dev->rx_max_packet_size = le16_to_cpu(ep_in->wMaxPacketSize);
2215
2216 *p_es58x_dev = es58x_dev;
2217
2218 return 0;
2219}
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229static int es58x_probe(struct usb_interface *intf,
2230 const struct usb_device_id *id)
2231{
2232 struct es58x_device *es58x_dev;
2233 int ch_idx, ret;
2234
2235 ret = es58x_init_es58x_dev(intf, &es58x_dev, id->driver_info);
2236 if (ret)
2237 return ret;
2238
2239 ret = es58x_get_product_info(es58x_dev);
2240 if (ret)
2241 goto cleanup_es58x_dev;
2242
2243 for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
2244 ret = es58x_init_netdev(es58x_dev, ch_idx);
2245 if (ret)
2246 goto cleanup_candev;
2247 }
2248
2249 return ret;
2250
2251 cleanup_candev:
2252 for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++)
2253 if (es58x_dev->netdev[ch_idx]) {
2254 unregister_candev(es58x_dev->netdev[ch_idx]);
2255 free_candev(es58x_dev->netdev[ch_idx]);
2256 }
2257 cleanup_es58x_dev:
2258 kfree(es58x_dev);
2259
2260 return ret;
2261}
2262
2263
2264
2265
2266
2267
2268
2269
2270static void es58x_disconnect(struct usb_interface *intf)
2271{
2272 struct es58x_device *es58x_dev = usb_get_intfdata(intf);
2273 struct net_device *netdev;
2274 int i;
2275
2276 dev_info(&intf->dev, "Disconnecting %s %s\n",
2277 es58x_dev->udev->manufacturer, es58x_dev->udev->product);
2278
2279 for (i = 0; i < es58x_dev->num_can_ch; i++) {
2280 netdev = es58x_dev->netdev[i];
2281 if (!netdev)
2282 continue;
2283 unregister_candev(netdev);
2284 es58x_dev->netdev[i] = NULL;
2285 free_candev(netdev);
2286 }
2287
2288 es58x_free_urbs(es58x_dev);
2289
2290 kfree(es58x_dev);
2291 usb_set_intfdata(intf, NULL);
2292}
2293
2294static struct usb_driver es58x_driver = {
2295 .name = ES58X_MODULE_NAME,
2296 .probe = es58x_probe,
2297 .disconnect = es58x_disconnect,
2298 .id_table = es58x_id_table
2299};
2300
2301module_usb_driver(es58x_driver);
2302