Skip to contentSkip to conversation

usbnet private-data accessor functions

PATCH In review Ben Dooks 4 participants 18 messages Oct 12, 2018

Subsystems (8)
lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

I have been looking at the usbnet drivers and the possibility of some code cleanups. One of the things I've found is that changing the way the drivers use the private data with the usbnet structure is often hand-coded each time is needed.

An easy cleanp (and making it easier in the future to change the access method) would be to add an inline conversion function to each driver so that it is done in one place.

Future work would be to look at the usbnet.data and see if it could be changed (such as moving to a union, allocation of the data after the usbnet structure, or similar).

author
#1 lore raw

PATCH 1/7 net: smsc75xx: add usbnet -> priv function

There are a number of places in the smsc75xx driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/smsc75xx.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
drivers/net/usb/smsc75xx.c +18 -13
index 05553d252446..6d12fcd9b4b0 100644
@@ -73,6 +73,11 @@ struct smsc75xx_priv {
 	u8 suspend_flags;
 };
 
+static inline struct smsc75xx_priv *usbnet_to_smsc(struct usbnet *dev)
+{
+	return (struct smsc75xx_priv *)dev->data[0];
+}
+
 struct usb_context {
 	struct usb_ctrlrequest req;
 	struct usbnet *dev;
@@ -469,7 +474,7 @@ static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
 static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
 				   u32 length, u32 *buf)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 dp_sel;
 	int i, ret;
 
@@ -553,7 +558,7 @@ static void smsc75xx_deferred_multicast_write(struct work_struct *param)
 static void smsc75xx_set_multicast(struct net_device *netdev)
 {
 	struct usbnet *dev = netdev_priv(netdev);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	unsigned long flags;
 	int i;
 
@@ -718,7 +723,7 @@ static void smsc75xx_ethtool_get_wol(struct net_device *net,
 				     struct ethtool_wolinfo *wolinfo)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 
 	wolinfo->supported = SUPPORTED_WAKE;
 	wolinfo->wolopts = pdata->wolopts;
@@ -728,7 +733,7 @@ static int smsc75xx_ethtool_set_wol(struct net_device *net,
 				    struct ethtool_wolinfo *wolinfo)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	int ret;
 
 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
@@ -945,7 +950,7 @@ static int smsc75xx_set_features(struct net_device *netdev,
 	netdev_features_t features)
 {
 	struct usbnet *dev = netdev_priv(netdev);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	unsigned long flags;
 	int ret;
 
@@ -1051,7 +1056,7 @@ static int smsc75xx_phy_gig_workaround(struct usbnet *dev)
 
 static int smsc75xx_reset(struct usbnet *dev)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 buf;
 	int ret = 0, timeout;
 
@@ -1515,7 +1520,7 @@ static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
 
 static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	if (pdata) {
 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
 		kfree(pdata);
@@ -1571,7 +1576,7 @@ static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
 
 static int smsc75xx_enter_suspend0(struct usbnet *dev)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 val;
 	int ret;
 
@@ -1597,7 +1602,7 @@ static int smsc75xx_enter_suspend0(struct usbnet *dev)
 
 static int smsc75xx_enter_suspend1(struct usbnet *dev)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 val;
 	int ret;
 
@@ -1633,7 +1638,7 @@ static int smsc75xx_enter_suspend1(struct usbnet *dev)
 
 static int smsc75xx_enter_suspend2(struct usbnet *dev)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 val;
 	int ret;
 
@@ -1659,7 +1664,7 @@ static int smsc75xx_enter_suspend2(struct usbnet *dev)
 
 static int smsc75xx_enter_suspend3(struct usbnet *dev)
 {
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 val;
 	int ret;
 
@@ -1794,7 +1799,7 @@ static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
 static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u32 val, link_up;
 	int ret;
 
@@ -2095,7 +2100,7 @@ static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
 static int smsc75xx_resume(struct usb_interface *intf)
 {
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
+	struct smsc75xx_priv *pdata = usbnet_to_smsc(dev);
 	u8 suspend_flags = pdata->suspend_flags;
 	int ret;
 	u32 val;
maintainer Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] net: smsc75xx: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the smsc75xx driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

author
#3 lore raw

PATCH 2/7 net: asix: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the asix driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/asix.h         |  5 +++++
 drivers/net/usb/asix_common.c  |  4 ++--
 drivers/net/usb/asix_devices.c | 16 ++++++++--------
 drivers/net/usb/ax88172a.c     |  2 +-
 4 files changed, 16 insertions(+), 11 deletions(-)
drivers/net/usb/asix.h +5 -0
index 9a4171b90947..4bbb52669ac4 100644
@@ -197,6 +197,11 @@ extern const struct driver_info ax88172a_info;
 /* ASIX specific flags */
 #define FLAG_EEPROM_MAC		(1UL << 0)  /* init device MAC from eeprom */
 
+static inline struct asix_data *usbnet_to_asix(struct usbnet *usb)
+{
+	return (struct asix_data *)&usb->data;
+}
+
 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
 		  u16 size, void *data, int in_pm);
 
drivers/net/usb/asix_common.c +2 -2
index e95dd12edec4..1fd650faca5b 100644
@@ -418,7 +418,7 @@ int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
 void asix_set_multicast(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
 
 	if (net->flags & IFF_PROMISC) {
@@ -751,7 +751,7 @@ void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
 int asix_set_mac_address(struct net_device *net, void *p)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	struct sockaddr *addr = p;
 
 	if (netif_running(net))
drivers/net/usb/asix_devices.c +8 -8
index b654f05b2ccd..8e387f06dccf 100644
@@ -144,7 +144,7 @@ static const struct ethtool_ops ax88172_ethtool_ops = {
 static void ax88172_set_multicast(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	u8 rx_ctl = 0x8c;
 
 	if (net->flags & IFF_PROMISC) {
@@ -332,7 +332,7 @@ static int ax88772_link_reset(struct usbnet *dev)
 
 static int ax88772_reset(struct usbnet *dev)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	int ret;
 
 	/* Rewrite MAC address */
@@ -359,7 +359,7 @@ static int ax88772_reset(struct usbnet *dev)
 
 static int ax88772_hw_reset(struct usbnet *dev, int in_pm)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	int ret, embd_phy;
 	u16 rx_ctl;
 
@@ -454,7 +454,7 @@ static int ax88772_hw_reset(struct usbnet *dev, int in_pm)
 
 static int ax88772a_hw_reset(struct usbnet *dev, int in_pm)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	int ret, embd_phy;
 	u16 rx_ctl, phy14h, phy15h, phy16h;
 	u8 chipcode = 0;
@@ -795,7 +795,7 @@ static const struct ethtool_ops ax88178_ethtool_ops = {
 
 static int marvell_phy_init(struct usbnet *dev)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	u16 reg;
 
 	netdev_dbg(dev->net, "marvell_phy_init()\n");
@@ -827,7 +827,7 @@ static int marvell_phy_init(struct usbnet *dev)
 
 static int rtl8211cl_phy_init(struct usbnet *dev)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 
 	netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
 
@@ -874,7 +874,7 @@ static int marvell_led_status(struct usbnet *dev, u16 speed)
 
 static int ax88178_reset(struct usbnet *dev)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	int ret;
 	__le16 eeprom;
 	u8 status;
@@ -962,7 +962,7 @@ static int ax88178_link_reset(struct usbnet *dev)
 {
 	u16 mode;
 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	u32 speed;
 
 	netdev_dbg(dev->net, "ax88178_link_reset()\n");
drivers/net/usb/ax88172a.c +1 -1
index 501576f53854..08f33c89f002 100644
@@ -289,7 +289,7 @@ static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
 
 static int ax88172a_reset(struct usbnet *dev)
 {
-	struct asix_data *data = (struct asix_data *)&dev->data;
+	struct asix_data *data = usbnet_to_asix(dev);
 	struct ax88172a_private *priv = dev->driver_priv;
 	int ret;
 	u16 rx_ctl;
maintainer Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] net: asix: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the asix driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

author
#5 lore raw

PATCH 3/7 net: usb: asix88179_178a: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the asix88179_178a driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/ax88179_178a.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/net/usb/ax88179_178a.c +12 -7
index 9e8ad372f419..f4b64e7e7706 100644
@@ -196,6 +196,11 @@ static const struct {
 	{7, 0xcc, 0x4c, 0x18, 8},
 };
 
+static inline struct ax88179_data *usbnet_to_ax(struct usbnet *usb)
+{
+	return (struct ax88179_data *)usb->data;
+}
+
 static int __ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
 			      u16 size, void *data, int in_pm)
 {
@@ -678,7 +683,7 @@ ax88179_ethtool_set_eee(struct usbnet *dev, struct ethtool_eee *data)
 static int ax88179_chk_eee(struct usbnet *dev)
 {
 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
-	struct ax88179_data *priv = (struct ax88179_data *)dev->data;
+	struct ax88179_data *priv = usbnet_to_ax(dev);
 
 	mii_ethtool_gset(&dev->mii, &ecmd);
 
@@ -781,7 +786,7 @@ static void ax88179_enable_eee(struct usbnet *dev)
 static int ax88179_get_eee(struct net_device *net, struct ethtool_eee *edata)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct ax88179_data *priv = (struct ax88179_data *)dev->data;
+	struct ax88179_data *priv = usbnet_to_ax(dev);
 
 	edata->eee_enabled = priv->eee_enabled;
 	edata->eee_active = priv->eee_active;
@@ -792,7 +797,7 @@ static int ax88179_get_eee(struct net_device *net, struct ethtool_eee *edata)
 static int ax88179_set_eee(struct net_device *net, struct ethtool_eee *edata)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct ax88179_data *priv = (struct ax88179_data *)dev->data;
+	struct ax88179_data *priv = usbnet_to_ax(dev);
 	int ret = -EOPNOTSUPP;
 
 	priv->eee_enabled = edata->eee_enabled;
@@ -841,7 +846,7 @@ static const struct ethtool_ops ax88179_ethtool_ops = {
 static void ax88179_set_multicast(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct ax88179_data *data = (struct ax88179_data *)dev->data;
+	struct ax88179_data *data = usbnet_to_ax(dev);
 	u8 *m_filter = ((u8 *)dev->data) + 12;
 
 	data->rxctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_CTL_IPE);
@@ -1228,7 +1233,7 @@ static int ax88179_bind(struct usbnet *dev, struct usb_interface *intf)
 	u8 buf[5];
 	u16 *tmp16;
 	u8 *tmp;
-	struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
+	struct ax88179_data *ax179_data = usbnet_to_ax(dev);
 	struct ethtool_eee eee_data;
 
 	usbnet_get_endpoints(dev, intf);
@@ -1458,7 +1463,7 @@ ax88179_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
 
 static int ax88179_link_reset(struct usbnet *dev)
 {
-	struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
+	struct ax88179_data *ax179_data = usbnet_to_ax(dev);
 	u8 tmp[5], link_sts;
 	u16 mode, tmp16, delay = HZ / 10;
 	u32 tmp32 = 0x40000000;
@@ -1533,7 +1538,7 @@ static int ax88179_reset(struct usbnet *dev)
 	u8 buf[5];
 	u16 *tmp16;
 	u8 *tmp;
-	struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
+	struct ax88179_data *ax179_data = usbnet_to_ax(dev);
 	struct ethtool_eee eee_data;
 
 	tmp16 = (u16 *)buf;
maintainer Reviewed-by on v1 3/7
#6 lore raw

Re: [PATCH 3/7] net: usb: asix88179_178a: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the asix88179_178a driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

author
#7 lore raw

PATCH 4/7 net: qmi_wwan: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the qmi_wwan driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/qmi_wwan.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
drivers/net/usb/qmi_wwan.c +18 -13
index 533b6fb8d923..45930758a945 100644
@@ -76,6 +76,11 @@ struct qmimux_priv {
 	u8 mux_id;
 };
 
+static inline struct qmi_wwan_state *usbnet_to_qmi(struct usbnet *usb)
+{
+	return (void *) &usb->data;
+}
+
 static int qmimux_open(struct net_device *dev)
 {
 	struct qmimux_priv *priv = netdev_priv(dev);
@@ -253,7 +258,7 @@ static void qmimux_unregister_device(struct net_device *dev)
 static void qmi_wwan_netdev_setup(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 
 	if (info->flags & QMI_WWAN_FLAG_RAWIP) {
 		net->header_ops      = NULL;  /* No header */
@@ -276,7 +281,7 @@ static void qmi_wwan_netdev_setup(struct net_device *net)
 static ssize_t raw_ip_show(struct device *d, struct device_attribute *attr, char *buf)
 {
 	struct usbnet *dev = netdev_priv(to_net_dev(d));
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 
 	return sprintf(buf, "%c\n", info->flags & QMI_WWAN_FLAG_RAWIP ? 'Y' : 'N');
 }
@@ -284,7 +289,7 @@ static ssize_t raw_ip_show(struct device *d, struct device_attribute *attr, char
 static ssize_t raw_ip_store(struct device *d,  struct device_attribute *attr, const char *buf, size_t len)
 {
 	struct usbnet *dev = netdev_priv(to_net_dev(d));
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	bool enable;
 	int ret;
 
@@ -346,7 +351,7 @@ static ssize_t add_mux_show(struct device *d, struct device_attribute *attr, cha
 static ssize_t add_mux_store(struct device *d,  struct device_attribute *attr, const char *buf, size_t len)
 {
 	struct usbnet *dev = netdev_priv(to_net_dev(d));
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	u8 mux_id;
 	int ret;
 
@@ -391,7 +396,7 @@ static ssize_t del_mux_show(struct device *d, struct device_attribute *attr, cha
 static ssize_t del_mux_store(struct device *d,  struct device_attribute *attr, const char *buf, size_t len)
 {
 	struct usbnet *dev = netdev_priv(to_net_dev(d));
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	struct net_device *del_dev;
 	u8 mux_id;
 	int ret = 0;
@@ -468,7 +473,7 @@ static const u8 buggy_fw_addr[ETH_ALEN] = {0x00, 0xa0, 0xc6, 0x00, 0x00, 0x00};
  */
 static int qmi_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
 {
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	bool rawip = info->flags & QMI_WWAN_FLAG_RAWIP;
 	__be16 proto;
 
@@ -555,7 +560,7 @@ static const struct net_device_ops qmi_wwan_netdev_ops = {
  */
 static int qmi_wwan_manage_power(struct usbnet *dev, int on)
 {
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	int rv;
 
 	dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__,
@@ -589,7 +594,7 @@ static int qmi_wwan_register_subdriver(struct usbnet *dev)
 {
 	int rv;
 	struct usb_driver *subdriver = NULL;
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 
 	/* collect bulk endpoints */
 	rv = usbnet_get_endpoints(dev, info->data);
@@ -651,7 +656,7 @@ static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
 	struct usb_cdc_union_desc *cdc_union;
 	struct usb_cdc_ether_desc *cdc_ether;
 	struct usb_driver *driver = driver_of(intf);
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	struct usb_cdc_parsed_header hdr;
 
 	BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) <
@@ -746,7 +751,7 @@ static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
 
 static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
 {
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	struct usb_driver *driver = driver_of(intf);
 	struct usb_interface *other;
 
@@ -785,7 +790,7 @@ static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
 static int qmi_wwan_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	int ret;
 
 	/* Both usbnet_suspend() and subdriver->suspend() MUST return 0
@@ -808,7 +813,7 @@ static int qmi_wwan_suspend(struct usb_interface *intf, pm_message_t message)
 static int qmi_wwan_resume(struct usb_interface *intf)
 {
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct qmi_wwan_state *info = (void *)&dev->data;
+	struct qmi_wwan_state *info = usbnet_to_qmi(dev);
 	int ret = 0;
 	bool callsub = (intf == info->control && info->subdriver &&
 			info->subdriver->resume);
@@ -1406,7 +1411,7 @@ static void qmi_wwan_disconnect(struct usb_interface *intf)
 	/* called twice if separate control and data intf */
 	if (!dev)
 		return;
-	info = (void *)&dev->data;
+	info = usbnet_to_qmi(dev);
 	if (info->flags & QMI_WWAN_FLAG_MUX) {
 		if (!rtnl_trylock()) {
 			restart_syscall();
maintainer Reviewed-by on v1 4/7
#8 lore raw

Re: [PATCH 4/7] net: qmi_wwan: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the qmi_wwan driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

maintainer
#9 lore raw

Re: [PATCH 4/7] net: qmi_wwan: add usbnet -> priv function

In reply to Ben Dooks

drivers/net/usb/qmi_wwan.c struct qmimux_priv { · line 82

Quoted 4 lines · Ben Dooks
drivers/net/usb/qmi_wwan.c +4 -0
+static inline struct qmi_wwan_state *usbnet_to_qmi(struct usbnet *usb)
+{
+	return (void *) &usb->data;
+}

checkpatch doesn't like this, and it is also inconsistent with the coding style elsewhere in the driver.

CHECK: No space is necessary after a cast
#30: FILE: drivers/net/usb/qmi_wwan.c:81:
+ return (void *) &usb->data;

total: 0 errors, 0 warnings, 1 checks, 115 lines checked

And as for consistency: I realize that "dev" is a very generic name, but it's used consistendly for all struct usbnet pointers in the driver:

bjorn@miraculix:/usr/local/src/git/linux$ git grep 'struct usbnet' drivers/net/usb/qmi_wwan.c
drivers/net/usb/qmi_wwan.c:static struct net_device *qmimux_find_dev(struct usbnet *dev, u8 mux_id)
drivers/net/usb/qmi_wwan.c:static bool qmimux_has_slaves(struct usbnet *dev)
drivers/net/usb/qmi_wwan.c:static int qmimux_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(net);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_manage_power(struct usbnet *dev, int on)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_register_subdriver(struct usbnet *dev)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_change_dtr(struct usbnet *dev, bool on)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
drivers/net/usb/qmi_wwan.c:     BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) <
drivers/net/usb/qmi_wwan.c:static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);

The name was chosen to be consistent with the cdc_ether usage. I'd prefer it if this function could use the "dev" name, to avoid confusing things unnecessarly with yet another generic name like "usb". Which isn't used anywhere else in the driver, and could just as easily refer to a struct usb_device or struct usb_interface.

(yes, I know there are a couple of "struct usb_device *dev" cases. But I'd rather see those renamed TBH)

I also don't see why this function should be qmi_wwan specific. No need to duplicate the same function in every minidriver, when you can do with two generic helpers (maybe with more meaningful names):

static inline void *usbnet_priv(const struct usbnet *dev)

{
        return (void *)&dev->data;
}

static inline void *usbnet_priv0(const struct usbnet * *dev)

{
	return (void *)dev->data[0];
}

Please fix the checkpatch warning and consider the other comments.

Personally I don't really think it makes the code any easier to read. But if you want it, then I'm fine this. I guess I've grown too used to seeing those casts ;-)

Bjørn

author
#10 lore raw

Re: [PATCH 4/7] net: qmi_wwan: add usbnet -> priv function

Quoted 10 lines · Bjørn Mork

drivers/net/usb/qmi_wwan.c struct qmimux_priv { · line 82

Quoted 4 lines · Ben Dooks
drivers/net/usb/qmi_wwan.c +4 -0
+static inline struct qmi_wwan_state *usbnet_to_qmi(struct usbnet *usb)
+{
+	return (void *) &usb->data;
+}

checkpatch doesn't like this, and it is also inconsistent with the coding style elsewhere in the driver.

Thank you for pointing this out, will fix in v2.

Quoted 9 lines

CHECK: No space is necessary after a cast
#30: FILE: drivers/net/usb/qmi_wwan.c:81:
+ return (void *) &usb->data;

total: 0 errors, 0 warnings, 1 checks, 115 lines checked

And as for consistency: I realize that "dev" is a very generic name, but it's used consistendly for all struct usbnet pointers in the driver:

Ok, I'll change it.

Quoted 22 lines
bjorn@miraculix:/usr/local/src/git/linux$ git grep 'struct usbnet' drivers/net/usb/qmi_wwan.c
drivers/net/usb/qmi_wwan.c:static struct net_device *qmimux_find_dev(struct usbnet *dev, u8 mux_id)
drivers/net/usb/qmi_wwan.c:static bool qmimux_has_slaves(struct usbnet *dev)
drivers/net/usb/qmi_wwan.c:static int qmimux_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(net);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = netdev_priv(to_net_dev(d));
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_manage_power(struct usbnet *dev, int on)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_register_subdriver(struct usbnet *dev)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_change_dtr(struct usbnet *dev, bool on)
drivers/net/usb/qmi_wwan.c:static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
drivers/net/usb/qmi_wwan.c:     BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) <
drivers/net/usb/qmi_wwan.c:static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
drivers/net/usb/qmi_wwan.c:     struct usbnet *dev = usb_get_intfdata(intf);
Quoted 5 lines

The name was chosen to be consistent with the cdc_ether usage. I'd prefer it if this function could use the "dev" name, to avoid confusing things unnecessarly with yet another generic name like "usb". Which isn't used anywhere else in the driver, and could just as easily refer to a struct usb_device or struct usb_interface.

Ok, should be fairly easy to change this.

Quoted 2 lines

(yes, I know there are a couple of "struct usb_device *dev" cases. But I'd rather see those renamed TBH)

I'd rather not touch that at the moment, this is already gaining complexity.

Quoted 3 lines

I also don't see why this function should be qmi_wwan specific. No need to duplicate the same function in every minidriver, when you can do with two generic helpers (maybe with more meaningful names):

I personally prefer the return type to be specifically the private data type for the driver. It makes it a bit easier to spot when you don't get the cast right.

The functions below are the idea I am working towards for the usbnet driver, I was trying to avoid doing everything in one go. Making the accessor functions means the next round of changes can be much smaller, touching 1-2 areas per driver.

Quoted 9 lines

static inline void *usbnet_priv(const struct usbnet *dev)

{
         return (void *)&dev->data;
}

static inline void *usbnet_priv0(const struct usbnet * *dev)

{
	return (void *)dev->data[0];
}

This is probably the end-game of the patch series, just need to look at a couple of issues and see if there's anything better than can be done.

I might also add a usbnet_set_privdata(dev, data) or something like usbnet_alloc_privdata(dev, sizeof(data).

Note, there's also the fun here of the usbnet having private data for itself, and these sub-drivers also having their own private data... this probably needs to be named carefully.

Quoted 5 lines

Please fix the checkpatch warning and consider the other comments.

Personally I don't really think it makes the code any easier to read. But if you want it, then I'm fine this. I guess I've grown too used to seeing those casts ;-)

author
#11 lore raw

PATCH 5/7 net: cdc_ether: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the cdc drivers where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/cdc_ether.c  |  8 ++---
 drivers/net/usb/cdc_mbim.c   | 23 ++++++++------
 drivers/net/usb/cdc_ncm.c    | 61 +++++++++++++++++++-----------------
 drivers/net/usb/rndis_host.c |  6 ++--
 include/linux/usb/usbnet.h   |  5 +++
 5 files changed, 59 insertions(+), 44 deletions(-)
drivers/net/usb/cdc_ether.c +4 -4
index 5c42cf81a08b..7fee0ebc1943 100644
@@ -77,7 +77,7 @@ static const u8 mbm_guid[16] = {
 
 static void usbnet_cdc_update_filter(struct usbnet *dev)
 {
-	struct cdc_state	*info = (void *) &dev->data;
+	struct cdc_state	*info = usbnet_to_cdc(dev);
 	struct usb_interface	*intf = info->control;
 	struct net_device	*net = dev->net;
 
@@ -115,7 +115,7 @@ int usbnet_generic_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
 	u8				*buf = intf->cur_altsetting->extra;
 	int				len = intf->cur_altsetting->extralen;
 	struct usb_interface_descriptor	*d;
-	struct cdc_state		*info = (void *) &dev->data;
+	struct cdc_state		*info = usbnet_to_cdc(dev);
 	int				status;
 	int				rndis;
 	bool				android_rndis_quirk = false;
@@ -353,7 +353,7 @@ EXPORT_SYMBOL_GPL(usbnet_ether_cdc_bind);
 
 void usbnet_cdc_unbind(struct usbnet *dev, struct usb_interface *intf)
 {
-	struct cdc_state		*info = (void *) &dev->data;
+	struct cdc_state		*info = usbnet_to_cdc(dev);
 	struct usb_driver		*driver = driver_of(intf);
 
 	/* combined interface - nothing  to do */
@@ -438,7 +438,7 @@ EXPORT_SYMBOL_GPL(usbnet_cdc_status);
 int usbnet_cdc_bind(struct usbnet *dev, struct usb_interface *intf)
 {
 	int				status;
-	struct cdc_state		*info = (void *) &dev->data;
+	struct cdc_state		*info = usbnet_to_cdc(dev);
 
 	BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data)
 			< sizeof(struct cdc_state)));
drivers/net/usb/cdc_mbim.c +14 -9
index 0362acd5cdca..aec8f8eb21a7 100644
@@ -36,6 +36,11 @@ struct cdc_mbim_state {
 	unsigned long flags;
 };
 
+static inline struct cdc_mbim_state *usbnet_to_mbim(struct usbnet *usb)
+{
+	return (void *)&usb->data;
+}
+
 /* flags for the cdc_mbim_state.flags field */
 enum cdc_mbim_flags {
 	FLAG_IPS0_VLAN = 1 << 0,	/* IP session 0 is tagged  */
@@ -44,7 +49,7 @@ enum cdc_mbim_flags {
 /* using a counter to merge subdriver requests with our own into a combined state */
 static int cdc_mbim_manage_power(struct usbnet *dev, int on)
 {
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	int rv = 0;
 
 	dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__, atomic_read(&info->pmcount), on);
@@ -73,7 +78,7 @@ static int cdc_mbim_wdm_manage_power(struct usb_interface *intf, int status)
 static int cdc_mbim_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
 {
 	struct usbnet *dev = netdev_priv(netdev);
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 
 	/* creation of this VLAN is a request to tag IP session 0 */
 	if (vid == MBIM_IPS0_VID)
@@ -87,7 +92,7 @@ static int cdc_mbim_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
 static int cdc_mbim_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
 {
 	struct usbnet *dev = netdev_priv(netdev);
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 
 	/* this is a request for an untagged IP session 0 */
 	if (vid == MBIM_IPS0_VID)
@@ -144,7 +149,7 @@ static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
 	struct usb_driver *subdriver = ERR_PTR(-ENODEV);
 	int ret = -ENODEV;
 	u8 data_altsetting = 1;
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 
 	/* should we change control altsetting on a NCM/MBIM function? */
 	if (cdc_ncm_select_altsetting(intf) == CDC_NCM_COMM_ALTSETTING_MBIM) {
@@ -195,7 +200,7 @@ static int cdc_mbim_bind(struct usbnet *dev, struct usb_interface *intf)
 
 static void cdc_mbim_unbind(struct usbnet *dev, struct usb_interface *intf)
 {
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	struct cdc_ncm_ctx *ctx = info->ctx;
 
 	/* disconnect subdriver from control interface */
@@ -221,7 +226,7 @@ static bool is_ip_proto(__be16 proto)
 static struct sk_buff *cdc_mbim_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
 {
 	struct sk_buff *skb_out;
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	struct cdc_ncm_ctx *ctx = info->ctx;
 	__le32 sign = cpu_to_le32(USB_CDC_MBIM_NDP16_IPS_SIGN);
 	u16 tci = 0;
@@ -411,7 +416,7 @@ static struct sk_buff *cdc_mbim_process_dgram(struct usbnet *dev, u8 *buf, size_
 static int cdc_mbim_rx_fixup(struct usbnet *dev, struct sk_buff *skb_in)
 {
 	struct sk_buff *skb;
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	struct cdc_ncm_ctx *ctx = info->ctx;
 	int len;
 	int nframes;
@@ -506,7 +511,7 @@ static int cdc_mbim_suspend(struct usb_interface *intf, pm_message_t message)
 {
 	int ret = -ENODEV;
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	struct cdc_ncm_ctx *ctx = info->ctx;
 
 	if (!ctx)
@@ -534,7 +539,7 @@ static int cdc_mbim_resume(struct usb_interface *intf)
 {
 	int  ret = 0;
 	struct usbnet *dev = usb_get_intfdata(intf);
-	struct cdc_mbim_state *info = (void *)&dev->data;
+	struct cdc_mbim_state *info = usbnet_to_mbim(dev);
 	struct cdc_ncm_ctx *ctx = info->ctx;
 	bool callsub = (intf == ctx->control && info->subdriver && info->subdriver->resume);
 
drivers/net/usb/cdc_ncm.c +33 -28

235 lines not on this page · raw message

drivers/net/usb/rndis_host.c +3 -3
index b807c91abe1d..a24af05a74fb 100644
@@ -69,7 +69,7 @@ EXPORT_SYMBOL_GPL(rndis_status);
 static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg,
 				int buflen)
 {
-	struct cdc_state *info = (void *)&dev->data;
+	struct cdc_state *info = usbnet_to_cdc(dev);
 	struct device *udev = &info->control->dev;
 
 	if (dev->driver_info->indication) {
@@ -102,7 +102,7 @@ static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg,
  */
 int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen)
 {
-	struct cdc_state	*info = (void *) &dev->data;
+	struct cdc_state	*info = usbnet_to_cdc(dev);
 	struct usb_cdc_notification notification;
 	int			master_ifnum;
 	int			retval;
@@ -301,7 +301,7 @@ generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
 {
 	int			retval;
 	struct net_device	*net = dev->net;
-	struct cdc_state	*info = (void *) &dev->data;
+	struct cdc_state	*info = usbnet_to_cdc(dev);
 	union {
 		void			*buf;
 		struct rndis_msg_hdr	*header;
include/linux/usb/usbnet.h +5 -0
index e2ec3582e549..cdb54dd3a4b4 100644
@@ -207,6 +207,11 @@ struct cdc_state {
 	struct usb_interface		*data;
 };
 
+static inline struct cdc_state *usbnet_to_cdc(struct usbnet *net)
+{
+	return (void *) &net->data;
+}
+
 extern int usbnet_generic_cdc_bind(struct usbnet *, struct usb_interface *);
 extern int usbnet_ether_cdc_bind(struct usbnet *dev, struct usb_interface *intf);
 extern int usbnet_cdc_bind(struct usbnet *, struct usb_interface *);
maintainer Reviewed-by on v1 5/7
#12 lore raw

Re: [PATCH 5/7] net: cdc_ether: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the cdc drivers where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

author
#13 lore raw

PATCH 6/7 net: huawei_cdc_ncm: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the huawei driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---

---
 drivers/net/usb/huawei_cdc_ncm.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/net/usb/huawei_cdc_ncm.c +10 -5
index 63f28908afda..d290b8c318be 100644
@@ -38,9 +38,14 @@ struct huawei_cdc_ncm_state {
 	struct usb_interface *data;
 };
 
+static inline struct huawei_cdc_ncm_state *usbnet_to_state(struct usbnet *usb)
+{
+	return (struct huawei_cdc_ncm_state *)&usb->data;
+}
+
 static int huawei_cdc_ncm_manage_power(struct usbnet *usbnet_dev, int on)
 {
-	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct huawei_cdc_ncm_state *drvstate = usbnet_to_state(usbnet_dev);
 	int rv;
 
 	if ((on && atomic_add_return(1, &drvstate->pmcount) == 1) ||
@@ -72,7 +77,7 @@ static int huawei_cdc_ncm_bind(struct usbnet *usbnet_dev,
 	struct cdc_ncm_ctx *ctx;
 	struct usb_driver *subdriver = ERR_PTR(-ENODEV);
 	int ret = -ENODEV;
-	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct huawei_cdc_ncm_state *drvstate = usbnet_to_state(usbnet_dev);
 	int drvflags = 0;
 
 	/* altsetting should always be 1 for NCM devices - so we hard-coded
@@ -119,7 +124,7 @@ static int huawei_cdc_ncm_bind(struct usbnet *usbnet_dev,
 static void huawei_cdc_ncm_unbind(struct usbnet *usbnet_dev,
 				  struct usb_interface *intf)
 {
-	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct huawei_cdc_ncm_state *drvstate = usbnet_to_state(usbnet_dev);
 	struct cdc_ncm_ctx *ctx = drvstate->ctx;
 
 	if (drvstate->subdriver && drvstate->subdriver->disconnect)
@@ -134,7 +139,7 @@ static int huawei_cdc_ncm_suspend(struct usb_interface *intf,
 {
 	int ret = 0;
 	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
-	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct huawei_cdc_ncm_state *drvstate = usbnet_to_state(usbnet_dev);
 	struct cdc_ncm_ctx *ctx = drvstate->ctx;
 
 	if (ctx == NULL) {
@@ -161,7 +166,7 @@ static int huawei_cdc_ncm_resume(struct usb_interface *intf)
 {
 	int ret = 0;
 	struct usbnet *usbnet_dev = usb_get_intfdata(intf);
-	struct huawei_cdc_ncm_state *drvstate = (void *)&usbnet_dev->data;
+	struct huawei_cdc_ncm_state *drvstate = usbnet_to_state(usbnet_dev);
 	bool callsub;
 	struct cdc_ncm_ctx *ctx = drvstate->ctx;
 
maintainer Reviewed-by on v1 6/7
#14 lore raw

Re: [PATCH 6/7] net: huawei_cdc_ncm: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the huawei driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

author
#15 lore raw

PATCH 7/7 net: usb: sr9800: add usbnet -> priv function

In reply to Ben Dooks

There are a number of places in the sr8900 driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

---
 drivers/net/usb/sr9800.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/net/usb/sr9800.c +10 -5
index 9277a0f228df..2093ecfff5a5 100644
@@ -25,6 +25,11 @@
 
 #include "sr9800.h"
 
+static inline struct sr_data *usbnet_to_sr(struct usbnet *usb)
+{
+	return (struct sr_data *)&usb->data;
+}
+
 static int sr_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
 			    u16 size, void *data)
 {
@@ -296,7 +301,7 @@ static int sr_write_gpio(struct usbnet *dev, u16 value, int sleep)
 static void sr_set_multicast(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct sr_data *data = (struct sr_data *)&dev->data;
+	struct sr_data *data = usbnet_to_sr(dev);
 	u16 rx_ctl = SR_DEFAULT_RX_CTL;
 
 	if (net->flags & IFF_PROMISC) {
@@ -436,7 +441,7 @@ sr_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
 static int sr_get_eeprom_len(struct net_device *net)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct sr_data *data = (struct sr_data *)&dev->data;
+	struct sr_data *data = usbnet_to_sr(dev);
 
 	return data->eeprom_len;
 }
@@ -493,7 +498,7 @@ static int sr_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
 static int sr_set_mac_address(struct net_device *net, void *p)
 {
 	struct usbnet *dev = netdev_priv(net);
-	struct sr_data *data = (struct sr_data *)&dev->data;
+	struct sr_data *data = usbnet_to_sr(dev);
 	struct sockaddr *addr = p;
 
 	if (netif_running(net))
@@ -595,7 +600,7 @@ static int sr9800_set_default_mode(struct usbnet *dev)
 
 static int sr9800_reset(struct usbnet *dev)
 {
-	struct sr_data *data = (struct sr_data *)&dev->data;
+	struct sr_data *data = usbnet_to_sr(dev);
 	int ret, embd_phy;
 	u16 rx_ctl;
 
@@ -726,7 +731,7 @@ static int sr9800_phy_powerup(struct usbnet *dev)
 
 static int sr9800_bind(struct usbnet *dev, struct usb_interface *intf)
 {
-	struct sr_data *data = (struct sr_data *)&dev->data;
+	struct sr_data *data = usbnet_to_sr(dev);
 	u16 led01_mux, led23_mux;
 	int ret, embd_phy;
 	u32 phyid;
maintainer Reviewed-by on v1 7/7
#16 lore raw

Re: [PATCH 7/7] net: usb: sr9800: add usbnet -> priv function

Quoted 6 lines · Ben Dooks

There are a number of places in the sr8900 driver where it gets the private-data from the usbnet passed in. It would be sensible to have one inline function to convert it and change all points in the driver to use that.

maintainer
#17 lore raw
In reply to Ben Dooks
Quoted 4 lines · Ben Dooks

I have been looking at the usbnet drivers and the possibility of some code cleanups. One of the things I've found is that changing the way the drivers use the private data with the usbnet structure is often hand-coded each time is needed.

Where is the improvement? You are just hiding how the data is passed. It may look more pleasant to you, but that is a subjective impression. I would suggest that if you want a nicely named way to get at private data, add a field to the appropriate data structure.

Regards
Oliver