]>
Commit | Line | Data |
---|---|---|
599e8d80 DV |
1 | /* |
2 | * Ultra Wide Band | |
3 | * Debug support | |
4 | * | |
5 | * Copyright (C) 2005-2006 Intel Corporation | |
6 | * Inaky Perez-Gonzalez <[email protected]> | |
e43ace89 | 7 | * Copyright (C) 2008 Cambridge Silicon Radio Ltd. |
599e8d80 DV |
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 | ||
27 | #include <linux/spinlock.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/notifier.h> | |
31 | #include <linux/device.h> | |
32 | #include <linux/debugfs.h> | |
33 | #include <linux/uaccess.h> | |
34 | #include <linux/seq_file.h> | |
35 | ||
36 | #include <linux/uwb/debug-cmd.h> | |
599e8d80 DV |
37 | |
38 | #include "uwb-internal.h" | |
39 | ||
599e8d80 DV |
40 | /* |
41 | * Debug interface | |
42 | * | |
43 | * Per radio controller debugfs files (in uwb/uwbN/): | |
44 | * | |
45 | * command: Flexible command interface (see <linux/uwb/debug-cmd.h>). | |
46 | * | |
47 | * reservations: information on reservations. | |
48 | * | |
49 | * accept: Set to true (Y or 1) to accept reservation requests from | |
50 | * peers. | |
51 | * | |
52 | * drp_avail: DRP availability information. | |
53 | */ | |
54 | ||
55 | struct uwb_dbg { | |
56 | struct uwb_pal pal; | |
57 | ||
58 | u32 accept; | |
59 | struct list_head rsvs; | |
60 | ||
61 | struct dentry *root_d; | |
62 | struct dentry *command_f; | |
63 | struct dentry *reservations_f; | |
64 | struct dentry *accept_f; | |
65 | struct dentry *drp_avail_f; | |
5b37717a | 66 | spinlock_t list_lock; |
599e8d80 DV |
67 | }; |
68 | ||
69 | static struct dentry *root_dir; | |
70 | ||
71 | static void uwb_dbg_rsv_cb(struct uwb_rsv *rsv) | |
72 | { | |
5b37717a | 73 | struct uwb_dbg *dbg = rsv->pal_priv; |
599e8d80 | 74 | |
5b37717a | 75 | uwb_rsv_dump("debug", rsv); |
cae1c114 DV |
76 | |
77 | if (rsv->state == UWB_RSV_STATE_NONE) { | |
5b37717a | 78 | spin_lock(&dbg->list_lock); |
cae1c114 | 79 | list_del(&rsv->pal_node); |
5b37717a | 80 | spin_unlock(&dbg->list_lock); |
cae1c114 DV |
81 | uwb_rsv_destroy(rsv); |
82 | } | |
599e8d80 DV |
83 | } |
84 | ||
85 | static int cmd_rsv_establish(struct uwb_rc *rc, | |
86 | struct uwb_dbg_cmd_rsv_establish *cmd) | |
87 | { | |
88 | struct uwb_mac_addr macaddr; | |
89 | struct uwb_rsv *rsv; | |
90 | struct uwb_dev *target; | |
91 | int ret; | |
92 | ||
93 | memcpy(&macaddr, cmd->target, sizeof(macaddr)); | |
94 | target = uwb_dev_get_by_macaddr(rc, &macaddr); | |
95 | if (target == NULL) | |
96 | return -ENODEV; | |
97 | ||
e4b49580 | 98 | rsv = uwb_rsv_create(rc, uwb_dbg_rsv_cb, rc->dbg); |
599e8d80 DV |
99 | if (rsv == NULL) { |
100 | uwb_dev_put(target); | |
101 | return -ENOMEM; | |
102 | } | |
103 | ||
5b37717a SP |
104 | rsv->target.type = UWB_RSV_TARGET_DEV; |
105 | rsv->target.dev = target; | |
106 | rsv->type = cmd->type; | |
107 | rsv->max_mas = cmd->max_mas; | |
108 | rsv->min_mas = cmd->min_mas; | |
109 | rsv->max_interval = cmd->max_interval; | |
599e8d80 DV |
110 | |
111 | ret = uwb_rsv_establish(rsv); | |
112 | if (ret) | |
113 | uwb_rsv_destroy(rsv); | |
5b37717a SP |
114 | else { |
115 | spin_lock(&(rc->dbg)->list_lock); | |
599e8d80 | 116 | list_add_tail(&rsv->pal_node, &rc->dbg->rsvs); |
5b37717a SP |
117 | spin_unlock(&(rc->dbg)->list_lock); |
118 | } | |
599e8d80 DV |
119 | return ret; |
120 | } | |
121 | ||
122 | static int cmd_rsv_terminate(struct uwb_rc *rc, | |
123 | struct uwb_dbg_cmd_rsv_terminate *cmd) | |
124 | { | |
125 | struct uwb_rsv *rsv, *found = NULL; | |
126 | int i = 0; | |
127 | ||
5b37717a SP |
128 | spin_lock(&(rc->dbg)->list_lock); |
129 | ||
599e8d80 DV |
130 | list_for_each_entry(rsv, &rc->dbg->rsvs, pal_node) { |
131 | if (i == cmd->index) { | |
132 | found = rsv; | |
5b37717a | 133 | uwb_rsv_get(found); |
599e8d80 DV |
134 | break; |
135 | } | |
cae1c114 | 136 | i++; |
599e8d80 | 137 | } |
5b37717a SP |
138 | |
139 | spin_unlock(&(rc->dbg)->list_lock); | |
140 | ||
599e8d80 DV |
141 | if (!found) |
142 | return -EINVAL; | |
143 | ||
599e8d80 | 144 | uwb_rsv_terminate(found); |
5b37717a | 145 | uwb_rsv_put(found); |
599e8d80 DV |
146 | |
147 | return 0; | |
148 | } | |
149 | ||
6d5a681d SP |
150 | static int cmd_ie_add(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_add) |
151 | { | |
152 | return uwb_rc_ie_add(rc, | |
153 | (const struct uwb_ie_hdr *) ie_to_add->data, | |
154 | ie_to_add->len); | |
155 | } | |
156 | ||
157 | static int cmd_ie_rm(struct uwb_rc *rc, struct uwb_dbg_cmd_ie *ie_to_rm) | |
158 | { | |
159 | return uwb_rc_ie_rm(rc, ie_to_rm->data[0]); | |
160 | } | |
161 | ||
599e8d80 DV |
162 | static int command_open(struct inode *inode, struct file *file) |
163 | { | |
164 | file->private_data = inode->i_private; | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
169 | static ssize_t command_write(struct file *file, const char __user *buf, | |
170 | size_t len, loff_t *off) | |
171 | { | |
172 | struct uwb_rc *rc = file->private_data; | |
173 | struct uwb_dbg_cmd cmd; | |
6fae35f9 | 174 | int ret = 0; |
5b37717a | 175 | |
599e8d80 DV |
176 | if (len != sizeof(struct uwb_dbg_cmd)) |
177 | return -EINVAL; | |
178 | ||
179 | if (copy_from_user(&cmd, buf, len) != 0) | |
180 | return -EFAULT; | |
181 | ||
182 | switch (cmd.type) { | |
183 | case UWB_DBG_CMD_RSV_ESTABLISH: | |
184 | ret = cmd_rsv_establish(rc, &cmd.rsv_establish); | |
185 | break; | |
186 | case UWB_DBG_CMD_RSV_TERMINATE: | |
187 | ret = cmd_rsv_terminate(rc, &cmd.rsv_terminate); | |
188 | break; | |
6d5a681d SP |
189 | case UWB_DBG_CMD_IE_ADD: |
190 | ret = cmd_ie_add(rc, &cmd.ie_add); | |
191 | break; | |
192 | case UWB_DBG_CMD_IE_RM: | |
193 | ret = cmd_ie_rm(rc, &cmd.ie_rm); | |
194 | break; | |
6fae35f9 DV |
195 | case UWB_DBG_CMD_RADIO_START: |
196 | ret = uwb_radio_start(&rc->dbg->pal); | |
197 | break; | |
198 | case UWB_DBG_CMD_RADIO_STOP: | |
199 | uwb_radio_stop(&rc->dbg->pal); | |
200 | break; | |
599e8d80 DV |
201 | default: |
202 | return -EINVAL; | |
203 | } | |
204 | ||
205 | return ret < 0 ? ret : len; | |
206 | } | |
207 | ||
828c0950 | 208 | static const struct file_operations command_fops = { |
599e8d80 DV |
209 | .open = command_open, |
210 | .write = command_write, | |
211 | .read = NULL, | |
212 | .llseek = no_llseek, | |
213 | .owner = THIS_MODULE, | |
214 | }; | |
215 | ||
216 | static int reservations_print(struct seq_file *s, void *p) | |
217 | { | |
218 | struct uwb_rc *rc = s->private; | |
219 | struct uwb_rsv *rsv; | |
220 | ||
221 | mutex_lock(&rc->rsvs_mutex); | |
222 | ||
223 | list_for_each_entry(rsv, &rc->reservations, rc_node) { | |
224 | struct uwb_dev_addr devaddr; | |
225 | char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE]; | |
226 | bool is_owner; | |
227 | char buf[72]; | |
228 | ||
229 | uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr); | |
230 | if (rsv->target.type == UWB_RSV_TARGET_DEV) { | |
231 | devaddr = rsv->target.dev->dev_addr; | |
232 | is_owner = &rc->uwb_dev == rsv->owner; | |
233 | } else { | |
234 | devaddr = rsv->target.devaddr; | |
235 | is_owner = true; | |
236 | } | |
237 | uwb_dev_addr_print(target, sizeof(target), &devaddr); | |
238 | ||
239 | seq_printf(s, "%c %s -> %s: %s\n", | |
240 | is_owner ? 'O' : 'T', | |
241 | owner, target, uwb_rsv_state_str(rsv->state)); | |
242 | seq_printf(s, " stream: %d type: %s\n", | |
243 | rsv->stream, uwb_rsv_type_str(rsv->type)); | |
244 | bitmap_scnprintf(buf, sizeof(buf), rsv->mas.bm, UWB_NUM_MAS); | |
245 | seq_printf(s, " %s\n", buf); | |
246 | } | |
247 | ||
248 | mutex_unlock(&rc->rsvs_mutex); | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static int reservations_open(struct inode *inode, struct file *file) | |
254 | { | |
255 | return single_open(file, reservations_print, inode->i_private); | |
256 | } | |
257 | ||
828c0950 | 258 | static const struct file_operations reservations_fops = { |
599e8d80 DV |
259 | .open = reservations_open, |
260 | .read = seq_read, | |
261 | .llseek = seq_lseek, | |
262 | .release = single_release, | |
263 | .owner = THIS_MODULE, | |
264 | }; | |
265 | ||
266 | static int drp_avail_print(struct seq_file *s, void *p) | |
267 | { | |
268 | struct uwb_rc *rc = s->private; | |
269 | char buf[72]; | |
270 | ||
271 | bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.global, UWB_NUM_MAS); | |
272 | seq_printf(s, "global: %s\n", buf); | |
273 | bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.local, UWB_NUM_MAS); | |
274 | seq_printf(s, "local: %s\n", buf); | |
275 | bitmap_scnprintf(buf, sizeof(buf), rc->drp_avail.pending, UWB_NUM_MAS); | |
276 | seq_printf(s, "pending: %s\n", buf); | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | static int drp_avail_open(struct inode *inode, struct file *file) | |
282 | { | |
283 | return single_open(file, drp_avail_print, inode->i_private); | |
284 | } | |
285 | ||
828c0950 | 286 | static const struct file_operations drp_avail_fops = { |
599e8d80 DV |
287 | .open = drp_avail_open, |
288 | .read = seq_read, | |
289 | .llseek = seq_lseek, | |
290 | .release = single_release, | |
291 | .owner = THIS_MODULE, | |
292 | }; | |
293 | ||
6fae35f9 DV |
294 | static void uwb_dbg_channel_changed(struct uwb_pal *pal, int channel) |
295 | { | |
6fae35f9 DV |
296 | struct device *dev = &pal->rc->uwb_dev.dev; |
297 | ||
298 | if (channel > 0) | |
299 | dev_info(dev, "debug: channel %d started\n", channel); | |
300 | else | |
301 | dev_info(dev, "debug: channel stopped\n"); | |
302 | } | |
303 | ||
e17be2b2 | 304 | static void uwb_dbg_new_rsv(struct uwb_pal *pal, struct uwb_rsv *rsv) |
599e8d80 | 305 | { |
e17be2b2 | 306 | struct uwb_dbg *dbg = container_of(pal, struct uwb_dbg, pal); |
599e8d80 | 307 | |
e17be2b2 | 308 | if (dbg->accept) { |
5b37717a | 309 | spin_lock(&dbg->list_lock); |
e17be2b2 | 310 | list_add_tail(&rsv->pal_node, &dbg->rsvs); |
5b37717a | 311 | spin_unlock(&dbg->list_lock); |
e17be2b2 | 312 | uwb_rsv_accept(rsv, uwb_dbg_rsv_cb, dbg); |
cae1c114 | 313 | } |
599e8d80 DV |
314 | } |
315 | ||
316 | /** | |
317 | * uwb_dbg_add_rc - add a debug interface for a radio controller | |
318 | * @rc: the radio controller | |
319 | */ | |
320 | void uwb_dbg_add_rc(struct uwb_rc *rc) | |
321 | { | |
322 | rc->dbg = kzalloc(sizeof(struct uwb_dbg), GFP_KERNEL); | |
323 | if (rc->dbg == NULL) | |
324 | return; | |
325 | ||
326 | INIT_LIST_HEAD(&rc->dbg->rsvs); | |
5b37717a | 327 | spin_lock_init(&(rc->dbg)->list_lock); |
599e8d80 DV |
328 | |
329 | uwb_pal_init(&rc->dbg->pal); | |
6fae35f9 DV |
330 | rc->dbg->pal.rc = rc; |
331 | rc->dbg->pal.channel_changed = uwb_dbg_channel_changed; | |
599e8d80 | 332 | rc->dbg->pal.new_rsv = uwb_dbg_new_rsv; |
6fae35f9 DV |
333 | uwb_pal_register(&rc->dbg->pal); |
334 | ||
599e8d80 DV |
335 | if (root_dir) { |
336 | rc->dbg->root_d = debugfs_create_dir(dev_name(&rc->uwb_dev.dev), | |
337 | root_dir); | |
338 | rc->dbg->command_f = debugfs_create_file("command", 0200, | |
339 | rc->dbg->root_d, rc, | |
340 | &command_fops); | |
341 | rc->dbg->reservations_f = debugfs_create_file("reservations", 0444, | |
342 | rc->dbg->root_d, rc, | |
343 | &reservations_fops); | |
344 | rc->dbg->accept_f = debugfs_create_bool("accept", 0644, | |
345 | rc->dbg->root_d, | |
346 | &rc->dbg->accept); | |
347 | rc->dbg->drp_avail_f = debugfs_create_file("drp_avail", 0444, | |
348 | rc->dbg->root_d, rc, | |
349 | &drp_avail_fops); | |
350 | } | |
351 | } | |
352 | ||
353 | /** | |
6d5a681d | 354 | * uwb_dbg_del_rc - remove a radio controller's debug interface |
599e8d80 DV |
355 | * @rc: the radio controller |
356 | */ | |
357 | void uwb_dbg_del_rc(struct uwb_rc *rc) | |
358 | { | |
359 | struct uwb_rsv *rsv, *t; | |
360 | ||
361 | if (rc->dbg == NULL) | |
362 | return; | |
363 | ||
364 | list_for_each_entry_safe(rsv, t, &rc->dbg->rsvs, pal_node) { | |
cae1c114 | 365 | uwb_rsv_terminate(rsv); |
599e8d80 DV |
366 | } |
367 | ||
6fae35f9 | 368 | uwb_pal_unregister(&rc->dbg->pal); |
599e8d80 DV |
369 | |
370 | if (root_dir) { | |
371 | debugfs_remove(rc->dbg->drp_avail_f); | |
372 | debugfs_remove(rc->dbg->accept_f); | |
373 | debugfs_remove(rc->dbg->reservations_f); | |
374 | debugfs_remove(rc->dbg->command_f); | |
375 | debugfs_remove(rc->dbg->root_d); | |
376 | } | |
377 | } | |
378 | ||
379 | /** | |
380 | * uwb_dbg_exit - initialize the debug interface sub-module | |
381 | */ | |
382 | void uwb_dbg_init(void) | |
383 | { | |
384 | root_dir = debugfs_create_dir("uwb", NULL); | |
385 | } | |
386 | ||
387 | /** | |
388 | * uwb_dbg_exit - clean-up the debug interface sub-module | |
389 | */ | |
390 | void uwb_dbg_exit(void) | |
391 | { | |
392 | debugfs_remove(root_dir); | |
393 | } | |
dcc7461e DV |
394 | |
395 | /** | |
396 | * uwb_dbg_create_pal_dir - create a debugfs directory for a PAL | |
397 | * @pal: The PAL. | |
398 | */ | |
399 | struct dentry *uwb_dbg_create_pal_dir(struct uwb_pal *pal) | |
400 | { | |
401 | struct uwb_rc *rc = pal->rc; | |
402 | ||
403 | if (root_dir && rc->dbg && rc->dbg->root_d && pal->name) | |
404 | return debugfs_create_dir(pal->name, rc->dbg->root_d); | |
405 | return NULL; | |
406 | } |