blob: 8a52e6efa2395e121b3b0a5a2c264dc4d2e554f2 [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.
180 *
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.
186 */
Sergey Lapin84594642009-06-08 12:18:51 +0000187static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
188 u8 channel,
189 u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
190 u8 coord_realign)
191{
192 return 0;
193}
194
Daniel Silverstone878fa892009-07-22 18:51:24 +0200195/**
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.
201 *
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
206 * channel.
207 *
208 * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
209 */
Sergey Lapin84594642009-06-08 12:18:51 +0000210static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
211 u8 duration)
212{
213 u8 edl[27] = {};
214 return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
215 channels,
216 type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
217}
218
219static 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,
225
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,
230};
231
232static int ieee802154_fake_open(struct net_device *dev)
233{
234 netif_start_queue(dev);
235 return 0;
236}
237
238static int ieee802154_fake_close(struct net_device *dev)
239{
240 netif_stop_queue(dev);
241 return 0;
242}
243
244static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
245{
246 skb->iif = dev->ifindex;
247 skb->dev = dev;
248 dev->stats.tx_packets++;
249 dev->stats.tx_bytes += skb->len;
250
251 dev->trans_start = jiffies;
252
253 /* FIXME: do hardware work here ... */
254
255 return 0;
256}
257
258
259static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
260 int cmd)
261{
262 struct sockaddr_ieee802154 *sa =
263 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
264 u16 pan_id, short_addr;
265
266 switch (cmd) {
267 case SIOCGIFADDR:
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;
274
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;
279 return 0;
280 }
281 return -ENOIOCTLCMD;
282}
283
284static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
285{
286 return -EBUSY; /* HW address is built into the device */
287}
288
289static 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,
295};
296
297
298static void ieee802154_fake_setup(struct net_device *dev)
299{
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 */
304 dev->mtu = 127;
305 dev->tx_queue_len = 10;
306 dev->type = ARPHRD_IEEE802154;
307 dev->flags = IFF_NOARP | IFF_BROADCAST;
308 dev->watchdog_timeo = 0;
309}
310
311
312static int __devinit ieee802154fake_probe(struct platform_device *pdev)
313{
314 struct net_device *dev =
315 alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
316 int err;
317
318 if (!dev)
319 return -ENOMEM;
320
321 memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
322 dev->addr_len);
323 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
324
325 dev->netdev_ops = &fake_ops;
326 dev->ml_priv = &fake_mlme;
327
328 /*
329 * If the name is a format string the caller wants us to do a
330 * name allocation.
331 */
332 if (strchr(dev->name, '%')) {
333 err = dev_alloc_name(dev, dev->name);
334 if (err < 0)
335 goto out;
336 }
337
338 SET_NETDEV_DEV(dev, &pdev->dev);
339
340 platform_set_drvdata(pdev, dev);
341
342 err = register_netdev(dev);
343 if (err < 0)
344 goto out;
345
346
347 dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
348 return 0;
349
350out:
351 unregister_netdev(dev);
352 return err;
353}
354
355static int __devexit ieee802154fake_remove(struct platform_device *pdev)
356{
357 struct net_device *dev = platform_get_drvdata(pdev);
358 unregister_netdev(dev);
359 free_netdev(dev);
360 return 0;
361}
362
363static struct platform_device *ieee802154fake_dev;
364
365static struct platform_driver ieee802154fake_driver = {
366 .probe = ieee802154fake_probe,
367 .remove = __devexit_p(ieee802154fake_remove),
368 .driver = {
369 .name = "ieee802154hardmac",
370 .owner = THIS_MODULE,
371 },
372};
373
374static __init int fake_init(void)
375{
376 ieee802154fake_dev = platform_device_register_simple(
377 "ieee802154hardmac", -1, NULL, 0);
378 return platform_driver_register(&ieee802154fake_driver);
379}
380
381static __exit void fake_exit(void)
382{
383 platform_driver_unregister(&ieee802154fake_driver);
384 platform_device_unregister(ieee802154fake_dev);
385}
386
387module_init(fake_init);
388module_exit(fake_exit);
389MODULE_LICENSE("GPL");
390