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 * Note: There is currently no way to notify the coordinator userland
182 * program of whether or not the PAN has started successfully. As
183 * such, the coordinator program cannot know when the MAC has
184 * completed starting the network and will simply have to assume
185 * completeness based on some form of time delay.
187 static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
189 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
196 * fake_scan_req - Start a channel scan.
197 * @dev: The network device on which to perform a channel scan.
198 * @type: The type of scan to perform.
199 * @channels: The channel bitmask to scan.
200 * @duration: How long to spend on each channel.
202 * This starts either a passive (energy) scan or an active (PAN) scan
203 * on the channels indicated in the @channels bitmask. The duration of
204 * the scan is measured in terms of superframe duration. Specifically,
205 * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
208 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
210 static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
214 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
216 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
219 static struct ieee802154_mlme_ops fake_mlme = {
220 .assoc_req = fake_assoc_req,
221 .assoc_resp = fake_assoc_resp,
222 .disassoc_req = fake_disassoc_req,
223 .start_req = fake_start_req,
224 .scan_req = fake_scan_req,
226 .get_pan_id = fake_get_pan_id,
227 .get_short_addr = fake_get_short_addr,
228 .get_dsn = fake_get_dsn,
229 .get_bsn = fake_get_bsn,
232 static int ieee802154_fake_open(struct net_device *dev)
234 netif_start_queue(dev);
238 static int ieee802154_fake_close(struct net_device *dev)
240 netif_stop_queue(dev);
244 static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
246 skb->iif = dev->ifindex;
248 dev->stats.tx_packets++;
249 dev->stats.tx_bytes += skb->len;
251 dev->trans_start = jiffies;
253 /* FIXME: do hardware work here ... */
259 static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
262 struct sockaddr_ieee802154 *sa =
263 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
264 u16 pan_id, short_addr;
268 /* FIXME: fixed here, get from device IRL */
269 pan_id = fake_get_pan_id(dev);
270 short_addr = fake_get_short_addr(dev);
271 if (pan_id == IEEE802154_PANID_BROADCAST ||
272 short_addr == IEEE802154_ADDR_BROADCAST)
273 return -EADDRNOTAVAIL;
275 sa->family = AF_IEEE802154;
276 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
277 sa->addr.pan_id = pan_id;
278 sa->addr.short_addr = short_addr;
284 static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
286 return -EBUSY; /* HW address is built into the device */
289 static const struct net_device_ops fake_ops = {
290 .ndo_open = ieee802154_fake_open,
291 .ndo_stop = ieee802154_fake_close,
292 .ndo_start_xmit = ieee802154_fake_xmit,
293 .ndo_do_ioctl = ieee802154_fake_ioctl,
294 .ndo_set_mac_address = ieee802154_fake_mac_addr,
298 static void ieee802154_fake_setup(struct net_device *dev)
300 dev->addr_len = IEEE802154_ADDR_LEN;
301 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
302 dev->features = NETIF_F_NO_CSUM;
303 dev->needed_tailroom = 2; /* FCS */
305 dev->tx_queue_len = 10;
306 dev->type = ARPHRD_IEEE802154;
307 dev->flags = IFF_NOARP | IFF_BROADCAST;
308 dev->watchdog_timeo = 0;
312 static int __devinit ieee802154fake_probe(struct platform_device *pdev)
314 struct net_device *dev =
315 alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
321 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
323 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
325 dev->netdev_ops = &fake_ops;
326 dev->ml_priv = &fake_mlme;
329 * If the name is a format string the caller wants us to do a
332 if (strchr(dev->name, '%')) {
333 err = dev_alloc_name(dev, dev->name);
338 SET_NETDEV_DEV(dev, &pdev->dev);
340 platform_set_drvdata(pdev, dev);
342 err = register_netdev(dev);
347 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
351 unregister_netdev(dev);
355 static int __devexit ieee802154fake_remove(struct platform_device *pdev)
357 struct net_device *dev = platform_get_drvdata(pdev);
358 unregister_netdev(dev);
363 static struct platform_device *ieee802154fake_dev;
365 static struct platform_driver ieee802154fake_driver = {
366 .probe = ieee802154fake_probe,
367 .remove = __devexit_p(ieee802154fake_remove),
369 .name = "ieee802154hardmac",
370 .owner = THIS_MODULE,
374 static __init int fake_init(void)
376 ieee802154fake_dev = platform_device_register_simple(
377 "ieee802154hardmac", -1, NULL, 0);
378 return platform_driver_register(&ieee802154fake_driver);
381 static __exit void fake_exit(void)
383 platform_driver_unregister(&ieee802154fake_driver);
384 platform_device_unregister(ieee802154fake_dev);
387 module_init(fake_init);
388 module_exit(fake_exit);
389 MODULE_LICENSE("GPL");