blob: 22a93bc764c5e75f682f5cdad74dcb99bae56399 [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
35struct wpan_phy *net_to_phy(struct net_device *dev)
36{
37 return container_of(dev->dev.parent, struct wpan_phy, dev);
38}
Sergey Lapin84594642009-06-08 12:18:51 +000039
Daniel Silverstone878fa892009-07-22 18:51:24 +020040/**
41 * fake_get_pan_id - Retrieve the PAN ID of the device.
42 * @dev: The network device to retrieve the PAN of.
43 *
44 * Return the ID of the PAN from the PIB.
45 */
Sergey Lapin84594642009-06-08 12:18:51 +000046static u16 fake_get_pan_id(struct net_device *dev)
47{
48 BUG_ON(dev->type != ARPHRD_IEEE802154);
49
50 return 0xeba1;
51}
52
Daniel Silverstone878fa892009-07-22 18:51:24 +020053/**
54 * fake_get_short_addr - Retrieve the short address of the device.
55 * @dev: The network device to retrieve the short address of.
56 *
57 * Returns the IEEE 802.15.4 short-form address cached for this
58 * device. If the device has not yet had a short address assigned
59 * then this should return 0xFFFF to indicate a lack of association.
60 */
Sergey Lapin84594642009-06-08 12:18:51 +000061static u16 fake_get_short_addr(struct net_device *dev)
62{
63 BUG_ON(dev->type != ARPHRD_IEEE802154);
64
65 return 0x1;
66}
67
Daniel Silverstone878fa892009-07-22 18:51:24 +020068/**
69 * fake_get_dsn - Retrieve the DSN of the device.
70 * @dev: The network device to retrieve the DSN for.
71 *
72 * Returns the IEEE 802.15.4 DSN for the network device.
73 * The DSN is the sequence number which will be added to each
74 * packet or MAC command frame by the MAC during transmission.
75 *
76 * DSN means 'Data Sequence Number'.
77 *
78 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
79 * document.
80 */
Sergey Lapin84594642009-06-08 12:18:51 +000081static u8 fake_get_dsn(struct net_device *dev)
82{
83 BUG_ON(dev->type != ARPHRD_IEEE802154);
84
85 return 0x00; /* DSN are implemented in HW, so return just 0 */
86}
87
Daniel Silverstone878fa892009-07-22 18:51:24 +020088/**
89 * fake_get_bsn - Retrieve the BSN of the device.
90 * @dev: The network device to retrieve the BSN for.
91 *
92 * Returns the IEEE 802.15.4 BSN for the network device.
93 * The BSN is the sequence number which will be added to each
94 * beacon frame sent by the MAC.
95 *
96 * BSN means 'Beacon Sequence Number'.
97 *
98 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
99 * document.
100 */
Sergey Lapin84594642009-06-08 12:18:51 +0000101static u8 fake_get_bsn(struct net_device *dev)
102{
103 BUG_ON(dev->type != ARPHRD_IEEE802154);
104
105 return 0x00; /* BSN are implemented in HW, so return just 0 */
106}
107
Daniel Silverstone878fa892009-07-22 18:51:24 +0200108/**
109 * 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,
122 struct ieee802154_addr *addr, u8 channel, u8 cap)
123{
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400124 struct wpan_phy *phy = net_to_phy(dev);
125
126 mutex_lock(&phy->pib_lock);
127 phy->current_channel = channel;
128 mutex_unlock(&phy->pib_lock);
129
Sergey Lapin84594642009-06-08 12:18:51 +0000130 /* We simply emulate it here */
131 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
132 IEEE802154_SUCCESS);
133}
134
Daniel Silverstone878fa892009-07-22 18:51:24 +0200135/**
136 * fake_assoc_resp - Send an association response to a device.
137 * @dev: The network device on which to send the response.
138 * @addr: The address of the device to respond to.
139 * @short_addr: The assigned short address for the device (if any).
140 * @status: The result of the association request.
141 *
142 * Queue the association response of the coordinator to another
143 * device's attempt to associate with the network which we
144 * coordinate. This is then added to the indirect-send queue to be
145 * transmitted to the end device when it polls for data.
146 *
147 * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
148 * 802.15.4-2006 document.
149 */
Sergey Lapin84594642009-06-08 12:18:51 +0000150static int fake_assoc_resp(struct net_device *dev,
151 struct ieee802154_addr *addr, u16 short_addr, u8 status)
152{
153 return 0;
154}
155
Daniel Silverstone878fa892009-07-22 18:51:24 +0200156/**
157 * fake_disassoc_req - Disassociate a device from a network.
158 * @dev: The network device on which we're disassociating a device.
159 * @addr: The device to disassociate from the network.
160 * @reason: The reason to give to the device for being disassociated.
161 *
162 * This sends a disassociation notification to the device being
163 * disassociated from the network.
164 *
165 * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
166 * document, with the reason described in 7.3.3.2.
167 */
Sergey Lapin84594642009-06-08 12:18:51 +0000168static int fake_disassoc_req(struct net_device *dev,
169 struct ieee802154_addr *addr, u8 reason)
170{
171 return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
172}
173
Daniel Silverstone878fa892009-07-22 18:51:24 +0200174/**
175 * fake_start_req - Start an IEEE 802.15.4 PAN.
176 * @dev: The network device on which to start the PAN.
177 * @addr: The coordinator address to use when starting the PAN.
178 * @channel: The channel on which to start the PAN.
179 * @bcn_ord: Beacon order.
180 * @sf_ord: Superframe order.
181 * @pan_coord: Whether or not we are the PAN coordinator or just
182 * requesting a realignment perhaps?
183 * @blx: Battery Life Extension feature bitfield.
184 * @coord_realign: Something to realign something else.
185 *
186 * If pan_coord is non-zero then this starts a network with the
187 * provided parameters, otherwise it attempts a coordinator
188 * realignment of the stated network instead.
189 *
190 * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
191 * document, with 7.3.8 describing coordinator realignment.
Daniel Silverstone878fa892009-07-22 18:51:24 +0200192 */
Sergey Lapin84594642009-06-08 12:18:51 +0000193static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
194 u8 channel,
195 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
196 u8 coord_realign)
197{
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400198 struct wpan_phy *phy = net_to_phy(dev);
199
200 mutex_lock(&phy->pib_lock);
201 phy->current_channel = channel;
202 mutex_unlock(&phy->pib_lock);
203
Dmitry Baryshkov798c7522009-08-07 02:58:45 +0000204 /* We don't emulate beacons here at all, so START should fail */
205 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
Sergey Lapin84594642009-06-08 12:18:51 +0000206 return 0;
207}
208
Daniel Silverstone878fa892009-07-22 18:51:24 +0200209/**
210 * fake_scan_req - Start a channel scan.
211 * @dev: The network device on which to perform a channel scan.
212 * @type: The type of scan to perform.
213 * @channels: The channel bitmask to scan.
214 * @duration: How long to spend on each channel.
215 *
216 * This starts either a passive (energy) scan or an active (PAN) scan
217 * on the channels indicated in the @channels bitmask. The duration of
218 * the scan is measured in terms of superframe duration. Specifically,
219 * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
220 * channel.
221 *
222 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
223 */
Sergey Lapin84594642009-06-08 12:18:51 +0000224static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
225 u8 duration)
226{
227 u8 edl[27] = {};
228 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
229 channels,
230 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
231}
232
233static struct ieee802154_mlme_ops fake_mlme = {
234 .assoc_req = fake_assoc_req,
235 .assoc_resp = fake_assoc_resp,
236 .disassoc_req = fake_disassoc_req,
237 .start_req = fake_start_req,
238 .scan_req = fake_scan_req,
239
240 .get_pan_id = fake_get_pan_id,
241 .get_short_addr = fake_get_short_addr,
242 .get_dsn = fake_get_dsn,
243 .get_bsn = fake_get_bsn,
244};
245
246static int ieee802154_fake_open(struct net_device *dev)
247{
248 netif_start_queue(dev);
249 return 0;
250}
251
252static int ieee802154_fake_close(struct net_device *dev)
253{
254 netif_stop_queue(dev);
255 return 0;
256}
257
258static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
259{
260 skb->iif = dev->ifindex;
261 skb->dev = dev;
262 dev->stats.tx_packets++;
263 dev->stats.tx_bytes += skb->len;
264
265 dev->trans_start = jiffies;
266
267 /* FIXME: do hardware work here ... */
268
Patrick McHardy6ed10652009-06-23 06:03:08 +0000269 return NETDEV_TX_OK;
Sergey Lapin84594642009-06-08 12:18:51 +0000270}
271
272
273static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
274 int cmd)
275{
276 struct sockaddr_ieee802154 *sa =
277 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
278 u16 pan_id, short_addr;
279
280 switch (cmd) {
281 case SIOCGIFADDR:
282 /* FIXME: fixed here, get from device IRL */
283 pan_id = fake_get_pan_id(dev);
284 short_addr = fake_get_short_addr(dev);
285 if (pan_id == IEEE802154_PANID_BROADCAST ||
286 short_addr == IEEE802154_ADDR_BROADCAST)
287 return -EADDRNOTAVAIL;
288
289 sa->family = AF_IEEE802154;
290 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
291 sa->addr.pan_id = pan_id;
292 sa->addr.short_addr = short_addr;
293 return 0;
294 }
295 return -ENOIOCTLCMD;
296}
297
298static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
299{
300 return -EBUSY; /* HW address is built into the device */
301}
302
303static const struct net_device_ops fake_ops = {
304 .ndo_open = ieee802154_fake_open,
305 .ndo_stop = ieee802154_fake_close,
306 .ndo_start_xmit = ieee802154_fake_xmit,
307 .ndo_do_ioctl = ieee802154_fake_ioctl,
308 .ndo_set_mac_address = ieee802154_fake_mac_addr,
309};
310
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400311static void ieee802154_fake_destruct(struct net_device *dev)
312{
313 struct wpan_phy *phy = net_to_phy(dev);
314
315 wpan_phy_unregister(phy);
316 free_netdev(dev);
317 wpan_phy_free(phy);
318}
Sergey Lapin84594642009-06-08 12:18:51 +0000319
320static void ieee802154_fake_setup(struct net_device *dev)
321{
322 dev->addr_len = IEEE802154_ADDR_LEN;
323 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
324 dev->features = NETIF_F_NO_CSUM;
325 dev->needed_tailroom = 2; /* FCS */
326 dev->mtu = 127;
327 dev->tx_queue_len = 10;
328 dev->type = ARPHRD_IEEE802154;
329 dev->flags = IFF_NOARP | IFF_BROADCAST;
330 dev->watchdog_timeo = 0;
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400331 dev->destructor = ieee802154_fake_destruct;
Sergey Lapin84594642009-06-08 12:18:51 +0000332}
333
334
335static int __devinit ieee802154fake_probe(struct platform_device *pdev)
336{
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400337 struct net_device *dev;
338 struct wpan_phy *phy = wpan_phy_alloc(0);
Sergey Lapin84594642009-06-08 12:18:51 +0000339 int err;
340
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400341 if (!phy)
Sergey Lapin84594642009-06-08 12:18:51 +0000342 return -ENOMEM;
343
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400344 dev = alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
345 if (!dev) {
346 wpan_phy_free(phy);
347 return -ENOMEM;
348 }
349
350 phy->dev.platform_data = dev;
351
Sergey Lapin84594642009-06-08 12:18:51 +0000352 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
353 dev->addr_len);
354 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
355
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400356 phy->channels_supported = (1 << 27) - 1;
357 phy->transmit_power = 0xbf;
358
Sergey Lapin84594642009-06-08 12:18:51 +0000359 dev->netdev_ops = &fake_ops;
360 dev->ml_priv = &fake_mlme;
361
362 /*
363 * If the name is a format string the caller wants us to do a
364 * name allocation.
365 */
366 if (strchr(dev->name, '%')) {
367 err = dev_alloc_name(dev, dev->name);
368 if (err < 0)
369 goto out;
370 }
371
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400372 SET_NETDEV_DEV(dev, &phy->dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000373
374 platform_set_drvdata(pdev, dev);
375
Dmitry Eremin-Solenikov81f95102009-08-19 18:56:57 +0400376 err = wpan_phy_register(&pdev->dev, phy);
377 if (err)
378 goto out;
379
Sergey Lapin84594642009-06-08 12:18:51 +0000380 err = register_netdev(dev);
381 if (err < 0)
382 goto out;
383
Sergey Lapin84594642009-06-08 12:18:51 +0000384 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
385 return 0;
386
387out:
388 unregister_netdev(dev);
389 return err;
390}
391
392static int __devexit ieee802154fake_remove(struct platform_device *pdev)
393{
394 struct net_device *dev = platform_get_drvdata(pdev);
395 unregister_netdev(dev);
Sergey Lapin84594642009-06-08 12:18:51 +0000396 return 0;
397}
398
399static struct platform_device *ieee802154fake_dev;
400
401static struct platform_driver ieee802154fake_driver = {
402 .probe = ieee802154fake_probe,
403 .remove = __devexit_p(ieee802154fake_remove),
404 .driver = {
405 .name = "ieee802154hardmac",
406 .owner = THIS_MODULE,
407 },
408};
409
410static __init int fake_init(void)
411{
412 ieee802154fake_dev = platform_device_register_simple(
413 "ieee802154hardmac", -1, NULL, 0);
414 return platform_driver_register(&ieee802154fake_driver);
415}
416
417static __exit void fake_exit(void)
418{
419 platform_driver_unregister(&ieee802154fake_driver);
420 platform_device_unregister(ieee802154fake_dev);
421}
422
423module_init(fake_init);
424module_exit(fake_exit);
425MODULE_LICENSE("GPL");
426