2 * Sample driver for HardMAC IEEE 802.15.4 devices
4 * Copyright (C) 2009 Siemens AG
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.
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.
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.
20 * Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
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>
29 #include <net/af_ieee802154.h>
30 #include <net/ieee802154_netdev.h>
31 #include <net/ieee802154.h>
32 #include <net/nl802154.h>
35 * fake_get_pan_id - Retrieve the PAN ID of the device.
36 * @dev: The network device to retrieve the PAN of.
38 * Return the ID of the PAN from the PIB.
40 static u16 fake_get_pan_id(struct net_device *dev)
42 BUG_ON(dev->type != ARPHRD_IEEE802154);
48 * fake_get_short_addr - Retrieve the short address of the device.
49 * @dev: The network device to retrieve the short address of.
51 * Returns the IEEE 802.15.4 short-form address cached for this
52 * device. If the device has not yet had a short address assigned
53 * then this should return 0xFFFF to indicate a lack of association.
55 static u16 fake_get_short_addr(struct net_device *dev)
57 BUG_ON(dev->type != ARPHRD_IEEE802154);
63 * fake_get_dsn - Retrieve the DSN of the device.
64 * @dev: The network device to retrieve the DSN for.
66 * Returns the IEEE 802.15.4 DSN for the network device.
67 * The DSN is the sequence number which will be added to each
68 * packet or MAC command frame by the MAC during transmission.
70 * DSN means 'Data Sequence Number'.
72 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
75 static u8 fake_get_dsn(struct net_device *dev)
77 BUG_ON(dev->type != ARPHRD_IEEE802154);
79 return 0x00; /* DSN are implemented in HW, so return just 0 */
83 * fake_get_bsn - Retrieve the BSN of the device.
84 * @dev: The network device to retrieve the BSN for.
86 * Returns the IEEE 802.15.4 BSN for the network device.
87 * The BSN is the sequence number which will be added to each
88 * beacon frame sent by the MAC.
90 * BSN means 'Beacon Sequence Number'.
92 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
95 static u8 fake_get_bsn(struct net_device *dev)
97 BUG_ON(dev->type != ARPHRD_IEEE802154);
99 return 0x00; /* BSN are implemented in HW, so return just 0 */
103 * fake_assoc_req - Make an association request to the HW.
104 * @dev: The network device which we are associating to a network.
105 * @addr: The coordinator with which we wish to associate.
106 * @channel: The channel on which to associate.
107 * @cap: The capability information field to use in the association.
109 * Start an association with a coordinator. The coordinator's address
110 * and PAN ID can be found in @addr.
112 * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
113 * 802.15.4-2006 document.
115 static int fake_assoc_req(struct net_device *dev,
116 struct ieee802154_addr *addr, u8 channel, u8 cap)
118 /* We simply emulate it here */
119 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
124 * fake_assoc_resp - Send an association response to a device.
125 * @dev: The network device on which to send the response.
126 * @addr: The address of the device to respond to.
127 * @short_addr: The assigned short address for the device (if any).
128 * @status: The result of the association request.
130 * Queue the association response of the coordinator to another
131 * device's attempt to associate with the network which we
132 * coordinate. This is then added to the indirect-send queue to be
133 * transmitted to the end device when it polls for data.
135 * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
136 * 802.15.4-2006 document.
138 static int fake_assoc_resp(struct net_device *dev,
139 struct ieee802154_addr *addr, u16 short_addr, u8 status)
145 * fake_disassoc_req - Disassociate a device from a network.
146 * @dev: The network device on which we're disassociating a device.
147 * @addr: The device to disassociate from the network.
148 * @reason: The reason to give to the device for being disassociated.
150 * This sends a disassociation notification to the device being
151 * disassociated from the network.
153 * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
154 * document, with the reason described in 7.3.3.2.
156 static int fake_disassoc_req(struct net_device *dev,
157 struct ieee802154_addr *addr, u8 reason)
159 return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
163 * fake_start_req - Start an IEEE 802.15.4 PAN.
164 * @dev: The network device on which to start the PAN.
165 * @addr: The coordinator address to use when starting the PAN.
166 * @channel: The channel on which to start the PAN.
167 * @bcn_ord: Beacon order.
168 * @sf_ord: Superframe order.
169 * @pan_coord: Whether or not we are the PAN coordinator or just
170 * requesting a realignment perhaps?
171 * @blx: Battery Life Extension feature bitfield.
172 * @coord_realign: Something to realign something else.
174 * If pan_coord is non-zero then this starts a network with the
175 * provided parameters, otherwise it attempts a coordinator
176 * realignment of the stated network instead.
178 * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
179 * document, with 7.3.8 describing coordinator realignment.
181 static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
183 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
186 /* We don't emulate beacons here at all, so START should fail */
187 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
192 * fake_scan_req - Start a channel scan.
193 * @dev: The network device on which to perform a channel scan.
194 * @type: The type of scan to perform.
195 * @channels: The channel bitmask to scan.
196 * @duration: How long to spend on each channel.
198 * This starts either a passive (energy) scan or an active (PAN) scan
199 * on the channels indicated in the @channels bitmask. The duration of
200 * the scan is measured in terms of superframe duration. Specifically,
201 * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
204 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
206 static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
210 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
212 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
215 static struct ieee802154_mlme_ops fake_mlme = {
216 .assoc_req = fake_assoc_req,
217 .assoc_resp = fake_assoc_resp,
218 .disassoc_req = fake_disassoc_req,
219 .start_req = fake_start_req,
220 .scan_req = fake_scan_req,
222 .get_pan_id = fake_get_pan_id,
223 .get_short_addr = fake_get_short_addr,
224 .get_dsn = fake_get_dsn,
225 .get_bsn = fake_get_bsn,
228 static int ieee802154_fake_open(struct net_device *dev)
230 netif_start_queue(dev);
234 static int ieee802154_fake_close(struct net_device *dev)
236 netif_stop_queue(dev);
240 static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
242 skb->iif = dev->ifindex;
244 dev->stats.tx_packets++;
245 dev->stats.tx_bytes += skb->len;
247 dev->trans_start = jiffies;
249 /* FIXME: do hardware work here ... */
255 static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
258 struct sockaddr_ieee802154 *sa =
259 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
260 u16 pan_id, short_addr;
264 /* FIXME: fixed here, get from device IRL */
265 pan_id = fake_get_pan_id(dev);
266 short_addr = fake_get_short_addr(dev);
267 if (pan_id == IEEE802154_PANID_BROADCAST ||
268 short_addr == IEEE802154_ADDR_BROADCAST)
269 return -EADDRNOTAVAIL;
271 sa->family = AF_IEEE802154;
272 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
273 sa->addr.pan_id = pan_id;
274 sa->addr.short_addr = short_addr;
280 static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
282 return -EBUSY; /* HW address is built into the device */
285 static const struct net_device_ops fake_ops = {
286 .ndo_open = ieee802154_fake_open,
287 .ndo_stop = ieee802154_fake_close,
288 .ndo_start_xmit = ieee802154_fake_xmit,
289 .ndo_do_ioctl = ieee802154_fake_ioctl,
290 .ndo_set_mac_address = ieee802154_fake_mac_addr,
294 static void ieee802154_fake_setup(struct net_device *dev)
296 dev->addr_len = IEEE802154_ADDR_LEN;
297 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
298 dev->features = NETIF_F_NO_CSUM;
299 dev->needed_tailroom = 2; /* FCS */
301 dev->tx_queue_len = 10;
302 dev->type = ARPHRD_IEEE802154;
303 dev->flags = IFF_NOARP | IFF_BROADCAST;
304 dev->watchdog_timeo = 0;
308 static int __devinit ieee802154fake_probe(struct platform_device *pdev)
310 struct net_device *dev =
311 alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
317 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
319 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
321 dev->netdev_ops = &fake_ops;
322 dev->ml_priv = &fake_mlme;
325 * If the name is a format string the caller wants us to do a
328 if (strchr(dev->name, '%')) {
329 err = dev_alloc_name(dev, dev->name);
334 SET_NETDEV_DEV(dev, &pdev->dev);
336 platform_set_drvdata(pdev, dev);
338 err = register_netdev(dev);
343 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
347 unregister_netdev(dev);
351 static int __devexit ieee802154fake_remove(struct platform_device *pdev)
353 struct net_device *dev = platform_get_drvdata(pdev);
354 unregister_netdev(dev);
359 static struct platform_device *ieee802154fake_dev;
361 static struct platform_driver ieee802154fake_driver = {
362 .probe = ieee802154fake_probe,
363 .remove = __devexit_p(ieee802154fake_remove),
365 .name = "ieee802154hardmac",
366 .owner = THIS_MODULE,
370 static __init int fake_init(void)
372 ieee802154fake_dev = platform_device_register_simple(
373 "ieee802154hardmac", -1, NULL, 0);
374 return platform_driver_register(&ieee802154fake_driver);
377 static __exit void fake_exit(void)
379 platform_driver_unregister(&ieee802154fake_driver);
380 platform_device_unregister(ieee802154fake_dev);
383 module_init(fake_init);
384 module_exit(fake_exit);
385 MODULE_LICENSE("GPL");