1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include <linux/kthread.h>
72#include <linux/slab.h>
73#include <linux/module.h>
74#include <linux/freezer.h>
75
76#include "uwb-internal.h"
77
78
79
80
81
82
83
84
85
86
87
88typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
89
90
91
92
93
94
95
96struct uwbd_event {
97 uwbd_evt_handler_f handler;
98 const char *name;
99};
100
101
102static struct uwbd_event uwbd_urc_events[] = {
103 [UWB_RC_EVT_IE_RCV] = {
104 .handler = uwbd_evt_handle_rc_ie_rcv,
105 .name = "IE_RECEIVED"
106 },
107 [UWB_RC_EVT_BEACON] = {
108 .handler = uwbd_evt_handle_rc_beacon,
109 .name = "BEACON_RECEIVED"
110 },
111 [UWB_RC_EVT_BEACON_SIZE] = {
112 .handler = uwbd_evt_handle_rc_beacon_size,
113 .name = "BEACON_SIZE_CHANGE"
114 },
115 [UWB_RC_EVT_BPOIE_CHANGE] = {
116 .handler = uwbd_evt_handle_rc_bpoie_change,
117 .name = "BPOIE_CHANGE"
118 },
119 [UWB_RC_EVT_BP_SLOT_CHANGE] = {
120 .handler = uwbd_evt_handle_rc_bp_slot_change,
121 .name = "BP_SLOT_CHANGE"
122 },
123 [UWB_RC_EVT_DRP_AVAIL] = {
124 .handler = uwbd_evt_handle_rc_drp_avail,
125 .name = "DRP_AVAILABILITY_CHANGE"
126 },
127 [UWB_RC_EVT_DRP] = {
128 .handler = uwbd_evt_handle_rc_drp,
129 .name = "DRP"
130 },
131 [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
132 .handler = uwbd_evt_handle_rc_dev_addr_conflict,
133 .name = "DEV_ADDR_CONFLICT",
134 },
135};
136
137
138
139struct uwbd_evt_type_handler {
140 const char *name;
141 struct uwbd_event *uwbd_events;
142 size_t size;
143};
144
145
146static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = {
147 [UWB_RC_CET_GENERAL] = {
148 .name = "URC",
149 .uwbd_events = uwbd_urc_events,
150 .size = ARRAY_SIZE(uwbd_urc_events),
151 },
152};
153
154static const struct uwbd_event uwbd_message_handlers[] = {
155 [UWB_EVT_MSG_RESET] = {
156 .handler = uwbd_msg_handle_reset,
157 .name = "reset",
158 },
159};
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178static
179int uwbd_event_handle_urc(struct uwb_event *evt)
180{
181 int result = -EINVAL;
182 struct uwbd_evt_type_handler *type_table;
183 uwbd_evt_handler_f handler;
184 u8 type, context;
185 u16 event;
186
187 type = evt->notif.rceb->bEventType;
188 event = le16_to_cpu(evt->notif.rceb->wEvent);
189 context = evt->notif.rceb->bEventContext;
190
191 if (type >= ARRAY_SIZE(uwbd_urc_evt_type_handlers))
192 goto out;
193 type_table = &uwbd_urc_evt_type_handlers[type];
194 if (type_table->uwbd_events == NULL)
195 goto out;
196 if (event >= type_table->size)
197 goto out;
198 handler = type_table->uwbd_events[event].handler;
199 if (handler == NULL)
200 goto out;
201
202 result = (*handler)(evt);
203out:
204 if (result < 0)
205 dev_err(&evt->rc->uwb_dev.dev,
206 "UWBD: event 0x%02x/%04x/%02x, handling failed: %d\n",
207 type, event, context, result);
208 return result;
209}
210
211static void uwbd_event_handle_message(struct uwb_event *evt)
212{
213 struct uwb_rc *rc;
214 int result;
215
216 rc = evt->rc;
217
218 if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
219 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
220 return;
221 }
222
223 result = uwbd_message_handlers[evt->message].handler(evt);
224 if (result < 0)
225 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
226 uwbd_message_handlers[evt->message].name, result);
227}
228
229static void uwbd_event_handle(struct uwb_event *evt)
230{
231 struct uwb_rc *rc;
232 int should_keep;
233
234 rc = evt->rc;
235
236 if (rc->ready) {
237 switch (evt->type) {
238 case UWB_EVT_TYPE_NOTIF:
239 should_keep = uwbd_event_handle_urc(evt);
240 if (should_keep <= 0)
241 kfree(evt->notif.rceb);
242 break;
243 case UWB_EVT_TYPE_MSG:
244 uwbd_event_handle_message(evt);
245 break;
246 default:
247 dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
248 break;
249 }
250 }
251
252 __uwb_rc_put(rc);
253}
254
255
256
257
258
259
260
261
262
263
264
265
266
267static int uwbd(void *param)
268{
269 struct uwb_rc *rc = param;
270 unsigned long flags;
271 struct uwb_event *evt;
272 int should_stop = 0;
273
274 while (1) {
275 wait_event_interruptible_timeout(
276 rc->uwbd.wq,
277 !list_empty(&rc->uwbd.event_list)
278 || (should_stop = kthread_should_stop()),
279 HZ);
280 if (should_stop)
281 break;
282
283 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
284 if (!list_empty(&rc->uwbd.event_list)) {
285 evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node);
286 list_del(&evt->list_node);
287 } else
288 evt = NULL;
289 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
290
291 if (evt) {
292 uwbd_event_handle(evt);
293 kfree(evt);
294 }
295
296 uwb_beca_purge(rc);
297 }
298 return 0;
299}
300
301
302
303void uwbd_start(struct uwb_rc *rc)
304{
305 rc->uwbd.task = kthread_run(uwbd, rc, "uwbd");
306 if (rc->uwbd.task == NULL)
307 printk(KERN_ERR "UWB: Cannot start management daemon; "
308 "UWB won't work\n");
309 else
310 rc->uwbd.pid = rc->uwbd.task->pid;
311}
312
313
314void uwbd_stop(struct uwb_rc *rc)
315{
316 kthread_stop(rc->uwbd.task);
317 uwbd_flush(rc);
318}
319
320
321
322
323
324
325
326
327
328
329
330
331
332void uwbd_event_queue(struct uwb_event *evt)
333{
334 struct uwb_rc *rc = evt->rc;
335 unsigned long flags;
336
337 spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
338 if (rc->uwbd.pid != 0) {
339 list_add(&evt->list_node, &rc->uwbd.event_list);
340 wake_up_all(&rc->uwbd.wq);
341 } else {
342 __uwb_rc_put(evt->rc);
343 if (evt->type == UWB_EVT_TYPE_NOTIF)
344 kfree(evt->notif.rceb);
345 kfree(evt);
346 }
347 spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
348 return;
349}
350
351void uwbd_flush(struct uwb_rc *rc)
352{
353 struct uwb_event *evt, *nxt;
354
355 spin_lock_irq(&rc->uwbd.event_list_lock);
356 list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) {
357 if (evt->rc == rc) {
358 __uwb_rc_put(rc);
359 list_del(&evt->list_node);
360 if (evt->type == UWB_EVT_TYPE_NOTIF)
361 kfree(evt->notif.rceb);
362 kfree(evt);
363 }
364 }
365 spin_unlock_irq(&rc->uwbd.event_list_lock);
366}
367