driver-ops.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __MAC802154_DRIVER_OPS
  3. #define __MAC802154_DRIVER_OPS
  4. #include <linux/types.h>
  5. #include <linux/rtnetlink.h>
  6. #include <net/mac802154.h>
  7. #include "ieee802154_i.h"
  8. #include "trace.h"
  9. static inline int
  10. drv_xmit_async(struct ieee802154_local *local, struct sk_buff *skb)
  11. {
  12. return local->ops->xmit_async(&local->hw, skb);
  13. }
  14. static inline int
  15. drv_xmit_sync(struct ieee802154_local *local, struct sk_buff *skb)
  16. {
  17. might_sleep();
  18. return local->ops->xmit_sync(&local->hw, skb);
  19. }
  20. static inline int drv_set_pan_id(struct ieee802154_local *local, __le16 pan_id)
  21. {
  22. struct ieee802154_hw_addr_filt filt;
  23. int ret;
  24. might_sleep();
  25. if (!local->ops->set_hw_addr_filt) {
  26. WARN_ON(1);
  27. return -EOPNOTSUPP;
  28. }
  29. filt.pan_id = pan_id;
  30. trace_802154_drv_set_pan_id(local, pan_id);
  31. ret = local->ops->set_hw_addr_filt(&local->hw, &filt,
  32. IEEE802154_AFILT_PANID_CHANGED);
  33. trace_802154_drv_return_int(local, ret);
  34. return ret;
  35. }
  36. static inline int
  37. drv_set_extended_addr(struct ieee802154_local *local, __le64 extended_addr)
  38. {
  39. struct ieee802154_hw_addr_filt filt;
  40. int ret;
  41. might_sleep();
  42. if (!local->ops->set_hw_addr_filt) {
  43. WARN_ON(1);
  44. return -EOPNOTSUPP;
  45. }
  46. filt.ieee_addr = extended_addr;
  47. trace_802154_drv_set_extended_addr(local, extended_addr);
  48. ret = local->ops->set_hw_addr_filt(&local->hw, &filt,
  49. IEEE802154_AFILT_IEEEADDR_CHANGED);
  50. trace_802154_drv_return_int(local, ret);
  51. return ret;
  52. }
  53. static inline int
  54. drv_set_short_addr(struct ieee802154_local *local, __le16 short_addr)
  55. {
  56. struct ieee802154_hw_addr_filt filt;
  57. int ret;
  58. might_sleep();
  59. if (!local->ops->set_hw_addr_filt) {
  60. WARN_ON(1);
  61. return -EOPNOTSUPP;
  62. }
  63. filt.short_addr = short_addr;
  64. trace_802154_drv_set_short_addr(local, short_addr);
  65. ret = local->ops->set_hw_addr_filt(&local->hw, &filt,
  66. IEEE802154_AFILT_SADDR_CHANGED);
  67. trace_802154_drv_return_int(local, ret);
  68. return ret;
  69. }
  70. static inline int
  71. drv_set_pan_coord(struct ieee802154_local *local, bool is_coord)
  72. {
  73. struct ieee802154_hw_addr_filt filt;
  74. int ret;
  75. might_sleep();
  76. if (!local->ops->set_hw_addr_filt) {
  77. WARN_ON(1);
  78. return -EOPNOTSUPP;
  79. }
  80. filt.pan_coord = is_coord;
  81. trace_802154_drv_set_pan_coord(local, is_coord);
  82. ret = local->ops->set_hw_addr_filt(&local->hw, &filt,
  83. IEEE802154_AFILT_PANC_CHANGED);
  84. trace_802154_drv_return_int(local, ret);
  85. return ret;
  86. }
  87. static inline int
  88. drv_set_promiscuous_mode(struct ieee802154_local *local, bool on)
  89. {
  90. int ret;
  91. might_sleep();
  92. if (!local->ops->set_promiscuous_mode) {
  93. WARN_ON(1);
  94. return -EOPNOTSUPP;
  95. }
  96. trace_802154_drv_set_promiscuous_mode(local, on);
  97. ret = local->ops->set_promiscuous_mode(&local->hw, on);
  98. trace_802154_drv_return_int(local, ret);
  99. return ret;
  100. }
  101. static inline int drv_start(struct ieee802154_local *local,
  102. enum ieee802154_filtering_level level,
  103. const struct ieee802154_hw_addr_filt *addr_filt)
  104. {
  105. int ret;
  106. might_sleep();
  107. /* setup receive mode parameters e.g. address mode */
  108. if (local->hw.flags & IEEE802154_HW_AFILT) {
  109. ret = drv_set_pan_id(local, addr_filt->pan_id);
  110. if (ret < 0)
  111. return ret;
  112. ret = drv_set_short_addr(local, addr_filt->short_addr);
  113. if (ret < 0)
  114. return ret;
  115. ret = drv_set_extended_addr(local, addr_filt->ieee_addr);
  116. if (ret < 0)
  117. return ret;
  118. }
  119. switch (level) {
  120. case IEEE802154_FILTERING_NONE:
  121. fallthrough;
  122. case IEEE802154_FILTERING_1_FCS:
  123. fallthrough;
  124. case IEEE802154_FILTERING_2_PROMISCUOUS:
  125. /* TODO: Requires a different receive mode setup e.g.
  126. * at86rf233 hardware.
  127. */
  128. fallthrough;
  129. case IEEE802154_FILTERING_3_SCAN:
  130. if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
  131. ret = drv_set_promiscuous_mode(local, true);
  132. if (ret < 0)
  133. return ret;
  134. } else {
  135. return -EOPNOTSUPP;
  136. }
  137. /* In practice other filtering levels can be requested, but as
  138. * for now most hardware/drivers only support
  139. * IEEE802154_FILTERING_NONE, we fallback to this actual
  140. * filtering level in hardware and make our own additional
  141. * filtering in mac802154 receive path.
  142. *
  143. * TODO: Move this logic to the device drivers as hardware may
  144. * support more higher level filters. Hardware may also require
  145. * a different order how register are set, which could currently
  146. * be buggy, so all received parameters need to be moved to the
  147. * start() callback and let the driver go into the mode before
  148. * it will turn on receive handling.
  149. */
  150. local->phy->filtering = IEEE802154_FILTERING_NONE;
  151. break;
  152. case IEEE802154_FILTERING_4_FRAME_FIELDS:
  153. /* Do not error out if IEEE802154_HW_PROMISCUOUS because we
  154. * expect the hardware to operate at the level
  155. * IEEE802154_FILTERING_4_FRAME_FIELDS anyway.
  156. */
  157. if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
  158. ret = drv_set_promiscuous_mode(local, false);
  159. if (ret < 0)
  160. return ret;
  161. }
  162. local->phy->filtering = IEEE802154_FILTERING_4_FRAME_FIELDS;
  163. break;
  164. default:
  165. WARN_ON(1);
  166. return -EINVAL;
  167. }
  168. trace_802154_drv_start(local);
  169. local->started = true;
  170. smp_mb();
  171. ret = local->ops->start(&local->hw);
  172. trace_802154_drv_return_int(local, ret);
  173. return ret;
  174. }
  175. static inline void drv_stop(struct ieee802154_local *local)
  176. {
  177. might_sleep();
  178. trace_802154_drv_stop(local);
  179. local->ops->stop(&local->hw);
  180. trace_802154_drv_return_void(local);
  181. /* sync away all work on the tasklet before clearing started */
  182. tasklet_disable(&local->tasklet);
  183. tasklet_enable(&local->tasklet);
  184. barrier();
  185. local->started = false;
  186. }
  187. static inline int
  188. drv_set_channel(struct ieee802154_local *local, u8 page, u8 channel)
  189. {
  190. int ret;
  191. might_sleep();
  192. trace_802154_drv_set_channel(local, page, channel);
  193. ret = local->ops->set_channel(&local->hw, page, channel);
  194. trace_802154_drv_return_int(local, ret);
  195. return ret;
  196. }
  197. static inline int drv_set_tx_power(struct ieee802154_local *local, s32 mbm)
  198. {
  199. int ret;
  200. might_sleep();
  201. if (!local->ops->set_txpower) {
  202. WARN_ON(1);
  203. return -EOPNOTSUPP;
  204. }
  205. trace_802154_drv_set_tx_power(local, mbm);
  206. ret = local->ops->set_txpower(&local->hw, mbm);
  207. trace_802154_drv_return_int(local, ret);
  208. return ret;
  209. }
  210. static inline int drv_set_cca_mode(struct ieee802154_local *local,
  211. const struct wpan_phy_cca *cca)
  212. {
  213. int ret;
  214. might_sleep();
  215. if (!local->ops->set_cca_mode) {
  216. WARN_ON(1);
  217. return -EOPNOTSUPP;
  218. }
  219. trace_802154_drv_set_cca_mode(local, cca);
  220. ret = local->ops->set_cca_mode(&local->hw, cca);
  221. trace_802154_drv_return_int(local, ret);
  222. return ret;
  223. }
  224. static inline int drv_set_lbt_mode(struct ieee802154_local *local, bool mode)
  225. {
  226. int ret;
  227. might_sleep();
  228. if (!local->ops->set_lbt) {
  229. WARN_ON(1);
  230. return -EOPNOTSUPP;
  231. }
  232. trace_802154_drv_set_lbt_mode(local, mode);
  233. ret = local->ops->set_lbt(&local->hw, mode);
  234. trace_802154_drv_return_int(local, ret);
  235. return ret;
  236. }
  237. static inline int
  238. drv_set_cca_ed_level(struct ieee802154_local *local, s32 mbm)
  239. {
  240. int ret;
  241. might_sleep();
  242. if (!local->ops->set_cca_ed_level) {
  243. WARN_ON(1);
  244. return -EOPNOTSUPP;
  245. }
  246. trace_802154_drv_set_cca_ed_level(local, mbm);
  247. ret = local->ops->set_cca_ed_level(&local->hw, mbm);
  248. trace_802154_drv_return_int(local, ret);
  249. return ret;
  250. }
  251. static inline int
  252. drv_set_csma_params(struct ieee802154_local *local, u8 min_be, u8 max_be,
  253. u8 max_csma_backoffs)
  254. {
  255. int ret;
  256. might_sleep();
  257. if (!local->ops->set_csma_params) {
  258. WARN_ON(1);
  259. return -EOPNOTSUPP;
  260. }
  261. trace_802154_drv_set_csma_params(local, min_be, max_be,
  262. max_csma_backoffs);
  263. ret = local->ops->set_csma_params(&local->hw, min_be, max_be,
  264. max_csma_backoffs);
  265. trace_802154_drv_return_int(local, ret);
  266. return ret;
  267. }
  268. static inline int
  269. drv_set_max_frame_retries(struct ieee802154_local *local, s8 max_frame_retries)
  270. {
  271. int ret;
  272. might_sleep();
  273. if (!local->ops->set_frame_retries) {
  274. WARN_ON(1);
  275. return -EOPNOTSUPP;
  276. }
  277. trace_802154_drv_set_max_frame_retries(local, max_frame_retries);
  278. ret = local->ops->set_frame_retries(&local->hw, max_frame_retries);
  279. trace_802154_drv_return_int(local, ret);
  280. return ret;
  281. }
  282. #endif /* __MAC802154_DRIVER_OPS */