fakehard.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /**
  33. * fake_get_pan_id - Retrieve the PAN ID of the device.
  34. * @dev: The network device to retrieve the PAN of.
  35. *
  36. * Return the ID of the PAN from the PIB.
  37. */
  38. static u16 fake_get_pan_id(struct net_device *dev)
  39. {
  40. BUG_ON(dev->type != ARPHRD_IEEE802154);
  41. return 0xeba1;
  42. }
  43. /**
  44. * fake_get_short_addr - Retrieve the short address of the device.
  45. * @dev: The network device to retrieve the short address of.
  46. *
  47. * Returns the IEEE 802.15.4 short-form address cached for this
  48. * device. If the device has not yet had a short address assigned
  49. * then this should return 0xFFFF to indicate a lack of association.
  50. */
  51. static u16 fake_get_short_addr(struct net_device *dev)
  52. {
  53. BUG_ON(dev->type != ARPHRD_IEEE802154);
  54. return 0x1;
  55. }
  56. /**
  57. * fake_get_dsn - Retrieve the DSN of the device.
  58. * @dev: The network device to retrieve the DSN for.
  59. *
  60. * Returns the IEEE 802.15.4 DSN for the network device.
  61. * The DSN is the sequence number which will be added to each
  62. * packet or MAC command frame by the MAC during transmission.
  63. *
  64. * DSN means 'Data Sequence Number'.
  65. *
  66. * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
  67. * document.
  68. */
  69. static u8 fake_get_dsn(struct net_device *dev)
  70. {
  71. BUG_ON(dev->type != ARPHRD_IEEE802154);
  72. return 0x00; /* DSN are implemented in HW, so return just 0 */
  73. }
  74. /**
  75. * fake_get_bsn - Retrieve the BSN of the device.
  76. * @dev: The network device to retrieve the BSN for.
  77. *
  78. * Returns the IEEE 802.15.4 BSN for the network device.
  79. * The BSN is the sequence number which will be added to each
  80. * beacon frame sent by the MAC.
  81. *
  82. * BSN means 'Beacon Sequence Number'.
  83. *
  84. * Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
  85. * document.
  86. */
  87. static u8 fake_get_bsn(struct net_device *dev)
  88. {
  89. BUG_ON(dev->type != ARPHRD_IEEE802154);
  90. return 0x00; /* BSN are implemented in HW, so return just 0 */
  91. }
  92. /**
  93. * fake_assoc_req - Make an association request to the HW.
  94. * @dev: The network device which we are associating to a network.
  95. * @addr: The coordinator with which we wish to associate.
  96. * @channel: The channel on which to associate.
  97. * @cap: The capability information field to use in the association.
  98. *
  99. * Start an association with a coordinator. The coordinator's address
  100. * and PAN ID can be found in @addr.
  101. *
  102. * Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
  103. * 802.15.4-2006 document.
  104. */
  105. static int fake_assoc_req(struct net_device *dev,
  106. struct ieee802154_addr *addr, u8 channel, u8 cap)
  107. {
  108. /* We simply emulate it here */
  109. return ieee802154_nl_assoc_confirm(dev, fake_get_short_addr(dev),
  110. IEEE802154_SUCCESS);
  111. }
  112. /**
  113. * fake_assoc_resp - Send an association response to a device.
  114. * @dev: The network device on which to send the response.
  115. * @addr: The address of the device to respond to.
  116. * @short_addr: The assigned short address for the device (if any).
  117. * @status: The result of the association request.
  118. *
  119. * Queue the association response of the coordinator to another
  120. * device's attempt to associate with the network which we
  121. * coordinate. This is then added to the indirect-send queue to be
  122. * transmitted to the end device when it polls for data.
  123. *
  124. * Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
  125. * 802.15.4-2006 document.
  126. */
  127. static int fake_assoc_resp(struct net_device *dev,
  128. struct ieee802154_addr *addr, u16 short_addr, u8 status)
  129. {
  130. return 0;
  131. }
  132. /**
  133. * fake_disassoc_req - Disassociate a device from a network.
  134. * @dev: The network device on which we're disassociating a device.
  135. * @addr: The device to disassociate from the network.
  136. * @reason: The reason to give to the device for being disassociated.
  137. *
  138. * This sends a disassociation notification to the device being
  139. * disassociated from the network.
  140. *
  141. * Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
  142. * document, with the reason described in 7.3.3.2.
  143. */
  144. static int fake_disassoc_req(struct net_device *dev,
  145. struct ieee802154_addr *addr, u8 reason)
  146. {
  147. return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
  148. }
  149. /**
  150. * fake_start_req - Start an IEEE 802.15.4 PAN.
  151. * @dev: The network device on which to start the PAN.
  152. * @addr: The coordinator address to use when starting the PAN.
  153. * @channel: The channel on which to start the PAN.
  154. * @bcn_ord: Beacon order.
  155. * @sf_ord: Superframe order.
  156. * @pan_coord: Whether or not we are the PAN coordinator or just
  157. * requesting a realignment perhaps?
  158. * @blx: Battery Life Extension feature bitfield.
  159. * @coord_realign: Something to realign something else.
  160. *
  161. * If pan_coord is non-zero then this starts a network with the
  162. * provided parameters, otherwise it attempts a coordinator
  163. * realignment of the stated network instead.
  164. *
  165. * Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
  166. * document, with 7.3.8 describing coordinator realignment.
  167. *
  168. * Note: There is currently no way to notify the coordinator userland
  169. * program of whether or not the PAN has started successfully. As
  170. * such, the coordinator program cannot know when the MAC has
  171. * completed starting the network and will simply have to assume
  172. * completeness based on some form of time delay.
  173. */
  174. static int fake_start_req(struct net_device *dev, struct ieee802154_addr *addr,
  175. u8 channel,
  176. u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
  177. u8 coord_realign)
  178. {
  179. return 0;
  180. }
  181. /**
  182. * fake_scan_req - Start a channel scan.
  183. * @dev: The network device on which to perform a channel scan.
  184. * @type: The type of scan to perform.
  185. * @channels: The channel bitmask to scan.
  186. * @duration: How long to spend on each channel.
  187. *
  188. * This starts either a passive (energy) scan or an active (PAN) scan
  189. * on the channels indicated in the @channels bitmask. The duration of
  190. * the scan is measured in terms of superframe duration. Specifically,
  191. * the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
  192. * channel.
  193. *
  194. * Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
  195. */
  196. static int fake_scan_req(struct net_device *dev, u8 type, u32 channels,
  197. u8 duration)
  198. {
  199. u8 edl[27] = {};
  200. return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
  201. channels,
  202. type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
  203. }
  204. static struct ieee802154_mlme_ops fake_mlme = {
  205. .assoc_req = fake_assoc_req,
  206. .assoc_resp = fake_assoc_resp,
  207. .disassoc_req = fake_disassoc_req,
  208. .start_req = fake_start_req,
  209. .scan_req = fake_scan_req,
  210. .get_pan_id = fake_get_pan_id,
  211. .get_short_addr = fake_get_short_addr,
  212. .get_dsn = fake_get_dsn,
  213. .get_bsn = fake_get_bsn,
  214. };
  215. static int ieee802154_fake_open(struct net_device *dev)
  216. {
  217. netif_start_queue(dev);
  218. return 0;
  219. }
  220. static int ieee802154_fake_close(struct net_device *dev)
  221. {
  222. netif_stop_queue(dev);
  223. return 0;
  224. }
  225. static int ieee802154_fake_xmit(struct sk_buff *skb, struct net_device *dev)
  226. {
  227. skb->iif = dev->ifindex;
  228. skb->dev = dev;
  229. dev->stats.tx_packets++;
  230. dev->stats.tx_bytes += skb->len;
  231. dev->trans_start = jiffies;
  232. /* FIXME: do hardware work here ... */
  233. return 0;
  234. }
  235. static int ieee802154_fake_ioctl(struct net_device *dev, struct ifreq *ifr,
  236. int cmd)
  237. {
  238. struct sockaddr_ieee802154 *sa =
  239. (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
  240. u16 pan_id, short_addr;
  241. switch (cmd) {
  242. case SIOCGIFADDR:
  243. /* FIXME: fixed here, get from device IRL */
  244. pan_id = fake_get_pan_id(dev);
  245. short_addr = fake_get_short_addr(dev);
  246. if (pan_id == IEEE802154_PANID_BROADCAST ||
  247. short_addr == IEEE802154_ADDR_BROADCAST)
  248. return -EADDRNOTAVAIL;
  249. sa->family = AF_IEEE802154;
  250. sa->addr.addr_type = IEEE802154_ADDR_SHORT;
  251. sa->addr.pan_id = pan_id;
  252. sa->addr.short_addr = short_addr;
  253. return 0;
  254. }
  255. return -ENOIOCTLCMD;
  256. }
  257. static int ieee802154_fake_mac_addr(struct net_device *dev, void *p)
  258. {
  259. return -EBUSY; /* HW address is built into the device */
  260. }
  261. static const struct net_device_ops fake_ops = {
  262. .ndo_open = ieee802154_fake_open,
  263. .ndo_stop = ieee802154_fake_close,
  264. .ndo_start_xmit = ieee802154_fake_xmit,
  265. .ndo_do_ioctl = ieee802154_fake_ioctl,
  266. .ndo_set_mac_address = ieee802154_fake_mac_addr,
  267. };
  268. static void ieee802154_fake_setup(struct net_device *dev)
  269. {
  270. dev->addr_len = IEEE802154_ADDR_LEN;
  271. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  272. dev->features = NETIF_F_NO_CSUM;
  273. dev->needed_tailroom = 2; /* FCS */
  274. dev->mtu = 127;
  275. dev->tx_queue_len = 10;
  276. dev->type = ARPHRD_IEEE802154;
  277. dev->flags = IFF_NOARP | IFF_BROADCAST;
  278. dev->watchdog_timeo = 0;
  279. }
  280. static int __devinit ieee802154fake_probe(struct platform_device *pdev)
  281. {
  282. struct net_device *dev =
  283. alloc_netdev(0, "hardwpan%d", ieee802154_fake_setup);
  284. int err;
  285. if (!dev)
  286. return -ENOMEM;
  287. memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
  288. dev->addr_len);
  289. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  290. dev->netdev_ops = &fake_ops;
  291. dev->ml_priv = &fake_mlme;
  292. /*
  293. * If the name is a format string the caller wants us to do a
  294. * name allocation.
  295. */
  296. if (strchr(dev->name, '%')) {
  297. err = dev_alloc_name(dev, dev->name);
  298. if (err < 0)
  299. goto out;
  300. }
  301. SET_NETDEV_DEV(dev, &pdev->dev);
  302. platform_set_drvdata(pdev, dev);
  303. err = register_netdev(dev);
  304. if (err < 0)
  305. goto out;
  306. dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
  307. return 0;
  308. out:
  309. unregister_netdev(dev);
  310. return err;
  311. }
  312. static int __devexit ieee802154fake_remove(struct platform_device *pdev)
  313. {
  314. struct net_device *dev = platform_get_drvdata(pdev);
  315. unregister_netdev(dev);
  316. free_netdev(dev);
  317. return 0;
  318. }
  319. static struct platform_device *ieee802154fake_dev;
  320. static struct platform_driver ieee802154fake_driver = {
  321. .probe = ieee802154fake_probe,
  322. .remove = __devexit_p(ieee802154fake_remove),
  323. .driver = {
  324. .name = "ieee802154hardmac",
  325. .owner = THIS_MODULE,
  326. },
  327. };
  328. static __init int fake_init(void)
  329. {
  330. ieee802154fake_dev = platform_device_register_simple(
  331. "ieee802154hardmac", -1, NULL, 0);
  332. return platform_driver_register(&ieee802154fake_driver);
  333. }
  334. static __exit void fake_exit(void)
  335. {
  336. platform_driver_unregister(&ieee802154fake_driver);
  337. platform_device_unregister(ieee802154fake_dev);
  338. }
  339. module_init(fake_init);
  340. module_exit(fake_exit);
  341. MODULE_LICENSE("GPL");