diff options
author | Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | 2008-09-17 11:34:08 -0400 |
---|---|---|
committer | David Vrabel <dv02@dv02pc01.europe.root.pri> | 2008-09-17 11:54:24 -0400 |
commit | 22d203ecef9b0cc1fa8d8f64c935b451ca7d1022 (patch) | |
tree | 5f310e8cff93a533f877f01f99b2fe8a8fc5f919 /drivers/uwb | |
parent | 0612edfd95ffe92201a2267e9e1b0fc68becf76d (diff) |
uwb: add the UWB stack (MLME)
Most of the MAC Layer Management Entity (MLME) support: address, beacon, IE
and scan management.
Signed-off-by: David Vrabel <david.vrabel@csr.com>
Diffstat (limited to 'drivers/uwb')
-rw-r--r-- | drivers/uwb/address.c | 374 | ||||
-rw-r--r-- | drivers/uwb/beacon.c | 644 | ||||
-rw-r--r-- | drivers/uwb/ie.c | 570 | ||||
-rw-r--r-- | drivers/uwb/scan.c | 133 |
4 files changed, 1721 insertions, 0 deletions
diff --git a/drivers/uwb/address.c b/drivers/uwb/address.c new file mode 100644 index 000000000000..1664ae5f1706 --- /dev/null +++ b/drivers/uwb/address.c | |||
@@ -0,0 +1,374 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Address management | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * FIXME: docs | ||
24 | */ | ||
25 | |||
26 | #include <linux/errno.h> | ||
27 | #include <linux/module.h> | ||
28 | #include <linux/device.h> | ||
29 | #include <linux/random.h> | ||
30 | #include <linux/etherdevice.h> | ||
31 | #include <linux/uwb/debug.h> | ||
32 | #include "uwb-internal.h" | ||
33 | |||
34 | |||
35 | /** Device Address Management command */ | ||
36 | struct uwb_rc_cmd_dev_addr_mgmt { | ||
37 | struct uwb_rccb rccb; | ||
38 | u8 bmOperationType; | ||
39 | u8 baAddr[6]; | ||
40 | } __attribute__((packed)); | ||
41 | |||
42 | |||
43 | /** | ||
44 | * Low level command for setting/getting UWB radio's addresses | ||
45 | * | ||
46 | * @hwarc: HWA Radio Control interface instance | ||
47 | * @bmOperationType: | ||
48 | * Set/get, MAC/DEV (see WUSB1.0[8.6.2.2]) | ||
49 | * @baAddr: address buffer--assumed to have enough data to hold | ||
50 | * the address type requested. | ||
51 | * @reply: Pointer to reply buffer (can be stack allocated) | ||
52 | * @returns: 0 if ok, < 0 errno code on error. | ||
53 | * | ||
54 | * @cmd has to be allocated because USB cannot grok USB or vmalloc | ||
55 | * buffers depending on your combination of host architecture. | ||
56 | */ | ||
57 | static | ||
58 | int uwb_rc_dev_addr_mgmt(struct uwb_rc *rc, | ||
59 | u8 bmOperationType, const u8 *baAddr, | ||
60 | struct uwb_rc_evt_dev_addr_mgmt *reply) | ||
61 | { | ||
62 | int result; | ||
63 | struct uwb_rc_cmd_dev_addr_mgmt *cmd; | ||
64 | |||
65 | result = -ENOMEM; | ||
66 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
67 | if (cmd == NULL) | ||
68 | goto error_kzalloc; | ||
69 | cmd->rccb.bCommandType = UWB_RC_CET_GENERAL; | ||
70 | cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_DEV_ADDR_MGMT); | ||
71 | cmd->bmOperationType = bmOperationType; | ||
72 | if (baAddr) { | ||
73 | size_t size = 0; | ||
74 | switch (bmOperationType >> 1) { | ||
75 | case 0: size = 2; break; | ||
76 | case 1: size = 6; break; | ||
77 | default: BUG(); | ||
78 | } | ||
79 | memcpy(cmd->baAddr, baAddr, size); | ||
80 | } | ||
81 | reply->rceb.bEventType = UWB_RC_CET_GENERAL; | ||
82 | reply->rceb.wEvent = UWB_RC_CMD_DEV_ADDR_MGMT; | ||
83 | result = uwb_rc_cmd(rc, "DEV-ADDR-MGMT", | ||
84 | &cmd->rccb, sizeof(*cmd), | ||
85 | &reply->rceb, sizeof(*reply)); | ||
86 | if (result < 0) | ||
87 | goto error_cmd; | ||
88 | if (result < sizeof(*reply)) { | ||
89 | dev_err(&rc->uwb_dev.dev, | ||
90 | "DEV-ADDR-MGMT: not enough data replied: " | ||
91 | "%d vs %zu bytes needed\n", result, sizeof(*reply)); | ||
92 | result = -ENOMSG; | ||
93 | } else if (reply->bResultCode != UWB_RC_RES_SUCCESS) { | ||
94 | dev_err(&rc->uwb_dev.dev, | ||
95 | "DEV-ADDR-MGMT: command execution failed: %s (%d)\n", | ||
96 | uwb_rc_strerror(reply->bResultCode), | ||
97 | reply->bResultCode); | ||
98 | result = -EIO; | ||
99 | } else | ||
100 | result = 0; | ||
101 | error_cmd: | ||
102 | kfree(cmd); | ||
103 | error_kzalloc: | ||
104 | return result; | ||
105 | } | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Set the UWB RC MAC or device address. | ||
110 | * | ||
111 | * @rc: UWB Radio Controller | ||
112 | * @_addr: Pointer to address to write [assumed to be either a | ||
113 | * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *']. | ||
114 | * @type: Type of address to set (UWB_ADDR_DEV or UWB_ADDR_MAC). | ||
115 | * @returns: 0 if ok, < 0 errno code on error. | ||
116 | * | ||
117 | * Some anal retentivity here: even if both 'struct | ||
118 | * uwb_{dev,mac}_addr' have the actual byte array in the same offset | ||
119 | * and I could just pass _addr to hwarc_cmd_dev_addr_mgmt(), I prefer | ||
120 | * to use some syntatic sugar in case someday we decide to change the | ||
121 | * format of the structs. The compiler will optimize it out anyway. | ||
122 | */ | ||
123 | static int uwb_rc_addr_set(struct uwb_rc *rc, | ||
124 | const void *_addr, enum uwb_addr_type type) | ||
125 | { | ||
126 | int result; | ||
127 | u8 bmOperationType = 0x1; /* Set address */ | ||
128 | const struct uwb_dev_addr *dev_addr = _addr; | ||
129 | const struct uwb_mac_addr *mac_addr = _addr; | ||
130 | struct uwb_rc_evt_dev_addr_mgmt reply; | ||
131 | const u8 *baAddr; | ||
132 | |||
133 | result = -EINVAL; | ||
134 | switch (type) { | ||
135 | case UWB_ADDR_DEV: | ||
136 | baAddr = dev_addr->data; | ||
137 | break; | ||
138 | case UWB_ADDR_MAC: | ||
139 | baAddr = mac_addr->data; | ||
140 | bmOperationType |= 0x2; | ||
141 | break; | ||
142 | default: | ||
143 | return result; | ||
144 | } | ||
145 | return uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &reply); | ||
146 | } | ||
147 | |||
148 | |||
149 | /** | ||
150 | * Get the UWB radio's MAC or device address. | ||
151 | * | ||
152 | * @rc: UWB Radio Controller | ||
153 | * @_addr: Where to write the address data [assumed to be either a | ||
154 | * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *']. | ||
155 | * @type: Type of address to get (UWB_ADDR_DEV or UWB_ADDR_MAC). | ||
156 | * @returns: 0 if ok (and *_addr set), < 0 errno code on error. | ||
157 | * | ||
158 | * See comment in uwb_rc_addr_set() about anal retentivity in the | ||
159 | * type handling of the address variables. | ||
160 | */ | ||
161 | static int uwb_rc_addr_get(struct uwb_rc *rc, | ||
162 | void *_addr, enum uwb_addr_type type) | ||
163 | { | ||
164 | int result; | ||
165 | u8 bmOperationType = 0x0; /* Get address */ | ||
166 | struct uwb_rc_evt_dev_addr_mgmt evt; | ||
167 | struct uwb_dev_addr *dev_addr = _addr; | ||
168 | struct uwb_mac_addr *mac_addr = _addr; | ||
169 | u8 *baAddr; | ||
170 | |||
171 | result = -EINVAL; | ||
172 | switch (type) { | ||
173 | case UWB_ADDR_DEV: | ||
174 | baAddr = dev_addr->data; | ||
175 | break; | ||
176 | case UWB_ADDR_MAC: | ||
177 | bmOperationType |= 0x2; | ||
178 | baAddr = mac_addr->data; | ||
179 | break; | ||
180 | default: | ||
181 | return result; | ||
182 | } | ||
183 | result = uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &evt); | ||
184 | if (result == 0) | ||
185 | switch (type) { | ||
186 | case UWB_ADDR_DEV: | ||
187 | memcpy(&dev_addr->data, evt.baAddr, | ||
188 | sizeof(dev_addr->data)); | ||
189 | break; | ||
190 | case UWB_ADDR_MAC: | ||
191 | memcpy(&mac_addr->data, evt.baAddr, | ||
192 | sizeof(mac_addr->data)); | ||
193 | break; | ||
194 | default: /* shut gcc up */ | ||
195 | BUG(); | ||
196 | } | ||
197 | return result; | ||
198 | } | ||
199 | |||
200 | |||
201 | /** Get @rc's MAC address to @addr */ | ||
202 | int uwb_rc_mac_addr_get(struct uwb_rc *rc, | ||
203 | struct uwb_mac_addr *addr) { | ||
204 | return uwb_rc_addr_get(rc, addr, UWB_ADDR_MAC); | ||
205 | } | ||
206 | EXPORT_SYMBOL_GPL(uwb_rc_mac_addr_get); | ||
207 | |||
208 | |||
209 | /** Get @rc's device address to @addr */ | ||
210 | int uwb_rc_dev_addr_get(struct uwb_rc *rc, | ||
211 | struct uwb_dev_addr *addr) { | ||
212 | return uwb_rc_addr_get(rc, addr, UWB_ADDR_DEV); | ||
213 | } | ||
214 | EXPORT_SYMBOL_GPL(uwb_rc_dev_addr_get); | ||
215 | |||
216 | |||
217 | /** Set @rc's address to @addr */ | ||
218 | int uwb_rc_mac_addr_set(struct uwb_rc *rc, | ||
219 | const struct uwb_mac_addr *addr) | ||
220 | { | ||
221 | int result = -EINVAL; | ||
222 | mutex_lock(&rc->uwb_dev.mutex); | ||
223 | result = uwb_rc_addr_set(rc, addr, UWB_ADDR_MAC); | ||
224 | mutex_unlock(&rc->uwb_dev.mutex); | ||
225 | return result; | ||
226 | } | ||
227 | |||
228 | |||
229 | /** Set @rc's address to @addr */ | ||
230 | int uwb_rc_dev_addr_set(struct uwb_rc *rc, | ||
231 | const struct uwb_dev_addr *addr) | ||
232 | { | ||
233 | int result = -EINVAL; | ||
234 | mutex_lock(&rc->uwb_dev.mutex); | ||
235 | result = uwb_rc_addr_set(rc, addr, UWB_ADDR_DEV); | ||
236 | rc->uwb_dev.dev_addr = *addr; | ||
237 | mutex_unlock(&rc->uwb_dev.mutex); | ||
238 | return result; | ||
239 | } | ||
240 | |||
241 | /* Returns !0 if given address is already assigned to device. */ | ||
242 | int __uwb_mac_addr_assigned_check(struct device *dev, void *_addr) | ||
243 | { | ||
244 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
245 | struct uwb_mac_addr *addr = _addr; | ||
246 | |||
247 | if (!uwb_mac_addr_cmp(addr, &uwb_dev->mac_addr)) | ||
248 | return !0; | ||
249 | return 0; | ||
250 | } | ||
251 | |||
252 | /* Returns !0 if given address is already assigned to device. */ | ||
253 | int __uwb_dev_addr_assigned_check(struct device *dev, void *_addr) | ||
254 | { | ||
255 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
256 | struct uwb_dev_addr *addr = _addr; | ||
257 | if (!uwb_dev_addr_cmp(addr, &uwb_dev->dev_addr)) | ||
258 | return !0; | ||
259 | return 0; | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * uwb_dev_addr_assign - assigned a generated DevAddr to a radio controller | ||
264 | * @rc: the (local) radio controller device requiring a new DevAddr | ||
265 | * | ||
266 | * A new DevAddr is required when: | ||
267 | * - first setting up a radio controller | ||
268 | * - if the hardware reports a DevAddr conflict | ||
269 | * | ||
270 | * The DevAddr is randomly generated in the generated DevAddr range | ||
271 | * [0x100, 0xfeff]. The number of devices in a beacon group is limited | ||
272 | * by mMaxBPLength (96) so this address space will never be exhausted. | ||
273 | * | ||
274 | * [ECMA-368] 17.1.1, 17.16. | ||
275 | */ | ||
276 | int uwb_rc_dev_addr_assign(struct uwb_rc *rc) | ||
277 | { | ||
278 | struct uwb_dev_addr new_addr; | ||
279 | |||
280 | do { | ||
281 | get_random_bytes(new_addr.data, sizeof(new_addr.data)); | ||
282 | } while (new_addr.data[0] == 0x00 || new_addr.data[0] == 0xff | ||
283 | || __uwb_dev_addr_assigned(rc, &new_addr)); | ||
284 | |||
285 | return uwb_rc_dev_addr_set(rc, &new_addr); | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * uwbd_evt_handle_rc_dev_addr_conflict - handle a DEV_ADDR_CONFLICT event | ||
290 | * @evt: the DEV_ADDR_CONFLICT notification from the radio controller | ||
291 | * | ||
292 | * A new (non-conflicting) DevAddr is assigned to the radio controller. | ||
293 | * | ||
294 | * [ECMA-368] 17.1.1.1. | ||
295 | */ | ||
296 | int uwbd_evt_handle_rc_dev_addr_conflict(struct uwb_event *evt) | ||
297 | { | ||
298 | struct uwb_rc *rc = evt->rc; | ||
299 | |||
300 | return uwb_rc_dev_addr_assign(rc); | ||
301 | } | ||
302 | |||
303 | /* | ||
304 | * Print the 48-bit EUI MAC address of the radio controller when | ||
305 | * reading /sys/class/uwb_rc/XX/mac_address | ||
306 | */ | ||
307 | static ssize_t uwb_rc_mac_addr_show(struct device *dev, | ||
308 | struct device_attribute *attr, char *buf) | ||
309 | { | ||
310 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
311 | struct uwb_rc *rc = uwb_dev->rc; | ||
312 | struct uwb_mac_addr addr; | ||
313 | ssize_t result; | ||
314 | |||
315 | mutex_lock(&rc->uwb_dev.mutex); | ||
316 | result = uwb_rc_addr_get(rc, &addr, UWB_ADDR_MAC); | ||
317 | mutex_unlock(&rc->uwb_dev.mutex); | ||
318 | if (result >= 0) { | ||
319 | result = uwb_mac_addr_print(buf, UWB_ADDR_STRSIZE, &addr); | ||
320 | buf[result++] = '\n'; | ||
321 | } | ||
322 | return result; | ||
323 | } | ||
324 | |||
325 | /* | ||
326 | * Parse a 48 bit address written to /sys/class/uwb_rc/XX/mac_address | ||
327 | * and if correct, set it. | ||
328 | */ | ||
329 | static ssize_t uwb_rc_mac_addr_store(struct device *dev, | ||
330 | struct device_attribute *attr, | ||
331 | const char *buf, size_t size) | ||
332 | { | ||
333 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
334 | struct uwb_rc *rc = uwb_dev->rc; | ||
335 | struct uwb_mac_addr addr; | ||
336 | ssize_t result; | ||
337 | |||
338 | result = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\n", | ||
339 | &addr.data[0], &addr.data[1], &addr.data[2], | ||
340 | &addr.data[3], &addr.data[4], &addr.data[5]); | ||
341 | if (result != 6) { | ||
342 | result = -EINVAL; | ||
343 | goto out; | ||
344 | } | ||
345 | if (is_multicast_ether_addr(addr.data)) { | ||
346 | dev_err(&rc->uwb_dev.dev, "refusing to set multicast " | ||
347 | "MAC address %s\n", buf); | ||
348 | result = -EINVAL; | ||
349 | goto out; | ||
350 | } | ||
351 | result = uwb_rc_mac_addr_set(rc, &addr); | ||
352 | if (result == 0) | ||
353 | rc->uwb_dev.mac_addr = addr; | ||
354 | out: | ||
355 | return result < 0 ? result : size; | ||
356 | } | ||
357 | DEVICE_ATTR(mac_address, S_IRUGO | S_IWUSR, uwb_rc_mac_addr_show, uwb_rc_mac_addr_store); | ||
358 | |||
359 | /** Print @addr to @buf, @return bytes written */ | ||
360 | size_t __uwb_addr_print(char *buf, size_t buf_size, const unsigned char *addr, | ||
361 | int type) | ||
362 | { | ||
363 | size_t result; | ||
364 | if (type) | ||
365 | result = scnprintf(buf, buf_size, | ||
366 | "%02x:%02x:%02x:%02x:%02x:%02x", | ||
367 | addr[0], addr[1], addr[2], | ||
368 | addr[3], addr[4], addr[5]); | ||
369 | else | ||
370 | result = scnprintf(buf, buf_size, "%02x:%02x", | ||
371 | addr[1], addr[0]); | ||
372 | return result; | ||
373 | } | ||
374 | EXPORT_SYMBOL_GPL(__uwb_addr_print); | ||
diff --git a/drivers/uwb/beacon.c b/drivers/uwb/beacon.c new file mode 100644 index 000000000000..f65a52c0afac --- /dev/null +++ b/drivers/uwb/beacon.c | |||
@@ -0,0 +1,644 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Beacon management | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * FIXME: docs | ||
24 | */ | ||
25 | |||
26 | #include <linux/kernel.h> | ||
27 | #include <linux/init.h> | ||
28 | #include <linux/module.h> | ||
29 | #include <linux/device.h> | ||
30 | #include <linux/err.h> | ||
31 | #include <linux/kdev_t.h> | ||
32 | #include "uwb-internal.h" | ||
33 | |||
34 | #define D_LOCAL 0 | ||
35 | #include <linux/uwb/debug.h> | ||
36 | |||
37 | /** Start Beaconing command structure */ | ||
38 | struct uwb_rc_cmd_start_beacon { | ||
39 | struct uwb_rccb rccb; | ||
40 | __le16 wBPSTOffset; | ||
41 | u8 bChannelNumber; | ||
42 | } __attribute__((packed)); | ||
43 | |||
44 | |||
45 | static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel) | ||
46 | { | ||
47 | int result; | ||
48 | struct uwb_rc_cmd_start_beacon *cmd; | ||
49 | struct uwb_rc_evt_confirm reply; | ||
50 | |||
51 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
52 | if (cmd == NULL) | ||
53 | return -ENOMEM; | ||
54 | cmd->rccb.bCommandType = UWB_RC_CET_GENERAL; | ||
55 | cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON); | ||
56 | cmd->wBPSTOffset = cpu_to_le16(bpst_offset); | ||
57 | cmd->bChannelNumber = channel; | ||
58 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; | ||
59 | reply.rceb.wEvent = UWB_RC_CMD_START_BEACON; | ||
60 | result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd), | ||
61 | &reply.rceb, sizeof(reply)); | ||
62 | if (result < 0) | ||
63 | goto error_cmd; | ||
64 | if (reply.bResultCode != UWB_RC_RES_SUCCESS) { | ||
65 | dev_err(&rc->uwb_dev.dev, | ||
66 | "START-BEACON: command execution failed: %s (%d)\n", | ||
67 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); | ||
68 | result = -EIO; | ||
69 | } | ||
70 | error_cmd: | ||
71 | kfree(cmd); | ||
72 | return result; | ||
73 | } | ||
74 | |||
75 | static int uwb_rc_stop_beacon(struct uwb_rc *rc) | ||
76 | { | ||
77 | int result; | ||
78 | struct uwb_rccb *cmd; | ||
79 | struct uwb_rc_evt_confirm reply; | ||
80 | |||
81 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
82 | if (cmd == NULL) | ||
83 | return -ENOMEM; | ||
84 | cmd->bCommandType = UWB_RC_CET_GENERAL; | ||
85 | cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON); | ||
86 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; | ||
87 | reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON; | ||
88 | result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd), | ||
89 | &reply.rceb, sizeof(reply)); | ||
90 | if (result < 0) | ||
91 | goto error_cmd; | ||
92 | if (reply.bResultCode != UWB_RC_RES_SUCCESS) { | ||
93 | dev_err(&rc->uwb_dev.dev, | ||
94 | "STOP-BEACON: command execution failed: %s (%d)\n", | ||
95 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); | ||
96 | result = -EIO; | ||
97 | } | ||
98 | error_cmd: | ||
99 | kfree(cmd); | ||
100 | return result; | ||
101 | } | ||
102 | |||
103 | /* | ||
104 | * Start/stop beacons | ||
105 | * | ||
106 | * @rc: UWB Radio Controller to operate on | ||
107 | * @channel: UWB channel on which to beacon (WUSB[table | ||
108 | * 5-12]). If -1, stop beaconing. | ||
109 | * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero | ||
110 | * | ||
111 | * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB | ||
112 | * of a SET IE command after the device sent the first beacon that includes | ||
113 | * the IEs specified in the SET IE command. So, after we start beaconing we | ||
114 | * check if there is anything in the IE cache and call the SET IE command | ||
115 | * if needed. | ||
116 | */ | ||
117 | int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset) | ||
118 | { | ||
119 | int result; | ||
120 | struct device *dev = &rc->uwb_dev.dev; | ||
121 | |||
122 | mutex_lock(&rc->uwb_dev.mutex); | ||
123 | if (channel < 0) | ||
124 | channel = -1; | ||
125 | if (channel == -1) | ||
126 | result = uwb_rc_stop_beacon(rc); | ||
127 | else { | ||
128 | /* channel >= 0...dah */ | ||
129 | result = uwb_rc_start_beacon(rc, bpst_offset, channel); | ||
130 | if (result < 0) | ||
131 | goto out_up; | ||
132 | if (le16_to_cpu(rc->ies->wIELength) > 0) { | ||
133 | result = uwb_rc_set_ie(rc, rc->ies); | ||
134 | if (result < 0) { | ||
135 | dev_err(dev, "Cannot set new IE on device: " | ||
136 | "%d\n", result); | ||
137 | result = uwb_rc_stop_beacon(rc); | ||
138 | channel = -1; | ||
139 | bpst_offset = 0; | ||
140 | } else | ||
141 | result = 0; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | if (result < 0) | ||
146 | goto out_up; | ||
147 | rc->beaconing = channel; | ||
148 | |||
149 | uwb_notify(rc, NULL, uwb_bg_joined(rc) ? UWB_NOTIF_BG_JOIN : UWB_NOTIF_BG_LEAVE); | ||
150 | |||
151 | out_up: | ||
152 | mutex_unlock(&rc->uwb_dev.mutex); | ||
153 | return result; | ||
154 | } | ||
155 | |||
156 | /* | ||
157 | * Beacon cache | ||
158 | * | ||
159 | * The purpose of this is to speed up the lookup of becon information | ||
160 | * when a new beacon arrives. The UWB Daemon uses it also to keep a | ||
161 | * tab of which devices are in radio distance and which not. When a | ||
162 | * device's beacon stays present for more than a certain amount of | ||
163 | * time, it is considered a new, usable device. When a beacon ceases | ||
164 | * to be received for a certain amount of time, it is considered that | ||
165 | * the device is gone. | ||
166 | * | ||
167 | * FIXME: use an allocator for the entries | ||
168 | * FIXME: use something faster for search than a list | ||
169 | */ | ||
170 | |||
171 | struct uwb_beca uwb_beca = { | ||
172 | .list = LIST_HEAD_INIT(uwb_beca.list), | ||
173 | .mutex = __MUTEX_INITIALIZER(uwb_beca.mutex) | ||
174 | }; | ||
175 | |||
176 | |||
177 | void uwb_bce_kfree(struct kref *_bce) | ||
178 | { | ||
179 | struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt); | ||
180 | |||
181 | kfree(bce->be); | ||
182 | kfree(bce); | ||
183 | } | ||
184 | |||
185 | |||
186 | /* Find a beacon by dev addr in the cache */ | ||
187 | static | ||
188 | struct uwb_beca_e *__uwb_beca_find_bydev(const struct uwb_dev_addr *dev_addr) | ||
189 | { | ||
190 | struct uwb_beca_e *bce, *next; | ||
191 | list_for_each_entry_safe(bce, next, &uwb_beca.list, node) { | ||
192 | d_printf(6, NULL, "looking for addr %02x:%02x in %02x:%02x\n", | ||
193 | dev_addr->data[0], dev_addr->data[1], | ||
194 | bce->dev_addr.data[0], bce->dev_addr.data[1]); | ||
195 | if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr))) | ||
196 | goto out; | ||
197 | } | ||
198 | bce = NULL; | ||
199 | out: | ||
200 | return bce; | ||
201 | } | ||
202 | |||
203 | /* Find a beacon by dev addr in the cache */ | ||
204 | static | ||
205 | struct uwb_beca_e *__uwb_beca_find_bymac(const struct uwb_mac_addr *mac_addr) | ||
206 | { | ||
207 | struct uwb_beca_e *bce, *next; | ||
208 | list_for_each_entry_safe(bce, next, &uwb_beca.list, node) { | ||
209 | if (!memcmp(bce->mac_addr, mac_addr->data, | ||
210 | sizeof(bce->mac_addr))) | ||
211 | goto out; | ||
212 | } | ||
213 | bce = NULL; | ||
214 | out: | ||
215 | return bce; | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr | ||
220 | * @rc: the radio controller that saw the device | ||
221 | * @devaddr: DevAddr of the UWB device to find | ||
222 | * | ||
223 | * There may be more than one matching device (in the case of a | ||
224 | * DevAddr conflict), but only the first one is returned. | ||
225 | */ | ||
226 | struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc, | ||
227 | const struct uwb_dev_addr *devaddr) | ||
228 | { | ||
229 | struct uwb_dev *found = NULL; | ||
230 | struct uwb_beca_e *bce; | ||
231 | |||
232 | mutex_lock(&uwb_beca.mutex); | ||
233 | bce = __uwb_beca_find_bydev(devaddr); | ||
234 | if (bce) | ||
235 | found = uwb_dev_try_get(rc, bce->uwb_dev); | ||
236 | mutex_unlock(&uwb_beca.mutex); | ||
237 | |||
238 | return found; | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48 | ||
243 | * @rc: the radio controller that saw the device | ||
244 | * @devaddr: EUI-48 of the UWB device to find | ||
245 | */ | ||
246 | struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc, | ||
247 | const struct uwb_mac_addr *macaddr) | ||
248 | { | ||
249 | struct uwb_dev *found = NULL; | ||
250 | struct uwb_beca_e *bce; | ||
251 | |||
252 | mutex_lock(&uwb_beca.mutex); | ||
253 | bce = __uwb_beca_find_bymac(macaddr); | ||
254 | if (bce) | ||
255 | found = uwb_dev_try_get(rc, bce->uwb_dev); | ||
256 | mutex_unlock(&uwb_beca.mutex); | ||
257 | |||
258 | return found; | ||
259 | } | ||
260 | |||
261 | /* Initialize a beacon cache entry */ | ||
262 | static void uwb_beca_e_init(struct uwb_beca_e *bce) | ||
263 | { | ||
264 | mutex_init(&bce->mutex); | ||
265 | kref_init(&bce->refcnt); | ||
266 | stats_init(&bce->lqe_stats); | ||
267 | stats_init(&bce->rssi_stats); | ||
268 | } | ||
269 | |||
270 | /* | ||
271 | * Add a beacon to the cache | ||
272 | * | ||
273 | * @be: Beacon event information | ||
274 | * @bf: Beacon frame (part of b, really) | ||
275 | * @ts_jiffies: Timestamp (in jiffies) when the beacon was received | ||
276 | */ | ||
277 | struct uwb_beca_e *__uwb_beca_add(struct uwb_rc_evt_beacon *be, | ||
278 | struct uwb_beacon_frame *bf, | ||
279 | unsigned long ts_jiffies) | ||
280 | { | ||
281 | struct uwb_beca_e *bce; | ||
282 | |||
283 | bce = kzalloc(sizeof(*bce), GFP_KERNEL); | ||
284 | if (bce == NULL) | ||
285 | return NULL; | ||
286 | uwb_beca_e_init(bce); | ||
287 | bce->ts_jiffies = ts_jiffies; | ||
288 | bce->uwb_dev = NULL; | ||
289 | list_add(&bce->node, &uwb_beca.list); | ||
290 | return bce; | ||
291 | } | ||
292 | |||
293 | /* | ||
294 | * Wipe out beacon entries that became stale | ||
295 | * | ||
296 | * Remove associated devicest too. | ||
297 | */ | ||
298 | void uwb_beca_purge(void) | ||
299 | { | ||
300 | struct uwb_beca_e *bce, *next; | ||
301 | unsigned long now = jiffies; | ||
302 | mutex_lock(&uwb_beca.mutex); | ||
303 | list_for_each_entry_safe(bce, next, &uwb_beca.list, node) { | ||
304 | if (now - bce->ts_jiffies | ||
305 | > msecs_to_jiffies(beacon_timeout_ms)) { | ||
306 | uwbd_dev_offair(bce); | ||
307 | list_del(&bce->node); | ||
308 | uwb_bce_put(bce); | ||
309 | } | ||
310 | } | ||
311 | mutex_unlock(&uwb_beca.mutex); | ||
312 | } | ||
313 | |||
314 | /* Clean up the whole beacon cache. Called on shutdown */ | ||
315 | void uwb_beca_release(void) | ||
316 | { | ||
317 | struct uwb_beca_e *bce, *next; | ||
318 | mutex_lock(&uwb_beca.mutex); | ||
319 | list_for_each_entry_safe(bce, next, &uwb_beca.list, node) { | ||
320 | list_del(&bce->node); | ||
321 | uwb_bce_put(bce); | ||
322 | } | ||
323 | mutex_unlock(&uwb_beca.mutex); | ||
324 | } | ||
325 | |||
326 | static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be, | ||
327 | struct uwb_beacon_frame *bf) | ||
328 | { | ||
329 | char macbuf[UWB_ADDR_STRSIZE]; | ||
330 | char devbuf[UWB_ADDR_STRSIZE]; | ||
331 | char dstbuf[UWB_ADDR_STRSIZE]; | ||
332 | |||
333 | uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier); | ||
334 | uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr); | ||
335 | uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr); | ||
336 | dev_info(&rc->uwb_dev.dev, | ||
337 | "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n", | ||
338 | devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset, | ||
339 | bf->Beacon_Slot_Number, macbuf); | ||
340 | } | ||
341 | |||
342 | /* | ||
343 | * @bce: beacon cache entry, referenced | ||
344 | */ | ||
345 | ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce, | ||
346 | char *buf, size_t size) | ||
347 | { | ||
348 | ssize_t result = 0; | ||
349 | struct uwb_rc_evt_beacon *be; | ||
350 | struct uwb_beacon_frame *bf; | ||
351 | struct uwb_buf_ctx ctx = { | ||
352 | .buf = buf, | ||
353 | .bytes = 0, | ||
354 | .size = size | ||
355 | }; | ||
356 | |||
357 | mutex_lock(&bce->mutex); | ||
358 | be = bce->be; | ||
359 | if (be == NULL) | ||
360 | goto out; | ||
361 | bf = (void *) be->BeaconInfo; | ||
362 | uwb_ie_for_each(uwb_dev, uwb_ie_dump_hex, &ctx, | ||
363 | bf->IEData, be->wBeaconInfoLength - sizeof(*bf)); | ||
364 | result = ctx.bytes; | ||
365 | out: | ||
366 | mutex_unlock(&bce->mutex); | ||
367 | return result; | ||
368 | } | ||
369 | |||
370 | /* | ||
371 | * Verify that the beacon event, frame and IEs are ok | ||
372 | */ | ||
373 | static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt, | ||
374 | struct uwb_rc_evt_beacon *be) | ||
375 | { | ||
376 | int result = -EINVAL; | ||
377 | struct uwb_beacon_frame *bf; | ||
378 | struct device *dev = &rc->uwb_dev.dev; | ||
379 | |||
380 | /* Is there enough data to decode a beacon frame? */ | ||
381 | if (evt->notif.size < sizeof(*be) + sizeof(*bf)) { | ||
382 | dev_err(dev, "BEACON event: Not enough data to decode " | ||
383 | "(%zu vs %zu bytes needed)\n", evt->notif.size, | ||
384 | sizeof(*be) + sizeof(*bf)); | ||
385 | goto error; | ||
386 | } | ||
387 | /* FIXME: make sure beacon frame IEs are fine and that the whole thing | ||
388 | * is consistent */ | ||
389 | result = 0; | ||
390 | error: | ||
391 | return result; | ||
392 | } | ||
393 | |||
394 | /* | ||
395 | * Handle UWB_RC_EVT_BEACON events | ||
396 | * | ||
397 | * We check the beacon cache to see how the received beacon fares. If | ||
398 | * is there already we refresh the timestamp. If not we create a new | ||
399 | * entry. | ||
400 | * | ||
401 | * According to the WHCI and WUSB specs, only one beacon frame is | ||
402 | * allowed per notification block, so we don't bother about scanning | ||
403 | * for more. | ||
404 | */ | ||
405 | int uwbd_evt_handle_rc_beacon(struct uwb_event *evt) | ||
406 | { | ||
407 | int result = -EINVAL; | ||
408 | struct uwb_rc *rc; | ||
409 | struct uwb_rc_evt_beacon *be; | ||
410 | struct uwb_beacon_frame *bf; | ||
411 | struct uwb_beca_e *bce; | ||
412 | struct device *dev = &evt->rc->uwb_dev.dev; | ||
413 | unsigned long last_ts; | ||
414 | |||
415 | rc = evt->rc; | ||
416 | be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb); | ||
417 | result = uwb_verify_beacon(rc, evt, be); | ||
418 | if (result < 0) | ||
419 | return result; | ||
420 | |||
421 | /* Ignore beacon if it is from an alien. */ | ||
422 | if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN || | ||
423 | be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) { | ||
424 | if (printk_ratelimit()) | ||
425 | dev_err(dev, "BEACON received from ALIEN. Action? \n"); | ||
426 | result = -ENOSYS; | ||
427 | return 0; | ||
428 | } | ||
429 | bf = (struct uwb_beacon_frame *) be->BeaconInfo; | ||
430 | |||
431 | /* | ||
432 | * Drop beacons from devices with a NULL EUI-48 -- they cannot | ||
433 | * be uniquely identified. | ||
434 | * | ||
435 | * It's expected that these will all be WUSB devices and they | ||
436 | * have a WUSB specific connection method so ignoring them | ||
437 | * here shouldn't be a problem. | ||
438 | */ | ||
439 | if (uwb_mac_addr_bcast(&bf->Device_Identifier)) | ||
440 | return 0; | ||
441 | |||
442 | mutex_lock(&uwb_beca.mutex); | ||
443 | bce = __uwb_beca_find_bymac(&bf->Device_Identifier); | ||
444 | if (bce == NULL) { | ||
445 | /* Not in there, a new device is pinging */ | ||
446 | uwb_beacon_print(evt->rc, be, bf); | ||
447 | bce = __uwb_beca_add(be, bf, evt->ts_jiffies); | ||
448 | if (bce == NULL) { | ||
449 | mutex_unlock(&uwb_beca.mutex); | ||
450 | return -ENOMEM; | ||
451 | } | ||
452 | } | ||
453 | mutex_unlock(&uwb_beca.mutex); | ||
454 | |||
455 | mutex_lock(&bce->mutex); | ||
456 | /* purge old beacon data */ | ||
457 | kfree(bce->be); | ||
458 | |||
459 | last_ts = bce->ts_jiffies; | ||
460 | |||
461 | /* Update commonly used fields */ | ||
462 | bce->ts_jiffies = evt->ts_jiffies; | ||
463 | bce->be = be; | ||
464 | bce->dev_addr = bf->hdr.SrcAddr; | ||
465 | bce->mac_addr = &bf->Device_Identifier; | ||
466 | be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset); | ||
467 | be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength); | ||
468 | stats_add_sample(&bce->lqe_stats, be->bLQI - 7); | ||
469 | stats_add_sample(&bce->rssi_stats, be->bRSSI + 18); | ||
470 | |||
471 | /* | ||
472 | * This might be a beacon from a new device. | ||
473 | */ | ||
474 | if (bce->uwb_dev == NULL) | ||
475 | uwbd_dev_onair(evt->rc, bce); | ||
476 | |||
477 | mutex_unlock(&bce->mutex); | ||
478 | |||
479 | return 1; /* we keep the event data */ | ||
480 | } | ||
481 | |||
482 | /* | ||
483 | * Handle UWB_RC_EVT_BEACON_SIZE events | ||
484 | * | ||
485 | * XXXXX | ||
486 | */ | ||
487 | int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt) | ||
488 | { | ||
489 | int result = -EINVAL; | ||
490 | struct device *dev = &evt->rc->uwb_dev.dev; | ||
491 | struct uwb_rc_evt_beacon_size *bs; | ||
492 | |||
493 | /* Is there enough data to decode the event? */ | ||
494 | if (evt->notif.size < sizeof(*bs)) { | ||
495 | dev_err(dev, "BEACON SIZE notification: Not enough data to " | ||
496 | "decode (%zu vs %zu bytes needed)\n", | ||
497 | evt->notif.size, sizeof(*bs)); | ||
498 | goto error; | ||
499 | } | ||
500 | bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb); | ||
501 | if (0) | ||
502 | dev_info(dev, "Beacon size changed to %u bytes " | ||
503 | "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize)); | ||
504 | else { | ||
505 | /* temporary hack until we do something with this message... */ | ||
506 | static unsigned count; | ||
507 | if (++count % 1000 == 0) | ||
508 | dev_info(dev, "Beacon size changed %u times " | ||
509 | "(FIXME: action?)\n", count); | ||
510 | } | ||
511 | result = 0; | ||
512 | error: | ||
513 | return result; | ||
514 | } | ||
515 | |||
516 | /** | ||
517 | * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event | ||
518 | * @evt: the BP_SLOT_CHANGE notification from the radio controller | ||
519 | * | ||
520 | * If the event indicates that no beacon period slots were available | ||
521 | * then radio controller has transitioned to a non-beaconing state. | ||
522 | * Otherwise, simply save the current beacon slot. | ||
523 | */ | ||
524 | int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt) | ||
525 | { | ||
526 | struct uwb_rc *rc = evt->rc; | ||
527 | struct device *dev = &rc->uwb_dev.dev; | ||
528 | struct uwb_rc_evt_bp_slot_change *bpsc; | ||
529 | |||
530 | if (evt->notif.size < sizeof(*bpsc)) { | ||
531 | dev_err(dev, "BP SLOT CHANGE event: Not enough data\n"); | ||
532 | return -EINVAL; | ||
533 | } | ||
534 | bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb); | ||
535 | |||
536 | mutex_lock(&rc->uwb_dev.mutex); | ||
537 | if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) { | ||
538 | dev_info(dev, "stopped beaconing: No free slots in BP\n"); | ||
539 | rc->beaconing = -1; | ||
540 | } else | ||
541 | rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc); | ||
542 | mutex_unlock(&rc->uwb_dev.mutex); | ||
543 | |||
544 | return 0; | ||
545 | } | ||
546 | |||
547 | /** | ||
548 | * Handle UWB_RC_EVT_BPOIE_CHANGE events | ||
549 | * | ||
550 | * XXXXX | ||
551 | */ | ||
552 | struct uwb_ie_bpo { | ||
553 | struct uwb_ie_hdr hdr; | ||
554 | u8 bp_length; | ||
555 | u8 data[]; | ||
556 | } __attribute__((packed)); | ||
557 | |||
558 | int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt) | ||
559 | { | ||
560 | int result = -EINVAL; | ||
561 | struct device *dev = &evt->rc->uwb_dev.dev; | ||
562 | struct uwb_rc_evt_bpoie_change *bpoiec; | ||
563 | struct uwb_ie_bpo *bpoie; | ||
564 | static unsigned count; /* FIXME: this is a temp hack */ | ||
565 | size_t iesize; | ||
566 | |||
567 | /* Is there enough data to decode it? */ | ||
568 | if (evt->notif.size < sizeof(*bpoiec)) { | ||
569 | dev_err(dev, "BPOIEC notification: Not enough data to " | ||
570 | "decode (%zu vs %zu bytes needed)\n", | ||
571 | evt->notif.size, sizeof(*bpoiec)); | ||
572 | goto error; | ||
573 | } | ||
574 | bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb); | ||
575 | iesize = le16_to_cpu(bpoiec->wBPOIELength); | ||
576 | if (iesize < sizeof(*bpoie)) { | ||
577 | dev_err(dev, "BPOIEC notification: Not enough IE data to " | ||
578 | "decode (%zu vs %zu bytes needed)\n", | ||
579 | iesize, sizeof(*bpoie)); | ||
580 | goto error; | ||
581 | } | ||
582 | if (++count % 1000 == 0) /* Lame placeholder */ | ||
583 | dev_info(dev, "BPOIE: %u changes received\n", count); | ||
584 | /* | ||
585 | * FIXME: At this point we should go over all the IEs in the | ||
586 | * bpoiec->BPOIE array and act on each. | ||
587 | */ | ||
588 | result = 0; | ||
589 | error: | ||
590 | return result; | ||
591 | } | ||
592 | |||
593 | /** | ||
594 | * uwb_bg_joined - is the RC in a beacon group? | ||
595 | * @rc: the radio controller | ||
596 | * | ||
597 | * Returns true if the radio controller is in a beacon group (even if | ||
598 | * it's the sole member). | ||
599 | */ | ||
600 | int uwb_bg_joined(struct uwb_rc *rc) | ||
601 | { | ||
602 | return rc->beaconing != -1; | ||
603 | } | ||
604 | EXPORT_SYMBOL_GPL(uwb_bg_joined); | ||
605 | |||
606 | /* | ||
607 | * Print beaconing state. | ||
608 | */ | ||
609 | static ssize_t uwb_rc_beacon_show(struct device *dev, | ||
610 | struct device_attribute *attr, char *buf) | ||
611 | { | ||
612 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
613 | struct uwb_rc *rc = uwb_dev->rc; | ||
614 | ssize_t result; | ||
615 | |||
616 | mutex_lock(&rc->uwb_dev.mutex); | ||
617 | result = sprintf(buf, "%d\n", rc->beaconing); | ||
618 | mutex_unlock(&rc->uwb_dev.mutex); | ||
619 | return result; | ||
620 | } | ||
621 | |||
622 | /* | ||
623 | * Start beaconing on the specified channel, or stop beaconing. | ||
624 | * | ||
625 | * The BPST offset of when to start searching for a beacon group to | ||
626 | * join may be specified. | ||
627 | */ | ||
628 | static ssize_t uwb_rc_beacon_store(struct device *dev, | ||
629 | struct device_attribute *attr, | ||
630 | const char *buf, size_t size) | ||
631 | { | ||
632 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
633 | struct uwb_rc *rc = uwb_dev->rc; | ||
634 | int channel; | ||
635 | unsigned bpst_offset = 0; | ||
636 | ssize_t result = -EINVAL; | ||
637 | |||
638 | result = sscanf(buf, "%d %u\n", &channel, &bpst_offset); | ||
639 | if (result >= 1) | ||
640 | result = uwb_rc_beacon(rc, channel, bpst_offset); | ||
641 | |||
642 | return result < 0 ? result : size; | ||
643 | } | ||
644 | DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store); | ||
diff --git a/drivers/uwb/ie.c b/drivers/uwb/ie.c new file mode 100644 index 000000000000..d54fe09a986d --- /dev/null +++ b/drivers/uwb/ie.c | |||
@@ -0,0 +1,570 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Information Element Handling | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * Reinette Chatre <reinette.chatre@intel.com> | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU General Public License version | ||
11 | * 2 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 | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
21 | * 02110-1301, USA. | ||
22 | * | ||
23 | * | ||
24 | * FIXME: docs | ||
25 | */ | ||
26 | |||
27 | #include "uwb-internal.h" | ||
28 | #define D_LOCAL 0 | ||
29 | #include <linux/uwb/debug.h> | ||
30 | |||
31 | /** | ||
32 | * uwb_ie_next - get the next IE in a buffer | ||
33 | * @ptr: start of the buffer containing the IE data | ||
34 | * @len: length of the buffer | ||
35 | * | ||
36 | * Both @ptr and @len are updated so subsequent calls to uwb_ie_next() | ||
37 | * will get the next IE. | ||
38 | * | ||
39 | * NULL is returned (and @ptr and @len will not be updated) if there | ||
40 | * are no more IEs in the buffer or the buffer is too short. | ||
41 | */ | ||
42 | struct uwb_ie_hdr *uwb_ie_next(void **ptr, size_t *len) | ||
43 | { | ||
44 | struct uwb_ie_hdr *hdr; | ||
45 | size_t ie_len; | ||
46 | |||
47 | if (*len < sizeof(struct uwb_ie_hdr)) | ||
48 | return NULL; | ||
49 | |||
50 | hdr = *ptr; | ||
51 | ie_len = sizeof(struct uwb_ie_hdr) + hdr->length; | ||
52 | |||
53 | if (*len < ie_len) | ||
54 | return NULL; | ||
55 | |||
56 | *ptr += ie_len; | ||
57 | *len -= ie_len; | ||
58 | |||
59 | return hdr; | ||
60 | } | ||
61 | EXPORT_SYMBOL_GPL(uwb_ie_next); | ||
62 | |||
63 | /** | ||
64 | * Get the IEs that a radio controller is sending in its beacon | ||
65 | * | ||
66 | * @uwb_rc: UWB Radio Controller | ||
67 | * @returns: Size read from the system | ||
68 | * | ||
69 | * We don't need to lock the uwb_rc's mutex because we don't modify | ||
70 | * anything. Once done with the iedata buffer, call | ||
71 | * uwb_rc_ie_release(iedata). Don't call kfree on it. | ||
72 | */ | ||
73 | ssize_t uwb_rc_get_ie(struct uwb_rc *uwb_rc, struct uwb_rc_evt_get_ie **pget_ie) | ||
74 | { | ||
75 | ssize_t result; | ||
76 | struct device *dev = &uwb_rc->uwb_dev.dev; | ||
77 | struct uwb_rccb *cmd = NULL; | ||
78 | struct uwb_rceb *reply = NULL; | ||
79 | struct uwb_rc_evt_get_ie *get_ie; | ||
80 | |||
81 | d_fnstart(3, dev, "(%p, %p)\n", uwb_rc, pget_ie); | ||
82 | result = -ENOMEM; | ||
83 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
84 | if (cmd == NULL) | ||
85 | goto error_kzalloc; | ||
86 | cmd->bCommandType = UWB_RC_CET_GENERAL; | ||
87 | cmd->wCommand = cpu_to_le16(UWB_RC_CMD_GET_IE); | ||
88 | result = uwb_rc_vcmd(uwb_rc, "GET_IE", cmd, sizeof(*cmd), | ||
89 | UWB_RC_CET_GENERAL, UWB_RC_CMD_GET_IE, | ||
90 | &reply); | ||
91 | if (result < 0) | ||
92 | goto error_cmd; | ||
93 | get_ie = container_of(reply, struct uwb_rc_evt_get_ie, rceb); | ||
94 | if (result < sizeof(*get_ie)) { | ||
95 | dev_err(dev, "not enough data returned for decoding GET IE " | ||
96 | "(%zu bytes received vs %zu needed)\n", | ||
97 | result, sizeof(*get_ie)); | ||
98 | result = -EINVAL; | ||
99 | } else if (result < sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength)) { | ||
100 | dev_err(dev, "not enough data returned for decoding GET IE " | ||
101 | "payload (%zu bytes received vs %zu needed)\n", result, | ||
102 | sizeof(*get_ie) + le16_to_cpu(get_ie->wIELength)); | ||
103 | result = -EINVAL; | ||
104 | } else | ||
105 | *pget_ie = get_ie; | ||
106 | error_cmd: | ||
107 | kfree(cmd); | ||
108 | error_kzalloc: | ||
109 | d_fnend(3, dev, "(%p, %p) = %d\n", uwb_rc, pget_ie, (int)result); | ||
110 | return result; | ||
111 | } | ||
112 | EXPORT_SYMBOL_GPL(uwb_rc_get_ie); | ||
113 | |||
114 | |||
115 | /* | ||
116 | * Given a pointer to an IE, print it in ASCII/hex followed by a new line | ||
117 | * | ||
118 | * @ie_hdr: pointer to the IE header. Length is in there, and it is | ||
119 | * guaranteed that the ie_hdr->length bytes following it are | ||
120 | * safely accesible. | ||
121 | * | ||
122 | * @_data: context data passed from uwb_ie_for_each(), an struct output_ctx | ||
123 | */ | ||
124 | int uwb_ie_dump_hex(struct uwb_dev *uwb_dev, const struct uwb_ie_hdr *ie_hdr, | ||
125 | size_t offset, void *_ctx) | ||
126 | { | ||
127 | struct uwb_buf_ctx *ctx = _ctx; | ||
128 | const u8 *pl = (void *)(ie_hdr + 1); | ||
129 | u8 pl_itr; | ||
130 | |||
131 | ctx->bytes += scnprintf(ctx->buf + ctx->bytes, ctx->size - ctx->bytes, | ||
132 | "%02x %02x ", (unsigned) ie_hdr->element_id, | ||
133 | (unsigned) ie_hdr->length); | ||
134 | pl_itr = 0; | ||
135 | while (pl_itr < ie_hdr->length && ctx->bytes < ctx->size) | ||
136 | ctx->bytes += scnprintf(ctx->buf + ctx->bytes, | ||
137 | ctx->size - ctx->bytes, | ||
138 | "%02x ", (unsigned) pl[pl_itr++]); | ||
139 | if (ctx->bytes < ctx->size) | ||
140 | ctx->buf[ctx->bytes++] = '\n'; | ||
141 | return 0; | ||
142 | } | ||
143 | EXPORT_SYMBOL_GPL(uwb_ie_dump_hex); | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Verify that a pointer in a buffer points to valid IE | ||
148 | * | ||
149 | * @start: pointer to start of buffer in which IE appears | ||
150 | * @itr: pointer to IE inside buffer that will be verified | ||
151 | * @top: pointer to end of buffer | ||
152 | * | ||
153 | * @returns: 0 if IE is valid, <0 otherwise | ||
154 | * | ||
155 | * Verification involves checking that the buffer can contain a | ||
156 | * header and the amount of data reported in the IE header can be found in | ||
157 | * the buffer. | ||
158 | */ | ||
159 | static | ||
160 | int uwb_rc_ie_verify(struct uwb_dev *uwb_dev, const void *start, | ||
161 | const void *itr, const void *top) | ||
162 | { | ||
163 | struct device *dev = &uwb_dev->dev; | ||
164 | const struct uwb_ie_hdr *ie_hdr; | ||
165 | |||
166 | if (top - itr < sizeof(*ie_hdr)) { | ||
167 | dev_err(dev, "Bad IE: no data to decode header " | ||
168 | "(%zu bytes left vs %zu needed) at offset %zu\n", | ||
169 | top - itr, sizeof(*ie_hdr), itr - start); | ||
170 | return -EINVAL; | ||
171 | } | ||
172 | ie_hdr = itr; | ||
173 | itr += sizeof(*ie_hdr); | ||
174 | if (top - itr < ie_hdr->length) { | ||
175 | dev_err(dev, "Bad IE: not enough data for payload " | ||
176 | "(%zu bytes left vs %zu needed) at offset %zu\n", | ||
177 | top - itr, (size_t)ie_hdr->length, | ||
178 | (void *)ie_hdr - start); | ||
179 | return -EINVAL; | ||
180 | } | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | |||
185 | /** | ||
186 | * Walk a buffer filled with consecutive IE's a buffer | ||
187 | * | ||
188 | * @uwb_dev: UWB device this IEs belong to (for err messages mainly) | ||
189 | * | ||
190 | * @fn: function to call with each IE; if it returns 0, we keep | ||
191 | * traversing the buffer. If it returns !0, we'll stop and return | ||
192 | * that value. | ||
193 | * | ||
194 | * @data: pointer passed to @fn | ||
195 | * | ||
196 | * @buf: buffer where the consecutive IEs are located | ||
197 | * | ||
198 | * @size: size of @buf | ||
199 | * | ||
200 | * Each IE is checked for basic correctness (there is space left for | ||
201 | * the header and the payload). If that test is failed, we stop | ||
202 | * processing. For every good IE, @fn is called. | ||
203 | */ | ||
204 | ssize_t uwb_ie_for_each(struct uwb_dev *uwb_dev, uwb_ie_f fn, void *data, | ||
205 | const void *buf, size_t size) | ||
206 | { | ||
207 | ssize_t result = 0; | ||
208 | const struct uwb_ie_hdr *ie_hdr; | ||
209 | const void *itr = buf, *top = itr + size; | ||
210 | |||
211 | while (itr < top) { | ||
212 | if (uwb_rc_ie_verify(uwb_dev, buf, itr, top) != 0) | ||
213 | break; | ||
214 | ie_hdr = itr; | ||
215 | itr += sizeof(*ie_hdr) + ie_hdr->length; | ||
216 | result = fn(uwb_dev, ie_hdr, itr - buf, data); | ||
217 | if (result != 0) | ||
218 | break; | ||
219 | } | ||
220 | return result; | ||
221 | } | ||
222 | EXPORT_SYMBOL_GPL(uwb_ie_for_each); | ||
223 | |||
224 | |||
225 | /** | ||
226 | * Replace all IEs currently being transmitted by a device | ||
227 | * | ||
228 | * @cmd: pointer to the SET-IE command with the IEs to set | ||
229 | * @size: size of @buf | ||
230 | */ | ||
231 | int uwb_rc_set_ie(struct uwb_rc *rc, struct uwb_rc_cmd_set_ie *cmd) | ||
232 | { | ||
233 | int result; | ||
234 | struct device *dev = &rc->uwb_dev.dev; | ||
235 | struct uwb_rc_evt_set_ie reply; | ||
236 | |||
237 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; | ||
238 | reply.rceb.wEvent = UWB_RC_CMD_SET_IE; | ||
239 | result = uwb_rc_cmd(rc, "SET-IE", &cmd->rccb, | ||
240 | sizeof(*cmd) + le16_to_cpu(cmd->wIELength), | ||
241 | &reply.rceb, sizeof(reply)); | ||
242 | if (result < 0) | ||
243 | goto error_cmd; | ||
244 | else if (result != sizeof(reply)) { | ||
245 | dev_err(dev, "SET-IE: not enough data to decode reply " | ||
246 | "(%d bytes received vs %zu needed)\n", | ||
247 | result, sizeof(reply)); | ||
248 | result = -EIO; | ||
249 | } else if (reply.bResultCode != UWB_RC_RES_SUCCESS) { | ||
250 | dev_err(dev, "SET-IE: command execution failed: %s (%d)\n", | ||
251 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); | ||
252 | result = -EIO; | ||
253 | } else | ||
254 | result = 0; | ||
255 | error_cmd: | ||
256 | return result; | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * Determine by IE id if IE is host settable | ||
261 | * WUSB 1.0 [8.6.2.8 Table 8.85] | ||
262 | * | ||
263 | * EXCEPTION: | ||
264 | * All but UWB_IE_WLP appears in Table 8.85 from WUSB 1.0. Setting this IE | ||
265 | * is required for the WLP substack to perform association with its WSS so | ||
266 | * we hope that the WUSB spec will be changed to reflect this. | ||
267 | */ | ||
268 | static | ||
269 | int uwb_rc_ie_is_host_settable(enum uwb_ie element_id) | ||
270 | { | ||
271 | if (element_id == UWB_PCA_AVAILABILITY || | ||
272 | element_id == UWB_BP_SWITCH_IE || | ||
273 | element_id == UWB_MAC_CAPABILITIES_IE || | ||
274 | element_id == UWB_PHY_CAPABILITIES_IE || | ||
275 | element_id == UWB_APP_SPEC_PROBE_IE || | ||
276 | element_id == UWB_IDENTIFICATION_IE || | ||
277 | element_id == UWB_MASTER_KEY_ID_IE || | ||
278 | element_id == UWB_IE_WLP || | ||
279 | element_id == UWB_APP_SPEC_IE) | ||
280 | return 1; | ||
281 | return 0; | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Extract Host Settable IEs from IE | ||
287 | * | ||
288 | * @ie_data: pointer to buffer containing all IEs | ||
289 | * @size: size of buffer | ||
290 | * | ||
291 | * @returns: length of buffer that only includes host settable IEs | ||
292 | * | ||
293 | * Given a buffer of IEs we move all Host Settable IEs to front of buffer | ||
294 | * by overwriting the IEs that are not Host Settable. | ||
295 | * Buffer length is adjusted accordingly. | ||
296 | */ | ||
297 | static | ||
298 | ssize_t uwb_rc_parse_host_settable_ie(struct uwb_dev *uwb_dev, | ||
299 | void *ie_data, size_t size) | ||
300 | { | ||
301 | size_t new_len = size; | ||
302 | struct uwb_ie_hdr *ie_hdr; | ||
303 | size_t ie_length; | ||
304 | void *itr = ie_data, *top = itr + size; | ||
305 | |||
306 | while (itr < top) { | ||
307 | if (uwb_rc_ie_verify(uwb_dev, ie_data, itr, top) != 0) | ||
308 | break; | ||
309 | ie_hdr = itr; | ||
310 | ie_length = sizeof(*ie_hdr) + ie_hdr->length; | ||
311 | if (uwb_rc_ie_is_host_settable(ie_hdr->element_id)) { | ||
312 | itr += ie_length; | ||
313 | } else { | ||
314 | memmove(itr, itr + ie_length, top - (itr + ie_length)); | ||
315 | new_len -= ie_length; | ||
316 | top -= ie_length; | ||
317 | } | ||
318 | } | ||
319 | return new_len; | ||
320 | } | ||
321 | |||
322 | |||
323 | /* Cleanup the whole IE management subsystem */ | ||
324 | void uwb_rc_ie_init(struct uwb_rc *uwb_rc) | ||
325 | { | ||
326 | mutex_init(&uwb_rc->ies_mutex); | ||
327 | } | ||
328 | |||
329 | |||
330 | /** | ||
331 | * Set up cache for host settable IEs currently being transmitted | ||
332 | * | ||
333 | * First we just call GET-IE to get the current IEs being transmitted | ||
334 | * (or we workaround and pretend we did) and (because the format is | ||
335 | * the same) reuse that as the IE cache (with the command prefix, as | ||
336 | * explained in 'struct uwb_rc'). | ||
337 | * | ||
338 | * @returns: size of cache created | ||
339 | */ | ||
340 | ssize_t uwb_rc_ie_setup(struct uwb_rc *uwb_rc) | ||
341 | { | ||
342 | struct device *dev = &uwb_rc->uwb_dev.dev; | ||
343 | ssize_t result; | ||
344 | size_t capacity; | ||
345 | struct uwb_rc_evt_get_ie *ie_info; | ||
346 | |||
347 | d_fnstart(3, dev, "(%p)\n", uwb_rc); | ||
348 | mutex_lock(&uwb_rc->ies_mutex); | ||
349 | result = uwb_rc_get_ie(uwb_rc, &ie_info); | ||
350 | if (result < 0) | ||
351 | goto error_get_ie; | ||
352 | capacity = result; | ||
353 | d_printf(5, dev, "Got IEs %zu bytes (%zu long at %p)\n", result, | ||
354 | (size_t)le16_to_cpu(ie_info->wIELength), ie_info); | ||
355 | |||
356 | /* Remove IEs that host should not set. */ | ||
357 | result = uwb_rc_parse_host_settable_ie(&uwb_rc->uwb_dev, | ||
358 | ie_info->IEData, le16_to_cpu(ie_info->wIELength)); | ||
359 | if (result < 0) | ||
360 | goto error_parse; | ||
361 | d_printf(5, dev, "purged non-settable IEs to %zu bytes\n", result); | ||
362 | uwb_rc->ies = (void *) ie_info; | ||
363 | uwb_rc->ies->rccb.bCommandType = UWB_RC_CET_GENERAL; | ||
364 | uwb_rc->ies->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_IE); | ||
365 | uwb_rc->ies_capacity = capacity; | ||
366 | d_printf(5, dev, "IE cache at %p %zu bytes, %zu capacity\n", | ||
367 | ie_info, result, capacity); | ||
368 | result = 0; | ||
369 | error_parse: | ||
370 | error_get_ie: | ||
371 | mutex_unlock(&uwb_rc->ies_mutex); | ||
372 | d_fnend(3, dev, "(%p) = %zu\n", uwb_rc, result); | ||
373 | return result; | ||
374 | } | ||
375 | |||
376 | |||
377 | /* Cleanup the whole IE management subsystem */ | ||
378 | void uwb_rc_ie_release(struct uwb_rc *uwb_rc) | ||
379 | { | ||
380 | kfree(uwb_rc->ies); | ||
381 | uwb_rc->ies = NULL; | ||
382 | uwb_rc->ies_capacity = 0; | ||
383 | } | ||
384 | |||
385 | |||
386 | static | ||
387 | int __acc_size(struct uwb_dev *uwb_dev, const struct uwb_ie_hdr *ie_hdr, | ||
388 | size_t offset, void *_ctx) | ||
389 | { | ||
390 | size_t *acc_size = _ctx; | ||
391 | *acc_size += sizeof(*ie_hdr) + ie_hdr->length; | ||
392 | d_printf(6, &uwb_dev->dev, "new acc size %zu\n", *acc_size); | ||
393 | return 0; | ||
394 | } | ||
395 | |||
396 | |||
397 | /** | ||
398 | * Add a new IE to IEs currently being transmitted by device | ||
399 | * | ||
400 | * @ies: the buffer containing the new IE or IEs to be added to | ||
401 | * the device's beacon. The buffer will be verified for | ||
402 | * consistence (meaning the headers should be right) and | ||
403 | * consistent with the buffer size. | ||
404 | * @size: size of @ies (in bytes, total buffer size) | ||
405 | * @returns: 0 if ok, <0 errno code on error | ||
406 | * | ||
407 | * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB | ||
408 | * after the device sent the first beacon that includes the IEs specified | ||
409 | * in the SET IE command. We thus cannot send this command if the device is | ||
410 | * not beaconing. Instead, a SET IE command will be sent later right after | ||
411 | * we start beaconing. | ||
412 | * | ||
413 | * Setting an IE on the device will overwrite all current IEs in device. So | ||
414 | * we take the current IEs being transmitted by the device, append the | ||
415 | * new one, and call SET IE with all the IEs needed. | ||
416 | * | ||
417 | * The local IE cache will only be updated with the new IE if SET IE | ||
418 | * completed successfully. | ||
419 | */ | ||
420 | int uwb_rc_ie_add(struct uwb_rc *uwb_rc, | ||
421 | const struct uwb_ie_hdr *ies, size_t size) | ||
422 | { | ||
423 | int result = 0; | ||
424 | struct device *dev = &uwb_rc->uwb_dev.dev; | ||
425 | struct uwb_rc_cmd_set_ie *new_ies; | ||
426 | size_t ies_size, total_size, acc_size = 0; | ||
427 | |||
428 | if (uwb_rc->ies == NULL) | ||
429 | return -ESHUTDOWN; | ||
430 | uwb_ie_for_each(&uwb_rc->uwb_dev, __acc_size, &acc_size, ies, size); | ||
431 | if (acc_size != size) { | ||
432 | dev_err(dev, "BUG: bad IEs, misconstructed headers " | ||
433 | "[%zu bytes reported vs %zu calculated]\n", | ||
434 | size, acc_size); | ||
435 | WARN_ON(1); | ||
436 | return -EINVAL; | ||
437 | } | ||
438 | mutex_lock(&uwb_rc->ies_mutex); | ||
439 | ies_size = le16_to_cpu(uwb_rc->ies->wIELength); | ||
440 | total_size = sizeof(*uwb_rc->ies) + ies_size; | ||
441 | if (total_size + size > uwb_rc->ies_capacity) { | ||
442 | d_printf(4, dev, "Reallocating IE cache from %p capacity %zu " | ||
443 | "to capacity %zu\n", uwb_rc->ies, uwb_rc->ies_capacity, | ||
444 | total_size + size); | ||
445 | new_ies = kzalloc(total_size + size, GFP_KERNEL); | ||
446 | if (new_ies == NULL) { | ||
447 | dev_err(dev, "No memory for adding new IE\n"); | ||
448 | result = -ENOMEM; | ||
449 | goto error_alloc; | ||
450 | } | ||
451 | memcpy(new_ies, uwb_rc->ies, total_size); | ||
452 | uwb_rc->ies_capacity = total_size + size; | ||
453 | kfree(uwb_rc->ies); | ||
454 | uwb_rc->ies = new_ies; | ||
455 | d_printf(4, dev, "New IE cache at %p capacity %zu\n", | ||
456 | uwb_rc->ies, uwb_rc->ies_capacity); | ||
457 | } | ||
458 | memcpy((void *)uwb_rc->ies + total_size, ies, size); | ||
459 | uwb_rc->ies->wIELength = cpu_to_le16(ies_size + size); | ||
460 | if (uwb_rc->beaconing != -1) { | ||
461 | result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies); | ||
462 | if (result < 0) { | ||
463 | dev_err(dev, "Cannot set new IE on device: %d\n", | ||
464 | result); | ||
465 | uwb_rc->ies->wIELength = cpu_to_le16(ies_size); | ||
466 | } else | ||
467 | result = 0; | ||
468 | } | ||
469 | d_printf(4, dev, "IEs now occupy %hu bytes of %zu capacity at %p\n", | ||
470 | le16_to_cpu(uwb_rc->ies->wIELength), uwb_rc->ies_capacity, | ||
471 | uwb_rc->ies); | ||
472 | error_alloc: | ||
473 | mutex_unlock(&uwb_rc->ies_mutex); | ||
474 | return result; | ||
475 | } | ||
476 | EXPORT_SYMBOL_GPL(uwb_rc_ie_add); | ||
477 | |||
478 | |||
479 | /* | ||
480 | * Remove an IE from internal cache | ||
481 | * | ||
482 | * We are dealing with our internal IE cache so no need to verify that the | ||
483 | * IEs are valid (it has been done already). | ||
484 | * | ||
485 | * Should be called with ies_mutex held | ||
486 | * | ||
487 | * We do not break out once an IE is found in the cache. It is currently | ||
488 | * possible to have more than one IE with the same ID included in the | ||
489 | * beacon. We don't reallocate, we just mark the size smaller. | ||
490 | */ | ||
491 | static | ||
492 | int uwb_rc_ie_cache_rm(struct uwb_rc *uwb_rc, enum uwb_ie to_remove) | ||
493 | { | ||
494 | struct uwb_ie_hdr *ie_hdr; | ||
495 | size_t new_len = le16_to_cpu(uwb_rc->ies->wIELength); | ||
496 | void *itr = uwb_rc->ies->IEData; | ||
497 | void *top = itr + new_len; | ||
498 | |||
499 | while (itr < top) { | ||
500 | ie_hdr = itr; | ||
501 | if (ie_hdr->element_id != to_remove) { | ||
502 | itr += sizeof(*ie_hdr) + ie_hdr->length; | ||
503 | } else { | ||
504 | int ie_length; | ||
505 | ie_length = sizeof(*ie_hdr) + ie_hdr->length; | ||
506 | if (top - itr != ie_length) | ||
507 | memmove(itr, itr + ie_length, top - itr + ie_length); | ||
508 | top -= ie_length; | ||
509 | new_len -= ie_length; | ||
510 | } | ||
511 | } | ||
512 | uwb_rc->ies->wIELength = cpu_to_le16(new_len); | ||
513 | return 0; | ||
514 | } | ||
515 | |||
516 | |||
517 | /** | ||
518 | * Remove an IE currently being transmitted by device | ||
519 | * | ||
520 | * @element_id: id of IE to be removed from device's beacon | ||
521 | */ | ||
522 | int uwb_rc_ie_rm(struct uwb_rc *uwb_rc, enum uwb_ie element_id) | ||
523 | { | ||
524 | struct device *dev = &uwb_rc->uwb_dev.dev; | ||
525 | int result; | ||
526 | |||
527 | if (uwb_rc->ies == NULL) | ||
528 | return -ESHUTDOWN; | ||
529 | mutex_lock(&uwb_rc->ies_mutex); | ||
530 | result = uwb_rc_ie_cache_rm(uwb_rc, element_id); | ||
531 | if (result < 0) | ||
532 | dev_err(dev, "Cannot remove IE from cache.\n"); | ||
533 | if (uwb_rc->beaconing != -1) { | ||
534 | result = uwb_rc_set_ie(uwb_rc, uwb_rc->ies); | ||
535 | if (result < 0) | ||
536 | dev_err(dev, "Cannot set new IE on device.\n"); | ||
537 | } | ||
538 | mutex_unlock(&uwb_rc->ies_mutex); | ||
539 | return result; | ||
540 | } | ||
541 | EXPORT_SYMBOL_GPL(uwb_rc_ie_rm); | ||
542 | |||
543 | |||
544 | /** | ||
545 | * Create and set new Identification IE | ||
546 | * | ||
547 | * Currently only sets the Vendor ID. The Vendor ID is set from the OUI, | ||
548 | * which is obtained from the first three bytes from the MAC address. | ||
549 | */ | ||
550 | int uwb_rc_set_identification_ie(struct uwb_rc *uwb_rc) | ||
551 | { | ||
552 | struct { | ||
553 | struct uwb_identification_ie id_ie; | ||
554 | struct uwb_dev_info dev_info; | ||
555 | struct uwb_vendor_id vendor_id; | ||
556 | } ie_data; | ||
557 | |||
558 | ie_data.id_ie.hdr.element_id = UWB_IDENTIFICATION_IE; | ||
559 | ie_data.id_ie.hdr.length = sizeof(struct uwb_dev_info) + | ||
560 | sizeof(struct uwb_vendor_id); | ||
561 | |||
562 | ie_data.dev_info.type = UWB_DEV_INFO_VENDOR_ID; | ||
563 | ie_data.dev_info.length = sizeof(struct uwb_vendor_id); | ||
564 | |||
565 | ie_data.vendor_id.data[0] = uwb_rc->uwb_dev.mac_addr.data[0]; | ||
566 | ie_data.vendor_id.data[1] = uwb_rc->uwb_dev.mac_addr.data[1]; | ||
567 | ie_data.vendor_id.data[2] = uwb_rc->uwb_dev.mac_addr.data[2]; | ||
568 | |||
569 | return uwb_rc_ie_add(uwb_rc, &ie_data.id_ie.hdr, sizeof(ie_data)); | ||
570 | } | ||
diff --git a/drivers/uwb/scan.c b/drivers/uwb/scan.c new file mode 100644 index 000000000000..2d270748f32b --- /dev/null +++ b/drivers/uwb/scan.c | |||
@@ -0,0 +1,133 @@ | |||
1 | /* | ||
2 | * Ultra Wide Band | ||
3 | * Scanning management | ||
4 | * | ||
5 | * Copyright (C) 2005-2006 Intel Corporation | ||
6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU General Public License version | ||
10 | * 2 as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
20 | * 02110-1301, USA. | ||
21 | * | ||
22 | * | ||
23 | * | ||
24 | * FIXME: docs | ||
25 | * FIXME: there are issues here on how BEACON and SCAN on USB RCI deal | ||
26 | * with each other. Currently seems that START_BEACON while | ||
27 | * SCAN_ONLY will cancel the scan, so we need to update the | ||
28 | * state here. Clarification request sent by email on | ||
29 | * 10/05/2005. | ||
30 | * 10/28/2005 No clear answer heard--maybe we'll hack the API | ||
31 | * so that when we start beaconing, if the HC is | ||
32 | * scanning in a mode not compatible with beaconing | ||
33 | * we just fail. | ||
34 | */ | ||
35 | |||
36 | #include <linux/device.h> | ||
37 | #include <linux/err.h> | ||
38 | #include "uwb-internal.h" | ||
39 | |||
40 | |||
41 | /** | ||
42 | * Start/stop scanning in a radio controller | ||
43 | * | ||
44 | * @rc: UWB Radio Controlller | ||
45 | * @channel: Channel to scan; encodings in WUSB1.0[Table 5.12] | ||
46 | * @type: Type of scanning to do. | ||
47 | * @bpst_offset: value at which to start scanning (if type == | ||
48 | * UWB_SCAN_ONLY_STARTTIME) | ||
49 | * @returns: 0 if ok, < 0 errno code on error | ||
50 | * | ||
51 | * We put the command on kmalloc'ed memory as some arches cannot do | ||
52 | * USB from the stack. The reply event is copied from an stage buffer, | ||
53 | * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details. | ||
54 | */ | ||
55 | int uwb_rc_scan(struct uwb_rc *rc, | ||
56 | unsigned channel, enum uwb_scan_type type, | ||
57 | unsigned bpst_offset) | ||
58 | { | ||
59 | int result; | ||
60 | struct uwb_rc_cmd_scan *cmd; | ||
61 | struct uwb_rc_evt_confirm reply; | ||
62 | |||
63 | result = -ENOMEM; | ||
64 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); | ||
65 | if (cmd == NULL) | ||
66 | goto error_kzalloc; | ||
67 | mutex_lock(&rc->uwb_dev.mutex); | ||
68 | cmd->rccb.bCommandType = UWB_RC_CET_GENERAL; | ||
69 | cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SCAN); | ||
70 | cmd->bChannelNumber = channel; | ||
71 | cmd->bScanState = type; | ||
72 | cmd->wStartTime = cpu_to_le16(bpst_offset); | ||
73 | reply.rceb.bEventType = UWB_RC_CET_GENERAL; | ||
74 | reply.rceb.wEvent = UWB_RC_CMD_SCAN; | ||
75 | result = uwb_rc_cmd(rc, "SCAN", &cmd->rccb, sizeof(*cmd), | ||
76 | &reply.rceb, sizeof(reply)); | ||
77 | if (result < 0) | ||
78 | goto error_cmd; | ||
79 | if (reply.bResultCode != UWB_RC_RES_SUCCESS) { | ||
80 | dev_err(&rc->uwb_dev.dev, | ||
81 | "SCAN: command execution failed: %s (%d)\n", | ||
82 | uwb_rc_strerror(reply.bResultCode), reply.bResultCode); | ||
83 | result = -EIO; | ||
84 | goto error_cmd; | ||
85 | } | ||
86 | rc->scanning = channel; | ||
87 | rc->scan_type = type; | ||
88 | error_cmd: | ||
89 | mutex_unlock(&rc->uwb_dev.mutex); | ||
90 | kfree(cmd); | ||
91 | error_kzalloc: | ||
92 | return result; | ||
93 | } | ||
94 | |||
95 | /* | ||
96 | * Print scanning state | ||
97 | */ | ||
98 | static ssize_t uwb_rc_scan_show(struct device *dev, | ||
99 | struct device_attribute *attr, char *buf) | ||
100 | { | ||
101 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
102 | struct uwb_rc *rc = uwb_dev->rc; | ||
103 | ssize_t result; | ||
104 | |||
105 | mutex_lock(&rc->uwb_dev.mutex); | ||
106 | result = sprintf(buf, "%d %d\n", rc->scanning, rc->scan_type); | ||
107 | mutex_unlock(&rc->uwb_dev.mutex); | ||
108 | return result; | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * | ||
113 | */ | ||
114 | static ssize_t uwb_rc_scan_store(struct device *dev, | ||
115 | struct device_attribute *attr, | ||
116 | const char *buf, size_t size) | ||
117 | { | ||
118 | struct uwb_dev *uwb_dev = to_uwb_dev(dev); | ||
119 | struct uwb_rc *rc = uwb_dev->rc; | ||
120 | unsigned channel; | ||
121 | unsigned type; | ||
122 | unsigned bpst_offset = 0; | ||
123 | ssize_t result = -EINVAL; | ||
124 | |||
125 | result = sscanf(buf, "%u %u %u\n", &channel, &type, &bpst_offset); | ||
126 | if (result >= 2 && type < UWB_SCAN_TOP) | ||
127 | result = uwb_rc_scan(rc, channel, type, bpst_offset); | ||
128 | |||
129 | return result < 0 ? result : size; | ||
130 | } | ||
131 | |||
132 | /** Radio Control sysfs interface (declaration) */ | ||
133 | DEVICE_ATTR(scan, S_IRUGO | S_IWUSR, uwb_rc_scan_show, uwb_rc_scan_store); | ||