瀏覽代碼

[netdevice] Add "chip" setting

Suggested-by: Robin Smidsrød <robin@smidsrod.no>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 年之前
父節點
當前提交
08bf79582a
共有 1 個檔案被更改,包括 131 行新增26 行删除
  1. 131
    26
      src/net/netdev_settings.c

+ 131
- 26
src/net/netdev_settings.c 查看文件

@@ -45,6 +45,113 @@ struct setting busid_setting __setting ( SETTING_NETDEV ) = {
45 45
 	.description = "Bus ID",
46 46
 	.type = &setting_type_hex,
47 47
 };
48
+struct setting chip_setting __setting ( SETTING_NETDEV ) = {
49
+	.name = "chip",
50
+	.description = "Chip",
51
+	.type = &setting_type_string,
52
+};
53
+
54
+/**
55
+ * Store MAC address setting
56
+ *
57
+ * @v netdev		Network device
58
+ * @v data		Setting data, or NULL to clear setting
59
+ * @v len		Length of setting data
60
+ * @ret rc		Return status code
61
+ */
62
+static int netdev_store_mac ( struct net_device *netdev,
63
+			      const void *data, size_t len ) {
64
+
65
+	if ( len != netdev->ll_protocol->ll_addr_len )
66
+		return -EINVAL;
67
+	memcpy ( netdev->ll_addr, data, len );
68
+	return 0;
69
+}
70
+
71
+/**
72
+ * Fetch MAC address setting
73
+ *
74
+ * @v netdev		Network device
75
+ * @v data		Buffer to fill with setting data
76
+ * @v len		Length of buffer
77
+ * @ret len		Length of setting data, or negative error
78
+ */
79
+static int netdev_fetch_mac ( struct net_device *netdev, void *data,
80
+			      size_t len ) {
81
+
82
+	if ( len > netdev->ll_protocol->ll_addr_len )
83
+		len = netdev->ll_protocol->ll_addr_len;
84
+	memcpy ( data, netdev->ll_addr, len );
85
+	return netdev->ll_protocol->ll_addr_len;
86
+}
87
+
88
+/**
89
+ * Fetch bus ID setting
90
+ *
91
+ * @v netdev		Network device
92
+ * @v data		Buffer to fill with setting data
93
+ * @v len		Length of buffer
94
+ * @ret len		Length of setting data, or negative error
95
+ */
96
+static int netdev_fetch_busid ( struct net_device *netdev, void *data,
97
+				size_t len ) {
98
+	struct device_description *desc = &netdev->dev->desc;
99
+	struct dhcp_netdev_desc dhcp_desc;
100
+
101
+	dhcp_desc.type = desc->bus_type;
102
+	dhcp_desc.vendor = htons ( desc->vendor );
103
+	dhcp_desc.device = htons ( desc->device );
104
+	if ( len > sizeof ( dhcp_desc ) )
105
+		len = sizeof ( dhcp_desc );
106
+	memcpy ( data, &dhcp_desc, len );
107
+	return sizeof ( dhcp_desc );
108
+}
109
+
110
+/**
111
+ * Fetch chip setting
112
+ *
113
+ * @v netdev		Network device
114
+ * @v data		Buffer to fill with setting data
115
+ * @v len		Length of buffer
116
+ * @ret len		Length of setting data, or negative error
117
+ */
118
+static int netdev_fetch_chip ( struct net_device *netdev, void *data,
119
+			       size_t len ) {
120
+	const char *chip = netdev->dev->driver_name;
121
+
122
+	strncpy ( data, chip, len );
123
+	return strlen ( chip );
124
+}
125
+
126
+/** A network device setting operation */
127
+struct netdev_setting_operation {
128
+	/** Setting */
129
+	struct setting *setting;
130
+	/** Store setting (or NULL if not supported)
131
+	 *
132
+	 * @v netdev		Network device
133
+	 * @v data		Setting data, or NULL to clear setting
134
+	 * @v len		Length of setting data
135
+	 * @ret rc		Return status code
136
+	 */
137
+	int ( * store ) ( struct net_device *netdev, const void *data,
138
+			  size_t len );
139
+	/** Fetch setting
140
+	 *
141
+	 * @v netdev		Network device
142
+	 * @v data		Buffer to fill with setting data
143
+	 * @v len		Length of buffer
144
+	 * @ret len		Length of setting data, or negative error
145
+	 */
146
+	int ( * fetch ) ( struct net_device *netdev, void *data, size_t len );
147
+};
148
+
149
+/** Network device settings */
150
+static struct netdev_setting_operation netdev_setting_operations[] = {
151
+	{ &mac_setting, netdev_store_mac, netdev_fetch_mac },
152
+	{ &busid_setting, NULL, netdev_fetch_busid },
153
+	{ &chip_setting, NULL, netdev_fetch_chip },
154
+};
48 155
 
49 156
 /**
50 157
  * Store value of network device setting
@@ -59,15 +166,21 @@ static int netdev_store ( struct settings *settings, struct setting *setting,
59 166
 			  const void *data, size_t len ) {
60 167
 	struct net_device *netdev = container_of ( settings, struct net_device,
61 168
 						   settings.settings );
169
+	struct netdev_setting_operation *op;
170
+	unsigned int i;
62 171
 
63
-	if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
64
-		if ( len != netdev->ll_protocol->ll_addr_len )
65
-			return -EINVAL;
66
-		memcpy ( netdev->ll_addr, data, len );
67
-		return 0;
172
+	/* Handle network device-specific settings */
173
+	for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
174
+			    sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
175
+		op = &netdev_setting_operations[i];
176
+		if ( setting_cmp ( setting, op->setting ) == 0 ) {
177
+			if ( op->store ) {
178
+				return op->store ( netdev, data, len );
179
+			} else {
180
+				return -ENOTSUP;
181
+			}
182
+		}
68 183
 	}
69
-	if ( setting_cmp ( setting, &busid_setting ) == 0 )
70
-		return -ENOTSUP;
71 184
 
72 185
 	return generic_settings_store ( settings, setting, data, len );
73 186
 }
@@ -77,31 +190,23 @@ static int netdev_store ( struct settings *settings, struct setting *setting,
77 190
  *
78 191
  * @v settings		Settings block
79 192
  * @v setting		Setting to fetch
80
- * @v data		Setting data, or NULL to clear setting
81
- * @v len		Length of setting data
82
- * @ret rc		Return status code
193
+ * @v data		Buffer to fill with setting data
194
+ * @v len		Length of buffer
195
+ * @ret len		Length of setting data, or negative error
83 196
  */
84 197
 static int netdev_fetch ( struct settings *settings, struct setting *setting,
85 198
 			  void *data, size_t len ) {
86 199
 	struct net_device *netdev = container_of ( settings, struct net_device,
87 200
 						   settings.settings );
88
-	struct device_description *desc = &netdev->dev->desc;
89
-	struct dhcp_netdev_desc dhcp_desc;
201
+	struct netdev_setting_operation *op;
202
+	unsigned int i;
90 203
 
91
-	if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
92
-		if ( len > netdev->ll_protocol->ll_addr_len )
93
-			len = netdev->ll_protocol->ll_addr_len;
94
-		memcpy ( data, netdev->ll_addr, len );
95
-		return netdev->ll_protocol->ll_addr_len;
96
-	}
97
-	if ( setting_cmp ( setting, &busid_setting ) == 0 ) {
98
-		dhcp_desc.type = desc->bus_type;
99
-		dhcp_desc.vendor = htons ( desc->vendor );
100
-		dhcp_desc.device = htons ( desc->device );
101
-		if ( len > sizeof ( dhcp_desc ) )
102
-			len = sizeof ( dhcp_desc );
103
-		memcpy ( data, &dhcp_desc, len );
104
-		return sizeof ( dhcp_desc );
204
+	/* Handle network device-specific settings */
205
+	for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
206
+			    sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
207
+		op = &netdev_setting_operations[i];
208
+		if ( setting_cmp ( setting, op->setting ) == 0 )
209
+			return op->fetch ( netdev, data, len );
105 210
 	}
106 211
 
107 212
 	return generic_settings_fetch ( settings, setting, data, len );

Loading…
取消
儲存