beacon.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Ultra Wide Band
  3. * Beacon management
  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. * FIXME: docs
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/device.h>
  29. #include <linux/err.h>
  30. #include <linux/kdev_t.h>
  31. #include "uwb-internal.h"
  32. #define D_LOCAL 0
  33. #include <linux/uwb/debug.h>
  34. /** Start Beaconing command structure */
  35. struct uwb_rc_cmd_start_beacon {
  36. struct uwb_rccb rccb;
  37. __le16 wBPSTOffset;
  38. u8 bChannelNumber;
  39. } __attribute__((packed));
  40. static int uwb_rc_start_beacon(struct uwb_rc *rc, u16 bpst_offset, u8 channel)
  41. {
  42. int result;
  43. struct uwb_rc_cmd_start_beacon *cmd;
  44. struct uwb_rc_evt_confirm reply;
  45. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  46. if (cmd == NULL)
  47. return -ENOMEM;
  48. cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
  49. cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_START_BEACON);
  50. cmd->wBPSTOffset = cpu_to_le16(bpst_offset);
  51. cmd->bChannelNumber = channel;
  52. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  53. reply.rceb.wEvent = UWB_RC_CMD_START_BEACON;
  54. result = uwb_rc_cmd(rc, "START-BEACON", &cmd->rccb, sizeof(*cmd),
  55. &reply.rceb, sizeof(reply));
  56. if (result < 0)
  57. goto error_cmd;
  58. if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  59. dev_err(&rc->uwb_dev.dev,
  60. "START-BEACON: command execution failed: %s (%d)\n",
  61. uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  62. result = -EIO;
  63. }
  64. error_cmd:
  65. kfree(cmd);
  66. return result;
  67. }
  68. static int uwb_rc_stop_beacon(struct uwb_rc *rc)
  69. {
  70. int result;
  71. struct uwb_rccb *cmd;
  72. struct uwb_rc_evt_confirm reply;
  73. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  74. if (cmd == NULL)
  75. return -ENOMEM;
  76. cmd->bCommandType = UWB_RC_CET_GENERAL;
  77. cmd->wCommand = cpu_to_le16(UWB_RC_CMD_STOP_BEACON);
  78. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  79. reply.rceb.wEvent = UWB_RC_CMD_STOP_BEACON;
  80. result = uwb_rc_cmd(rc, "STOP-BEACON", cmd, sizeof(*cmd),
  81. &reply.rceb, sizeof(reply));
  82. if (result < 0)
  83. goto error_cmd;
  84. if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  85. dev_err(&rc->uwb_dev.dev,
  86. "STOP-BEACON: command execution failed: %s (%d)\n",
  87. uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  88. result = -EIO;
  89. }
  90. error_cmd:
  91. kfree(cmd);
  92. return result;
  93. }
  94. /*
  95. * Start/stop beacons
  96. *
  97. * @rc: UWB Radio Controller to operate on
  98. * @channel: UWB channel on which to beacon (WUSB[table
  99. * 5-12]). If -1, stop beaconing.
  100. * @bpst_offset: Beacon Period Start Time offset; FIXME-do zero
  101. *
  102. * According to WHCI 0.95 [4.13.6] the driver will only receive the RCEB
  103. * of a SET IE command after the device sent the first beacon that includes
  104. * the IEs specified in the SET IE command. So, after we start beaconing we
  105. * check if there is anything in the IE cache and call the SET IE command
  106. * if needed.
  107. */
  108. int uwb_rc_beacon(struct uwb_rc *rc, int channel, unsigned bpst_offset)
  109. {
  110. int result;
  111. struct device *dev = &rc->uwb_dev.dev;
  112. mutex_lock(&rc->uwb_dev.mutex);
  113. if (channel < 0)
  114. channel = -1;
  115. if (channel == -1)
  116. result = uwb_rc_stop_beacon(rc);
  117. else {
  118. /* channel >= 0...dah */
  119. result = uwb_rc_start_beacon(rc, bpst_offset, channel);
  120. if (result < 0)
  121. goto out_up;
  122. if (le16_to_cpu(rc->ies->wIELength) > 0) {
  123. result = uwb_rc_set_ie(rc, rc->ies);
  124. if (result < 0) {
  125. dev_err(dev, "Cannot set new IE on device: "
  126. "%d\n", result);
  127. result = uwb_rc_stop_beacon(rc);
  128. channel = -1;
  129. bpst_offset = 0;
  130. } else
  131. result = 0;
  132. }
  133. }
  134. if (result < 0)
  135. goto out_up;
  136. rc->beaconing = channel;
  137. uwb_notify(rc, NULL, uwb_bg_joined(rc) ? UWB_NOTIF_BG_JOIN : UWB_NOTIF_BG_LEAVE);
  138. out_up:
  139. mutex_unlock(&rc->uwb_dev.mutex);
  140. return result;
  141. }
  142. /*
  143. * Beacon cache
  144. *
  145. * The purpose of this is to speed up the lookup of becon information
  146. * when a new beacon arrives. The UWB Daemon uses it also to keep a
  147. * tab of which devices are in radio distance and which not. When a
  148. * device's beacon stays present for more than a certain amount of
  149. * time, it is considered a new, usable device. When a beacon ceases
  150. * to be received for a certain amount of time, it is considered that
  151. * the device is gone.
  152. *
  153. * FIXME: use an allocator for the entries
  154. * FIXME: use something faster for search than a list
  155. */
  156. struct uwb_beca uwb_beca = {
  157. .list = LIST_HEAD_INIT(uwb_beca.list),
  158. .mutex = __MUTEX_INITIALIZER(uwb_beca.mutex)
  159. };
  160. void uwb_bce_kfree(struct kref *_bce)
  161. {
  162. struct uwb_beca_e *bce = container_of(_bce, struct uwb_beca_e, refcnt);
  163. kfree(bce->be);
  164. kfree(bce);
  165. }
  166. /* Find a beacon by dev addr in the cache */
  167. static
  168. struct uwb_beca_e *__uwb_beca_find_bydev(const struct uwb_dev_addr *dev_addr)
  169. {
  170. struct uwb_beca_e *bce, *next;
  171. list_for_each_entry_safe(bce, next, &uwb_beca.list, node) {
  172. d_printf(6, NULL, "looking for addr %02x:%02x in %02x:%02x\n",
  173. dev_addr->data[0], dev_addr->data[1],
  174. bce->dev_addr.data[0], bce->dev_addr.data[1]);
  175. if (!memcmp(&bce->dev_addr, dev_addr, sizeof(bce->dev_addr)))
  176. goto out;
  177. }
  178. bce = NULL;
  179. out:
  180. return bce;
  181. }
  182. /* Find a beacon by dev addr in the cache */
  183. static
  184. struct uwb_beca_e *__uwb_beca_find_bymac(const struct uwb_mac_addr *mac_addr)
  185. {
  186. struct uwb_beca_e *bce, *next;
  187. list_for_each_entry_safe(bce, next, &uwb_beca.list, node) {
  188. if (!memcmp(bce->mac_addr, mac_addr->data,
  189. sizeof(struct uwb_mac_addr)))
  190. goto out;
  191. }
  192. bce = NULL;
  193. out:
  194. return bce;
  195. }
  196. /**
  197. * uwb_dev_get_by_devaddr - get a UWB device with a specific DevAddr
  198. * @rc: the radio controller that saw the device
  199. * @devaddr: DevAddr of the UWB device to find
  200. *
  201. * There may be more than one matching device (in the case of a
  202. * DevAddr conflict), but only the first one is returned.
  203. */
  204. struct uwb_dev *uwb_dev_get_by_devaddr(struct uwb_rc *rc,
  205. const struct uwb_dev_addr *devaddr)
  206. {
  207. struct uwb_dev *found = NULL;
  208. struct uwb_beca_e *bce;
  209. mutex_lock(&uwb_beca.mutex);
  210. bce = __uwb_beca_find_bydev(devaddr);
  211. if (bce)
  212. found = uwb_dev_try_get(rc, bce->uwb_dev);
  213. mutex_unlock(&uwb_beca.mutex);
  214. return found;
  215. }
  216. /**
  217. * uwb_dev_get_by_macaddr - get a UWB device with a specific EUI-48
  218. * @rc: the radio controller that saw the device
  219. * @devaddr: EUI-48 of the UWB device to find
  220. */
  221. struct uwb_dev *uwb_dev_get_by_macaddr(struct uwb_rc *rc,
  222. const struct uwb_mac_addr *macaddr)
  223. {
  224. struct uwb_dev *found = NULL;
  225. struct uwb_beca_e *bce;
  226. mutex_lock(&uwb_beca.mutex);
  227. bce = __uwb_beca_find_bymac(macaddr);
  228. if (bce)
  229. found = uwb_dev_try_get(rc, bce->uwb_dev);
  230. mutex_unlock(&uwb_beca.mutex);
  231. return found;
  232. }
  233. /* Initialize a beacon cache entry */
  234. static void uwb_beca_e_init(struct uwb_beca_e *bce)
  235. {
  236. mutex_init(&bce->mutex);
  237. kref_init(&bce->refcnt);
  238. stats_init(&bce->lqe_stats);
  239. stats_init(&bce->rssi_stats);
  240. }
  241. /*
  242. * Add a beacon to the cache
  243. *
  244. * @be: Beacon event information
  245. * @bf: Beacon frame (part of b, really)
  246. * @ts_jiffies: Timestamp (in jiffies) when the beacon was received
  247. */
  248. struct uwb_beca_e *__uwb_beca_add(struct uwb_rc_evt_beacon *be,
  249. struct uwb_beacon_frame *bf,
  250. unsigned long ts_jiffies)
  251. {
  252. struct uwb_beca_e *bce;
  253. bce = kzalloc(sizeof(*bce), GFP_KERNEL);
  254. if (bce == NULL)
  255. return NULL;
  256. uwb_beca_e_init(bce);
  257. bce->ts_jiffies = ts_jiffies;
  258. bce->uwb_dev = NULL;
  259. list_add(&bce->node, &uwb_beca.list);
  260. return bce;
  261. }
  262. /*
  263. * Wipe out beacon entries that became stale
  264. *
  265. * Remove associated devicest too.
  266. */
  267. void uwb_beca_purge(void)
  268. {
  269. struct uwb_beca_e *bce, *next;
  270. unsigned long expires;
  271. mutex_lock(&uwb_beca.mutex);
  272. list_for_each_entry_safe(bce, next, &uwb_beca.list, node) {
  273. expires = bce->ts_jiffies + msecs_to_jiffies(beacon_timeout_ms);
  274. if (time_after(jiffies, expires)) {
  275. uwbd_dev_offair(bce);
  276. list_del(&bce->node);
  277. uwb_bce_put(bce);
  278. }
  279. }
  280. mutex_unlock(&uwb_beca.mutex);
  281. }
  282. /* Clean up the whole beacon cache. Called on shutdown */
  283. void uwb_beca_release(void)
  284. {
  285. struct uwb_beca_e *bce, *next;
  286. mutex_lock(&uwb_beca.mutex);
  287. list_for_each_entry_safe(bce, next, &uwb_beca.list, node) {
  288. list_del(&bce->node);
  289. uwb_bce_put(bce);
  290. }
  291. mutex_unlock(&uwb_beca.mutex);
  292. }
  293. static void uwb_beacon_print(struct uwb_rc *rc, struct uwb_rc_evt_beacon *be,
  294. struct uwb_beacon_frame *bf)
  295. {
  296. char macbuf[UWB_ADDR_STRSIZE];
  297. char devbuf[UWB_ADDR_STRSIZE];
  298. char dstbuf[UWB_ADDR_STRSIZE];
  299. uwb_mac_addr_print(macbuf, sizeof(macbuf), &bf->Device_Identifier);
  300. uwb_dev_addr_print(devbuf, sizeof(devbuf), &bf->hdr.SrcAddr);
  301. uwb_dev_addr_print(dstbuf, sizeof(dstbuf), &bf->hdr.DestAddr);
  302. dev_info(&rc->uwb_dev.dev,
  303. "BEACON from %s to %s (ch%u offset %u slot %u MAC %s)\n",
  304. devbuf, dstbuf, be->bChannelNumber, be->wBPSTOffset,
  305. bf->Beacon_Slot_Number, macbuf);
  306. }
  307. /*
  308. * @bce: beacon cache entry, referenced
  309. */
  310. ssize_t uwb_bce_print_IEs(struct uwb_dev *uwb_dev, struct uwb_beca_e *bce,
  311. char *buf, size_t size)
  312. {
  313. ssize_t result = 0;
  314. struct uwb_rc_evt_beacon *be;
  315. struct uwb_beacon_frame *bf;
  316. struct uwb_buf_ctx ctx = {
  317. .buf = buf,
  318. .bytes = 0,
  319. .size = size
  320. };
  321. mutex_lock(&bce->mutex);
  322. be = bce->be;
  323. if (be == NULL)
  324. goto out;
  325. bf = (void *) be->BeaconInfo;
  326. uwb_ie_for_each(uwb_dev, uwb_ie_dump_hex, &ctx,
  327. bf->IEData, be->wBeaconInfoLength - sizeof(*bf));
  328. result = ctx.bytes;
  329. out:
  330. mutex_unlock(&bce->mutex);
  331. return result;
  332. }
  333. /*
  334. * Verify that the beacon event, frame and IEs are ok
  335. */
  336. static int uwb_verify_beacon(struct uwb_rc *rc, struct uwb_event *evt,
  337. struct uwb_rc_evt_beacon *be)
  338. {
  339. int result = -EINVAL;
  340. struct uwb_beacon_frame *bf;
  341. struct device *dev = &rc->uwb_dev.dev;
  342. /* Is there enough data to decode a beacon frame? */
  343. if (evt->notif.size < sizeof(*be) + sizeof(*bf)) {
  344. dev_err(dev, "BEACON event: Not enough data to decode "
  345. "(%zu vs %zu bytes needed)\n", evt->notif.size,
  346. sizeof(*be) + sizeof(*bf));
  347. goto error;
  348. }
  349. /* FIXME: make sure beacon frame IEs are fine and that the whole thing
  350. * is consistent */
  351. result = 0;
  352. error:
  353. return result;
  354. }
  355. /*
  356. * Handle UWB_RC_EVT_BEACON events
  357. *
  358. * We check the beacon cache to see how the received beacon fares. If
  359. * is there already we refresh the timestamp. If not we create a new
  360. * entry.
  361. *
  362. * According to the WHCI and WUSB specs, only one beacon frame is
  363. * allowed per notification block, so we don't bother about scanning
  364. * for more.
  365. */
  366. int uwbd_evt_handle_rc_beacon(struct uwb_event *evt)
  367. {
  368. int result = -EINVAL;
  369. struct uwb_rc *rc;
  370. struct uwb_rc_evt_beacon *be;
  371. struct uwb_beacon_frame *bf;
  372. struct uwb_beca_e *bce;
  373. unsigned long last_ts;
  374. rc = evt->rc;
  375. be = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon, rceb);
  376. result = uwb_verify_beacon(rc, evt, be);
  377. if (result < 0)
  378. return result;
  379. /* FIXME: handle alien beacons. */
  380. if (be->bBeaconType == UWB_RC_BEACON_TYPE_OL_ALIEN ||
  381. be->bBeaconType == UWB_RC_BEACON_TYPE_NOL_ALIEN) {
  382. return -ENOSYS;
  383. }
  384. bf = (struct uwb_beacon_frame *) be->BeaconInfo;
  385. /*
  386. * Drop beacons from devices with a NULL EUI-48 -- they cannot
  387. * be uniquely identified.
  388. *
  389. * It's expected that these will all be WUSB devices and they
  390. * have a WUSB specific connection method so ignoring them
  391. * here shouldn't be a problem.
  392. */
  393. if (uwb_mac_addr_bcast(&bf->Device_Identifier))
  394. return 0;
  395. mutex_lock(&uwb_beca.mutex);
  396. bce = __uwb_beca_find_bymac(&bf->Device_Identifier);
  397. if (bce == NULL) {
  398. /* Not in there, a new device is pinging */
  399. uwb_beacon_print(evt->rc, be, bf);
  400. bce = __uwb_beca_add(be, bf, evt->ts_jiffies);
  401. if (bce == NULL) {
  402. mutex_unlock(&uwb_beca.mutex);
  403. return -ENOMEM;
  404. }
  405. }
  406. mutex_unlock(&uwb_beca.mutex);
  407. mutex_lock(&bce->mutex);
  408. /* purge old beacon data */
  409. kfree(bce->be);
  410. last_ts = bce->ts_jiffies;
  411. /* Update commonly used fields */
  412. bce->ts_jiffies = evt->ts_jiffies;
  413. bce->be = be;
  414. bce->dev_addr = bf->hdr.SrcAddr;
  415. bce->mac_addr = &bf->Device_Identifier;
  416. be->wBPSTOffset = le16_to_cpu(be->wBPSTOffset);
  417. be->wBeaconInfoLength = le16_to_cpu(be->wBeaconInfoLength);
  418. stats_add_sample(&bce->lqe_stats, be->bLQI - 7);
  419. stats_add_sample(&bce->rssi_stats, be->bRSSI + 18);
  420. /*
  421. * This might be a beacon from a new device.
  422. */
  423. if (bce->uwb_dev == NULL)
  424. uwbd_dev_onair(evt->rc, bce);
  425. mutex_unlock(&bce->mutex);
  426. return 1; /* we keep the event data */
  427. }
  428. /*
  429. * Handle UWB_RC_EVT_BEACON_SIZE events
  430. *
  431. * XXXXX
  432. */
  433. int uwbd_evt_handle_rc_beacon_size(struct uwb_event *evt)
  434. {
  435. int result = -EINVAL;
  436. struct device *dev = &evt->rc->uwb_dev.dev;
  437. struct uwb_rc_evt_beacon_size *bs;
  438. /* Is there enough data to decode the event? */
  439. if (evt->notif.size < sizeof(*bs)) {
  440. dev_err(dev, "BEACON SIZE notification: Not enough data to "
  441. "decode (%zu vs %zu bytes needed)\n",
  442. evt->notif.size, sizeof(*bs));
  443. goto error;
  444. }
  445. bs = container_of(evt->notif.rceb, struct uwb_rc_evt_beacon_size, rceb);
  446. if (0)
  447. dev_info(dev, "Beacon size changed to %u bytes "
  448. "(FIXME: action?)\n", le16_to_cpu(bs->wNewBeaconSize));
  449. else {
  450. /* temporary hack until we do something with this message... */
  451. static unsigned count;
  452. if (++count % 1000 == 0)
  453. dev_info(dev, "Beacon size changed %u times "
  454. "(FIXME: action?)\n", count);
  455. }
  456. result = 0;
  457. error:
  458. return result;
  459. }
  460. /**
  461. * uwbd_evt_handle_rc_bp_slot_change - handle a BP_SLOT_CHANGE event
  462. * @evt: the BP_SLOT_CHANGE notification from the radio controller
  463. *
  464. * If the event indicates that no beacon period slots were available
  465. * then radio controller has transitioned to a non-beaconing state.
  466. * Otherwise, simply save the current beacon slot.
  467. */
  468. int uwbd_evt_handle_rc_bp_slot_change(struct uwb_event *evt)
  469. {
  470. struct uwb_rc *rc = evt->rc;
  471. struct device *dev = &rc->uwb_dev.dev;
  472. struct uwb_rc_evt_bp_slot_change *bpsc;
  473. if (evt->notif.size < sizeof(*bpsc)) {
  474. dev_err(dev, "BP SLOT CHANGE event: Not enough data\n");
  475. return -EINVAL;
  476. }
  477. bpsc = container_of(evt->notif.rceb, struct uwb_rc_evt_bp_slot_change, rceb);
  478. mutex_lock(&rc->uwb_dev.mutex);
  479. if (uwb_rc_evt_bp_slot_change_no_slot(bpsc)) {
  480. dev_info(dev, "stopped beaconing: No free slots in BP\n");
  481. rc->beaconing = -1;
  482. } else
  483. rc->uwb_dev.beacon_slot = uwb_rc_evt_bp_slot_change_slot_num(bpsc);
  484. mutex_unlock(&rc->uwb_dev.mutex);
  485. return 0;
  486. }
  487. /**
  488. * Handle UWB_RC_EVT_BPOIE_CHANGE events
  489. *
  490. * XXXXX
  491. */
  492. struct uwb_ie_bpo {
  493. struct uwb_ie_hdr hdr;
  494. u8 bp_length;
  495. u8 data[];
  496. } __attribute__((packed));
  497. int uwbd_evt_handle_rc_bpoie_change(struct uwb_event *evt)
  498. {
  499. int result = -EINVAL;
  500. struct device *dev = &evt->rc->uwb_dev.dev;
  501. struct uwb_rc_evt_bpoie_change *bpoiec;
  502. struct uwb_ie_bpo *bpoie;
  503. static unsigned count; /* FIXME: this is a temp hack */
  504. size_t iesize;
  505. /* Is there enough data to decode it? */
  506. if (evt->notif.size < sizeof(*bpoiec)) {
  507. dev_err(dev, "BPOIEC notification: Not enough data to "
  508. "decode (%zu vs %zu bytes needed)\n",
  509. evt->notif.size, sizeof(*bpoiec));
  510. goto error;
  511. }
  512. bpoiec = container_of(evt->notif.rceb, struct uwb_rc_evt_bpoie_change, rceb);
  513. iesize = le16_to_cpu(bpoiec->wBPOIELength);
  514. if (iesize < sizeof(*bpoie)) {
  515. dev_err(dev, "BPOIEC notification: Not enough IE data to "
  516. "decode (%zu vs %zu bytes needed)\n",
  517. iesize, sizeof(*bpoie));
  518. goto error;
  519. }
  520. if (++count % 1000 == 0) /* Lame placeholder */
  521. dev_info(dev, "BPOIE: %u changes received\n", count);
  522. /*
  523. * FIXME: At this point we should go over all the IEs in the
  524. * bpoiec->BPOIE array and act on each.
  525. */
  526. result = 0;
  527. error:
  528. return result;
  529. }
  530. /**
  531. * uwb_bg_joined - is the RC in a beacon group?
  532. * @rc: the radio controller
  533. *
  534. * Returns true if the radio controller is in a beacon group (even if
  535. * it's the sole member).
  536. */
  537. int uwb_bg_joined(struct uwb_rc *rc)
  538. {
  539. return rc->beaconing != -1;
  540. }
  541. EXPORT_SYMBOL_GPL(uwb_bg_joined);
  542. /*
  543. * Print beaconing state.
  544. */
  545. static ssize_t uwb_rc_beacon_show(struct device *dev,
  546. struct device_attribute *attr, char *buf)
  547. {
  548. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  549. struct uwb_rc *rc = uwb_dev->rc;
  550. ssize_t result;
  551. mutex_lock(&rc->uwb_dev.mutex);
  552. result = sprintf(buf, "%d\n", rc->beaconing);
  553. mutex_unlock(&rc->uwb_dev.mutex);
  554. return result;
  555. }
  556. /*
  557. * Start beaconing on the specified channel, or stop beaconing.
  558. *
  559. * The BPST offset of when to start searching for a beacon group to
  560. * join may be specified.
  561. */
  562. static ssize_t uwb_rc_beacon_store(struct device *dev,
  563. struct device_attribute *attr,
  564. const char *buf, size_t size)
  565. {
  566. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  567. struct uwb_rc *rc = uwb_dev->rc;
  568. int channel;
  569. unsigned bpst_offset = 0;
  570. ssize_t result = -EINVAL;
  571. result = sscanf(buf, "%d %u\n", &channel, &bpst_offset);
  572. if (result >= 1)
  573. result = uwb_rc_beacon(rc, channel, bpst_offset);
  574. return result < 0 ? result : size;
  575. }
  576. DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, uwb_rc_beacon_show, uwb_rc_beacon_store);