fakehard.c 12 KB

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