ieee802154_dev.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, mac802154_monitor_setup);
  136. break;
  137. case IEEE802154_DEV_WPAN:
  138. dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
  139. name, mac802154_wpan_setup);
  140. break;
  141. default:
  142. dev = NULL;
  143. err = -EINVAL;
  144. break;
  145. }
  146. if (!dev)
  147. goto err;
  148. err = mac802154_netdev_register(phy, dev);
  149. if (err)
  150. goto err_free;
  151. dev_hold(dev); /* we return an incremented device refcount */
  152. return dev;
  153. err_free:
  154. free_netdev(dev);
  155. err:
  156. return ERR_PTR(err);
  157. }
  158. static int mac802154_set_txpower(struct wpan_phy *phy, int db)
  159. {
  160. struct mac802154_priv *priv = wpan_phy_priv(phy);
  161. return priv->ops->set_txpower(&priv->hw, db);
  162. }
  163. static int mac802154_set_lbt(struct wpan_phy *phy, bool on)
  164. {
  165. struct mac802154_priv *priv = wpan_phy_priv(phy);
  166. return priv->ops->set_lbt(&priv->hw, on);
  167. }
  168. static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode)
  169. {
  170. struct mac802154_priv *priv = wpan_phy_priv(phy);
  171. return priv->ops->set_cca_mode(&priv->hw, mode);
  172. }
  173. static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level)
  174. {
  175. struct mac802154_priv *priv = wpan_phy_priv(phy);
  176. return priv->ops->set_cca_ed_level(&priv->hw, level);
  177. }
  178. static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be,
  179. u8 max_be, u8 retries)
  180. {
  181. struct mac802154_priv *priv = wpan_phy_priv(phy);
  182. return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries);
  183. }
  184. static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries)
  185. {
  186. struct mac802154_priv *priv = wpan_phy_priv(phy);
  187. return priv->ops->set_frame_retries(&priv->hw, retries);
  188. }
  189. struct ieee802154_dev *
  190. ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
  191. {
  192. struct wpan_phy *phy;
  193. struct mac802154_priv *priv;
  194. size_t priv_size;
  195. if (!ops || !ops->xmit || !ops->ed || !ops->start ||
  196. !ops->stop || !ops->set_channel) {
  197. pr_err("undefined IEEE802.15.4 device operations\n");
  198. return NULL;
  199. }
  200. /* Ensure 32-byte alignment of our private data and hw private data.
  201. * We use the wpan_phy priv data for both our mac802154_priv and for
  202. * the driver's private data
  203. *
  204. * in memory it'll be like this:
  205. *
  206. * +-----------------------+
  207. * | struct wpan_phy |
  208. * +-----------------------+
  209. * | struct mac802154_priv |
  210. * +-----------------------+
  211. * | driver's private data |
  212. * +-----------------------+
  213. *
  214. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  215. * so lets allign them here.
  216. */
  217. priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
  218. phy = wpan_phy_alloc(priv_size);
  219. if (!phy) {
  220. pr_err("failure to allocate master IEEE802.15.4 device\n");
  221. return NULL;
  222. }
  223. priv = wpan_phy_priv(phy);
  224. priv->hw.phy = priv->phy = phy;
  225. priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
  226. priv->ops = ops;
  227. INIT_LIST_HEAD(&priv->slaves);
  228. mutex_init(&priv->slaves_mtx);
  229. return &priv->hw;
  230. }
  231. EXPORT_SYMBOL(ieee802154_alloc_device);
  232. void ieee802154_free_device(struct ieee802154_dev *hw)
  233. {
  234. struct mac802154_priv *priv = mac802154_to_priv(hw);
  235. BUG_ON(!list_empty(&priv->slaves));
  236. mutex_destroy(&priv->slaves_mtx);
  237. wpan_phy_free(priv->phy);
  238. }
  239. EXPORT_SYMBOL(ieee802154_free_device);
  240. int ieee802154_register_device(struct ieee802154_dev *dev)
  241. {
  242. struct mac802154_priv *priv = mac802154_to_priv(dev);
  243. int rc = -ENOMEM;
  244. priv->dev_workqueue =
  245. create_singlethread_workqueue(wpan_phy_name(priv->phy));
  246. if (!priv->dev_workqueue)
  247. goto out;
  248. wpan_phy_set_dev(priv->phy, priv->hw.parent);
  249. priv->phy->add_iface = mac802154_add_iface;
  250. priv->phy->del_iface = mac802154_del_iface;
  251. if (priv->ops->set_txpower)
  252. priv->phy->set_txpower = mac802154_set_txpower;
  253. if (priv->ops->set_lbt)
  254. priv->phy->set_lbt = mac802154_set_lbt;
  255. if (priv->ops->set_cca_mode)
  256. priv->phy->set_cca_mode = mac802154_set_cca_mode;
  257. if (priv->ops->set_cca_ed_level)
  258. priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level;
  259. if (priv->ops->set_csma_params)
  260. priv->phy->set_csma_params = mac802154_set_csma_params;
  261. if (priv->ops->set_frame_retries)
  262. priv->phy->set_frame_retries = mac802154_set_frame_retries;
  263. rc = wpan_phy_register(priv->phy);
  264. if (rc < 0)
  265. goto out_wq;
  266. rtnl_lock();
  267. mutex_lock(&priv->slaves_mtx);
  268. priv->running = MAC802154_DEVICE_RUN;
  269. mutex_unlock(&priv->slaves_mtx);
  270. rtnl_unlock();
  271. return 0;
  272. out_wq:
  273. destroy_workqueue(priv->dev_workqueue);
  274. out:
  275. return rc;
  276. }
  277. EXPORT_SYMBOL(ieee802154_register_device);
  278. void ieee802154_unregister_device(struct ieee802154_dev *dev)
  279. {
  280. struct mac802154_priv *priv = mac802154_to_priv(dev);
  281. struct mac802154_sub_if_data *sdata, *next;
  282. flush_workqueue(priv->dev_workqueue);
  283. destroy_workqueue(priv->dev_workqueue);
  284. rtnl_lock();
  285. mutex_lock(&priv->slaves_mtx);
  286. priv->running = MAC802154_DEVICE_STOPPED;
  287. mutex_unlock(&priv->slaves_mtx);
  288. list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
  289. mutex_lock(&sdata->hw->slaves_mtx);
  290. list_del(&sdata->list);
  291. mutex_unlock(&sdata->hw->slaves_mtx);
  292. unregister_netdevice(sdata->dev);
  293. }
  294. rtnl_unlock();
  295. wpan_phy_unregister(priv->phy);
  296. }
  297. EXPORT_SYMBOL(ieee802154_unregister_device);
  298. MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
  299. MODULE_LICENSE("GPL v2");