0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/fs.h>
0014 #include <linux/debugfs.h>
0015 #include <linux/slab.h>
0016 #include <linux/netdevice.h>
0017 #include <linux/pci.h>
0018 #include <linux/mutex.h>
0019
0020 #include "b43legacy.h"
0021 #include "main.h"
0022 #include "debugfs.h"
0023 #include "dma.h"
0024 #include "pio.h"
0025 #include "xmit.h"
0026
0027
0028
0029 static struct dentry *rootdir;
0030
0031 struct b43legacy_debugfs_fops {
0032 ssize_t (*read)(struct b43legacy_wldev *dev, char *buf, size_t bufsize);
0033 int (*write)(struct b43legacy_wldev *dev, const char *buf, size_t count);
0034 struct file_operations fops;
0035
0036 size_t file_struct_offset;
0037
0038 bool take_irqlock;
0039 };
0040
0041 static inline
0042 struct b43legacy_dfs_file * fops_to_dfs_file(struct b43legacy_wldev *dev,
0043 const struct b43legacy_debugfs_fops *dfops)
0044 {
0045 void *p;
0046
0047 p = dev->dfsentry;
0048 p += dfops->file_struct_offset;
0049
0050 return p;
0051 }
0052
0053
0054 #define fappend(fmt, x...) \
0055 do { \
0056 if (bufsize - count) \
0057 count += scnprintf(buf + count, \
0058 bufsize - count, \
0059 fmt , ##x); \
0060 else \
0061 printk(KERN_ERR "b43legacy: fappend overflow\n"); \
0062 } while (0)
0063
0064
0065
0066 static ssize_t tsf_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
0067 {
0068 ssize_t count = 0;
0069 u64 tsf;
0070
0071 b43legacy_tsf_read(dev, &tsf);
0072 fappend("0x%08x%08x\n",
0073 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
0074 (unsigned int)(tsf & 0xFFFFFFFFULL));
0075
0076 return count;
0077 }
0078
0079
0080 static int tsf_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
0081 {
0082 u64 tsf;
0083
0084 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
0085 return -EINVAL;
0086 b43legacy_tsf_write(dev, tsf);
0087
0088 return 0;
0089 }
0090
0091
0092 static ssize_t ucode_regs_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
0093 {
0094 ssize_t count = 0;
0095 int i;
0096
0097 for (i = 0; i < 64; i++) {
0098 fappend("r%d = 0x%04x\n", i,
0099 b43legacy_shm_read16(dev, B43legacy_SHM_WIRELESS, i));
0100 }
0101
0102 return count;
0103 }
0104
0105
0106 static ssize_t shm_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
0107 {
0108 ssize_t count = 0;
0109 int i;
0110 u16 tmp;
0111 __le16 *le16buf = (__le16 *)buf;
0112
0113 for (i = 0; i < 0x1000; i++) {
0114 if (bufsize < sizeof(tmp))
0115 break;
0116 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 2 * i);
0117 le16buf[i] = cpu_to_le16(tmp);
0118 count += sizeof(tmp);
0119 bufsize -= sizeof(tmp);
0120 }
0121
0122 return count;
0123 }
0124
0125 static ssize_t txstat_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
0126 {
0127 struct b43legacy_txstatus_log *log = &dev->dfsentry->txstatlog;
0128 ssize_t count = 0;
0129 unsigned long flags;
0130 int i, idx;
0131 struct b43legacy_txstatus *stat;
0132
0133 spin_lock_irqsave(&log->lock, flags);
0134 if (log->end < 0) {
0135 fappend("Nothing transmitted, yet\n");
0136 goto out_unlock;
0137 }
0138 fappend("b43legacy TX status reports:\n\n"
0139 "index | cookie | seq | phy_stat | frame_count | "
0140 "rts_count | supp_reason | pm_indicated | "
0141 "intermediate | for_ampdu | acked\n" "---\n");
0142 i = log->end + 1;
0143 idx = 0;
0144 while (1) {
0145 if (i == B43legacy_NR_LOGGED_TXSTATUS)
0146 i = 0;
0147 stat = &(log->log[i]);
0148 if (stat->cookie) {
0149 fappend("%03d | "
0150 "0x%04X | 0x%04X | 0x%02X | "
0151 "0x%X | 0x%X | "
0152 "%u | %u | "
0153 "%u | %u | %u\n",
0154 idx,
0155 stat->cookie, stat->seq, stat->phy_stat,
0156 stat->frame_count, stat->rts_count,
0157 stat->supp_reason, stat->pm_indicated,
0158 stat->intermediate, stat->for_ampdu,
0159 stat->acked);
0160 idx++;
0161 }
0162 if (i == log->end)
0163 break;
0164 i++;
0165 }
0166 out_unlock:
0167 spin_unlock_irqrestore(&log->lock, flags);
0168
0169 return count;
0170 }
0171
0172
0173 static int restart_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
0174 {
0175 int err = 0;
0176
0177 if (count > 0 && buf[0] == '1') {
0178 b43legacy_controller_restart(dev, "manually restarted");
0179 } else
0180 err = -EINVAL;
0181
0182 return err;
0183 }
0184
0185 #undef fappend
0186
0187 static ssize_t b43legacy_debugfs_read(struct file *file, char __user *userbuf,
0188 size_t count, loff_t *ppos)
0189 {
0190 struct b43legacy_wldev *dev;
0191 struct b43legacy_debugfs_fops *dfops;
0192 struct b43legacy_dfs_file *dfile;
0193 ssize_t ret;
0194 char *buf;
0195 const size_t bufsize = 1024 * 16;
0196 const size_t buforder = get_order(bufsize);
0197 int err = 0;
0198
0199 if (!count)
0200 return 0;
0201 dev = file->private_data;
0202 if (!dev)
0203 return -ENODEV;
0204
0205 mutex_lock(&dev->wl->mutex);
0206 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
0207 err = -ENODEV;
0208 goto out_unlock;
0209 }
0210
0211 dfops = container_of(debugfs_real_fops(file),
0212 struct b43legacy_debugfs_fops, fops);
0213 if (!dfops->read) {
0214 err = -ENOSYS;
0215 goto out_unlock;
0216 }
0217 dfile = fops_to_dfs_file(dev, dfops);
0218
0219 if (!dfile->buffer) {
0220 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
0221 if (!buf) {
0222 err = -ENOMEM;
0223 goto out_unlock;
0224 }
0225 memset(buf, 0, bufsize);
0226 if (dfops->take_irqlock) {
0227 spin_lock_irq(&dev->wl->irq_lock);
0228 ret = dfops->read(dev, buf, bufsize);
0229 spin_unlock_irq(&dev->wl->irq_lock);
0230 } else
0231 ret = dfops->read(dev, buf, bufsize);
0232 if (ret <= 0) {
0233 free_pages((unsigned long)buf, buforder);
0234 err = ret;
0235 goto out_unlock;
0236 }
0237 dfile->data_len = ret;
0238 dfile->buffer = buf;
0239 }
0240
0241 ret = simple_read_from_buffer(userbuf, count, ppos,
0242 dfile->buffer,
0243 dfile->data_len);
0244 if (*ppos >= dfile->data_len) {
0245 free_pages((unsigned long)dfile->buffer, buforder);
0246 dfile->buffer = NULL;
0247 dfile->data_len = 0;
0248 }
0249 out_unlock:
0250 mutex_unlock(&dev->wl->mutex);
0251
0252 return err ? err : ret;
0253 }
0254
0255 static ssize_t b43legacy_debugfs_write(struct file *file,
0256 const char __user *userbuf,
0257 size_t count, loff_t *ppos)
0258 {
0259 struct b43legacy_wldev *dev;
0260 struct b43legacy_debugfs_fops *dfops;
0261 char *buf;
0262 int err = 0;
0263
0264 if (!count)
0265 return 0;
0266 if (count > PAGE_SIZE)
0267 return -E2BIG;
0268 dev = file->private_data;
0269 if (!dev)
0270 return -ENODEV;
0271
0272 mutex_lock(&dev->wl->mutex);
0273 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
0274 err = -ENODEV;
0275 goto out_unlock;
0276 }
0277
0278 dfops = container_of(debugfs_real_fops(file),
0279 struct b43legacy_debugfs_fops, fops);
0280 if (!dfops->write) {
0281 err = -ENOSYS;
0282 goto out_unlock;
0283 }
0284
0285 buf = (char *)get_zeroed_page(GFP_KERNEL);
0286 if (!buf) {
0287 err = -ENOMEM;
0288 goto out_unlock;
0289 }
0290 if (copy_from_user(buf, userbuf, count)) {
0291 err = -EFAULT;
0292 goto out_freepage;
0293 }
0294 if (dfops->take_irqlock) {
0295 spin_lock_irq(&dev->wl->irq_lock);
0296 err = dfops->write(dev, buf, count);
0297 spin_unlock_irq(&dev->wl->irq_lock);
0298 } else
0299 err = dfops->write(dev, buf, count);
0300 if (err)
0301 goto out_freepage;
0302
0303 out_freepage:
0304 free_page((unsigned long)buf);
0305 out_unlock:
0306 mutex_unlock(&dev->wl->mutex);
0307
0308 return err ? err : count;
0309 }
0310
0311
0312 #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
0313 static struct b43legacy_debugfs_fops fops_##name = { \
0314 .read = _read, \
0315 .write = _write, \
0316 .fops = { \
0317 .open = simple_open, \
0318 .read = b43legacy_debugfs_read, \
0319 .write = b43legacy_debugfs_write, \
0320 .llseek = generic_file_llseek, \
0321 }, \
0322 .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
0323 file_##name), \
0324 .take_irqlock = _take_irqlock, \
0325 }
0326
0327 B43legacy_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
0328 B43legacy_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
0329 B43legacy_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
0330 B43legacy_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
0331 B43legacy_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
0332
0333
0334 int b43legacy_debug(struct b43legacy_wldev *dev, enum b43legacy_dyndbg feature)
0335 {
0336 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
0337 }
0338
0339 static void b43legacy_add_dynamic_debug(struct b43legacy_wldev *dev)
0340 {
0341 struct b43legacy_dfsentry *e = dev->dfsentry;
0342
0343 #define add_dyn_dbg(name, id, initstate) do { \
0344 e->dyn_debug[id] = (initstate); \
0345 debugfs_create_bool(name, 0600, e->subdir, \
0346 &(e->dyn_debug[id])); \
0347 } while (0)
0348
0349 add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER, false);
0350 add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW, false);
0351 add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE, false);
0352 add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST, false);
0353 add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP, false);
0354
0355 #undef add_dyn_dbg
0356 }
0357
0358 void b43legacy_debugfs_add_device(struct b43legacy_wldev *dev)
0359 {
0360 struct b43legacy_dfsentry *e;
0361 struct b43legacy_txstatus_log *log;
0362 char devdir[16];
0363
0364 B43legacy_WARN_ON(!dev);
0365 e = kzalloc(sizeof(*e), GFP_KERNEL);
0366 if (!e) {
0367 b43legacyerr(dev->wl, "debugfs: add device OOM\n");
0368 return;
0369 }
0370 e->dev = dev;
0371 log = &e->txstatlog;
0372 log->log = kcalloc(B43legacy_NR_LOGGED_TXSTATUS,
0373 sizeof(struct b43legacy_txstatus), GFP_KERNEL);
0374 if (!log->log) {
0375 b43legacyerr(dev->wl, "debugfs: add device txstatus OOM\n");
0376 kfree(e);
0377 return;
0378 }
0379 log->end = -1;
0380 spin_lock_init(&log->lock);
0381
0382 dev->dfsentry = e;
0383
0384 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
0385 e->subdir = debugfs_create_dir(devdir, rootdir);
0386
0387 #define ADD_FILE(name, mode) \
0388 do { \
0389 debugfs_create_file(__stringify(name), mode, \
0390 e->subdir, dev, \
0391 &fops_##name.fops); \
0392 } while (0)
0393
0394
0395 ADD_FILE(tsf, 0600);
0396 ADD_FILE(ucode_regs, 0400);
0397 ADD_FILE(shm, 0400);
0398 ADD_FILE(txstat, 0400);
0399 ADD_FILE(restart, 0200);
0400
0401 #undef ADD_FILE
0402
0403 b43legacy_add_dynamic_debug(dev);
0404 }
0405
0406 void b43legacy_debugfs_remove_device(struct b43legacy_wldev *dev)
0407 {
0408 struct b43legacy_dfsentry *e;
0409
0410 if (!dev)
0411 return;
0412 e = dev->dfsentry;
0413 if (!e)
0414 return;
0415
0416 debugfs_remove(e->subdir);
0417 kfree(e->txstatlog.log);
0418 kfree(e);
0419 }
0420
0421 void b43legacy_debugfs_log_txstat(struct b43legacy_wldev *dev,
0422 const struct b43legacy_txstatus *status)
0423 {
0424 struct b43legacy_dfsentry *e = dev->dfsentry;
0425 struct b43legacy_txstatus_log *log;
0426 struct b43legacy_txstatus *cur;
0427 int i;
0428
0429 if (!e)
0430 return;
0431 log = &e->txstatlog;
0432 B43legacy_WARN_ON(!irqs_disabled());
0433 spin_lock(&log->lock);
0434 i = log->end + 1;
0435 if (i == B43legacy_NR_LOGGED_TXSTATUS)
0436 i = 0;
0437 log->end = i;
0438 cur = &(log->log[i]);
0439 memcpy(cur, status, sizeof(*cur));
0440 spin_unlock(&log->lock);
0441 }
0442
0443 void b43legacy_debugfs_init(void)
0444 {
0445 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
0446 }
0447
0448 void b43legacy_debugfs_exit(void)
0449 {
0450 debugfs_remove(rootdir);
0451 }