1 // SPDX-License-Identifier: GPL-2.0-only
3 * Ultra Wide Band Radio Control
4 * Event Size Tables management
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
11 * Infrastructure, code and data tables for guessing the size of
12 * events received on the notification endpoints of UWB radio
15 * You define a table of events and for each, its size and how to get
20 * uwb_est_{init/destroy}(): To initialize/release the EST subsystem.
22 * uwb_est_[u]register(): To un/register event size tables
25 * uwb_est_find_size(): Get the size of an event
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
30 #include <linux/export.h>
32 #include "uwb-internal.h"
38 const struct uwb_est_entry
*entry
;
41 static struct uwb_est
*uwb_est
;
42 static u8 uwb_est_size
;
43 static u8 uwb_est_used
;
44 static DEFINE_RWLOCK(uwb_est_lock
);
47 * WUSB Standard Event Size Table, HWA-RC interface
49 * Sizes for events and notifications type 0 (general), high nibble 0.
52 struct uwb_est_entry uwb_est_00_00xx
[] = {
53 [UWB_RC_EVT_IE_RCV
] = {
54 .size
= sizeof(struct uwb_rc_evt_ie_rcv
),
55 .offset
= 1 + offsetof(struct uwb_rc_evt_ie_rcv
, wIELength
),
57 [UWB_RC_EVT_BEACON
] = {
58 .size
= sizeof(struct uwb_rc_evt_beacon
),
59 .offset
= 1 + offsetof(struct uwb_rc_evt_beacon
, wBeaconInfoLength
),
61 [UWB_RC_EVT_BEACON_SIZE
] = {
62 .size
= sizeof(struct uwb_rc_evt_beacon_size
),
64 [UWB_RC_EVT_BPOIE_CHANGE
] = {
65 .size
= sizeof(struct uwb_rc_evt_bpoie_change
),
66 .offset
= 1 + offsetof(struct uwb_rc_evt_bpoie_change
,
69 [UWB_RC_EVT_BP_SLOT_CHANGE
] = {
70 .size
= sizeof(struct uwb_rc_evt_bp_slot_change
),
72 [UWB_RC_EVT_BP_SWITCH_IE_RCV
] = {
73 .size
= sizeof(struct uwb_rc_evt_bp_switch_ie_rcv
),
74 .offset
= 1 + offsetof(struct uwb_rc_evt_bp_switch_ie_rcv
, wIELength
),
76 [UWB_RC_EVT_DEV_ADDR_CONFLICT
] = {
77 .size
= sizeof(struct uwb_rc_evt_dev_addr_conflict
),
79 [UWB_RC_EVT_DRP_AVAIL
] = {
80 .size
= sizeof(struct uwb_rc_evt_drp_avail
)
83 .size
= sizeof(struct uwb_rc_evt_drp
),
84 .offset
= 1 + offsetof(struct uwb_rc_evt_drp
, ie_length
),
86 [UWB_RC_EVT_BP_SWITCH_STATUS
] = {
87 .size
= sizeof(struct uwb_rc_evt_bp_switch_status
),
89 [UWB_RC_EVT_CMD_FRAME_RCV
] = {
90 .size
= sizeof(struct uwb_rc_evt_cmd_frame_rcv
),
91 .offset
= 1 + offsetof(struct uwb_rc_evt_cmd_frame_rcv
, dataLength
),
93 [UWB_RC_EVT_CHANNEL_CHANGE_IE_RCV
] = {
94 .size
= sizeof(struct uwb_rc_evt_channel_change_ie_rcv
),
95 .offset
= 1 + offsetof(struct uwb_rc_evt_channel_change_ie_rcv
, wIELength
),
97 [UWB_RC_CMD_CHANNEL_CHANGE
] = {
98 .size
= sizeof(struct uwb_rc_evt_confirm
),
100 [UWB_RC_CMD_DEV_ADDR_MGMT
] = {
101 .size
= sizeof(struct uwb_rc_evt_dev_addr_mgmt
) },
102 [UWB_RC_CMD_GET_IE
] = {
103 .size
= sizeof(struct uwb_rc_evt_get_ie
),
104 .offset
= 1 + offsetof(struct uwb_rc_evt_get_ie
, wIELength
),
106 [UWB_RC_CMD_RESET
] = {
107 .size
= sizeof(struct uwb_rc_evt_confirm
),
109 [UWB_RC_CMD_SCAN
] = {
110 .size
= sizeof(struct uwb_rc_evt_confirm
),
112 [UWB_RC_CMD_SET_BEACON_FILTER
] = {
113 .size
= sizeof(struct uwb_rc_evt_confirm
),
115 [UWB_RC_CMD_SET_DRP_IE
] = {
116 .size
= sizeof(struct uwb_rc_evt_set_drp_ie
),
118 [UWB_RC_CMD_SET_IE
] = {
119 .size
= sizeof(struct uwb_rc_evt_set_ie
),
121 [UWB_RC_CMD_SET_NOTIFICATION_FILTER
] = {
122 .size
= sizeof(struct uwb_rc_evt_confirm
),
124 [UWB_RC_CMD_SET_TX_POWER
] = {
125 .size
= sizeof(struct uwb_rc_evt_confirm
),
127 [UWB_RC_CMD_SLEEP
] = {
128 .size
= sizeof(struct uwb_rc_evt_confirm
),
130 [UWB_RC_CMD_START_BEACON
] = {
131 .size
= sizeof(struct uwb_rc_evt_confirm
),
133 [UWB_RC_CMD_STOP_BEACON
] = {
134 .size
= sizeof(struct uwb_rc_evt_confirm
),
136 [UWB_RC_CMD_BP_MERGE
] = {
137 .size
= sizeof(struct uwb_rc_evt_confirm
),
139 [UWB_RC_CMD_SEND_COMMAND_FRAME
] = {
140 .size
= sizeof(struct uwb_rc_evt_confirm
),
142 [UWB_RC_CMD_SET_ASIE_NOTIF
] = {
143 .size
= sizeof(struct uwb_rc_evt_confirm
),
148 struct uwb_est_entry uwb_est_01_00xx
[] = {
149 [UWB_RC_DAA_ENERGY_DETECTED
] = {
150 .size
= sizeof(struct uwb_rc_evt_daa_energy_detected
),
152 [UWB_RC_SET_DAA_ENERGY_MASK
] = {
153 .size
= sizeof(struct uwb_rc_evt_set_daa_energy_mask
),
155 [UWB_RC_SET_NOTIFICATION_FILTER_EX
] = {
156 .size
= sizeof(struct uwb_rc_evt_set_notification_filter_ex
),
161 * Initialize the EST subsystem
163 * Register the standard tables also.
167 int uwb_est_create(void)
173 uwb_est
= kcalloc(uwb_est_size
, sizeof(uwb_est
[0]), GFP_KERNEL
);
177 result
= uwb_est_register(UWB_RC_CET_GENERAL
, 0, 0xffff, 0xffff,
178 uwb_est_00_00xx
, ARRAY_SIZE(uwb_est_00_00xx
));
181 result
= uwb_est_register(UWB_RC_CET_EX_TYPE_1
, 0, 0xffff, 0xffff,
182 uwb_est_01_00xx
, ARRAY_SIZE(uwb_est_01_00xx
));
189 void uwb_est_destroy(void)
193 uwb_est_size
= uwb_est_used
= 0;
198 * Double the capacity of the EST table
200 * @returns 0 if ok, < 0 errno no error.
203 int uwb_est_grow(void)
205 size_t actual_size
= uwb_est_size
* sizeof(uwb_est
[0]);
206 void *new = kmalloc_array(2, actual_size
, GFP_ATOMIC
);
209 memcpy(new, uwb_est
, actual_size
);
210 memset(new + actual_size
, 0, actual_size
);
219 * Register an event size table
221 * Makes room for it if the table is full, and then inserts it in the
222 * right position (entries are sorted by type, event_high, vendor and
225 * @vendor: vendor code for matching against the device (0x0000 and
226 * 0xffff mean any); use 0x0000 to force all to match without
227 * checking possible vendor specific ones, 0xfffff to match
228 * after checking vendor specific ones.
230 * @product: product code from that vendor; same matching rules, use
231 * 0x0000 for not allowing vendor specific matches, 0xffff
234 * This arragement just makes the tables sort differenty. Because the
235 * table is sorted by growing type-event_high-vendor-product, a zero
236 * vendor will match before than a 0x456a vendor, that will match
237 * before a 0xfffff vendor.
239 * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
241 /* FIXME: add bus type to vendor/product code */
242 int uwb_est_register(u8 type
, u8 event_high
, u16 vendor
, u16 product
,
243 const struct uwb_est_entry
*entry
, size_t entries
)
249 write_lock_irqsave(&uwb_est_lock
, flags
);
250 if (uwb_est_used
== uwb_est_size
) {
251 result
= uwb_est_grow();
255 /* Find the right spot to insert it in */
256 for (itr
= 0; itr
< uwb_est_used
; itr
++)
257 if (uwb_est
[itr
].type_event_high
< type
258 && uwb_est
[itr
].vendor
< vendor
259 && uwb_est
[itr
].product
< product
)
262 /* Shift others to make room for the new one? */
263 if (itr
< uwb_est_used
)
264 memmove(&uwb_est
[itr
+1], &uwb_est
[itr
], uwb_est_used
- itr
);
265 uwb_est
[itr
].type_event_high
= type
<< 8 | event_high
;
266 uwb_est
[itr
].vendor
= vendor
;
267 uwb_est
[itr
].product
= product
;
268 uwb_est
[itr
].entry
= entry
;
269 uwb_est
[itr
].entries
= entries
;
272 write_unlock_irqrestore(&uwb_est_lock
, flags
);
275 EXPORT_SYMBOL_GPL(uwb_est_register
);
279 * Unregister an event size table
281 * This just removes the specified entry and moves the ones after it
282 * to fill in the gap. This is needed to keep the list sorted; no
283 * reallocation is done to reduce the size of the table.
285 * We unregister by all the data we used to register instead of by
286 * pointer to the @entry array because we might have used the same
287 * table for a bunch of IDs (for example).
289 * @returns 0 if ok, < 0 errno on error (-ENOENT if not found).
291 int uwb_est_unregister(u8 type
, u8 event_high
, u16 vendor
, u16 product
,
292 const struct uwb_est_entry
*entry
, size_t entries
)
296 struct uwb_est est_cmp
= {
297 .type_event_high
= type
<< 8 | event_high
,
303 write_lock_irqsave(&uwb_est_lock
, flags
);
304 for (itr
= 0; itr
< uwb_est_used
; itr
++)
305 if (!memcmp(&uwb_est
[itr
], &est_cmp
, sizeof(est_cmp
)))
307 write_unlock_irqrestore(&uwb_est_lock
, flags
);
311 if (itr
< uwb_est_used
- 1) /* Not last one? move ones above */
312 memmove(&uwb_est
[itr
], &uwb_est
[itr
+1], uwb_est_used
- itr
- 1);
314 write_unlock_irqrestore(&uwb_est_lock
, flags
);
317 EXPORT_SYMBOL_GPL(uwb_est_unregister
);
321 * Get the size of an event from a table
323 * @rceb: pointer to the buffer with the event
324 * @rceb_size: size of the area pointed to by @rceb in bytes.
325 * @returns: > 0 Size of the event
326 * -ENOSPC An area big enough was not provided to look
327 * ahead into the event's guts and guess the size.
328 * -EINVAL Unknown event code (wEvent).
330 * This will look at the received RCEB and guess what is the total
331 * size. For variable sized events, it will look further ahead into
332 * their length field to see how much data should be read.
334 * Note this size is *not* final--the neh (Notification/Event Handle)
335 * might specificy an extra size to add.
338 ssize_t
uwb_est_get_size(struct uwb_rc
*uwb_rc
, struct uwb_est
*est
,
339 u8 event_low
, const struct uwb_rceb
*rceb
,
344 struct device
*dev
= &uwb_rc
->uwb_dev
.dev
;
345 const struct uwb_est_entry
*entry
;
348 if (event_low
>= est
->entries
) { /* in range? */
349 dev_err(dev
, "EST %p 0x%04x/%04x/%04x[%u]: event %u out of range\n",
350 est
, est
->type_event_high
, est
->vendor
, est
->product
,
351 est
->entries
, event_low
);
355 entry
= &est
->entry
[event_low
];
356 if (entry
->size
== 0 && entry
->offset
== 0) { /* unknown? */
357 dev_err(dev
, "EST %p 0x%04x/%04x/%04x[%u]: event %u unknown\n",
358 est
, est
->type_event_high
, est
->vendor
, est
->product
,
359 est
->entries
, event_low
);
362 offset
= entry
->offset
; /* extra fries with that? */
366 /* Ops, got an extra size field at 'offset'--read it */
367 const void *ptr
= rceb
;
368 size_t type_size
= 0;
370 size
= -ENOSPC
; /* enough data for more? */
371 switch (entry
->type
) {
372 case UWB_EST_16
: type_size
= sizeof(__le16
); break;
373 case UWB_EST_8
: type_size
= sizeof(u8
); break;
376 if (offset
+ type_size
> rceb_size
) {
377 dev_err(dev
, "EST %p 0x%04x/%04x/%04x[%u]: "
378 "not enough data to read extra size\n",
379 est
, est
->type_event_high
, est
->vendor
,
380 est
->product
, est
->entries
);
385 switch (entry
->type
) {
386 case UWB_EST_16
: size
+= le16_to_cpu(*(__le16
*)ptr
); break;
387 case UWB_EST_8
: size
+= *(u8
*)ptr
; break;
397 * Guesses the size of a WA event
399 * @rceb: pointer to the buffer with the event
400 * @rceb_size: size of the area pointed to by @rceb in bytes.
401 * @returns: > 0 Size of the event
402 * -ENOSPC An area big enough was not provided to look
403 * ahead into the event's guts and guess the size.
404 * -EINVAL Unknown event code (wEvent).
406 * This will look at the received RCEB and guess what is the total
407 * size by checking all the tables registered with
408 * uwb_est_register(). For variable sized events, it will look further
409 * ahead into their length field to see how much data should be read.
411 * Note this size is *not* final--the neh (Notification/Event Handle)
412 * might specificy an extra size to add or replace.
414 ssize_t
uwb_est_find_size(struct uwb_rc
*rc
, const struct uwb_rceb
*rceb
,
417 /* FIXME: add vendor/product data */
419 struct device
*dev
= &rc
->uwb_dev
.dev
;
422 u16 type_event_high
, event
;
424 read_lock_irqsave(&uwb_est_lock
, flags
);
426 if (rceb_size
< sizeof(*rceb
))
428 event
= le16_to_cpu(rceb
->wEvent
);
429 type_event_high
= rceb
->bEventType
<< 8 | (event
& 0xff00) >> 8;
430 for (itr
= 0; itr
< uwb_est_used
; itr
++) {
431 if (uwb_est
[itr
].type_event_high
!= type_event_high
)
433 size
= uwb_est_get_size(rc
, &uwb_est
[itr
],
434 event
& 0x00ff, rceb
, rceb_size
);
435 /* try more tables that might handle the same type */
440 "event 0x%02x/%04x/%02x: no handlers available; RCEB %4ph\n",
441 (unsigned) rceb
->bEventType
,
442 (unsigned) le16_to_cpu(rceb
->wEvent
),
443 (unsigned) rceb
->bEventContext
,
447 read_unlock_irqrestore(&uwb_est_lock
, flags
);
450 EXPORT_SYMBOL_GPL(uwb_est_find_size
);