fakehard.c 12 KB

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