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/module.h>
73#include <linux/freezer.h>
74
75#include "uwb-internal.h"
76
77
78
79
80
81
82
83
84
85
86
87typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
88
89
90
91
92
93
94
95struct uwbd_event {
96 uwbd_evt_handler_f handler;
97 const char *name;
98};
99
100
101static struct uwbd_event uwbd_urc_events[] = {
102 [UWB_RC_EVT_IE_RCV] = {
103 .handler = uwbd_evt_handle_rc_ie_rcv,
104 .name = "IE_RECEIVED"
105 },
106 [UWB_RC_EVT_BEACON] = {
107 .handler = uwbd_evt_handle_rc_beacon,
108 .name = "BEACON_RECEIVED"
109 },
110 [UWB_RC_EVT_BEACON_SIZE] = {
111 .handler = uwbd_evt_handle_rc_beacon_size,
112 .name = "BEACON_SIZE_CHANGE"
113 },
114 [UWB_RC_EVT_BPOIE_CHANGE] = {
115 .handler = uwbd_evt_handle_rc_bpoie_change,
116 .name = "BPOIE_CHANGE"
117 },
118 [UWB_RC_EVT_BP_SLOT_CHANGE] = {
119 .handler = uwbd_evt_handle_rc_bp_slot_change,
120 .name = "BP_SLOT_CHANGE"
121 },
122 [UWB_RC_EVT_DRP_AVAIL] = {
123 .handler = uwbd_evt_handle_rc_drp_avail,
124 .name = "DRP_AVAILABILITY_CHANGE"
125 },
126 [UWB_RC_EVT_DRP] = {
127 .handler = uwbd_evt_handle_rc_drp,
128 .name = "DRP"
129 },
130 [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
131 .handler = uwbd_evt_handle_rc_dev_addr_conflict,
132 .name = "DEV_ADDR_CONFLICT",
133 },
134};
135
136
137
138struct uwbd_evt_type_handler {
139 const char *name;
140 struct uwbd_event *uwbd_events;
141 size_t size;
142};
143
144
145static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = {
146 [UWB_RC_CET_GENERAL] = {
147 .name = "URC",
148 .uwbd_events = uwbd_urc_events,
149 .size = ARRAY_SIZE(uwbd_urc_events),
150 },
151};
152
153static const struct uwbd_event uwbd_message_handlers[] = {
154 [UWB_EVT_MSG_RESET] = {
155 .handler = uwbd_msg_handle_reset,
156 .name = "reset",
157 },
158};
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177static
178int uwbd_event_handle_urc(struct uwb_event *evt)
179{
180 int result = -EINVAL;
181 struct uwbd_evt_type_handler *type_table;
182 uwbd_evt_handler_f handler;
183 u8 type, context;
184 u16 event;
185
186 type = evt->notif.rceb->bEventType;
187 event = le16_to_cpu(evt->notif.rceb->wEvent);
188 context = evt->notif.rceb->bEventContext;
189
190 if (type >= ARRAY_SIZE(uwbd_urc_evt_type_handlers))
191 goto out;
192 type_table = &uwbd_urc_evt_type_handlers[type];
193 if (type_table->uwbd_events == NULL)
194 goto out;
195 if (event >= type_table->size)
196 goto out;
197 handler = type_table->uwbd_events[event].handler;
198 if (handler == NULL)
199 goto out;
200
201 result = (*handler)(evt);
202out:
203 if (result < 0)
204 dev_err(&evt->rc->uwb_dev.dev,
205 "UWBD: event 0x%02x/%04x/%02x, handling failed: %d\n",
206 type, event, context, result);
207 return result;
208}
209
210static void uwbd_event_handle_message(struct uwb_event *evt)
211{
212 struct uwb_rc *rc;
213 int result;
214
215 rc = evt->rc;
216
217 if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
218 dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
219 return;
220 }
221
222 result = uwbd_message_handlers[evt->message].handler(evt);
223 if (result < 0)
224 dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
225 uwbd_message_handlers[evt->message].name, result);
226}
227
228static void uwbd_event_handle(struct uwb_event *evt)
229{
230 struct uwb_rc *rc;
231 int should_keep;
232
233 rc = evt->rc;
234
235 if (rc->ready) {
236 switch (evt->type) {
237 case UWB_EVT_TYPE_NOTIF:
238 should_keep = uwbd_event_handle_urc(evt);
239 if (should_keep <= 0)
240 kfree(evt->notif.rceb);
241 break;
242 case UWB_EVT_TYPE_MSG:
243 uwbd_event_handle_message(evt);
244 break;
245 default:
246 dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
247 break;
248 }
249 }
250
251 __uwb_rc_put(rc);
252}
253
254
255
256
257
258
259
260
261
262
263
264
265
266static int uwbd(void *param)
267{
268 struct uwb_rc *rc = param;
269 unsigned long flags;
270 struct uwb_event *evt;
271 int should_stop = 0;
272
273 while (1) {
274 wait_event_interruptible_timeout(
275 rc->uwbd.wq,
276 !list_empty(&rc->uwbd.event_list)
277 || (should_stop = kthread_should_stop()),
278 HZ);
279 if (should_stop)
280 break;
281 try_to_freeze();
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