block.c 15.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Block driver for media (i.e., flash cards)
 *
 * Copyright 2002 Hewlett-Packard Company
5
 * Copyright 2005-2008 Pierre Ossman
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>
26
#include <linux/slab.h>
L
Linus Torvalds 已提交
27 28 29 30
#include <linux/errno.h>
#include <linux/hdreg.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
31
#include <linux/mutex.h>
32
#include <linux/smp_lock.h>
33
#include <linux/scatterlist.h>
34
#include <linux/string_helpers.h>
L
Linus Torvalds 已提交
35 36

#include <linux/mmc/card.h>
P
Pierre Ossman 已提交
37
#include <linux/mmc/host.h>
P
Pierre Ossman 已提交
38 39
#include <linux/mmc/mmc.h>
#include <linux/mmc/sd.h>
L
Linus Torvalds 已提交
40 41 42 43

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

44
#include "queue.h"
L
Linus Torvalds 已提交
45

46 47
MODULE_ALIAS("mmc:block");

L
Linus Torvalds 已提交
48 49 50 51
/*
 * max 8 partitions per card
 */
#define MMC_SHIFT	3
52 53
#define MMC_NUM_MINORS	(256 >> MMC_SHIFT)

54
static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64

/*
 * 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;
65
	unsigned int	read_only;
L
Linus Torvalds 已提交
66 67
};

68
static DEFINE_MUTEX(open_lock);
L
Linus Torvalds 已提交
69 70 71 72 73

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

74
	mutex_lock(&open_lock);
L
Linus Torvalds 已提交
75 76 77 78 79
	md = disk->private_data;
	if (md && md->usage == 0)
		md = NULL;
	if (md)
		md->usage++;
80
	mutex_unlock(&open_lock);
L
Linus Torvalds 已提交
81 82 83 84 85 86

	return md;
}

static void mmc_blk_put(struct mmc_blk_data *md)
{
87
	mutex_lock(&open_lock);
L
Linus Torvalds 已提交
88 89
	md->usage--;
	if (md->usage == 0) {
90
		int devmaj = MAJOR(disk_devt(md->disk));
91
		int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
92 93 94 95

		if (!devmaj)
			devidx = md->disk->first_minor >> MMC_SHIFT;

A
Adrian Hunter 已提交
96 97
		blk_cleanup_queue(md->queue.queue);

98 99
		__clear_bit(devidx, dev_use);

L
Linus Torvalds 已提交
100 101 102
		put_disk(md->disk);
		kfree(md);
	}
103
	mutex_unlock(&open_lock);
L
Linus Torvalds 已提交
104 105
}

A
Al Viro 已提交
106
static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
L
Linus Torvalds 已提交
107
{
A
Al Viro 已提交
108
	struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
L
Linus Torvalds 已提交
109 110
	int ret = -ENXIO;

111
	lock_kernel();
L
Linus Torvalds 已提交
112 113
	if (md) {
		if (md->usage == 2)
A
Al Viro 已提交
114
			check_disk_change(bdev);
L
Linus Torvalds 已提交
115
		ret = 0;
P
Pierre Ossman 已提交
116

A
Al Viro 已提交
117
		if ((mode & FMODE_WRITE) && md->read_only) {
118
			mmc_blk_put(md);
P
Pierre Ossman 已提交
119
			ret = -EROFS;
120
		}
L
Linus Torvalds 已提交
121
	}
122
	unlock_kernel();
L
Linus Torvalds 已提交
123 124 125 126

	return ret;
}

A
Al Viro 已提交
127
static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
L
Linus Torvalds 已提交
128
{
A
Al Viro 已提交
129
	struct mmc_blk_data *md = disk->private_data;
L
Linus Torvalds 已提交
130

131
	lock_kernel();
L
Linus Torvalds 已提交
132
	mmc_blk_put(md);
133
	unlock_kernel();
L
Linus Torvalds 已提交
134 135 136 137
	return 0;
}

static int
138
mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
L
Linus Torvalds 已提交
139
{
140 141 142 143
	geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
	geo->heads = 4;
	geo->sectors = 16;
	return 0;
L
Linus Torvalds 已提交
144 145
}

146
static const struct block_device_operations mmc_bdops = {
A
Al Viro 已提交
147 148
	.open			= mmc_blk_open,
	.release		= mmc_blk_release,
149
	.getgeo			= mmc_blk_getgeo,
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159
	.owner			= THIS_MODULE,
};

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

160 161 162
static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
{
	int err;
B
Ben Dooks 已提交
163 164
	u32 result;
	__be32 *blocks;
165 166 167 168 169 170 171 172 173 174 175 176

	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;
D
David Brownell 已提交
177
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
178 179

	err = mmc_wait_for_cmd(card->host, &cmd, 0);
D
David Brownell 已提交
180 181 182
	if (err)
		return (u32)-1;
	if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
183 184 185 186 187 188
		return (u32)-1;

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

	cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
	cmd.arg = 0;
D
David Brownell 已提交
189
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	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;

B
Ben Dooks 已提交
216 217 218 219 220
	blocks = kmalloc(4, GFP_KERNEL);
	if (!blocks)
		return (u32)-1;

	sg_init_one(&sg, blocks, 4);
221 222 223

	mmc_wait_for_req(card->host, &mrq);

B
Ben Dooks 已提交
224 225 226
	result = ntohl(*blocks);
	kfree(blocks);

P
Pierre Ossman 已提交
227
	if (cmd.error || data.error)
B
Ben Dooks 已提交
228
		result = (u32)-1;
229

B
Ben Dooks 已提交
230
	return result;
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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];
}

L
Linus Torvalds 已提交
250 251 252 253
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;
254
	struct mmc_blk_request brq;
255
	int ret = 1, disable_multi = 0;
L
Linus Torvalds 已提交
256

P
Pierre Ossman 已提交
257
	mmc_claim_host(card->host);
L
Linus Torvalds 已提交
258 259 260

	do {
		struct mmc_command cmd;
261
		u32 readcmd, writecmd, status = 0;
L
Linus Torvalds 已提交
262 263 264 265 266

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

267
		brq.cmd.arg = blk_rq_pos(req);
268 269
		if (!mmc_card_blockaddr(card))
			brq.cmd.arg <<= 9;
D
David Brownell 已提交
270
		brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
271
		brq.data.blksz = 512;
L
Linus Torvalds 已提交
272 273
		brq.stop.opcode = MMC_STOP_TRANSMISSION;
		brq.stop.arg = 0;
D
David Brownell 已提交
274
		brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
275
		brq.data.blocks = blk_rq_sectors(req);
L
Linus Torvalds 已提交
276

277 278 279 280 281 282 283 284
		/*
		 * The block layer doesn't support all sector count
		 * restrictions, so we need to be prepared for too big
		 * requests.
		 */
		if (brq.data.blocks > card->host->max_blk_count)
			brq.data.blocks = card->host->max_blk_count;

285 286 287 288 289 290 291 292
		/*
		 * 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;

R
Russell King 已提交
293
		if (brq.data.blocks > 1) {
D
David Brownell 已提交
294 295 296 297 298 299
			/* 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;
300 301
			readcmd = MMC_READ_MULTIPLE_BLOCK;
			writecmd = MMC_WRITE_MULTIPLE_BLOCK;
R
Russell King 已提交
302 303
		} else {
			brq.mrq.stop = NULL;
304 305 306 307 308 309 310 311 312 313
			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;
R
Russell King 已提交
314
		}
L
Linus Torvalds 已提交
315

316 317
		mmc_set_data_timeout(&brq.data, card);

L
Linus Torvalds 已提交
318
		brq.data.sg = mq->sg;
319 320
		brq.data.sg_len = mmc_queue_map_sg(mq);

321 322 323 324
		/*
		 * Adjust the sg list so it is the same size as the
		 * request.
		 */
325
		if (brq.data.blocks != blk_rq_sectors(req)) {
326 327 328 329 330 331 332 333 334 335 336 337 338 339
			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;
		}

340
		mmc_queue_bounce_pre(mq);
L
Linus Torvalds 已提交
341 342

		mmc_wait_for_req(card->host, &brq.mrq);
343 344 345

		mmc_queue_bounce_post(mq);

346 347 348 349 350
		/*
		 * 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.
		 */
351 352 353 354 355 356 357 358
		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;
			}
359
			status = get_card_status(card, req);
360
		}
361

L
Linus Torvalds 已提交
362
		if (brq.cmd.error) {
363 364 365 366
			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);
L
Linus Torvalds 已提交
367 368 369
		}

		if (brq.data.error) {
370 371 372 373 374 375
			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,
376 377
			       (unsigned)blk_rq_pos(req),
			       (unsigned)blk_rq_sectors(req), status);
L
Linus Torvalds 已提交
378 379 380
		}

		if (brq.stop.error) {
381 382 383 384
			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);
L
Linus Torvalds 已提交
385 386
		}

D
David Brownell 已提交
387
		if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
388 389 390 391 392 393 394 395 396 397 398 399
			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;
				}
400 401 402 403 404 405 406
				/*
				 * 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));
L
Linus Torvalds 已提交
407 408

#if 0
409 410 411 412 413
			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;
L
Linus Torvalds 已提交
414
#endif
415
		}
L
Linus Torvalds 已提交
416

417 418 419 420 421 422 423 424 425 426 427 428
		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;
			}
429
			goto cmd_err;
430
		}
431

L
Linus Torvalds 已提交
432 433 434 435
		/*
		 * A block was successfully transferred.
		 */
		spin_lock_irq(&md->lock);
436
		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
L
Linus Torvalds 已提交
437 438 439
		spin_unlock_irq(&md->lock);
	} while (ret);

P
Pierre Ossman 已提交
440
	mmc_release_host(card->host);
L
Linus Torvalds 已提交
441 442 443 444

	return 1;

 cmd_err:
445 446 447 448 449
 	/*
 	 * 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
450 451
	 * as reported by the controller (which might be less than
	 * the real number of written sectors, but never more).
L
Linus Torvalds 已提交
452
	 */
453 454
	if (mmc_card_sd(card)) {
		u32 blocks;
455

456 457
		blocks = mmc_sd_num_wr_blocks(card);
		if (blocks != (u32)-1) {
458
			spin_lock_irq(&md->lock);
459
			ret = __blk_end_request(req, 0, blocks << 9);
460 461
			spin_unlock_irq(&md->lock);
		}
462 463 464 465
	} else {
		spin_lock_irq(&md->lock);
		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
		spin_unlock_irq(&md->lock);
466 467
	}

P
Pierre Ossman 已提交
468
	mmc_release_host(card->host);
469

L
Linus Torvalds 已提交
470
	spin_lock_irq(&md->lock);
471 472
	while (ret)
		ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
L
Linus Torvalds 已提交
473 474 475 476 477 478
	spin_unlock_irq(&md->lock);

	return 0;
}


479 480 481 482 483 484
static inline int mmc_blk_readonly(struct mmc_card *card)
{
	return mmc_card_readonly(card) ||
	       !(card->csd.cmdclass & CCC_BLOCK_WRITE);
}

L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492 493 494
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);

495
	md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
496 497 498 499
	if (!md) {
		ret = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
500 501


502 503 504 505 506
	/*
	 * Set the read-only status based on the supported commands
	 * and the write protect switch.
	 */
	md->read_only = mmc_blk_readonly(card);
L
Linus Torvalds 已提交
507

508 509 510 511 512
	md->disk = alloc_disk(1 << MMC_SHIFT);
	if (md->disk == NULL) {
		ret = -ENOMEM;
		goto err_kfree;
	}
L
Linus Torvalds 已提交
513

514 515
	spin_lock_init(&md->lock);
	md->usage = 1;
L
Linus Torvalds 已提交
516

517 518 519
	ret = mmc_init_queue(&md->queue, card, &md->lock);
	if (ret)
		goto err_putdisk;
L
Linus Torvalds 已提交
520

521 522
	md->queue.issue_fn = mmc_blk_issue_rq;
	md->queue.data = md;
523

524
	md->disk->major	= MMC_BLOCK_MAJOR;
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	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);

545
	blk_queue_logical_block_size(md->queue.queue, 512);
546

P
Pierre Ossman 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560
	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));
	}
L
Linus Torvalds 已提交
561
	return md;
562 563 564 565 566 567 568

 err_putdisk:
	put_disk(md->disk);
 err_kfree:
	kfree(md);
 out:
	return ERR_PTR(ret);
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576
}

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

577 578 579 580
	/* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
	if (mmc_card_blockaddr(card))
		return 0;

P
Pierre Ossman 已提交
581
	mmc_claim_host(card->host);
L
Linus Torvalds 已提交
582
	cmd.opcode = MMC_SET_BLOCKLEN;
583
	cmd.arg = 512;
D
David Brownell 已提交
584
	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
L
Linus Torvalds 已提交
585
	err = mmc_wait_for_cmd(card->host, &cmd, 5);
P
Pierre Ossman 已提交
586
	mmc_release_host(card->host);
L
Linus Torvalds 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

	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;

602 603
	char cap_str[10];

604 605 606 607
	/*
	 * Check that the card supports the command class(es) we need.
	 */
	if (!(card->csd.cmdclass & CCC_BLOCK_READ))
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617
		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;

618
	string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
619 620
			cap_str, sizeof(cap_str));
	printk(KERN_INFO "%s: %s %s %s %s\n",
L
Linus Torvalds 已提交
621
		md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
622
		cap_str, md->read_only ? "(ro)" : "");
L
Linus Torvalds 已提交
623 624 625 626 627 628

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

 out:
629
	mmc_cleanup_queue(&md->queue);
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639
	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) {
640
		/* Stop new requests from getting into the queue */
L
Linus Torvalds 已提交
641 642
		del_gendisk(md->disk);

643 644
		/* Then flush out any already in there */
		mmc_cleanup_queue(&md->queue);
L
Linus Torvalds 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

		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)
{
689
	int res;
L
Linus Torvalds 已提交
690

691 692
	res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
	if (res)
L
Linus Torvalds 已提交
693 694
		goto out;

695 696 697
	res = mmc_register_driver(&mmc_driver);
	if (res)
		goto out2;
L
Linus Torvalds 已提交
698

699 700 701
	return 0;
 out2:
	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
L
Linus Torvalds 已提交
702 703 704 705 706 707 708
 out:
	return res;
}

static void __exit mmc_blk_exit(void)
{
	mmc_unregister_driver(&mmc_driver);
709
	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717
}

module_init(mmc_blk_init);
module_exit(mmc_blk_exit);

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