1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2009 Luis R. Rodriguez <lrodriguez@atheros.com>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2023 Intel Corporation
10 #include <linux/slab.h>
14 #define DEBUGFS_READONLY_FILE(name, buflen, fmt, value...) \
15 static ssize_t name## _read(struct file *file, char __user *userbuf, \
16 size_t count, loff_t *ppos) \
18 struct wiphy *wiphy = file->private_data; \
22 res = scnprintf(buf, buflen, fmt "\n", ##value); \
23 return simple_read_from_buffer(userbuf, count, ppos, buf, res); \
26 static const struct file_operations name## _ops = { \
27 .read = name## _read, \
28 .open = simple_open, \
29 .llseek = generic_file_llseek, \
32 DEBUGFS_READONLY_FILE(rts_threshold
, 20, "%d",
33 wiphy
->rts_threshold
);
34 DEBUGFS_READONLY_FILE(fragmentation_threshold
, 20, "%d",
35 wiphy
->frag_threshold
);
36 DEBUGFS_READONLY_FILE(short_retry_limit
, 20, "%d",
38 DEBUGFS_READONLY_FILE(long_retry_limit
, 20, "%d",
41 static int ht_print_chan(struct ieee80211_channel
*chan
,
42 char *buf
, int buf_size
, int offset
)
44 if (WARN_ON(offset
> buf_size
))
47 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
48 return scnprintf(buf
+ offset
,
53 return scnprintf(buf
+ offset
,
57 (chan
->flags
& IEEE80211_CHAN_NO_HT40MINUS
) ?
59 (chan
->flags
& IEEE80211_CHAN_NO_HT40PLUS
) ?
63 static ssize_t
ht40allow_map_read(struct file
*file
,
64 char __user
*user_buf
,
65 size_t count
, loff_t
*ppos
)
67 struct wiphy
*wiphy
= file
->private_data
;
69 unsigned int offset
= 0, buf_size
= PAGE_SIZE
, i
;
70 enum nl80211_band band
;
71 struct ieee80211_supported_band
*sband
;
74 buf
= kzalloc(buf_size
, GFP_KERNEL
);
78 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
79 sband
= wiphy
->bands
[band
];
82 for (i
= 0; i
< sband
->n_channels
; i
++)
83 offset
+= ht_print_chan(&sband
->channels
[i
],
84 buf
, buf_size
, offset
);
87 r
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, offset
);
94 static const struct file_operations ht40allow_map_ops
= {
95 .read
= ht40allow_map_read
,
97 .llseek
= default_llseek
,
100 #define DEBUGFS_ADD(name) \
101 debugfs_create_file(#name, 0444, phyd, &rdev->wiphy, &name## _ops)
103 void cfg80211_debugfs_rdev_add(struct cfg80211_registered_device
*rdev
)
105 struct dentry
*phyd
= rdev
->wiphy
.debugfsdir
;
107 DEBUGFS_ADD(rts_threshold
);
108 DEBUGFS_ADD(fragmentation_threshold
);
109 DEBUGFS_ADD(short_retry_limit
);
110 DEBUGFS_ADD(long_retry_limit
);
111 DEBUGFS_ADD(ht40allow_map
);
114 struct debugfs_read_work
{
115 struct wiphy_work work
;
116 ssize_t (*handler
)(struct wiphy
*wiphy
,
127 struct completion completion
;
130 static void wiphy_locked_debugfs_read_work(struct wiphy
*wiphy
,
131 struct wiphy_work
*work
)
133 struct debugfs_read_work
*w
= container_of(work
, typeof(*w
), work
);
135 w
->ret
= w
->handler(w
->wiphy
, w
->file
, w
->buf
, w
->bufsize
, w
->data
);
136 complete(&w
->completion
);
139 static void wiphy_locked_debugfs_read_cancel(struct dentry
*dentry
,
142 struct debugfs_read_work
*w
= data
;
144 wiphy_work_cancel(w
->wiphy
, &w
->work
);
145 complete(&w
->completion
);
148 ssize_t
wiphy_locked_debugfs_read(struct wiphy
*wiphy
, struct file
*file
,
149 char *buf
, size_t bufsize
,
150 char __user
*userbuf
, size_t count
,
152 ssize_t (*handler
)(struct wiphy
*wiphy
,
159 struct debugfs_read_work work
= {
167 .completion
= COMPLETION_INITIALIZER_ONSTACK(work
.completion
),
169 struct debugfs_cancellation cancellation
= {
170 .cancel
= wiphy_locked_debugfs_read_cancel
,
171 .cancel_data
= &work
,
174 /* don't leak stack data or whatever */
175 memset(buf
, 0, bufsize
);
177 wiphy_work_init(&work
.work
, wiphy_locked_debugfs_read_work
);
178 wiphy_work_queue(wiphy
, &work
.work
);
180 debugfs_enter_cancellation(file
, &cancellation
);
181 wait_for_completion(&work
.completion
);
182 debugfs_leave_cancellation(file
, &cancellation
);
187 if (WARN_ON(work
.ret
> bufsize
))
190 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, work
.ret
);
192 EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_read
);
194 struct debugfs_write_work
{
195 struct wiphy_work work
;
196 ssize_t (*handler
)(struct wiphy
*wiphy
,
207 struct completion completion
;
210 static void wiphy_locked_debugfs_write_work(struct wiphy
*wiphy
,
211 struct wiphy_work
*work
)
213 struct debugfs_write_work
*w
= container_of(work
, typeof(*w
), work
);
215 w
->ret
= w
->handler(w
->wiphy
, w
->file
, w
->buf
, w
->count
, w
->data
);
216 complete(&w
->completion
);
219 static void wiphy_locked_debugfs_write_cancel(struct dentry
*dentry
,
222 struct debugfs_write_work
*w
= data
;
224 wiphy_work_cancel(w
->wiphy
, &w
->work
);
225 complete(&w
->completion
);
228 ssize_t
wiphy_locked_debugfs_write(struct wiphy
*wiphy
,
229 struct file
*file
, char *buf
, size_t bufsize
,
230 const char __user
*userbuf
, size_t count
,
231 ssize_t (*handler
)(struct wiphy
*wiphy
,
238 struct debugfs_write_work work
= {
246 .completion
= COMPLETION_INITIALIZER_ONSTACK(work
.completion
),
248 struct debugfs_cancellation cancellation
= {
249 .cancel
= wiphy_locked_debugfs_write_cancel
,
250 .cancel_data
= &work
,
253 /* mostly used for strings so enforce NUL-termination for safety */
254 if (count
>= bufsize
)
257 memset(buf
, 0, bufsize
);
259 if (copy_from_user(buf
, userbuf
, count
))
262 wiphy_work_init(&work
.work
, wiphy_locked_debugfs_write_work
);
263 wiphy_work_queue(wiphy
, &work
.work
);
265 debugfs_enter_cancellation(file
, &cancellation
);
266 wait_for_completion(&work
.completion
);
267 debugfs_leave_cancellation(file
, &cancellation
);
271 EXPORT_SYMBOL_GPL(wiphy_locked_debugfs_write
);