rt2x00debug.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 sk_buff *skb)
  140. {
  141. struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
  142. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  143. struct sk_buff *skbcopy;
  144. struct rt2x00dump_hdr *dump_hdr;
  145. struct timeval timestamp;
  146. u32 data_len;
  147. if (likely(!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)))
  148. return;
  149. do_gettimeofday(&timestamp);
  150. if (skb_queue_len(&intf->frame_dump_skbqueue) > 20) {
  151. rt2x00_dbg(rt2x00dev, "txrx dump queue length exceeded\n");
  152. return;
  153. }
  154. data_len = skb->len;
  155. if (skbdesc->flags & SKBDESC_DESC_IN_SKB)
  156. data_len -= skbdesc->desc_len;
  157. skbcopy = alloc_skb(sizeof(*dump_hdr) + skbdesc->desc_len + data_len,
  158. GFP_ATOMIC);
  159. if (!skbcopy) {
  160. rt2x00_dbg(rt2x00dev, "Failed to copy skb for dump\n");
  161. return;
  162. }
  163. dump_hdr = (struct rt2x00dump_hdr *)skb_put(skbcopy, sizeof(*dump_hdr));
  164. dump_hdr->version = cpu_to_le32(DUMP_HEADER_VERSION);
  165. dump_hdr->header_length = cpu_to_le32(sizeof(*dump_hdr));
  166. dump_hdr->desc_length = cpu_to_le32(skbdesc->desc_len);
  167. dump_hdr->data_length = cpu_to_le32(data_len);
  168. dump_hdr->chip_rt = cpu_to_le16(rt2x00dev->chip.rt);
  169. dump_hdr->chip_rf = cpu_to_le16(rt2x00dev->chip.rf);
  170. dump_hdr->chip_rev = cpu_to_le16(rt2x00dev->chip.rev);
  171. dump_hdr->type = cpu_to_le16(type);
  172. dump_hdr->queue_index = skbdesc->entry->queue->qid;
  173. dump_hdr->entry_index = skbdesc->entry->entry_idx;
  174. dump_hdr->timestamp_sec = cpu_to_le32(timestamp.tv_sec);
  175. dump_hdr->timestamp_usec = cpu_to_le32(timestamp.tv_usec);
  176. if (!(skbdesc->flags & SKBDESC_DESC_IN_SKB))
  177. memcpy(skb_put(skbcopy, skbdesc->desc_len), skbdesc->desc,
  178. skbdesc->desc_len);
  179. memcpy(skb_put(skbcopy, skb->len), skb->data, skb->len);
  180. skb_queue_tail(&intf->frame_dump_skbqueue, skbcopy);
  181. wake_up_interruptible(&intf->frame_dump_waitqueue);
  182. /*
  183. * Verify that the file has not been closed while we were working.
  184. */
  185. if (!test_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags))
  186. skb_queue_purge(&intf->frame_dump_skbqueue);
  187. }
  188. EXPORT_SYMBOL_GPL(rt2x00debug_dump_frame);
  189. static int rt2x00debug_file_open(struct inode *inode, struct file *file)
  190. {
  191. struct rt2x00debug_intf *intf = inode->i_private;
  192. file->private_data = inode->i_private;
  193. if (!try_module_get(intf->debug->owner))
  194. return -EBUSY;
  195. return 0;
  196. }
  197. static int rt2x00debug_file_release(struct inode *inode, struct file *file)
  198. {
  199. struct rt2x00debug_intf *intf = file->private_data;
  200. module_put(intf->debug->owner);
  201. return 0;
  202. }
  203. static int rt2x00debug_open_queue_dump(struct inode *inode, struct file *file)
  204. {
  205. struct rt2x00debug_intf *intf = inode->i_private;
  206. int retval;
  207. retval = rt2x00debug_file_open(inode, file);
  208. if (retval)
  209. return retval;
  210. if (test_and_set_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags)) {
  211. rt2x00debug_file_release(inode, file);
  212. return -EBUSY;
  213. }
  214. return 0;
  215. }
  216. static int rt2x00debug_release_queue_dump(struct inode *inode, struct file *file)
  217. {
  218. struct rt2x00debug_intf *intf = inode->i_private;
  219. skb_queue_purge(&intf->frame_dump_skbqueue);
  220. clear_bit(FRAME_DUMP_FILE_OPEN, &intf->frame_dump_flags);
  221. return rt2x00debug_file_release(inode, file);
  222. }
  223. static ssize_t rt2x00debug_read_queue_dump(struct file *file,
  224. char __user *buf,
  225. size_t length,
  226. loff_t *offset)
  227. {
  228. struct rt2x00debug_intf *intf = file->private_data;
  229. struct sk_buff *skb;
  230. size_t status;
  231. int retval;
  232. if (file->f_flags & O_NONBLOCK)
  233. return -EAGAIN;
  234. retval =
  235. wait_event_interruptible(intf->frame_dump_waitqueue,
  236. (skb =
  237. skb_dequeue(&intf->frame_dump_skbqueue)));
  238. if (retval)
  239. return retval;
  240. status = min_t(size_t, skb->len, length);
  241. if (copy_to_user(buf, skb->data, status)) {
  242. status = -EFAULT;
  243. goto exit;
  244. }
  245. *offset += status;
  246. exit:
  247. kfree_skb(skb);
  248. return status;
  249. }
  250. static unsigned int rt2x00debug_poll_queue_dump(struct file *file,
  251. poll_table *wait)
  252. {
  253. struct rt2x00debug_intf *intf = file->private_data;
  254. poll_wait(file, &intf->frame_dump_waitqueue, wait);
  255. if (!skb_queue_empty(&intf->frame_dump_skbqueue))
  256. return POLLOUT | POLLWRNORM;
  257. return 0;
  258. }
  259. static const struct file_operations rt2x00debug_fop_queue_dump = {
  260. .owner = THIS_MODULE,
  261. .read = rt2x00debug_read_queue_dump,
  262. .poll = rt2x00debug_poll_queue_dump,
  263. .open = rt2x00debug_open_queue_dump,
  264. .release = rt2x00debug_release_queue_dump,
  265. .llseek = default_llseek,
  266. };
  267. static ssize_t rt2x00debug_read_queue_stats(struct file *file,
  268. char __user *buf,
  269. size_t length,
  270. loff_t *offset)
  271. {
  272. struct rt2x00debug_intf *intf = file->private_data;
  273. struct data_queue *queue;
  274. unsigned long irqflags;
  275. unsigned int lines = 1 + intf->rt2x00dev->data_queues;
  276. size_t size;
  277. char *data;
  278. char *temp;
  279. if (*offset)
  280. return 0;
  281. data = kcalloc(lines, MAX_LINE_LENGTH, GFP_KERNEL);
  282. if (!data)
  283. return -ENOMEM;
  284. temp = data +
  285. sprintf(data, "qid\tflags\t\tcount\tlimit\tlength\tindex\tdma done\tdone\n");
  286. queue_for_each(intf->rt2x00dev, queue) {
  287. spin_lock_irqsave(&queue->index_lock, irqflags);
  288. temp += sprintf(temp, "%d\t0x%.8x\t%d\t%d\t%d\t%d\t%d\t\t%d\n",
  289. queue->qid, (unsigned int)queue->flags,
  290. queue->count, queue->limit, queue->length,
  291. queue->index[Q_INDEX],
  292. queue->index[Q_INDEX_DMA_DONE],
  293. queue->index[Q_INDEX_DONE]);
  294. spin_unlock_irqrestore(&queue->index_lock, irqflags);
  295. }
  296. size = strlen(data);
  297. size = min(size, length);
  298. if (copy_to_user(buf, data, size)) {
  299. kfree(data);
  300. return -EFAULT;
  301. }
  302. kfree(data);
  303. *offset += size;
  304. return size;
  305. }
  306. static const struct file_operations rt2x00debug_fop_queue_stats = {
  307. .owner = THIS_MODULE,
  308. .read = rt2x00debug_read_queue_stats,
  309. .open = rt2x00debug_file_open,
  310. .release = rt2x00debug_file_release,
  311. .llseek = default_llseek,
  312. };
  313. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  314. static ssize_t rt2x00debug_read_crypto_stats(struct file *file,
  315. char __user *buf,
  316. size_t length,
  317. loff_t *offset)
  318. {
  319. struct rt2x00debug_intf *intf = file->private_data;
  320. static const char * const name[] = { "WEP64", "WEP128", "TKIP", "AES" };
  321. char *data;
  322. char *temp;
  323. size_t size;
  324. unsigned int i;
  325. if (*offset)
  326. return 0;
  327. data = kzalloc((1 + CIPHER_MAX) * MAX_LINE_LENGTH, GFP_KERNEL);
  328. if (!data)
  329. return -ENOMEM;
  330. temp = data;
  331. temp += sprintf(data, "cipher\tsuccess\ticv err\tmic err\tkey err\n");
  332. for (i = 0; i < CIPHER_MAX; i++) {
  333. temp += sprintf(temp, "%s\t%lu\t%lu\t%lu\t%lu\n", name[i],
  334. intf->crypto_stats[i].success,
  335. intf->crypto_stats[i].icv_error,
  336. intf->crypto_stats[i].mic_error,
  337. intf->crypto_stats[i].key_error);
  338. }
  339. size = strlen(data);
  340. size = min(size, length);
  341. if (copy_to_user(buf, data, size)) {
  342. kfree(data);
  343. return -EFAULT;
  344. }
  345. kfree(data);
  346. *offset += size;
  347. return size;
  348. }
  349. static const struct file_operations rt2x00debug_fop_crypto_stats = {
  350. .owner = THIS_MODULE,
  351. .read = rt2x00debug_read_crypto_stats,
  352. .open = rt2x00debug_file_open,
  353. .release = rt2x00debug_file_release,
  354. .llseek = default_llseek,
  355. };
  356. #endif
  357. #define RT2X00DEBUGFS_OPS_READ(__name, __format, __type) \
  358. static ssize_t rt2x00debug_read_##__name(struct file *file, \
  359. char __user *buf, \
  360. size_t length, \
  361. loff_t *offset) \
  362. { \
  363. struct rt2x00debug_intf *intf = file->private_data; \
  364. const struct rt2x00debug *debug = intf->debug; \
  365. char line[16]; \
  366. size_t size; \
  367. unsigned int index = intf->offset_##__name; \
  368. __type value; \
  369. \
  370. if (*offset) \
  371. return 0; \
  372. \
  373. if (index >= debug->__name.word_count) \
  374. return -EINVAL; \
  375. \
  376. index += (debug->__name.word_base / \
  377. debug->__name.word_size); \
  378. \
  379. if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
  380. index *= debug->__name.word_size; \
  381. \
  382. debug->__name.read(intf->rt2x00dev, index, &value); \
  383. \
  384. size = sprintf(line, __format, value); \
  385. \
  386. if (copy_to_user(buf, line, size)) \
  387. return -EFAULT; \
  388. \
  389. *offset += size; \
  390. return size; \
  391. }
  392. #define RT2X00DEBUGFS_OPS_WRITE(__name, __type) \
  393. static ssize_t rt2x00debug_write_##__name(struct file *file, \
  394. const char __user *buf,\
  395. size_t length, \
  396. loff_t *offset) \
  397. { \
  398. struct rt2x00debug_intf *intf = file->private_data; \
  399. const struct rt2x00debug *debug = intf->debug; \
  400. char line[16]; \
  401. size_t size; \
  402. unsigned int index = intf->offset_##__name; \
  403. __type value; \
  404. \
  405. if (*offset) \
  406. return 0; \
  407. \
  408. if (index >= debug->__name.word_count) \
  409. return -EINVAL; \
  410. \
  411. if (length > sizeof(line)) \
  412. return -EINVAL; \
  413. \
  414. if (copy_from_user(line, buf, length)) \
  415. return -EFAULT; \
  416. \
  417. size = strlen(line); \
  418. value = simple_strtoul(line, NULL, 0); \
  419. \
  420. index += (debug->__name.word_base / \
  421. debug->__name.word_size); \
  422. \
  423. if (debug->__name.flags & RT2X00DEBUGFS_OFFSET) \
  424. index *= debug->__name.word_size; \
  425. \
  426. debug->__name.write(intf->rt2x00dev, index, value); \
  427. \
  428. *offset += size; \
  429. return size; \
  430. }
  431. #define RT2X00DEBUGFS_OPS(__name, __format, __type) \
  432. RT2X00DEBUGFS_OPS_READ(__name, __format, __type); \
  433. RT2X00DEBUGFS_OPS_WRITE(__name, __type); \
  434. \
  435. static const struct file_operations rt2x00debug_fop_##__name = {\
  436. .owner = THIS_MODULE, \
  437. .read = rt2x00debug_read_##__name, \
  438. .write = rt2x00debug_write_##__name, \
  439. .open = rt2x00debug_file_open, \
  440. .release = rt2x00debug_file_release, \
  441. .llseek = generic_file_llseek, \
  442. };
  443. RT2X00DEBUGFS_OPS(csr, "0x%.8x\n", u32);
  444. RT2X00DEBUGFS_OPS(eeprom, "0x%.4x\n", u16);
  445. RT2X00DEBUGFS_OPS(bbp, "0x%.2x\n", u8);
  446. RT2X00DEBUGFS_OPS(rf, "0x%.8x\n", u32);
  447. RT2X00DEBUGFS_OPS(rfcsr, "0x%.2x\n", u8);
  448. static ssize_t rt2x00debug_read_dev_flags(struct file *file,
  449. char __user *buf,
  450. size_t length,
  451. loff_t *offset)
  452. {
  453. struct rt2x00debug_intf *intf = file->private_data;
  454. char line[16];
  455. size_t size;
  456. if (*offset)
  457. return 0;
  458. size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->flags);
  459. if (copy_to_user(buf, line, size))
  460. return -EFAULT;
  461. *offset += size;
  462. return size;
  463. }
  464. static const struct file_operations rt2x00debug_fop_dev_flags = {
  465. .owner = THIS_MODULE,
  466. .read = rt2x00debug_read_dev_flags,
  467. .open = rt2x00debug_file_open,
  468. .release = rt2x00debug_file_release,
  469. .llseek = default_llseek,
  470. };
  471. static ssize_t rt2x00debug_read_cap_flags(struct file *file,
  472. char __user *buf,
  473. size_t length,
  474. loff_t *offset)
  475. {
  476. struct rt2x00debug_intf *intf = file->private_data;
  477. char line[16];
  478. size_t size;
  479. if (*offset)
  480. return 0;
  481. size = sprintf(line, "0x%.8x\n", (unsigned int)intf->rt2x00dev->cap_flags);
  482. if (copy_to_user(buf, line, size))
  483. return -EFAULT;
  484. *offset += size;
  485. return size;
  486. }
  487. static const struct file_operations rt2x00debug_fop_cap_flags = {
  488. .owner = THIS_MODULE,
  489. .read = rt2x00debug_read_cap_flags,
  490. .open = rt2x00debug_file_open,
  491. .release = rt2x00debug_file_release,
  492. .llseek = default_llseek,
  493. };
  494. static struct dentry *rt2x00debug_create_file_driver(const char *name,
  495. struct rt2x00debug_intf
  496. *intf,
  497. struct debugfs_blob_wrapper
  498. *blob)
  499. {
  500. char *data;
  501. data = kzalloc(3 * MAX_LINE_LENGTH, GFP_KERNEL);
  502. if (!data)
  503. return NULL;
  504. blob->data = data;
  505. data += sprintf(data, "driver:\t%s\n", intf->rt2x00dev->ops->name);
  506. data += sprintf(data, "version:\t%s\n", DRV_VERSION);
  507. blob->size = strlen(blob->data);
  508. return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
  509. }
  510. static struct dentry *rt2x00debug_create_file_chipset(const char *name,
  511. struct rt2x00debug_intf
  512. *intf,
  513. struct
  514. debugfs_blob_wrapper
  515. *blob)
  516. {
  517. const struct rt2x00debug *debug = intf->debug;
  518. char *data;
  519. data = kzalloc(9 * MAX_LINE_LENGTH, GFP_KERNEL);
  520. if (!data)
  521. return NULL;
  522. blob->data = data;
  523. data += sprintf(data, "rt chip:\t%04x\n", intf->rt2x00dev->chip.rt);
  524. data += sprintf(data, "rf chip:\t%04x\n", intf->rt2x00dev->chip.rf);
  525. data += sprintf(data, "revision:\t%04x\n", intf->rt2x00dev->chip.rev);
  526. data += sprintf(data, "\n");
  527. data += sprintf(data, "register\tbase\twords\twordsize\n");
  528. #define RT2X00DEBUGFS_SPRINTF_REGISTER(__name) \
  529. { \
  530. if(debug->__name.read) \
  531. data += sprintf(data, __stringify(__name) \
  532. "\t%d\t%d\t%d\n", \
  533. debug->__name.word_base, \
  534. debug->__name.word_count, \
  535. debug->__name.word_size); \
  536. }
  537. RT2X00DEBUGFS_SPRINTF_REGISTER(csr);
  538. RT2X00DEBUGFS_SPRINTF_REGISTER(eeprom);
  539. RT2X00DEBUGFS_SPRINTF_REGISTER(bbp);
  540. RT2X00DEBUGFS_SPRINTF_REGISTER(rf);
  541. RT2X00DEBUGFS_SPRINTF_REGISTER(rfcsr);
  542. #undef RT2X00DEBUGFS_SPRINTF_REGISTER
  543. blob->size = strlen(blob->data);
  544. return debugfs_create_blob(name, S_IRUSR, intf->driver_folder, blob);
  545. }
  546. void rt2x00debug_register(struct rt2x00_dev *rt2x00dev)
  547. {
  548. const struct rt2x00debug *debug = rt2x00dev->ops->debugfs;
  549. struct rt2x00debug_intf *intf;
  550. intf = kzalloc(sizeof(struct rt2x00debug_intf), GFP_KERNEL);
  551. if (!intf) {
  552. rt2x00_err(rt2x00dev, "Failed to allocate debug handler\n");
  553. return;
  554. }
  555. intf->debug = debug;
  556. intf->rt2x00dev = rt2x00dev;
  557. rt2x00dev->debugfs_intf = intf;
  558. intf->driver_folder =
  559. debugfs_create_dir(intf->rt2x00dev->ops->name,
  560. rt2x00dev->hw->wiphy->debugfsdir);
  561. if (IS_ERR(intf->driver_folder) || !intf->driver_folder)
  562. goto exit;
  563. intf->driver_entry =
  564. rt2x00debug_create_file_driver("driver", intf, &intf->driver_blob);
  565. if (IS_ERR(intf->driver_entry) || !intf->driver_entry)
  566. goto exit;
  567. intf->chipset_entry =
  568. rt2x00debug_create_file_chipset("chipset",
  569. intf, &intf->chipset_blob);
  570. if (IS_ERR(intf->chipset_entry) || !intf->chipset_entry)
  571. goto exit;
  572. intf->dev_flags = debugfs_create_file("dev_flags", S_IRUSR,
  573. intf->driver_folder, intf,
  574. &rt2x00debug_fop_dev_flags);
  575. if (IS_ERR(intf->dev_flags) || !intf->dev_flags)
  576. goto exit;
  577. intf->cap_flags = debugfs_create_file("cap_flags", S_IRUSR,
  578. intf->driver_folder, intf,
  579. &rt2x00debug_fop_cap_flags);
  580. if (IS_ERR(intf->cap_flags) || !intf->cap_flags)
  581. goto exit;
  582. intf->register_folder =
  583. debugfs_create_dir("register", intf->driver_folder);
  584. if (IS_ERR(intf->register_folder) || !intf->register_folder)
  585. goto exit;
  586. #define RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(__intf, __name) \
  587. ({ \
  588. if(debug->__name.read) { \
  589. (__intf)->__name##_off_entry = \
  590. debugfs_create_u32(__stringify(__name) "_offset", \
  591. S_IRUSR | S_IWUSR, \
  592. (__intf)->register_folder, \
  593. &(__intf)->offset_##__name); \
  594. if (IS_ERR((__intf)->__name##_off_entry) \
  595. || !(__intf)->__name##_off_entry) \
  596. goto exit; \
  597. \
  598. (__intf)->__name##_val_entry = \
  599. debugfs_create_file(__stringify(__name) "_value", \
  600. S_IRUSR | S_IWUSR, \
  601. (__intf)->register_folder, \
  602. (__intf), &rt2x00debug_fop_##__name); \
  603. if (IS_ERR((__intf)->__name##_val_entry) \
  604. || !(__intf)->__name##_val_entry) \
  605. goto exit; \
  606. } \
  607. })
  608. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, csr);
  609. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, eeprom);
  610. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, bbp);
  611. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rf);
  612. RT2X00DEBUGFS_CREATE_REGISTER_ENTRY(intf, rfcsr);
  613. #undef RT2X00DEBUGFS_CREATE_REGISTER_ENTRY
  614. intf->queue_folder =
  615. debugfs_create_dir("queue", intf->driver_folder);
  616. if (IS_ERR(intf->queue_folder) || !intf->queue_folder)
  617. goto exit;
  618. intf->queue_frame_dump_entry =
  619. debugfs_create_file("dump", S_IRUSR, intf->queue_folder,
  620. intf, &rt2x00debug_fop_queue_dump);
  621. if (IS_ERR(intf->queue_frame_dump_entry)
  622. || !intf->queue_frame_dump_entry)
  623. goto exit;
  624. skb_queue_head_init(&intf->frame_dump_skbqueue);
  625. init_waitqueue_head(&intf->frame_dump_waitqueue);
  626. intf->queue_stats_entry =
  627. debugfs_create_file("queue", S_IRUSR, intf->queue_folder,
  628. intf, &rt2x00debug_fop_queue_stats);
  629. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  630. if (rt2x00_has_cap_hw_crypto(rt2x00dev))
  631. intf->crypto_stats_entry =
  632. debugfs_create_file("crypto", S_IRUGO, intf->queue_folder,
  633. intf, &rt2x00debug_fop_crypto_stats);
  634. #endif
  635. return;
  636. exit:
  637. rt2x00debug_deregister(rt2x00dev);
  638. rt2x00_err(rt2x00dev, "Failed to register debug handler\n");
  639. }
  640. void rt2x00debug_deregister(struct rt2x00_dev *rt2x00dev)
  641. {
  642. struct rt2x00debug_intf *intf = rt2x00dev->debugfs_intf;
  643. if (unlikely(!intf))
  644. return;
  645. skb_queue_purge(&intf->frame_dump_skbqueue);
  646. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  647. debugfs_remove(intf->crypto_stats_entry);
  648. #endif
  649. debugfs_remove(intf->queue_stats_entry);
  650. debugfs_remove(intf->queue_frame_dump_entry);
  651. debugfs_remove(intf->queue_folder);
  652. debugfs_remove(intf->rfcsr_val_entry);
  653. debugfs_remove(intf->rfcsr_off_entry);
  654. debugfs_remove(intf->rf_val_entry);
  655. debugfs_remove(intf->rf_off_entry);
  656. debugfs_remove(intf->bbp_val_entry);
  657. debugfs_remove(intf->bbp_off_entry);
  658. debugfs_remove(intf->eeprom_val_entry);
  659. debugfs_remove(intf->eeprom_off_entry);
  660. debugfs_remove(intf->csr_val_entry);
  661. debugfs_remove(intf->csr_off_entry);
  662. debugfs_remove(intf->register_folder);
  663. debugfs_remove(intf->dev_flags);
  664. debugfs_remove(intf->cap_flags);
  665. debugfs_remove(intf->chipset_entry);
  666. debugfs_remove(intf->driver_entry);
  667. debugfs_remove(intf->driver_folder);
  668. kfree(intf->chipset_blob.data);
  669. kfree(intf->driver_blob.data);
  670. kfree(intf);
  671. rt2x00dev->debugfs_intf = NULL;
  672. }