blob: d8e9e6f0e096c8bdfae9044b1c4ebf068b7e26f9 [file] [log] [blame]
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +00001/*
2 * IEEE802.15.4-2003 specification
3 *
4 * Copyright (C) 2007-2012 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 *
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000015 */
16#ifndef NET_MAC802154_H
17#define NET_MAC802154_H
18
19#include <net/af_ieee802154.h>
Alexander Aring239a84a2014-11-05 20:51:23 +010020#include <linux/ieee802154.h>
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010021#include <linux/skbuff.h>
Alexander Aringba5bf172015-02-11 14:39:16 +010022#include <linux/unaligned/memmove.h>
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000023
Alexander Aring7fe9a382014-12-10 15:33:12 +010024#include <net/cfg802154.h>
25
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000026/* General MAC frame format:
27 * 2 bytes: Frame Control
28 * 1 byte: Sequence Number
29 * 20 bytes: Addressing fields
30 * 14 bytes: Auxiliary Security Header
31 */
32#define MAC802154_FRAME_HARD_HEADER_LEN (2 + 1 + 20 + 14)
33
Alexander Aring6b70a432015-06-06 17:30:47 +020034/**
35 * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
36 *
37 * The following flags are used to indicate changed address settings from
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000038 * the stack to the hardware.
Alexander Aring6b70a432015-06-06 17:30:47 +020039 *
40 * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
41 * change.
42 *
43 * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
44 * will be change.
45 *
46 * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
47 *
48 * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
49 * do frame address filtering as a pan coordinator.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000050 */
Alexander Aring6b70a432015-06-06 17:30:47 +020051enum ieee802154_hw_addr_filt_flags {
52 IEEE802154_AFILT_SADDR_CHANGED = BIT(1),
53 IEEE802154_AFILT_IEEEADDR_CHANGED = BIT(2),
54 IEEE802154_AFILT_PANID_CHANGED = BIT(3),
55 IEEE802154_AFILT_PANC_CHANGED = BIT(4),
56};
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000057
58struct ieee802154_hw_addr_filt {
59 __le16 pan_id; /* Each independent PAN selects a unique
60 * identifier. This PAN id allows communication
61 * between devices within a network using short
62 * addresses and enables transmissions between
63 * devices across independent networks.
64 */
65 __le16 short_addr;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010066 __le64 ieee_addr;
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000067 u8 pan_coord;
68};
69
Alexander Aring5a504392014-10-25 17:16:34 +020070struct ieee802154_hw {
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000071 /* filled by the driver */
72 int extra_tx_headroom;
73 u32 flags;
74 struct device *parent;
Alexander Aringaf69a342015-06-06 17:30:51 +020075 void *priv;
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000076
77 /* filled by mac802154 core */
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000078 struct wpan_phy *phy;
79};
80
Alexander Aringbcbfd202015-06-06 17:30:49 +020081/**
82 * enum ieee802154_hw_flags - hardware flags
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000083 *
Alexander Aringbcbfd202015-06-06 17:30:49 +020084 * These flags are used to indicate hardware capabilities to
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000085 * the stack. Generally, flags here should have their meaning
86 * done in a way that the simplest hardware doesn't need setting
87 * any particular flags. There are some exceptions to this rule,
88 * however, so you are advised to review these flags carefully.
Alexander Aringbcbfd202015-06-06 17:30:49 +020089 *
90 * @IEEE802154_HW_TX_OMIT_CKSUM: Indicates that xmitter will add FCS on it's
91 * own.
92 *
93 * @IEEE802154_HW_LBT: Indicates that transceiver will support listen before
94 * transmit.
95 *
96 * @IEEE802154_HW_CSMA_PARAMS: Indicates that transceiver will support csma
97 * parameters (max_be, min_be, backoff exponents).
98 *
99 * @IEEE802154_HW_FRAME_RETRIES: Indicates that transceiver will support ARET
100 * frame retries setting.
101 *
102 * @IEEE802154_HW_AFILT: Indicates that transceiver will support hardware
103 * address filter setting.
104 *
105 * @IEEE802154_HW_PROMISCUOUS: Indicates that transceiver will support
106 * promiscuous mode setting.
107 *
108 * @IEEE802154_HW_RX_OMIT_CKSUM: Indicates that receiver omits FCS.
109 *
110 * @IEEE802154_HW_RX_DROP_BAD_CKSUM: Indicates that receiver will not filter
111 * frames with bad checksum.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000112 */
Alexander Aringbcbfd202015-06-06 17:30:49 +0200113enum ieee802154_hw_flags {
114 IEEE802154_HW_TX_OMIT_CKSUM = BIT(1),
115 IEEE802154_HW_LBT = BIT(2),
116 IEEE802154_HW_CSMA_PARAMS = BIT(3),
117 IEEE802154_HW_FRAME_RETRIES = BIT(4),
118 IEEE802154_HW_AFILT = BIT(5),
119 IEEE802154_HW_PROMISCUOUS = BIT(6),
120 IEEE802154_HW_RX_OMIT_CKSUM = BIT(7),
121 IEEE802154_HW_RX_DROP_BAD_CKSUM = BIT(8),
122};
Alexander Aring90386a72014-10-29 21:34:34 +0100123
124/* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
125#define IEEE802154_HW_OMIT_CKSUM (IEEE802154_HW_TX_OMIT_CKSUM | \
126 IEEE802154_HW_RX_OMIT_CKSUM)
Alexander Aring640985e2014-07-03 00:20:43 +0200127
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000128/* struct ieee802154_ops - callbacks from mac802154 to the driver
129 *
130 * This structure contains various callbacks that the driver may
131 * handle or, in some cases, must handle, for example to transmit
132 * a frame.
133 *
134 * start: Handler that 802.15.4 module calls for device initialization.
135 * This function is called before the first interface is attached.
136 *
137 * stop: Handler that 802.15.4 module calls for device cleanup.
138 * This function is called after the last interface is removed.
139 *
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100140 * xmit_sync:
141 * Handler that 802.15.4 module calls for each transmitted frame.
142 * skb cntains the buffer starting from the IEEE 802.15.4 header.
143 * The low-level driver should send the frame based on available
144 * configuration. This is called by a workqueue and useful for
145 * synchronous 802.15.4 drivers.
146 * This function should return zero or negative errno.
147 *
Alexander Aring1e7283a2014-10-26 09:37:14 +0100148 * WARNING:
149 * This will be deprecated soon. We don't accept synced xmit callbacks
150 * drivers anymore.
151 *
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100152 * xmit_async:
153 * Handler that 802.15.4 module calls for each transmitted frame.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000154 * skb cntains the buffer starting from the IEEE 802.15.4 header.
155 * The low-level driver should send the frame based on available
156 * configuration.
Alexander Aringdc67c6b2014-10-26 09:37:04 +0100157 * This function should return zero or negative errno.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000158 *
159 * ed: Handler that 802.15.4 module calls for Energy Detection.
160 * This function should place the value for detected energy
161 * (usually device-dependant) in the level pointer and return
162 * either zero or negative errno. Called with pib_lock held.
163 *
164 * set_channel:
165 * Set radio for listening on specific channel.
166 * Set the device for listening on specified channel.
167 * Returns either zero, or negative errno. Called with pib_lock held.
168 *
169 * set_hw_addr_filt:
170 * Set radio for listening on specific address.
171 * Set the device for listening on specified address.
172 * Returns either zero, or negative errno.
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100173 *
174 * set_txpower:
Alexander Aringe2eb1732015-05-17 21:44:40 +0200175 * Set radio transmit power in mBm. Called with pib_lock held.
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100176 * Returns either zero, or negative errno.
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100177 *
178 * set_lbt
179 * Enables or disables listen before talk on the device. Called with
180 * pib_lock held.
181 * Returns either zero, or negative errno.
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100182 *
183 * set_cca_mode
184 * Sets the CCA mode used by the device. Called with pib_lock held.
185 * Returns either zero, or negative errno.
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100186 *
187 * set_cca_ed_level
Alexander Aring32b23552015-05-17 21:44:41 +0200188 * Sets the CCA energy detection threshold in mBm. Called with pib_lock
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100189 * held.
190 * Returns either zero, or negative errno.
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100191 *
192 * set_csma_params
193 * Sets the CSMA parameter set for the PHY. Called with pib_lock held.
194 * Returns either zero, or negative errno.
195 *
196 * set_frame_retries
197 * Sets the retransmission attempt limit. Called with pib_lock held.
198 * Returns either zero, or negative errno.
Alexander Aring94b79222014-10-29 21:34:32 +0100199 *
200 * set_promiscuous_mode
201 * Enables or disable promiscuous mode.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000202 */
203struct ieee802154_ops {
204 struct module *owner;
Alexander Aring5a504392014-10-25 17:16:34 +0200205 int (*start)(struct ieee802154_hw *hw);
206 void (*stop)(struct ieee802154_hw *hw);
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100207 int (*xmit_sync)(struct ieee802154_hw *hw,
208 struct sk_buff *skb);
209 int (*xmit_async)(struct ieee802154_hw *hw,
210 struct sk_buff *skb);
Alexander Aring5a504392014-10-25 17:16:34 +0200211 int (*ed)(struct ieee802154_hw *hw, u8 *level);
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100212 int (*set_channel)(struct ieee802154_hw *hw, u8 page,
213 u8 channel);
Alexander Aring5a504392014-10-25 17:16:34 +0200214 int (*set_hw_addr_filt)(struct ieee802154_hw *hw,
215 struct ieee802154_hw_addr_filt *filt,
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000216 unsigned long changed);
Alexander Aringe2eb1732015-05-17 21:44:40 +0200217 int (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
Alexander Aring5a504392014-10-25 17:16:34 +0200218 int (*set_lbt)(struct ieee802154_hw *hw, bool on);
Alexander Aring7fe9a382014-12-10 15:33:12 +0100219 int (*set_cca_mode)(struct ieee802154_hw *hw,
220 const struct wpan_phy_cca *cca);
Alexander Aring32b23552015-05-17 21:44:41 +0200221 int (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
Alexander Aring5a504392014-10-25 17:16:34 +0200222 int (*set_csma_params)(struct ieee802154_hw *hw,
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100223 u8 min_be, u8 max_be, u8 retries);
Alexander Aring5a504392014-10-25 17:16:34 +0200224 int (*set_frame_retries)(struct ieee802154_hw *hw,
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100225 s8 retries);
Alexander Aring94b79222014-10-29 21:34:32 +0100226 int (*set_promiscuous_mode)(struct ieee802154_hw *hw,
227 const bool on);
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000228};
229
Alexander Aringab24f502014-11-02 04:18:40 +0100230/**
Alexander Aring705cbbb2014-11-05 20:51:24 +0100231 * ieee802154_be64_to_le64 - copies and convert be64 to le64
232 * @le64_dst: le64 destination pointer
233 * @be64_src: be64 source pointer
Alexander Aringab24f502014-11-02 04:18:40 +0100234 */
Alexander Aring705cbbb2014-11-05 20:51:24 +0100235static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
Alexander Aringab24f502014-11-02 04:18:40 +0100236{
Alexander Aringba5bf172015-02-11 14:39:16 +0100237 __put_unaligned_memmove64(swab64p(be64_src), le64_dst);
Alexander Aringab24f502014-11-02 04:18:40 +0100238}
239
Alexander Aring239a84a2014-11-05 20:51:23 +0100240/**
241 * ieee802154_le64_to_be64 - copies and convert le64 to be64
242 * @be64_dst: be64 destination pointer
243 * @le64_src: le64 source pointer
244 */
245static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
246{
Alexander Aringb9767962015-02-11 14:39:17 +0100247 __put_unaligned_memmove64(swab64p(le64_src), be64_dst);
Alexander Aring239a84a2014-11-05 20:51:23 +0100248}
249
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200250/**
251 * ieee802154_alloc_hw - Allocate a new hardware device
252 *
253 * This must be called once for each hardware device. The returned pointer
254 * must be used to refer to this device when calling other functions.
255 * mac802154 allocates a private data area for the driver pointed to by
256 * @priv in &struct ieee802154_hw, the size of this area is given as
257 * @priv_data_len.
258 *
259 * @priv_data_len: length of private data
260 * @ops: callbacks for this device
261 *
262 * Return: A pointer to the new hardware device, or %NULL on error.
263 */
Alexander Aring5a504392014-10-25 17:16:34 +0200264struct ieee802154_hw *
Alexander Aring16301862014-10-28 18:21:18 +0100265ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200266
267/**
268 * ieee802154_free_hw - free hardware descriptor
269 *
270 * This function frees everything that was allocated, including the
271 * private data for the driver. You must call ieee802154_unregister_hw()
272 * before calling this function.
273 *
274 * @hw: the hardware to free
275 */
Alexander Aring5a504392014-10-25 17:16:34 +0200276void ieee802154_free_hw(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200277
278/**
279 * ieee802154_register_hw - Register hardware device
280 *
281 * You must call this function before any other functions in
282 * mac802154. Note that before a hardware can be registered, you
283 * need to fill the contained wpan_phy's information.
284 *
285 * @hw: the device to register as returned by ieee802154_alloc_hw()
286 *
287 * Return: 0 on success. An error code otherwise.
288 */
Alexander Aring5a504392014-10-25 17:16:34 +0200289int ieee802154_register_hw(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200290
291/**
292 * ieee802154_unregister_hw - Unregister a hardware device
293 *
294 * This function instructs mac802154 to free allocated resources
295 * and unregister netdevices from the networking subsystem.
296 *
297 * @hw: the hardware to unregister
298 */
Alexander Aring5a504392014-10-25 17:16:34 +0200299void ieee802154_unregister_hw(struct ieee802154_hw *hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000300
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200301/**
302 * ieee802154_rx - receive frame
303 *
304 * Use this function to hand received frames to mac802154. The receive
305 * buffer in @skb must start with an IEEE 802.15.4 header. In case of a
306 * paged @skb is used, the driver is recommended to put the ieee802154
307 * header of the frame on the linear part of the @skb to avoid memory
308 * allocation and/or memcpy by the stack.
309 *
310 * This function may not be called in IRQ context. Calls to this function
311 * for a single hardware must be synchronized against each other.
312 *
313 * @hw: the hardware this frame came in on
314 * @skb: the buffer to receive, owned by mac802154 after this call
315 */
Alexander Aringc5c47e62014-10-27 17:13:30 +0100316void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200317
318/**
319 * ieee802154_rx_irqsafe - receive frame
320 *
321 * Like ieee802154_rx() but can be called in IRQ context
322 * (internally defers to a tasklet.)
323 *
324 * @hw: the hardware this frame came in on
325 * @skb: the buffer to receive, owned by mac802154 after this call
326 * @lqi: link quality indicator
327 */
Alexander Aring5a504392014-10-25 17:16:34 +0200328void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000329 u8 lqi);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200330/**
331 * ieee802154_wake_queue - wake ieee802154 queue
332 * @hw: pointer as obtained from ieee802154_alloc_hw().
333 *
334 * Drivers should use this function instead of netif_wake_queue.
335 */
Alexander Aringc2085102014-10-26 09:37:05 +0100336void ieee802154_wake_queue(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200337
338/**
339 * ieee802154_stop_queue - stop ieee802154 queue
340 * @hw: pointer as obtained from ieee802154_alloc_hw().
341 *
342 * Drivers should use this function instead of netif_stop_queue.
343 */
Alexander Aringc2085102014-10-26 09:37:05 +0100344void ieee802154_stop_queue(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200345
346/**
347 * ieee802154_xmit_complete - frame transmission complete
348 *
349 * @hw: pointer as obtained from ieee802154_alloc_hw().
350 * @skb: buffer for transmission
351 * @ifs_handling: indicate interframe space handling
352 */
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100353void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
354 bool ifs_handling);
Alexander Aringc2085102014-10-26 09:37:05 +0100355
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000356#endif /* NET_MAC802154_H */