debugfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Debugfs support for hosts and cards
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/stat.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/host.h>
  16. #include "core.h"
  17. #include "mmc_ops.h"
  18. /* The debugfs functions are optimized away when CONFIG_DEBUG_FS isn't set. */
  19. static int mmc_ios_show(struct seq_file *s, void *data)
  20. {
  21. static const char *vdd_str[] = {
  22. [8] = "2.0",
  23. [9] = "2.1",
  24. [10] = "2.2",
  25. [11] = "2.3",
  26. [12] = "2.4",
  27. [13] = "2.5",
  28. [14] = "2.6",
  29. [15] = "2.7",
  30. [16] = "2.8",
  31. [17] = "2.9",
  32. [18] = "3.0",
  33. [19] = "3.1",
  34. [20] = "3.2",
  35. [21] = "3.3",
  36. [22] = "3.4",
  37. [23] = "3.5",
  38. [24] = "3.6",
  39. };
  40. struct mmc_host *host = s->private;
  41. struct mmc_ios *ios = &host->ios;
  42. const char *str;
  43. seq_printf(s, "clock:\t\t%u Hz\n", ios->clock);
  44. seq_printf(s, "vdd:\t\t%u ", ios->vdd);
  45. if ((1 << ios->vdd) & MMC_VDD_165_195)
  46. seq_printf(s, "(1.65 - 1.95 V)\n");
  47. else if (ios->vdd < (ARRAY_SIZE(vdd_str) - 1)
  48. && vdd_str[ios->vdd] && vdd_str[ios->vdd + 1])
  49. seq_printf(s, "(%s ~ %s V)\n", vdd_str[ios->vdd],
  50. vdd_str[ios->vdd + 1]);
  51. else
  52. seq_printf(s, "(invalid)\n");
  53. switch (ios->bus_mode) {
  54. case MMC_BUSMODE_OPENDRAIN:
  55. str = "open drain";
  56. break;
  57. case MMC_BUSMODE_PUSHPULL:
  58. str = "push-pull";
  59. break;
  60. default:
  61. str = "invalid";
  62. break;
  63. }
  64. seq_printf(s, "bus mode:\t%u (%s)\n", ios->bus_mode, str);
  65. switch (ios->chip_select) {
  66. case MMC_CS_DONTCARE:
  67. str = "don't care";
  68. break;
  69. case MMC_CS_HIGH:
  70. str = "active high";
  71. break;
  72. case MMC_CS_LOW:
  73. str = "active low";
  74. break;
  75. default:
  76. str = "invalid";
  77. break;
  78. }
  79. seq_printf(s, "chip select:\t%u (%s)\n", ios->chip_select, str);
  80. switch (ios->power_mode) {
  81. case MMC_POWER_OFF:
  82. str = "off";
  83. break;
  84. case MMC_POWER_UP:
  85. str = "up";
  86. break;
  87. case MMC_POWER_ON:
  88. str = "on";
  89. break;
  90. default:
  91. str = "invalid";
  92. break;
  93. }
  94. seq_printf(s, "power mode:\t%u (%s)\n", ios->power_mode, str);
  95. seq_printf(s, "bus width:\t%u (%u bits)\n",
  96. ios->bus_width, 1 << ios->bus_width);
  97. switch (ios->timing) {
  98. case MMC_TIMING_LEGACY:
  99. str = "legacy";
  100. break;
  101. case MMC_TIMING_MMC_HS:
  102. str = "mmc high-speed";
  103. break;
  104. case MMC_TIMING_SD_HS:
  105. str = "sd high-speed";
  106. break;
  107. default:
  108. str = "invalid";
  109. break;
  110. }
  111. seq_printf(s, "timing spec:\t%u (%s)\n", ios->timing, str);
  112. return 0;
  113. }
  114. static int mmc_ios_open(struct inode *inode, struct file *file)
  115. {
  116. return single_open(file, mmc_ios_show, inode->i_private);
  117. }
  118. static const struct file_operations mmc_ios_fops = {
  119. .open = mmc_ios_open,
  120. .read = seq_read,
  121. .llseek = seq_lseek,
  122. .release = single_release,
  123. };
  124. void mmc_add_host_debugfs(struct mmc_host *host)
  125. {
  126. struct dentry *root;
  127. root = debugfs_create_dir(mmc_hostname(host), NULL);
  128. if (IS_ERR(root))
  129. /* Don't complain -- debugfs just isn't enabled */
  130. return;
  131. if (!root)
  132. /* Complain -- debugfs is enabled, but it failed to
  133. * create the directory. */
  134. goto err_root;
  135. host->debugfs_root = root;
  136. if (!debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops))
  137. goto err_ios;
  138. return;
  139. err_ios:
  140. debugfs_remove_recursive(root);
  141. host->debugfs_root = NULL;
  142. err_root:
  143. dev_err(&host->class_dev, "failed to initialize debugfs\n");
  144. }
  145. void mmc_remove_host_debugfs(struct mmc_host *host)
  146. {
  147. debugfs_remove_recursive(host->debugfs_root);
  148. }
  149. static int mmc_dbg_card_status_get(void *data, u64 *val)
  150. {
  151. struct mmc_card *card = data;
  152. u32 status;
  153. int ret;
  154. mmc_claim_host(card->host);
  155. ret = mmc_send_status(data, &status);
  156. if (!ret)
  157. *val = status;
  158. mmc_release_host(card->host);
  159. return ret;
  160. }
  161. DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
  162. NULL, "%08llx\n");
  163. #define EXT_CSD_STR_LEN 1025
  164. static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
  165. {
  166. struct mmc_card *card = inode->i_private;
  167. char *buf;
  168. ssize_t n = 0;
  169. u8 *ext_csd;
  170. int err, i;
  171. buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
  172. if (!buf)
  173. return -ENOMEM;
  174. ext_csd = kmalloc(512, GFP_KERNEL);
  175. if (!ext_csd) {
  176. err = -ENOMEM;
  177. goto out_free;
  178. }
  179. mmc_claim_host(card->host);
  180. err = mmc_send_ext_csd(card, ext_csd);
  181. mmc_release_host(card->host);
  182. if (err)
  183. goto out_free;
  184. for (i = 511; i >= 0; i--)
  185. n += sprintf(buf + n, "%02x", ext_csd[i]);
  186. n += sprintf(buf + n, "\n");
  187. BUG_ON(n != EXT_CSD_STR_LEN);
  188. filp->private_data = buf;
  189. kfree(ext_csd);
  190. return 0;
  191. out_free:
  192. kfree(buf);
  193. kfree(ext_csd);
  194. return err;
  195. }
  196. static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
  197. size_t cnt, loff_t *ppos)
  198. {
  199. char *buf = filp->private_data;
  200. return simple_read_from_buffer(ubuf, cnt, ppos,
  201. buf, EXT_CSD_STR_LEN);
  202. }
  203. static int mmc_ext_csd_release(struct inode *inode, struct file *file)
  204. {
  205. kfree(file->private_data);
  206. return 0;
  207. }
  208. static struct file_operations mmc_dbg_ext_csd_fops = {
  209. .open = mmc_ext_csd_open,
  210. .read = mmc_ext_csd_read,
  211. .release = mmc_ext_csd_release,
  212. };
  213. void mmc_add_card_debugfs(struct mmc_card *card)
  214. {
  215. struct mmc_host *host = card->host;
  216. struct dentry *root;
  217. if (!host->debugfs_root)
  218. return;
  219. root = debugfs_create_dir(mmc_card_id(card), host->debugfs_root);
  220. if (IS_ERR(root))
  221. /* Don't complain -- debugfs just isn't enabled */
  222. return;
  223. if (!root)
  224. /* Complain -- debugfs is enabled, but it failed to
  225. * create the directory. */
  226. goto err;
  227. card->debugfs_root = root;
  228. if (!debugfs_create_x32("state", S_IRUSR, root, &card->state))
  229. goto err;
  230. if (mmc_card_mmc(card) || mmc_card_sd(card))
  231. if (!debugfs_create_file("status", S_IRUSR, root, card,
  232. &mmc_dbg_card_status_fops))
  233. goto err;
  234. if (mmc_card_mmc(card))
  235. if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
  236. &mmc_dbg_ext_csd_fops))
  237. goto err;
  238. return;
  239. err:
  240. debugfs_remove_recursive(root);
  241. card->debugfs_root = NULL;
  242. dev_err(&card->dev, "failed to initialize debugfs\n");
  243. }
  244. void mmc_remove_card_debugfs(struct mmc_card *card)
  245. {
  246. debugfs_remove_recursive(card->debugfs_root);
  247. }