mtdchar.c 14.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
L
Linus Torvalds 已提交
3 4 5 6 7 8
 *
 * Character-device access to raw MTD devices.
 *
 */

#include <linux/config.h>
9 10 11
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
L
Linus Torvalds 已提交
12 13
#include <linux/kernel.h>
#include <linux/module.h>
14 15 16
#include <linux/slab.h>
#include <linux/sched.h>

L
Linus Torvalds 已提交
17 18 19
#include <linux/mtd/mtd.h>
#include <linux/mtd/compatmac.h>

20
#include <asm/uaccess.h>
21 22

static struct class *mtd_class;
L
Linus Torvalds 已提交
23 24 25 26 27 28

static void mtd_notify_add(struct mtd_info* mtd)
{
	if (!mtd)
		return;

29
	class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
30
			    NULL, "mtd%d", mtd->index);
31

32
	class_device_create(mtd_class, NULL,
33 34
			    MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
			    NULL, "mtd%dro", mtd->index);
L
Linus Torvalds 已提交
35 36 37 38 39 40
}

static void mtd_notify_remove(struct mtd_info* mtd)
{
	if (!mtd)
		return;
41 42 43

	class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
	class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
L
Linus Torvalds 已提交
44 45 46 47 48 49 50
}

static struct mtd_notifier notifier = {
	.add	= mtd_notify_add,
	.remove	= mtd_notify_remove,
};

51 52 53 54 55 56 57
/*
 * We use file->private_data to store a pointer to the MTDdevice.
 * Since alighment is at least 32 bits, we have 2 bits free for OTP
 * modes as well.
 */

#define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58

59 60 61 62 63 64 65
#define MTD_MODE_OTP_FACT	1
#define MTD_MODE_OTP_USER	2
#define MTD_MODE(file)		((long)((file)->private_data) & 3)

#define SET_MTD_MODE(file, mode) \
	do { long __p = (long)((file)->private_data); \
	     (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
66

L
Linus Torvalds 已提交
67 68
static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
69
	struct mtd_info *mtd = TO_MTD(file);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76

	switch (orig) {
	case 0:
		/* SEEK_SET */
		break;
	case 1:
		/* SEEK_CUR */
77
		offset += file->f_pos;
L
Linus Torvalds 已提交
78 79 80
		break;
	case 2:
		/* SEEK_END */
81
		offset += mtd->size;
L
Linus Torvalds 已提交
82 83 84 85 86
		break;
	default:
		return -EINVAL;
	}

87 88
	if (offset >= 0 && offset < mtd->size)
		return file->f_pos = offset;
L
Linus Torvalds 已提交
89

90
	return -EINVAL;
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}



static int mtd_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	int devnum = minor >> 1;
	struct mtd_info *mtd;

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");

	if (devnum >= MAX_MTD_DEVICES)
		return -ENODEV;

	/* You can't open the RO devices RW */
	if ((file->f_mode & 2) && (minor & 1))
		return -EACCES;

	mtd = get_mtd_device(NULL, devnum);
111

L
Linus Torvalds 已提交
112 113
	if (!mtd)
		return -ENODEV;
114

L
Linus Torvalds 已提交
115 116 117 118 119 120
	if (MTD_ABSENT == mtd->type) {
		put_mtd_device(mtd);
		return -ENODEV;
	}

	file->private_data = mtd;
121

L
Linus Torvalds 已提交
122 123 124 125 126
	/* You can't open it RW if it's not a writeable device */
	if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
		put_mtd_device(mtd);
		return -EACCES;
	}
127

L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138
	return 0;
} /* mtd_open */

/*====================================================================*/

static int mtd_close(struct inode *inode, struct file *file)
{
	struct mtd_info *mtd;

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");

139
	mtd = TO_MTD(file);
140

L
Linus Torvalds 已提交
141 142
	if (mtd->sync)
		mtd->sync(mtd);
143

L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155
	put_mtd_device(mtd);

	return 0;
} /* mtd_close */

/* FIXME: This _really_ needs to die. In 2.5, we should lock the
   userspace buffer down and use it directly with readv/writev.
*/
#define MAX_KMALLOC_SIZE 0x20000

static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
{
156
	struct mtd_info *mtd = TO_MTD(file);
L
Linus Torvalds 已提交
157 158 159 160 161
	size_t retlen=0;
	size_t total_retlen=0;
	int ret=0;
	int len;
	char *kbuf;
162

L
Linus Torvalds 已提交
163 164 165 166 167 168 169
	DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");

	if (*ppos + count > mtd->size)
		count = mtd->size - *ppos;

	if (!count)
		return 0;
170

L
Linus Torvalds 已提交
171 172
	/* FIXME: Use kiovec in 2.5 to lock down the user's buffers
	   and pass them directly to the MTD functions */
173 174 175 176 177 178 179 180 181

	if (count > MAX_KMALLOC_SIZE)
		kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
	else
		kbuf=kmalloc(count, GFP_KERNEL);

	if (!kbuf)
		return -ENOMEM;

L
Linus Torvalds 已提交
182
	while (count) {
183

184
		if (count > MAX_KMALLOC_SIZE)
L
Linus Torvalds 已提交
185 186 187 188
			len = MAX_KMALLOC_SIZE;
		else
			len = count;

189
		switch (MTD_MODE(file)) {
190 191 192 193 194 195 196
		case MTD_MODE_OTP_FACT:
			ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
			break;
		case MTD_MODE_OTP_USER:
			ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
			break;
		default:
197
			ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
198
		}
L
Linus Torvalds 已提交
199 200
		/* Nand returns -EBADMSG on ecc errors, but it returns
		 * the data. For our userspace tools it is important
201
		 * to dump areas with ecc errors !
202 203 204
		 * For kernel internal usage it also might return -EUCLEAN
		 * to signal the caller that a bitflip has occured and has
		 * been corrected by the ECC algorithm.
L
Linus Torvalds 已提交
205 206 207
		 * Userspace software which accesses NAND this way
		 * must be aware of the fact that it deals with NAND
		 */
208
		if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
L
Linus Torvalds 已提交
209 210
			*ppos += retlen;
			if (copy_to_user(buf, kbuf, retlen)) {
211
				kfree(kbuf);
L
Linus Torvalds 已提交
212 213 214 215 216 217 218
				return -EFAULT;
			}
			else
				total_retlen += retlen;

			count -= retlen;
			buf += retlen;
219 220
			if (retlen == 0)
				count = 0;
L
Linus Torvalds 已提交
221 222 223 224 225
		}
		else {
			kfree(kbuf);
			return ret;
		}
226

L
Linus Torvalds 已提交
227 228
	}

229
	kfree(kbuf);
L
Linus Torvalds 已提交
230 231 232 233 234
	return total_retlen;
} /* mtd_read */

static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
{
235
	struct mtd_info *mtd = TO_MTD(file);
L
Linus Torvalds 已提交
236 237 238 239 240 241 242
	char *kbuf;
	size_t retlen;
	size_t total_retlen=0;
	int ret=0;
	int len;

	DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
243

L
Linus Torvalds 已提交
244 245
	if (*ppos == mtd->size)
		return -ENOSPC;
246

L
Linus Torvalds 已提交
247 248 249 250 251 252
	if (*ppos + count > mtd->size)
		count = mtd->size - *ppos;

	if (!count)
		return 0;

253 254 255 256 257 258 259 260
	if (count > MAX_KMALLOC_SIZE)
		kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
	else
		kbuf=kmalloc(count, GFP_KERNEL);

	if (!kbuf)
		return -ENOMEM;

L
Linus Torvalds 已提交
261
	while (count) {
262

263
		if (count > MAX_KMALLOC_SIZE)
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271
			len = MAX_KMALLOC_SIZE;
		else
			len = count;

		if (copy_from_user(kbuf, buf, len)) {
			kfree(kbuf);
			return -EFAULT;
		}
272

273
		switch (MTD_MODE(file)) {
274 275 276 277 278 279 280 281 282 283 284 285 286
		case MTD_MODE_OTP_FACT:
			ret = -EROFS;
			break;
		case MTD_MODE_OTP_USER:
			if (!mtd->write_user_prot_reg) {
				ret = -EOPNOTSUPP;
				break;
			}
			ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
			break;
		default:
			ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
		}
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298
		if (!ret) {
			*ppos += retlen;
			total_retlen += retlen;
			count -= retlen;
			buf += retlen;
		}
		else {
			kfree(kbuf);
			return ret;
		}
	}

299
	kfree(kbuf);
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	return total_retlen;
} /* mtd_write */

/*======================================================================

    IOCTL calls for getting device parameters.

======================================================================*/
static void mtdchar_erase_callback (struct erase_info *instr)
{
	wake_up((wait_queue_head_t *)instr->priv);
}

static int mtd_ioctl(struct inode *inode, struct file *file,
		     u_int cmd, u_long arg)
{
316
	struct mtd_info *mtd = TO_MTD(file);
L
Linus Torvalds 已提交
317 318 319
	void __user *argp = (void __user *)arg;
	int ret = 0;
	u_long size;
320

L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331
	DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");

	size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
	if (cmd & IOC_IN) {
		if (!access_ok(VERIFY_READ, argp, size))
			return -EFAULT;
	}
	if (cmd & IOC_OUT) {
		if (!access_ok(VERIFY_WRITE, argp, size))
			return -EFAULT;
	}
332

L
Linus Torvalds 已提交
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
	switch (cmd) {
	case MEMGETREGIONCOUNT:
		if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
			return -EFAULT;
		break;

	case MEMGETREGIONINFO:
	{
		struct region_info_user ur;

		if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
			return -EFAULT;

		if (ur.regionindex >= mtd->numeraseregions)
			return -EINVAL;
		if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
				sizeof(struct mtd_erase_region_info)))
			return -EFAULT;
		break;
	}

	case MEMGETINFO:
		if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
			return -EFAULT;
		break;

	case MEMERASE:
	{
		struct erase_info *erase;

		if(!(file->f_mode & 2))
			return -EPERM;

		erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
		if (!erase)
			ret = -ENOMEM;
		else {
			wait_queue_head_t waitq;
			DECLARE_WAITQUEUE(wait, current);

			init_waitqueue_head(&waitq);

			memset (erase,0,sizeof(struct erase_info));
			if (copy_from_user(&erase->addr, argp,
				    sizeof(struct erase_info_user))) {
				kfree(erase);
				return -EFAULT;
			}
			erase->mtd = mtd;
			erase->callback = mtdchar_erase_callback;
			erase->priv = (unsigned long)&waitq;
384

L
Linus Torvalds 已提交
385 386 387
			/*
			  FIXME: Allow INTERRUPTIBLE. Which means
			  not having the wait_queue head on the stack.
388

L
Linus Torvalds 已提交
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
			  If the wq_head is on the stack, and we
			  leave because we got interrupted, then the
			  wq_head is no longer there when the
			  callback routine tries to wake us up.
			*/
			ret = mtd->erase(mtd, erase);
			if (!ret) {
				set_current_state(TASK_UNINTERRUPTIBLE);
				add_wait_queue(&waitq, &wait);
				if (erase->state != MTD_ERASE_DONE &&
				    erase->state != MTD_ERASE_FAILED)
					schedule();
				remove_wait_queue(&waitq, &wait);
				set_current_state(TASK_RUNNING);

				ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
			}
			kfree(erase);
		}
		break;
	}

	case MEMWRITEOOB:
	{
		struct mtd_oob_buf buf;
414
		struct mtd_oob_ops ops;
415

L
Linus Torvalds 已提交
416 417 418 419 420
		if(!(file->f_mode & 2))
			return -EPERM;

		if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
			return -EFAULT;
421

422
		if (buf.length > 4096)
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431 432 433
			return -EINVAL;

		if (!mtd->write_oob)
			ret = -EOPNOTSUPP;
		else
			ret = access_ok(VERIFY_READ, buf.ptr,
					buf.length) ? 0 : EFAULT;

		if (ret)
			return ret;

434 435 436 437 438 439 440 441 442 443 444
		ops.len = buf.length;
		ops.ooblen = mtd->oobsize;
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

		if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
			return -EINVAL;

		ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
		if (!ops.oobbuf)
L
Linus Torvalds 已提交
445
			return -ENOMEM;
446

447 448
		if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
			kfree(ops.oobbuf);
L
Linus Torvalds 已提交
449 450 451
			return -EFAULT;
		}

452 453
		buf.start &= ~(mtd->oobsize - 1);
		ret = mtd->write_oob(mtd, buf.start, &ops);
L
Linus Torvalds 已提交
454

455 456
		if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
				 sizeof(uint32_t)))
L
Linus Torvalds 已提交
457 458
			ret = -EFAULT;

459
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
460 461 462 463 464 465 466
		break;

	}

	case MEMREADOOB:
	{
		struct mtd_oob_buf buf;
467
		struct mtd_oob_ops ops;
L
Linus Torvalds 已提交
468 469 470

		if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
			return -EFAULT;
471

472
		if (buf.length > 4096)
L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480 481 482
			return -EINVAL;

		if (!mtd->read_oob)
			ret = -EOPNOTSUPP;
		else
			ret = access_ok(VERIFY_WRITE, buf.ptr,
					buf.length) ? 0 : -EFAULT;
		if (ret)
			return ret;

483 484 485 486 487 488 489 490 491 492 493
		ops.len = buf.length;
		ops.ooblen = mtd->oobsize;
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

		if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
			return -EINVAL;

		ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
		if (!ops.oobbuf)
L
Linus Torvalds 已提交
494
			return -ENOMEM;
495

496 497
		buf.start &= ~(mtd->oobsize - 1);
		ret = mtd->read_oob(mtd, buf.start, &ops);
L
Linus Torvalds 已提交
498

499
		if (put_user(ops.retlen, (uint32_t __user *)argp))
L
Linus Torvalds 已提交
500
			ret = -EFAULT;
501 502
		else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
						    ops.retlen))
L
Linus Torvalds 已提交
503
			ret = -EFAULT;
504

505
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
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
		break;
	}

	case MEMLOCK:
	{
		struct erase_info_user info;

		if (copy_from_user(&info, argp, sizeof(info)))
			return -EFAULT;

		if (!mtd->lock)
			ret = -EOPNOTSUPP;
		else
			ret = mtd->lock(mtd, info.start, info.length);
		break;
	}

	case MEMUNLOCK:
	{
		struct erase_info_user info;

		if (copy_from_user(&info, argp, sizeof(info)))
			return -EFAULT;

		if (!mtd->unlock)
			ret = -EOPNOTSUPP;
		else
			ret = mtd->unlock(mtd, info.start, info.length);
		break;
	}

537
	/* Legacy interface */
L
Linus Torvalds 已提交
538 539
	case MEMGETOOBSEL:
	{
540 541 542 543 544 545 546 547 548 549 550 551 552
		struct nand_oobinfo oi;

		if (!mtd->ecclayout)
			return -EOPNOTSUPP;
		if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
			return -EINVAL;

		oi.useecc = MTD_NANDECC_AUTOPLACE;
		memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
		memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
		       sizeof(oi.oobfree));

		if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
L
Linus Torvalds 已提交
553 554 555 556
			return -EFAULT;
		break;
	}

557 558 559 560 561 562 563 564 565 566
	case ECCGETLAYOUT:

		if (!mtd->ecclayout)
			return -EOPNOTSUPP;

		if (copy_to_user(argp, &mtd->ecclayout,
				 sizeof(struct nand_ecclayout)))
			return -EFAULT;
		break;

L
Linus Torvalds 已提交
567 568 569
	case MEMGETBADBLOCK:
	{
		loff_t offs;
570

L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
		if (copy_from_user(&offs, argp, sizeof(loff_t)))
			return -EFAULT;
		if (!mtd->block_isbad)
			ret = -EOPNOTSUPP;
		else
			return mtd->block_isbad(mtd, offs);
		break;
	}

	case MEMSETBADBLOCK:
	{
		loff_t offs;

		if (copy_from_user(&offs, argp, sizeof(loff_t)))
			return -EFAULT;
		if (!mtd->block_markbad)
			ret = -EOPNOTSUPP;
		else
			return mtd->block_markbad(mtd, offs);
		break;
	}

593
#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
594 595 596 597 598
	case OTPSELECT:
	{
		int mode;
		if (copy_from_user(&mode, argp, sizeof(int)))
			return -EFAULT;
599
		SET_MTD_MODE(file, 0);
600 601 602 603 604
		switch (mode) {
		case MTD_OTP_FACTORY:
			if (!mtd->read_fact_prot_reg)
				ret = -EOPNOTSUPP;
			else
605
				SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
606 607 608 609 610
			break;
		case MTD_OTP_USER:
			if (!mtd->read_fact_prot_reg)
				ret = -EOPNOTSUPP;
			else
611
				SET_MTD_MODE(file, MTD_MODE_OTP_USER);
612 613 614 615 616 617
			break;
		default:
			ret = -EINVAL;
		case MTD_OTP_OFF:
			break;
		}
618
		file->f_pos = 0;
619 620 621 622 623 624 625 626 627 628
		break;
	}

	case OTPGETREGIONCOUNT:
	case OTPGETREGIONINFO:
	{
		struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
		if (!buf)
			return -ENOMEM;
		ret = -EOPNOTSUPP;
629
		switch (MTD_MODE(file)) {
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
		case MTD_MODE_OTP_FACT:
			if (mtd->get_fact_prot_info)
				ret = mtd->get_fact_prot_info(mtd, buf, 4096);
			break;
		case MTD_MODE_OTP_USER:
			if (mtd->get_user_prot_info)
				ret = mtd->get_user_prot_info(mtd, buf, 4096);
			break;
		}
		if (ret >= 0) {
			if (cmd == OTPGETREGIONCOUNT) {
				int nbr = ret / sizeof(struct otp_info);
				ret = copy_to_user(argp, &nbr, sizeof(int));
			} else
				ret = copy_to_user(argp, buf, ret);
			if (ret)
				ret = -EFAULT;
		}
		kfree(buf);
		break;
	}

	case OTPLOCK:
	{
		struct otp_info info;

656
		if (MTD_MODE(file) != MTD_MODE_OTP_USER)
657 658 659 660 661 662 663 664 665 666
			return -EINVAL;
		if (copy_from_user(&info, argp, sizeof(info)))
			return -EFAULT;
		if (!mtd->lock_user_prot_reg)
			return -EOPNOTSUPP;
		ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
		break;
	}
#endif

L
Linus Torvalds 已提交
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
	default:
		ret = -ENOTTY;
	}

	return ret;
} /* memory_ioctl */

static struct file_operations mtd_fops = {
	.owner		= THIS_MODULE,
	.llseek		= mtd_lseek,
	.read		= mtd_read,
	.write		= mtd_write,
	.ioctl		= mtd_ioctl,
	.open		= mtd_open,
	.release	= mtd_close,
};

static int __init init_mtdchar(void)
{
	if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
		printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
		       MTD_CHAR_MAJOR);
		return -EAGAIN;
	}

692 693 694 695 696
	mtd_class = class_create(THIS_MODULE, "mtd");

	if (IS_ERR(mtd_class)) {
		printk(KERN_ERR "Error creating mtd class.\n");
		unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
697
		return PTR_ERR(mtd_class);
698 699 700
	}

	register_mtd_user(&notifier);
L
Linus Torvalds 已提交
701 702 703 704 705
	return 0;
}

static void __exit cleanup_mtdchar(void)
{
706 707
	unregister_mtd_user(&notifier);
	class_destroy(mtd_class);
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715 716 717
	unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
}

module_init(init_mtdchar);
module_exit(cleanup_mtdchar);


MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
MODULE_DESCRIPTION("Direct character-device access to MTD devices");