1 // SPDX-License-Identifier: GPL-2.0-only
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
13 #include <linux/spinlock.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/notifier.h>
17 #include <linux/device.h>
18 #include <linux/debugfs.h>
19 #include <linux/uaccess.h>
20 #include <linux/seq_file.h>
22 #include "include/debug-cmd.h"
23 #include "uwb-internal.h"
28 * Per radio controller debugfs files (in uwb/uwbN/):
30 * command: Flexible command interface (see <linux/uwb/debug-cmd.h>).
32 * reservations: information on reservations.
34 * accept: Set to true (Y or 1) to accept reservation requests from
37 * drp_avail: DRP availability information.
44 struct list_head rsvs
;
46 struct dentry
*root_d
;
47 struct dentry
*command_f
;
48 struct dentry
*reservations_f
;
49 struct dentry
*accept_f
;
50 struct dentry
*drp_avail_f
;
54 static struct dentry
*root_dir
;
56 static void uwb_dbg_rsv_cb(struct uwb_rsv
*rsv
)
58 struct uwb_dbg
*dbg
= rsv
->pal_priv
;
60 uwb_rsv_dump("debug", rsv
);
62 if (rsv
->state
== UWB_RSV_STATE_NONE
) {
63 spin_lock(&dbg
->list_lock
);
64 list_del(&rsv
->pal_node
);
65 spin_unlock(&dbg
->list_lock
);
70 static int cmd_rsv_establish(struct uwb_rc
*rc
,
71 struct uwb_dbg_cmd_rsv_establish
*cmd
)
73 struct uwb_mac_addr macaddr
;
75 struct uwb_dev
*target
;
78 memcpy(&macaddr
, cmd
->target
, sizeof(macaddr
));
79 target
= uwb_dev_get_by_macaddr(rc
, &macaddr
);
83 rsv
= uwb_rsv_create(rc
, uwb_dbg_rsv_cb
, rc
->dbg
);
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
;
96 ret
= uwb_rsv_establish(rsv
);
100 spin_lock(&(rc
->dbg
)->list_lock
);
101 list_add_tail(&rsv
->pal_node
, &rc
->dbg
->rsvs
);
102 spin_unlock(&(rc
->dbg
)->list_lock
);
107 static int cmd_rsv_terminate(struct uwb_rc
*rc
,
108 struct uwb_dbg_cmd_rsv_terminate
*cmd
)
110 struct uwb_rsv
*rsv
, *found
= NULL
;
113 spin_lock(&(rc
->dbg
)->list_lock
);
115 list_for_each_entry(rsv
, &rc
->dbg
->rsvs
, pal_node
) {
116 if (i
== cmd
->index
) {
124 spin_unlock(&(rc
->dbg
)->list_lock
);
129 uwb_rsv_terminate(found
);
135 static int cmd_ie_add(struct uwb_rc
*rc
, struct uwb_dbg_cmd_ie
*ie_to_add
)
137 return uwb_rc_ie_add(rc
,
138 (const struct uwb_ie_hdr
*) ie_to_add
->data
,
142 static int cmd_ie_rm(struct uwb_rc
*rc
, struct uwb_dbg_cmd_ie
*ie_to_rm
)
144 return uwb_rc_ie_rm(rc
, ie_to_rm
->data
[0]);
147 static ssize_t
command_write(struct file
*file
, const char __user
*buf
,
148 size_t len
, loff_t
*off
)
150 struct uwb_rc
*rc
= file
->private_data
;
151 struct uwb_dbg_cmd cmd
;
154 if (len
!= sizeof(struct uwb_dbg_cmd
))
157 if (copy_from_user(&cmd
, buf
, len
) != 0)
161 case UWB_DBG_CMD_RSV_ESTABLISH
:
162 ret
= cmd_rsv_establish(rc
, &cmd
.rsv_establish
);
164 case UWB_DBG_CMD_RSV_TERMINATE
:
165 ret
= cmd_rsv_terminate(rc
, &cmd
.rsv_terminate
);
167 case UWB_DBG_CMD_IE_ADD
:
168 ret
= cmd_ie_add(rc
, &cmd
.ie_add
);
170 case UWB_DBG_CMD_IE_RM
:
171 ret
= cmd_ie_rm(rc
, &cmd
.ie_rm
);
173 case UWB_DBG_CMD_RADIO_START
:
174 ret
= uwb_radio_start(&rc
->dbg
->pal
);
176 case UWB_DBG_CMD_RADIO_STOP
:
177 uwb_radio_stop(&rc
->dbg
->pal
);
183 return ret
< 0 ? ret
: len
;
186 static const struct file_operations command_fops
= {
188 .write
= command_write
,
191 .owner
= THIS_MODULE
,
194 static int reservations_show(struct seq_file
*s
, void *p
)
196 struct uwb_rc
*rc
= s
->private;
199 mutex_lock(&rc
->rsvs_mutex
);
201 list_for_each_entry(rsv
, &rc
->reservations
, rc_node
) {
202 struct uwb_dev_addr devaddr
;
203 char owner
[UWB_ADDR_STRSIZE
], target
[UWB_ADDR_STRSIZE
];
206 uwb_dev_addr_print(owner
, sizeof(owner
), &rsv
->owner
->dev_addr
);
207 if (rsv
->target
.type
== UWB_RSV_TARGET_DEV
) {
208 devaddr
= rsv
->target
.dev
->dev_addr
;
209 is_owner
= &rc
->uwb_dev
== rsv
->owner
;
211 devaddr
= rsv
->target
.devaddr
;
214 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 seq_printf(s
, " %*pb\n", UWB_NUM_MAS
, rsv
->mas
.bm
);
224 mutex_unlock(&rc
->rsvs_mutex
);
228 DEFINE_SHOW_ATTRIBUTE(reservations
);
230 static int drp_avail_show(struct seq_file
*s
, void *p
)
232 struct uwb_rc
*rc
= s
->private;
234 seq_printf(s
, "global: %*pb\n", UWB_NUM_MAS
, rc
->drp_avail
.global
);
235 seq_printf(s
, "local: %*pb\n", UWB_NUM_MAS
, rc
->drp_avail
.local
);
236 seq_printf(s
, "pending: %*pb\n", UWB_NUM_MAS
, rc
->drp_avail
.pending
);
240 DEFINE_SHOW_ATTRIBUTE(drp_avail
);
242 static void uwb_dbg_channel_changed(struct uwb_pal
*pal
, int channel
)
244 struct device
*dev
= &pal
->rc
->uwb_dev
.dev
;
247 dev_info(dev
, "debug: channel %d started\n", channel
);
249 dev_info(dev
, "debug: channel stopped\n");
252 static void uwb_dbg_new_rsv(struct uwb_pal
*pal
, struct uwb_rsv
*rsv
)
254 struct uwb_dbg
*dbg
= container_of(pal
, struct uwb_dbg
, pal
);
257 spin_lock(&dbg
->list_lock
);
258 list_add_tail(&rsv
->pal_node
, &dbg
->rsvs
);
259 spin_unlock(&dbg
->list_lock
);
260 uwb_rsv_accept(rsv
, uwb_dbg_rsv_cb
, dbg
);
265 * uwb_dbg_add_rc - add a debug interface for a radio controller
266 * @rc: the radio controller
268 void uwb_dbg_add_rc(struct uwb_rc
*rc
)
270 rc
->dbg
= kzalloc(sizeof(struct uwb_dbg
), GFP_KERNEL
);
274 INIT_LIST_HEAD(&rc
->dbg
->rsvs
);
275 spin_lock_init(&(rc
->dbg
)->list_lock
);
277 uwb_pal_init(&rc
->dbg
->pal
);
278 rc
->dbg
->pal
.rc
= rc
;
279 rc
->dbg
->pal
.channel_changed
= uwb_dbg_channel_changed
;
280 rc
->dbg
->pal
.new_rsv
= uwb_dbg_new_rsv
;
281 uwb_pal_register(&rc
->dbg
->pal
);
284 rc
->dbg
->root_d
= debugfs_create_dir(dev_name(&rc
->uwb_dev
.dev
),
286 rc
->dbg
->command_f
= debugfs_create_file("command", 0200,
289 rc
->dbg
->reservations_f
= debugfs_create_file("reservations", 0444,
292 rc
->dbg
->accept_f
= debugfs_create_bool("accept", 0644,
295 rc
->dbg
->drp_avail_f
= debugfs_create_file("drp_avail", 0444,
302 * uwb_dbg_del_rc - remove a radio controller's debug interface
303 * @rc: the radio controller
305 void uwb_dbg_del_rc(struct uwb_rc
*rc
)
307 struct uwb_rsv
*rsv
, *t
;
312 list_for_each_entry_safe(rsv
, t
, &rc
->dbg
->rsvs
, pal_node
) {
313 uwb_rsv_terminate(rsv
);
316 uwb_pal_unregister(&rc
->dbg
->pal
);
319 debugfs_remove(rc
->dbg
->drp_avail_f
);
320 debugfs_remove(rc
->dbg
->accept_f
);
321 debugfs_remove(rc
->dbg
->reservations_f
);
322 debugfs_remove(rc
->dbg
->command_f
);
323 debugfs_remove(rc
->dbg
->root_d
);
328 * uwb_dbg_exit - initialize the debug interface sub-module
330 void uwb_dbg_init(void)
332 root_dir
= debugfs_create_dir("uwb", NULL
);
336 * uwb_dbg_exit - clean-up the debug interface sub-module
338 void uwb_dbg_exit(void)
340 debugfs_remove(root_dir
);
344 * uwb_dbg_create_pal_dir - create a debugfs directory for a PAL
347 struct dentry
*uwb_dbg_create_pal_dir(struct uwb_pal
*pal
)
349 struct uwb_rc
*rc
= pal
->rc
;
351 if (root_dir
&& rc
->dbg
&& rc
->dbg
->root_d
&& pal
->name
)
352 return debugfs_create_dir(pal
->name
, rc
->dbg
->root_d
);