mmc: Add per-card debugfs support

For each card successfully added to the bus, create a subdirectory under
the host's debugfs root with information about the card.

At the moment, only a single file is added to the card directory for
all cards: "state". It reflects the "state" field in struct mmc_card,
indicating whether the card is present, readonly, etc.

For MMC and SD cards (not SDIO), another file is added: "status".
Reading this file will ask the card about its current status and
return it. This can be useful if the card just refuses to respond to
any commands, which might indicate that the card state is not what the
MMC core thinks it is (due to a missing stop command, for example.)

Signed-off-by: Haavard Skinnemoen <haavard.skinnemoen@atmel.com>
Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>

authored by

Haavard Skinnemoen and committed by
Pierre Ossman
f4b7f927 6edd8ee6

+74
+8
drivers/mmc/core/bus.c
··· 252 252 if (ret) 253 253 return ret; 254 254 255 + #ifdef CONFIG_DEBUG_FS 256 + mmc_add_card_debugfs(card); 257 + #endif 258 + 255 259 mmc_card_set_present(card); 256 260 257 261 return 0; ··· 267 263 */ 268 264 void mmc_remove_card(struct mmc_card *card) 269 265 { 266 + #ifdef CONFIG_DEBUG_FS 267 + mmc_remove_card_debugfs(card); 268 + #endif 269 + 270 270 if (mmc_card_present(card)) { 271 271 if (mmc_host_is_spi(card->host)) { 272 272 printk(KERN_INFO "%s: SPI card removed\n",
+3
drivers/mmc/core/core.h
··· 56 56 void mmc_add_host_debugfs(struct mmc_host *host); 57 57 void mmc_remove_host_debugfs(struct mmc_host *host); 58 58 59 + void mmc_add_card_debugfs(struct mmc_card *card); 60 + void mmc_remove_card_debugfs(struct mmc_card *card); 61 + 59 62 #endif 60 63
+61
drivers/mmc/core/debugfs.c
··· 12 12 #include <linux/seq_file.h> 13 13 #include <linux/stat.h> 14 14 15 + #include <linux/mmc/card.h> 15 16 #include <linux/mmc/host.h> 16 17 17 18 #include "core.h" 19 + #include "mmc_ops.h" 18 20 19 21 /* The debugfs functions are optimized away when CONFIG_DEBUG_FS isn't set. */ 20 22 static int mmc_ios_show(struct seq_file *s, void *data) ··· 163 161 void mmc_remove_host_debugfs(struct mmc_host *host) 164 162 { 165 163 debugfs_remove_recursive(host->debugfs_root); 164 + } 165 + 166 + static int mmc_dbg_card_status_get(void *data, u64 *val) 167 + { 168 + struct mmc_card *card = data; 169 + u32 status; 170 + int ret; 171 + 172 + mmc_claim_host(card->host); 173 + 174 + ret = mmc_send_status(data, &status); 175 + if (!ret) 176 + *val = status; 177 + 178 + mmc_release_host(card->host); 179 + 180 + return ret; 181 + } 182 + DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, 183 + NULL, "%08llx\n"); 184 + 185 + void mmc_add_card_debugfs(struct mmc_card *card) 186 + { 187 + struct mmc_host *host = card->host; 188 + struct dentry *root; 189 + 190 + if (!host->debugfs_root) 191 + return; 192 + 193 + root = debugfs_create_dir(mmc_card_id(card), host->debugfs_root); 194 + if (IS_ERR(root)) 195 + /* Don't complain -- debugfs just isn't enabled */ 196 + return; 197 + if (!root) 198 + /* Complain -- debugfs is enabled, but it failed to 199 + * create the directory. */ 200 + goto err; 201 + 202 + card->debugfs_root = root; 203 + 204 + if (!debugfs_create_x32("state", S_IRUSR, root, &card->state)) 205 + goto err; 206 + 207 + if (mmc_card_mmc(card) || mmc_card_sd(card)) 208 + if (!debugfs_create_file("status", S_IRUSR, root, card, 209 + &mmc_dbg_card_status_fops)) 210 + goto err; 211 + 212 + return; 213 + 214 + err: 215 + debugfs_remove_recursive(root); 216 + card->debugfs_root = NULL; 217 + dev_err(&card->dev, "failed to initialize debugfs\n"); 218 + } 219 + 220 + void mmc_remove_card_debugfs(struct mmc_card *card) 221 + { 222 + debugfs_remove_recursive(card->debugfs_root); 166 223 }
+2
include/linux/mmc/card.h
··· 111 111 unsigned num_info; /* number of info strings */ 112 112 const char **info; /* info strings */ 113 113 struct sdio_func_tuple *tuples; /* unknown common tuples */ 114 + 115 + struct dentry *debugfs_root; 114 116 }; 115 117 116 118 #define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)