uwbd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Ultra Wide Band
  3. * Neighborhood Management Daemon
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * This daemon takes care of maintaing information that describes the
  24. * UWB neighborhood that the radios in this machine can see. It also
  25. * keeps a tab of which devices are visible, makes sure each HC sits
  26. * on a different channel to avoid interfering, etc.
  27. *
  28. * Different drivers (radio controller, device, any API in general)
  29. * communicate with this daemon through an event queue. Daemon wakes
  30. * up, takes a list of events and handles them one by one; handling
  31. * function is extracted from a table based on the event's type and
  32. * subtype. Events are freed only if the handling function says so.
  33. *
  34. * . Lock protecting the event list has to be an spinlock and locked
  35. * with IRQSAVE because it might be called from an interrupt
  36. * context (ie: when events arrive and the notification drops
  37. * down from the ISR).
  38. *
  39. * . UWB radio controller drivers queue events to the daemon using
  40. * uwbd_event_queue(). They just get the event, chew it to make it
  41. * look like UWBD likes it and pass it in a buffer allocated with
  42. * uwb_event_alloc().
  43. *
  44. * EVENTS
  45. *
  46. * Events have a type, a subtype, a lenght, some other stuff and the
  47. * data blob, which depends on the event. The header is 'struct
  48. * uwb_event'; for payloads, see 'struct uwbd_evt_*'.
  49. *
  50. * EVENT HANDLER TABLES
  51. *
  52. * To find a handling function for an event, the type is used to index
  53. * a subtype-table in the type-table. The subtype-table is indexed
  54. * with the subtype to get the function that handles the event. Start
  55. * with the main type-table 'uwbd_evt_type_handler'.
  56. *
  57. * DEVICES
  58. *
  59. * Devices are created when a bunch of beacons have been received and
  60. * it is stablished that the device has stable radio presence. CREATED
  61. * only, not configured. Devices are ONLY configured when an
  62. * Application-Specific IE Probe is receieved, in which the device
  63. * declares which Protocol ID it groks. Then the device is CONFIGURED
  64. * (and the driver->probe() stuff of the device model is invoked).
  65. *
  66. * Devices are considered disconnected when a certain number of
  67. * beacons are not received in an amount of time.
  68. *
  69. * Handler functions are called normally uwbd_evt_handle_*().
  70. */
  71. #include <linux/kthread.h>
  72. #include <linux/module.h>
  73. #include <linux/freezer.h>
  74. #include "uwb-internal.h"
  75. #define D_LOCAL 1
  76. #include <linux/uwb/debug.h>
  77. /**
  78. * UWBD Event handler function signature
  79. *
  80. * Return !0 if the event needs not to be freed (ie the handler
  81. * takes/took care of it). 0 means the daemon code will free the
  82. * event.
  83. *
  84. * @evt->rc is already referenced and guaranteed to exist. See
  85. * uwb_evt_handle().
  86. */
  87. typedef int (*uwbd_evt_handler_f)(struct uwb_event *);
  88. /**
  89. * Properties of a UWBD event
  90. *
  91. * @handler: the function that will handle this event
  92. * @name: text name of event
  93. */
  94. struct uwbd_event {
  95. uwbd_evt_handler_f handler;
  96. const char *name;
  97. };
  98. /** Table of handlers for and properties of the UWBD Radio Control Events */
  99. static
  100. struct uwbd_event uwbd_events[] = {
  101. [UWB_RC_EVT_BEACON] = {
  102. .handler = uwbd_evt_handle_rc_beacon,
  103. .name = "BEACON_RECEIVED"
  104. },
  105. [UWB_RC_EVT_BEACON_SIZE] = {
  106. .handler = uwbd_evt_handle_rc_beacon_size,
  107. .name = "BEACON_SIZE_CHANGE"
  108. },
  109. [UWB_RC_EVT_BPOIE_CHANGE] = {
  110. .handler = uwbd_evt_handle_rc_bpoie_change,
  111. .name = "BPOIE_CHANGE"
  112. },
  113. [UWB_RC_EVT_BP_SLOT_CHANGE] = {
  114. .handler = uwbd_evt_handle_rc_bp_slot_change,
  115. .name = "BP_SLOT_CHANGE"
  116. },
  117. [UWB_RC_EVT_DRP_AVAIL] = {
  118. .handler = uwbd_evt_handle_rc_drp_avail,
  119. .name = "DRP_AVAILABILITY_CHANGE"
  120. },
  121. [UWB_RC_EVT_DRP] = {
  122. .handler = uwbd_evt_handle_rc_drp,
  123. .name = "DRP"
  124. },
  125. [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  126. .handler = uwbd_evt_handle_rc_dev_addr_conflict,
  127. .name = "DEV_ADDR_CONFLICT",
  128. },
  129. };
  130. struct uwbd_evt_type_handler {
  131. const char *name;
  132. struct uwbd_event *uwbd_events;
  133. size_t size;
  134. };
  135. #define UWBD_EVT_TYPE_HANDLER(n,a) { \
  136. .name = (n), \
  137. .uwbd_events = (a), \
  138. .size = sizeof(a)/sizeof((a)[0]) \
  139. }
  140. /** Table of handlers for each UWBD Event type. */
  141. static
  142. struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = {
  143. [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events)
  144. };
  145. static const
  146. size_t uwbd_evt_type_handlers_len =
  147. sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]);
  148. static const struct uwbd_event uwbd_message_handlers[] = {
  149. [UWB_EVT_MSG_RESET] = {
  150. .handler = uwbd_msg_handle_reset,
  151. .name = "reset",
  152. },
  153. };
  154. static DEFINE_MUTEX(uwbd_event_mutex);
  155. /**
  156. * Handle an URC event passed to the UWB Daemon
  157. *
  158. * @evt: the event to handle
  159. * @returns: 0 if the event can be kfreed, !0 on the contrary
  160. * (somebody else took ownership) [coincidentally, returning
  161. * a <0 errno code will free it :)].
  162. *
  163. * Looks up the two indirection tables (one for the type, one for the
  164. * subtype) to decide which function handles it and then calls the
  165. * handler.
  166. *
  167. * The event structure passed to the event handler has the radio
  168. * controller in @evt->rc referenced. The reference will be dropped
  169. * once the handler returns, so if it needs it for longer (async),
  170. * it'll need to take another one.
  171. */
  172. static
  173. int uwbd_event_handle_urc(struct uwb_event *evt)
  174. {
  175. struct uwbd_evt_type_handler *type_table;
  176. uwbd_evt_handler_f handler;
  177. u8 type, context;
  178. u16 event;
  179. type = evt->notif.rceb->bEventType;
  180. event = le16_to_cpu(evt->notif.rceb->wEvent);
  181. context = evt->notif.rceb->bEventContext;
  182. if (type > uwbd_evt_type_handlers_len) {
  183. printk(KERN_ERR "UWBD: event type %u: unknown (too high)\n", type);
  184. return -EINVAL;
  185. }
  186. type_table = &uwbd_evt_type_handlers[type];
  187. if (type_table->uwbd_events == NULL) {
  188. printk(KERN_ERR "UWBD: event type %u: unknown\n", type);
  189. return -EINVAL;
  190. }
  191. if (event > type_table->size) {
  192. printk(KERN_ERR "UWBD: event %s[%u]: unknown (too high)\n",
  193. type_table->name, event);
  194. return -EINVAL;
  195. }
  196. handler = type_table->uwbd_events[event].handler;
  197. if (handler == NULL) {
  198. printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", type_table->name, event);
  199. return -EINVAL;
  200. }
  201. return (*handler)(evt);
  202. }
  203. static void uwbd_event_handle_message(struct uwb_event *evt)
  204. {
  205. struct uwb_rc *rc;
  206. int result;
  207. rc = evt->rc;
  208. if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
  209. dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
  210. return;
  211. }
  212. /* If this is a reset event we need to drop the
  213. * uwbd_event_mutex or it deadlocks when the reset handler
  214. * attempts to flush the uwbd events. */
  215. if (evt->message == UWB_EVT_MSG_RESET)
  216. mutex_unlock(&uwbd_event_mutex);
  217. result = uwbd_message_handlers[evt->message].handler(evt);
  218. if (result < 0)
  219. dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
  220. uwbd_message_handlers[evt->message].name, result);
  221. if (evt->message == UWB_EVT_MSG_RESET)
  222. mutex_lock(&uwbd_event_mutex);
  223. }
  224. static void uwbd_event_handle(struct uwb_event *evt)
  225. {
  226. struct uwb_rc *rc;
  227. int should_keep;
  228. rc = evt->rc;
  229. if (rc->ready) {
  230. switch (evt->type) {
  231. case UWB_EVT_TYPE_NOTIF:
  232. should_keep = uwbd_event_handle_urc(evt);
  233. if (should_keep <= 0)
  234. kfree(evt->notif.rceb);
  235. break;
  236. case UWB_EVT_TYPE_MSG:
  237. uwbd_event_handle_message(evt);
  238. break;
  239. default:
  240. dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
  241. break;
  242. }
  243. }
  244. __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
  245. }
  246. /* The UWB Daemon */
  247. /** Daemon's PID: used to decide if we can queue or not */
  248. static int uwbd_pid;
  249. /** Daemon's task struct for managing the kthread */
  250. static struct task_struct *uwbd_task;
  251. /** Daemon's waitqueue for waiting for new events */
  252. static DECLARE_WAIT_QUEUE_HEAD(uwbd_wq);
  253. /** Daemon's list of events; we queue/dequeue here */
  254. static struct list_head uwbd_event_list = LIST_HEAD_INIT(uwbd_event_list);
  255. /** Daemon's list lock to protect concurent access */
  256. static DEFINE_SPINLOCK(uwbd_event_list_lock);
  257. /**
  258. * UWB Daemon
  259. *
  260. * Listens to all UWB notifications and takes care to track the state
  261. * of the UWB neighboorhood for the kernel. When we do a run, we
  262. * spinlock, move the list to a private copy and release the
  263. * lock. Hold it as little as possible. Not a conflict: it is
  264. * guaranteed we own the events in the private list.
  265. *
  266. * FIXME: should change so we don't have a 1HZ timer all the time, but
  267. * only if there are devices.
  268. */
  269. static int uwbd(void *unused)
  270. {
  271. unsigned long flags;
  272. struct list_head list = LIST_HEAD_INIT(list);
  273. struct uwb_event *evt, *nxt;
  274. int should_stop = 0;
  275. while (1) {
  276. wait_event_interruptible_timeout(
  277. uwbd_wq,
  278. !list_empty(&uwbd_event_list)
  279. || (should_stop = kthread_should_stop()),
  280. HZ);
  281. if (should_stop)
  282. break;
  283. try_to_freeze();
  284. mutex_lock(&uwbd_event_mutex);
  285. spin_lock_irqsave(&uwbd_event_list_lock, flags);
  286. list_splice_init(&uwbd_event_list, &list);
  287. spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
  288. list_for_each_entry_safe(evt, nxt, &list, list_node) {
  289. list_del(&evt->list_node);
  290. uwbd_event_handle(evt);
  291. kfree(evt);
  292. }
  293. mutex_unlock(&uwbd_event_mutex);
  294. uwb_beca_purge(); /* Purge devices that left */
  295. }
  296. return 0;
  297. }
  298. /** Start the UWB daemon */
  299. void uwbd_start(void)
  300. {
  301. uwbd_task = kthread_run(uwbd, NULL, "uwbd");
  302. if (uwbd_task == NULL)
  303. printk(KERN_ERR "UWB: Cannot start management daemon; "
  304. "UWB won't work\n");
  305. else
  306. uwbd_pid = uwbd_task->pid;
  307. }
  308. /* Stop the UWB daemon and free any unprocessed events */
  309. void uwbd_stop(void)
  310. {
  311. unsigned long flags;
  312. struct uwb_event *evt, *nxt;
  313. kthread_stop(uwbd_task);
  314. spin_lock_irqsave(&uwbd_event_list_lock, flags);
  315. uwbd_pid = 0;
  316. list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
  317. if (evt->type == UWB_EVT_TYPE_NOTIF)
  318. kfree(evt->notif.rceb);
  319. kfree(evt);
  320. }
  321. spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
  322. uwb_beca_release();
  323. }
  324. /*
  325. * Queue an event for the management daemon
  326. *
  327. * When some lower layer receives an event, it uses this function to
  328. * push it forward to the UWB daemon.
  329. *
  330. * Once you pass the event, you don't own it any more, but the daemon
  331. * does. It will uwb_event_free() it when done, so make sure you
  332. * uwb_event_alloc()ed it or bad things will happen.
  333. *
  334. * If the daemon is not running, we just free the event.
  335. */
  336. void uwbd_event_queue(struct uwb_event *evt)
  337. {
  338. unsigned long flags;
  339. spin_lock_irqsave(&uwbd_event_list_lock, flags);
  340. if (uwbd_pid != 0) {
  341. list_add(&evt->list_node, &uwbd_event_list);
  342. wake_up_all(&uwbd_wq);
  343. } else {
  344. __uwb_rc_put(evt->rc);
  345. if (evt->type == UWB_EVT_TYPE_NOTIF)
  346. kfree(evt->notif.rceb);
  347. kfree(evt);
  348. }
  349. spin_unlock_irqrestore(&uwbd_event_list_lock, flags);
  350. return;
  351. }
  352. void uwbd_flush(struct uwb_rc *rc)
  353. {
  354. struct uwb_event *evt, *nxt;
  355. mutex_lock(&uwbd_event_mutex);
  356. spin_lock_irq(&uwbd_event_list_lock);
  357. list_for_each_entry_safe(evt, nxt, &uwbd_event_list, list_node) {
  358. if (evt->rc == rc) {
  359. __uwb_rc_put(rc);
  360. list_del(&evt->list_node);
  361. if (evt->type == UWB_EVT_TYPE_NOTIF)
  362. kfree(evt->notif.rceb);
  363. kfree(evt);
  364. }
  365. }
  366. spin_unlock_irq(&uwbd_event_list_lock);
  367. mutex_unlock(&uwbd_event_mutex);
  368. }