mmc_test.c 18 KB
Newer Older
Pierre Ossman's avatar
Pierre Ossman committed
1 2 3
/*
 *  linux/drivers/mmc/card/mmc_test.c
 *
4
 *  Copyright 2007-2008 Pierre Ossman
Pierre Ossman's avatar
Pierre Ossman committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 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 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 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 */

#include <linux/mmc/core.h>
#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
#include <linux/mmc/mmc.h>

#include <linux/scatterlist.h>

#define RESULT_OK		0
#define RESULT_FAIL		1
#define RESULT_UNSUP_HOST	2
#define RESULT_UNSUP_CARD	3

#define BUFFER_SIZE	(PAGE_SIZE * 4)

struct mmc_test_card {
	struct mmc_card	*card;

	u8		*buffer;
};

/*******************************************************************/
/*  Helper functions                                               */
/*******************************************************************/

static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
{
	struct mmc_command cmd;
	int ret;

	cmd.opcode = MMC_SET_BLOCKLEN;
	cmd.arg = size;
	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
	ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
	if (ret)
		return ret;

	return 0;
}

static int __mmc_test_transfer(struct mmc_test_card *test, int write,
	unsigned broken_xfer, u8 *buffer, unsigned addr,
	unsigned blocks, unsigned blksz)
{
	int ret, busy;

	struct mmc_request mrq;
	struct mmc_command cmd;
	struct mmc_command stop;
	struct mmc_data data;

	struct scatterlist sg;

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

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

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

	if (broken_xfer) {
		if (blocks > 1) {
			cmd.opcode = write ?
				MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
		} else {
			cmd.opcode = MMC_SEND_STATUS;
		}
	} else {
		if (blocks > 1) {
			cmd.opcode = write ?
				MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
		} else {
			cmd.opcode = write ?
				MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
		}
	}

	if (broken_xfer && blocks == 1)
		cmd.arg = test->card->rca << 16;
	else
		cmd.arg = addr;
	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;

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

	if (!broken_xfer && (blocks > 1)) {
		stop.opcode = MMC_STOP_TRANSMISSION;
		stop.arg = 0;
		stop.flags = MMC_RSP_R1B | MMC_CMD_AC;

		mrq.stop = &stop;
	}

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

	data.blksz = blksz;
	data.blocks = blocks;
	data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
	data.sg = &sg;
	data.sg_len = 1;

	sg_init_one(&sg, buffer, blocks * blksz);

	mmc_set_data_timeout(&data, test->card);

	mmc_wait_for_req(test->card->host, &mrq);

	ret = 0;

	if (broken_xfer) {
		if (!ret && cmd.error)
			ret = cmd.error;
		if (!ret && data.error == 0)
			ret = RESULT_FAIL;
		if (!ret && data.error != -ETIMEDOUT)
			ret = data.error;
		if (!ret && stop.error)
			ret = stop.error;
		if (blocks > 1) {
			if (!ret && data.bytes_xfered > blksz)
				ret = RESULT_FAIL;
		} else {
			if (!ret && data.bytes_xfered > 0)
				ret = RESULT_FAIL;
		}
	} else {
		if (!ret && cmd.error)
			ret = cmd.error;
		if (!ret && data.error)
			ret = data.error;
		if (!ret && stop.error)
			ret = stop.error;
		if (!ret && data.bytes_xfered != blocks * blksz)
			ret = RESULT_FAIL;
	}

	if (ret == -EINVAL)
		ret = RESULT_UNSUP_HOST;

	busy = 0;
	do {
		int ret2;

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

		cmd.opcode = MMC_SEND_STATUS;
		cmd.arg = test->card->rca << 16;
		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;

		ret2 = mmc_wait_for_cmd(test->card->host, &cmd, 0);
		if (ret2)
			break;

		if (!busy && !(cmd.resp[0] & R1_READY_FOR_DATA)) {
			busy = 1;
			printk(KERN_INFO "%s: Warning: Host did not "
				"wait for busy state to end.\n",
				mmc_hostname(test->card->host));
		}
	} while (!(cmd.resp[0] & R1_READY_FOR_DATA));

	return ret;
}

static int mmc_test_transfer(struct mmc_test_card *test, int write,
	u8 *buffer, unsigned addr, unsigned blocks, unsigned blksz)
{
	return __mmc_test_transfer(test, write, 0, buffer,
			addr, blocks, blksz);
}

static int mmc_test_prepare_verify(struct mmc_test_card *test, int write)
{
	int ret, i;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	if (write)
		memset(test->buffer, 0xDF, BUFFER_SIZE);
	else {
		for (i = 0;i < BUFFER_SIZE;i++)
			test->buffer[i] = i;
	}

	for (i = 0;i < BUFFER_SIZE / 512;i++) {
		ret = mmc_test_transfer(test, 1, test->buffer + i * 512,
			i * 512, 1, 512);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_prepare_verify_write(struct mmc_test_card *test)
{
	return mmc_test_prepare_verify(test, 1);
}

static int mmc_test_prepare_verify_read(struct mmc_test_card *test)
{
	return mmc_test_prepare_verify(test, 0);
}

static int mmc_test_verified_transfer(struct mmc_test_card *test, int write,
	u8 *buffer, unsigned addr, unsigned blocks, unsigned blksz)
{
	int ret, i, sectors;

	/*
	 * It is assumed that the above preparation has been done.
	 */

	memset(test->buffer, 0, BUFFER_SIZE);

	if (write) {
		for (i = 0;i < blocks * blksz;i++)
			buffer[i] = i;
	}

	ret = mmc_test_set_blksize(test, blksz);
	if (ret)
		return ret;

	ret = mmc_test_transfer(test, write, buffer, addr, blocks, blksz);
	if (ret)
		return ret;

	if (write) {
		ret = mmc_test_set_blksize(test, 512);
		if (ret)
			return ret;

		sectors = (blocks * blksz + 511) / 512;
		if ((sectors * 512) == (blocks * blksz))
			sectors++;

		if ((sectors * 512) > BUFFER_SIZE)
			return -EINVAL;

		memset(test->buffer, 0, sectors * 512);

		for (i = 0;i < sectors;i++) {
			ret = mmc_test_transfer(test, 0,
				test->buffer + i * 512,
				addr + i * 512, 1, 512);
			if (ret)
				return ret;
		}

		for (i = 0;i < blocks * blksz;i++) {
			if (test->buffer[i] != (u8)i)
				return RESULT_FAIL;
		}

		for (;i < sectors * 512;i++) {
			if (test->buffer[i] != 0xDF)
				return RESULT_FAIL;
		}
	} else {
		for (i = 0;i < blocks * blksz;i++) {
			if (buffer[i] != (u8)i)
				return RESULT_FAIL;
		}
	}

	return 0;
}

static int mmc_test_cleanup_verify(struct mmc_test_card *test)
{
	int ret, i;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	memset(test->buffer, 0, BUFFER_SIZE);

	for (i = 0;i < BUFFER_SIZE / 512;i++) {
		ret = mmc_test_transfer(test, 1, test->buffer + i * 512,
			i * 512, 1, 512);
		if (ret)
			return ret;
	}

	return 0;
}

/*******************************************************************/
/*  Tests                                                          */
/*******************************************************************/

struct mmc_test_case {
	const char *name;

	int (*prepare)(struct mmc_test_card *);
	int (*run)(struct mmc_test_card *);
	int (*cleanup)(struct mmc_test_card *);
};

static int mmc_test_basic_write(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = mmc_test_transfer(test, 1, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_basic_read(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = mmc_test_transfer(test, 0, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_verify_write(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_verified_transfer(test, 1, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_verify_read(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_verified_transfer(test, 0, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_multi_write(struct mmc_test_card *test)
{
	int ret;
	unsigned int size;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	size = PAGE_SIZE * 2;
	size = min(size, test->card->host->max_req_size);
	size = min(size, test->card->host->max_seg_size);
	size = min(size, test->card->host->max_blk_count * 512);

	if (size < 1024)
		return RESULT_UNSUP_HOST;

	ret = mmc_test_verified_transfer(test, 1, test->buffer, 0,
		size / 512, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_multi_read(struct mmc_test_card *test)
{
	int ret;
	unsigned int size;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	size = PAGE_SIZE * 2;
	size = min(size, test->card->host->max_req_size);
	size = min(size, test->card->host->max_seg_size);
	size = min(size, test->card->host->max_blk_count * 512);

	if (size < 1024)
		return RESULT_UNSUP_HOST;

	ret = mmc_test_verified_transfer(test, 0, test->buffer, 0,
		size / 512, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_pow2_write(struct mmc_test_card *test)
{
	int ret, i;

	if (!test->card->csd.write_partial)
		return RESULT_UNSUP_CARD;

	for (i = 1; i < 512;i <<= 1) {
		ret = mmc_test_verified_transfer(test, 1,
			test->buffer, 0, 1, i);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_pow2_read(struct mmc_test_card *test)
{
	int ret, i;

	if (!test->card->csd.read_partial)
		return RESULT_UNSUP_CARD;

	for (i = 1; i < 512;i <<= 1) {
		ret = mmc_test_verified_transfer(test, 0,
			test->buffer, 0, 1, i);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_weird_write(struct mmc_test_card *test)
{
	int ret, i;

	if (!test->card->csd.write_partial)
		return RESULT_UNSUP_CARD;

	for (i = 3; i < 512;i += 7) {
		ret = mmc_test_verified_transfer(test, 1,
			test->buffer, 0, 1, i);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_weird_read(struct mmc_test_card *test)
{
	int ret, i;

	if (!test->card->csd.read_partial)
		return RESULT_UNSUP_CARD;

	for (i = 3; i < 512;i += 7) {
		ret = mmc_test_verified_transfer(test, 0,
			test->buffer, 0, 1, i);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_align_write(struct mmc_test_card *test)
{
	int ret, i;

	for (i = 1;i < 4;i++) {
		ret = mmc_test_verified_transfer(test, 1, test->buffer + i,
			0, 1, 512);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_align_read(struct mmc_test_card *test)
{
	int ret, i;

	for (i = 1;i < 4;i++) {
		ret = mmc_test_verified_transfer(test, 0, test->buffer + i,
			0, 1, 512);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_align_multi_write(struct mmc_test_card *test)
{
	int ret, i;
	unsigned int size;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	size = PAGE_SIZE * 2;
	size = min(size, test->card->host->max_req_size);
	size = min(size, test->card->host->max_seg_size);
	size = min(size, test->card->host->max_blk_count * 512);

	if (size < 1024)
		return RESULT_UNSUP_HOST;

	for (i = 1;i < 4;i++) {
		ret = mmc_test_verified_transfer(test, 1, test->buffer + i,
			0, size / 512, 512);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_align_multi_read(struct mmc_test_card *test)
{
	int ret, i;
	unsigned int size;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	size = PAGE_SIZE * 2;
	size = min(size, test->card->host->max_req_size);
	size = min(size, test->card->host->max_seg_size);
	size = min(size, test->card->host->max_blk_count * 512);

	if (size < 1024)
		return RESULT_UNSUP_HOST;

	for (i = 1;i < 4;i++) {
		ret = mmc_test_verified_transfer(test, 0, test->buffer + i,
			0, size / 512, 512);
		if (ret)
			return ret;
	}

	return 0;
}

static int mmc_test_xfersize_write(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = __mmc_test_transfer(test, 1, 1, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_xfersize_read(struct mmc_test_card *test)
{
	int ret;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = __mmc_test_transfer(test, 0, 1, test->buffer, 0, 1, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
{
	int ret;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = __mmc_test_transfer(test, 1, 1, test->buffer, 0, 2, 512);
	if (ret)
		return ret;

	return 0;
}

static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
{
	int ret;

	if (test->card->host->max_blk_count == 1)
		return RESULT_UNSUP_HOST;

	ret = mmc_test_set_blksize(test, 512);
	if (ret)
		return ret;

	ret = __mmc_test_transfer(test, 0, 1, test->buffer, 0, 2, 512);
	if (ret)
		return ret;

	return 0;
}

static const struct mmc_test_case mmc_test_cases[] = {
	{
		.name = "Basic write (no data verification)",
		.run = mmc_test_basic_write,
	},

	{
		.name = "Basic read (no data verification)",
		.run = mmc_test_basic_read,
	},

	{
		.name = "Basic write (with data verification)",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_verify_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Basic read (with data verification)",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_verify_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Multi-block write",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_multi_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Multi-block read",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_multi_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Power of two block writes",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_pow2_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Power of two block reads",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_pow2_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Weird sized block writes",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_weird_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Weird sized block reads",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_weird_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Badly aligned write",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_align_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Badly aligned read",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_align_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Badly aligned multi-block write",
		.prepare = mmc_test_prepare_verify_write,
		.run = mmc_test_align_multi_write,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Badly aligned multi-block read",
		.prepare = mmc_test_prepare_verify_read,
		.run = mmc_test_align_multi_read,
		.cleanup = mmc_test_cleanup_verify,
	},

	{
		.name = "Correct xfer_size at write (start failure)",
		.run = mmc_test_xfersize_write,
	},

	{
		.name = "Correct xfer_size at read (start failure)",
		.run = mmc_test_xfersize_read,
	},

	{
		.name = "Correct xfer_size at write (midway failure)",
		.run = mmc_test_multi_xfersize_write,
	},

	{
		.name = "Correct xfer_size at read (midway failure)",
		.run = mmc_test_multi_xfersize_read,
	},
};

static struct mutex mmc_test_lock;

746
static void mmc_test_run(struct mmc_test_card *test, int testcase)
Pierre Ossman's avatar
Pierre Ossman committed
747 748 749 750 751 752 753 754 755
{
	int i, ret;

	printk(KERN_INFO "%s: Starting tests of card %s...\n",
		mmc_hostname(test->card->host), mmc_card_id(test->card));

	mmc_claim_host(test->card->host);

	for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
756 757 758
		if (testcase && ((i + 1) != testcase))
			continue;

Pierre Ossman's avatar
Pierre Ossman committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
		printk(KERN_INFO "%s: Test case %d. %s...\n",
			mmc_hostname(test->card->host), i + 1,
			mmc_test_cases[i].name);

		if (mmc_test_cases[i].prepare) {
			ret = mmc_test_cases[i].prepare(test);
			if (ret) {
				printk(KERN_INFO "%s: Result: Prepare "
					"stage failed! (%d)\n",
					mmc_hostname(test->card->host),
					ret);
				continue;
			}
		}

		ret = mmc_test_cases[i].run(test);
		switch (ret) {
		case RESULT_OK:
			printk(KERN_INFO "%s: Result: OK\n",
				mmc_hostname(test->card->host));
			break;
		case RESULT_FAIL:
			printk(KERN_INFO "%s: Result: FAILED\n",
				mmc_hostname(test->card->host));
			break;
		case RESULT_UNSUP_HOST:
			printk(KERN_INFO "%s: Result: UNSUPPORTED "
				"(by host)\n",
				mmc_hostname(test->card->host));
			break;
		case RESULT_UNSUP_CARD:
			printk(KERN_INFO "%s: Result: UNSUPPORTED "
				"(by card)\n",
				mmc_hostname(test->card->host));
			break;
		default:
			printk(KERN_INFO "%s: Result: ERROR (%d)\n",
				mmc_hostname(test->card->host), ret);
		}

		if (mmc_test_cases[i].cleanup) {
			ret = mmc_test_cases[i].cleanup(test);
			if (ret) {
				printk(KERN_INFO "%s: Warning: Cleanup "
					"stage failed! (%d)\n",
					mmc_hostname(test->card->host),
					ret);
			}
		}
	}

	mmc_release_host(test->card->host);

	printk(KERN_INFO "%s: Tests completed.\n",
		mmc_hostname(test->card->host));
}

static ssize_t mmc_test_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	mutex_lock(&mmc_test_lock);
	mutex_unlock(&mmc_test_lock);

	return 0;
}

static ssize_t mmc_test_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct mmc_card *card;
	struct mmc_test_card *test;
830
	int testcase;
Pierre Ossman's avatar
Pierre Ossman committed
831 832 833

	card = container_of(dev, struct mmc_card, dev);

834 835
	testcase = simple_strtol(buf, NULL, 10);

Pierre Ossman's avatar
Pierre Ossman committed
836 837 838 839 840 841 842 843 844
	test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
	if (!test)
		return -ENOMEM;

	test->card = card;

	test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
	if (test->buffer) {
		mutex_lock(&mmc_test_lock);
845
		mmc_test_run(test, testcase);
Pierre Ossman's avatar
Pierre Ossman committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
		mutex_unlock(&mmc_test_lock);
	}

	kfree(test->buffer);
	kfree(test);

	return count;
}

static DEVICE_ATTR(test, S_IWUSR | S_IRUGO, mmc_test_show, mmc_test_store);

static int mmc_test_probe(struct mmc_card *card)
{
	int ret;

861 862 863
	if ((card->type != MMC_TYPE_MMC) && (card->type != MMC_TYPE_SD))
		return -ENODEV;

Pierre Ossman's avatar
Pierre Ossman committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	mutex_init(&mmc_test_lock);

	ret = device_create_file(&card->dev, &dev_attr_test);
	if (ret)
		return ret;

	return 0;
}

static void mmc_test_remove(struct mmc_card *card)
{
	device_remove_file(&card->dev, &dev_attr_test);
}

static struct mmc_driver mmc_driver = {
	.drv		= {
		.name	= "mmc_test",
	},
	.probe		= mmc_test_probe,
	.remove		= mmc_test_remove,
};

static int __init mmc_test_init(void)
{
	return mmc_register_driver(&mmc_driver);
}

static void __exit mmc_test_exit(void)
{
	mmc_unregister_driver(&mmc_driver);
}

module_init(mmc_test_init);
module_exit(mmc_test_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Multimedia Card (MMC) host test driver");
MODULE_AUTHOR("Pierre Ossman");