blob: d9d0e13efe47bc3f9610d412e7799cc67042ac1f [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 */
Dmitry Eremin-Solenikova9966b52009-10-02 19:05:00 +040066static u16 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
70 return 0xeba1;
71}
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 */
Dmitry Eremin-Solenikova9966b52009-10-02 19:05:00 +040081static u16 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
85 return 0x1;
86}
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/**
109 * fake_get_bsn - Retrieve the BSN of the device.
110 * @dev: The network device to retrieve the BSN for.
111 *
112 * Returns the IEEE 802.15.4 BSN for the network device.
113 * The BSN is the sequence number which will be added to each
114 * beacon frame sent by the MAC.
115 *
116 * BSN means 'Beacon Sequence Number'.
117 *
118 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
119 * document.
120 */
Dmitry Eremin-Solenikova9966b52009-10-02 19:05:00 +0400121static u8 fake_get_bsn(const struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +0000122{
123 BUG_ON(dev->type != ARPHRD_IEEE802154);
124
125 return 0x00; /* BSN are implemented in HW, so return just 0 */
126}
127
Daniel Silverstone878fa892009-07-22 18:51:24 +0200128/**
129 * fake_assoc_req - Make an association request to the HW.
130 * @dev: The network device which we are associating to a network.
131 * @addr: The coordinator with which we wish to associate.
132 * @channel: The channel on which to associate.
133 * @cap: The capability information field to use in the association.
134 *
135 * Start an association with a coordinator. The coordinator's address
136 * and PAN ID can be found in @addr.
137 *
138 * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
139 * 802.15.4-2006 document.
140 */
Sergey Lapin84594642009-06-08 12:18:51 +0000141static int fake_assoc_req(struct net_device *dev,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400142 struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
Sergey Lapin84594642009-06-08 12:18:51 +0000143{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300144 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400145
146 mutex_lock(&phy->pib_lock);
147 phy->current_channel = channel;
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400148 phy->current_page = page;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400149 mutex_unlock(&phy->pib_lock);
150
Sergey Lapin84594642009-06-08 12:18:51 +0000151 /* We simply emulate it here */
152 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
153 IEEE802154_SUCCESS);
154}
155
Daniel Silverstone878fa892009-07-22 18:51:24 +0200156/**
157 * fake_assoc_resp - Send an association response to a device.
158 * @dev: The network device on which to send the response.
159 * @addr: The address of the device to respond to.
160 * @short_addr: The assigned short address for the device (if any).
161 * @status: The result of the association request.
162 *
163 * Queue the association response of the coordinator to another
164 * device's attempt to associate with the network which we
165 * coordinate. This is then added to the indirect-send queue to be
166 * transmitted to the end device when it polls for data.
167 *
168 * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
169 * 802.15.4-2006 document.
170 */
Sergey Lapin84594642009-06-08 12:18:51 +0000171static int fake_assoc_resp(struct net_device *dev,
172 struct ieee802154_addr *addr, u16 short_addr, u8 status)
173{
174 return 0;
175}
176
Daniel Silverstone878fa892009-07-22 18:51:24 +0200177/**
178 * fake_disassoc_req - Disassociate a device from a network.
179 * @dev: The network device on which we're disassociating a device.
180 * @addr: The device to disassociate from the network.
181 * @reason: The reason to give to the device for being disassociated.
182 *
183 * This sends a disassociation notification to the device being
184 * disassociated from the network.
185 *
186 * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
187 * document, with the reason described in 7.3.3.2.
188 */
Sergey Lapin84594642009-06-08 12:18:51 +0000189static 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
Daniel Silverstone878fa892009-07-22 18:51:24 +0200195/**
196 * fake_start_req - Start an IEEE 802.15.4 PAN.
197 * @dev: The network device on which to start the PAN.
198 * @addr: The coordinator address to use when starting the PAN.
199 * @channel: The channel on which to start the PAN.
200 * @bcn_ord: Beacon order.
201 * @sf_ord: Superframe order.
202 * @pan_coord: Whether or not we are the PAN coordinator or just
203 * requesting a realignment perhaps?
204 * @blx: Battery Life Extension feature bitfield.
205 * @coord_realign: Something to realign something else.
206 *
207 * If pan_coord is non-zero then this starts a network with the
208 * provided parameters, otherwise it attempts a coordinator
209 * realignment of the stated network instead.
210 *
211 * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
212 * document, with 7.3.8 describing coordinator realignment.
Daniel Silverstone878fa892009-07-22 18:51:24 +0200213 */
Sergey Lapin84594642009-06-08 12:18:51 +0000214static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400215 u8 channel, u8 page,
Sergey Lapin84594642009-06-08 12:18:51 +0000216 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
217 u8 coord_realign)
218{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300219 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400220
221 mutex_lock(&phy->pib_lock);
222 phy->current_channel = channel;
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400223 phy->current_page = page;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400224 mutex_unlock(&phy->pib_lock);
225
Dmitry Baryshkov798c7522009-08-07 02:58:45 +0000226 /* We don't emulate beacons here at all, so START should fail */
227 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
Sergey Lapin84594642009-06-08 12:18:51 +0000228 return 0;
229}
230
Daniel Silverstone878fa892009-07-22 18:51:24 +0200231/**
232 * fake_scan_req - Start a channel scan.
233 * @dev: The network device on which to perform a channel scan.
234 * @type: The type of scan to perform.
235 * @channels: The channel bitmask to scan.
236 * @duration: How long to spend on each channel.
237 *
238 * This starts either a passive (energy) scan or an active (PAN) scan
239 * on the channels indicated in the @channels bitmask. The duration of
240 * the scan is measured in terms of superframe duration. Specifically,
241 * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
242 * channel.
243 *
244 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
245 */
Sergey Lapin84594642009-06-08 12:18:51 +0000246static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400247 u8 page, u8 duration)
Sergey Lapin84594642009-06-08 12:18:51 +0000248{
249 u8 edl[27] = {};
250 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
Dmitry Eremin-Solenikov16eea492009-08-19 19:32:24 +0400251 channels, page,
Sergey Lapin84594642009-06-08 12:18:51 +0000252 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
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300262 .get_phy = fake_get_phy,
263
Sergey Lapin84594642009-06-08 12:18:51 +0000264 .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
Stephen Hemminger613573252009-08-31 19:50:58 +0000282static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,
283 struct net_device *dev)
Sergey Lapin84594642009-06-08 12:18:51 +0000284{
Sergey Lapin84594642009-06-08 12:18:51 +0000285 dev->stats.tx_packets++;
286 dev->stats.tx_bytes += skb->len;
287
Sergey Lapin84594642009-06-08 12:18:51 +0000288 /* FIXME: do hardware work here ... */
289
Eric Dumazet56cf5482009-11-19 01:28:10 +0000290 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000291 return NETDEV_TX_OK;
Sergey Lapin84594642009-06-08 12:18:51 +0000292}
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 /* FIXME: fixed here, get from device IRL */
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; /* HW address is built into the device */
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
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400333static void ieee802154_fake_destruct(struct net_device *dev)
334{
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300335 struct wpan_phy *phy = fake_to_phy(dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400336
337 wpan_phy_unregister(phy);
338 free_netdev(dev);
339 wpan_phy_free(phy);
340}
Sergey Lapin84594642009-06-08 12:18:51 +0000341
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_NO_CSUM;
347 dev->needed_tailroom = 2; /* FCS */
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;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400353 dev->destructor = ieee802154_fake_destruct;
Sergey Lapin84594642009-06-08 12:18:51 +0000354}
355
356
357static int __devinit ieee802154fake_probe(struct platform_device *pdev)
358{
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400359 struct net_device *dev;
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300360 struct fakehard_priv *priv;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400361 struct wpan_phy *phy = wpan_phy_alloc(0);
Sergey Lapin84594642009-06-08 12:18:51 +0000362 int err;
363
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400364 if (!phy)
Sergey Lapin84594642009-06-08 12:18:51 +0000365 return -ENOMEM;
366
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300367 dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400368 if (!dev) {
369 wpan_phy_free(phy);
370 return -ENOMEM;
371 }
372
373 phy->dev.platform_data = dev;
374
Sergey Lapin84594642009-06-08 12:18:51 +0000375 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
376 dev->addr_len);
377 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
378
Dmitry Eremin-Solenikov478e87c2009-09-22 15:31:25 +0400379 /*
380 * For now we'd like to emulate 2.4 GHz-only device,
381 * both O-QPSK and CSS
382 */
383 /* 2.4 GHz O-QPSK 802.15.4-2003 */
384 phy->channels_supported[0] |= 0x7FFF800;
385 /* 2.4 GHz CSS 802.15.4a-2007 */
386 phy->channels_supported[3] |= 0x3fff;
387
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400388 phy->transmit_power = 0xbf;
389
Sergey Lapin84594642009-06-08 12:18:51 +0000390 dev->netdev_ops = &fake_ops;
391 dev->ml_priv = &fake_mlme;
392
Dmitry Eremin-Solenikov405cd392009-11-04 17:53:40 +0300393 priv = netdev_priv(dev);
394 priv->phy = phy;
395
Sergey Lapin84594642009-06-08 12:18:51 +0000396 /*
397 * If the name is a format string the caller wants us to do a
398 * name allocation.
399 */
400 if (strchr(dev->name, '%')) {
401 err = dev_alloc_name(dev, dev->name);
402 if (err < 0)
403 goto out;
404 }
405
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400406 wpan_phy_set_dev(phy, &pdev->dev);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400407 SET_NETDEV_DEV(dev, &phy->dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000408
409 platform_set_drvdata(pdev, dev);
410
Dmitry Eremin-Solenikove9cf3562009-09-28 19:01:20 +0400411 err = wpan_phy_register(phy);
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400412 if (err)
413 goto out;
414
Sergey Lapin84594642009-06-08 12:18:51 +0000415 err = register_netdev(dev);
416 if (err < 0)
417 goto out;
418
Sergey Lapin84594642009-06-08 12:18:51 +0000419 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
420 return 0;
421
422out:
423 unregister_netdev(dev);
424 return err;
425}
426
427static int __devexit ieee802154fake_remove(struct platform_device *pdev)
428{
429 struct net_device *dev = platform_get_drvdata(pdev);
430 unregister_netdev(dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000431 return 0;
432}
433
434static struct platform_device *ieee802154fake_dev;
435
436static struct platform_driver ieee802154fake_driver = {
437 .probe = ieee802154fake_probe,
438 .remove = __devexit_p(ieee802154fake_remove),
439 .driver = {
440 .name = "ieee802154hardmac",
441 .owner = THIS_MODULE,
442 },
443};
444
445static __init int fake_init(void)
446{
447 ieee802154fake_dev = platform_device_register_simple(
448 "ieee802154hardmac", -1, NULL, 0);
449 return platform_driver_register(&ieee802154fake_driver);
450}
451
452static __exit void fake_exit(void)
453{
454 platform_driver_unregister(&ieee802154fake_driver);
455 platform_device_unregister(ieee802154fake_dev);
456}
457
458module_init(fake_init);
459module_exit(fake_exit);
460MODULE_LICENSE("GPL");
461