beacon.c 16 KB

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