uwb-debug.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * FIXME: doc
  25. */
  26. #include <linux/spinlock.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/notifier.h>
  30. #include <linux/device.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/uwb/debug-cmd.h>
  35. #include "uwb-internal.h"
  36. /*
  37. * Debug interface
  38. *
  39. * Per radio controller debugfs files (in uwb/uwbN/):
  40. *
  41. * command: Flexible command interface (see <linux/uwb/debug-cmd.h>).
  42. *
  43. * reservations: information on reservations.
  44. *
  45. * accept: Set to true (Y or 1) to accept reservation requests from
  46. * peers.
  47. *
  48. * drp_avail: DRP availability information.
  49. */
  50. struct uwb_dbg {
  51. struct uwb_pal pal;
  52. u32 accept;
  53. struct list_head rsvs;
  54. struct dentry *root_d;
  55. struct dentry *command_f;
  56. struct dentry *reservations_f;
  57. struct dentry *accept_f;
  58. struct dentry *drp_avail_f;
  59. spinlock_t list_lock;
  60. };
  61. static struct dentry *root_dir;
  62. static void uwb_dbg_rsv_cb(struct uwb_rsv *rsv)
  63. {
  64. struct uwb_dbg *dbg = rsv->pal_priv;
  65. uwb_rsv_dump("debug", rsv);
  66. if (rsv->state == UWB_RSV_STATE_NONE) {
  67. spin_lock(&dbg->list_lock);
  68. list_del(&rsv->pal_node);
  69. spin_unlock(&dbg->list_lock);
  70. uwb_rsv_destroy(rsv);
  71. }
  72. }
  73. static int cmd_rsv_establish(struct uwb_rc *rc,
  74. struct uwb_dbg_cmd_rsv_establish *cmd)
  75. {
  76. struct uwb_mac_addr macaddr;
  77. struct uwb_rsv *rsv;
  78. struct uwb_dev *target;
  79. int ret;
  80. memcpy(&macaddr, cmd->target, sizeof(macaddr));
  81. target = uwb_dev_get_by_macaddr(rc, &macaddr);
  82. if (target == NULL)
  83. return -ENODEV;
  84. rsv = uwb_rsv_create(rc, uwb_dbg_rsv_cb, rc->dbg);
  85. if (rsv == NULL) {
  86. uwb_dev_put(target);
  87. return -ENOMEM;
  88. }
  89. rsv->target.type = UWB_RSV_TARGET_DEV;
  90. rsv->target.dev = target;
  91. rsv->type = cmd->type;
  92. rsv->max_mas = cmd->max_mas;
  93. rsv->min_mas = cmd->min_mas;
  94. rsv->max_interval = cmd->max_interval;
  95. ret = uwb_rsv_establish(rsv);
  96. if (ret)
  97. uwb_rsv_destroy(rsv);
  98. else {
  99. spin_lock(&(rc->dbg)->list_lock);
  100. list_add_tail(&rsv->pal_node, &rc->dbg->rsvs);
  101. spin_unlock(&(rc->dbg)->list_lock);
  102. }
  103. return ret;
  104. }
  105. static int cmd_rsv_terminate(struct uwb_rc *rc,
  106. struct uwb_dbg_cmd_rsv_terminate *cmd)
  107. {
  108. struct uwb_rsv *rsv, *found = NULL;
  109. int i = 0;
  110. spin_lock(&(rc->dbg)->list_lock);
  111. list_for_each_entry(rsv, &rc->dbg->rsvs, pal_node) {
  112. if (i == cmd->index) {
  113. found = rsv;
  114. uwb_rsv_get(found);
  115. break;
  116. }
  117. i++;
  118. }
  119. spin_unlock(&(rc->dbg)->list_lock);
  120. if (!found)
  121. return -EINVAL;
  122. uwb_rsv_terminate(found);
  123. uwb_rsv_put(found);
  124. return 0;
  125. }
  126. static int cmd_ie_add(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_add)
  127. {
  128. return uwb_rc_ie_add(rc,
  129. (const struct uwb_ie_hdr *) ie_to_add->data,
  130. ie_to_add->len);
  131. }
  132. static int cmd_ie_rm(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_rm)
  133. {
  134. return uwb_rc_ie_rm(rc, ie_to_rm->data[0]);
  135. }
  136. static int command_open(struct inode *inode, struct file *file)
  137. {
  138. file->private_data = inode->i_private;
  139. return 0;
  140. }
  141. static ssize_t command_write(struct file *file, const char __user *buf,
  142. size_t len, loff_t *off)
  143. {
  144. struct uwb_rc *rc = file->private_data;
  145. struct uwb_dbg_cmd cmd;
  146. int ret = 0;
  147. if (len != sizeof(struct uwb_dbg_cmd))
  148. return -EINVAL;
  149. if (copy_from_user(&cmd, buf, len) != 0)
  150. return -EFAULT;
  151. switch (cmd.type) {
  152. case UWB_DBG_CMD_RSV_ESTABLISH:
  153. ret = cmd_rsv_establish(rc, &cmd.rsv_establish);
  154. break;
  155. case UWB_DBG_CMD_RSV_TERMINATE:
  156. ret = cmd_rsv_terminate(rc, &cmd.rsv_terminate);
  157. break;
  158. case UWB_DBG_CMD_IE_ADD:
  159. ret = cmd_ie_add(rc, &cmd.ie_add);
  160. break;
  161. case UWB_DBG_CMD_IE_RM:
  162. ret = cmd_ie_rm(rc, &cmd.ie_rm);
  163. break;
  164. case UWB_DBG_CMD_RADIO_START:
  165. ret = uwb_radio_start(&rc->dbg->pal);
  166. break;
  167. case UWB_DBG_CMD_RADIO_STOP:
  168. uwb_radio_stop(&rc->dbg->pal);
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. return ret < 0 ? ret : len;
  174. }
  175. static const struct file_operations command_fops = {
  176. .open = command_open,
  177. .write = command_write,
  178. .read = NULL,
  179. .llseek = no_llseek,
  180. .owner = THIS_MODULE,
  181. };
  182. static int reservations_print(struct seq_file *s, void *p)
  183. {
  184. struct uwb_rc *rc = s->private;
  185. struct uwb_rsv *rsv;
  186. mutex_lock(&rc->rsvs_mutex);
  187. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  188. struct uwb_dev_addr devaddr;
  189. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  190. bool is_owner;
  191. char buf[72];
  192. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  193. if (rsv->target.type == UWB_RSV_TARGET_DEV) {
  194. devaddr = rsv->target.dev->dev_addr;
  195. is_owner = &rc->uwb_dev == rsv->owner;
  196. } else {
  197. devaddr = rsv->target.devaddr;
  198. is_owner = true;
  199. }
  200. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  201. seq_printf(s, "%c %s -> %s: %s\n",
  202. is_owner ? 'O' : 'T',
  203. owner, target, uwb_rsv_state_str(rsv->state));
  204. seq_printf(s, " stream: %d type: %s\n",
  205. rsv->stream, uwb_rsv_type_str(rsv->type));
  206. bitmap_scnprintf(buf, sizeof(buf), rsv->mas.bm, UWB_NUM_MAS);
  207. seq_printf(s, " %s\n", buf);
  208. }
  209. mutex_unlock(&rc->rsvs_mutex);
  210. return 0;
  211. }
  212. static int reservations_open(struct inode *inode, struct file *file)
  213. {
  214. return single_open(file, reservations_print, inode->i_private);
  215. }
  216. static const struct file_operations reservations_fops = {
  217. .open = reservations_open,
  218. .read = seq_read,
  219. .llseek = seq_lseek,
  220. .release = single_release,
  221. .owner = THIS_MODULE,
  222. };
  223. static int drp_avail_print(struct seq_file *s, void *p)
  224. {
  225. struct uwb_rc *rc = s->private;
  226. char buf[72];
  227. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.global, UWB_NUM_MAS);
  228. seq_printf(s, "global: %s\n", buf);
  229. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.local, UWB_NUM_MAS);
  230. seq_printf(s, "local: %s\n", buf);
  231. bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.pending, UWB_NUM_MAS);
  232. seq_printf(s, "pending: %s\n", buf);
  233. return 0;
  234. }
  235. static int drp_avail_open(struct inode *inode, struct file *file)
  236. {
  237. return single_open(file, drp_avail_print, inode->i_private);
  238. }
  239. static const struct file_operations drp_avail_fops = {
  240. .open = drp_avail_open,
  241. .read = seq_read,
  242. .llseek = seq_lseek,
  243. .release = single_release,
  244. .owner = THIS_MODULE,
  245. };
  246. static void uwb_dbg_channel_changed(struct uwb_pal *pal, int channel)
  247. {
  248. struct device *dev = &pal->rc->uwb_dev.dev;
  249. if (channel > 0)
  250. dev_info(dev, "debug: channel %d started\n", channel);
  251. else
  252. dev_info(dev, "debug: channel stopped\n");
  253. }
  254. static void uwb_dbg_new_rsv(struct uwb_pal *pal, struct uwb_rsv *rsv)
  255. {
  256. struct uwb_dbg *dbg = container_of(pal, struct uwb_dbg, pal);
  257. if (dbg->accept) {
  258. spin_lock(&dbg->list_lock);
  259. list_add_tail(&rsv->pal_node, &dbg->rsvs);
  260. spin_unlock(&dbg->list_lock);
  261. uwb_rsv_accept(rsv, uwb_dbg_rsv_cb, dbg);
  262. }
  263. }
  264. /**
  265. * uwb_dbg_add_rc - add a debug interface for a radio controller
  266. * @rc: the radio controller
  267. */
  268. void uwb_dbg_add_rc(struct uwb_rc *rc)
  269. {
  270. rc->dbg = kzalloc(sizeof(struct uwb_dbg), GFP_KERNEL);
  271. if (rc->dbg == NULL)
  272. return;
  273. INIT_LIST_HEAD(&rc->dbg->rsvs);
  274. spin_lock_init(&(rc->dbg)->list_lock);
  275. uwb_pal_init(&rc->dbg->pal);
  276. rc->dbg->pal.rc = rc;
  277. rc->dbg->pal.channel_changed = uwb_dbg_channel_changed;
  278. rc->dbg->pal.new_rsv = uwb_dbg_new_rsv;
  279. uwb_pal_register(&rc->dbg->pal);
  280. if (root_dir) {
  281. rc->dbg->root_d = debugfs_create_dir(dev_name(&rc->uwb_dev.dev),
  282. root_dir);
  283. rc->dbg->command_f = debugfs_create_file("command", 0200,
  284. rc->dbg->root_d, rc,
  285. &command_fops);
  286. rc->dbg->reservations_f = debugfs_create_file("reservations", 0444,
  287. rc->dbg->root_d, rc,
  288. &reservations_fops);
  289. rc->dbg->accept_f = debugfs_create_bool("accept", 0644,
  290. rc->dbg->root_d,
  291. &rc->dbg->accept);
  292. rc->dbg->drp_avail_f = debugfs_create_file("drp_avail", 0444,
  293. rc->dbg->root_d, rc,
  294. &drp_avail_fops);
  295. }
  296. }
  297. /**
  298. * uwb_dbg_del_rc - remove a radio controller's debug interface
  299. * @rc: the radio controller
  300. */
  301. void uwb_dbg_del_rc(struct uwb_rc *rc)
  302. {
  303. struct uwb_rsv *rsv, *t;
  304. if (rc->dbg == NULL)
  305. return;
  306. list_for_each_entry_safe(rsv, t, &rc->dbg->rsvs, pal_node) {
  307. uwb_rsv_terminate(rsv);
  308. }
  309. uwb_pal_unregister(&rc->dbg->pal);
  310. if (root_dir) {
  311. debugfs_remove(rc->dbg->drp_avail_f);
  312. debugfs_remove(rc->dbg->accept_f);
  313. debugfs_remove(rc->dbg->reservations_f);
  314. debugfs_remove(rc->dbg->command_f);
  315. debugfs_remove(rc->dbg->root_d);
  316. }
  317. }
  318. /**
  319. * uwb_dbg_exit - initialize the debug interface sub-module
  320. */
  321. void uwb_dbg_init(void)
  322. {
  323. root_dir = debugfs_create_dir("uwb", NULL);
  324. }
  325. /**
  326. * uwb_dbg_exit - clean-up the debug interface sub-module
  327. */
  328. void uwb_dbg_exit(void)
  329. {
  330. debugfs_remove(root_dir);
  331. }
  332. /**
  333. * uwb_dbg_create_pal_dir - create a debugfs directory for a PAL
  334. * @pal: The PAL.
  335. */
  336. struct dentry *uwb_dbg_create_pal_dir(struct uwb_pal *pal)
  337. {
  338. struct uwb_rc *rc = pal->rc;
  339. if (root_dir && rc->dbg && rc->dbg->root_d && pal->name)
  340. return debugfs_create_dir(pal->name, rc->dbg->root_d);
  341. return NULL;
  342. }