uwb-debug.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Ultra Wide Band
  3. * Debug support
  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: doc
  24. */
  25. #include <linux/spinlock.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/notifier.h>
  29. #include <linux/device.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/uwb/debug-cmd.h>
  34. #define D_LOCAL 0
  35. #include <linux/uwb/debug.h>
  36. #include "uwb-internal.h"
  37. void dump_bytes(struct device *dev, const void *_buf, size_t rsize)
  38. {
  39. const char *buf = _buf;
  40. char line[32];
  41. size_t offset = 0;
  42. int cnt, cnt2;
  43. for (cnt = 0; cnt < rsize; cnt += 8) {
  44. size_t rtop = rsize - cnt < 8 ? rsize - cnt : 8;
  45. for (offset = cnt2 = 0; cnt2 < rtop; cnt2++) {
  46. offset += scnprintf(line + offset, sizeof(line) - offset,
  47. "%02x ", buf[cnt + cnt2] & 0xff);
  48. }
  49. if (dev)
  50. dev_info(dev, "%s\n", line);
  51. else
  52. printk(KERN_INFO "%s\n", line);
  53. }
  54. }
  55. EXPORT_SYMBOL_GPL(dump_bytes);
  56. /*
  57. * Debug interface
  58. *
  59. * Per radio controller debugfs files (in uwb/uwbN/):
  60. *
  61. * command: Flexible command interface (see <linux/uwb/debug-cmd.h>).
  62. *
  63. * reservations: information on reservations.
  64. *
  65. * accept: Set to true (Y or 1) to accept reservation requests from
  66. * peers.
  67. *
  68. * drp_avail: DRP availability information.
  69. */
  70. struct uwb_dbg {
  71. struct uwb_pal pal;
  72. u32 accept;
  73. struct list_head rsvs;
  74. struct dentry *root_d;
  75. struct dentry *command_f;
  76. struct dentry *reservations_f;
  77. struct dentry *accept_f;
  78. struct dentry *drp_avail_f;
  79. };
  80. static struct dentry *root_dir;
  81. static void uwb_dbg_rsv_cb(struct uwb_rsv *rsv)
  82. {
  83. struct uwb_rc *rc = rsv->rc;
  84. struct device *dev = &rc->uwb_dev.dev;
  85. struct uwb_dev_addr devaddr;
  86. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  87. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  88. if (rsv->target.type == UWB_RSV_TARGET_DEV)
  89. devaddr = rsv->target.dev->dev_addr;
  90. else
  91. devaddr = rsv->target.devaddr;
  92. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  93. dev_dbg(dev, "debug: rsv %s -> %s: %s\n",
  94. owner, target, uwb_rsv_state_str(rsv->state));
  95. if (rsv->state == UWB_RSV_STATE_NONE) {
  96. list_del(&rsv->pal_node);
  97. uwb_rsv_destroy(rsv);
  98. }
  99. }
  100. static int cmd_rsv_establish(struct uwb_rc *rc,
  101. struct uwb_dbg_cmd_rsv_establish *cmd)
  102. {
  103. struct uwb_mac_addr macaddr;
  104. struct uwb_rsv *rsv;
  105. struct uwb_dev *target;
  106. int ret;
  107. memcpy(&macaddr, cmd->target, sizeof(macaddr));
  108. target = uwb_dev_get_by_macaddr(rc, &macaddr);
  109. if (target == NULL)
  110. return -ENODEV;
  111. rsv = uwb_rsv_create(rc, uwb_dbg_rsv_cb, NULL);
  112. if (rsv == NULL) {
  113. uwb_dev_put(target);
  114. return -ENOMEM;
  115. }
  116. rsv->owner = &rc->uwb_dev;
  117. rsv->target.type = UWB_RSV_TARGET_DEV;
  118. rsv->target.dev = target;
  119. rsv->type = cmd->type;
  120. rsv->max_mas = cmd->max_mas;
  121. rsv->min_mas = cmd->min_mas;
  122. rsv->sparsity = cmd->sparsity;
  123. ret = uwb_rsv_establish(rsv);
  124. if (ret)
  125. uwb_rsv_destroy(rsv);
  126. else
  127. list_add_tail(&rsv->pal_node, &rc->dbg->rsvs);
  128. return ret;
  129. }
  130. static int cmd_rsv_terminate(struct uwb_rc *rc,
  131. struct uwb_dbg_cmd_rsv_terminate *cmd)
  132. {
  133. struct uwb_rsv *rsv, *found = NULL;
  134. int i = 0;
  135. list_for_each_entry(rsv, &rc->dbg->rsvs, pal_node) {
  136. if (i == cmd->index) {
  137. found = rsv;
  138. break;
  139. }
  140. i++;
  141. }
  142. if (!found)
  143. return -EINVAL;
  144. uwb_rsv_terminate(found);
  145. return 0;
  146. }
  147. static int cmd_ie_add(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_add)
  148. {
  149. return uwb_rc_ie_add(rc,
  150. (const struct uwb_ie_hdr *) ie_to_add->data,
  151. ie_to_add->len);
  152. }
  153. static int cmd_ie_rm(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_rm)
  154. {
  155. return uwb_rc_ie_rm(rc, ie_to_rm->data[0]);
  156. }
  157. static int command_open(struct inode *inode, struct file *file)
  158. {
  159. file->private_data = inode->i_private;
  160. return 0;
  161. }
  162. static ssize_t command_write(struct file *file, const char __user *buf,
  163. size_t len, loff_t *off)
  164. {
  165. struct uwb_rc *rc = file->private_data;
  166. struct uwb_dbg_cmd cmd;
  167. int ret;
  168. if (len != sizeof(struct uwb_dbg_cmd))
  169. return -EINVAL;
  170. if (copy_from_user(&cmd, buf, len) != 0)
  171. return -EFAULT;
  172. switch (cmd.type) {
  173. case UWB_DBG_CMD_RSV_ESTABLISH:
  174. ret = cmd_rsv_establish(rc, &cmd.rsv_establish);
  175. break;
  176. case UWB_DBG_CMD_RSV_TERMINATE:
  177. ret = cmd_rsv_terminate(rc, &cmd.rsv_terminate);
  178. break;
  179. case UWB_DBG_CMD_IE_ADD:
  180. ret = cmd_ie_add(rc, &cmd.ie_add);
  181. break;
  182. case UWB_DBG_CMD_IE_RM:
  183. ret = cmd_ie_rm(rc, &cmd.ie_rm);
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. return ret < 0 ? ret : len;
  189. }
  190. static struct file_operations command_fops = {
  191. .open = command_open,
  192. .write = command_write,
  193. .read = NULL,
  194. .llseek = no_llseek,
  195. .owner = THIS_MODULE,
  196. };
  197. static int reservations_print(struct seq_file *s, void *p)
  198. {
  199. struct uwb_rc *rc = s->private;
  200. struct uwb_rsv *rsv;
  201. mutex_lock(&rc->rsvs_mutex);
  202. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  203. struct uwb_dev_addr devaddr;
  204. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  205. bool is_owner;
  206. char buf[72];
  207. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  208. if (rsv->target.type == UWB_RSV_TARGET_DEV) {
  209. devaddr = rsv->target.dev->dev_addr;
  210. is_owner = &rc->uwb_dev == rsv->owner;
  211. } else {
  212. devaddr = rsv->target.devaddr;
  213. is_owner = true;
  214. }
  215. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  216. seq_printf(s, "%c %s -> %s: %s\n",
  217. is_owner ? 'O' : 'T',
  218. owner, target, uwb_rsv_state_str(rsv->state));
  219. seq_printf(s, " stream: %d type: %s\n",
  220. rsv->stream, uwb_rsv_type_str(rsv->type));
  221. bitmap_scnprintf(buf, sizeof(buf), rsv->mas.bm, UWB_NUM_MAS);
  222. seq_printf(s, " %s\n", buf);
  223. }
  224. mutex_unlock(&rc->rsvs_mutex);
  225. return 0;
  226. }
  227. static int reservations_open(struct inode *inode, struct file *file)
  228. {
  229. return single_open(file, reservations_print, inode->i_private);
  230. }
  231. static struct file_operations reservations_fops = {
  232. .open = reservations_open,
  233. .read = seq_read,
  234. .llseek = seq_lseek,
  235. .release = single_release,
  236. .owner = THIS_MODULE,
  237. };
  238. static int drp_avail_print(struct seq_file *s, void *p)
  239. {
  240. struct uwb_rc *rc = s->private;
  241. char buf[72];
  242. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.global, UWB_NUM_MAS);
  243. seq_printf(s, "global: %s\n", buf);
  244. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.local, UWB_NUM_MAS);
  245. seq_printf(s, "local: %s\n", buf);
  246. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.pending, UWB_NUM_MAS);
  247. seq_printf(s, "pending: %s\n", buf);
  248. return 0;
  249. }
  250. static int drp_avail_open(struct inode *inode, struct file *file)
  251. {
  252. return single_open(file, drp_avail_print, inode->i_private);
  253. }
  254. static struct file_operations drp_avail_fops = {
  255. .open = drp_avail_open,
  256. .read = seq_read,
  257. .llseek = seq_lseek,
  258. .release = single_release,
  259. .owner = THIS_MODULE,
  260. };
  261. static void uwb_dbg_new_rsv(struct uwb_pal *pal, struct uwb_rsv *rsv)
  262. {
  263. struct uwb_dbg *dbg = container_of(pal, struct uwb_dbg, pal);
  264. if (dbg->accept) {
  265. list_add_tail(&rsv->pal_node, &dbg->rsvs);
  266. uwb_rsv_accept(rsv, uwb_dbg_rsv_cb, dbg);
  267. }
  268. }
  269. /**
  270. * uwb_dbg_add_rc - add a debug interface for a radio controller
  271. * @rc: the radio controller
  272. */
  273. void uwb_dbg_add_rc(struct uwb_rc *rc)
  274. {
  275. rc->dbg = kzalloc(sizeof(struct uwb_dbg), GFP_KERNEL);
  276. if (rc->dbg == NULL)
  277. return;
  278. INIT_LIST_HEAD(&rc->dbg->rsvs);
  279. uwb_pal_init(&rc->dbg->pal);
  280. rc->dbg->pal.new_rsv = uwb_dbg_new_rsv;
  281. uwb_pal_register(rc, &rc->dbg->pal);
  282. if (root_dir) {
  283. rc->dbg->root_d = debugfs_create_dir(dev_name(&rc->uwb_dev.dev),
  284. root_dir);
  285. rc->dbg->command_f = debugfs_create_file("command", 0200,
  286. rc->dbg->root_d, rc,
  287. &command_fops);
  288. rc->dbg->reservations_f = debugfs_create_file("reservations", 0444,
  289. rc->dbg->root_d, rc,
  290. &reservations_fops);
  291. rc->dbg->accept_f = debugfs_create_bool("accept", 0644,
  292. rc->dbg->root_d,
  293. &rc->dbg->accept);
  294. rc->dbg->drp_avail_f = debugfs_create_file("drp_avail", 0444,
  295. rc->dbg->root_d, rc,
  296. &drp_avail_fops);
  297. }
  298. }
  299. /**
  300. * uwb_dbg_del_rc - remove a radio controller's debug interface
  301. * @rc: the radio controller
  302. */
  303. void uwb_dbg_del_rc(struct uwb_rc *rc)
  304. {
  305. struct uwb_rsv *rsv, *t;
  306. if (rc->dbg == NULL)
  307. return;
  308. list_for_each_entry_safe(rsv, t, &rc->dbg->rsvs, pal_node) {
  309. uwb_rsv_terminate(rsv);
  310. }
  311. uwb_pal_unregister(rc, &rc->dbg->pal);
  312. if (root_dir) {
  313. debugfs_remove(rc->dbg->drp_avail_f);
  314. debugfs_remove(rc->dbg->accept_f);
  315. debugfs_remove(rc->dbg->reservations_f);
  316. debugfs_remove(rc->dbg->command_f);
  317. debugfs_remove(rc->dbg->root_d);
  318. }
  319. }
  320. /**
  321. * uwb_dbg_exit - initialize the debug interface sub-module
  322. */
  323. void uwb_dbg_init(void)
  324. {
  325. root_dir = debugfs_create_dir("uwb", NULL);
  326. }
  327. /**
  328. * uwb_dbg_exit - clean-up the debug interface sub-module
  329. */
  330. void uwb_dbg_exit(void)
  331. {
  332. debugfs_remove(root_dir);
  333. }