fakehard.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include <net/ieee802154/af_ieee802154.h>
  29. #include <net/ieee802154/netdevice.h>
  30. #include <net/ieee802154/mac_def.h>
  31. #include <net/ieee802154/nl802154.h>
  32. static u16 fake_get_pan_id(struct net_device *dev)
  33. {
  34. BUG_ON(dev->type != ARPHRD_IEEE802154);
  35. return 0xeba1;
  36. }
  37. static u16 fake_get_short_addr(struct net_device *dev)
  38. {
  39. BUG_ON(dev->type != ARPHRD_IEEE802154);
  40. return 0x1;
  41. }
  42. static u8 fake_get_dsn(struct net_device *dev)
  43. {
  44. BUG_ON(dev->type != ARPHRD_IEEE802154);
  45. return 0x00; /* DSN are implemented in HW, so return just 0 */
  46. }
  47. static u8 fake_get_bsn(struct net_device *dev)
  48. {
  49. BUG_ON(dev->type != ARPHRD_IEEE802154);
  50. return 0x00; /* BSN are implemented in HW, so return just 0 */
  51. }
  52. static int fake_assoc_req(struct net_device *dev,
  53. struct ieee802154_addr *addr, u8 channel, u8 cap)
  54. {
  55. /* We simply emulate it here */
  56. return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
  57. IEEE802154_SUCCESS);
  58. }
  59. static int fake_assoc_resp(struct net_device *dev,
  60. struct ieee802154_addr *addr, u16 short_addr, u8 status)
  61. {
  62. return 0;
  63. }
  64. static int fake_disassoc_req(struct net_device *dev,
  65. struct ieee802154_addr *addr, u8 reason)
  66. {
  67. return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
  68. }
  69. static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
  70. u8 channel,
  71. u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
  72. u8 coord_realign)
  73. {
  74. return 0;
  75. }
  76. static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
  77. u8 duration)
  78. {
  79. u8 edl[27] = {};
  80. return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
  81. channels,
  82. type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
  83. }
  84. static struct ieee802154_mlme_ops fake_mlme = {
  85. .assoc_req = fake_assoc_req,
  86. .assoc_resp = fake_assoc_resp,
  87. .disassoc_req = fake_disassoc_req,
  88. .start_req = fake_start_req,
  89. .scan_req = fake_scan_req,
  90. .get_pan_id = fake_get_pan_id,
  91. .get_short_addr = fake_get_short_addr,
  92. .get_dsn = fake_get_dsn,
  93. .get_bsn = fake_get_bsn,
  94. };
  95. static int ieee802154_fake_open(struct net_device *dev)
  96. {
  97. netif_start_queue(dev);
  98. return 0;
  99. }
  100. static int ieee802154_fake_close(struct net_device *dev)
  101. {
  102. netif_stop_queue(dev);
  103. return 0;
  104. }
  105. static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
  106. {
  107. skb->iif = dev->ifindex;
  108. skb->dev = dev;
  109. dev->stats.tx_packets++;
  110. dev->stats.tx_bytes += skb->len;
  111. dev->trans_start = jiffies;
  112. /* FIXME: do hardware work here ... */
  113. return 0;
  114. }
  115. static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
  116. int cmd)
  117. {
  118. struct sockaddr_ieee802154 *sa =
  119. (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
  120. u16 pan_id, short_addr;
  121. switch (cmd) {
  122. case SIOCGIFADDR:
  123. /* FIXME: fixed here, get from device IRL */
  124. pan_id = fake_get_pan_id(dev);
  125. short_addr = fake_get_short_addr(dev);
  126. if (pan_id == IEEE802154_PANID_BROADCAST ||
  127. short_addr == IEEE802154_ADDR_BROADCAST)
  128. return -EADDRNOTAVAIL;
  129. sa->family = AF_IEEE802154;
  130. sa->addr.addr_type = IEEE802154_ADDR_SHORT;
  131. sa->addr.pan_id = pan_id;
  132. sa->addr.short_addr = short_addr;
  133. return 0;
  134. }
  135. return -ENOIOCTLCMD;
  136. }
  137. static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
  138. {
  139. return -EBUSY; /* HW address is built into the device */
  140. }
  141. static const struct net_device_ops fake_ops = {
  142. .ndo_open = ieee802154_fake_open,
  143. .ndo_stop = ieee802154_fake_close,
  144. .ndo_start_xmit = ieee802154_fake_xmit,
  145. .ndo_do_ioctl = ieee802154_fake_ioctl,
  146. .ndo_set_mac_address = ieee802154_fake_mac_addr,
  147. };
  148. static void ieee802154_fake_setup(struct net_device *dev)
  149. {
  150. dev->addr_len = IEEE802154_ADDR_LEN;
  151. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  152. dev->features = NETIF_F_NO_CSUM;
  153. dev->needed_tailroom = 2; /* FCS */
  154. dev->mtu = 127;
  155. dev->tx_queue_len = 10;
  156. dev->type = ARPHRD_IEEE802154;
  157. dev->flags = IFF_NOARP | IFF_BROADCAST;
  158. dev->watchdog_timeo = 0;
  159. }
  160. static int __devinit ieee802154fake_probe(struct platform_device *pdev)
  161. {
  162. struct net_device *dev =
  163. alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
  164. int err;
  165. if (!dev)
  166. return -ENOMEM;
  167. memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
  168. dev->addr_len);
  169. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  170. dev->netdev_ops = &fake_ops;
  171. dev->ml_priv = &fake_mlme;
  172. /*
  173. * If the name is a format string the caller wants us to do a
  174. * name allocation.
  175. */
  176. if (strchr(dev->name, '%')) {
  177. err = dev_alloc_name(dev, dev->name);
  178. if (err < 0)
  179. goto out;
  180. }
  181. SET_NETDEV_DEV(dev, &pdev->dev);
  182. platform_set_drvdata(pdev, dev);
  183. err = register_netdev(dev);
  184. if (err < 0)
  185. goto out;
  186. dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
  187. return 0;
  188. out:
  189. unregister_netdev(dev);
  190. return err;
  191. }
  192. static int __devexit ieee802154fake_remove(struct platform_device *pdev)
  193. {
  194. struct net_device *dev = platform_get_drvdata(pdev);
  195. unregister_netdev(dev);
  196. free_netdev(dev);
  197. return 0;
  198. }
  199. static struct platform_device *ieee802154fake_dev;
  200. static struct platform_driver ieee802154fake_driver = {
  201. .probe = ieee802154fake_probe,
  202. .remove = __devexit_p(ieee802154fake_remove),
  203. .driver = {
  204. .name = "ieee802154hardmac",
  205. .owner = THIS_MODULE,
  206. },
  207. };
  208. static __init int fake_init(void)
  209. {
  210. ieee802154fake_dev = platform_device_register_simple(
  211. "ieee802154hardmac", -1, NULL, 0);
  212. return platform_driver_register(&ieee802154fake_driver);
  213. }
  214. static __exit void fake_exit(void)
  215. {
  216. platform_driver_unregister(&ieee802154fake_driver);
  217. platform_device_unregister(ieee802154fake_dev);
  218. }
  219. module_init(fake_init);
  220. module_exit(fake_exit);
  221. MODULE_LICENSE("GPL");