1 #include <linux/hardirq.h>
2 #include <linux/netdevice.h>
3 #include <linux/ethtool.h>
4 #include <linux/delay.h>
5 
6 #include "decl.h"
7 #include "cmd.h"
8 #include "mesh.h"
9 
10 
11 static void lbs_ethtool_get_drvinfo(struct net_device *dev,
12 					 struct ethtool_drvinfo *info)
13 {
14 	struct lbs_private *priv = dev->ml_priv;
15 
16 	snprintf(info->fw_version, sizeof(info->fw_version),
17 		"%u.%u.%u.p%u",
18 		priv->fwrelease >> 24 & 0xff,
19 		priv->fwrelease >> 16 & 0xff,
20 		priv->fwrelease >>  8 & 0xff,
21 		priv->fwrelease       & 0xff);
22 	strlcpy(info->driver, "libertas", sizeof(info->driver));
23 	strlcpy(info->version, lbs_driver_version, sizeof(info->version));
24 }
25 
26 /*
27  * All 8388 parts have 16KiB EEPROM size at the time of writing.
28  * In case that changes this needs fixing.
29  */
30 #define LBS_EEPROM_LEN 16384
31 
32 static int lbs_ethtool_get_eeprom_len(struct net_device *dev)
33 {
34 	return LBS_EEPROM_LEN;
35 }
36 
37 static int lbs_ethtool_get_eeprom(struct net_device *dev,
38                                   struct ethtool_eeprom *eeprom, u8 * bytes)
39 {
40 	struct lbs_private *priv = dev->ml_priv;
41 	struct cmd_ds_802_11_eeprom_access cmd;
42 	int ret;
43 
44 	if (eeprom->offset + eeprom->len > LBS_EEPROM_LEN ||
45 	    eeprom->len > LBS_EEPROM_READ_LEN) {
46 		ret = -EINVAL;
47 		goto out;
48 	}
49 
50 	cmd.hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_eeprom_access) -
51 		LBS_EEPROM_READ_LEN + eeprom->len);
52 	cmd.action = cpu_to_le16(CMD_ACT_GET);
53 	cmd.offset = cpu_to_le16(eeprom->offset);
54 	cmd.len    = cpu_to_le16(eeprom->len);
55 	ret = lbs_cmd_with_response(priv, CMD_802_11_EEPROM_ACCESS, &cmd);
56 	if (!ret)
57 		memcpy(bytes, cmd.value, eeprom->len);
58 
59 out:
60         return ret;
61 }
62 
63 static void lbs_ethtool_get_wol(struct net_device *dev,
64 				struct ethtool_wolinfo *wol)
65 {
66 	struct lbs_private *priv = dev->ml_priv;
67 
68 	wol->supported = WAKE_UCAST|WAKE_MCAST|WAKE_BCAST|WAKE_PHY;
69 
70 	if (priv->wol_criteria == EHS_REMOVE_WAKEUP)
71 		return;
72 
73 	if (priv->wol_criteria & EHS_WAKE_ON_UNICAST_DATA)
74 		wol->wolopts |= WAKE_UCAST;
75 	if (priv->wol_criteria & EHS_WAKE_ON_MULTICAST_DATA)
76 		wol->wolopts |= WAKE_MCAST;
77 	if (priv->wol_criteria & EHS_WAKE_ON_BROADCAST_DATA)
78 		wol->wolopts |= WAKE_BCAST;
79 	if (priv->wol_criteria & EHS_WAKE_ON_MAC_EVENT)
80 		wol->wolopts |= WAKE_PHY;
81 }
82 
83 static int lbs_ethtool_set_wol(struct net_device *dev,
84 			       struct ethtool_wolinfo *wol)
85 {
86 	struct lbs_private *priv = dev->ml_priv;
87 
88 	if (wol->wolopts & ~(WAKE_UCAST|WAKE_MCAST|WAKE_BCAST|WAKE_PHY))
89 		return -EOPNOTSUPP;
90 
91 	priv->wol_criteria = 0;
92 	if (wol->wolopts & WAKE_UCAST)
93 		priv->wol_criteria |= EHS_WAKE_ON_UNICAST_DATA;
94 	if (wol->wolopts & WAKE_MCAST)
95 		priv->wol_criteria |= EHS_WAKE_ON_MULTICAST_DATA;
96 	if (wol->wolopts & WAKE_BCAST)
97 		priv->wol_criteria |= EHS_WAKE_ON_BROADCAST_DATA;
98 	if (wol->wolopts & WAKE_PHY)
99 		priv->wol_criteria |= EHS_WAKE_ON_MAC_EVENT;
100 	if (wol->wolopts == 0)
101 		priv->wol_criteria |= EHS_REMOVE_WAKEUP;
102 	return 0;
103 }
104 
105 const struct ethtool_ops lbs_ethtool_ops = {
106 	.get_drvinfo = lbs_ethtool_get_drvinfo,
107 	.get_eeprom =  lbs_ethtool_get_eeprom,
108 	.get_eeprom_len = lbs_ethtool_get_eeprom_len,
109 #ifdef CONFIG_LIBERTAS_MESH
110 	.get_sset_count = lbs_mesh_ethtool_get_sset_count,
111 	.get_ethtool_stats = lbs_mesh_ethtool_get_stats,
112 	.get_strings = lbs_mesh_ethtool_get_strings,
113 #endif
114 	.get_wol = lbs_ethtool_get_wol,
115 	.set_wol = lbs_ethtool_set_wol,
116 };
117 
118