ieee802154_dev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * Written by:
  5. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  6. *
  7. * Based on the code from 'linux-zigbee.sourceforge.net' project.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/netdevice.h>
  25. #include <net/netlink.h>
  26. #include <linux/nl802154.h>
  27. #include <net/mac802154.h>
  28. #include <net/ieee802154_netdev.h>
  29. #include <net/route.h>
  30. #include <net/wpan-phy.h>
  31. #include "mac802154.h"
  32. int mac802154_slave_open(struct net_device *dev)
  33. {
  34. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  35. struct mac802154_sub_if_data *subif;
  36. struct mac802154_priv *ipriv = priv->hw;
  37. int res = 0;
  38. ASSERT_RTNL();
  39. if (priv->type == IEEE802154_DEV_WPAN) {
  40. mutex_lock(&priv->hw->slaves_mtx);
  41. list_for_each_entry(subif, &priv->hw->slaves, list) {
  42. if (subif != priv && subif->type == priv->type &&
  43. subif->running) {
  44. mutex_unlock(&priv->hw->slaves_mtx);
  45. return -EBUSY;
  46. }
  47. }
  48. mutex_unlock(&priv->hw->slaves_mtx);
  49. }
  50. mutex_lock(&priv->hw->slaves_mtx);
  51. priv->running = true;
  52. mutex_unlock(&priv->hw->slaves_mtx);
  53. if (ipriv->open_count++ == 0) {
  54. res = ipriv->ops->start(&ipriv->hw);
  55. WARN_ON(res);
  56. if (res)
  57. goto err;
  58. }
  59. if (ipriv->ops->ieee_addr) {
  60. __le64 addr = ieee802154_devaddr_from_raw(dev->dev_addr);
  61. res = ipriv->ops->ieee_addr(&ipriv->hw, addr);
  62. WARN_ON(res);
  63. if (res)
  64. goto err;
  65. mac802154_dev_set_ieee_addr(dev);
  66. }
  67. netif_start_queue(dev);
  68. return 0;
  69. err:
  70. priv->hw->open_count--;
  71. return res;
  72. }
  73. int mac802154_slave_close(struct net_device *dev)
  74. {
  75. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  76. struct mac802154_priv *ipriv = priv->hw;
  77. ASSERT_RTNL();
  78. netif_stop_queue(dev);
  79. mutex_lock(&priv->hw->slaves_mtx);
  80. priv->running = false;
  81. mutex_unlock(&priv->hw->slaves_mtx);
  82. if (!--ipriv->open_count)
  83. ipriv->ops->stop(&ipriv->hw);
  84. return 0;
  85. }
  86. static int
  87. mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
  88. {
  89. struct mac802154_sub_if_data *priv;
  90. struct mac802154_priv *ipriv;
  91. int err;
  92. ipriv = wpan_phy_priv(phy);
  93. priv = netdev_priv(dev);
  94. priv->dev = dev;
  95. priv->hw = ipriv;
  96. dev->needed_headroom = ipriv->hw.extra_tx_headroom;
  97. SET_NETDEV_DEV(dev, &ipriv->phy->dev);
  98. mutex_lock(&ipriv->slaves_mtx);
  99. if (!ipriv->running) {
  100. mutex_unlock(&ipriv->slaves_mtx);
  101. return -ENODEV;
  102. }
  103. mutex_unlock(&ipriv->slaves_mtx);
  104. err = register_netdev(dev);
  105. if (err < 0)
  106. return err;
  107. rtnl_lock();
  108. mutex_lock(&ipriv->slaves_mtx);
  109. list_add_tail_rcu(&priv->list, &ipriv->slaves);
  110. mutex_unlock(&ipriv->slaves_mtx);
  111. rtnl_unlock();
  112. return 0;
  113. }
  114. static void
  115. mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
  116. {
  117. struct mac802154_sub_if_data *sdata;
  118. ASSERT_RTNL();
  119. sdata = netdev_priv(dev);
  120. BUG_ON(sdata->hw->phy != phy);
  121. mutex_lock(&sdata->hw->slaves_mtx);
  122. list_del_rcu(&sdata->list);
  123. mutex_unlock(&sdata->hw->slaves_mtx);
  124. synchronize_rcu();
  125. unregister_netdevice(sdata->dev);
  126. }
  127. static struct net_device *
  128. mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
  129. {
  130. struct net_device *dev;
  131. int err = -ENOMEM;
  132. switch (type) {
  133. case IEEE802154_DEV_MONITOR:
  134. dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
  135. name, NET_NAME_UNKNOWN,
  136. mac802154_monitor_setup);
  137. break;
  138. case IEEE802154_DEV_WPAN:
  139. dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
  140. name, NET_NAME_UNKNOWN,
  141. mac802154_wpan_setup);
  142. break;
  143. default:
  144. dev = NULL;
  145. err = -EINVAL;
  146. break;
  147. }
  148. if (!dev)
  149. goto err;
  150. err = mac802154_netdev_register(phy, dev);
  151. if (err)
  152. goto err_free;
  153. dev_hold(dev); /* we return an incremented device refcount */
  154. return dev;
  155. err_free:
  156. free_netdev(dev);
  157. err:
  158. return ERR_PTR(err);
  159. }
  160. static int mac802154_set_txpower(struct wpan_phy *phy, int db)
  161. {
  162. struct mac802154_priv *priv = wpan_phy_priv(phy);
  163. return priv->ops->set_txpower(&priv->hw, db);
  164. }
  165. static int mac802154_set_lbt(struct wpan_phy *phy, bool on)
  166. {
  167. struct mac802154_priv *priv = wpan_phy_priv(phy);
  168. return priv->ops->set_lbt(&priv->hw, on);
  169. }
  170. static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode)
  171. {
  172. struct mac802154_priv *priv = wpan_phy_priv(phy);
  173. return priv->ops->set_cca_mode(&priv->hw, mode);
  174. }
  175. static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level)
  176. {
  177. struct mac802154_priv *priv = wpan_phy_priv(phy);
  178. return priv->ops->set_cca_ed_level(&priv->hw, level);
  179. }
  180. static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be,
  181. u8 max_be, u8 retries)
  182. {
  183. struct mac802154_priv *priv = wpan_phy_priv(phy);
  184. return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries);
  185. }
  186. static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries)
  187. {
  188. struct mac802154_priv *priv = wpan_phy_priv(phy);
  189. return priv->ops->set_frame_retries(&priv->hw, retries);
  190. }
  191. struct ieee802154_dev *
  192. ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
  193. {
  194. struct wpan_phy *phy;
  195. struct mac802154_priv *priv;
  196. size_t priv_size;
  197. if (!ops || !ops->xmit || !ops->ed || !ops->start ||
  198. !ops->stop || !ops->set_channel) {
  199. pr_err("undefined IEEE802.15.4 device operations\n");
  200. return NULL;
  201. }
  202. /* Ensure 32-byte alignment of our private data and hw private data.
  203. * We use the wpan_phy priv data for both our mac802154_priv and for
  204. * the driver's private data
  205. *
  206. * in memory it'll be like this:
  207. *
  208. * +-----------------------+
  209. * | struct wpan_phy |
  210. * +-----------------------+
  211. * | struct mac802154_priv |
  212. * +-----------------------+
  213. * | driver's private data |
  214. * +-----------------------+
  215. *
  216. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  217. * so lets allign them here.
  218. */
  219. priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
  220. phy = wpan_phy_alloc(priv_size);
  221. if (!phy) {
  222. pr_err("failure to allocate master IEEE802.15.4 device\n");
  223. return NULL;
  224. }
  225. priv = wpan_phy_priv(phy);
  226. priv->phy = phy;
  227. priv->hw.phy = priv->phy;
  228. priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
  229. priv->ops = ops;
  230. INIT_LIST_HEAD(&priv->slaves);
  231. mutex_init(&priv->slaves_mtx);
  232. return &priv->hw;
  233. }
  234. EXPORT_SYMBOL(ieee802154_alloc_device);
  235. void ieee802154_free_device(struct ieee802154_dev *hw)
  236. {
  237. struct mac802154_priv *priv = mac802154_to_priv(hw);
  238. BUG_ON(!list_empty(&priv->slaves));
  239. mutex_destroy(&priv->slaves_mtx);
  240. wpan_phy_free(priv->phy);
  241. }
  242. EXPORT_SYMBOL(ieee802154_free_device);
  243. int ieee802154_register_device(struct ieee802154_dev *dev)
  244. {
  245. struct mac802154_priv *priv = mac802154_to_priv(dev);
  246. int rc = -ENOSYS;
  247. if (dev->flags & IEEE802154_HW_TXPOWER) {
  248. if (!priv->ops->set_txpower)
  249. goto out;
  250. priv->phy->set_txpower = mac802154_set_txpower;
  251. }
  252. if (dev->flags & IEEE802154_HW_LBT) {
  253. if (!priv->ops->set_lbt)
  254. goto out;
  255. priv->phy->set_lbt = mac802154_set_lbt;
  256. }
  257. if (dev->flags & IEEE802154_HW_CCA_MODE) {
  258. if (!priv->ops->set_cca_mode)
  259. goto out;
  260. priv->phy->set_cca_mode = mac802154_set_cca_mode;
  261. }
  262. if (dev->flags & IEEE802154_HW_CCA_ED_LEVEL) {
  263. if (!priv->ops->set_cca_ed_level)
  264. goto out;
  265. priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level;
  266. }
  267. if (dev->flags & IEEE802154_HW_CSMA_PARAMS) {
  268. if (!priv->ops->set_csma_params)
  269. goto out;
  270. priv->phy->set_csma_params = mac802154_set_csma_params;
  271. }
  272. if (dev->flags & IEEE802154_HW_FRAME_RETRIES) {
  273. if (!priv->ops->set_frame_retries)
  274. goto out;
  275. priv->phy->set_frame_retries = mac802154_set_frame_retries;
  276. }
  277. priv->dev_workqueue =
  278. create_singlethread_workqueue(wpan_phy_name(priv->phy));
  279. if (!priv->dev_workqueue) {
  280. rc = -ENOMEM;
  281. goto out;
  282. }
  283. wpan_phy_set_dev(priv->phy, priv->hw.parent);
  284. priv->phy->add_iface = mac802154_add_iface;
  285. priv->phy->del_iface = mac802154_del_iface;
  286. rc = wpan_phy_register(priv->phy);
  287. if (rc < 0)
  288. goto out_wq;
  289. rtnl_lock();
  290. mutex_lock(&priv->slaves_mtx);
  291. priv->running = MAC802154_DEVICE_RUN;
  292. mutex_unlock(&priv->slaves_mtx);
  293. rtnl_unlock();
  294. return 0;
  295. out_wq:
  296. destroy_workqueue(priv->dev_workqueue);
  297. out:
  298. return rc;
  299. }
  300. EXPORT_SYMBOL(ieee802154_register_device);
  301. void ieee802154_unregister_device(struct ieee802154_dev *dev)
  302. {
  303. struct mac802154_priv *priv = mac802154_to_priv(dev);
  304. struct mac802154_sub_if_data *sdata, *next;
  305. flush_workqueue(priv->dev_workqueue);
  306. destroy_workqueue(priv->dev_workqueue);
  307. rtnl_lock();
  308. mutex_lock(&priv->slaves_mtx);
  309. priv->running = MAC802154_DEVICE_STOPPED;
  310. mutex_unlock(&priv->slaves_mtx);
  311. list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
  312. mutex_lock(&sdata->hw->slaves_mtx);
  313. list_del(&sdata->list);
  314. mutex_unlock(&sdata->hw->slaves_mtx);
  315. unregister_netdevice(sdata->dev);
  316. }
  317. rtnl_unlock();
  318. wpan_phy_unregister(priv->phy);
  319. }
  320. EXPORT_SYMBOL(ieee802154_unregister_device);
  321. MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
  322. MODULE_LICENSE("GPL v2");