blob: 9ce854f43917ad723da924e7a99e3629c6a6e9bd [file] [log] [blame]
Sergey Lapin84594642009-06-08 12:18:51 +00001/*
2 * Sample driver for HardMAC IEEE 802.15.4 devices
3 *
4 * Copyright (C) 2009 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
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
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040029#include <net/af_ieee802154.h>
30#include <net/ieee802154_netdev.h>
31#include <net/ieee802154.h>
32#include <net/nl802154.h>
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +040033#include <net/wpan-phy.h>
34
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +030035struct fakehard_priv {
36 struct wpan_phy *phy;
37};
38
39static struct wpan_phy *fake_to_phy(const struct net_device *dev)
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +040040{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +030041 struct fakehard_priv *priv = netdev_priv(dev);
42 return priv->phy;
43}
44
45/**
46 * fake_get_phy - Return a phy corresponding to this device.
47 * @dev: The network device for which to return the wan-phy object
48 *
49 * This function returns a wpan-phy object corresponding to the passed
50 * network device. Reference counter for wpan-phy object is incremented,
51 * so when the wpan-phy isn't necessary, you should drop the reference
52 * via @wpan_phy_put() call.
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));
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +040058}
Sergey Lapin84594642009-06-08 12:18:51 +000059
Daniel Silverstone878fa892009-07-22 18:51:24 +020060/**
61 * fake_get_pan_id - Retrieve the PAN ID of the device.
62 * @dev: The network device to retrieve the PAN of.
63 *
64 * Return the ID of the PAN from the PIB.
65 */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010066static __le16 fake_get_pan_id(const struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +000067{
68 BUG_ON(dev->type != ARPHRD_IEEE802154);
69
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010070 return cpu_to_le16(0xeba1);
Sergey Lapin84594642009-06-08 12:18:51 +000071}
72
Daniel Silverstone878fa892009-07-22 18:51:24 +020073/**
74 * fake_get_short_addr - Retrieve the short address of the device.
75 * @dev: The network device to retrieve the short address of.
76 *
77 * Returns the IEEE 802.15.4 short-form address cached for this
78 * device. If the device has not yet had a short address assigned
79 * then this should return 0xFFFF to indicate a lack of association.
80 */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010081static __le16 fake_get_short_addr(const struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +000082{
83 BUG_ON(dev->type != ARPHRD_IEEE802154);
84
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010085 return cpu_to_le16(0x1);
Sergey Lapin84594642009-06-08 12:18:51 +000086}
87
Daniel Silverstone878fa892009-07-22 18:51:24 +020088/**
89 * fake_get_dsn - Retrieve the DSN of the device.
90 * @dev: The network device to retrieve the DSN for.
91 *
92 * Returns the IEEE 802.15.4 DSN for the network device.
93 * The DSN is the sequence number which will be added to each
94 * packet or MAC command frame by the MAC during transmission.
95 *
96 * DSN means 'Data Sequence Number'.
97 *
98 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
99 * document.
100 */
Dmitry Eremin-Solenikova9966b52009-10-02 19:05:00 +0400101static u8 fake_get_dsn(const struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +0000102{
103 BUG_ON(dev->type != ARPHRD_IEEE802154);
104
105 return 0x00; /* DSN are implemented in HW, so return just 0 */
106}
107
Daniel Silverstone878fa892009-07-22 18:51:24 +0200108/**
Daniel Silverstone878fa892009-07-22 18:51:24 +0200109 * fake_assoc_req - Make an association request to the HW.
110 * @dev: The network device which we are associating to a network.
111 * @addr: The coordinator with which we wish to associate.
112 * @channel: The channel on which to associate.
113 * @cap: The capability information field to use in the association.
114 *
115 * Start an association with a coordinator. The coordinator's address
116 * and PAN ID can be found in @addr.
117 *
118 * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
119 * 802.15.4-2006 document.
120 */
Sergey Lapin84594642009-06-08 12:18:51 +0000121static int fake_assoc_req(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100122 struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
Sergey Lapin84594642009-06-08 12:18:51 +0000123{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300124 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400125
126 mutex_lock(&phy->pib_lock);
127 phy->current_channel = channel;
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400128 phy->current_page = page;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400129 mutex_unlock(&phy->pib_lock);
130
Sergey Lapin84594642009-06-08 12:18:51 +0000131 /* We simply emulate it here */
132 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
133 IEEE802154_SUCCESS);
134}
135
Daniel Silverstone878fa892009-07-22 18:51:24 +0200136/**
137 * fake_assoc_resp - Send an association response to a device.
138 * @dev: The network device on which to send the response.
139 * @addr: The address of the device to respond to.
140 * @short_addr: The assigned short address for the device (if any).
141 * @status: The result of the association request.
142 *
143 * Queue the association response of the coordinator to another
144 * device's attempt to associate with the network which we
145 * coordinate. This is then added to the indirect-send queue to be
146 * transmitted to the end device when it polls for data.
147 *
148 * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
149 * 802.15.4-2006 document.
150 */
Sergey Lapin84594642009-06-08 12:18:51 +0000151static int fake_assoc_resp(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100152 struct ieee802154_addr *addr, __le16 short_addr, u8 status)
Sergey Lapin84594642009-06-08 12:18:51 +0000153{
154 return 0;
155}
156
Daniel Silverstone878fa892009-07-22 18:51:24 +0200157/**
158 * fake_disassoc_req - Disassociate a device from a network.
159 * @dev: The network device on which we're disassociating a device.
160 * @addr: The device to disassociate from the network.
161 * @reason: The reason to give to the device for being disassociated.
162 *
163 * This sends a disassociation notification to the device being
164 * disassociated from the network.
165 *
166 * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
167 * document, with the reason described in 7.3.3.2.
168 */
Sergey Lapin84594642009-06-08 12:18:51 +0000169static int fake_disassoc_req(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100170 struct ieee802154_addr *addr, u8 reason)
Sergey Lapin84594642009-06-08 12:18:51 +0000171{
172 return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
173}
174
Daniel Silverstone878fa892009-07-22 18:51:24 +0200175/**
176 * fake_start_req - Start an IEEE 802.15.4 PAN.
177 * @dev: The network device on which to start the PAN.
178 * @addr: The coordinator address to use when starting the PAN.
179 * @channel: The channel on which to start the PAN.
180 * @bcn_ord: Beacon order.
181 * @sf_ord: Superframe order.
182 * @pan_coord: Whether or not we are the PAN coordinator or just
183 * requesting a realignment perhaps?
184 * @blx: Battery Life Extension feature bitfield.
185 * @coord_realign: Something to realign something else.
186 *
187 * If pan_coord is non-zero then this starts a network with the
188 * provided parameters, otherwise it attempts a coordinator
189 * realignment of the stated network instead.
190 *
191 * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
192 * document, with 7.3.8 describing coordinator realignment.
Daniel Silverstone878fa892009-07-22 18:51:24 +0200193 */
Phoebe Buckheister376b7bd2014-03-14 21:23:57 +0100194static int fake_start_req(struct net_device *dev,
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100195 struct ieee802154_addr *addr, u8 channel, u8 page,
Phoebe Buckheister376b7bd2014-03-14 21:23:57 +0100196 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
197 u8 coord_realign)
Sergey Lapin84594642009-06-08 12:18:51 +0000198{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300199 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400200
201 mutex_lock(&phy->pib_lock);
202 phy->current_channel = channel;
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400203 phy->current_page = page;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400204 mutex_unlock(&phy->pib_lock);
205
Dmitry Baryshkov798c7522009-08-07 02:58:45 +0000206 /* We don't emulate beacons here at all, so START should fail */
207 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
Sergey Lapin84594642009-06-08 12:18:51 +0000208 return 0;
209}
210
Daniel Silverstone878fa892009-07-22 18:51:24 +0200211/**
212 * fake_scan_req - Start a channel scan.
213 * @dev: The network device on which to perform a channel scan.
214 * @type: The type of scan to perform.
215 * @channels: The channel bitmask to scan.
216 * @duration: How long to spend on each channel.
217 *
218 * This starts either a passive (energy) scan or an active (PAN) scan
219 * on the channels indicated in the @channels bitmask. The duration of
220 * the scan is measured in terms of superframe duration. Specifically,
221 * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
222 * channel.
223 *
224 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
225 */
Sergey Lapin84594642009-06-08 12:18:51 +0000226static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400227 u8 page, u8 duration)
Sergey Lapin84594642009-06-08 12:18:51 +0000228{
229 u8 edl[27] = {};
230 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400231 channels, page,
Sergey Lapin84594642009-06-08 12:18:51 +0000232 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
233}
234
235static struct ieee802154_mlme_ops fake_mlme = {
236 .assoc_req = fake_assoc_req,
237 .assoc_resp = fake_assoc_resp,
238 .disassoc_req = fake_disassoc_req,
239 .start_req = fake_start_req,
240 .scan_req = fake_scan_req,
241
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300242 .get_phy = fake_get_phy,
243
Sergey Lapin84594642009-06-08 12:18:51 +0000244 .get_pan_id = fake_get_pan_id,
245 .get_short_addr = fake_get_short_addr,
246 .get_dsn = fake_get_dsn,
Sergey Lapin84594642009-06-08 12:18:51 +0000247};
248
249static int ieee802154_fake_open(struct net_device *dev)
250{
251 netif_start_queue(dev);
252 return 0;
253}
254
255static int ieee802154_fake_close(struct net_device *dev)
256{
257 netif_stop_queue(dev);
258 return 0;
259}
260
Stephen Hemminger613573252009-08-31 19:50:58 +0000261static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,
262 struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +0000263{
Sergey Lapin84594642009-06-08 12:18:51 +0000264 dev->stats.tx_packets++;
265 dev->stats.tx_bytes += skb->len;
266
Sergey Lapin84594642009-06-08 12:18:51 +0000267 /* FIXME: do hardware work here ... */
268
Eric Dumazet56cf5482009-11-19 01:28:10 +0000269 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000270 return NETDEV_TX_OK;
Sergey Lapin84594642009-06-08 12:18:51 +0000271}
272
273
274static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
275 int cmd)
276{
277 struct sockaddr_ieee802154 *sa =
278 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
279 u16 pan_id, short_addr;
280
281 switch (cmd) {
282 case SIOCGIFADDR:
283 /* FIXME: fixed here, get from device IRL */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100284 pan_id = le16_to_cpu(fake_get_pan_id(dev));
285 short_addr = le16_to_cpu(fake_get_short_addr(dev));
Sergey Lapin84594642009-06-08 12:18:51 +0000286 if (pan_id == IEEE802154_PANID_BROADCAST ||
287 short_addr == IEEE802154_ADDR_BROADCAST)
288 return -EADDRNOTAVAIL;
289
290 sa->family = AF_IEEE802154;
291 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
292 sa->addr.pan_id = pan_id;
293 sa->addr.short_addr = short_addr;
294 return 0;
295 }
296 return -ENOIOCTLCMD;
297}
298
299static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
300{
301 return -EBUSY; /* HW address is built into the device */
302}
303
304static const struct net_device_ops fake_ops = {
305 .ndo_open = ieee802154_fake_open,
306 .ndo_stop = ieee802154_fake_close,
307 .ndo_start_xmit = ieee802154_fake_xmit,
308 .ndo_do_ioctl = ieee802154_fake_ioctl,
309 .ndo_set_mac_address = ieee802154_fake_mac_addr,
310};
311
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400312static void ieee802154_fake_destruct(struct net_device *dev)
313{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300314 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400315
316 wpan_phy_unregister(phy);
317 free_netdev(dev);
318 wpan_phy_free(phy);
319}
Sergey Lapin84594642009-06-08 12:18:51 +0000320
321static void ieee802154_fake_setup(struct net_device *dev)
322{
323 dev->addr_len = IEEE802154_ADDR_LEN;
324 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
Michał Mirosław34324dc2011-11-15 15:29:55 +0000325 dev->features = NETIF_F_HW_CSUM;
Sergey Lapin84594642009-06-08 12:18:51 +0000326 dev->needed_tailroom = 2; /* FCS */
327 dev->mtu = 127;
328 dev->tx_queue_len = 10;
329 dev->type = ARPHRD_IEEE802154;
330 dev->flags = IFF_NOARP | IFF_BROADCAST;
331 dev->watchdog_timeo = 0;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400332 dev->destructor = ieee802154_fake_destruct;
Sergey Lapin84594642009-06-08 12:18:51 +0000333}
334
335
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500336static int ieee802154fake_probe(struct platform_device *pdev)
Sergey Lapin84594642009-06-08 12:18:51 +0000337{
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400338 struct net_device *dev;
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300339 struct fakehard_priv *priv;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400340 struct wpan_phy *phy = wpan_phy_alloc(0);
Sergey Lapin84594642009-06-08 12:18:51 +0000341 int err;
342
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400343 if (!phy)
Sergey Lapin84594642009-06-08 12:18:51 +0000344 return -ENOMEM;
345
Tom Gundersenc835a672014-07-14 16:37:24 +0200346 dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d",
347 NET_NAME_UNKNOWN, ieee802154_fake_setup);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400348 if (!dev) {
349 wpan_phy_free(phy);
350 return -ENOMEM;
351 }
352
Sergey Lapin84594642009-06-08 12:18:51 +0000353 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
354 dev->addr_len);
Sergey Lapin84594642009-06-08 12:18:51 +0000355
Dmitry Eremin-Solenikov478e87c2009-09-22 15:31:25 +0400356 /*
357 * For now we'd like to emulate 2.4 GHz-only device,
358 * both O-QPSK and CSS
359 */
360 /* 2.4 GHz O-QPSK 802.15.4-2003 */
361 phy->channels_supported[0] |= 0x7FFF800;
362 /* 2.4 GHz CSS 802.15.4a-2007 */
363 phy->channels_supported[3] |= 0x3fff;
364
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400365 phy->transmit_power = 0xbf;
366
Sergey Lapin84594642009-06-08 12:18:51 +0000367 dev->netdev_ops = &fake_ops;
368 dev->ml_priv = &fake_mlme;
369
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300370 priv = netdev_priv(dev);
371 priv->phy = phy;
372
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400373 wpan_phy_set_dev(phy, &pdev->dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400374 SET_NETDEV_DEV(dev, &phy->dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000375
376 platform_set_drvdata(pdev, dev);
377
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400378 err = wpan_phy_register(phy);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400379 if (err)
380 goto out;
381
Sergey Lapin84594642009-06-08 12:18:51 +0000382 err = register_netdev(dev);
383 if (err < 0)
384 goto out;
385
Sergey Lapin84594642009-06-08 12:18:51 +0000386 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
387 return 0;
388
389out:
390 unregister_netdev(dev);
391 return err;
392}
393
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500394static int ieee802154fake_remove(struct platform_device *pdev)
Sergey Lapin84594642009-06-08 12:18:51 +0000395{
396 struct net_device *dev = platform_get_drvdata(pdev);
397 unregister_netdev(dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000398 return 0;
399}
400
401static struct platform_device *ieee802154fake_dev;
402
403static struct platform_driver ieee802154fake_driver = {
404 .probe = ieee802154fake_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -0500405 .remove = ieee802154fake_remove,
Sergey Lapin84594642009-06-08 12:18:51 +0000406 .driver = {
407 .name = "ieee802154hardmac",
408 .owner = THIS_MODULE,
409 },
410};
411
412static __init int fake_init(void)
413{
414 ieee802154fake_dev = platform_device_register_simple(
415 "ieee802154hardmac", -1, NULL, 0);
416 return platform_driver_register(&ieee802154fake_driver);
417}
418
419static __exit void fake_exit(void)
420{
421 platform_driver_unregister(&ieee802154fake_driver);
422 platform_device_unregister(ieee802154fake_dev);
423}
424
425module_init(fake_init);
426module_exit(fake_exit);
427MODULE_LICENSE("GPL");