block.c 15.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
 * Block driver for media (i.e., flash cards)
 *
 * Copyright 2002 Hewlett-Packard Company
5
 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Use consistent with the GNU GPL is permitted,
 * provided that this copyright notice is
 * preserved in its entirety in all copies and derived works.
 *
 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
 * FITNESS FOR ANY PARTICULAR PURPOSE.
 *
 * Many thanks to Alessandro Rubini and Jonathan Corbet!
 *
 * Author:  Andrew Christian
 *          28 May 2002
 */
#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
30
#include <linux/mutex.h>
31
#include <linux/scatterlist.h>
32
#include <linux/string_helpers.h>
Linus Torvalds's avatar
Linus Torvalds committed
33 34

#include <linux/mmc/card.h>
35
#include <linux/mmc/host.h>
Pierre Ossman's avatar
Pierre Ossman committed
36 37
#include <linux/mmc/mmc.h>
#include <linux/mmc/sd.h>
Linus Torvalds's avatar
Linus Torvalds committed
38 39 40 41

#include <asm/system.h>
#include <asm/uaccess.h>

42
#include "queue.h"
Linus Torvalds's avatar
Linus Torvalds committed
43

44 45
MODULE_ALIAS("mmc:block");

Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49
/*
 * max 8 partitions per card
 */
#define MMC_SHIFT	3
50 51
#define MMC_NUM_MINORS	(256 >> MMC_SHIFT)

52
static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56 57 58 59 60 61 62

/*
 * There is one mmc_blk_data per slot.
 */
struct mmc_blk_data {
	spinlock_t	lock;
	struct gendisk	*disk;
	struct mmc_queue queue;

	unsigned int	usage;
63
	unsigned int	read_only;
Linus Torvalds's avatar
Linus Torvalds committed
64 65
};

66
static DEFINE_MUTEX(open_lock);
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71

static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
{
	struct mmc_blk_data *md;

72
	mutex_lock(&open_lock);
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77
	md = disk->private_data;
	if (md && md->usage == 0)
		md = NULL;
	if (md)
		md->usage++;
78
	mutex_unlock(&open_lock);
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83 84

	return md;
}

static void mmc_blk_put(struct mmc_blk_data *md)
{
85
	mutex_lock(&open_lock);
Linus Torvalds's avatar
Linus Torvalds committed
86 87
	md->usage--;
	if (md->usage == 0) {
88
		int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
89 90
		__clear_bit(devidx, dev_use);

Linus Torvalds's avatar
Linus Torvalds committed
91 92 93
		put_disk(md->disk);
		kfree(md);
	}
94
	mutex_unlock(&open_lock);
Linus Torvalds's avatar
Linus Torvalds committed
95 96
}

Al Viro's avatar
Al Viro committed
97
static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
98
{
Al Viro's avatar
Al Viro committed
99
	struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102 103
	int ret = -ENXIO;

	if (md) {
		if (md->usage == 2)
Al Viro's avatar
Al Viro committed
104
			check_disk_change(bdev);
Linus Torvalds's avatar
Linus Torvalds committed
105
		ret = 0;
Pierre Ossman's avatar
Pierre Ossman committed
106

Al Viro's avatar
Al Viro committed
107
		if ((mode & FMODE_WRITE) && md->read_only) {
108
			mmc_blk_put(md);
Pierre Ossman's avatar
Pierre Ossman committed
109
			ret = -EROFS;
110
		}
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115
	}

	return ret;
}

Al Viro's avatar
Al Viro committed
116
static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
117
{
Al Viro's avatar
Al Viro committed
118
	struct mmc_blk_data *md = disk->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124

	mmc_blk_put(md);
	return 0;
}

static int
125
mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds's avatar
Linus Torvalds committed
126
{
127 128 129 130
	geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
	geo->heads = 4;
	geo->sectors = 16;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133
}

static struct block_device_operations mmc_bdops = {
Al Viro's avatar
Al Viro committed
134 135
	.open			= mmc_blk_open,
	.release		= mmc_blk_release,
136
	.getgeo			= mmc_blk_getgeo,
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145 146
	.owner			= THIS_MODULE,
};

struct mmc_blk_request {
	struct mmc_request	mrq;
	struct mmc_command	cmd;
	struct mmc_command	stop;
	struct mmc_data		data;
};

147 148 149
static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
{
	int err;
150
	__be32 blocks;
151 152 153 154 155 156 157 158 159 160 161 162

	struct mmc_request mrq;
	struct mmc_command cmd;
	struct mmc_data data;
	unsigned int timeout_us;

	struct scatterlist sg;

	memset(&cmd, 0, sizeof(struct mmc_command));

	cmd.opcode = MMC_APP_CMD;
	cmd.arg = card->rca << 16;
David Brownell's avatar
David Brownell committed
163
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
164 165

	err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell's avatar
David Brownell committed
166 167 168
	if (err)
		return (u32)-1;
	if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
169 170 171 172 173 174
		return (u32)-1;

	memset(&cmd, 0, sizeof(struct mmc_command));

	cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
	cmd.arg = 0;
David Brownell's avatar
David Brownell committed
175
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	memset(&data, 0, sizeof(struct mmc_data));

	data.timeout_ns = card->csd.tacc_ns * 100;
	data.timeout_clks = card->csd.tacc_clks * 100;

	timeout_us = data.timeout_ns / 1000;
	timeout_us += data.timeout_clks * 1000 /
		(card->host->ios.clock / 1000);

	if (timeout_us > 100000) {
		data.timeout_ns = 100000000;
		data.timeout_clks = 0;
	}

	data.blksz = 4;
	data.blocks = 1;
	data.flags = MMC_DATA_READ;
	data.sg = &sg;
	data.sg_len = 1;

	memset(&mrq, 0, sizeof(struct mmc_request));

	mrq.cmd = &cmd;
	mrq.data = &data;

	sg_init_one(&sg, &blocks, 4);

	mmc_wait_for_req(card->host, &mrq);

Pierre Ossman's avatar
Pierre Ossman committed
206
	if (cmd.error || data.error)
207 208
		return (u32)-1;

209
	return ntohl(blocks);
210 211
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static u32 get_card_status(struct mmc_card *card, struct request *req)
{
	struct mmc_command cmd;
	int err;

	memset(&cmd, 0, sizeof(struct mmc_command));
	cmd.opcode = MMC_SEND_STATUS;
	if (!mmc_host_is_spi(card->host))
		cmd.arg = card->rca << 16;
	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
	err = mmc_wait_for_cmd(card->host, &cmd, 0);
	if (err)
		printk(KERN_ERR "%s: error %d sending status comand",
		       req->rq_disk->disk_name, err);
	return cmd.resp[0];
}

Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232
static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
{
	struct mmc_blk_data *md = mq->data;
	struct mmc_card *card = md->queue.card;
233
	struct mmc_blk_request brq;
234
	int ret = 1, disable_multi = 0;
Linus Torvalds's avatar
Linus Torvalds committed
235

236
	mmc_claim_host(card->host);
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239

	do {
		struct mmc_command cmd;
240
		u32 readcmd, writecmd, status = 0;
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244 245

		memset(&brq, 0, sizeof(struct mmc_blk_request));
		brq.mrq.cmd = &brq.cmd;
		brq.mrq.data = &brq.data;

246 247 248
		brq.cmd.arg = req->sector;
		if (!mmc_card_blockaddr(card))
			brq.cmd.arg <<= 9;
David Brownell's avatar
David Brownell committed
249
		brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
250
		brq.data.blksz = 512;
Linus Torvalds's avatar
Linus Torvalds committed
251 252
		brq.stop.opcode = MMC_STOP_TRANSMISSION;
		brq.stop.arg = 0;
David Brownell's avatar
David Brownell committed
253
		brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
254
		brq.data.blocks = req->nr_sectors;
Linus Torvalds's avatar
Linus Torvalds committed
255

256 257 258 259 260 261 262 263
		/*
		 * After a read error, we redo the request one sector at a time
		 * in order to accurately determine which sectors can be read
		 * successfully.
		 */
		if (disable_multi && brq.data.blocks > 1)
			brq.data.blocks = 1;

Russell King's avatar
Russell King committed
264
		if (brq.data.blocks > 1) {
David Brownell's avatar
David Brownell committed
265 266 267 268 269 270
			/* SPI multiblock writes terminate using a special
			 * token, not a STOP_TRANSMISSION request.
			 */
			if (!mmc_host_is_spi(card->host)
					|| rq_data_dir(req) == READ)
				brq.mrq.stop = &brq.stop;
271 272
			readcmd = MMC_READ_MULTIPLE_BLOCK;
			writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King's avatar
Russell King committed
273 274
		} else {
			brq.mrq.stop = NULL;
275 276 277 278 279 280 281 282 283 284
			readcmd = MMC_READ_SINGLE_BLOCK;
			writecmd = MMC_WRITE_BLOCK;
		}

		if (rq_data_dir(req) == READ) {
			brq.cmd.opcode = readcmd;
			brq.data.flags |= MMC_DATA_READ;
		} else {
			brq.cmd.opcode = writecmd;
			brq.data.flags |= MMC_DATA_WRITE;
Russell King's avatar
Russell King committed
285
		}
Linus Torvalds's avatar
Linus Torvalds committed
286

287 288
		mmc_set_data_timeout(&brq.data, card);

Linus Torvalds's avatar
Linus Torvalds committed
289
		brq.data.sg = mq->sg;
290 291
		brq.data.sg_len = mmc_queue_map_sg(mq);

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		/*
		 * Adjust the sg list so it is the same size as the
		 * request.
		 */
		if (brq.data.blocks != req->nr_sectors) {
			int i, data_size = brq.data.blocks << 9;
			struct scatterlist *sg;

			for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
				data_size -= sg->length;
				if (data_size <= 0) {
					sg->length += data_size;
					i++;
					break;
				}
			}
			brq.data.sg_len = i;
		}

311
		mmc_queue_bounce_pre(mq);
Linus Torvalds's avatar
Linus Torvalds committed
312 313

		mmc_wait_for_req(card->host, &brq.mrq);
314 315 316

		mmc_queue_bounce_post(mq);

317 318 319 320 321
		/*
		 * Check for errors here, but don't jump to cmd_err
		 * until later as we need to wait for the card to leave
		 * programming mode even when things go wrong.
		 */
322 323 324 325 326 327 328 329
		if (brq.cmd.error || brq.data.error || brq.stop.error) {
			if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
				/* Redo read one sector at a time */
				printk(KERN_WARNING "%s: retrying using single "
				       "block read\n", req->rq_disk->disk_name);
				disable_multi = 1;
				continue;
			}
330
			status = get_card_status(card, req);
331
		}
332

Linus Torvalds's avatar
Linus Torvalds committed
333
		if (brq.cmd.error) {
334 335 336 337
			printk(KERN_ERR "%s: error %d sending read/write "
			       "command, response %#x, card status %#x\n",
			       req->rq_disk->disk_name, brq.cmd.error,
			       brq.cmd.resp[0], status);
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340
		}

		if (brq.data.error) {
341 342 343 344 345 346 347 348
			if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
				/* 'Stop' response contains card status */
				status = brq.mrq.stop->resp[0];
			printk(KERN_ERR "%s: error %d transferring data,"
			       " sector %u, nr %u, card status %#x\n",
			       req->rq_disk->disk_name, brq.data.error,
			       (unsigned)req->sector,
			       (unsigned)req->nr_sectors, status);
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351
		}

		if (brq.stop.error) {
352 353 354 355
			printk(KERN_ERR "%s: error %d sending stop command, "
			       "response %#x, card status %#x\n",
			       req->rq_disk->disk_name, brq.stop.error,
			       brq.stop.resp[0], status);
Linus Torvalds's avatar
Linus Torvalds committed
356 357
		}

David Brownell's avatar
David Brownell committed
358
		if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
359 360 361 362 363 364 365 366 367 368 369 370
			do {
				int err;

				cmd.opcode = MMC_SEND_STATUS;
				cmd.arg = card->rca << 16;
				cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
				err = mmc_wait_for_cmd(card->host, &cmd, 5);
				if (err) {
					printk(KERN_ERR "%s: error %d requesting status\n",
					       req->rq_disk->disk_name, err);
					goto cmd_err;
				}
371 372 373 374 375 376 377
				/*
				 * Some cards mishandle the status bits,
				 * so make sure to check both the busy
				 * indication and the card state.
				 */
			} while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
				(R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds's avatar
Linus Torvalds committed
378 379

#if 0
380 381 382 383 384
			if (cmd.resp[0] & ~0x00000900)
				printk(KERN_ERR "%s: status = %08x\n",
				       req->rq_disk->disk_name, cmd.resp[0]);
			if (mmc_decode_status(cmd.resp))
				goto cmd_err;
Linus Torvalds's avatar
Linus Torvalds committed
385
#endif
386
		}
Linus Torvalds's avatar
Linus Torvalds committed
387

388 389 390 391 392 393 394 395 396 397 398 399
		if (brq.cmd.error || brq.stop.error || brq.data.error) {
			if (rq_data_dir(req) == READ) {
				/*
				 * After an error, we redo I/O one sector at a
				 * time, so we only reach here after trying to
				 * read a single sector.
				 */
				spin_lock_irq(&md->lock);
				ret = __blk_end_request(req, -EIO, brq.data.blksz);
				spin_unlock_irq(&md->lock);
				continue;
			}
400
			goto cmd_err;
401
		}
402

Linus Torvalds's avatar
Linus Torvalds committed
403 404 405 406
		/*
		 * A block was successfully transferred.
		 */
		spin_lock_irq(&md->lock);
407
		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410
		spin_unlock_irq(&md->lock);
	} while (ret);

411
	mmc_release_host(card->host);
Linus Torvalds's avatar
Linus Torvalds committed
412 413 414 415

	return 1;

 cmd_err:
416 417 418 419 420
 	/*
 	 * If this is an SD card and we're writing, we can first
 	 * mark the known good sectors as ok.
 	 *
	 * If the card is not SD, we can still ok written sectors
421 422
	 * as reported by the controller (which might be less than
	 * the real number of written sectors, but never more).
Linus Torvalds's avatar
Linus Torvalds committed
423
	 */
424 425
	if (mmc_card_sd(card)) {
		u32 blocks;
426

427 428
		blocks = mmc_sd_num_wr_blocks(card);
		if (blocks != (u32)-1) {
429
			spin_lock_irq(&md->lock);
430
			ret = __blk_end_request(req, 0, blocks << 9);
431 432
			spin_unlock_irq(&md->lock);
		}
433 434 435 436
	} else {
		spin_lock_irq(&md->lock);
		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
		spin_unlock_irq(&md->lock);
437 438
	}

439
	mmc_release_host(card->host);
440

Linus Torvalds's avatar
Linus Torvalds committed
441
	spin_lock_irq(&md->lock);
442 443
	while (ret)
		ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds's avatar
Linus Torvalds committed
444 445 446 447 448 449
	spin_unlock_irq(&md->lock);

	return 0;
}


450 451 452 453 454 455
static inline int mmc_blk_readonly(struct mmc_card *card)
{
	return mmc_card_readonly(card) ||
	       !(card->csd.cmdclass & CCC_BLOCK_WRITE);
}

Linus Torvalds's avatar
Linus Torvalds committed
456 457 458 459 460 461 462 463 464 465
static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
{
	struct mmc_blk_data *md;
	int devidx, ret;

	devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
	if (devidx >= MMC_NUM_MINORS)
		return ERR_PTR(-ENOSPC);
	__set_bit(devidx, dev_use);

466
	md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
467 468 469 470
	if (!md) {
		ret = -ENOMEM;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
471 472


473 474 475 476 477
	/*
	 * Set the read-only status based on the supported commands
	 * and the write protect switch.
	 */
	md->read_only = mmc_blk_readonly(card);
Linus Torvalds's avatar
Linus Torvalds committed
478

479 480 481 482 483
	md->disk = alloc_disk(1 << MMC_SHIFT);
	if (md->disk == NULL) {
		ret = -ENOMEM;
		goto err_kfree;
	}
Linus Torvalds's avatar
Linus Torvalds committed
484

485 486
	spin_lock_init(&md->lock);
	md->usage = 1;
Linus Torvalds's avatar
Linus Torvalds committed
487

488 489 490
	ret = mmc_init_queue(&md->queue, card, &md->lock);
	if (ret)
		goto err_putdisk;
Linus Torvalds's avatar
Linus Torvalds committed
491

492 493
	md->queue.issue_fn = mmc_blk_issue_rq;
	md->queue.data = md;
494

495
	md->disk->major	= MMC_BLOCK_MAJOR;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	md->disk->first_minor = devidx << MMC_SHIFT;
	md->disk->fops = &mmc_bdops;
	md->disk->private_data = md;
	md->disk->queue = md->queue.queue;
	md->disk->driverfs_dev = &card->dev;

	/*
	 * As discussed on lkml, GENHD_FL_REMOVABLE should:
	 *
	 * - be set for removable media with permanent block devices
	 * - be unset for removable block devices with permanent media
	 *
	 * Since MMC block devices clearly fall under the second
	 * case, we do not set GENHD_FL_REMOVABLE.  Userspace
	 * should use the block device creation/destruction hotplug
	 * messages to tell when the card is present.
	 */

	sprintf(md->disk->disk_name, "mmcblk%d", devidx);

516
	blk_queue_hardsect_size(md->queue.queue, 512);
517

Pierre Ossman's avatar
Pierre Ossman committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531
	if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
		/*
		 * The EXT_CSD sector count is in number or 512 byte
		 * sectors.
		 */
		set_capacity(md->disk, card->ext_csd.sectors);
	} else {
		/*
		 * The CSD capacity field is in units of read_blkbits.
		 * set_capacity takes units of 512 bytes.
		 */
		set_capacity(md->disk,
			card->csd.capacity << (card->csd.read_blkbits - 9));
	}
Linus Torvalds's avatar
Linus Torvalds committed
532
	return md;
533 534 535 536 537 538 539

 err_putdisk:
	put_disk(md->disk);
 err_kfree:
	kfree(md);
 out:
	return ERR_PTR(ret);
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547
}

static int
mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
{
	struct mmc_command cmd;
	int err;

548 549 550 551
	/* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
	if (mmc_card_blockaddr(card))
		return 0;

552
	mmc_claim_host(card->host);
Linus Torvalds's avatar
Linus Torvalds committed
553
	cmd.opcode = MMC_SET_BLOCKLEN;
554
	cmd.arg = 512;
David Brownell's avatar
David Brownell committed
555
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds's avatar
Linus Torvalds committed
556
	err = mmc_wait_for_cmd(card->host, &cmd, 5);
557
	mmc_release_host(card->host);
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

	if (err) {
		printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
			md->disk->disk_name, cmd.arg, err);
		return -EINVAL;
	}

	return 0;
}

static int mmc_blk_probe(struct mmc_card *card)
{
	struct mmc_blk_data *md;
	int err;

573 574
	char cap_str[10];

575 576 577 578
	/*
	 * Check that the card supports the command class(es) we need.
	 */
	if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581 582 583 584 585 586 587 588
		return -ENODEV;

	md = mmc_blk_alloc(card);
	if (IS_ERR(md))
		return PTR_ERR(md);

	err = mmc_blk_set_blksize(md, card);
	if (err)
		goto out;

589
	string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
590 591
			cap_str, sizeof(cap_str));
	printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
592
		md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
593
		cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

	mmc_set_drvdata(card, md);
	add_disk(md->disk);
	return 0;

 out:
	mmc_blk_put(md);

	return err;
}

static void mmc_blk_remove(struct mmc_card *card)
{
	struct mmc_blk_data *md = mmc_get_drvdata(card);

	if (md) {
610
		/* Stop new requests from getting into the queue */
Linus Torvalds's avatar
Linus Torvalds committed
611 612
		del_gendisk(md->disk);

613 614
		/* Then flush out any already in there */
		mmc_cleanup_queue(&md->queue);
Linus Torvalds's avatar
Linus Torvalds committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

		mmc_blk_put(md);
	}
	mmc_set_drvdata(card, NULL);
}

#ifdef CONFIG_PM
static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
{
	struct mmc_blk_data *md = mmc_get_drvdata(card);

	if (md) {
		mmc_queue_suspend(&md->queue);
	}
	return 0;
}

static int mmc_blk_resume(struct mmc_card *card)
{
	struct mmc_blk_data *md = mmc_get_drvdata(card);

	if (md) {
		mmc_blk_set_blksize(md, card);
		mmc_queue_resume(&md->queue);
	}
	return 0;
}
#else
#define	mmc_blk_suspend	NULL
#define mmc_blk_resume	NULL
#endif

static struct mmc_driver mmc_driver = {
	.drv		= {
		.name	= "mmcblk",
	},
	.probe		= mmc_blk_probe,
	.remove		= mmc_blk_remove,
	.suspend	= mmc_blk_suspend,
	.resume		= mmc_blk_resume,
};

static int __init mmc_blk_init(void)
{
659
	int res;
Linus Torvalds's avatar
Linus Torvalds committed
660

661 662
	res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
	if (res)
Linus Torvalds's avatar
Linus Torvalds committed
663 664
		goto out;

665 666 667
	res = mmc_register_driver(&mmc_driver);
	if (res)
		goto out2;
Linus Torvalds's avatar
Linus Torvalds committed
668

669 670 671
	return 0;
 out2:
	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds's avatar
Linus Torvalds committed
672 673 674 675 676 677 678
 out:
	return res;
}

static void __exit mmc_blk_exit(void)
{
	mmc_unregister_driver(&mmc_driver);
679
	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682 683 684 685 686 687
}

module_init(mmc_blk_init);
module_exit(mmc_blk_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");