lc-dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Ultra Wide Band
  3. * Life cycle of devices
  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/device.h>
  27. #include <linux/err.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/random.h>
  30. #include "uwb-internal.h"
  31. #define D_LOCAL 1
  32. #include <linux/uwb/debug.h>
  33. /* We initialize addresses to 0xff (invalid, as it is bcast) */
  34. static inline void uwb_dev_addr_init(struct uwb_dev_addr *addr)
  35. {
  36. memset(&addr->data, 0xff, sizeof(addr->data));
  37. }
  38. static inline void uwb_mac_addr_init(struct uwb_mac_addr *addr)
  39. {
  40. memset(&addr->data, 0xff, sizeof(addr->data));
  41. }
  42. /* @returns !0 if a device @addr is a broadcast address */
  43. static inline int uwb_dev_addr_bcast(const struct uwb_dev_addr *addr)
  44. {
  45. static const struct uwb_dev_addr bcast = { .data = { 0xff, 0xff } };
  46. return !uwb_dev_addr_cmp(addr, &bcast);
  47. }
  48. /*
  49. * Add callback @new to be called when an event occurs in @rc.
  50. */
  51. int uwb_notifs_register(struct uwb_rc *rc, struct uwb_notifs_handler *new)
  52. {
  53. if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
  54. return -ERESTARTSYS;
  55. list_add(&new->list_node, &rc->notifs_chain.list);
  56. mutex_unlock(&rc->notifs_chain.mutex);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(uwb_notifs_register);
  60. /*
  61. * Remove event handler (callback)
  62. */
  63. int uwb_notifs_deregister(struct uwb_rc *rc, struct uwb_notifs_handler *entry)
  64. {
  65. if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
  66. return -ERESTARTSYS;
  67. list_del(&entry->list_node);
  68. mutex_unlock(&rc->notifs_chain.mutex);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL_GPL(uwb_notifs_deregister);
  72. /*
  73. * Notify all event handlers of a given event on @rc
  74. *
  75. * We are called with a valid reference to the device, or NULL if the
  76. * event is not for a particular event (e.g., a BG join event).
  77. */
  78. void uwb_notify(struct uwb_rc *rc, struct uwb_dev *uwb_dev, enum uwb_notifs event)
  79. {
  80. struct uwb_notifs_handler *handler;
  81. if (mutex_lock_interruptible(&rc->notifs_chain.mutex))
  82. return;
  83. if (!list_empty(&rc->notifs_chain.list)) {
  84. list_for_each_entry(handler, &rc->notifs_chain.list, list_node) {
  85. handler->cb(handler->data, uwb_dev, event);
  86. }
  87. }
  88. mutex_unlock(&rc->notifs_chain.mutex);
  89. }
  90. /*
  91. * Release the backing device of a uwb_dev that has been dynamically allocated.
  92. */
  93. static void uwb_dev_sys_release(struct device *dev)
  94. {
  95. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  96. d_fnstart(4, NULL, "(dev %p uwb_dev %p)\n", dev, uwb_dev);
  97. uwb_bce_put(uwb_dev->bce);
  98. d_printf(0, &uwb_dev->dev, "uwb_dev %p freed\n", uwb_dev);
  99. memset(uwb_dev, 0x69, sizeof(*uwb_dev));
  100. kfree(uwb_dev);
  101. d_fnend(4, NULL, "(dev %p uwb_dev %p) = void\n", dev, uwb_dev);
  102. }
  103. /*
  104. * Initialize a UWB device instance
  105. *
  106. * Alloc, zero and call this function.
  107. */
  108. void uwb_dev_init(struct uwb_dev *uwb_dev)
  109. {
  110. mutex_init(&uwb_dev->mutex);
  111. device_initialize(&uwb_dev->dev);
  112. uwb_dev->dev.release = uwb_dev_sys_release;
  113. uwb_dev_addr_init(&uwb_dev->dev_addr);
  114. uwb_mac_addr_init(&uwb_dev->mac_addr);
  115. bitmap_fill(uwb_dev->streams, UWB_NUM_GLOBAL_STREAMS);
  116. }
  117. static ssize_t uwb_dev_EUI_48_show(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  121. char addr[UWB_ADDR_STRSIZE];
  122. uwb_mac_addr_print(addr, sizeof(addr), &uwb_dev->mac_addr);
  123. return sprintf(buf, "%s\n", addr);
  124. }
  125. static DEVICE_ATTR(EUI_48, S_IRUGO, uwb_dev_EUI_48_show, NULL);
  126. static ssize_t uwb_dev_DevAddr_show(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  130. char addr[UWB_ADDR_STRSIZE];
  131. uwb_dev_addr_print(addr, sizeof(addr), &uwb_dev->dev_addr);
  132. return sprintf(buf, "%s\n", addr);
  133. }
  134. static DEVICE_ATTR(DevAddr, S_IRUGO, uwb_dev_DevAddr_show, NULL);
  135. /*
  136. * Show the BPST of this device.
  137. *
  138. * Calculated from the receive time of the device's beacon and it's
  139. * slot number.
  140. */
  141. static ssize_t uwb_dev_BPST_show(struct device *dev,
  142. struct device_attribute *attr, char *buf)
  143. {
  144. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  145. struct uwb_beca_e *bce;
  146. struct uwb_beacon_frame *bf;
  147. u16 bpst;
  148. bce = uwb_dev->bce;
  149. mutex_lock(&bce->mutex);
  150. bf = (struct uwb_beacon_frame *)bce->be->BeaconInfo;
  151. bpst = bce->be->wBPSTOffset
  152. - (u16)(bf->Beacon_Slot_Number * UWB_BEACON_SLOT_LENGTH_US);
  153. mutex_unlock(&bce->mutex);
  154. return sprintf(buf, "%d\n", bpst);
  155. }
  156. static DEVICE_ATTR(BPST, S_IRUGO, uwb_dev_BPST_show, NULL);
  157. /*
  158. * Show the IEs a device is beaconing
  159. *
  160. * We need to access the beacon cache, so we just lock it really
  161. * quick, print the IEs and unlock.
  162. *
  163. * We have a reference on the cache entry, so that should be
  164. * quite safe.
  165. */
  166. static ssize_t uwb_dev_IEs_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  170. return uwb_bce_print_IEs(uwb_dev, uwb_dev->bce, buf, PAGE_SIZE);
  171. }
  172. static DEVICE_ATTR(IEs, S_IRUGO | S_IWUSR, uwb_dev_IEs_show, NULL);
  173. static ssize_t uwb_dev_LQE_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  177. struct uwb_beca_e *bce = uwb_dev->bce;
  178. size_t result;
  179. mutex_lock(&bce->mutex);
  180. result = stats_show(&uwb_dev->bce->lqe_stats, buf);
  181. mutex_unlock(&bce->mutex);
  182. return result;
  183. }
  184. static ssize_t uwb_dev_LQE_store(struct device *dev,
  185. struct device_attribute *attr,
  186. const char *buf, size_t size)
  187. {
  188. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  189. struct uwb_beca_e *bce = uwb_dev->bce;
  190. ssize_t result;
  191. mutex_lock(&bce->mutex);
  192. result = stats_store(&uwb_dev->bce->lqe_stats, buf, size);
  193. mutex_unlock(&bce->mutex);
  194. return result;
  195. }
  196. static DEVICE_ATTR(LQE, S_IRUGO | S_IWUSR, uwb_dev_LQE_show, uwb_dev_LQE_store);
  197. static ssize_t uwb_dev_RSSI_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  201. struct uwb_beca_e *bce = uwb_dev->bce;
  202. size_t result;
  203. mutex_lock(&bce->mutex);
  204. result = stats_show(&uwb_dev->bce->rssi_stats, buf);
  205. mutex_unlock(&bce->mutex);
  206. return result;
  207. }
  208. static ssize_t uwb_dev_RSSI_store(struct device *dev,
  209. struct device_attribute *attr,
  210. const char *buf, size_t size)
  211. {
  212. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  213. struct uwb_beca_e *bce = uwb_dev->bce;
  214. ssize_t result;
  215. mutex_lock(&bce->mutex);
  216. result = stats_store(&uwb_dev->bce->rssi_stats, buf, size);
  217. mutex_unlock(&bce->mutex);
  218. return result;
  219. }
  220. static DEVICE_ATTR(RSSI, S_IRUGO | S_IWUSR, uwb_dev_RSSI_show, uwb_dev_RSSI_store);
  221. static struct attribute *dev_attrs[] = {
  222. &dev_attr_EUI_48.attr,
  223. &dev_attr_DevAddr.attr,
  224. &dev_attr_BPST.attr,
  225. &dev_attr_IEs.attr,
  226. &dev_attr_LQE.attr,
  227. &dev_attr_RSSI.attr,
  228. NULL,
  229. };
  230. static struct attribute_group dev_attr_group = {
  231. .attrs = dev_attrs,
  232. };
  233. static struct attribute_group *groups[] = {
  234. &dev_attr_group,
  235. NULL,
  236. };
  237. /**
  238. * Device SYSFS registration
  239. *
  240. *
  241. */
  242. static int __uwb_dev_sys_add(struct uwb_dev *uwb_dev, struct device *parent_dev)
  243. {
  244. int result;
  245. struct device *dev;
  246. d_fnstart(4, NULL, "(uwb_dev %p parent_dev %p)\n", uwb_dev, parent_dev);
  247. BUG_ON(parent_dev == NULL);
  248. dev = &uwb_dev->dev;
  249. /* Device sysfs files are only useful for neighbor devices not
  250. local radio controllers. */
  251. if (&uwb_dev->rc->uwb_dev != uwb_dev)
  252. dev->groups = groups;
  253. dev->parent = parent_dev;
  254. dev_set_drvdata(dev, uwb_dev);
  255. result = device_add(dev);
  256. d_fnend(4, NULL, "(uwb_dev %p parent_dev %p) = %d\n", uwb_dev, parent_dev, result);
  257. return result;
  258. }
  259. static void __uwb_dev_sys_rm(struct uwb_dev *uwb_dev)
  260. {
  261. d_fnstart(4, NULL, "(uwb_dev %p)\n", uwb_dev);
  262. dev_set_drvdata(&uwb_dev->dev, NULL);
  263. device_del(&uwb_dev->dev);
  264. d_fnend(4, NULL, "(uwb_dev %p) = void\n", uwb_dev);
  265. }
  266. /**
  267. * Register and initialize a new UWB device
  268. *
  269. * Did you call uwb_dev_init() on it?
  270. *
  271. * @parent_rc: is the parent radio controller who has the link to the
  272. * device. When registering the UWB device that is a UWB
  273. * Radio Controller, we point back to it.
  274. *
  275. * If registering the device that is part of a radio, caller has set
  276. * rc->uwb_dev->dev. Otherwise it is to be left NULL--a new one will
  277. * be allocated.
  278. */
  279. int uwb_dev_add(struct uwb_dev *uwb_dev, struct device *parent_dev,
  280. struct uwb_rc *parent_rc)
  281. {
  282. int result;
  283. struct device *dev;
  284. BUG_ON(uwb_dev == NULL);
  285. BUG_ON(parent_dev == NULL);
  286. BUG_ON(parent_rc == NULL);
  287. mutex_lock(&uwb_dev->mutex);
  288. dev = &uwb_dev->dev;
  289. uwb_dev->rc = parent_rc;
  290. result = __uwb_dev_sys_add(uwb_dev, parent_dev);
  291. if (result < 0)
  292. printk(KERN_ERR "UWB: unable to register dev %s with sysfs: %d\n",
  293. dev_name(dev), result);
  294. mutex_unlock(&uwb_dev->mutex);
  295. return result;
  296. }
  297. void uwb_dev_rm(struct uwb_dev *uwb_dev)
  298. {
  299. mutex_lock(&uwb_dev->mutex);
  300. __uwb_dev_sys_rm(uwb_dev);
  301. mutex_unlock(&uwb_dev->mutex);
  302. }
  303. static
  304. int __uwb_dev_try_get(struct device *dev, void *__target_uwb_dev)
  305. {
  306. struct uwb_dev *target_uwb_dev = __target_uwb_dev;
  307. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  308. if (uwb_dev == target_uwb_dev) {
  309. uwb_dev_get(uwb_dev);
  310. return 1;
  311. } else
  312. return 0;
  313. }
  314. /**
  315. * Given a UWB device descriptor, validate and refcount it
  316. *
  317. * @returns NULL if the device does not exist or is quiescing; the ptr to
  318. * it otherwise.
  319. */
  320. struct uwb_dev *uwb_dev_try_get(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
  321. {
  322. if (uwb_dev_for_each(rc, __uwb_dev_try_get, uwb_dev))
  323. return uwb_dev;
  324. else
  325. return NULL;
  326. }
  327. EXPORT_SYMBOL_GPL(uwb_dev_try_get);
  328. /**
  329. * Remove a device from the system [grunt for other functions]
  330. */
  331. int __uwb_dev_offair(struct uwb_dev *uwb_dev, struct uwb_rc *rc)
  332. {
  333. struct device *dev = &uwb_dev->dev;
  334. char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
  335. d_fnstart(3, NULL, "(dev %p [uwb_dev %p], uwb_rc %p)\n", dev, uwb_dev, rc);
  336. uwb_mac_addr_print(macbuf, sizeof(macbuf), &uwb_dev->mac_addr);
  337. uwb_dev_addr_print(devbuf, sizeof(devbuf), &uwb_dev->dev_addr);
  338. dev_info(dev, "uwb device (mac %s dev %s) disconnected from %s %s\n",
  339. macbuf, devbuf,
  340. rc ? rc->uwb_dev.dev.parent->bus->name : "n/a",
  341. rc ? dev_name(rc->uwb_dev.dev.parent) : "");
  342. uwb_dev_rm(uwb_dev);
  343. uwb_dev_put(uwb_dev); /* for the creation in _onair() */
  344. d_fnend(3, NULL, "(dev %p [uwb_dev %p], uwb_rc %p) = 0\n", dev, uwb_dev, rc);
  345. return 0;
  346. }
  347. /**
  348. * A device went off the air, clean up after it!
  349. *
  350. * This is called by the UWB Daemon (through the beacon purge function
  351. * uwb_bcn_cache_purge) when it is detected that a device has been in
  352. * radio silence for a while.
  353. *
  354. * If this device is actually a local radio controller we don't need
  355. * to go through the offair process, as it is not registered as that.
  356. *
  357. * NOTE: uwb_bcn_cache.mutex is held!
  358. */
  359. void uwbd_dev_offair(struct uwb_beca_e *bce)
  360. {
  361. struct uwb_dev *uwb_dev;
  362. uwb_dev = bce->uwb_dev;
  363. if (uwb_dev) {
  364. uwb_notify(uwb_dev->rc, uwb_dev, UWB_NOTIF_OFFAIR);
  365. __uwb_dev_offair(uwb_dev, uwb_dev->rc);
  366. }
  367. }
  368. /**
  369. * A device went on the air, start it up!
  370. *
  371. * This is called by the UWB Daemon when it is detected that a device
  372. * has popped up in the radio range of the radio controller.
  373. *
  374. * It will just create the freaking device, register the beacon and
  375. * stuff and yatla, done.
  376. *
  377. *
  378. * NOTE: uwb_beca.mutex is held, bce->mutex is held
  379. */
  380. void uwbd_dev_onair(struct uwb_rc *rc, struct uwb_beca_e *bce)
  381. {
  382. int result;
  383. struct device *dev = &rc->uwb_dev.dev;
  384. struct uwb_dev *uwb_dev;
  385. char macbuf[UWB_ADDR_STRSIZE], devbuf[UWB_ADDR_STRSIZE];
  386. uwb_mac_addr_print(macbuf, sizeof(macbuf), bce->mac_addr);
  387. uwb_dev_addr_print(devbuf, sizeof(devbuf), &bce->dev_addr);
  388. uwb_dev = kzalloc(sizeof(struct uwb_dev), GFP_KERNEL);
  389. if (uwb_dev == NULL) {
  390. dev_err(dev, "new device %s: Cannot allocate memory\n",
  391. macbuf);
  392. return;
  393. }
  394. uwb_dev_init(uwb_dev); /* This sets refcnt to one, we own it */
  395. uwb_dev->mac_addr = *bce->mac_addr;
  396. uwb_dev->dev_addr = bce->dev_addr;
  397. dev_set_name(&uwb_dev->dev, macbuf);
  398. result = uwb_dev_add(uwb_dev, &rc->uwb_dev.dev, rc);
  399. if (result < 0) {
  400. dev_err(dev, "new device %s: cannot instantiate device\n",
  401. macbuf);
  402. goto error_dev_add;
  403. }
  404. /* plug the beacon cache */
  405. bce->uwb_dev = uwb_dev;
  406. uwb_dev->bce = bce;
  407. uwb_bce_get(bce); /* released in uwb_dev_sys_release() */
  408. dev_info(dev, "uwb device (mac %s dev %s) connected to %s %s\n",
  409. macbuf, devbuf, rc->uwb_dev.dev.parent->bus->name,
  410. dev_name(rc->uwb_dev.dev.parent));
  411. uwb_notify(rc, uwb_dev, UWB_NOTIF_ONAIR);
  412. return;
  413. error_dev_add:
  414. kfree(uwb_dev);
  415. return;
  416. }
  417. /**
  418. * Iterate over the list of UWB devices, calling a @function on each
  419. *
  420. * See docs for bus_for_each()....
  421. *
  422. * @rc: radio controller for the devices.
  423. * @function: function to call.
  424. * @priv: data to pass to @function.
  425. * @returns: 0 if no invocation of function() returned a value
  426. * different to zero. That value otherwise.
  427. */
  428. int uwb_dev_for_each(struct uwb_rc *rc, uwb_dev_for_each_f function, void *priv)
  429. {
  430. return device_for_each_child(&rc->uwb_dev.dev, priv, function);
  431. }
  432. EXPORT_SYMBOL_GPL(uwb_dev_for_each);