Commit | Line | Data |
---|---|---|
8459464f SL |
1 | /* |
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 | ||
f0166e5e DES |
29 | #include <net/af_ieee802154.h> |
30 | #include <net/ieee802154_netdev.h> | |
31 | #include <net/ieee802154.h> | |
32 | #include <net/nl802154.h> | |
81f95103 DES |
33 | #include <net/wpan-phy.h> |
34 | ||
c83b2077 | 35 | static struct wpan_phy *net_to_phy(struct net_device *dev) |
81f95103 DES |
36 | { |
37 | return container_of(dev->dev.parent, struct wpan_phy, dev); | |
38 | } | |
8459464f | 39 | |
878fa89f DS |
40 | /** |
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 | */ | |
a9966b58 | 46 | static u16 fake_get_pan_id(const struct net_device *dev) |
8459464f SL |
47 | { |
48 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
49 | ||
50 | return 0xeba1; | |
51 | } | |
52 | ||
878fa89f DS |
53 | /** |
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 | */ | |
a9966b58 | 61 | static u16 fake_get_short_addr(const struct net_device *dev) |
8459464f SL |
62 | { |
63 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
64 | ||
65 | return 0x1; | |
66 | } | |
67 | ||
878fa89f DS |
68 | /** |
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 | */ | |
a9966b58 | 81 | static u8 fake_get_dsn(const struct net_device *dev) |
8459464f SL |
82 | { |
83 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
84 | ||
85 | return 0x00; /* DSN are implemented in HW, so return just 0 */ | |
86 | } | |
87 | ||
878fa89f DS |
88 | /** |
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 | */ | |
a9966b58 | 101 | static u8 fake_get_bsn(const struct net_device *dev) |
8459464f SL |
102 | { |
103 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
104 | ||
105 | return 0x00; /* BSN are implemented in HW, so return just 0 */ | |
106 | } | |
107 | ||
878fa89f DS |
108 | /** |
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 | */ | |
8459464f | 121 | static int fake_assoc_req(struct net_device *dev, |
16eea493 | 122 | struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap) |
8459464f | 123 | { |
81f95103 DES |
124 | struct wpan_phy *phy = net_to_phy(dev); |
125 | ||
126 | mutex_lock(&phy->pib_lock); | |
127 | phy->current_channel = channel; | |
16eea493 | 128 | phy->current_page = page; |
81f95103 DES |
129 | mutex_unlock(&phy->pib_lock); |
130 | ||
8459464f SL |
131 | /* We simply emulate it here */ |
132 | return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev), | |
133 | IEEE802154_SUCCESS); | |
134 | } | |
135 | ||
878fa89f DS |
136 | /** |
137 | * fake_assoc_resp - Send an association response to a device. | |
138 | * @dev: The network device on which to send the response. | |
139 | * @addr: The address of the device to respond to. | |
140 | * @short_addr: The assigned short address for the device (if any). | |
141 | * @status: The result of the association request. | |
142 | * | |
143 | * Queue the association response of the coordinator to another | |
144 | * device's attempt to associate with the network which we | |
145 | * coordinate. This is then added to the indirect-send queue to be | |
146 | * transmitted to the end device when it polls for data. | |
147 | * | |
148 | * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE | |
149 | * 802.15.4-2006 document. | |
150 | */ | |
8459464f SL |
151 | static int fake_assoc_resp(struct net_device *dev, |
152 | struct ieee802154_addr *addr, u16 short_addr, u8 status) | |
153 | { | |
154 | return 0; | |
155 | } | |
156 | ||
878fa89f DS |
157 | /** |
158 | * fake_disassoc_req - Disassociate a device from a network. | |
159 | * @dev: The network device on which we're disassociating a device. | |
160 | * @addr: The device to disassociate from the network. | |
161 | * @reason: The reason to give to the device for being disassociated. | |
162 | * | |
163 | * This sends a disassociation notification to the device being | |
164 | * disassociated from the network. | |
165 | * | |
166 | * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006 | |
167 | * document, with the reason described in 7.3.3.2. | |
168 | */ | |
8459464f SL |
169 | static int fake_disassoc_req(struct net_device *dev, |
170 | struct ieee802154_addr *addr, u8 reason) | |
171 | { | |
172 | return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS); | |
173 | } | |
174 | ||
878fa89f DS |
175 | /** |
176 | * fake_start_req - Start an IEEE 802.15.4 PAN. | |
177 | * @dev: The network device on which to start the PAN. | |
178 | * @addr: The coordinator address to use when starting the PAN. | |
179 | * @channel: The channel on which to start the PAN. | |
180 | * @bcn_ord: Beacon order. | |
181 | * @sf_ord: Superframe order. | |
182 | * @pan_coord: Whether or not we are the PAN coordinator or just | |
183 | * requesting a realignment perhaps? | |
184 | * @blx: Battery Life Extension feature bitfield. | |
185 | * @coord_realign: Something to realign something else. | |
186 | * | |
187 | * If pan_coord is non-zero then this starts a network with the | |
188 | * provided parameters, otherwise it attempts a coordinator | |
189 | * realignment of the stated network instead. | |
190 | * | |
191 | * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006 | |
192 | * document, with 7.3.8 describing coordinator realignment. | |
878fa89f | 193 | */ |
8459464f | 194 | static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr, |
16eea493 | 195 | u8 channel, u8 page, |
8459464f SL |
196 | u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx, |
197 | u8 coord_realign) | |
198 | { | |
81f95103 DES |
199 | struct wpan_phy *phy = net_to_phy(dev); |
200 | ||
201 | mutex_lock(&phy->pib_lock); | |
202 | phy->current_channel = channel; | |
16eea493 | 203 | phy->current_page = page; |
81f95103 DES |
204 | mutex_unlock(&phy->pib_lock); |
205 | ||
798c752b DB |
206 | /* We don't emulate beacons here at all, so START should fail */ |
207 | ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER); | |
8459464f SL |
208 | return 0; |
209 | } | |
210 | ||
878fa89f DS |
211 | /** |
212 | * fake_scan_req - Start a channel scan. | |
213 | * @dev: The network device on which to perform a channel scan. | |
214 | * @type: The type of scan to perform. | |
215 | * @channels: The channel bitmask to scan. | |
216 | * @duration: How long to spend on each channel. | |
217 | * | |
218 | * This starts either a passive (energy) scan or an active (PAN) scan | |
219 | * on the channels indicated in the @channels bitmask. The duration of | |
220 | * the scan is measured in terms of superframe duration. Specifically, | |
221 | * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each | |
222 | * channel. | |
223 | * | |
224 | * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document. | |
225 | */ | |
8459464f | 226 | static int fake_scan_req(struct net_device *dev, u8 type, u32 channels, |
16eea493 | 227 | u8 page, u8 duration) |
8459464f SL |
228 | { |
229 | u8 edl[27] = {}; | |
230 | return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type, | |
16eea493 | 231 | channels, page, |
8459464f SL |
232 | type == IEEE802154_MAC_SCAN_ED ? edl : NULL); |
233 | } | |
234 | ||
235 | static struct ieee802154_mlme_ops fake_mlme = { | |
236 | .assoc_req = fake_assoc_req, | |
237 | .assoc_resp = fake_assoc_resp, | |
238 | .disassoc_req = fake_disassoc_req, | |
239 | .start_req = fake_start_req, | |
240 | .scan_req = fake_scan_req, | |
241 | ||
242 | .get_pan_id = fake_get_pan_id, | |
243 | .get_short_addr = fake_get_short_addr, | |
244 | .get_dsn = fake_get_dsn, | |
245 | .get_bsn = fake_get_bsn, | |
246 | }; | |
247 | ||
248 | static int ieee802154_fake_open(struct net_device *dev) | |
249 | { | |
250 | netif_start_queue(dev); | |
251 | return 0; | |
252 | } | |
253 | ||
254 | static int ieee802154_fake_close(struct net_device *dev) | |
255 | { | |
256 | netif_stop_queue(dev); | |
257 | return 0; | |
258 | } | |
259 | ||
61357325 SH |
260 | static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb, |
261 | struct net_device *dev) | |
8459464f SL |
262 | { |
263 | skb->iif = dev->ifindex; | |
264 | skb->dev = dev; | |
265 | dev->stats.tx_packets++; | |
266 | dev->stats.tx_bytes += skb->len; | |
267 | ||
268 | dev->trans_start = jiffies; | |
269 | ||
270 | /* FIXME: do hardware work here ... */ | |
271 | ||
6ed10654 | 272 | return NETDEV_TX_OK; |
8459464f SL |
273 | } |
274 | ||
275 | ||
276 | static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr, | |
277 | int cmd) | |
278 | { | |
279 | struct sockaddr_ieee802154 *sa = | |
280 | (struct sockaddr_ieee802154 *)&ifr->ifr_addr; | |
281 | u16 pan_id, short_addr; | |
282 | ||
283 | switch (cmd) { | |
284 | case SIOCGIFADDR: | |
285 | /* FIXME: fixed here, get from device IRL */ | |
286 | pan_id = fake_get_pan_id(dev); | |
287 | short_addr = fake_get_short_addr(dev); | |
288 | if (pan_id == IEEE802154_PANID_BROADCAST || | |
289 | short_addr == IEEE802154_ADDR_BROADCAST) | |
290 | return -EADDRNOTAVAIL; | |
291 | ||
292 | sa->family = AF_IEEE802154; | |
293 | sa->addr.addr_type = IEEE802154_ADDR_SHORT; | |
294 | sa->addr.pan_id = pan_id; | |
295 | sa->addr.short_addr = short_addr; | |
296 | return 0; | |
297 | } | |
298 | return -ENOIOCTLCMD; | |
299 | } | |
300 | ||
301 | static int ieee802154_fake_mac_addr(struct net_device *dev, void *p) | |
302 | { | |
303 | return -EBUSY; /* HW address is built into the device */ | |
304 | } | |
305 | ||
306 | static const struct net_device_ops fake_ops = { | |
307 | .ndo_open = ieee802154_fake_open, | |
308 | .ndo_stop = ieee802154_fake_close, | |
309 | .ndo_start_xmit = ieee802154_fake_xmit, | |
310 | .ndo_do_ioctl = ieee802154_fake_ioctl, | |
311 | .ndo_set_mac_address = ieee802154_fake_mac_addr, | |
312 | }; | |
313 | ||
81f95103 DES |
314 | static void ieee802154_fake_destruct(struct net_device *dev) |
315 | { | |
316 | struct wpan_phy *phy = net_to_phy(dev); | |
317 | ||
318 | wpan_phy_unregister(phy); | |
319 | free_netdev(dev); | |
320 | wpan_phy_free(phy); | |
321 | } | |
8459464f SL |
322 | |
323 | static void ieee802154_fake_setup(struct net_device *dev) | |
324 | { | |
325 | dev->addr_len = IEEE802154_ADDR_LEN; | |
326 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); | |
327 | dev->features = NETIF_F_NO_CSUM; | |
328 | dev->needed_tailroom = 2; /* FCS */ | |
329 | dev->mtu = 127; | |
330 | dev->tx_queue_len = 10; | |
331 | dev->type = ARPHRD_IEEE802154; | |
332 | dev->flags = IFF_NOARP | IFF_BROADCAST; | |
333 | dev->watchdog_timeo = 0; | |
81f95103 | 334 | dev->destructor = ieee802154_fake_destruct; |
8459464f SL |
335 | } |
336 | ||
337 | ||
338 | static int __devinit ieee802154fake_probe(struct platform_device *pdev) | |
339 | { | |
81f95103 DES |
340 | struct net_device *dev; |
341 | struct wpan_phy *phy = wpan_phy_alloc(0); | |
8459464f SL |
342 | int err; |
343 | ||
81f95103 DES |
344 | if (!phy) |
345 | return -ENOMEM; | |
346 | ||
347 | dev = alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup); | |
348 | if (!dev) { | |
349 | wpan_phy_free(phy); | |
8459464f | 350 | return -ENOMEM; |
81f95103 DES |
351 | } |
352 | ||
353 | phy->dev.platform_data = dev; | |
8459464f SL |
354 | |
355 | memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef", | |
356 | dev->addr_len); | |
357 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
358 | ||
478e87c2 DES |
359 | /* |
360 | * For now we'd like to emulate 2.4 GHz-only device, | |
361 | * both O-QPSK and CSS | |
362 | */ | |
363 | /* 2.4 GHz O-QPSK 802.15.4-2003 */ | |
364 | phy->channels_supported[0] |= 0x7FFF800; | |
365 | /* 2.4 GHz CSS 802.15.4a-2007 */ | |
366 | phy->channels_supported[3] |= 0x3fff; | |
367 | ||
81f95103 DES |
368 | phy->transmit_power = 0xbf; |
369 | ||
8459464f SL |
370 | dev->netdev_ops = &fake_ops; |
371 | dev->ml_priv = &fake_mlme; | |
372 | ||
373 | /* | |
374 | * If the name is a format string the caller wants us to do a | |
375 | * name allocation. | |
376 | */ | |
377 | if (strchr(dev->name, '%')) { | |
378 | err = dev_alloc_name(dev, dev->name); | |
379 | if (err < 0) | |
380 | goto out; | |
381 | } | |
382 | ||
e9cf356c | 383 | wpan_phy_set_dev(phy, &pdev->dev); |
81f95103 | 384 | SET_NETDEV_DEV(dev, &phy->dev); |
8459464f SL |
385 | |
386 | platform_set_drvdata(pdev, dev); | |
387 | ||
e9cf356c | 388 | err = wpan_phy_register(phy); |
81f95103 DES |
389 | if (err) |
390 | goto out; | |
391 | ||
8459464f SL |
392 | err = register_netdev(dev); |
393 | if (err < 0) | |
394 | goto out; | |
395 | ||
8459464f SL |
396 | dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n"); |
397 | return 0; | |
398 | ||
399 | out: | |
400 | unregister_netdev(dev); | |
401 | return err; | |
402 | } | |
403 | ||
404 | static int __devexit ieee802154fake_remove(struct platform_device *pdev) | |
405 | { | |
406 | struct net_device *dev = platform_get_drvdata(pdev); | |
407 | unregister_netdev(dev); | |
8459464f SL |
408 | return 0; |
409 | } | |
410 | ||
411 | static struct platform_device *ieee802154fake_dev; | |
412 | ||
413 | static struct platform_driver ieee802154fake_driver = { | |
414 | .probe = ieee802154fake_probe, | |
415 | .remove = __devexit_p(ieee802154fake_remove), | |
416 | .driver = { | |
417 | .name = "ieee802154hardmac", | |
418 | .owner = THIS_MODULE, | |
419 | }, | |
420 | }; | |
421 | ||
422 | static __init int fake_init(void) | |
423 | { | |
424 | ieee802154fake_dev = platform_device_register_simple( | |
425 | "ieee802154hardmac", -1, NULL, 0); | |
426 | return platform_driver_register(&ieee802154fake_driver); | |
427 | } | |
428 | ||
429 | static __exit void fake_exit(void) | |
430 | { | |
431 | platform_driver_unregister(&ieee802154fake_driver); | |
432 | platform_device_unregister(ieee802154fake_dev); | |
433 | } | |
434 | ||
435 | module_init(fake_init); | |
436 | module_exit(fake_exit); | |
437 | MODULE_LICENSE("GPL"); | |
438 |