1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/netdevice.h>
26#include <linux/skbuff.h>
27#include <linux/if_arp.h>
28
29#include <net/af_ieee802154.h>
30#include <net/ieee802154_netdev.h>
31#include <net/ieee802154.h>
32#include <net/nl802154.h>
33#include <net/wpan-phy.h>
34
35struct fakehard_priv {
36 struct wpan_phy *phy;
37};
38
39static struct wpan_phy *fake_to_phy(const struct net_device *dev)
40{
41 struct fakehard_priv *priv = netdev_priv(dev);
42 return priv->phy;
43}
44
45
46
47
48
49
50
51
52
53
54static struct wpan_phy *fake_get_phy(const struct net_device *dev)
55{
56 struct wpan_phy *phy = fake_to_phy(dev);
57 return to_phy(get_device(&phy->dev));
58}
59
60
61
62
63
64
65
66static u16 fake_get_pan_id(const struct net_device *dev)
67{
68 BUG_ON(dev->type != ARPHRD_IEEE802154);
69
70 return 0xeba1;
71}
72
73
74
75
76
77
78
79
80
81static u16 fake_get_short_addr(const struct net_device *dev)
82{
83 BUG_ON(dev->type != ARPHRD_IEEE802154);
84
85 return 0x1;
86}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101static u8 fake_get_dsn(const struct net_device *dev)
102{
103 BUG_ON(dev->type != ARPHRD_IEEE802154);
104
105 return 0x00;
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121static u8 fake_get_bsn(const struct net_device *dev)
122{
123 BUG_ON(dev->type != ARPHRD_IEEE802154);
124
125 return 0x00;
126}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141static int fake_assoc_req(struct net_device *dev,
142 struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
143{
144 struct wpan_phy *phy = fake_to_phy(dev);
145
146 mutex_lock(&phy->pib_lock);
147 phy->current_channel = channel;
148 phy->current_page = page;
149 mutex_unlock(&phy->pib_lock);
150
151
152 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
153 IEEE802154_SUCCESS);
154}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171static int fake_assoc_resp(struct net_device *dev,
172 struct ieee802154_addr *addr, u16 short_addr, u8 status)
173{
174 return 0;
175}
176
177
178
179
180
181
182
183
184
185
186
187
188
189static int fake_disassoc_req(struct net_device *dev,
190 struct ieee802154_addr *addr, u8 reason)
191{
192 return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
193}
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
215 u8 channel, u8 page,
216 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
217 u8 coord_realign)
218{
219 struct wpan_phy *phy = fake_to_phy(dev);
220
221 mutex_lock(&phy->pib_lock);
222 phy->current_channel = channel;
223 phy->current_page = page;
224 mutex_unlock(&phy->pib_lock);
225
226
227 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
228 return 0;
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
247 u8 page, u8 duration)
248{
249 u8 edl[27] = {};
250 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
251 channels, page,
252 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
253}
254
255static struct ieee802154_mlme_ops fake_mlme = {
256 .assoc_req = fake_assoc_req,
257 .assoc_resp = fake_assoc_resp,
258 .disassoc_req = fake_disassoc_req,
259 .start_req = fake_start_req,
260 .scan_req = fake_scan_req,
261
262 .get_phy = fake_get_phy,
263
264 .get_pan_id = fake_get_pan_id,
265 .get_short_addr = fake_get_short_addr,
266 .get_dsn = fake_get_dsn,
267 .get_bsn = fake_get_bsn,
268};
269
270static int ieee802154_fake_open(struct net_device *dev)
271{
272 netif_start_queue(dev);
273 return 0;
274}
275
276static int ieee802154_fake_close(struct net_device *dev)
277{
278 netif_stop_queue(dev);
279 return 0;
280}
281
282static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,
283 struct net_device *dev)
284{
285 dev->stats.tx_packets++;
286 dev->stats.tx_bytes += skb->len;
287
288
289
290 dev_kfree_skb(skb);
291 return NETDEV_TX_OK;
292}
293
294
295static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
296 int cmd)
297{
298 struct sockaddr_ieee802154 *sa =
299 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
300 u16 pan_id, short_addr;
301
302 switch (cmd) {
303 case SIOCGIFADDR:
304
305 pan_id = fake_get_pan_id(dev);
306 short_addr = fake_get_short_addr(dev);
307 if (pan_id == IEEE802154_PANID_BROADCAST ||
308 short_addr == IEEE802154_ADDR_BROADCAST)
309 return -EADDRNOTAVAIL;
310
311 sa->family = AF_IEEE802154;
312 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
313 sa->addr.pan_id = pan_id;
314 sa->addr.short_addr = short_addr;
315 return 0;
316 }
317 return -ENOIOCTLCMD;
318}
319
320static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
321{
322 return -EBUSY;
323}
324
325static const struct net_device_ops fake_ops = {
326 .ndo_open = ieee802154_fake_open,
327 .ndo_stop = ieee802154_fake_close,
328 .ndo_start_xmit = ieee802154_fake_xmit,
329 .ndo_do_ioctl = ieee802154_fake_ioctl,
330 .ndo_set_mac_address = ieee802154_fake_mac_addr,
331};
332
333static void ieee802154_fake_destruct(struct net_device *dev)
334{
335 struct wpan_phy *phy = fake_to_phy(dev);
336
337 wpan_phy_unregister(phy);
338 free_netdev(dev);
339 wpan_phy_free(phy);
340}
341
342static void ieee802154_fake_setup(struct net_device *dev)
343{
344 dev->addr_len = IEEE802154_ADDR_LEN;
345 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
346 dev->features = NETIF_F_HW_CSUM;
347 dev->needed_tailroom = 2;
348 dev->mtu = 127;
349 dev->tx_queue_len = 10;
350 dev->type = ARPHRD_IEEE802154;
351 dev->flags = IFF_NOARP | IFF_BROADCAST;
352 dev->watchdog_timeo = 0;
353 dev->destructor = ieee802154_fake_destruct;
354}
355
356
357static int __devinit ieee802154fake_probe(struct platform_device *pdev)
358{
359 struct net_device *dev;
360 struct fakehard_priv *priv;
361 struct wpan_phy *phy = wpan_phy_alloc(0);
362 int err;
363
364 if (!phy)
365 return -ENOMEM;
366
367 dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);
368 if (!dev) {
369 wpan_phy_free(phy);
370 return -ENOMEM;
371 }
372
373 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
374 dev->addr_len);
375 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
376
377
378
379
380
381
382 phy->channels_supported[0] |= 0x7FFF800;
383
384 phy->channels_supported[3] |= 0x3fff;
385
386 phy->transmit_power = 0xbf;
387
388 dev->netdev_ops = &fake_ops;
389 dev->ml_priv = &fake_mlme;
390
391 priv = netdev_priv(dev);
392 priv->phy = phy;
393
394 wpan_phy_set_dev(phy, &pdev->dev);
395 SET_NETDEV_DEV(dev, &phy->dev);
396
397 platform_set_drvdata(pdev, dev);
398
399 err = wpan_phy_register(phy);
400 if (err)
401 goto out;
402
403 err = register_netdev(dev);
404 if (err < 0)
405 goto out;
406
407 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
408 return 0;
409
410out:
411 unregister_netdev(dev);
412 return err;
413}
414
415static int __devexit ieee802154fake_remove(struct platform_device *pdev)
416{
417 struct net_device *dev = platform_get_drvdata(pdev);
418 unregister_netdev(dev);
419 return 0;
420}
421
422static struct platform_device *ieee802154fake_dev;
423
424static struct platform_driver ieee802154fake_driver = {
425 .probe = ieee802154fake_probe,
426 .remove = __devexit_p(ieee802154fake_remove),
427 .driver = {
428 .name = "ieee802154hardmac",
429 .owner = THIS_MODULE,
430 },
431};
432
433static __init int fake_init(void)
434{
435 ieee802154fake_dev = platform_device_register_simple(
436 "ieee802154hardmac", -1, NULL, 0);
437 return platform_driver_register(&ieee802154fake_driver);
438}
439
440static __exit void fake_exit(void)
441{
442 platform_driver_unregister(&ieee802154fake_driver);
443 platform_device_unregister(ieee802154fake_dev);
444}
445
446module_init(fake_init);
447module_exit(fake_exit);
448MODULE_LICENSE("GPL");
449
450