uwbd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_IE_RCV] = {
  102. .handler = uwbd_evt_handle_rc_ie_rcv,
  103. .name = "IE_RECEIVED"
  104. },
  105. [UWB_RC_EVT_BEACON] = {
  106. .handler = uwbd_evt_handle_rc_beacon,
  107. .name = "BEACON_RECEIVED"
  108. },
  109. [UWB_RC_EVT_BEACON_SIZE] = {
  110. .handler = uwbd_evt_handle_rc_beacon_size,
  111. .name = "BEACON_SIZE_CHANGE"
  112. },
  113. [UWB_RC_EVT_BPOIE_CHANGE] = {
  114. .handler = uwbd_evt_handle_rc_bpoie_change,
  115. .name = "BPOIE_CHANGE"
  116. },
  117. [UWB_RC_EVT_BP_SLOT_CHANGE] = {
  118. .handler = uwbd_evt_handle_rc_bp_slot_change,
  119. .name = "BP_SLOT_CHANGE"
  120. },
  121. [UWB_RC_EVT_DRP_AVAIL] = {
  122. .handler = uwbd_evt_handle_rc_drp_avail,
  123. .name = "DRP_AVAILABILITY_CHANGE"
  124. },
  125. [UWB_RC_EVT_DRP] = {
  126. .handler = uwbd_evt_handle_rc_drp,
  127. .name = "DRP"
  128. },
  129. [UWB_RC_EVT_DEV_ADDR_CONFLICT] = {
  130. .handler = uwbd_evt_handle_rc_dev_addr_conflict,
  131. .name = "DEV_ADDR_CONFLICT",
  132. },
  133. };
  134. struct uwbd_evt_type_handler {
  135. const char *name;
  136. struct uwbd_event *uwbd_events;
  137. size_t size;
  138. };
  139. #define UWBD_EVT_TYPE_HANDLER(n,a) { \
  140. .name = (n), \
  141. .uwbd_events = (a), \
  142. .size = sizeof(a)/sizeof((a)[0]) \
  143. }
  144. /** Table of handlers for each UWBD Event type. */
  145. static
  146. struct uwbd_evt_type_handler uwbd_evt_type_handlers[] = {
  147. [UWB_RC_CET_GENERAL] = UWBD_EVT_TYPE_HANDLER("RC", uwbd_events)
  148. };
  149. static const
  150. size_t uwbd_evt_type_handlers_len =
  151. sizeof(uwbd_evt_type_handlers) / sizeof(uwbd_evt_type_handlers[0]);
  152. static const struct uwbd_event uwbd_message_handlers[] = {
  153. [UWB_EVT_MSG_RESET] = {
  154. .handler = uwbd_msg_handle_reset,
  155. .name = "reset",
  156. },
  157. };
  158. /**
  159. * Handle an URC event passed to the UWB Daemon
  160. *
  161. * @evt: the event to handle
  162. * @returns: 0 if the event can be kfreed, !0 on the contrary
  163. * (somebody else took ownership) [coincidentally, returning
  164. * a <0 errno code will free it :)].
  165. *
  166. * Looks up the two indirection tables (one for the type, one for the
  167. * subtype) to decide which function handles it and then calls the
  168. * handler.
  169. *
  170. * The event structure passed to the event handler has the radio
  171. * controller in @evt->rc referenced. The reference will be dropped
  172. * once the handler returns, so if it needs it for longer (async),
  173. * it'll need to take another one.
  174. */
  175. static
  176. int uwbd_event_handle_urc(struct uwb_event *evt)
  177. {
  178. struct uwbd_evt_type_handler *type_table;
  179. uwbd_evt_handler_f handler;
  180. u8 type, context;
  181. u16 event;
  182. type = evt->notif.rceb->bEventType;
  183. event = le16_to_cpu(evt->notif.rceb->wEvent);
  184. context = evt->notif.rceb->bEventContext;
  185. if (type > uwbd_evt_type_handlers_len) {
  186. printk(KERN_ERR "UWBD: event type %u: unknown (too high)\n", type);
  187. return -EINVAL;
  188. }
  189. type_table = &uwbd_evt_type_handlers[type];
  190. if (type_table->uwbd_events == NULL) {
  191. printk(KERN_ERR "UWBD: event type %u: unknown\n", type);
  192. return -EINVAL;
  193. }
  194. if (event > type_table->size) {
  195. printk(KERN_ERR "UWBD: event %s[%u]: unknown (too high)\n",
  196. type_table->name, event);
  197. return -EINVAL;
  198. }
  199. handler = type_table->uwbd_events[event].handler;
  200. if (handler == NULL) {
  201. printk(KERN_ERR "UWBD: event %s[%u]: unknown\n", type_table->name, event);
  202. return -EINVAL;
  203. }
  204. return (*handler)(evt);
  205. }
  206. static void uwbd_event_handle_message(struct uwb_event *evt)
  207. {
  208. struct uwb_rc *rc;
  209. int result;
  210. rc = evt->rc;
  211. if (evt->message < 0 || evt->message >= ARRAY_SIZE(uwbd_message_handlers)) {
  212. dev_err(&rc->uwb_dev.dev, "UWBD: invalid message type %d\n", evt->message);
  213. return;
  214. }
  215. result = uwbd_message_handlers[evt->message].handler(evt);
  216. if (result < 0)
  217. dev_err(&rc->uwb_dev.dev, "UWBD: '%s' message failed: %d\n",
  218. uwbd_message_handlers[evt->message].name, result);
  219. }
  220. static void uwbd_event_handle(struct uwb_event *evt)
  221. {
  222. struct uwb_rc *rc;
  223. int should_keep;
  224. rc = evt->rc;
  225. if (rc->ready) {
  226. switch (evt->type) {
  227. case UWB_EVT_TYPE_NOTIF:
  228. should_keep = uwbd_event_handle_urc(evt);
  229. if (should_keep <= 0)
  230. kfree(evt->notif.rceb);
  231. break;
  232. case UWB_EVT_TYPE_MSG:
  233. uwbd_event_handle_message(evt);
  234. break;
  235. default:
  236. dev_err(&rc->uwb_dev.dev, "UWBD: invalid event type %d\n", evt->type);
  237. break;
  238. }
  239. }
  240. __uwb_rc_put(rc); /* for the __uwb_rc_get() in uwb_rc_notif_cb() */
  241. }
  242. /**
  243. * UWB Daemon
  244. *
  245. * Listens to all UWB notifications and takes care to track the state
  246. * of the UWB neighboorhood for the kernel. When we do a run, we
  247. * spinlock, move the list to a private copy and release the
  248. * lock. Hold it as little as possible. Not a conflict: it is
  249. * guaranteed we own the events in the private list.
  250. *
  251. * FIXME: should change so we don't have a 1HZ timer all the time, but
  252. * only if there are devices.
  253. */
  254. static int uwbd(void *param)
  255. {
  256. struct uwb_rc *rc = param;
  257. unsigned long flags;
  258. struct uwb_event *evt;
  259. int should_stop = 0;
  260. while (1) {
  261. wait_event_interruptible_timeout(
  262. rc->uwbd.wq,
  263. !list_empty(&rc->uwbd.event_list)
  264. || (should_stop = kthread_should_stop()),
  265. HZ);
  266. if (should_stop)
  267. break;
  268. try_to_freeze();
  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);
  273. } else
  274. evt = NULL;
  275. spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
  276. if (evt) {
  277. uwbd_event_handle(evt);
  278. kfree(evt);
  279. }
  280. uwb_beca_purge(rc); /* Purge devices that left */
  281. }
  282. return 0;
  283. }
  284. /** Start the UWB daemon */
  285. void uwbd_start(struct uwb_rc *rc)
  286. {
  287. rc->uwbd.task = kthread_run(uwbd, rc, "uwbd");
  288. if (rc->uwbd.task == NULL)
  289. printk(KERN_ERR "UWB: Cannot start management daemon; "
  290. "UWB won't work\n");
  291. else
  292. rc->uwbd.pid = rc->uwbd.task->pid;
  293. }
  294. /* Stop the UWB daemon and free any unprocessed events */
  295. void uwbd_stop(struct uwb_rc *rc)
  296. {
  297. kthread_stop(rc->uwbd.task);
  298. uwbd_flush(rc);
  299. }
  300. /*
  301. * Queue an event for the management daemon
  302. *
  303. * When some lower layer receives an event, it uses this function to
  304. * push it forward to the UWB daemon.
  305. *
  306. * Once you pass the event, you don't own it any more, but the daemon
  307. * does. It will uwb_event_free() it when done, so make sure you
  308. * uwb_event_alloc()ed it or bad things will happen.
  309. *
  310. * If the daemon is not running, we just free the event.
  311. */
  312. void uwbd_event_queue(struct uwb_event *evt)
  313. {
  314. struct uwb_rc *rc = evt->rc;
  315. unsigned long flags;
  316. spin_lock_irqsave(&rc->uwbd.event_list_lock, flags);
  317. if (rc->uwbd.pid != 0) {
  318. list_add(&evt->list_node, &rc->uwbd.event_list);
  319. wake_up_all(&rc->uwbd.wq);
  320. } else {
  321. __uwb_rc_put(evt->rc);
  322. if (evt->type == UWB_EVT_TYPE_NOTIF)
  323. kfree(evt->notif.rceb);
  324. kfree(evt);
  325. }
  326. spin_unlock_irqrestore(&rc->uwbd.event_list_lock, flags);
  327. return;
  328. }
  329. void uwbd_flush(struct uwb_rc *rc)
  330. {
  331. struct uwb_event *evt, *nxt;
  332. spin_lock_irq(&rc->uwbd.event_list_lock);
  333. list_for_each_entry_safe(evt, nxt, &rc->uwbd.event_list, list_node) {
  334. if (evt->rc == rc) {
  335. __uwb_rc_put(rc);
  336. list_del(&evt->list_node);
  337. if (evt->type == UWB_EVT_TYPE_NOTIF)
  338. kfree(evt->notif.rceb);
  339. kfree(evt);
  340. }
  341. }
  342. spin_unlock_irq(&rc->uwbd.event_list_lock);
  343. }