V4L/DVB (5835): saa7146/dvb-ttpci: Fix signedness warnings (gcc 4.1.1, kernel 2.6.22)
[safe/jmp/linux-2.6] / drivers / mmc / core / sd.c
index 6597e77..1240684 100644 (file)
 #include <linux/mmc/host.h>
 #include <linux/mmc/card.h>
 #include <linux/mmc/mmc.h>
+#include <linux/mmc/sd.h>
 
 #include "core.h"
 #include "sysfs.h"
+#include "bus.h"
 #include "mmc_ops.h"
 #include "sd_ops.h"
 
-#include "core.h"
-
 static const unsigned int tran_exp[] = {
        10000,          100000,         1000000,        10000000,
        0,              0,              0,              0
@@ -88,7 +88,7 @@ static void mmc_decode_cid(struct mmc_card *card)
 /*
  * Given a 128-bit response, decode to our card CSD structure.
  */
-static void mmc_decode_csd(struct mmc_card *card)
+static int mmc_decode_csd(struct mmc_card *card)
 {
        struct mmc_csd *csd = &card->csd;
        unsigned int e, m, csd_struct;
@@ -151,15 +151,16 @@ static void mmc_decode_csd(struct mmc_card *card)
        default:
                printk("%s: unrecognised CSD structure version %d\n",
                        mmc_hostname(card->host), csd_struct);
-               mmc_card_set_bad(card);
-               return;
+               return -EINVAL;
        }
+
+       return 0;
 }
 
 /*
  * Given a 64-bit response, decode to our card SCR structure.
  */
-static void mmc_decode_scr(struct mmc_card *card)
+static int mmc_decode_scr(struct mmc_card *card)
 {
        struct sd_scr *scr = &card->scr;
        unsigned int scr_struct;
@@ -174,12 +175,13 @@ static void mmc_decode_scr(struct mmc_card *card)
        if (scr_struct != 0) {
                printk("%s: unrecognised SCR structure version %d\n",
                        mmc_hostname(card->host), scr_struct);
-               mmc_card_set_bad(card);
-               return;
+               return -EINVAL;
        }
 
        scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
        scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
+
+       return 0;
 }
 
 /*
@@ -190,6 +192,16 @@ static int mmc_read_switch(struct mmc_card *card)
        int err;
        u8 *status;
 
+       if (card->scr.sda_vsn < SCR_SPEC_VER_1)
+               return MMC_ERR_NONE;
+
+       if (!(card->csd.cmdclass & CCC_SWITCH)) {
+               printk(KERN_WARNING "%s: card lacks mandatory switch "
+                       "function, performance might suffer.\n",
+                       mmc_hostname(card->host));
+               return MMC_ERR_NONE;
+       }
+
        err = MMC_ERR_FAILED;
 
        status = kmalloc(64, GFP_KERNEL);
@@ -202,10 +214,9 @@ static int mmc_read_switch(struct mmc_card *card)
 
        err = mmc_sd_switch(card, 0, 0, 1, status);
        if (err != MMC_ERR_NONE) {
-               /*
-                * Card not supporting high-speed will ignore the
-                * command.
-                */
+               printk(KERN_WARNING "%s: problem reading switch "
+                       "capabilities, performance might suffer.\n",
+                       mmc_hostname(card->host));
                err = MMC_ERR_NONE;
                goto out;
        }
@@ -227,6 +238,12 @@ static int mmc_switch_hs(struct mmc_card *card)
        int err;
        u8 *status;
 
+       if (card->scr.sda_vsn < SCR_SPEC_VER_1)
+               return MMC_ERR_NONE;
+
+       if (!(card->csd.cmdclass & CCC_SWITCH))
+               return MMC_ERR_NONE;
+
        if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
                return MMC_ERR_NONE;
 
@@ -342,7 +359,10 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
                if (err != MMC_ERR_NONE)
                        goto free_card;
 
-               mmc_decode_csd(card);
+               err = mmc_decode_csd(card);
+               if (err < 0)
+                       goto free_card;
+
                mmc_decode_cid(card);
        }
 
@@ -361,7 +381,9 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
                if (err != MMC_ERR_NONE)
                        goto free_card;
 
-               mmc_decode_scr(card);
+               err = mmc_decode_scr(card);
+               if (err < 0)
+                       goto free_card;
 
                /*
                 * Fetch switch information from card.
@@ -395,7 +417,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
        /*
         * Switch to wider bus (if supported).
         */
-       if ((host->caps && MMC_CAP_4_BIT_DATA) &&
+       if ((host->caps & MMC_CAP_4_BIT_DATA) &&
                (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
                err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
                if (err != MMC_ERR_NONE)
@@ -404,6 +426,21 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
                mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
        }
 
+       /*
+        * Check if read-only switch is active.
+        */
+       if (!oldcard) {
+               if (!host->ops->get_ro) {
+                       printk(KERN_WARNING "%s: host does not "
+                               "support reading read-only "
+                               "switch. assuming write-enable.\n",
+                               mmc_hostname(host));
+               } else {
+                       if (host->ops->get_ro(host))
+                               mmc_card_set_readonly(card);
+               }
+       }
+
        if (!oldcard)
                host->card = card;
 
@@ -449,8 +486,7 @@ static void mmc_sd_detect(struct mmc_host *host)
        mmc_release_host(host);
 
        if (err != MMC_ERR_NONE) {
-               mmc_remove_card(host->card);
-               host->card = NULL;
+               mmc_sd_remove(host);
 
                mmc_claim_host(host);
                mmc_detach_bus(host);
@@ -458,6 +494,55 @@ static void mmc_sd_detect(struct mmc_host *host)
        }
 }
 
+MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
+       card->raw_cid[2], card->raw_cid[3]);
+MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
+       card->raw_csd[2], card->raw_csd[3]);
+MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
+MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year);
+MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev);
+MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev);
+MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid);
+MMC_ATTR_FN(name, "%s\n", card->cid.prod_name);
+MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid);
+MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial);
+
+static struct device_attribute mmc_sd_dev_attrs[] = {
+       MMC_ATTR_RO(cid),
+       MMC_ATTR_RO(csd),
+       MMC_ATTR_RO(scr),
+       MMC_ATTR_RO(date),
+       MMC_ATTR_RO(fwrev),
+       MMC_ATTR_RO(hwrev),
+       MMC_ATTR_RO(manfid),
+       MMC_ATTR_RO(name),
+       MMC_ATTR_RO(oemid),
+       MMC_ATTR_RO(serial),
+       __ATTR_NULL,
+};
+
+/*
+ * Adds sysfs entries as relevant.
+ */
+static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card)
+{
+       int ret;
+
+       ret = mmc_add_attrs(card, mmc_sd_dev_attrs);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+/*
+ * Removes the sysfs entries added by mmc_sysfs_add().
+ */
+static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card)
+{
+       mmc_remove_attrs(card, mmc_sd_dev_attrs);
+}
+
 #ifdef CONFIG_MMC_UNSAFE_RESUME
 
 /*
@@ -491,9 +576,7 @@ static void mmc_sd_resume(struct mmc_host *host)
 
        err = mmc_sd_init_card(host, host->ocr, host->card);
        if (err != MMC_ERR_NONE) {
-               mmc_remove_card(host->card);
-               host->card = NULL;
-
+               mmc_sd_remove(host);
                mmc_detach_bus(host);
        }
 
@@ -510,6 +593,8 @@ static void mmc_sd_resume(struct mmc_host *host)
 static const struct mmc_bus_ops mmc_sd_ops = {
        .remove = mmc_sd_remove,
        .detect = mmc_sd_detect,
+       .sysfs_add = mmc_sd_sysfs_add,
+       .sysfs_remove = mmc_sd_sysfs_remove,
        .suspend = mmc_sd_suspend,
        .resume = mmc_sd_resume,
 };
@@ -561,7 +646,7 @@ int mmc_attach_sd(struct mmc_host *host, u32 ocr)
 
        mmc_release_host(host);
 
-       err = mmc_register_card(host->card);
+       err = mmc_add_card(host->card);
        if (err)
                goto reclaim_host;