LCOV - code coverage report
Current view: top level - drivers/mmc/card - block.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 149 322 46.3 %
Date: 2017-01-25 Functions: 12 15 80.0 %

          Line data    Source code
       1             : /*
       2             :  * Block driver for media (i.e., flash cards)
       3             :  *
       4             :  * Copyright 2002 Hewlett-Packard Company
       5             :  * Copyright 2005-2008 Pierre Ossman
       6             :  *
       7             :  * Use consistent with the GNU GPL is permitted,
       8             :  * provided that this copyright notice is
       9             :  * preserved in its entirety in all copies and derived works.
      10             :  *
      11             :  * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
      12             :  * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
      13             :  * FITNESS FOR ANY PARTICULAR PURPOSE.
      14             :  *
      15             :  * Many thanks to Alessandro Rubini and Jonathan Corbet!
      16             :  *
      17             :  * Author:  Andrew Christian
      18             :  *          28 May 2002
      19             :  */
      20             : #include <linux/moduleparam.h>
      21             : #include <linux/module.h>
      22             : #include <linux/init.h>
      23             : 
      24             : #include <linux/kernel.h>
      25             : #include <linux/fs.h>
      26             : #include <linux/errno.h>
      27             : #include <linux/hdreg.h>
      28             : #include <linux/kdev_t.h>
      29             : #include <linux/blkdev.h>
      30             : #include <linux/mutex.h>
      31             : #include <linux/scatterlist.h>
      32             : #include <linux/string_helpers.h>
      33             : 
      34             : #include <linux/mmc/card.h>
      35             : #include <linux/mmc/host.h>
      36             : #include <linux/mmc/mmc.h>
      37             : #include <linux/mmc/sd.h>
      38             : 
      39             : #include <asm/system.h>
      40             : #include <asm/uaccess.h>
      41             : 
      42             : #include "queue.h"
      43             : 
      44             : MODULE_ALIAS("mmc:block");
      45             : 
      46             : /*
      47             :  * max 8 partitions per card
      48             :  */
      49             : #define MMC_SHIFT       3
      50             : #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
      51             : 
      52           1 : static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
      53           1 : 
      54             : /*
      55             :  * There is one mmc_blk_data per slot.
      56             :  */
      57             : struct mmc_blk_data {
      58             :         spinlock_t      lock;
      59             :         struct gendisk  *disk;
      60             :         struct mmc_queue queue;
      61             : 
      62             :         unsigned int    usage;
      63             :         unsigned int    read_only;
      64             : };
      65             : 
      66           1 : static DEFINE_MUTEX(open_lock);
      67             : 
      68             : static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
      69             : {
      70           1 :         struct mmc_blk_data *md;
      71             : 
      72           1 :         mutex_lock(&open_lock);
      73           2 :         md = disk->private_data;
      74           4 :         if (md && md->usage == 0)
      75           1 :                 md = NULL;
      76           2 :         if (md)
      77           1 :                 md->usage++;
      78           1 :         mutex_unlock(&open_lock);
      79             : 
      80           1 :         return md;
      81             : }
      82             : 
      83             : static void mmc_blk_put(struct mmc_blk_data *md)
      84             : {
      85           8 :         mutex_lock(&open_lock);
      86           8 :         md->usage--;
      87          12 :         if (md->usage == 0) {
      88          16 :                 int devmaj = MAJOR(disk_devt(md->disk));
      89          12 :                 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
      90             : 
      91           8 :                 if (!devmaj)
      92           4 :                         devidx = md->disk->first_minor >> MMC_SHIFT;
      93             : 
      94           4 :                 blk_cleanup_queue(md->queue.queue);
      95             : 
      96           8 :                 __clear_bit(devidx, dev_use);
      97             : 
      98           4 :                 put_disk(md->disk);
      99           4 :                 kfree(md);
     100             :         }
     101           8 :         mutex_unlock(&open_lock);
     102           8 : }
     103             : 
     104             : static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
     105             : {
     106           4 :         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
     107           2 :         int ret = -ENXIO;
     108           1 : 
     109           2 :         if (md) {
     110           2 :                 if (md->usage == 2)
     111           1 :                         check_disk_change(bdev);
     112           1 :                 ret = 0;
     113             : 
     114           4 :                 if ((mode & FMODE_WRITE) && md->read_only) {
     115           3 :                         mmc_blk_put(md);
     116           1 :                         ret = -EROFS;
     117             :                 }
     118             :         }
     119             : 
     120           2 :         return ret;
     121             : }
     122             : 
     123             : static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
     124             : {
     125           3 :         struct mmc_blk_data *md = disk->private_data;
     126             : 
     127           3 :         mmc_blk_put(md);
     128           1 :         return 0;
     129             : }
     130             : 
     131             : static int
     132             : mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
     133             : {
     134           4 :         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
     135           1 :         geo->heads = 4;
     136           1 :         geo->sectors = 16;
     137           1 :         return 0;
     138             : }
     139             : 
     140           1 : static const struct block_device_operations mmc_bdops = {
     141             :         .open                   = mmc_blk_open,
     142             :         .release                = mmc_blk_release,
     143             :         .getgeo                 = mmc_blk_getgeo,
     144           1 :         .owner                  = THIS_MODULE,
     145             : };
     146             : 
     147             : struct mmc_blk_request {
     148             :         struct mmc_request      mrq;
     149             :         struct mmc_command      cmd;
     150             :         struct mmc_command      stop;
     151             :         struct mmc_data         data;
     152             : };
     153             : 
     154             : static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
     155             : {
     156           0 :         int err;
     157           0 :         u32 result;
     158           0 :         __be32 *blocks;
     159           0 : 
     160           0 :         struct mmc_request mrq;
     161           0 :         struct mmc_command cmd;
     162           0 :         struct mmc_data data;
     163           0 :         unsigned int timeout_us;
     164           0 : 
     165           0 :         struct scatterlist sg;
     166             : 
     167           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     168             : 
     169           0 :         cmd.opcode = MMC_APP_CMD;
     170           0 :         cmd.arg = card->rca << 16;
     171           0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
     172             : 
     173           0 :         err = mmc_wait_for_cmd(card->host, &cmd, 0);
     174           0 :         if (err)
     175           0 :                 return (u32)-1;
     176           0 :         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
     177           0 :                 return (u32)-1;
     178             : 
     179           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     180             : 
     181           0 :         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
     182           0 :         cmd.arg = 0;
     183           0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
     184             : 
     185           0 :         memset(&data, 0, sizeof(struct mmc_data));
     186             : 
     187           0 :         data.timeout_ns = card->csd.tacc_ns * 100;
     188           0 :         data.timeout_clks = card->csd.tacc_clks * 100;
     189             : 
     190           0 :         timeout_us = data.timeout_ns / 1000;
     191           0 :         timeout_us += data.timeout_clks * 1000 /
     192             :                 (card->host->ios.clock / 1000);
     193             : 
     194           0 :         if (timeout_us > 100000) {
     195           0 :                 data.timeout_ns = 100000000;
     196           0 :                 data.timeout_clks = 0;
     197             :         }
     198             : 
     199           0 :         data.blksz = 4;
     200           0 :         data.blocks = 1;
     201           0 :         data.flags = MMC_DATA_READ;
     202           0 :         data.sg = &sg;
     203           0 :         data.sg_len = 1;
     204             : 
     205           0 :         memset(&mrq, 0, sizeof(struct mmc_request));
     206             : 
     207           0 :         mrq.cmd = &cmd;
     208           0 :         mrq.data = &data;
     209             : 
     210           0 :         blocks = kmalloc(4, GFP_KERNEL);
     211           0 :         if (!blocks)
     212           0 :                 return (u32)-1;
     213             : 
     214           0 :         sg_init_one(&sg, blocks, 4);
     215             : 
     216           0 :         mmc_wait_for_req(card->host, &mrq);
     217             : 
     218           0 :         result = ntohl(*blocks);
     219           0 :         kfree(blocks);
     220             : 
     221           0 :         if (cmd.error || data.error)
     222           0 :                 result = (u32)-1;
     223             : 
     224           0 :         return result;
     225             : }
     226             : 
     227             : static u32 get_card_status(struct mmc_card *card, struct request *req)
     228             : {
     229           0 :         struct mmc_command cmd;
     230           0 :         int err;
     231             : 
     232           0 :         memset(&cmd, 0, sizeof(struct mmc_command));
     233           0 :         cmd.opcode = MMC_SEND_STATUS;
     234           0 :         if (!mmc_host_is_spi(card->host))
     235           0 :                 cmd.arg = card->rca << 16;
     236           0 :         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
     237           0 :         err = mmc_wait_for_cmd(card->host, &cmd, 0);
     238           0 :         if (err)
     239           0 :                 printk(KERN_ERR "%s: error %d sending status comand",
     240             :                        req->rq_disk->disk_name, err);
     241           0 :         return cmd.resp[0];
     242             : }
     243             : 
     244             : static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
     245             : {
     246           0 :         struct mmc_blk_data *md = mq->data;
     247           0 :         struct mmc_card *card = md->queue.card;
     248           0 :         struct mmc_blk_request brq;
     249           0 :         int ret = 1, disable_multi = 0;
     250           0 : 
     251           0 :         mmc_claim_host(card->host);
     252           0 : 
     253           0 :         do {
     254           0 :                 struct mmc_command cmd;
     255           0 :                 u32 readcmd, writecmd, status = 0;
     256           0 : 
     257           0 :                 memset(&brq, 0, sizeof(struct mmc_blk_request));
     258           0 :                 brq.mrq.cmd = &brq.cmd;
     259           0 :                 brq.mrq.data = &brq.data;
     260           0 : 
     261           0 :                 brq.cmd.arg = blk_rq_pos(req);
     262           0 :                 if (!mmc_card_blockaddr(card))
     263           0 :                         brq.cmd.arg <<= 9;
     264           0 :                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
     265           0 :                 brq.data.blksz = 512;
     266           0 :                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
     267           0 :                 brq.stop.arg = 0;
     268           0 :                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
     269           0 :                 brq.data.blocks = blk_rq_sectors(req);
     270             : 
     271             :                 /*
     272             :                  * The block layer doesn't support all sector count
     273             :                  * restrictions, so we need to be prepared for too big
     274             :                  * requests.
     275             :                  */
     276           0 :                 if (brq.data.blocks > card->host->max_blk_count)
     277           0 :                         brq.data.blocks = card->host->max_blk_count;
     278             : 
     279             :                 /*
     280             :                  * After a read error, we redo the request one sector at a time
     281             :                  * in order to accurately determine which sectors can be read
     282             :                  * successfully.
     283             :                  */
     284           0 :                 if (disable_multi && brq.data.blocks > 1)
     285           0 :                         brq.data.blocks = 1;
     286             : 
     287           0 :                 if (brq.data.blocks > 1) {
     288             :                         /* SPI multiblock writes terminate using a special
     289             :                          * token, not a STOP_TRANSMISSION request.
     290             :                          */
     291           0 :                         if (!mmc_host_is_spi(card->host)
     292             :                                         || rq_data_dir(req) == READ)
     293           0 :                                 brq.mrq.stop = &brq.stop;
     294           0 :                         readcmd = MMC_READ_MULTIPLE_BLOCK;
     295           0 :                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
     296             :                 } else {
     297           0 :                         brq.mrq.stop = NULL;
     298           0 :                         readcmd = MMC_READ_SINGLE_BLOCK;
     299           0 :                         writecmd = MMC_WRITE_BLOCK;
     300             :                 }
     301             : 
     302           0 :                 if (rq_data_dir(req) == READ) {
     303           0 :                         brq.cmd.opcode = readcmd;
     304           0 :                         brq.data.flags |= MMC_DATA_READ;
     305             :                 } else {
     306           0 :                         brq.cmd.opcode = writecmd;
     307           0 :                         brq.data.flags |= MMC_DATA_WRITE;
     308             :                 }
     309             : 
     310           0 :                 mmc_set_data_timeout(&brq.data, card);
     311             : 
     312           0 :                 brq.data.sg = mq->sg;
     313           0 :                 brq.data.sg_len = mmc_queue_map_sg(mq);
     314             : 
     315             :                 /*
     316             :                  * Adjust the sg list so it is the same size as the
     317             :                  * request.
     318             :                  */
     319           0 :                 if (brq.data.blocks != blk_rq_sectors(req)) {
     320           0 :                         int i, data_size = brq.data.blocks << 9;
     321             :                         struct scatterlist *sg;
     322             : 
     323           0 :                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
     324           0 :                                 data_size -= sg->length;
     325           0 :                                 if (data_size <= 0) {
     326           0 :                                         sg->length += data_size;
     327           0 :                                         i++;
     328           0 :                                         break;
     329           0 :                                 }
     330             :                         }
     331           0 :                         brq.data.sg_len = i;
     332             :                 }
     333             : 
     334           0 :                 mmc_queue_bounce_pre(mq);
     335             : 
     336           0 :                 mmc_wait_for_req(card->host, &brq.mrq);
     337             : 
     338           0 :                 mmc_queue_bounce_post(mq);
     339             : 
     340             :                 /*
     341             :                  * Check for errors here, but don't jump to cmd_err
     342             :                  * until later as we need to wait for the card to leave
     343             :                  * programming mode even when things go wrong.
     344             :                  */
     345           0 :                 if (brq.cmd.error || brq.data.error || brq.stop.error) {
     346           0 :                         if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
     347             :                                 /* Redo read one sector at a time */
     348           0 :                                 printk(KERN_WARNING "%s: retrying using single "
     349             :                                        "block read\n", req->rq_disk->disk_name);
     350           0 :                                 disable_multi = 1;
     351           0 :                                 continue;
     352             :                         }
     353           0 :                         status = get_card_status(card, req);
     354             :                 }
     355             : 
     356           0 :                 if (brq.cmd.error) {
     357           0 :                         printk(KERN_ERR "%s: error %d sending read/write "
     358             :                                "command, response %#x, card status %#x\n",
     359             :                                req->rq_disk->disk_name, brq.cmd.error,
     360             :                                brq.cmd.resp[0], status);
     361             :                 }
     362             : 
     363           0 :                 if (brq.data.error) {
     364           0 :                         if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
     365             :                                 /* 'Stop' response contains card status */
     366           0 :                                 status = brq.mrq.stop->resp[0];
     367           0 :                         printk(KERN_ERR "%s: error %d transferring data,"
     368             :                                " sector %u, nr %u, card status %#x\n",
     369             :                                req->rq_disk->disk_name, brq.data.error,
     370             :                                (unsigned)blk_rq_pos(req),
     371             :                                (unsigned)blk_rq_sectors(req), status);
     372             :                 }
     373             : 
     374           0 :                 if (brq.stop.error) {
     375           0 :                         printk(KERN_ERR "%s: error %d sending stop command, "
     376             :                                "response %#x, card status %#x\n",
     377             :                                req->rq_disk->disk_name, brq.stop.error,
     378             :                                brq.stop.resp[0], status);
     379             :                 }
     380             : 
     381           0 :                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
     382           0 :                         do {
     383             :                                 int err;
     384             : 
     385           0 :                                 cmd.opcode = MMC_SEND_STATUS;
     386           0 :                                 cmd.arg = card->rca << 16;
     387           0 :                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
     388           0 :                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
     389           0 :                                 if (err) {
     390           0 :                                         printk(KERN_ERR "%s: error %d requesting status\n",
     391             :                                                req->rq_disk->disk_name, err);
     392           0 :                                         goto cmd_err;
     393             :                                 }
     394             :                                 /*
     395             :                                  * Some cards mishandle the status bits,
     396             :                                  * so make sure to check both the busy
     397             :                                  * indication and the card state.
     398             :                                  */
     399             :                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
     400           0 :                                 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
     401             : 
     402           0 : #if 0
     403             :                         if (cmd.resp[0] & ~0x00000900)
     404             :                                 printk(KERN_ERR "%s: status = %08x\n",
     405             :                                        req->rq_disk->disk_name, cmd.resp[0]);
     406             :                         if (mmc_decode_status(cmd.resp))
     407             :                                 goto cmd_err;
     408             : #endif
     409             :                 }
     410             : 
     411           0 :                 if (brq.cmd.error || brq.stop.error || brq.data.error) {
     412           0 :                         if (rq_data_dir(req) == READ) {
     413             :                                 /*
     414             :                                  * After an error, we redo I/O one sector at a
     415             :                                  * time, so we only reach here after trying to
     416             :                                  * read a single sector.
     417             :                                  */
     418           0 :                                 spin_lock_irq(&md->lock);
     419           0 :                                 ret = __blk_end_request(req, -EIO, brq.data.blksz);
     420           0 :                                 spin_unlock_irq(&md->lock);
     421           0 :                                 continue;
     422             :                         }
     423           0 :                         goto cmd_err;
     424             :                 }
     425             : 
     426             :                 /*
     427             :                  * A block was successfully transferred.
     428             :                  */
     429           0 :                 spin_lock_irq(&md->lock);
     430           0 :                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
     431           0 :                 spin_unlock_irq(&md->lock);
     432           0 :         } while (ret);
     433           0 : 
     434           0 :         mmc_release_host(card->host);
     435             : 
     436           0 :         return 1;
     437           0 : 
     438             :  cmd_err:
     439             :         /*
     440             :          * If this is an SD card and we're writing, we can first
     441             :          * mark the known good sectors as ok.
     442             :          *
     443             :          * If the card is not SD, we can still ok written sectors
     444             :          * as reported by the controller (which might be less than
     445             :          * the real number of written sectors, but never more).
     446             :          */
     447           0 :         if (mmc_card_sd(card)) {
     448             :                 u32 blocks;
     449             : 
     450           0 :                 blocks = mmc_sd_num_wr_blocks(card);
     451           0 :                 if (blocks != (u32)-1) {
     452           0 :                         spin_lock_irq(&md->lock);
     453           0 :                         ret = __blk_end_request(req, 0, blocks << 9);
     454           0 :                         spin_unlock_irq(&md->lock);
     455             :                 }
     456             :         } else {
     457           0 :                 spin_lock_irq(&md->lock);
     458           0 :                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
     459           0 :                 spin_unlock_irq(&md->lock);
     460             :         }
     461             : 
     462           0 :         mmc_release_host(card->host);
     463             : 
     464           0 :         spin_lock_irq(&md->lock);
     465           0 :         while (ret)
     466           0 :                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
     467           0 :         spin_unlock_irq(&md->lock);
     468           0 : 
     469           0 :         return 0;
     470             : }
     471             : 
     472             : 
     473             : static inline int mmc_blk_readonly(struct mmc_card *card)
     474             : {
     475           6 :         return mmc_card_readonly(card) ||
     476             :                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
     477             : }
     478             : 
     479             : static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
     480             : {
     481           1 :         struct mmc_blk_data *md;
     482           1 :         int devidx, ret;
     483           1 : 
     484           3 :         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
     485           3 :         if (devidx >= MMC_NUM_MINORS)
     486           4 :                 return ERR_PTR(-ENOSPC);
     487           3 :         __set_bit(devidx, dev_use);
     488           1 : 
     489           4 :         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
     490           2 :         if (!md) {
     491           1 :                 ret = -ENOMEM;
     492           1 :                 goto out;
     493             :         }
     494             : 
     495             : 
     496             :         /*
     497             :          * Set the read-only status based on the supported commands
     498             :          * and the write protect switch.
     499             :          */
     500           3 :         md->read_only = mmc_blk_readonly(card);
     501             : 
     502           1 :         md->disk = alloc_disk(1 << MMC_SHIFT);
     503           3 :         if (md->disk == NULL) {
     504           1 :                 ret = -ENOMEM;
     505           1 :                 goto err_kfree;
     506             :         }
     507             : 
     508           4 :         spin_lock_init(&md->lock);
     509           1 :         md->usage = 1;
     510             : 
     511           5 :         ret = mmc_init_queue(&md->queue, card, &md->lock);
     512           2 :         if (ret)
     513           1 :                 goto err_putdisk;
     514             : 
     515           1 :         md->queue.issue_fn = mmc_blk_issue_rq;
     516           1 :         md->queue.data = md;
     517             : 
     518           1 :         md->disk->major   = MMC_BLOCK_MAJOR;
     519           1 :         md->disk->first_minor = devidx << MMC_SHIFT;
     520           1 :         md->disk->fops = &mmc_bdops;
     521           1 :         md->disk->private_data = md;
     522           1 :         md->disk->queue = md->queue.queue;
     523           1 :         md->disk->driverfs_dev = &card->dev;
     524             : 
     525             :         /*
     526             :          * As discussed on lkml, GENHD_FL_REMOVABLE should:
     527             :          *
     528             :          * - be set for removable media with permanent block devices
     529             :          * - be unset for removable block devices with permanent media
     530             :          *
     531             :          * Since MMC block devices clearly fall under the second
     532             :          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
     533             :          * should use the block device creation/destruction hotplug
     534             :          * messages to tell when the card is present.
     535             :          */
     536             : 
     537           1 :         sprintf(md->disk->disk_name, "mmcblk%d", devidx);
     538             : 
     539           1 :         blk_queue_logical_block_size(md->queue.queue, 512);
     540             : 
     541           4 :         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
     542             :                 /*
     543             :                  * The EXT_CSD sector count is in number or 512 byte
     544             :                  * sectors.
     545             :                  */
     546           2 :                 set_capacity(md->disk, card->ext_csd.sectors);
     547             :         } else {
     548             :                 /*
     549             :                  * The CSD capacity field is in units of read_blkbits.
     550             :                  * set_capacity takes units of 512 bytes.
     551             :                  */
     552           2 :                 set_capacity(md->disk,
     553             :                         card->csd.capacity << (card->csd.read_blkbits - 9));
     554             :         }
     555           2 :         return md;
     556           1 : 
     557             :  err_putdisk:
     558           1 :         put_disk(md->disk);
     559             :  err_kfree:
     560           3 :         kfree(md);
     561             :  out:
     562           9 :         return ERR_PTR(ret);
     563             : }
     564             : 
     565             : static int
     566             : mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
     567             : {
     568           1 :         struct mmc_command cmd;
     569           1 :         int err;
     570             : 
     571             :         /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
     572           2 :         if (mmc_card_blockaddr(card))
     573           1 :                 return 0;
     574             : 
     575           2 :         mmc_claim_host(card->host);
     576           1 :         cmd.opcode = MMC_SET_BLOCKLEN;
     577           1 :         cmd.arg = 512;
     578           1 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
     579           1 :         err = mmc_wait_for_cmd(card->host, &cmd, 5);
     580           1 :         mmc_release_host(card->host);
     581             : 
     582           2 :         if (err) {
     583           1 :                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
     584             :                         md->disk->disk_name, cmd.arg, err);
     585           1 :                 return -EINVAL;
     586             :         }
     587             : 
     588           1 :         return 0;
     589             : }
     590             : 
     591             : static int mmc_blk_probe(struct mmc_card *card)
     592             : {
     593           1 :         struct mmc_blk_data *md;
     594           1 :         int err;
     595           1 : 
     596           1 :         char cap_str[10];
     597           1 : 
     598           1 :         /*
     599           1 :          * Check that the card supports the command class(es) we need.
     600             :          */
     601           2 :         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
     602           1 :                 return -ENODEV;
     603             : 
     604           5 :         md = mmc_blk_alloc(card);
     605           4 :         if (IS_ERR(md))
     606           3 :                 return PTR_ERR(md);
     607             : 
     608           3 :         err = mmc_blk_set_blksize(md, card);
     609           2 :         if (err)
     610           1 :                 goto out;
     611             : 
     612           3 :         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
     613             :                         cap_str, sizeof(cap_str));
     614           4 :         printk(KERN_INFO "%s: %s %s %s %s\n",
     615           4 :                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
     616             :                 cap_str, md->read_only ? "(ro)" : "");
     617             : 
     618           1 :         mmc_set_drvdata(card, md);
     619           1 :         add_disk(md->disk);
     620           1 :         return 0;
     621           1 : 
     622             :  out:
     623           2 :         mmc_cleanup_queue(&md->queue);
     624           3 :         mmc_blk_put(md);
     625             : 
     626           1 :         return err;
     627             : }
     628             : 
     629             : static void mmc_blk_remove(struct mmc_card *card)
     630             : {
     631           4 :         struct mmc_blk_data *md = mmc_get_drvdata(card);
     632           1 : 
     633           2 :         if (md) {
     634             :                 /* Stop new requests from getting into the queue */
     635           1 :                 del_gendisk(md->disk);
     636             : 
     637             :                 /* Then flush out any already in there */
     638           2 :                 mmc_cleanup_queue(&md->queue);
     639             : 
     640           3 :                 mmc_blk_put(md);
     641             :         }
     642           2 :         mmc_set_drvdata(card, NULL);
     643           2 : }
     644             : 
     645             : #ifdef CONFIG_PM
     646             : static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
     647             : {
     648             :         struct mmc_blk_data *md = mmc_get_drvdata(card);
     649             : 
     650             :         if (md) {
     651             :                 mmc_queue_suspend(&md->queue);
     652             :         }
     653             :         return 0;
     654             : }
     655             : 
     656             : static int mmc_blk_resume(struct mmc_card *card)
     657             : {
     658             :         struct mmc_blk_data *md = mmc_get_drvdata(card);
     659             : 
     660             :         if (md) {
     661             :                 mmc_blk_set_blksize(md, card);
     662             :                 mmc_queue_resume(&md->queue);
     663             :         }
     664             :         return 0;
     665             : }
     666             : #else
     667             : #define mmc_blk_suspend NULL
     668             : #define mmc_blk_resume  NULL
     669             : #endif
     670             : 
     671           1 : static struct mmc_driver mmc_driver = {
     672             :         .drv            = {
     673             :                 .name   = "mmcblk",
     674             :         },
     675             :         .probe          = mmc_blk_probe,
     676             :         .remove         = mmc_blk_remove,
     677             :         .suspend        = mmc_blk_suspend,
     678             :         .resume         = mmc_blk_resume,
     679             : };
     680             : 
     681             : static int __init mmc_blk_init(void)
     682             : {
     683           1 :         int res;
     684             : 
     685           1 :         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
     686           2 :         if (res)
     687           1 :                 goto out;
     688             : 
     689           2 :         res = mmc_register_driver(&mmc_driver);
     690           2 :         if (res)
     691           1 :                 goto out2;
     692             : 
     693           1 :         return 0;
     694           1 :  out2:
     695           1 :         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
     696             :  out:
     697           3 :         return res;
     698             : }
     699             : 
     700             : static void __exit mmc_blk_exit(void)
     701             : {
     702           4 :         mmc_unregister_driver(&mmc_driver);
     703           2 :         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
     704           2 : }
     705             : 
     706             : module_init(mmc_blk_init);
     707             : module_exit(mmc_blk_exit);
     708           1 : 
     709             : MODULE_LICENSE("GPL");
     710             : MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
     711             : 

Generated by: LCOV version 1.10