rt2x00debug.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Module: rt2x00lib
  17. Abstract: rt2x00 debugfs specific routines.
  18. */
  19. #include <linux/debugfs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/poll.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/uaccess.h>
  26. #include "rt2x00.h"
  27. #include "rt2x00lib.h"
  28. #include "rt2x00dump.h"
  29. #define MAX_LINE_LENGTH 64
  30. struct rt2x00debug_crypto {
  31. unsigned long success;
  32. unsigned long icv_error;
  33. unsigned long mic_error;
  34. unsigned long key_error;
  35. };
  36. struct rt2x00debug_intf {
  37. /*
  38. * Pointer to driver structure where
  39. * this debugfs entry belongs to.
  40. */
  41. struct rt2x00_dev *rt2x00dev;
  42. /*
  43. * Reference to the rt2x00debug structure
  44. * which can be used to communicate with
  45. * the registers.
  46. */
  47. const struct rt2x00debug *debug;
  48. /*
  49. * Debugfs entries for:
  50. * - driver folder
  51. * - driver file
  52. * - chipset file
  53. * - device state flags file
  54. * - device capability flags file
  55. * - register folder
  56. * - csr offset/value files
  57. * - eeprom offset/value files
  58. * - bbp offset/value files
  59. * - rf offset/value files
  60. * - rfcsr offset/value files
  61. * - queue folder
  62. * - frame dump file
  63. * - queue stats file
  64. * - crypto stats file
  65. */
  66. struct dentry *driver_folder;
  67. struct dentry *driver_entry;
  68. struct dentry *chipset_entry;
  69. struct dentry *dev_flags;
  70. struct dentry *cap_flags;
  71. struct dentry *register_folder;
  72. struct dentry *csr_off_entry;
  73. struct dentry *csr_val_entry;
  74. struct dentry *eeprom_off_entry;
  75. struct dentry *eeprom_val_entry;
  76. struct dentry *bbp_off_entry;
  77. struct dentry *bbp_val_entry;
  78. struct dentry *rf_off_entry;
  79. struct dentry *rf_val_entry;
  80. struct dentry *rfcsr_off_entry;
  81. struct dentry *rfcsr_val_entry;
  82. struct dentry *queue_folder;
  83. struct dentry *queue_frame_dump_entry;
  84. struct dentry *queue_stats_entry;
  85. struct dentry *crypto_stats_entry;
  86. /*
  87. * The frame dump file only allows a single reader,
  88. * so we need to store the current state here.
  89. */
  90. unsigned long frame_dump_flags;
  91. #define FRAME_DUMP_FILE_OPEN 1
  92. /*
  93. * We queue each frame before dumping it to the user,
  94. * per read command we will pass a single skb structure
  95. * so we should be prepared to queue multiple sk buffers
  96. * before sending it to userspace.
  97. */
  98. struct sk_buff_head frame_dump_skbqueue;
  99. wait_queue_head_t frame_dump_waitqueue;
  100. /*
  101. * HW crypto statistics.
  102. * All statistics are stored separately per cipher type.
  103. */
  104. struct rt2x00debug_crypto crypto_stats[CIPHER_MAX];
  105. /*
  106. * Driver and chipset files will use a data buffer
  107. * that has been created in advance. This will simplify
  108. * the code since we can use the debugfs functions.
  109. */
  110. struct debugfs_blob_wrapper driver_blob;
  111. struct debugfs_blob_wrapper chipset_blob;
  112. /*
  113. * Requested offset for each register type.
  114. */
  115. unsigned int offset_csr;
  116. unsigned int offset_eeprom;
  117. unsigned int offset_bbp;
  118. unsigned int offset_rf;
  119. unsigned int offset_rfcsr;
  120. };
  121. void rt2x00debug_update_crypto(struct rt2x00_dev *rt2x00dev,
  122. struct rxdone_entry_desc *rxdesc)
  123. {
  124. struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
  125. enum cipher cipher = rxdesc->cipher;
  126. enum rx_crypto status = rxdesc->cipher_status;
  127. if (cipher == CIPHER_TKIP_NO_MIC)
  128. cipher = CIPHER_TKIP;
  129. if (cipher == CIPHER_NONE || cipher >= CIPHER_MAX)
  130. return;
  131. /* Remove CIPHER_NONE index */
  132. cipher--;
  133. intf->crypto_stats[cipher].success += (status == RX_CRYPTO_SUCCESS);
  134. intf->crypto_stats[cipher].icv_error += (status == RX_CRYPTO_FAIL_ICV);
  135. intf->crypto_stats[cipher].mic_error += (status == RX_CRYPTO_FAIL_MIC);
  136. intf->crypto_stats[cipher].key_error += (status == RX_CRYPTO_FAIL_KEY);
  137. }
  138. void rt2x00debug_dump_frame(struct rt2x00_dev *rt2x00dev,
  139. enum rt2x00_dump_type type, struct queue_entry *entry)
  140. {
  141. struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
  142. struct sk_buff *skb = entry->skb;
  143. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  144. struct sk_buff *skbcopy;
  145. struct rt2x00dump_hdr *dump_hdr;
  146. struct timeval timestamp;
  147. u32 data_len;
  148. if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
  149. return;
  150. do_gettimeofday(&timestamp);
  151. if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
  152. rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
  153. return;
  154. }
  155. data_len = skb->len;
  156. if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
  157. data_len -= skbdesc->desc_len;
  158. skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
  159. GFP_ATOMIC);
  160. if (!skbcopy) {
  161. rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
  162. return;
  163. }
  164. dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
  165. dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
  166. dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
  167. dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
  168. dump_hdr->data_length = cpu_to_le32(data_len);
  169. dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
  170. dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
  171. dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
  172. dump_hdr->type = cpu_to_le16(type);
  173. dump_hdr->queue_index = entry->queue->qid;
  174. dump_hdr->entry_index = entry->entry_idx;
  175. dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
  176. dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
  177. if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
  178. memcpy(skb_put(skbcopy, skbdesc->desc_len), skbdesc->desc,
  179. skbdesc->desc_len);
  180. memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
  181. skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
  182. wake_up_interruptible(&intf->frame_dump_waitqueue);
  183. /*
  184. * Verify that the file has not been closed while we were working.
  185. */
  186. if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
  187. skb_queue_purge(&intf->frame_dump_skbqueue);
  188. }
  189. EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
  190. static int rt2x00debug_file_open(struct inode *inode, struct file *file)
  191. {
  192. struct rt2x00debug_intf *intf = inode->i_private;
  193. file->private_data = inode->i_private;
  194. if (!try_module_get(intf->debug->owner))
  195. return -EBUSY;
  196. return 0;
  197. }
  198. static int rt2x00debug_file_release(struct inode *inode, struct file *file)
  199. {
  200. struct rt2x00debug_intf *intf = file->private_data;
  201. module_put(intf->debug->owner);
  202. return 0;
  203. }
  204. static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
  205. {
  206. struct rt2x00debug_intf *intf = inode->i_private;
  207. int retval;
  208. retval = rt2x00debug_file_open(inode, file);
  209. if (retval)
  210. return retval;
  211. if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
  212. rt2x00debug_file_release(inode, file);
  213. return -EBUSY;
  214. }
  215. return 0;
  216. }
  217. static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
  218. {
  219. struct rt2x00debug_intf *intf = inode->i_private;
  220. skb_queue_purge(&intf->frame_dump_skbqueue);
  221. clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
  222. return rt2x00debug_file_release(inode, file);
  223. }
  224. static ssize_t rt2x00debug_read_queue_dump(struct file *file,
  225. char __user *buf,
  226. size_t length,
  227. loff_t *offset)
  228. {
  229. struct rt2x00debug_intf *intf = file->private_data;
  230. struct sk_buff *skb;
  231. size_t status;
  232. int retval;
  233. if (file->f_flags & O_NONBLOCK)
  234. return -EAGAIN;
  235. retval =
  236. wait_event_interruptible(intf->frame_dump_waitqueue,
  237. (skb =
  238. skb_dequeue(&intf->frame_dump_skbqueue)));
  239. if (retval)
  240. return retval;
  241. status = min_t(size_t, skb->len, length);
  242. if (copy_to_user(buf, skb->data, status)) {
  243. status = -EFAULT;
  244. goto exit;
  245. }
  246. *offset += status;
  247. exit:
  248. kfree_skb(skb);
  249. return status;
  250. }
  251. static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
  252. poll_table *wait)
  253. {
  254. struct rt2x00debug_intf *intf = file->private_data;
  255. poll_wait(file, &intf->frame_dump_waitqueue, wait);
  256. if (!skb_queue_empty(&intf->frame_dump_skbqueue))
  257. return POLLOUT | POLLWRNORM;
  258. return 0;
  259. }
  260. static const struct file_operations rt2x00debug_fop_queue_dump = {
  261. .owner = THIS_MODULE,
  262. .read = rt2x00debug_read_queue_dump,
  263. .poll = rt2x00debug_poll_queue_dump,
  264. .open = rt2x00debug_open_queue_dump,
  265. .release = rt2x00debug_release_queue_dump,
  266. .llseek = default_llseek,
  267. };
  268. static ssize_t rt2x00debug_read_queue_stats(struct file *file,
  269. char __user *buf,
  270. size_t length,
  271. loff_t *offset)
  272. {
  273. struct rt2x00debug_intf *intf = file->private_data;
  274. struct data_queue *queue;
  275. unsigned long irqflags;
  276. unsigned int lines = 1 + intf->rt2x00dev->data_queues;
  277. size_t size;
  278. char *data;
  279. char *temp;
  280. if (*offset)
  281. return 0;
  282. data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
  283. if (!data)
  284. return -ENOMEM;
  285. temp = data +
  286. sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
  287. queue_for_each(intf->rt2x00dev, queue) {
  288. spin_lock_irqsave(&queue->index_lock, irqflags);
  289. temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
  290. queue->qid, (unsigned int)queue->flags,
  291. queue->count, queue->limit, queue->length,
  292. queue->index[Q_INDEX],
  293. queue->index[Q_INDEX_DMA_DONE],
  294. queue->index[Q_INDEX_DONE]);
  295. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  296. }
  297. size = strlen(data);
  298. size = min(size, length);
  299. if (copy_to_user(buf, data, size)) {
  300. kfree(data);
  301. return -EFAULT;
  302. }
  303. kfree(data);
  304. *offset += size;
  305. return size;
  306. }
  307. static const struct file_operations rt2x00debug_fop_queue_stats = {
  308. .owner = THIS_MODULE,
  309. .read = rt2x00debug_read_queue_stats,
  310. .open = rt2x00debug_file_open,
  311. .release = rt2x00debug_file_release,
  312. .llseek = default_llseek,
  313. };
  314. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  315. static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
  316. char __user *buf,
  317. size_t length,
  318. loff_t *offset)
  319. {
  320. struct rt2x00debug_intf *intf = file->private_data;
  321. static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
  322. char *data;
  323. char *temp;
  324. size_t size;
  325. unsigned int i;
  326. if (*offset)
  327. return 0;
  328. data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
  329. if (!data)
  330. return -ENOMEM;
  331. temp = data;
  332. temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
  333. for (i = 0; i < CIPHER_MAX; i++) {
  334. temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
  335. intf->crypto_stats[i].success,
  336. intf->crypto_stats[i].icv_error,
  337. intf->crypto_stats[i].mic_error,
  338. intf->crypto_stats[i].key_error);
  339. }
  340. size = strlen(data);
  341. size = min(size, length);
  342. if (copy_to_user(buf, data, size)) {
  343. kfree(data);
  344. return -EFAULT;
  345. }
  346. kfree(data);
  347. *offset += size;
  348. return size;
  349. }
  350. static const struct file_operations rt2x00debug_fop_crypto_stats = {
  351. .owner = THIS_MODULE,
  352. .read = rt2x00debug_read_crypto_stats,
  353. .open = rt2x00debug_file_open,
  354. .release = rt2x00debug_file_release,
  355. .llseek = default_llseek,
  356. };
  357. #endif
  358. #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
  359. static ssize_t rt2x00debug_read_##__name(struct file *file, \
  360. char __user *buf, \
  361. size_t length, \
  362. loff_t *offset) \
  363. { \
  364. struct rt2x00debug_intf *intf = file->private_data; \
  365. const struct rt2x00debug *debug = intf->debug; \
  366. char line[16]; \
  367. size_t size; \
  368. unsigned int index = intf->offset_##__name; \
  369. __type value; \
  370. \
  371. if (*offset) \
  372. return 0; \
  373. \
  374. if (index >= debug->__name.word_count) \
  375. return -EINVAL; \
  376. \
  377. index += (debug->__name.word_base / \
  378. debug->__name.word_size); \
  379. \
  380. if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
  381. index *= debug->__name.word_size; \
  382. \
  383. debug->__name.read(intf->rt2x00dev, index, &value); \
  384. \
  385. size = sprintf(line, __format, value); \
  386. \
  387. if (copy_to_user(buf, line, size)) \
  388. return -EFAULT; \
  389. \
  390. *offset += size; \
  391. return size; \
  392. }
  393. #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
  394. static ssize_t rt2x00debug_write_##__name(struct file *file, \
  395. const char __user *buf,\
  396. size_t length, \
  397. loff_t *offset) \
  398. { \
  399. struct rt2x00debug_intf *intf = file->private_data; \
  400. const struct rt2x00debug *debug = intf->debug; \
  401. char line[17]; \
  402. size_t size; \
  403. unsigned int index = intf->offset_##__name; \
  404. __type value; \
  405. \
  406. if (*offset) \
  407. return 0; \
  408. \
  409. if (index >= debug->__name.word_count) \
  410. return -EINVAL; \
  411. \
  412. if (length > sizeof(line)) \
  413. return -EINVAL; \
  414. \
  415. if (copy_from_user(line, buf, length)) \
  416. return -EFAULT; \
  417. line[16] = 0; \
  418. \
  419. size = strlen(line); \
  420. value = simple_strtoul(line, NULL, 0); \
  421. \
  422. index += (debug->__name.word_base / \
  423. debug->__name.word_size); \
  424. \
  425. if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
  426. index *= debug->__name.word_size; \
  427. \
  428. debug->__name.write(intf->rt2x00dev, index, value); \
  429. \
  430. *offset += size; \
  431. return size; \
  432. }
  433. #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
  434. RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
  435. RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
  436. \
  437. static const struct file_operations rt2x00debug_fop_##__name = {\
  438. .owner = THIS_MODULE, \
  439. .read = rt2x00debug_read_##__name, \
  440. .write = rt2x00debug_write_##__name, \
  441. .open = rt2x00debug_file_open, \
  442. .release = rt2x00debug_file_release, \
  443. .llseek = generic_file_llseek, \
  444. };
  445. RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
  446. RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
  447. RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
  448. RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
  449. RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
  450. static ssize_t rt2x00debug_read_dev_flags(struct file *file,
  451. char __user *buf,
  452. size_t length,
  453. loff_t *offset)
  454. {
  455. struct rt2x00debug_intf *intf = file->private_data;
  456. char line[16];
  457. size_t size;
  458. if (*offset)
  459. return 0;
  460. size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
  461. if (copy_to_user(buf, line, size))
  462. return -EFAULT;
  463. *offset += size;
  464. return size;
  465. }
  466. static const struct file_operations rt2x00debug_fop_dev_flags = {
  467. .owner = THIS_MODULE,
  468. .read = rt2x00debug_read_dev_flags,
  469. .open = rt2x00debug_file_open,
  470. .release = rt2x00debug_file_release,
  471. .llseek = default_llseek,
  472. };
  473. static ssize_t rt2x00debug_read_cap_flags(struct file *file,
  474. char __user *buf,
  475. size_t length,
  476. loff_t *offset)
  477. {
  478. struct rt2x00debug_intf *intf = file->private_data;
  479. char line[16];
  480. size_t size;
  481. if (*offset)
  482. return 0;
  483. size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
  484. if (copy_to_user(buf, line, size))
  485. return -EFAULT;
  486. *offset += size;
  487. return size;
  488. }
  489. static const struct file_operations rt2x00debug_fop_cap_flags = {
  490. .owner = THIS_MODULE,
  491. .read = rt2x00debug_read_cap_flags,
  492. .open = rt2x00debug_file_open,
  493. .release = rt2x00debug_file_release,
  494. .llseek = default_llseek,
  495. };
  496. static struct dentry *rt2x00debug_create_file_driver(const char *name,
  497. struct rt2x00debug_intf
  498. *intf,
  499. struct debugfs_blob_wrapper
  500. *blob)
  501. {
  502. char *data;
  503. data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
  504. if (!data)
  505. return NULL;
  506. blob->data = data;
  507. data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
  508. data += sprintf(data, "version:\t%s\n", DRV_VERSION);
  509. blob->size = strlen(blob->data);
  510. return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
  511. }
  512. static struct dentry *rt2x00debug_create_file_chipset(const char *name,
  513. struct rt2x00debug_intf
  514. *intf,
  515. struct
  516. debugfs_blob_wrapper
  517. *blob)
  518. {
  519. const struct rt2x00debug *debug = intf->debug;
  520. char *data;
  521. data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
  522. if (!data)
  523. return NULL;
  524. blob->data = data;
  525. data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
  526. data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
  527. data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
  528. data += sprintf(data, "\n");
  529. data += sprintf(data, "register\tbase\twords\twordsize\n");
  530. #define RT2X00DEBUGFS_SPRINTF_REGISTER(__name) \
  531. { \
  532. if (debug->__name.read) \
  533. data += sprintf(data, __stringify(__name) \
  534. "\t%d\t%d\t%d\n", \
  535. debug->__name.word_base, \
  536. debug->__name.word_count, \
  537. debug->__name.word_size); \
  538. }
  539. RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
  540. RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
  541. RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
  542. RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
  543. RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
  544. #undef RT2X00DEBUGFS_SPRINTF_REGISTER
  545. blob->size = strlen(blob->data);
  546. return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
  547. }
  548. void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
  549. {
  550. const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
  551. struct rt2x00debug_intf *intf;
  552. intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
  553. if (!intf) {
  554. rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
  555. return;
  556. }
  557. intf->debug = debug;
  558. intf->rt2x00dev = rt2x00dev;
  559. rt2x00dev->debugfs_intf = intf;
  560. intf->driver_folder =
  561. debugfs_create_dir(intf->rt2x00dev->ops->name,
  562. rt2x00dev->hw->wiphy->debugfsdir);
  563. if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
  564. goto exit;
  565. intf->driver_entry =
  566. rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
  567. if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
  568. goto exit;
  569. intf->chipset_entry =
  570. rt2x00debug_create_file_chipset("chipset",
  571. intf, &intf->chipset_blob);
  572. if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
  573. goto exit;
  574. intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
  575. intf->driver_folder, intf,
  576. &rt2x00debug_fop_dev_flags);
  577. if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
  578. goto exit;
  579. intf->cap_flags = debugfs_create_file("cap_flags", S_IRUSR,
  580. intf->driver_folder, intf,
  581. &rt2x00debug_fop_cap_flags);
  582. if (IS_ERR(intf->cap_flags) || !intf->cap_flags)
  583. goto exit;
  584. intf->register_folder =
  585. debugfs_create_dir("register", intf->driver_folder);
  586. if (IS_ERR(intf->register_folder) || !intf->register_folder)
  587. goto exit;
  588. #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
  589. ({ \
  590. if (debug->__name.read) { \
  591. (__intf)->__name##_off_entry = \
  592. debugfs_create_u32(__stringify(__name) "_offset", \
  593. S_IRUSR | S_IWUSR, \
  594. (__intf)->register_folder, \
  595. &(__intf)->offset_##__name); \
  596. if (IS_ERR((__intf)->__name##_off_entry) \
  597. || !(__intf)->__name##_off_entry) \
  598. goto exit; \
  599. \
  600. (__intf)->__name##_val_entry = \
  601. debugfs_create_file(__stringify(__name) "_value", \
  602. S_IRUSR | S_IWUSR, \
  603. (__intf)->register_folder, \
  604. (__intf), &rt2x00debug_fop_##__name); \
  605. if (IS_ERR((__intf)->__name##_val_entry) \
  606. || !(__intf)->__name##_val_entry) \
  607. goto exit; \
  608. } \
  609. })
  610. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
  611. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
  612. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
  613. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
  614. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
  615. #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
  616. intf->queue_folder =
  617. debugfs_create_dir("queue", intf->driver_folder);
  618. if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
  619. goto exit;
  620. intf->queue_frame_dump_entry =
  621. debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
  622. intf, &rt2x00debug_fop_queue_dump);
  623. if (IS_ERR(intf->queue_frame_dump_entry)
  624. || !intf->queue_frame_dump_entry)
  625. goto exit;
  626. skb_queue_head_init(&intf->frame_dump_skbqueue);
  627. init_waitqueue_head(&intf->frame_dump_waitqueue);
  628. intf->queue_stats_entry =
  629. debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
  630. intf, &rt2x00debug_fop_queue_stats);
  631. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  632. if (rt2x00_has_cap_hw_crypto(rt2x00dev))
  633. intf->crypto_stats_entry =
  634. debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
  635. intf, &rt2x00debug_fop_crypto_stats);
  636. #endif
  637. return;
  638. exit:
  639. rt2x00debug_deregister(rt2x00dev);
  640. rt2x00_err(rt2x00dev, "Failed to register debug handler\n");
  641. }
  642. void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
  643. {
  644. struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
  645. if (unlikely(!intf))
  646. return;
  647. skb_queue_purge(&intf->frame_dump_skbqueue);
  648. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  649. debugfs_remove(intf->crypto_stats_entry);
  650. #endif
  651. debugfs_remove(intf->queue_stats_entry);
  652. debugfs_remove(intf->queue_frame_dump_entry);
  653. debugfs_remove(intf->queue_folder);
  654. debugfs_remove(intf->rfcsr_val_entry);
  655. debugfs_remove(intf->rfcsr_off_entry);
  656. debugfs_remove(intf->rf_val_entry);
  657. debugfs_remove(intf->rf_off_entry);
  658. debugfs_remove(intf->bbp_val_entry);
  659. debugfs_remove(intf->bbp_off_entry);
  660. debugfs_remove(intf->eeprom_val_entry);
  661. debugfs_remove(intf->eeprom_off_entry);
  662. debugfs_remove(intf->csr_val_entry);
  663. debugfs_remove(intf->csr_off_entry);
  664. debugfs_remove(intf->register_folder);
  665. debugfs_remove(intf->dev_flags);
  666. debugfs_remove(intf->cap_flags);
  667. debugfs_remove(intf->chipset_entry);
  668. debugfs_remove(intf->driver_entry);
  669. debugfs_remove(intf->driver_folder);
  670. kfree(intf->chipset_blob.data);
  671. kfree(intf->driver_blob.data);
  672. kfree(intf);
  673. rt2x00dev->debugfs_intf = NULL;
  674. }