blob: 262536fae905ced89d8f2b4a65e8e0b9464d63ae [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>
Sergey Lapin84594642009-06-08 12:18:51 +000033
Daniel Silverstone878fa892009-07-22 18:51:24 +020034/**
35 * fake_get_pan_id - Retrieve the PAN ID of the device.
36 * @dev: The network device to retrieve the PAN of.
37 *
38 * Return the ID of the PAN from the PIB.
39 */
Sergey Lapin84594642009-06-08 12:18:51 +000040static u16 fake_get_pan_id(struct net_device *dev)
41{
42 BUG_ON(dev->type != ARPHRD_IEEE802154);
43
44 return 0xeba1;
45}
46
Daniel Silverstone878fa892009-07-22 18:51:24 +020047/**
48 * fake_get_short_addr - Retrieve the short address of the device.
49 * @dev: The network device to retrieve the short address of.
50 *
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.
54 */
Sergey Lapin84594642009-06-08 12:18:51 +000055static u16 fake_get_short_addr(struct net_device *dev)
56{
57 BUG_ON(dev->type != ARPHRD_IEEE802154);
58
59 return 0x1;
60}
61
Daniel Silverstone878fa892009-07-22 18:51:24 +020062/**
63 * fake_get_dsn - Retrieve the DSN of the device.
64 * @dev: The network device to retrieve the DSN for.
65 *
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.
69 *
70 * DSN means 'Data Sequence Number'.
71 *
72 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
73 * document.
74 */
Sergey Lapin84594642009-06-08 12:18:51 +000075static u8 fake_get_dsn(struct net_device *dev)
76{
77 BUG_ON(dev->type != ARPHRD_IEEE802154);
78
79 return 0x00; /* DSN are implemented in HW, so return just 0 */
80}
81
Daniel Silverstone878fa892009-07-22 18:51:24 +020082/**
83 * fake_get_bsn - Retrieve the BSN of the device.
84 * @dev: The network device to retrieve the BSN for.
85 *
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.
89 *
90 * BSN means 'Beacon Sequence Number'.
91 *
92 * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
93 * document.
94 */
Sergey Lapin84594642009-06-08 12:18:51 +000095static u8 fake_get_bsn(struct net_device *dev)
96{
97 BUG_ON(dev->type != ARPHRD_IEEE802154);
98
99 return 0x00; /* BSN are implemented in HW, so return just 0 */
100}
101
Daniel Silverstone878fa892009-07-22 18:51:24 +0200102/**
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.
108 *
109 * Start an association with a coordinator. The coordinator's address
110 * and PAN ID can be found in @addr.
111 *
112 * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
113 * 802.15.4-2006 document.
114 */
Sergey Lapin84594642009-06-08 12:18:51 +0000115static int fake_assoc_req(struct net_device *dev,
116 struct ieee802154_addr *addr, u8 channel, u8 cap)
117{
118 /* We simply emulate it here */
119 return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
120 IEEE802154_SUCCESS);
121}
122
Daniel Silverstone878fa892009-07-22 18:51:24 +0200123/**
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.
129 *
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.
134 *
135 * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
136 * 802.15.4-2006 document.
137 */
Sergey Lapin84594642009-06-08 12:18:51 +0000138static int fake_assoc_resp(struct net_device *dev,
139 struct ieee802154_addr *addr, u16 short_addr, u8 status)
140{
141 return 0;
142}
143
Daniel Silverstone878fa892009-07-22 18:51:24 +0200144/**
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.
149 *
150 * This sends a disassociation notification to the device being
151 * disassociated from the network.
152 *
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.
155 */
Sergey Lapin84594642009-06-08 12:18:51 +0000156static int fake_disassoc_req(struct net_device *dev,
157 struct ieee802154_addr *addr, u8 reason)
158{
159 return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
160}
161
Daniel Silverstone878fa892009-07-22 18:51:24 +0200162/**
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.
173 *
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.
177 *
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.
Daniel Silverstone878fa892009-07-22 18:51:24 +0200180 */
Sergey Lapin84594642009-06-08 12:18:51 +0000181static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
182 u8 channel,
183 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
184 u8 coord_realign)
185{
Dmitry Baryshkov798c7522009-08-07 02:58:45 +0000186 /* We don't emulate beacons here at all, so START should fail */
187 ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
Sergey Lapin84594642009-06-08 12:18:51 +0000188 return 0;
189}
190
Daniel Silverstone878fa892009-07-22 18:51:24 +0200191/**
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.
197 *
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
202 * channel.
203 *
204 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
205 */
Sergey Lapin84594642009-06-08 12:18:51 +0000206static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
207 u8 duration)
208{
209 u8 edl[27] = {};
210 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
211 channels,
212 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
213}
214
215static 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,
221
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,
226};
227
228static int ieee802154_fake_open(struct net_device *dev)
229{
230 netif_start_queue(dev);
231 return 0;
232}
233
234static int ieee802154_fake_close(struct net_device *dev)
235{
236 netif_stop_queue(dev);
237 return 0;
238}
239
240static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
241{
242 skb->iif = dev->ifindex;
243 skb->dev = dev;
244 dev->stats.tx_packets++;
245 dev->stats.tx_bytes += skb->len;
246
247 dev->trans_start = jiffies;
248
249 /* FIXME: do hardware work here ... */
250
Patrick McHardy6ed10652009-06-23 06:03:08 +0000251 return NETDEV_TX_OK;
Sergey Lapin84594642009-06-08 12:18:51 +0000252}
253
254
255static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
256 int cmd)
257{
258 struct sockaddr_ieee802154 *sa =
259 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
260 u16 pan_id, short_addr;
261
262 switch (cmd) {
263 case SIOCGIFADDR:
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;
270
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;
275 return 0;
276 }
277 return -ENOIOCTLCMD;
278}
279
280static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
281{
282 return -EBUSY; /* HW address is built into the device */
283}
284
285static 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,
291};
292
293
294static void ieee802154_fake_setup(struct net_device *dev)
295{
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 */
300 dev->mtu = 127;
301 dev->tx_queue_len = 10;
302 dev->type = ARPHRD_IEEE802154;
303 dev->flags = IFF_NOARP | IFF_BROADCAST;
304 dev->watchdog_timeo = 0;
305}
306
307
308static int __devinit ieee802154fake_probe(struct platform_device *pdev)
309{
310 struct net_device *dev =
311 alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
312 int err;
313
314 if (!dev)
315 return -ENOMEM;
316
317 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
318 dev->addr_len);
319 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
320
321 dev->netdev_ops = &fake_ops;
322 dev->ml_priv = &fake_mlme;
323
324 /*
325 * If the name is a format string the caller wants us to do a
326 * name allocation.
327 */
328 if (strchr(dev->name, '%')) {
329 err = dev_alloc_name(dev, dev->name);
330 if (err < 0)
331 goto out;
332 }
333
334 SET_NETDEV_DEV(dev, &pdev->dev);
335
336 platform_set_drvdata(pdev, dev);
337
338 err = register_netdev(dev);
339 if (err < 0)
340 goto out;
341
342
343 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
344 return 0;
345
346out:
347 unregister_netdev(dev);
348 return err;
349}
350
351static int __devexit ieee802154fake_remove(struct platform_device *pdev)
352{
353 struct net_device *dev = platform_get_drvdata(pdev);
354 unregister_netdev(dev);
355 free_netdev(dev);
356 return 0;
357}
358
359static struct platform_device *ieee802154fake_dev;
360
361static struct platform_driver ieee802154fake_driver = {
362 .probe = ieee802154fake_probe,
363 .remove = __devexit_p(ieee802154fake_remove),
364 .driver = {
365 .name = "ieee802154hardmac",
366 .owner = THIS_MODULE,
367 },
368};
369
370static __init int fake_init(void)
371{
372 ieee802154fake_dev = platform_device_register_simple(
373 "ieee802154hardmac", -1, NULL, 0);
374 return platform_driver_register(&ieee802154fake_driver);
375}
376
377static __exit void fake_exit(void)
378{
379 platform_driver_unregister(&ieee802154fake_driver);
380 platform_device_unregister(ieee802154fake_dev);
381}
382
383module_init(fake_init);
384module_exit(fake_exit);
385MODULE_LICENSE("GPL");
386