lc-rc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Ultra Wide Band
  3. * Life cycle of radio controllers
  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. * A UWB radio controller is also a UWB device, so it embeds one...
  26. *
  27. * List of RCs comes from the 'struct class uwb_rc_class'.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/device.h>
  32. #include <linux/err.h>
  33. #include <linux/random.h>
  34. #include <linux/kdev_t.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/usb.h>
  37. #include <linux/slab.h>
  38. #include <linux/export.h>
  39. #include "uwb-internal.h"
  40. static int uwb_rc_index_match(struct device *dev, const void *data)
  41. {
  42. const int *index = data;
  43. struct uwb_rc *rc = dev_get_drvdata(dev);
  44. if (rc->index == *index)
  45. return 1;
  46. return 0;
  47. }
  48. static struct uwb_rc *uwb_rc_find_by_index(int index)
  49. {
  50. struct device *dev;
  51. struct uwb_rc *rc = NULL;
  52. dev = class_find_device(&uwb_rc_class, NULL, &index, uwb_rc_index_match);
  53. if (dev) {
  54. rc = dev_get_drvdata(dev);
  55. put_device(dev);
  56. }
  57. return rc;
  58. }
  59. static int uwb_rc_new_index(void)
  60. {
  61. int index = 0;
  62. for (;;) {
  63. if (!uwb_rc_find_by_index(index))
  64. return index;
  65. if (++index < 0)
  66. index = 0;
  67. }
  68. }
  69. /**
  70. * Release the backing device of a uwb_rc that has been dynamically allocated.
  71. */
  72. static void uwb_rc_sys_release(struct device *dev)
  73. {
  74. struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev);
  75. struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev);
  76. uwb_rc_ie_release(rc);
  77. kfree(rc);
  78. }
  79. void uwb_rc_init(struct uwb_rc *rc)
  80. {
  81. struct uwb_dev *uwb_dev = &rc->uwb_dev;
  82. uwb_dev_init(uwb_dev);
  83. rc->uwb_dev.dev.class = &uwb_rc_class;
  84. rc->uwb_dev.dev.release = uwb_rc_sys_release;
  85. uwb_rc_neh_create(rc);
  86. rc->beaconing = -1;
  87. rc->scan_type = UWB_SCAN_DISABLED;
  88. INIT_LIST_HEAD(&rc->notifs_chain.list);
  89. mutex_init(&rc->notifs_chain.mutex);
  90. INIT_LIST_HEAD(&rc->uwb_beca.list);
  91. mutex_init(&rc->uwb_beca.mutex);
  92. uwb_drp_avail_init(rc);
  93. uwb_rc_ie_init(rc);
  94. uwb_rsv_init(rc);
  95. uwb_rc_pal_init(rc);
  96. }
  97. EXPORT_SYMBOL_GPL(uwb_rc_init);
  98. struct uwb_rc *uwb_rc_alloc(void)
  99. {
  100. struct uwb_rc *rc;
  101. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  102. if (rc == NULL)
  103. return NULL;
  104. uwb_rc_init(rc);
  105. return rc;
  106. }
  107. EXPORT_SYMBOL_GPL(uwb_rc_alloc);
  108. /*
  109. * Show the ASIE that is broadcast in the UWB beacon by this uwb_rc device.
  110. */
  111. static ssize_t ASIE_show(struct device *dev,
  112. struct device_attribute *attr, char *buf)
  113. {
  114. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  115. struct uwb_rc *rc = uwb_dev->rc;
  116. struct uwb_ie_hdr *ie;
  117. void *ptr;
  118. size_t len;
  119. int result = 0;
  120. /* init empty buffer. */
  121. result = scnprintf(buf, PAGE_SIZE, "\n");
  122. mutex_lock(&rc->ies_mutex);
  123. /* walk IEData looking for an ASIE. */
  124. ptr = rc->ies->IEData;
  125. len = le16_to_cpu(rc->ies->wIELength);
  126. for (;;) {
  127. ie = uwb_ie_next(&ptr, &len);
  128. if (!ie)
  129. break;
  130. if (ie->element_id == UWB_APP_SPEC_IE) {
  131. result = uwb_ie_dump_hex(ie,
  132. ie->length + sizeof(struct uwb_ie_hdr),
  133. buf, PAGE_SIZE);
  134. break;
  135. }
  136. }
  137. mutex_unlock(&rc->ies_mutex);
  138. return result;
  139. }
  140. /*
  141. * Update the ASIE that is broadcast in the UWB beacon by this uwb_rc device.
  142. */
  143. static ssize_t ASIE_store(struct device *dev,
  144. struct device_attribute *attr,
  145. const char *buf, size_t size)
  146. {
  147. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  148. struct uwb_rc *rc = uwb_dev->rc;
  149. char ie_buf[255];
  150. int result, ie_len = 0;
  151. const char *cur_ptr = buf;
  152. struct uwb_ie_hdr *ie;
  153. /* empty string means clear the ASIE. */
  154. if (strlen(buf) <= 1) {
  155. uwb_rc_ie_rm(rc, UWB_APP_SPEC_IE);
  156. return size;
  157. }
  158. /* if non-empty string, convert string of hex chars to binary. */
  159. while (ie_len < sizeof(ie_buf)) {
  160. int char_count;
  161. if (sscanf(cur_ptr, " %02hhX %n",
  162. &(ie_buf[ie_len]), &char_count) > 0) {
  163. ++ie_len;
  164. /* skip chars read from cur_ptr. */
  165. cur_ptr += char_count;
  166. } else {
  167. break;
  168. }
  169. }
  170. /* validate IE length and type. */
  171. if (ie_len < sizeof(struct uwb_ie_hdr)) {
  172. dev_err(dev, "%s: Invalid ASIE size %d.\n", __func__, ie_len);
  173. return -EINVAL;
  174. }
  175. ie = (struct uwb_ie_hdr *)ie_buf;
  176. if (ie->element_id != UWB_APP_SPEC_IE) {
  177. dev_err(dev, "%s: Invalid IE element type size = 0x%02X.\n",
  178. __func__, ie->element_id);
  179. return -EINVAL;
  180. }
  181. /* bounds check length field from user. */
  182. if (ie->length > (ie_len - sizeof(struct uwb_ie_hdr)))
  183. ie->length = ie_len - sizeof(struct uwb_ie_hdr);
  184. /*
  185. * Valid ASIE received. Remove current ASIE then add the new one using
  186. * uwb_rc_ie_add.
  187. */
  188. uwb_rc_ie_rm(rc, UWB_APP_SPEC_IE);
  189. result = uwb_rc_ie_add(rc, ie, ie->length + sizeof(struct uwb_ie_hdr));
  190. return result >= 0 ? size : result;
  191. }
  192. static DEVICE_ATTR_RW(ASIE);
  193. static struct attribute *rc_attrs[] = {
  194. &dev_attr_mac_address.attr,
  195. &dev_attr_scan.attr,
  196. &dev_attr_beacon.attr,
  197. &dev_attr_ASIE.attr,
  198. NULL,
  199. };
  200. static struct attribute_group rc_attr_group = {
  201. .attrs = rc_attrs,
  202. };
  203. /*
  204. * Registration of sysfs specific stuff
  205. */
  206. static int uwb_rc_sys_add(struct uwb_rc *rc)
  207. {
  208. return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
  209. }
  210. static void __uwb_rc_sys_rm(struct uwb_rc *rc)
  211. {
  212. sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
  213. }
  214. /**
  215. * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
  216. * @rc: the radio controller.
  217. *
  218. * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
  219. * then a random locally administered EUI-48 is generated and set on
  220. * the device. The probability of address collisions is sufficiently
  221. * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
  222. */
  223. static
  224. int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
  225. {
  226. int result;
  227. struct device *dev = &rc->uwb_dev.dev;
  228. struct uwb_dev *uwb_dev = &rc->uwb_dev;
  229. char devname[UWB_ADDR_STRSIZE];
  230. struct uwb_mac_addr addr;
  231. result = uwb_rc_mac_addr_get(rc, &addr);
  232. if (result < 0) {
  233. dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
  234. return result;
  235. }
  236. if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) {
  237. addr.data[0] = 0x02; /* locally administered and unicast */
  238. get_random_bytes(&addr.data[1], sizeof(addr.data)-1);
  239. result = uwb_rc_mac_addr_set(rc, &addr);
  240. if (result < 0) {
  241. uwb_mac_addr_print(devname, sizeof(devname), &addr);
  242. dev_err(dev, "cannot set EUI-48 address %s: %d\n",
  243. devname, result);
  244. return result;
  245. }
  246. }
  247. uwb_dev->mac_addr = addr;
  248. return 0;
  249. }
  250. static int uwb_rc_setup(struct uwb_rc *rc)
  251. {
  252. int result;
  253. struct device *dev = &rc->uwb_dev.dev;
  254. result = uwb_radio_setup(rc);
  255. if (result < 0) {
  256. dev_err(dev, "cannot setup UWB radio: %d\n", result);
  257. goto error;
  258. }
  259. result = uwb_rc_mac_addr_setup(rc);
  260. if (result < 0) {
  261. dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
  262. goto error;
  263. }
  264. result = uwb_rc_dev_addr_assign(rc);
  265. if (result < 0) {
  266. dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
  267. goto error;
  268. }
  269. result = uwb_rc_ie_setup(rc);
  270. if (result < 0) {
  271. dev_err(dev, "cannot setup IE subsystem: %d\n", result);
  272. goto error_ie_setup;
  273. }
  274. result = uwb_rsv_setup(rc);
  275. if (result < 0) {
  276. dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
  277. goto error_rsv_setup;
  278. }
  279. uwb_dbg_add_rc(rc);
  280. return 0;
  281. error_rsv_setup:
  282. uwb_rc_ie_release(rc);
  283. error_ie_setup:
  284. error:
  285. return result;
  286. }
  287. /**
  288. * Register a new UWB radio controller
  289. *
  290. * Did you call uwb_rc_init() on your rc?
  291. *
  292. * We assume that this is being called with a > 0 refcount on
  293. * it [through ops->{get|put}_device(). We'll take our own, though.
  294. *
  295. * @parent_dev is our real device, the one that provides the actual UWB device
  296. */
  297. int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
  298. {
  299. int result;
  300. struct device *dev;
  301. char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
  302. rc->index = uwb_rc_new_index();
  303. dev = &rc->uwb_dev.dev;
  304. dev_set_name(dev, "uwb%d", rc->index);
  305. rc->priv = priv;
  306. init_waitqueue_head(&rc->uwbd.wq);
  307. INIT_LIST_HEAD(&rc->uwbd.event_list);
  308. spin_lock_init(&rc->uwbd.event_list_lock);
  309. uwbd_start(rc);
  310. result = rc->start(rc);
  311. if (result < 0)
  312. goto error_rc_start;
  313. result = uwb_rc_setup(rc);
  314. if (result < 0) {
  315. dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
  316. goto error_rc_setup;
  317. }
  318. result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
  319. if (result < 0 && result != -EADDRNOTAVAIL)
  320. goto error_dev_add;
  321. result = uwb_rc_sys_add(rc);
  322. if (result < 0) {
  323. dev_err(parent_dev, "cannot register UWB radio controller "
  324. "dev attributes: %d\n", result);
  325. goto error_sys_add;
  326. }
  327. uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
  328. uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
  329. dev_info(dev,
  330. "new uwb radio controller (mac %s dev %s) on %s %s\n",
  331. macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
  332. rc->ready = 1;
  333. return 0;
  334. error_sys_add:
  335. uwb_dev_rm(&rc->uwb_dev);
  336. error_dev_add:
  337. error_rc_setup:
  338. rc->stop(rc);
  339. error_rc_start:
  340. uwbd_stop(rc);
  341. return result;
  342. }
  343. EXPORT_SYMBOL_GPL(uwb_rc_add);
  344. static int uwb_dev_offair_helper(struct device *dev, void *priv)
  345. {
  346. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  347. return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
  348. }
  349. /*
  350. * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
  351. */
  352. void uwb_rc_rm(struct uwb_rc *rc)
  353. {
  354. rc->ready = 0;
  355. uwb_dbg_del_rc(rc);
  356. uwb_rsv_remove_all(rc);
  357. uwb_radio_shutdown(rc);
  358. rc->stop(rc);
  359. uwbd_stop(rc);
  360. uwb_rc_neh_destroy(rc);
  361. uwb_dev_lock(&rc->uwb_dev);
  362. rc->priv = NULL;
  363. rc->cmd = NULL;
  364. uwb_dev_unlock(&rc->uwb_dev);
  365. mutex_lock(&rc->uwb_beca.mutex);
  366. uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
  367. __uwb_rc_sys_rm(rc);
  368. mutex_unlock(&rc->uwb_beca.mutex);
  369. uwb_rsv_cleanup(rc);
  370. uwb_beca_release(rc);
  371. uwb_dev_rm(&rc->uwb_dev);
  372. }
  373. EXPORT_SYMBOL_GPL(uwb_rc_rm);
  374. static int find_rc_try_get(struct device *dev, const void *data)
  375. {
  376. const struct uwb_rc *target_rc = data;
  377. struct uwb_rc *rc = dev_get_drvdata(dev);
  378. if (rc == NULL) {
  379. WARN_ON(1);
  380. return 0;
  381. }
  382. if (rc == target_rc) {
  383. if (rc->ready == 0)
  384. return 0;
  385. else
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. /**
  391. * Given a radio controller descriptor, validate and refcount it
  392. *
  393. * @returns NULL if the rc does not exist or is quiescing; the ptr to
  394. * it otherwise.
  395. */
  396. struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
  397. {
  398. struct device *dev;
  399. struct uwb_rc *rc = NULL;
  400. dev = class_find_device(&uwb_rc_class, NULL, target_rc,
  401. find_rc_try_get);
  402. if (dev) {
  403. rc = dev_get_drvdata(dev);
  404. __uwb_rc_get(rc);
  405. put_device(dev);
  406. }
  407. return rc;
  408. }
  409. EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
  410. /*
  411. * RC get for external refcount acquirers...
  412. *
  413. * Increments the refcount of the device and it's backend modules
  414. */
  415. static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
  416. {
  417. if (rc->ready == 0)
  418. return NULL;
  419. uwb_dev_get(&rc->uwb_dev);
  420. return rc;
  421. }
  422. static int find_rc_grandpa(struct device *dev, const void *data)
  423. {
  424. const struct device *grandpa_dev = data;
  425. struct uwb_rc *rc = dev_get_drvdata(dev);
  426. if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
  427. rc = uwb_rc_get(rc);
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. /**
  433. * Locate and refcount a radio controller given a common grand-parent
  434. *
  435. * @grandpa_dev Pointer to the 'grandparent' device structure.
  436. * @returns NULL If the rc does not exist or is quiescing; the ptr to
  437. * it otherwise, properly referenced.
  438. *
  439. * The Radio Control interface (or the UWB Radio Controller) is always
  440. * an interface of a device. The parent is the interface, the
  441. * grandparent is the device that encapsulates the interface.
  442. *
  443. * There is no need to lock around as the "grandpa" would be
  444. * refcounted by the target, and to remove the referemes, the
  445. * uwb_rc_class->sem would have to be taken--we hold it, ergo we
  446. * should be safe.
  447. */
  448. struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
  449. {
  450. struct device *dev;
  451. struct uwb_rc *rc = NULL;
  452. dev = class_find_device(&uwb_rc_class, NULL, grandpa_dev,
  453. find_rc_grandpa);
  454. if (dev) {
  455. rc = dev_get_drvdata(dev);
  456. put_device(dev);
  457. }
  458. return rc;
  459. }
  460. EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
  461. /**
  462. * Find a radio controller by device address
  463. *
  464. * @returns the pointer to the radio controller, properly referenced
  465. */
  466. static int find_rc_dev(struct device *dev, const void *data)
  467. {
  468. const struct uwb_dev_addr *addr = data;
  469. struct uwb_rc *rc = dev_get_drvdata(dev);
  470. if (rc == NULL) {
  471. WARN_ON(1);
  472. return 0;
  473. }
  474. if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
  475. rc = uwb_rc_get(rc);
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
  481. {
  482. struct device *dev;
  483. struct uwb_rc *rc = NULL;
  484. dev = class_find_device(&uwb_rc_class, NULL, addr, find_rc_dev);
  485. if (dev) {
  486. rc = dev_get_drvdata(dev);
  487. put_device(dev);
  488. }
  489. return rc;
  490. }
  491. EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
  492. /**
  493. * Drop a reference on a radio controller
  494. *
  495. * This is the version that should be done by entities external to the
  496. * UWB Radio Control stack (ie: clients of the API).
  497. */
  498. void uwb_rc_put(struct uwb_rc *rc)
  499. {
  500. __uwb_rc_put(rc);
  501. }
  502. EXPORT_SYMBOL_GPL(uwb_rc_put);