lc-rc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #define D_LOCAL 1
  38. #include <linux/uwb/debug.h>
  39. #include "uwb-internal.h"
  40. static int uwb_rc_index_match(struct device *dev, void *data)
  41. {
  42. 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. return rc;
  56. }
  57. static int uwb_rc_new_index(void)
  58. {
  59. int index = 0;
  60. for (;;) {
  61. if (!uwb_rc_find_by_index(index))
  62. return index;
  63. if (++index < 0)
  64. index = 0;
  65. }
  66. }
  67. /**
  68. * Release the backing device of a uwb_rc that has been dynamically allocated.
  69. */
  70. static void uwb_rc_sys_release(struct device *dev)
  71. {
  72. struct uwb_dev *uwb_dev = container_of(dev, struct uwb_dev, dev);
  73. struct uwb_rc *rc = container_of(uwb_dev, struct uwb_rc, uwb_dev);
  74. uwb_rc_neh_destroy(rc);
  75. uwb_rc_ie_release(rc);
  76. d_printf(1, dev, "freed uwb_rc %p\n", 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. uwb_drp_avail_init(rc);
  91. uwb_rc_ie_init(rc);
  92. uwb_rsv_init(rc);
  93. uwb_rc_pal_init(rc);
  94. }
  95. EXPORT_SYMBOL_GPL(uwb_rc_init);
  96. struct uwb_rc *uwb_rc_alloc(void)
  97. {
  98. struct uwb_rc *rc;
  99. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  100. if (rc == NULL)
  101. return NULL;
  102. uwb_rc_init(rc);
  103. return rc;
  104. }
  105. EXPORT_SYMBOL_GPL(uwb_rc_alloc);
  106. static struct attribute *rc_attrs[] = {
  107. &dev_attr_mac_address.attr,
  108. &dev_attr_scan.attr,
  109. &dev_attr_beacon.attr,
  110. NULL,
  111. };
  112. static struct attribute_group rc_attr_group = {
  113. .attrs = rc_attrs,
  114. };
  115. /*
  116. * Registration of sysfs specific stuff
  117. */
  118. static int uwb_rc_sys_add(struct uwb_rc *rc)
  119. {
  120. return sysfs_create_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
  121. }
  122. static void __uwb_rc_sys_rm(struct uwb_rc *rc)
  123. {
  124. sysfs_remove_group(&rc->uwb_dev.dev.kobj, &rc_attr_group);
  125. }
  126. /**
  127. * uwb_rc_mac_addr_setup - get an RC's EUI-48 address or set it
  128. * @rc: the radio controller.
  129. *
  130. * If the EUI-48 address is 00:00:00:00:00:00 or FF:FF:FF:FF:FF:FF
  131. * then a random locally administered EUI-48 is generated and set on
  132. * the device. The probability of address collisions is sufficiently
  133. * unlikely (1/2^40 = 9.1e-13) that they're not checked for.
  134. */
  135. static
  136. int uwb_rc_mac_addr_setup(struct uwb_rc *rc)
  137. {
  138. int result;
  139. struct device *dev = &rc->uwb_dev.dev;
  140. struct uwb_dev *uwb_dev = &rc->uwb_dev;
  141. char devname[UWB_ADDR_STRSIZE];
  142. struct uwb_mac_addr addr;
  143. result = uwb_rc_mac_addr_get(rc, &addr);
  144. if (result < 0) {
  145. dev_err(dev, "cannot retrieve UWB EUI-48 address: %d\n", result);
  146. return result;
  147. }
  148. if (uwb_mac_addr_unset(&addr) || uwb_mac_addr_bcast(&addr)) {
  149. addr.data[0] = 0x02; /* locally adminstered and unicast */
  150. get_random_bytes(&addr.data[1], sizeof(addr.data)-1);
  151. result = uwb_rc_mac_addr_set(rc, &addr);
  152. if (result < 0) {
  153. uwb_mac_addr_print(devname, sizeof(devname), &addr);
  154. dev_err(dev, "cannot set EUI-48 address %s: %d\n",
  155. devname, result);
  156. return result;
  157. }
  158. }
  159. uwb_dev->mac_addr = addr;
  160. return 0;
  161. }
  162. static int uwb_rc_setup(struct uwb_rc *rc)
  163. {
  164. int result;
  165. struct device *dev = &rc->uwb_dev.dev;
  166. result = uwb_rc_reset(rc);
  167. if (result < 0) {
  168. dev_err(dev, "cannot reset UWB radio: %d\n", result);
  169. goto error;
  170. }
  171. result = uwb_rc_mac_addr_setup(rc);
  172. if (result < 0) {
  173. dev_err(dev, "cannot setup UWB MAC address: %d\n", result);
  174. goto error;
  175. }
  176. result = uwb_rc_dev_addr_assign(rc);
  177. if (result < 0) {
  178. dev_err(dev, "cannot assign UWB DevAddr: %d\n", result);
  179. goto error;
  180. }
  181. result = uwb_rc_ie_setup(rc);
  182. if (result < 0) {
  183. dev_err(dev, "cannot setup IE subsystem: %d\n", result);
  184. goto error_ie_setup;
  185. }
  186. result = uwb_rsv_setup(rc);
  187. if (result < 0) {
  188. dev_err(dev, "cannot setup reservation subsystem: %d\n", result);
  189. goto error_rsv_setup;
  190. }
  191. uwb_dbg_add_rc(rc);
  192. return 0;
  193. error_rsv_setup:
  194. uwb_rc_ie_release(rc);
  195. error_ie_setup:
  196. error:
  197. return result;
  198. }
  199. /**
  200. * Register a new UWB radio controller
  201. *
  202. * Did you call uwb_rc_init() on your rc?
  203. *
  204. * We assume that this is being called with a > 0 refcount on
  205. * it [through ops->{get|put}_device(). We'll take our own, though.
  206. *
  207. * @parent_dev is our real device, the one that provides the actual UWB device
  208. */
  209. int uwb_rc_add(struct uwb_rc *rc, struct device *parent_dev, void *priv)
  210. {
  211. int result;
  212. struct device *dev;
  213. char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
  214. rc->index = uwb_rc_new_index();
  215. dev = &rc->uwb_dev.dev;
  216. dev_set_name(dev, "uwb%d", rc->index);
  217. rc->priv = priv;
  218. result = rc->start(rc);
  219. if (result < 0)
  220. goto error_rc_start;
  221. result = uwb_rc_setup(rc);
  222. if (result < 0) {
  223. dev_err(dev, "cannot setup UWB radio controller: %d\n", result);
  224. goto error_rc_setup;
  225. }
  226. result = uwb_dev_add(&rc->uwb_dev, parent_dev, rc);
  227. if (result < 0 && result != -EADDRNOTAVAIL)
  228. goto error_dev_add;
  229. result = uwb_rc_sys_add(rc);
  230. if (result < 0) {
  231. dev_err(parent_dev, "cannot register UWB radio controller "
  232. "dev attributes: %d\n", result);
  233. goto error_sys_add;
  234. }
  235. uwb_mac_addr_print(macbuf, sizeof(macbuf), &rc->uwb_dev.mac_addr);
  236. uwb_dev_addr_print(devbuf, sizeof(devbuf), &rc->uwb_dev.dev_addr);
  237. dev_info(dev,
  238. "new uwb radio controller (mac %s dev %s) on %s %s\n",
  239. macbuf, devbuf, parent_dev->bus->name, dev_name(parent_dev));
  240. rc->ready = 1;
  241. return 0;
  242. error_sys_add:
  243. uwb_dev_rm(&rc->uwb_dev);
  244. error_dev_add:
  245. error_rc_setup:
  246. rc->stop(rc);
  247. uwbd_flush(rc);
  248. error_rc_start:
  249. return result;
  250. }
  251. EXPORT_SYMBOL_GPL(uwb_rc_add);
  252. static int uwb_dev_offair_helper(struct device *dev, void *priv)
  253. {
  254. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  255. return __uwb_dev_offair(uwb_dev, uwb_dev->rc);
  256. }
  257. /*
  258. * Remove a Radio Controller; stop beaconing/scanning, disconnect all children
  259. */
  260. void uwb_rc_rm(struct uwb_rc *rc)
  261. {
  262. rc->ready = 0;
  263. uwb_dbg_del_rc(rc);
  264. uwb_rsv_cleanup(rc);
  265. uwb_rc_ie_rm(rc, UWB_IDENTIFICATION_IE);
  266. if (rc->beaconing >= 0)
  267. uwb_rc_beacon(rc, -1, 0);
  268. if (rc->scan_type != UWB_SCAN_DISABLED)
  269. uwb_rc_scan(rc, rc->scanning, UWB_SCAN_DISABLED, 0);
  270. uwb_rc_reset(rc);
  271. rc->stop(rc);
  272. uwbd_flush(rc);
  273. uwb_dev_lock(&rc->uwb_dev);
  274. rc->priv = NULL;
  275. rc->cmd = NULL;
  276. uwb_dev_unlock(&rc->uwb_dev);
  277. mutex_lock(&uwb_beca.mutex);
  278. uwb_dev_for_each(rc, uwb_dev_offair_helper, NULL);
  279. __uwb_rc_sys_rm(rc);
  280. mutex_unlock(&uwb_beca.mutex);
  281. uwb_dev_rm(&rc->uwb_dev);
  282. }
  283. EXPORT_SYMBOL_GPL(uwb_rc_rm);
  284. static int find_rc_try_get(struct device *dev, void *data)
  285. {
  286. struct uwb_rc *target_rc = data;
  287. struct uwb_rc *rc = dev_get_drvdata(dev);
  288. if (rc == NULL) {
  289. WARN_ON(1);
  290. return 0;
  291. }
  292. if (rc == target_rc) {
  293. if (rc->ready == 0)
  294. return 0;
  295. else
  296. return 1;
  297. }
  298. return 0;
  299. }
  300. /**
  301. * Given a radio controller descriptor, validate and refcount it
  302. *
  303. * @returns NULL if the rc does not exist or is quiescing; the ptr to
  304. * it otherwise.
  305. */
  306. struct uwb_rc *__uwb_rc_try_get(struct uwb_rc *target_rc)
  307. {
  308. struct device *dev;
  309. struct uwb_rc *rc = NULL;
  310. dev = class_find_device(&uwb_rc_class, NULL, target_rc,
  311. find_rc_try_get);
  312. if (dev) {
  313. rc = dev_get_drvdata(dev);
  314. __uwb_rc_get(rc);
  315. }
  316. return rc;
  317. }
  318. EXPORT_SYMBOL_GPL(__uwb_rc_try_get);
  319. /*
  320. * RC get for external refcount acquirers...
  321. *
  322. * Increments the refcount of the device and it's backend modules
  323. */
  324. static inline struct uwb_rc *uwb_rc_get(struct uwb_rc *rc)
  325. {
  326. if (rc->ready == 0)
  327. return NULL;
  328. uwb_dev_get(&rc->uwb_dev);
  329. return rc;
  330. }
  331. static int find_rc_grandpa(struct device *dev, void *data)
  332. {
  333. struct device *grandpa_dev = data;
  334. struct uwb_rc *rc = dev_get_drvdata(dev);
  335. if (rc->uwb_dev.dev.parent->parent == grandpa_dev) {
  336. rc = uwb_rc_get(rc);
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. /**
  342. * Locate and refcount a radio controller given a common grand-parent
  343. *
  344. * @grandpa_dev Pointer to the 'grandparent' device structure.
  345. * @returns NULL If the rc does not exist or is quiescing; the ptr to
  346. * it otherwise, properly referenced.
  347. *
  348. * The Radio Control interface (or the UWB Radio Controller) is always
  349. * an interface of a device. The parent is the interface, the
  350. * grandparent is the device that encapsulates the interface.
  351. *
  352. * There is no need to lock around as the "grandpa" would be
  353. * refcounted by the target, and to remove the referemes, the
  354. * uwb_rc_class->sem would have to be taken--we hold it, ergo we
  355. * should be safe.
  356. */
  357. struct uwb_rc *uwb_rc_get_by_grandpa(const struct device *grandpa_dev)
  358. {
  359. struct device *dev;
  360. struct uwb_rc *rc = NULL;
  361. dev = class_find_device(&uwb_rc_class, NULL, (void *)grandpa_dev,
  362. find_rc_grandpa);
  363. if (dev)
  364. rc = dev_get_drvdata(dev);
  365. return rc;
  366. }
  367. EXPORT_SYMBOL_GPL(uwb_rc_get_by_grandpa);
  368. /**
  369. * Find a radio controller by device address
  370. *
  371. * @returns the pointer to the radio controller, properly referenced
  372. */
  373. static int find_rc_dev(struct device *dev, void *data)
  374. {
  375. struct uwb_dev_addr *addr = data;
  376. struct uwb_rc *rc = dev_get_drvdata(dev);
  377. if (rc == NULL) {
  378. WARN_ON(1);
  379. return 0;
  380. }
  381. if (!uwb_dev_addr_cmp(&rc->uwb_dev.dev_addr, addr)) {
  382. rc = uwb_rc_get(rc);
  383. return 1;
  384. }
  385. return 0;
  386. }
  387. struct uwb_rc *uwb_rc_get_by_dev(const struct uwb_dev_addr *addr)
  388. {
  389. struct device *dev;
  390. struct uwb_rc *rc = NULL;
  391. dev = class_find_device(&uwb_rc_class, NULL, (void *)addr,
  392. find_rc_dev);
  393. if (dev)
  394. rc = dev_get_drvdata(dev);
  395. return rc;
  396. }
  397. EXPORT_SYMBOL_GPL(uwb_rc_get_by_dev);
  398. /**
  399. * Drop a reference on a radio controller
  400. *
  401. * This is the version that should be done by entities external to the
  402. * UWB Radio Control stack (ie: clients of the API).
  403. */
  404. void uwb_rc_put(struct uwb_rc *rc)
  405. {
  406. __uwb_rc_put(rc);
  407. }
  408. EXPORT_SYMBOL_GPL(uwb_rc_put);
  409. /*
  410. *
  411. *
  412. */
  413. ssize_t uwb_rc_print_IEs(struct uwb_rc *uwb_rc, char *buf, size_t size)
  414. {
  415. ssize_t result;
  416. struct uwb_rc_evt_get_ie *ie_info;
  417. struct uwb_buf_ctx ctx;
  418. result = uwb_rc_get_ie(uwb_rc, &ie_info);
  419. if (result < 0)
  420. goto error_get_ie;
  421. ctx.buf = buf;
  422. ctx.size = size;
  423. ctx.bytes = 0;
  424. uwb_ie_for_each(&uwb_rc->uwb_dev, uwb_ie_dump_hex, &ctx,
  425. ie_info->IEData, result - sizeof(*ie_info));
  426. result = ctx.bytes;
  427. kfree(ie_info);
  428. error_get_ie:
  429. return result;
  430. }