uwb-debug.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. bool 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 ssize_t command_write(struct file *file, const char __user *buf,
  137. size_t len, loff_t *off)
  138. {
  139. struct uwb_rc *rc = file->private_data;
  140. struct uwb_dbg_cmd cmd;
  141. int ret = 0;
  142. if (len != sizeof(struct uwb_dbg_cmd))
  143. return -EINVAL;
  144. if (copy_from_user(&cmd, buf, len) != 0)
  145. return -EFAULT;
  146. switch (cmd.type) {
  147. case UWB_DBG_CMD_RSV_ESTABLISH:
  148. ret = cmd_rsv_establish(rc, &cmd.rsv_establish);
  149. break;
  150. case UWB_DBG_CMD_RSV_TERMINATE:
  151. ret = cmd_rsv_terminate(rc, &cmd.rsv_terminate);
  152. break;
  153. case UWB_DBG_CMD_IE_ADD:
  154. ret = cmd_ie_add(rc, &cmd.ie_add);
  155. break;
  156. case UWB_DBG_CMD_IE_RM:
  157. ret = cmd_ie_rm(rc, &cmd.ie_rm);
  158. break;
  159. case UWB_DBG_CMD_RADIO_START:
  160. ret = uwb_radio_start(&rc->dbg->pal);
  161. break;
  162. case UWB_DBG_CMD_RADIO_STOP:
  163. uwb_radio_stop(&rc->dbg->pal);
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. return ret < 0 ? ret : len;
  169. }
  170. static const struct file_operations command_fops = {
  171. .open = simple_open,
  172. .write = command_write,
  173. .read = NULL,
  174. .llseek = no_llseek,
  175. .owner = THIS_MODULE,
  176. };
  177. static int reservations_print(struct seq_file *s, void *p)
  178. {
  179. struct uwb_rc *rc = s->private;
  180. struct uwb_rsv *rsv;
  181. mutex_lock(&rc->rsvs_mutex);
  182. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  183. struct uwb_dev_addr devaddr;
  184. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  185. bool is_owner;
  186. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  187. if (rsv->target.type == UWB_RSV_TARGET_DEV) {
  188. devaddr = rsv->target.dev->dev_addr;
  189. is_owner = &rc->uwb_dev == rsv->owner;
  190. } else {
  191. devaddr = rsv->target.devaddr;
  192. is_owner = true;
  193. }
  194. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  195. seq_printf(s, "%c %s -> %s: %s\n",
  196. is_owner ? 'O' : 'T',
  197. owner, target, uwb_rsv_state_str(rsv->state));
  198. seq_printf(s, " stream: %d type: %s\n",
  199. rsv->stream, uwb_rsv_type_str(rsv->type));
  200. seq_printf(s, " %*pb\n", UWB_NUM_MAS, rsv->mas.bm);
  201. }
  202. mutex_unlock(&rc->rsvs_mutex);
  203. return 0;
  204. }
  205. static int reservations_open(struct inode *inode, struct file *file)
  206. {
  207. return single_open(file, reservations_print, inode->i_private);
  208. }
  209. static const struct file_operations reservations_fops = {
  210. .open = reservations_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = single_release,
  214. .owner = THIS_MODULE,
  215. };
  216. static int drp_avail_print(struct seq_file *s, void *p)
  217. {
  218. struct uwb_rc *rc = s->private;
  219. seq_printf(s, "global: %*pb\n", UWB_NUM_MAS, rc->drp_avail.global);
  220. seq_printf(s, "local: %*pb\n", UWB_NUM_MAS, rc->drp_avail.local);
  221. seq_printf(s, "pending: %*pb\n", UWB_NUM_MAS, rc->drp_avail.pending);
  222. return 0;
  223. }
  224. static int drp_avail_open(struct inode *inode, struct file *file)
  225. {
  226. return single_open(file, drp_avail_print, inode->i_private);
  227. }
  228. static const struct file_operations drp_avail_fops = {
  229. .open = drp_avail_open,
  230. .read = seq_read,
  231. .llseek = seq_lseek,
  232. .release = single_release,
  233. .owner = THIS_MODULE,
  234. };
  235. static void uwb_dbg_channel_changed(struct uwb_pal *pal, int channel)
  236. {
  237. struct device *dev = &pal->rc->uwb_dev.dev;
  238. if (channel > 0)
  239. dev_info(dev, "debug: channel %d started\n", channel);
  240. else
  241. dev_info(dev, "debug: channel stopped\n");
  242. }
  243. static void uwb_dbg_new_rsv(struct uwb_pal *pal, struct uwb_rsv *rsv)
  244. {
  245. struct uwb_dbg *dbg = container_of(pal, struct uwb_dbg, pal);
  246. if (dbg->accept) {
  247. spin_lock(&dbg->list_lock);
  248. list_add_tail(&rsv->pal_node, &dbg->rsvs);
  249. spin_unlock(&dbg->list_lock);
  250. uwb_rsv_accept(rsv, uwb_dbg_rsv_cb, dbg);
  251. }
  252. }
  253. /**
  254. * uwb_dbg_add_rc - add a debug interface for a radio controller
  255. * @rc: the radio controller
  256. */
  257. void uwb_dbg_add_rc(struct uwb_rc *rc)
  258. {
  259. rc->dbg = kzalloc(sizeof(struct uwb_dbg), GFP_KERNEL);
  260. if (rc->dbg == NULL)
  261. return;
  262. INIT_LIST_HEAD(&rc->dbg->rsvs);
  263. spin_lock_init(&(rc->dbg)->list_lock);
  264. uwb_pal_init(&rc->dbg->pal);
  265. rc->dbg->pal.rc = rc;
  266. rc->dbg->pal.channel_changed = uwb_dbg_channel_changed;
  267. rc->dbg->pal.new_rsv = uwb_dbg_new_rsv;
  268. uwb_pal_register(&rc->dbg->pal);
  269. if (root_dir) {
  270. rc->dbg->root_d = debugfs_create_dir(dev_name(&rc->uwb_dev.dev),
  271. root_dir);
  272. rc->dbg->command_f = debugfs_create_file("command", 0200,
  273. rc->dbg->root_d, rc,
  274. &command_fops);
  275. rc->dbg->reservations_f = debugfs_create_file("reservations", 0444,
  276. rc->dbg->root_d, rc,
  277. &reservations_fops);
  278. rc->dbg->accept_f = debugfs_create_bool("accept", 0644,
  279. rc->dbg->root_d,
  280. &rc->dbg->accept);
  281. rc->dbg->drp_avail_f = debugfs_create_file("drp_avail", 0444,
  282. rc->dbg->root_d, rc,
  283. &drp_avail_fops);
  284. }
  285. }
  286. /**
  287. * uwb_dbg_del_rc - remove a radio controller's debug interface
  288. * @rc: the radio controller
  289. */
  290. void uwb_dbg_del_rc(struct uwb_rc *rc)
  291. {
  292. struct uwb_rsv *rsv, *t;
  293. if (rc->dbg == NULL)
  294. return;
  295. list_for_each_entry_safe(rsv, t, &rc->dbg->rsvs, pal_node) {
  296. uwb_rsv_terminate(rsv);
  297. }
  298. uwb_pal_unregister(&rc->dbg->pal);
  299. if (root_dir) {
  300. debugfs_remove(rc->dbg->drp_avail_f);
  301. debugfs_remove(rc->dbg->accept_f);
  302. debugfs_remove(rc->dbg->reservations_f);
  303. debugfs_remove(rc->dbg->command_f);
  304. debugfs_remove(rc->dbg->root_d);
  305. }
  306. }
  307. /**
  308. * uwb_dbg_exit - initialize the debug interface sub-module
  309. */
  310. void uwb_dbg_init(void)
  311. {
  312. root_dir = debugfs_create_dir("uwb", NULL);
  313. }
  314. /**
  315. * uwb_dbg_exit - clean-up the debug interface sub-module
  316. */
  317. void uwb_dbg_exit(void)
  318. {
  319. debugfs_remove(root_dir);
  320. }
  321. /**
  322. * uwb_dbg_create_pal_dir - create a debugfs directory for a PAL
  323. * @pal: The PAL.
  324. */
  325. struct dentry *uwb_dbg_create_pal_dir(struct uwb_pal *pal)
  326. {
  327. struct uwb_rc *rc = pal->rc;
  328. if (root_dir && rc->dbg && rc->dbg->root_d && pal->name)
  329. return debugfs_create_dir(pal->name, rc->dbg->root_d);
  330. return NULL;
  331. }