Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fakehard.c
Go to the documentation of this file.
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 <[email protected]>
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 
29 #include <net/af_ieee802154.h>
30 #include <net/ieee802154_netdev.h>
31 #include <net/ieee802154.h>
32 #include <net/nl802154.h>
33 #include <net/wpan-phy.h>
34 
35 struct fakehard_priv {
36  struct wpan_phy *phy;
37 };
38 
39 static struct wpan_phy *fake_to_phy(const struct net_device *dev)
40 {
41  struct fakehard_priv *priv = netdev_priv(dev);
42  return priv->phy;
43 }
44 
54 static struct wpan_phy *fake_get_phy(const struct net_device *dev)
55 {
56  struct wpan_phy *phy = fake_to_phy(dev);
57  return to_phy(get_device(&phy->dev));
58 }
59 
66 static u16 fake_get_pan_id(const struct net_device *dev)
67 {
68  BUG_ON(dev->type != ARPHRD_IEEE802154);
69 
70  return 0xeba1;
71 }
72 
81 static u16 fake_get_short_addr(const struct net_device *dev)
82 {
83  BUG_ON(dev->type != ARPHRD_IEEE802154);
84 
85  return 0x1;
86 }
87 
101 static u8 fake_get_dsn(const struct net_device *dev)
102 {
103  BUG_ON(dev->type != ARPHRD_IEEE802154);
104 
105  return 0x00; /* DSN are implemented in HW, so return just 0 */
106 }
107 
121 static u8 fake_get_bsn(const struct net_device *dev)
122 {
123  BUG_ON(dev->type != ARPHRD_IEEE802154);
124 
125  return 0x00; /* BSN are implemented in HW, so return just 0 */
126 }
127 
141 static int fake_assoc_req(struct net_device *dev,
142  struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
143 {
144  struct wpan_phy *phy = fake_to_phy(dev);
145 
146  mutex_lock(&phy->pib_lock);
147  phy->current_channel = channel;
148  phy->current_page = page;
149  mutex_unlock(&phy->pib_lock);
150 
151  /* We simply emulate it here */
152  return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
154 }
155 
171 static int fake_assoc_resp(struct net_device *dev,
172  struct ieee802154_addr *addr, u16 short_addr, u8 status)
173 {
174  return 0;
175 }
176 
189 static int fake_disassoc_req(struct net_device *dev,
190  struct ieee802154_addr *addr, u8 reason)
191 {
193 }
194 
214 static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
215  u8 channel, u8 page,
216  u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
217  u8 coord_realign)
218 {
219  struct wpan_phy *phy = fake_to_phy(dev);
220 
221  mutex_lock(&phy->pib_lock);
222  phy->current_channel = channel;
223  phy->current_page = page;
224  mutex_unlock(&phy->pib_lock);
225 
226  /* We don't emulate beacons here at all, so START should fail */
228  return 0;
229 }
230 
246 static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
247  u8 page, u8 duration)
248 {
249  u8 edl[27] = {};
251  channels, page,
252  type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
253 }
254 
255 static struct ieee802154_mlme_ops fake_mlme = {
256  .assoc_req = fake_assoc_req,
257  .assoc_resp = fake_assoc_resp,
258  .disassoc_req = fake_disassoc_req,
259  .start_req = fake_start_req,
260  .scan_req = fake_scan_req,
261 
262  .get_phy = fake_get_phy,
263 
264  .get_pan_id = fake_get_pan_id,
265  .get_short_addr = fake_get_short_addr,
266  .get_dsn = fake_get_dsn,
267  .get_bsn = fake_get_bsn,
268 };
269 
270 static int ieee802154_fake_open(struct net_device *dev)
271 {
272  netif_start_queue(dev);
273  return 0;
274 }
275 
276 static int ieee802154_fake_close(struct net_device *dev)
277 {
278  netif_stop_queue(dev);
279  return 0;
280 }
281 
282 static netdev_tx_t ieee802154_fake_xmit(struct sk_buff *skb,
283  struct net_device *dev)
284 {
285  dev->stats.tx_packets++;
286  dev->stats.tx_bytes += skb->len;
287 
288  /* FIXME: do hardware work here ... */
289 
290  dev_kfree_skb(skb);
291  return NETDEV_TX_OK;
292 }
293 
294 
295 static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
296  int cmd)
297 {
298  struct sockaddr_ieee802154 *sa =
299  (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
300  u16 pan_id, short_addr;
301 
302  switch (cmd) {
303  case SIOCGIFADDR:
304  /* FIXME: fixed here, get from device IRL */
305  pan_id = fake_get_pan_id(dev);
306  short_addr = fake_get_short_addr(dev);
307  if (pan_id == IEEE802154_PANID_BROADCAST ||
308  short_addr == IEEE802154_ADDR_BROADCAST)
309  return -EADDRNOTAVAIL;
310 
311  sa->family = AF_IEEE802154;
312  sa->addr.addr_type = IEEE802154_ADDR_SHORT;
313  sa->addr.pan_id = pan_id;
314  sa->addr.short_addr = short_addr;
315  return 0;
316  }
317  return -ENOIOCTLCMD;
318 }
319 
320 static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
321 {
322  return -EBUSY; /* HW address is built into the device */
323 }
324 
325 static const struct net_device_ops fake_ops = {
326  .ndo_open = ieee802154_fake_open,
327  .ndo_stop = ieee802154_fake_close,
328  .ndo_start_xmit = ieee802154_fake_xmit,
329  .ndo_do_ioctl = ieee802154_fake_ioctl,
330  .ndo_set_mac_address = ieee802154_fake_mac_addr,
331 };
332 
333 static void ieee802154_fake_destruct(struct net_device *dev)
334 {
335  struct wpan_phy *phy = fake_to_phy(dev);
336 
337  wpan_phy_unregister(phy);
338  free_netdev(dev);
339  wpan_phy_free(phy);
340 }
341 
342 static void ieee802154_fake_setup(struct net_device *dev)
343 {
345  memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
346  dev->features = NETIF_F_HW_CSUM;
347  dev->needed_tailroom = 2; /* FCS */
348  dev->mtu = 127;
349  dev->tx_queue_len = 10;
350  dev->type = ARPHRD_IEEE802154;
351  dev->flags = IFF_NOARP | IFF_BROADCAST;
352  dev->watchdog_timeo = 0;
353  dev->destructor = ieee802154_fake_destruct;
354 }
355 
356 
357 static int __devinit ieee802154fake_probe(struct platform_device *pdev)
358 {
359  struct net_device *dev;
360  struct fakehard_priv *priv;
361  struct wpan_phy *phy = wpan_phy_alloc(0);
362  int err;
363 
364  if (!phy)
365  return -ENOMEM;
366 
367  dev = alloc_netdev(sizeof(struct fakehard_priv), "hardwpan%d", ieee802154_fake_setup);
368  if (!dev) {
369  wpan_phy_free(phy);
370  return -ENOMEM;
371  }
372 
373  memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
374  dev->addr_len);
375  memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
376 
377  /*
378  * For now we'd like to emulate 2.4 GHz-only device,
379  * both O-QPSK and CSS
380  */
381  /* 2.4 GHz O-QPSK 802.15.4-2003 */
382  phy->channels_supported[0] |= 0x7FFF800;
383  /* 2.4 GHz CSS 802.15.4a-2007 */
384  phy->channels_supported[3] |= 0x3fff;
385 
386  phy->transmit_power = 0xbf;
387 
388  dev->netdev_ops = &fake_ops;
389  dev->ml_priv = &fake_mlme;
390 
391  priv = netdev_priv(dev);
392  priv->phy = phy;
393 
394  wpan_phy_set_dev(phy, &pdev->dev);
395  SET_NETDEV_DEV(dev, &phy->dev);
396 
397  platform_set_drvdata(pdev, dev);
398 
399  err = wpan_phy_register(phy);
400  if (err)
401  goto out;
402 
403  err = register_netdev(dev);
404  if (err < 0)
405  goto out;
406 
407  dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
408  return 0;
409 
410 out:
411  unregister_netdev(dev);
412  return err;
413 }
414 
415 static int __devexit ieee802154fake_remove(struct platform_device *pdev)
416 {
417  struct net_device *dev = platform_get_drvdata(pdev);
418  unregister_netdev(dev);
419  return 0;
420 }
421 
422 static struct platform_device *ieee802154fake_dev;
423 
424 static struct platform_driver ieee802154fake_driver = {
425  .probe = ieee802154fake_probe,
426  .remove = __devexit_p(ieee802154fake_remove),
427  .driver = {
428  .name = "ieee802154hardmac",
429  .owner = THIS_MODULE,
430  },
431 };
432 
433 static __init int fake_init(void)
434 {
435  ieee802154fake_dev = platform_device_register_simple(
436  "ieee802154hardmac", -1, NULL, 0);
437  return platform_driver_register(&ieee802154fake_driver);
438 }
439 
440 static __exit void fake_exit(void)
441 {
442  platform_driver_unregister(&ieee802154fake_driver);
443  platform_device_unregister(ieee802154fake_dev);
444 }
445 
446 module_init(fake_init);
447 module_exit(fake_exit);
448 MODULE_LICENSE("GPL");