1 // SPDX-License-Identifier: GPL-2.0-only
4 * Neighborhood Management Daemon
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This daemon takes care of maintaing information that describes the
10 * UWB neighborhood that the radios in this machine can see. It also
11 * keeps a tab of which devices are visible, makes sure each HC sits
12 * on a different channel to avoid interfering, etc.
14 * Different drivers (radio controller, device, any API in general)
15 * communicate with this daemon through an event queue. Daemon wakes
16 * up, takes a list of events and handles them one by one; handling
17 * function is extracted from a table based on the event's type and
18 * subtype. Events are freed only if the handling function says so.
20 * . Lock protecting the event list has to be an spinlock and locked
21 * with IRQSAVE because it might be called from an interrupt
22 * context (ie: when events arrive and the notification drops
25 * . UWB radio controller drivers queue events to the daemon using
26 * uwbd_event_queue(). They just get the event, chew it to make it
27 * look like UWBD likes it and pass it in a buffer allocated with
32 * Events have a type, a subtype, a length, some other stuff and the
33 * data blob, which depends on the event. The header is 'struct
34 * uwb_event'; for payloads, see 'struct uwbd_evt_*'.
36 * EVENT HANDLER TABLES
38 * To find a handling function for an event, the type is used to index
39 * a subtype-table in the type-table. The subtype-table is indexed
40 * with the subtype to get the function that handles the event. Start
41 * with the main type-table 'uwbd_evt_type_handler'.
45 * Devices are created when a bunch of beacons have been received and
46 * it is stablished that the device has stable radio presence. CREATED
47 * only, not configured. Devices are ONLY configured when an
48 * Application-Specific IE Probe is receieved, in which the device
49 * declares which Protocol ID it groks. Then the device is CONFIGURED
50 * (and the driver->probe() stuff of the device model is invoked).
52 * Devices are considered disconnected when a certain number of
53 * beacons are not received in an amount of time.
55 * Handler functions are called normally uwbd_evt_handle_*().
57 #include <linux/kthread.h>
58 #include <linux/slab.h>
59 #include <linux/module.h>
60 #include <linux/freezer.h>
62 #include "uwb-internal.h"
65 * UWBD Event handler function signature
67 * Return !0 if the event needs not to be freed (ie the handler
68 * takes/took care of it). 0 means the daemon code will free the
71 * @evt->rc is already referenced and guaranteed to exist. See
74 typedef int (*uwbd_evt_handler_f
)(struct uwb_event
*);
77 * Properties of a UWBD event
79 * @handler: the function that will handle this event
80 * @name: text name of event
83 uwbd_evt_handler_f handler
;
87 /* Table of handlers for and properties of the UWBD Radio Control Events */
88 static struct uwbd_event uwbd_urc_events
[] = {
89 [UWB_RC_EVT_IE_RCV
] = {
90 .handler
= uwbd_evt_handle_rc_ie_rcv
,
93 [UWB_RC_EVT_BEACON
] = {
94 .handler
= uwbd_evt_handle_rc_beacon
,
95 .name
= "BEACON_RECEIVED"
97 [UWB_RC_EVT_BEACON_SIZE
] = {
98 .handler
= uwbd_evt_handle_rc_beacon_size
,
99 .name
= "BEACON_SIZE_CHANGE"
101 [UWB_RC_EVT_BPOIE_CHANGE
] = {
102 .handler
= uwbd_evt_handle_rc_bpoie_change
,
103 .name
= "BPOIE_CHANGE"
105 [UWB_RC_EVT_BP_SLOT_CHANGE
] = {
106 .handler
= uwbd_evt_handle_rc_bp_slot_change
,
107 .name
= "BP_SLOT_CHANGE"
109 [UWB_RC_EVT_DRP_AVAIL
] = {
110 .handler
= uwbd_evt_handle_rc_drp_avail
,
111 .name
= "DRP_AVAILABILITY_CHANGE"
114 .handler
= uwbd_evt_handle_rc_drp
,
117 [UWB_RC_EVT_DEV_ADDR_CONFLICT
] = {
118 .handler
= uwbd_evt_handle_rc_dev_addr_conflict
,
119 .name
= "DEV_ADDR_CONFLICT",
125 struct uwbd_evt_type_handler
{
127 struct uwbd_event
*uwbd_events
;
131 /* Table of handlers for each UWBD Event type. */
132 static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers
[] = {
133 [UWB_RC_CET_GENERAL
] = {
135 .uwbd_events
= uwbd_urc_events
,
136 .size
= ARRAY_SIZE(uwbd_urc_events
),
140 static const struct uwbd_event uwbd_message_handlers
[] = {
141 [UWB_EVT_MSG_RESET
] = {
142 .handler
= uwbd_msg_handle_reset
,
148 * Handle an URC event passed to the UWB Daemon
150 * @evt: the event to handle
151 * @returns: 0 if the event can be kfreed, !0 on the contrary
152 * (somebody else took ownership) [coincidentally, returning
153 * a <0 errno code will free it :)].
155 * Looks up the two indirection tables (one for the type, one for the
156 * subtype) to decide which function handles it and then calls the
159 * The event structure passed to the event handler has the radio
160 * controller in @evt->rc referenced. The reference will be dropped
161 * once the handler returns, so if it needs it for longer (async),
162 * it'll need to take another one.
165 int uwbd_event_handle_urc(struct uwb_event
*evt
)
167 int result
= -EINVAL
;
168 struct uwbd_evt_type_handler
*type_table
;
169 uwbd_evt_handler_f handler
;
173 type
= evt
->notif
.rceb
->bEventType
;
174 event
= le16_to_cpu(evt
->notif
.rceb
->wEvent
);
175 context
= evt
->notif
.rceb
->bEventContext
;
177 if (type
>= ARRAY_SIZE(uwbd_urc_evt_type_handlers
))
179 type_table
= &uwbd_urc_evt_type_handlers
[type
];
180 if (type_table
->uwbd_events
== NULL
)
182 if (event
>= type_table
->size
)
184 handler
= type_table
->uwbd_events
[event
].handler
;
188 result
= (*handler
)(evt
);
191 dev_err(&evt
->rc
->uwb_dev
.dev
,
192 "UWBD: event 0x%02x/%04x/%02x, handling failed: %d\n",
193 type
, event
, context
, result
);
197 static void uwbd_event_handle_message(struct uwb_event
*evt
)
204 if (evt
->message
< 0 || evt
->message
>= ARRAY_SIZE(uwbd_message_handlers
)) {
205 dev_err(&rc
->uwb_dev
.dev
, "UWBD: invalid message type %d\n", evt
->message
);
209 result
= uwbd_message_handlers
[evt
->message
].handler(evt
);
211 dev_err(&rc
->uwb_dev
.dev
, "UWBD: '%s' message failed: %d\n",
212 uwbd_message_handlers
[evt
->message
].name
, result
);
215 static void uwbd_event_handle(struct uwb_event
*evt
)
224 case UWB_EVT_TYPE_NOTIF
:
225 should_keep
= uwbd_event_handle_urc(evt
);
226 if (should_keep
<= 0)
227 kfree(evt
->notif
.rceb
);
229 case UWB_EVT_TYPE_MSG
:
230 uwbd_event_handle_message(evt
);
233 dev_err(&rc
->uwb_dev
.dev
, "UWBD: invalid event type %d\n", evt
->type
);
238 __uwb_rc_put(rc
); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
244 * Listens to all UWB notifications and takes care to track the state
245 * of the UWB neighbourhood for the kernel. When we do a run, we
246 * spinlock, move the list to a private copy and release the
247 * lock. Hold it as little as possible. Not a conflict: it is
248 * guaranteed we own the events in the private list.
250 * FIXME: should change so we don't have a 1HZ timer all the time, but
251 * only if there are devices.
253 static int uwbd(void *param
)
255 struct uwb_rc
*rc
= param
;
257 struct uwb_event
*evt
;
261 wait_event_interruptible_timeout(
263 !list_empty(&rc
->uwbd
.event_list
)
264 || (should_stop
= kthread_should_stop()),
269 spin_lock_irqsave(&rc
->uwbd
.event_list_lock
, flags
);
270 if (!list_empty(&rc
->uwbd
.event_list
)) {
271 evt
= list_first_entry(&rc
->uwbd
.event_list
, struct uwb_event
, list_node
);
272 list_del(&evt
->list_node
);
275 spin_unlock_irqrestore(&rc
->uwbd
.event_list_lock
, flags
);
278 uwbd_event_handle(evt
);
282 uwb_beca_purge(rc
); /* Purge devices that left */
288 /** Start the UWB daemon */
289 void uwbd_start(struct uwb_rc
*rc
)
291 struct task_struct
*task
= kthread_run(uwbd
, rc
, "uwbd");
293 rc
->uwbd
.task
= NULL
;
294 printk(KERN_ERR
"UWB: Cannot start management daemon; "
297 rc
->uwbd
.task
= task
;
298 rc
->uwbd
.pid
= rc
->uwbd
.task
->pid
;
302 /* Stop the UWB daemon and free any unprocessed events */
303 void uwbd_stop(struct uwb_rc
*rc
)
306 kthread_stop(rc
->uwbd
.task
);
311 * Queue an event for the management daemon
313 * When some lower layer receives an event, it uses this function to
314 * push it forward to the UWB daemon.
316 * Once you pass the event, you don't own it any more, but the daemon
317 * does. It will uwb_event_free() it when done, so make sure you
318 * uwb_event_alloc()ed it or bad things will happen.
320 * If the daemon is not running, we just free the event.
322 void uwbd_event_queue(struct uwb_event
*evt
)
324 struct uwb_rc
*rc
= evt
->rc
;
327 spin_lock_irqsave(&rc
->uwbd
.event_list_lock
, flags
);
328 if (rc
->uwbd
.pid
!= 0) {
329 list_add(&evt
->list_node
, &rc
->uwbd
.event_list
);
330 wake_up_all(&rc
->uwbd
.wq
);
332 __uwb_rc_put(evt
->rc
);
333 if (evt
->type
== UWB_EVT_TYPE_NOTIF
)
334 kfree(evt
->notif
.rceb
);
337 spin_unlock_irqrestore(&rc
->uwbd
.event_list_lock
, flags
);
341 void uwbd_flush(struct uwb_rc
*rc
)
343 struct uwb_event
*evt
, *nxt
;
345 spin_lock_irq(&rc
->uwbd
.event_list_lock
);
346 list_for_each_entry_safe(evt
, nxt
, &rc
->uwbd
.event_list
, list_node
) {
349 list_del(&evt
->list_node
);
350 if (evt
->type
== UWB_EVT_TYPE_NOTIF
)
351 kfree(evt
->notif
.rceb
);
355 spin_unlock_irq(&rc
->uwbd
.event_list_lock
);