Blame view

drivers/uwb/uwbd.c 10.2 KB
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
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
  /*
   * Ultra Wide Band
   * Neighborhood Management Daemon
   *
   * Copyright (C) 2005-2006 Intel Corporation
   * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version
   * 2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   * 02110-1301, USA.
   *
   *
   * This daemon takes care of maintaing information that describes the
   * UWB neighborhood that the radios in this machine can see. It also
   * keeps a tab of which devices are visible, makes sure each HC sits
   * on a different channel to avoid interfering, etc.
   *
   * Different drivers (radio controller, device, any API in general)
   * communicate with this daemon through an event queue. Daemon wakes
   * up, takes a list of events and handles them one by one; handling
   * function is extracted from a table based on the event's type and
   * subtype. Events are freed only if the handling function says so.
   *
   *   . Lock protecting the event list has to be an spinlock and locked
   *     with IRQSAVE because it might be called from an interrupt
   *     context (ie: when events arrive and the notification drops
   *     down from the ISR).
   *
   *   . UWB radio controller drivers queue events to the daemon using
   *     uwbd_event_queue(). They just get the event, chew it to make it
   *     look like UWBD likes it and pass it in a buffer allocated with
   *     uwb_event_alloc().
   *
   * EVENTS
   *
1537a3638   Daniel Mack   tree-wide: fix 'l...
46
   * Events have a type, a subtype, a length, some other stuff and the
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
   * data blob, which depends on the event. The header is 'struct
   * uwb_event'; for payloads, see 'struct uwbd_evt_*'.
   *
   * EVENT HANDLER TABLES
   *
   * To find a handling function for an event, the type is used to index
   * a subtype-table in the type-table. The subtype-table is indexed
   * with the subtype to get the function that handles the event. Start
   * with the main type-table 'uwbd_evt_type_handler'.
   *
   * DEVICES
   *
   * Devices are created when a bunch of beacons have been received and
   * it is stablished that the device has stable radio presence. CREATED
   * only, not configured. Devices are ONLY configured when an
   * Application-Specific IE Probe is receieved, in which the device
   * declares which Protocol ID it groks. Then the device is CONFIGURED
   * (and the driver->probe() stuff of the device model is invoked).
   *
   * Devices are considered disconnected when a certain number of
   * beacons are not received in an amount of time.
   *
   * Handler functions are called normally uwbd_evt_handle_*().
   */
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
71
  #include <linux/kthread.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
72
  #include <linux/slab.h>
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
73
74
  #include <linux/module.h>
  #include <linux/freezer.h>
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
75

bce83697c   David Vrabel   uwb: use dev_dbg(...
76
  #include "uwb-internal.h"
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
77

bce83697c   David Vrabel   uwb: use dev_dbg(...
78
  /*
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
   * UWBD Event handler function signature
   *
   * Return !0 if the event needs not to be freed (ie the handler
   * takes/took care of it). 0 means the daemon code will free the
   * event.
   *
   * @evt->rc is already referenced and guaranteed to exist. See
   * uwb_evt_handle().
   */
  typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
  
  /**
   * Properties of a UWBD event
   *
   * @handler:    the function that will handle this event
   * @name:       text name of event
   */
  struct uwbd_event {
  	uwbd_evt_handler_f handler;
  	const char *name;
  };
bce83697c   David Vrabel   uwb: use dev_dbg(...
100
101
  /* Table of handlers for and properties of the UWBD Radio Control Events */
  static struct uwbd_event uwbd_urc_events[] = {
c5995bd28   Stefano Panella   uwb: infrastructu...
102
103
104
105
  	[UWB_RC_EVT_IE_RCV] = {
  		.handler = uwbd_evt_handle_rc_ie_rcv,
  		.name = "IE_RECEIVED"
  	},
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  	[UWB_RC_EVT_BEACON] = {
  		.handler = uwbd_evt_handle_rc_beacon,
  		.name = "BEACON_RECEIVED"
  	},
  	[UWB_RC_EVT_BEACON_SIZE] = {
  		.handler = uwbd_evt_handle_rc_beacon_size,
  		.name = "BEACON_SIZE_CHANGE"
  	},
  	[UWB_RC_EVT_BPOIE_CHANGE] = {
  		.handler = uwbd_evt_handle_rc_bpoie_change,
  		.name = "BPOIE_CHANGE"
  	},
  	[UWB_RC_EVT_BP_SLOT_CHANGE] = {
  		.handler = uwbd_evt_handle_rc_bp_slot_change,
  		.name = "BP_SLOT_CHANGE"
  	},
  	[UWB_RC_EVT_DRP_AVAIL] = {
  		.handler = uwbd_evt_handle_rc_drp_avail,
  		.name = "DRP_AVAILABILITY_CHANGE"
  	},
  	[UWB_RC_EVT_DRP] = {
  		.handler = uwbd_evt_handle_rc_drp,
  		.name = "DRP"
  	},
  	[UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  		.handler = uwbd_evt_handle_rc_dev_addr_conflict,
  		.name = "DEV_ADDR_CONFLICT",
  	},
  };
  
  
  
  struct uwbd_evt_type_handler {
  	const char *name;
  	struct uwbd_event *uwbd_events;
  	size_t size;
  };
bce83697c   David Vrabel   uwb: use dev_dbg(...
143
144
145
146
147
148
149
  /* Table of handlers for each UWBD Event type. */
  static struct uwbd_evt_type_handler uwbd_urc_evt_type_handlers[] = {
  	[UWB_RC_CET_GENERAL] = {
  		.name        = "URC",
  		.uwbd_events = uwbd_urc_events,
  		.size        = ARRAY_SIZE(uwbd_urc_events),
  	},
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
150
  };
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
151
152
153
154
155
156
  static const struct uwbd_event uwbd_message_handlers[] = {
  	[UWB_EVT_MSG_RESET] = {
  		.handler = uwbd_msg_handle_reset,
  		.name = "reset",
  	},
  };
bce83697c   David Vrabel   uwb: use dev_dbg(...
157
  /*
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
   * Handle an URC event passed to the UWB Daemon
   *
   * @evt: the event to handle
   * @returns: 0 if the event can be kfreed, !0 on the contrary
   *           (somebody else took ownership) [coincidentally, returning
   *           a <0 errno code will free it :)].
   *
   * Looks up the two indirection tables (one for the type, one for the
   * subtype) to decide which function handles it and then calls the
   * handler.
   *
   * The event structure passed to the event handler has the radio
   * controller in @evt->rc referenced. The reference will be dropped
   * once the handler returns, so if it needs it for longer (async),
   * it'll need to take another one.
   */
  static
  int uwbd_event_handle_urc(struct uwb_event *evt)
  {
bce83697c   David Vrabel   uwb: use dev_dbg(...
177
  	int result = -EINVAL;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
178
179
180
181
182
183
184
185
  	struct uwbd_evt_type_handler *type_table;
  	uwbd_evt_handler_f handler;
  	u8 type, context;
  	u16 event;
  
  	type = evt->notif.rceb->bEventType;
  	event = le16_to_cpu(evt->notif.rceb->wEvent);
  	context = evt->notif.rceb->bEventContext;
3fafdd7cc   David Vrabel   uwb: handle radio...
186
  	if (type >= ARRAY_SIZE(uwbd_urc_evt_type_handlers))
bce83697c   David Vrabel   uwb: use dev_dbg(...
187
188
189
190
  		goto out;
  	type_table = &uwbd_urc_evt_type_handlers[type];
  	if (type_table->uwbd_events == NULL)
  		goto out;
3fafdd7cc   David Vrabel   uwb: handle radio...
191
  	if (event >= type_table->size)
bce83697c   David Vrabel   uwb: use dev_dbg(...
192
  		goto out;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
193
  	handler = type_table->uwbd_events[event].handler;
bce83697c   David Vrabel   uwb: use dev_dbg(...
194
195
196
197
198
199
200
201
202
203
204
  	if (handler == NULL)
  		goto out;
  
  	result = (*handler)(evt);
  out:
  	if (result < 0)
  		dev_err(&evt->rc->uwb_dev.dev,
  			"UWBD: event 0x%02x/%04x/%02x, handling failed: %d
  ",
  			type, event, context, result);
  	return result;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  }
  
  static void uwbd_event_handle_message(struct uwb_event *evt)
  {
  	struct uwb_rc *rc;
  	int result;
  
  	rc = evt->rc;
  
  	if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
  		dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d
  ", evt->message);
  		return;
  	}
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
219
220
221
222
223
  	result = uwbd_message_handlers[evt->message].handler(evt);
  	if (result < 0)
  		dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d
  ",
  			uwbd_message_handlers[evt->message].name, result);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  }
  
  static void uwbd_event_handle(struct uwb_event *evt)
  {
  	struct uwb_rc *rc;
  	int should_keep;
  
  	rc = evt->rc;
  
  	if (rc->ready) {
  		switch (evt->type) {
  		case UWB_EVT_TYPE_NOTIF:
  			should_keep = uwbd_event_handle_urc(evt);
  			if (should_keep <= 0)
  				kfree(evt->notif.rceb);
  			break;
  		case UWB_EVT_TYPE_MSG:
  			uwbd_event_handle_message(evt);
  			break;
  		default:
  			dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d
  ", evt->type);
  			break;
  		}
  	}
  
  	__uwb_rc_put(rc);	/* for the __uwb_rc_get() in uwb_rc_notif_cb() */
  }
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
252
253
254
255
256
  
  /**
   * UWB Daemon
   *
   * Listens to all UWB notifications and takes care to track the state
06b72d06d   Jesper Juhl   uwb: Fix misspell...
257
   * of the UWB neighbourhood for the kernel. When we do a run, we
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
258
259
260
261
262
263
264
   * spinlock, move the list to a private copy and release the
   * lock. Hold it as little as possible. Not a conflict: it is
   * guaranteed we own the events in the private list.
   *
   * FIXME: should change so we don't have a 1HZ timer all the time, but
   *        only if there are devices.
   */
fec1a5932   Stefano Panella   uwb: per-radio co...
265
  static int uwbd(void *param)
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
266
  {
fec1a5932   Stefano Panella   uwb: per-radio co...
267
  	struct uwb_rc *rc = param;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
268
  	unsigned long flags;
fec1a5932   Stefano Panella   uwb: per-radio co...
269
  	struct uwb_event *evt;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
270
  	int should_stop = 0;
fec1a5932   Stefano Panella   uwb: per-radio co...
271

183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
272
273
  	while (1) {
  		wait_event_interruptible_timeout(
fec1a5932   Stefano Panella   uwb: per-radio co...
274
275
  			rc->uwbd.wq,
  			!list_empty(&rc->uwbd.event_list)
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
276
277
278
279
280
  			  || (should_stop = kthread_should_stop()),
  			HZ);
  		if (should_stop)
  			break;
  		try_to_freeze();
fec1a5932   Stefano Panella   uwb: per-radio co...
281
282
283
  		spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
  		if (!list_empty(&rc->uwbd.event_list)) {
  			evt = list_first_entry(&rc->uwbd.event_list, struct uwb_event, list_node);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
284
  			list_del(&evt->list_node);
fec1a5932   Stefano Panella   uwb: per-radio co...
285
286
287
288
289
  		} else
  			evt = NULL;
  		spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
  
  		if (evt) {
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
290
291
292
  			uwbd_event_handle(evt);
  			kfree(evt);
  		}
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
293

fec1a5932   Stefano Panella   uwb: per-radio co...
294
  		uwb_beca_purge(rc);	/* Purge devices that left */
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
295
296
297
298
299
300
  	}
  	return 0;
  }
  
  
  /** Start the UWB daemon */
fec1a5932   Stefano Panella   uwb: per-radio co...
301
  void uwbd_start(struct uwb_rc *rc)
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
302
  {
fec1a5932   Stefano Panella   uwb: per-radio co...
303
304
  	rc->uwbd.task = kthread_run(uwbd, rc, "uwbd");
  	if (rc->uwbd.task == NULL)
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
305
306
307
308
  		printk(KERN_ERR "UWB: Cannot start management daemon; "
  		       "UWB won't work
  ");
  	else
fec1a5932   Stefano Panella   uwb: per-radio co...
309
  		rc->uwbd.pid = rc->uwbd.task->pid;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
310
311
312
  }
  
  /* Stop the UWB daemon and free any unprocessed events */
fec1a5932   Stefano Panella   uwb: per-radio co...
313
  void uwbd_stop(struct uwb_rc *rc)
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
314
  {
fec1a5932   Stefano Panella   uwb: per-radio co...
315
316
  	kthread_stop(rc->uwbd.task);
  	uwbd_flush(rc);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  }
  
  /*
   * Queue an event for the management daemon
   *
   * When some lower layer receives an event, it uses this function to
   * push it forward to the UWB daemon.
   *
   * Once you pass the event, you don't own it any more, but the daemon
   * does. It will uwb_event_free() it when done, so make sure you
   * uwb_event_alloc()ed it or bad things will happen.
   *
   * If the daemon is not running, we just free the event.
   */
  void uwbd_event_queue(struct uwb_event *evt)
  {
fec1a5932   Stefano Panella   uwb: per-radio co...
333
  	struct uwb_rc *rc = evt->rc;
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
334
  	unsigned long flags;
fec1a5932   Stefano Panella   uwb: per-radio co...
335
336
337
338
339
  
  	spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
  	if (rc->uwbd.pid != 0) {
  		list_add(&evt->list_node, &rc->uwbd.event_list);
  		wake_up_all(&rc->uwbd.wq);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
340
341
342
343
344
345
  	} else {
  		__uwb_rc_put(evt->rc);
  		if (evt->type == UWB_EVT_TYPE_NOTIF)
  			kfree(evt->notif.rceb);
  		kfree(evt);
  	}
fec1a5932   Stefano Panella   uwb: per-radio co...
346
  	spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
347
348
349
350
351
352
  	return;
  }
  
  void uwbd_flush(struct uwb_rc *rc)
  {
  	struct uwb_event *evt, *nxt;
fec1a5932   Stefano Panella   uwb: per-radio co...
353
354
  	spin_lock_irq(&rc->uwbd.event_list_lock);
  	list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) {
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
355
356
357
358
359
360
361
362
  		if (evt->rc == rc) {
  			__uwb_rc_put(rc);
  			list_del(&evt->list_node);
  			if (evt->type == UWB_EVT_TYPE_NOTIF)
  				kfree(evt->notif.rceb);
  			kfree(evt);
  		}
  	}
fec1a5932   Stefano Panella   uwb: per-radio co...
363
  	spin_unlock_irq(&rc->uwbd.event_list_lock);
183b9b592   Inaky Perez-Gonzalez   uwb: add the UWB ...
364
  }