uwbd.c 10 KB

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