| 1 | |
| 2 | |
| 3 |
|
| 4 |
|
| 5 | #include <linux/can/dev.h>
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 | static const u8 dlc2len[] = {
|
| 10 | 0, 1, 2, 3, 4, 5, 6, 7,
|
| 11 | 8, 12, 16, 20, 24, 32, 48, 64
|
| 12 | };
|
| 13 |
|
| 14 |
|
| 15 | u8 can_fd_dlc2len(u8 dlc)
|
| 16 | {
|
| 17 | return dlc2len[dlc & 0x0F];
|
| 18 | }
|
| 19 | EXPORT_SYMBOL_GPL(can_fd_dlc2len);
|
| 20 |
|
| 21 | static const u8 len2dlc[] = {
|
| 22 | 0, 1, 2, 3, 4, 5, 6, 7, 8,
|
| 23 | 9, 9, 9, 9,
|
| 24 | 10, 10, 10, 10,
|
| 25 | 11, 11, 11, 11,
|
| 26 | 12, 12, 12, 12,
|
| 27 | 13, 13, 13, 13, 13, 13, 13, 13,
|
| 28 | 14, 14, 14, 14, 14, 14, 14, 14,
|
| 29 | 14, 14, 14, 14, 14, 14, 14, 14,
|
| 30 | 15, 15, 15, 15, 15, 15, 15, 15,
|
| 31 | 15, 15, 15, 15, 15, 15, 15, 15
|
| 32 | };
|
| 33 |
|
| 34 |
|
| 35 | u8 can_fd_len2dlc(u8 len)
|
| 36 | {
|
| 37 |
|
| 38 | BUILD_BUG_ON(ARRAY_SIZE(len2dlc) != CANFD_MAX_DLEN + 1);
|
| 39 |
|
| 40 | if (unlikely(len > CANFD_MAX_DLEN))
|
| 41 | return CANFD_MAX_DLC;
|
| 42 |
|
| 43 | return len2dlc[len];
|
| 44 | }
|
| 45 | EXPORT_SYMBOL_GPL(can_fd_len2dlc);
|
| 46 |
|
| 47 | |
| 48 | * can_skb_get_frame_len() - Calculate the CAN Frame length in bytes
|
| 49 | * of a given skb.
|
| 50 | * @skb: socket buffer of a CAN message.
|
| 51 | *
|
| 52 | * Do a rough calculation: bit stuffing is ignored and length in bits
|
| 53 | * is rounded up to a length in bytes.
|
| 54 | *
|
| 55 | * Rationale: this function is to be used for the BQL functions
|
| 56 | * (netdev_sent_queue() and netdev_completed_queue()) which expect a
|
| 57 | * value in bytes. Just using skb->len is insufficient because it will
|
| 58 | * return the constant value of CAN(FD)_MTU. Doing the bit stuffing
|
| 59 | * calculation would be too expensive in term of computing resources
|
| 60 | * for no noticeable gain.
|
| 61 | *
|
| 62 | * Remarks: The payload of CAN FD frames with BRS flag are sent at a
|
| 63 | * different bitrate. Currently, the can-utils canbusload tool does
|
| 64 | * not support CAN-FD yet and so we could not run any benchmark to
|
| 65 | * measure the impact. There might be possible improvement here.
|
| 66 | *
|
| 67 | * Return: length in bytes.
|
| 68 |
|
| 69 | unsigned int can_skb_get_frame_len(const struct sk_buff *skb)
|
| 70 | {
|
| 71 | const struct canfd_frame *cf = (const struct canfd_frame *)skb->data;
|
| 72 | u8 len;
|
| 73 |
|
| 74 | if (can_is_canfd_skb(skb))
|
| 75 | len = canfd_sanitize_len(cf->len);
|
| 76 | else if (cf->can_id & CAN_RTR_FLAG)
|
| 77 | len = 0;
|
| 78 | else
|
| 79 | len = cf->len;
|
| 80 |
|
| 81 | return can_frame_bytes(can_is_canfd_skb(skb), cf->can_id & CAN_EFF_FLAG,
|
| 82 | false, len);
|
| 83 | }
|
| 84 | EXPORT_SYMBOL_GPL(can_skb_get_frame_len);
|