debugfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. Broadcom B43 wireless driver
  3. debugfs driver debugging code
  4. Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/pci.h>
  23. #include <linux/mutex.h>
  24. #include "b43.h"
  25. #include "main.h"
  26. #include "debugfs.h"
  27. #include "dma.h"
  28. #include "xmit.h"
  29. /* The root directory. */
  30. static struct dentry *rootdir;
  31. struct b43_debugfs_fops {
  32. ssize_t (*read)(struct b43_wldev *dev, char *buf, size_t bufsize);
  33. int (*write)(struct b43_wldev *dev, const char *buf, size_t count);
  34. struct file_operations fops;
  35. /* Offset of struct b43_dfs_file in struct b43_dfsentry */
  36. size_t file_struct_offset;
  37. /* Take wl->irq_lock before calling read/write? */
  38. bool take_irqlock;
  39. };
  40. static inline
  41. struct b43_dfs_file * fops_to_dfs_file(struct b43_wldev *dev,
  42. const struct b43_debugfs_fops *dfops)
  43. {
  44. void *p;
  45. p = dev->dfsentry;
  46. p += dfops->file_struct_offset;
  47. return p;
  48. }
  49. #define fappend(fmt, x...) \
  50. do { \
  51. if (bufsize - count) \
  52. count += snprintf(buf + count, \
  53. bufsize - count, \
  54. fmt , ##x); \
  55. else \
  56. printk(KERN_ERR "b43: fappend overflow\n"); \
  57. } while (0)
  58. /* The biggest MMIO address that we allow access to from the debugfs files. */
  59. #define B43_MAX_MMIO_ACCESS (0xF00 - 1)
  60. static ssize_t mmio16read__read_file(struct b43_wldev *dev,
  61. char *buf, size_t bufsize)
  62. {
  63. ssize_t count = 0;
  64. unsigned int addr;
  65. u16 val;
  66. addr = dev->dfsentry->mmio16read_next;
  67. if (addr > B43_MAX_MMIO_ACCESS)
  68. return -EDESTADDRREQ;
  69. val = b43_read16(dev, addr);
  70. fappend("0x%04X\n", val);
  71. return count;
  72. }
  73. static int mmio16read__write_file(struct b43_wldev *dev,
  74. const char *buf, size_t count)
  75. {
  76. unsigned int addr;
  77. int res;
  78. res = sscanf(buf, "0x%X", &addr);
  79. if (res != 1)
  80. return -EINVAL;
  81. if (addr > B43_MAX_MMIO_ACCESS)
  82. return -EADDRNOTAVAIL;
  83. dev->dfsentry->mmio16read_next = addr;
  84. return 0;
  85. }
  86. static int mmio16write__write_file(struct b43_wldev *dev,
  87. const char *buf, size_t count)
  88. {
  89. unsigned int addr, val;
  90. int res;
  91. res = sscanf(buf, "0x%X = 0x%X", &addr, &val);
  92. if (res != 2)
  93. return -EINVAL;
  94. if (addr > B43_MAX_MMIO_ACCESS)
  95. return -EADDRNOTAVAIL;
  96. if (val > 0xFFFF)
  97. return -E2BIG;
  98. b43_write16(dev, addr, val);
  99. return 0;
  100. }
  101. static ssize_t mmio32read__read_file(struct b43_wldev *dev,
  102. char *buf, size_t bufsize)
  103. {
  104. ssize_t count = 0;
  105. unsigned int addr;
  106. u32 val;
  107. addr = dev->dfsentry->mmio32read_next;
  108. if (addr > B43_MAX_MMIO_ACCESS)
  109. return -EDESTADDRREQ;
  110. val = b43_read32(dev, addr);
  111. fappend("0x%08X\n", val);
  112. return count;
  113. }
  114. static int mmio32read__write_file(struct b43_wldev *dev,
  115. const char *buf, size_t count)
  116. {
  117. unsigned int addr;
  118. int res;
  119. res = sscanf(buf, "0x%X", &addr);
  120. if (res != 1)
  121. return -EINVAL;
  122. if (addr > B43_MAX_MMIO_ACCESS)
  123. return -EADDRNOTAVAIL;
  124. dev->dfsentry->mmio32read_next = addr;
  125. return 0;
  126. }
  127. static int mmio32write__write_file(struct b43_wldev *dev,
  128. const char *buf, size_t count)
  129. {
  130. unsigned int addr, val;
  131. int res;
  132. res = sscanf(buf, "0x%X = 0x%X", &addr, &val);
  133. if (res != 2)
  134. return -EINVAL;
  135. if (addr > B43_MAX_MMIO_ACCESS)
  136. return -EADDRNOTAVAIL;
  137. if (val > 0xFFFFFFFF)
  138. return -E2BIG;
  139. b43_write32(dev, addr, val);
  140. return 0;
  141. }
  142. /* wl->irq_lock is locked */
  143. static ssize_t tsf_read_file(struct b43_wldev *dev,
  144. char *buf, size_t bufsize)
  145. {
  146. ssize_t count = 0;
  147. u64 tsf;
  148. b43_tsf_read(dev, &tsf);
  149. fappend("0x%08x%08x\n",
  150. (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
  151. (unsigned int)(tsf & 0xFFFFFFFFULL));
  152. return count;
  153. }
  154. /* wl->irq_lock is locked */
  155. static int tsf_write_file(struct b43_wldev *dev,
  156. const char *buf, size_t count)
  157. {
  158. u64 tsf;
  159. if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
  160. return -EINVAL;
  161. b43_tsf_write(dev, tsf);
  162. return 0;
  163. }
  164. /* wl->irq_lock is locked */
  165. static ssize_t ucode_regs_read_file(struct b43_wldev *dev,
  166. char *buf, size_t bufsize)
  167. {
  168. ssize_t count = 0;
  169. int i;
  170. for (i = 0; i < 64; i++) {
  171. fappend("r%d = 0x%04x\n", i,
  172. b43_shm_read16(dev, B43_SHM_SCRATCH, i));
  173. }
  174. return count;
  175. }
  176. /* wl->irq_lock is locked */
  177. static ssize_t shm_read_file(struct b43_wldev *dev,
  178. char *buf, size_t bufsize)
  179. {
  180. ssize_t count = 0;
  181. int i;
  182. u16 tmp;
  183. __le16 *le16buf = (__le16 *)buf;
  184. for (i = 0; i < 0x1000; i++) {
  185. if (bufsize < sizeof(tmp))
  186. break;
  187. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 2 * i);
  188. le16buf[i] = cpu_to_le16(tmp);
  189. count += sizeof(tmp);
  190. bufsize -= sizeof(tmp);
  191. }
  192. return count;
  193. }
  194. static ssize_t txstat_read_file(struct b43_wldev *dev,
  195. char *buf, size_t bufsize)
  196. {
  197. struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
  198. ssize_t count = 0;
  199. unsigned long flags;
  200. int i, idx;
  201. struct b43_txstatus *stat;
  202. spin_lock_irqsave(&log->lock, flags);
  203. if (log->end < 0) {
  204. fappend("Nothing transmitted, yet\n");
  205. goto out_unlock;
  206. }
  207. fappend("b43 TX status reports:\n\n"
  208. "index | cookie | seq | phy_stat | frame_count | "
  209. "rts_count | supp_reason | pm_indicated | "
  210. "intermediate | for_ampdu | acked\n" "---\n");
  211. i = log->end + 1;
  212. idx = 0;
  213. while (1) {
  214. if (i == B43_NR_LOGGED_TXSTATUS)
  215. i = 0;
  216. stat = &(log->log[i]);
  217. if (stat->cookie) {
  218. fappend("%03d | "
  219. "0x%04X | 0x%04X | 0x%02X | "
  220. "0x%X | 0x%X | "
  221. "%u | %u | "
  222. "%u | %u | %u\n",
  223. idx,
  224. stat->cookie, stat->seq, stat->phy_stat,
  225. stat->frame_count, stat->rts_count,
  226. stat->supp_reason, stat->pm_indicated,
  227. stat->intermediate, stat->for_ampdu,
  228. stat->acked);
  229. idx++;
  230. }
  231. if (i == log->end)
  232. break;
  233. i++;
  234. }
  235. out_unlock:
  236. spin_unlock_irqrestore(&log->lock, flags);
  237. return count;
  238. }
  239. static ssize_t txpower_g_read_file(struct b43_wldev *dev,
  240. char *buf, size_t bufsize)
  241. {
  242. ssize_t count = 0;
  243. if (dev->phy.type != B43_PHYTYPE_G) {
  244. fappend("Device is not a G-PHY\n");
  245. goto out;
  246. }
  247. fappend("Control: %s\n", dev->phy.manual_txpower_control ?
  248. "MANUAL" : "AUTOMATIC");
  249. fappend("Baseband attenuation: %u\n", dev->phy.bbatt.att);
  250. fappend("Radio attenuation: %u\n", dev->phy.rfatt.att);
  251. fappend("TX Mixer Gain: %s\n",
  252. (dev->phy.tx_control & B43_TXCTL_TXMIX) ? "ON" : "OFF");
  253. fappend("PA Gain 2dB: %s\n",
  254. (dev->phy.tx_control & B43_TXCTL_PA2DB) ? "ON" : "OFF");
  255. fappend("PA Gain 3dB: %s\n",
  256. (dev->phy.tx_control & B43_TXCTL_PA3DB) ? "ON" : "OFF");
  257. fappend("\n\n");
  258. fappend("You can write to this file:\n");
  259. fappend("Writing \"auto\" enables automatic txpower control.\n");
  260. fappend
  261. ("Writing the attenuation values as \"bbatt rfatt txmix pa2db pa3db\" "
  262. "enables manual txpower control.\n");
  263. fappend("Example: 5 4 0 0 1\n");
  264. fappend("Enables manual control with Baseband attenuation 5, "
  265. "Radio attenuation 4, No TX Mixer Gain, "
  266. "No PA Gain 2dB, With PA Gain 3dB.\n");
  267. out:
  268. return count;
  269. }
  270. static int txpower_g_write_file(struct b43_wldev *dev,
  271. const char *buf, size_t count)
  272. {
  273. if (dev->phy.type != B43_PHYTYPE_G)
  274. return -ENODEV;
  275. if ((count >= 4) && (memcmp(buf, "auto", 4) == 0)) {
  276. /* Automatic control */
  277. dev->phy.manual_txpower_control = 0;
  278. b43_phy_xmitpower(dev);
  279. } else {
  280. int bbatt = 0, rfatt = 0, txmix = 0, pa2db = 0, pa3db = 0;
  281. /* Manual control */
  282. if (sscanf(buf, "%d %d %d %d %d", &bbatt, &rfatt,
  283. &txmix, &pa2db, &pa3db) != 5)
  284. return -EINVAL;
  285. b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
  286. dev->phy.manual_txpower_control = 1;
  287. dev->phy.bbatt.att = bbatt;
  288. dev->phy.rfatt.att = rfatt;
  289. dev->phy.tx_control = 0;
  290. if (txmix)
  291. dev->phy.tx_control |= B43_TXCTL_TXMIX;
  292. if (pa2db)
  293. dev->phy.tx_control |= B43_TXCTL_PA2DB;
  294. if (pa3db)
  295. dev->phy.tx_control |= B43_TXCTL_PA3DB;
  296. b43_phy_lock(dev);
  297. b43_radio_lock(dev);
  298. b43_set_txpower_g(dev, &dev->phy.bbatt,
  299. &dev->phy.rfatt, dev->phy.tx_control);
  300. b43_radio_unlock(dev);
  301. b43_phy_unlock(dev);
  302. }
  303. return 0;
  304. }
  305. /* wl->irq_lock is locked */
  306. static int restart_write_file(struct b43_wldev *dev,
  307. const char *buf, size_t count)
  308. {
  309. int err = 0;
  310. if (count > 0 && buf[0] == '1') {
  311. b43_controller_restart(dev, "manually restarted");
  312. } else
  313. err = -EINVAL;
  314. return err;
  315. }
  316. static unsigned long calc_expire_secs(unsigned long now,
  317. unsigned long time,
  318. unsigned long expire)
  319. {
  320. expire = time + expire;
  321. if (time_after(now, expire))
  322. return 0; /* expired */
  323. if (expire < now) {
  324. /* jiffies wrapped */
  325. expire -= MAX_JIFFY_OFFSET;
  326. now -= MAX_JIFFY_OFFSET;
  327. }
  328. B43_WARN_ON(expire < now);
  329. return (expire - now) / HZ;
  330. }
  331. static ssize_t loctls_read_file(struct b43_wldev *dev,
  332. char *buf, size_t bufsize)
  333. {
  334. ssize_t count = 0;
  335. struct b43_txpower_lo_control *lo;
  336. int i, err = 0;
  337. struct b43_lo_calib *cal;
  338. unsigned long now = jiffies;
  339. struct b43_phy *phy = &dev->phy;
  340. if (phy->type != B43_PHYTYPE_G) {
  341. fappend("Device is not a G-PHY\n");
  342. err = -ENODEV;
  343. goto out;
  344. }
  345. lo = phy->lo_control;
  346. fappend("-- Local Oscillator calibration data --\n\n");
  347. fappend("HW-power-control enabled: %d\n",
  348. dev->phy.hardware_power_control);
  349. fappend("TX Bias: 0x%02X, TX Magn: 0x%02X (expire in %lu sec)\n",
  350. lo->tx_bias, lo->tx_magn,
  351. calc_expire_secs(now, lo->txctl_measured_time,
  352. B43_LO_TXCTL_EXPIRE));
  353. fappend("Power Vector: 0x%08X%08X (expires in %lu sec)\n",
  354. (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
  355. (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL),
  356. calc_expire_secs(now, lo->pwr_vec_read_time,
  357. B43_LO_PWRVEC_EXPIRE));
  358. fappend("\nCalibrated settings:\n");
  359. list_for_each_entry(cal, &lo->calib_list, list) {
  360. bool active;
  361. active = (b43_compare_bbatt(&cal->bbatt, &phy->bbatt) &&
  362. b43_compare_rfatt(&cal->rfatt, &phy->rfatt));
  363. fappend("BB(%d), RF(%d,%d) -> I=%d, Q=%d "
  364. "(expires in %lu sec)%s\n",
  365. cal->bbatt.att,
  366. cal->rfatt.att, cal->rfatt.with_padmix,
  367. cal->ctl.i, cal->ctl.q,
  368. calc_expire_secs(now, cal->calib_time,
  369. B43_LO_CALIB_EXPIRE),
  370. active ? " ACTIVE" : "");
  371. }
  372. fappend("\nUsed RF attenuation values: Value(WithPadmix flag)\n");
  373. for (i = 0; i < lo->rfatt_list.len; i++) {
  374. fappend("%u(%d), ",
  375. lo->rfatt_list.list[i].att,
  376. lo->rfatt_list.list[i].with_padmix);
  377. }
  378. fappend("\n");
  379. fappend("\nUsed Baseband attenuation values:\n");
  380. for (i = 0; i < lo->bbatt_list.len; i++) {
  381. fappend("%u, ",
  382. lo->bbatt_list.list[i].att);
  383. }
  384. fappend("\n");
  385. out:
  386. return err ? err : count;
  387. }
  388. #undef fappend
  389. static int b43_debugfs_open(struct inode *inode, struct file *file)
  390. {
  391. file->private_data = inode->i_private;
  392. return 0;
  393. }
  394. static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
  395. size_t count, loff_t *ppos)
  396. {
  397. struct b43_wldev *dev;
  398. struct b43_debugfs_fops *dfops;
  399. struct b43_dfs_file *dfile;
  400. ssize_t uninitialized_var(ret);
  401. char *buf;
  402. const size_t bufsize = 1024 * 16; /* 16 kiB buffer */
  403. const size_t buforder = get_order(bufsize);
  404. int err = 0;
  405. if (!count)
  406. return 0;
  407. dev = file->private_data;
  408. if (!dev)
  409. return -ENODEV;
  410. mutex_lock(&dev->wl->mutex);
  411. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  412. err = -ENODEV;
  413. goto out_unlock;
  414. }
  415. dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
  416. if (!dfops->read) {
  417. err = -ENOSYS;
  418. goto out_unlock;
  419. }
  420. dfile = fops_to_dfs_file(dev, dfops);
  421. if (!dfile->buffer) {
  422. buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
  423. if (!buf) {
  424. err = -ENOMEM;
  425. goto out_unlock;
  426. }
  427. memset(buf, 0, bufsize);
  428. if (dfops->take_irqlock) {
  429. spin_lock_irq(&dev->wl->irq_lock);
  430. ret = dfops->read(dev, buf, bufsize);
  431. spin_unlock_irq(&dev->wl->irq_lock);
  432. } else
  433. ret = dfops->read(dev, buf, bufsize);
  434. if (ret <= 0) {
  435. free_pages((unsigned long)buf, buforder);
  436. err = ret;
  437. goto out_unlock;
  438. }
  439. dfile->data_len = ret;
  440. dfile->buffer = buf;
  441. }
  442. ret = simple_read_from_buffer(userbuf, count, ppos,
  443. dfile->buffer,
  444. dfile->data_len);
  445. if (*ppos >= dfile->data_len) {
  446. free_pages((unsigned long)dfile->buffer, buforder);
  447. dfile->buffer = NULL;
  448. dfile->data_len = 0;
  449. }
  450. out_unlock:
  451. mutex_unlock(&dev->wl->mutex);
  452. return err ? err : ret;
  453. }
  454. static ssize_t b43_debugfs_write(struct file *file,
  455. const char __user *userbuf,
  456. size_t count, loff_t *ppos)
  457. {
  458. struct b43_wldev *dev;
  459. struct b43_debugfs_fops *dfops;
  460. char *buf;
  461. int err = 0;
  462. if (!count)
  463. return 0;
  464. if (count > PAGE_SIZE)
  465. return -E2BIG;
  466. dev = file->private_data;
  467. if (!dev)
  468. return -ENODEV;
  469. mutex_lock(&dev->wl->mutex);
  470. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  471. err = -ENODEV;
  472. goto out_unlock;
  473. }
  474. dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
  475. if (!dfops->write) {
  476. err = -ENOSYS;
  477. goto out_unlock;
  478. }
  479. buf = (char *)get_zeroed_page(GFP_KERNEL);
  480. if (!buf) {
  481. err = -ENOMEM;
  482. goto out_unlock;
  483. }
  484. if (copy_from_user(buf, userbuf, count)) {
  485. err = -EFAULT;
  486. goto out_freepage;
  487. }
  488. if (dfops->take_irqlock) {
  489. spin_lock_irq(&dev->wl->irq_lock);
  490. err = dfops->write(dev, buf, count);
  491. spin_unlock_irq(&dev->wl->irq_lock);
  492. } else
  493. err = dfops->write(dev, buf, count);
  494. if (err)
  495. goto out_freepage;
  496. out_freepage:
  497. free_page((unsigned long)buf);
  498. out_unlock:
  499. mutex_unlock(&dev->wl->mutex);
  500. return err ? err : count;
  501. }
  502. #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
  503. static struct b43_debugfs_fops fops_##name = { \
  504. .read = _read, \
  505. .write = _write, \
  506. .fops = { \
  507. .open = b43_debugfs_open, \
  508. .read = b43_debugfs_read, \
  509. .write = b43_debugfs_write, \
  510. }, \
  511. .file_struct_offset = offsetof(struct b43_dfsentry, \
  512. file_##name), \
  513. .take_irqlock = _take_irqlock, \
  514. }
  515. B43_DEBUGFS_FOPS(mmio16read, mmio16read__read_file, mmio16read__write_file, 1);
  516. B43_DEBUGFS_FOPS(mmio16write, NULL, mmio16write__write_file, 1);
  517. B43_DEBUGFS_FOPS(mmio32read, mmio32read__read_file, mmio32read__write_file, 1);
  518. B43_DEBUGFS_FOPS(mmio32write, NULL, mmio32write__write_file, 1);
  519. B43_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
  520. B43_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
  521. B43_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
  522. B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
  523. B43_DEBUGFS_FOPS(txpower_g, txpower_g_read_file, txpower_g_write_file, 0);
  524. B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
  525. B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
  526. int b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
  527. {
  528. return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
  529. }
  530. static void b43_remove_dynamic_debug(struct b43_wldev *dev)
  531. {
  532. struct b43_dfsentry *e = dev->dfsentry;
  533. int i;
  534. for (i = 0; i < __B43_NR_DYNDBG; i++)
  535. debugfs_remove(e->dyn_debug_dentries[i]);
  536. }
  537. static void b43_add_dynamic_debug(struct b43_wldev *dev)
  538. {
  539. struct b43_dfsentry *e = dev->dfsentry;
  540. struct dentry *d;
  541. #define add_dyn_dbg(name, id, initstate) do { \
  542. e->dyn_debug[id] = (initstate); \
  543. d = debugfs_create_bool(name, 0600, e->subdir, \
  544. &(e->dyn_debug[id])); \
  545. if (!IS_ERR(d)) \
  546. e->dyn_debug_dentries[id] = d; \
  547. } while (0)
  548. add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
  549. add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
  550. add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
  551. add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
  552. add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
  553. add_dyn_dbg("debug_lo", B43_DBG_LO, 0);
  554. #undef add_dyn_dbg
  555. }
  556. void b43_debugfs_add_device(struct b43_wldev *dev)
  557. {
  558. struct b43_dfsentry *e;
  559. struct b43_txstatus_log *log;
  560. char devdir[16];
  561. B43_WARN_ON(!dev);
  562. e = kzalloc(sizeof(*e), GFP_KERNEL);
  563. if (!e) {
  564. b43err(dev->wl, "debugfs: add device OOM\n");
  565. return;
  566. }
  567. e->dev = dev;
  568. log = &e->txstatlog;
  569. log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
  570. sizeof(struct b43_txstatus), GFP_KERNEL);
  571. if (!log->log) {
  572. b43err(dev->wl, "debugfs: add device txstatus OOM\n");
  573. kfree(e);
  574. return;
  575. }
  576. log->end = -1;
  577. spin_lock_init(&log->lock);
  578. dev->dfsentry = e;
  579. snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
  580. e->subdir = debugfs_create_dir(devdir, rootdir);
  581. if (!e->subdir || IS_ERR(e->subdir)) {
  582. if (e->subdir == ERR_PTR(-ENODEV)) {
  583. b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
  584. "enabled in kernel config\n");
  585. } else {
  586. b43err(dev->wl, "debugfs: cannot create %s directory\n",
  587. devdir);
  588. }
  589. dev->dfsentry = NULL;
  590. kfree(log->log);
  591. kfree(e);
  592. return;
  593. }
  594. e->mmio16read_next = 0xFFFF; /* invalid address */
  595. e->mmio32read_next = 0xFFFF; /* invalid address */
  596. #define ADD_FILE(name, mode) \
  597. do { \
  598. struct dentry *d; \
  599. d = debugfs_create_file(__stringify(name), \
  600. mode, e->subdir, dev, \
  601. &fops_##name.fops); \
  602. e->file_##name.dentry = NULL; \
  603. if (!IS_ERR(d)) \
  604. e->file_##name.dentry = d; \
  605. } while (0)
  606. ADD_FILE(mmio16read, 0600);
  607. ADD_FILE(mmio16write, 0200);
  608. ADD_FILE(mmio32read, 0600);
  609. ADD_FILE(mmio32write, 0200);
  610. ADD_FILE(tsf, 0600);
  611. ADD_FILE(ucode_regs, 0400);
  612. ADD_FILE(shm, 0400);
  613. ADD_FILE(txstat, 0400);
  614. ADD_FILE(txpower_g, 0600);
  615. ADD_FILE(restart, 0200);
  616. ADD_FILE(loctls, 0400);
  617. #undef ADD_FILE
  618. b43_add_dynamic_debug(dev);
  619. }
  620. void b43_debugfs_remove_device(struct b43_wldev *dev)
  621. {
  622. struct b43_dfsentry *e;
  623. if (!dev)
  624. return;
  625. e = dev->dfsentry;
  626. if (!e)
  627. return;
  628. b43_remove_dynamic_debug(dev);
  629. debugfs_remove(e->file_mmio16read.dentry);
  630. debugfs_remove(e->file_mmio16write.dentry);
  631. debugfs_remove(e->file_mmio32read.dentry);
  632. debugfs_remove(e->file_mmio32write.dentry);
  633. debugfs_remove(e->file_tsf.dentry);
  634. debugfs_remove(e->file_ucode_regs.dentry);
  635. debugfs_remove(e->file_shm.dentry);
  636. debugfs_remove(e->file_txstat.dentry);
  637. debugfs_remove(e->file_txpower_g.dentry);
  638. debugfs_remove(e->file_restart.dentry);
  639. debugfs_remove(e->file_loctls.dentry);
  640. debugfs_remove(e->subdir);
  641. kfree(e->txstatlog.log);
  642. kfree(e);
  643. }
  644. /* Called with IRQs disabled. */
  645. void b43_debugfs_log_txstat(struct b43_wldev *dev,
  646. const struct b43_txstatus *status)
  647. {
  648. struct b43_dfsentry *e = dev->dfsentry;
  649. struct b43_txstatus_log *log;
  650. struct b43_txstatus *cur;
  651. int i;
  652. if (!e)
  653. return;
  654. log = &e->txstatlog;
  655. spin_lock(&log->lock); /* IRQs are already disabled. */
  656. i = log->end + 1;
  657. if (i == B43_NR_LOGGED_TXSTATUS)
  658. i = 0;
  659. log->end = i;
  660. cur = &(log->log[i]);
  661. memcpy(cur, status, sizeof(*cur));
  662. spin_unlock(&log->lock);
  663. }
  664. void b43_debugfs_init(void)
  665. {
  666. rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  667. if (IS_ERR(rootdir))
  668. rootdir = NULL;
  669. }
  670. void b43_debugfs_exit(void)
  671. {
  672. debugfs_remove(rootdir);
  673. }