Skip to content

Commit 299a557

Browse files
Frank Jungclausmarckleinebudde
authored andcommitted
can: esd_usb: Replace hardcoded message length given to USB commands
Replace all hardcoded values supplied to the len element of esd_usb_msg (and its siblings) by more readable expressions, based on sizeof(), offsetof(), etc. Also spend documentation / comments that the len element of esd_usb_msg is in multiples of 32bit words and not in bytes. Link: https://lore.kernel.org/all/CAMZ6RqLaDNy-fZ2G0+QMhUEckkXLL+ZyELVSDFmqpd++aBzZQg@mail.gmail.com/ Suggested-by: Vincent MAILHOL <[email protected]> Signed-off-by: Frank Jungclaus <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 8ef426e commit 299a557

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

drivers/net/can/usb/esd_usb.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,21 @@ MODULE_LICENSE("GPL v2");
9090
#define ESD_USB_MAX_TX_URBS 16 /* must be power of 2 */
9191

9292
struct esd_usb_header_msg {
93-
u8 len; /* len is always the total message length in 32bit words */
93+
u8 len; /* total message length in 32bit words */
9494
u8 cmd;
9595
u8 rsvd[2];
9696
};
9797

9898
struct esd_usb_version_msg {
99-
u8 len;
99+
u8 len; /* total message length in 32bit words */
100100
u8 cmd;
101101
u8 rsvd;
102102
u8 flags;
103103
__le32 drv_version;
104104
};
105105

106106
struct esd_usb_version_reply_msg {
107-
u8 len;
107+
u8 len; /* total message length in 32bit words */
108108
u8 cmd;
109109
u8 nets;
110110
u8 features;
@@ -115,7 +115,7 @@ struct esd_usb_version_reply_msg {
115115
};
116116

117117
struct esd_usb_rx_msg {
118-
u8 len;
118+
u8 len; /* total message length in 32bit words */
119119
u8 cmd;
120120
u8 net;
121121
u8 dlc;
@@ -133,7 +133,7 @@ struct esd_usb_rx_msg {
133133
};
134134

135135
struct esd_usb_tx_msg {
136-
u8 len;
136+
u8 len; /* total message length in 32bit words */
137137
u8 cmd;
138138
u8 net;
139139
u8 dlc;
@@ -143,7 +143,7 @@ struct esd_usb_tx_msg {
143143
};
144144

145145
struct esd_usb_tx_done_msg {
146-
u8 len;
146+
u8 len; /* total message length in 32bit words */
147147
u8 cmd;
148148
u8 net;
149149
u8 status;
@@ -152,15 +152,15 @@ struct esd_usb_tx_done_msg {
152152
};
153153

154154
struct esd_usb_id_filter_msg {
155-
u8 len;
155+
u8 len; /* total message length in 32bit words */
156156
u8 cmd;
157157
u8 net;
158158
u8 option;
159-
__le32 mask[ESD_USB_MAX_ID_SEGMENT + 1];
159+
__le32 mask[ESD_USB_MAX_ID_SEGMENT + 1]; /* +1 for 29bit extended IDs */
160160
};
161161

162162
struct esd_usb_set_baudrate_msg {
163-
u8 len;
163+
u8 len; /* total message length in 32bit words */
164164
u8 cmd;
165165
u8 net;
166166
u8 rsvd;
@@ -438,7 +438,7 @@ static void esd_usb_read_bulk_callback(struct urb *urb)
438438
break;
439439
}
440440

441-
pos += msg->hdr.len << 2;
441+
pos += msg->hdr.len * sizeof(u32); /* convert to # of bytes */
442442

443443
if (pos > urb->actual_length) {
444444
dev_err(dev->udev->dev.parent, "format error\n");
@@ -532,7 +532,7 @@ static int esd_usb_send_msg(struct esd_usb *dev, union esd_usb_msg *msg)
532532
return usb_bulk_msg(dev->udev,
533533
usb_sndbulkpipe(dev->udev, 2),
534534
msg,
535-
msg->hdr.len << 2,
535+
msg->hdr.len * sizeof(u32), /* convert to # of bytes */
536536
&actual_length,
537537
1000);
538538
}
@@ -648,7 +648,7 @@ static int esd_usb_start(struct esd_usb_net_priv *priv)
648648
* field followed by only some bitmasks.
649649
*/
650650
msg->hdr.cmd = ESD_USB_CMD_IDADD;
651-
msg->hdr.len = 2 + ESD_USB_MAX_ID_SEGMENT;
651+
msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32); /* # of 32bit words */
652652
msg->filter.net = priv->index;
653653
msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */
654654
for (i = 0; i < ESD_USB_MAX_ID_SEGMENT; i++)
@@ -759,7 +759,8 @@ static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
759759

760760
msg = (union esd_usb_msg *)buf;
761761

762-
msg->hdr.len = 3; /* minimal length */
762+
/* minimal length as # of 32bit words */
763+
msg->hdr.len = offsetof(struct esd_usb_tx_msg, data) / sizeof(u32);
763764
msg->hdr.cmd = ESD_USB_CMD_CAN_TX;
764765
msg->tx.net = priv->index;
765766
msg->tx.dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
@@ -774,7 +775,8 @@ static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
774775
for (i = 0; i < cf->len; i++)
775776
msg->tx.data[i] = cf->data[i];
776777

777-
msg->hdr.len += (cf->len + 3) >> 2;
778+
/* round up, then divide by 4 to add the payload length as # of 32bit words */
779+
msg->hdr.len += DIV_ROUND_UP(cf->len, sizeof(u32));
778780

779781
for (i = 0; i < ESD_USB_MAX_TX_URBS; i++) {
780782
if (priv->tx_contexts[i].echo_index == ESD_USB_MAX_TX_URBS) {
@@ -797,7 +799,7 @@ static netdev_tx_t esd_usb_start_xmit(struct sk_buff *skb,
797799
msg->tx.hnd = 0x80000000 | i; /* returned in TX done message */
798800

799801
usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
800-
msg->hdr.len << 2,
802+
msg->hdr.len * sizeof(u32), /* convert to # of bytes */
801803
esd_usb_write_bulk_callback, context);
802804

803805
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
@@ -860,7 +862,7 @@ static int esd_usb_close(struct net_device *netdev)
860862

861863
/* Disable all IDs (see esd_usb_start()) */
862864
msg->hdr.cmd = ESD_USB_CMD_IDADD;
863-
msg->hdr.len = 2 + ESD_USB_MAX_ID_SEGMENT;
865+
msg->hdr.len = sizeof(struct esd_usb_id_filter_msg) / sizeof(u32);/* # of 32bit words */
864866
msg->filter.net = priv->index;
865867
msg->filter.option = ESD_USB_ID_ENABLE; /* start with segment 0 */
866868
for (i = 0; i <= ESD_USB_MAX_ID_SEGMENT; i++)
@@ -869,7 +871,7 @@ static int esd_usb_close(struct net_device *netdev)
869871
netdev_err(netdev, "sending idadd message failed\n");
870872

871873
/* set CAN controller to reset mode */
872-
msg->hdr.len = 2;
874+
msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */
873875
msg->hdr.cmd = ESD_USB_CMD_SETBAUD;
874876
msg->setbaud.net = priv->index;
875877
msg->setbaud.rsvd = 0;
@@ -947,7 +949,7 @@ static int esd_usb_2_set_bittiming(struct net_device *netdev)
947949
if (!msg)
948950
return -ENOMEM;
949951

950-
msg->hdr.len = 2;
952+
msg->hdr.len = sizeof(struct esd_usb_set_baudrate_msg) / sizeof(u32); /* # of 32bit words */
951953
msg->hdr.cmd = ESD_USB_CMD_SETBAUD;
952954
msg->setbaud.net = priv->index;
953955
msg->setbaud.rsvd = 0;
@@ -1086,7 +1088,7 @@ static int esd_usb_probe(struct usb_interface *intf,
10861088

10871089
/* query number of CAN interfaces (nets) */
10881090
msg->hdr.cmd = ESD_USB_CMD_VERSION;
1089-
msg->hdr.len = 2;
1091+
msg->hdr.len = sizeof(struct esd_usb_version_msg) / sizeof(u32); /* # of 32bit words */
10901092
msg->version.rsvd = 0;
10911093
msg->version.flags = 0;
10921094
msg->version.drv_version = 0;

0 commit comments

Comments
 (0)