beacon.c 16 KB

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