LCOV - code coverage report
Current view: top level - lkbce/drivers/mmc/core - bus.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 87 126 69.0 %
Date: 2017-01-25 Functions: 11 15 73.3 %

          Line data    Source code
       1             : /*
       2             :  *  linux/drivers/mmc/core/bus.c
       3             :  *
       4             :  *  Copyright (C) 2003 Russell King, All Rights Reserved.
       5             :  *  Copyright (C) 2007 Pierre Ossman
       6             :  *
       7             :  * This program is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License version 2 as
       9             :  * published by the Free Software Foundation.
      10             :  *
      11             :  *  MMC card bus driver model
      12             :  */
      13             : 
      14             : #include <linux/device.h>
      15             : #include <linux/err.h>
      16             : 
      17             : #include <linux/mmc/card.h>
      18             : #include <linux/mmc/host.h>
      19             : 
      20             : #include "core.h"
      21             : #include "sdio_cis.h"
      22             : #include "bus.h"
      23             : 
      24             : #define dev_to_mmc_card(d)      container_of(d, struct mmc_card, dev)
      25             : #define to_mmc_driver(d)        container_of(d, struct mmc_driver, drv)
      26             : 
      27             : static ssize_t mmc_type_show(struct device *dev,
      28             :         struct device_attribute *attr, char *buf)
      29             : {
      30           3 :         struct mmc_card *card = dev_to_mmc_card(dev);
      31           1 : 
      32           1 :         switch (card->type) {
      33           4 :         case MMC_TYPE_MMC:
      34           3 :                 return sprintf(buf, "MMC\n");
      35           4 :         case MMC_TYPE_SD:
      36           2 :                 return sprintf(buf, "SD\n");
      37           4 :         case MMC_TYPE_SDIO:
      38           2 :                 return sprintf(buf, "SDIO\n");
      39           2 :         default:
      40           2 :                 return -EFAULT;
      41             :         }
      42             : }
      43             : 
      44           1 : static struct device_attribute mmc_dev_attrs[] = {
      45             :         __ATTR(type, S_IRUGO, mmc_type_show, NULL),
      46             :         __ATTR_NULL,
      47             : };
      48             : 
      49             : /*
      50             :  * This currently matches any MMC driver to any MMC card - drivers
      51             :  * themselves make the decision whether to drive this card in their
      52             :  * probe method.
      53             :  */
      54             : static int mmc_bus_match(struct device *dev, struct device_driver *drv)
      55             : {
      56           6 :         return 1;
      57             : }
      58             : 
      59             : static int
      60             : mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
      61             : {
      62           3 :         struct mmc_card *card = dev_to_mmc_card(dev);
      63           1 :         const char *type;
      64           2 :         int retval = 0;
      65           1 : 
      66             :         switch (card->type) {
      67           3 :         case MMC_TYPE_MMC:
      68           1 :                 type = "MMC";
      69           1 :                 break;
      70           4 :         case MMC_TYPE_SD:
      71           1 :                 type = "SD";
      72           1 :                 break;
      73           4 :         case MMC_TYPE_SDIO:
      74           1 :                 type = "SDIO";
      75           1 :                 break;
      76           2 :         default:
      77           2 :                 type = NULL;
      78           1 :         }
      79             : 
      80           3 :         if (type) {
      81           2 :                 retval = add_uevent_var(env, "MMC_TYPE=%s", type);
      82           2 :                 if (retval)
      83           1 :                         return retval;
      84             :         }
      85             : 
      86           4 :         retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
      87           2 :         if (retval)
      88           1 :                 return retval;
      89             : 
      90             :         /*
      91             :          * Request the mmc_block device.  Note: that this is a direct request
      92             :          * for the module it carries no information as to what is inserted.
      93             :          */
      94           2 :         retval = add_uevent_var(env, "MODALIAS=mmc:block");
      95             : 
      96           1 :         return retval;
      97             : }
      98             : 
      99             : static int mmc_bus_probe(struct device *dev)
     100             : {
     101          24 :         struct mmc_driver *drv = to_mmc_driver(dev->driver);
     102          18 :         struct mmc_card *card = dev_to_mmc_card(dev);
     103           6 : 
     104          24 :         return drv->probe(card);
     105           6 : }
     106             : 
     107             : static int mmc_bus_remove(struct device *dev)
     108             : {
     109          24 :         struct mmc_driver *drv = to_mmc_driver(dev->driver);
     110          18 :         struct mmc_card *card = dev_to_mmc_card(dev);
     111           6 : 
     112          12 :         drv->remove(card);
     113             : 
     114           6 :         return 0;
     115             : }
     116             : 
     117             : static int mmc_bus_suspend(struct device *dev, pm_message_t state)
     118             : {
     119          72 :         struct mmc_driver *drv = to_mmc_driver(dev->driver);
     120          54 :         struct mmc_card *card = dev_to_mmc_card(dev);
     121          36 :         int ret = 0;
     122          18 : 
     123         126 :         if (dev->driver && drv->suspend)
     124          18 :                 ret = drv->suspend(card, state);
     125          18 :         return ret;
     126             : }
     127             : 
     128             : static int mmc_bus_resume(struct device *dev)
     129             : {
     130          84 :         struct mmc_driver *drv = to_mmc_driver(dev->driver);
     131          63 :         struct mmc_card *card = dev_to_mmc_card(dev);
     132          42 :         int ret = 0;
     133          21 : 
     134         147 :         if (dev->driver && drv->resume)
     135          42 :                 ret = drv->resume(card);
     136          21 :         return ret;
     137             : }
     138             : 
     139           1 : static struct bus_type mmc_bus_type = {
     140             :         .name           = "mmc",
     141             :         .dev_attrs      = mmc_dev_attrs,
     142             :         .match          = mmc_bus_match,
     143             :         .uevent         = mmc_bus_uevent,
     144             :         .probe          = mmc_bus_probe,
     145             :         .remove         = mmc_bus_remove,
     146             :         .suspend        = mmc_bus_suspend,
     147             :         .resume         = mmc_bus_resume,
     148             : };
     149             : 
     150             : int mmc_register_bus(void)
     151             : {
     152           3 :         return bus_register(&mmc_bus_type);
     153             : }
     154             : 
     155             : void mmc_unregister_bus(void)
     156             : {
     157           4 :         bus_unregister(&mmc_bus_type);
     158           4 : }
     159             : 
     160             : /**
     161             :  *      mmc_register_driver - register a media driver
     162             :  *      @drv: MMC media driver
     163             :  */
     164             : int mmc_register_driver(struct mmc_driver *drv)
     165             : {
     166           0 :         drv->drv.bus = &mmc_bus_type;
     167           0 :         return driver_register(&drv->drv);
     168             : }
     169             : 
     170             : EXPORT_SYMBOL(mmc_register_driver);
     171             : 
     172             : /**
     173             :  *      mmc_unregister_driver - unregister a media driver
     174             :  *      @drv: MMC media driver
     175             :  */
     176             : void mmc_unregister_driver(struct mmc_driver *drv)
     177             : {
     178           0 :         drv->drv.bus = &mmc_bus_type;
     179           0 :         driver_unregister(&drv->drv);
     180           0 : }
     181             : 
     182             : EXPORT_SYMBOL(mmc_unregister_driver);
     183             : 
     184             : static void mmc_release_card(struct device *dev)
     185             : {
     186           0 :         struct mmc_card *card = dev_to_mmc_card(dev);
     187           0 : 
     188           0 :         sdio_free_common_cis(card);
     189             : 
     190           0 :         if (card->info)
     191           0 :                 kfree(card->info);
     192             : 
     193           0 :         kfree(card);
     194           0 : }
     195             : 
     196             : /*
     197             :  * Allocate and initialise a new MMC card structure.
     198             :  */
     199             : struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
     200             : {
     201           8 :         struct mmc_card *card;
     202           8 : 
     203          32 :         card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
     204          16 :         if (!card)
     205          24 :                 return ERR_PTR(-ENOMEM);
     206             : 
     207           8 :         card->host = host;
     208             : 
     209           8 :         device_initialize(&card->dev);
     210             : 
     211           8 :         card->dev.parent = mmc_classdev(host);
     212           8 :         card->dev.bus = &mmc_bus_type;
     213           8 :         card->dev.release = mmc_release_card;
     214           8 :         card->dev.type = type;
     215             : 
     216           8 :         return card;
     217             : }
     218             : 
     219             : /*
     220             :  * Register a new MMC card with the driver model.
     221             :  */
     222             : int mmc_add_card(struct mmc_card *card)
     223             : {
     224           0 :         int ret;
     225           0 :         const char *type;
     226           0 : 
     227           0 :         dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
     228           0 : 
     229             :         switch (card->type) {
     230           0 :         case MMC_TYPE_MMC:
     231           0 :                 type = "MMC";
     232           0 :                 break;
     233           0 :         case MMC_TYPE_SD:
     234           0 :                 type = "SD";
     235           0 :                 if (mmc_card_blockaddr(card))
     236           0 :                         type = "SDHC";
     237           0 :                 break;
     238           0 :         case MMC_TYPE_SDIO:
     239           0 :                 type = "SDIO";
     240           0 :                 break;
     241           0 :         default:
     242           0 :                 type = "?";
     243           0 :                 break;
     244             :         }
     245             : 
     246           0 :         if (mmc_host_is_spi(card->host)) {
     247           0 :                 printk(KERN_INFO "%s: new %s%s card on SPI\n",
     248             :                         mmc_hostname(card->host),
     249             :                         mmc_card_highspeed(card) ? "high speed " : "",
     250             :                         type);
     251             :         } else {
     252           0 :                 printk(KERN_INFO "%s: new %s%s card at address %04x\n",
     253             :                         mmc_hostname(card->host),
     254             :                         mmc_card_highspeed(card) ? "high speed " : "",
     255             :                         type, card->rca);
     256             :         }
     257             : 
     258           0 :         ret = device_add(&card->dev);
     259           0 :         if (ret)
     260           0 :                 return ret;
     261             : 
     262             : #ifdef CONFIG_DEBUG_FS
     263             :         mmc_add_card_debugfs(card);
     264             : #endif
     265             : 
     266           0 :         mmc_card_set_present(card);
     267             : 
     268           0 :         return 0;
     269             : }
     270             : 
     271             : /*
     272             :  * Unregister a new MMC card with the driver model, and
     273             :  * (eventually) free it.
     274             :  */
     275             : void mmc_remove_card(struct mmc_card *card)
     276             : {
     277          41 : #ifdef CONFIG_DEBUG_FS
     278          41 :         mmc_remove_card_debugfs(card);
     279             : #endif
     280             : 
     281         123 :         if (mmc_card_present(card)) {
     282          82 :                 if (mmc_host_is_spi(card->host)) {
     283         123 :                         printk(KERN_INFO "%s: SPI card removed\n",
     284             :                                 mmc_hostname(card->host));
     285             :                 } else {
     286         123 :                         printk(KERN_INFO "%s: card %04x removed\n",
     287             :                                 mmc_hostname(card->host), card->rca);
     288             :                 }
     289          82 :                 device_del(&card->dev);
     290             :         }
     291             : 
     292         123 :         put_device(&card->dev);
     293         123 : }
     294             : 

Generated by: LCOV version 1.10