mtdchar.c 16.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * Character-device access to raw MTD devices.
 *
 */

6 7
#include <linux/device.h>
#include <linux/fs.h>
A
Andrew Morton 已提交
8
#include <linux/mm.h>
9
#include <linux/err.h>
10
#include <linux/init.h>
L
Linus Torvalds 已提交
11 12
#include <linux/kernel.h>
#include <linux/module.h>
13 14
#include <linux/slab.h>
#include <linux/sched.h>
15
#include <linux/smp_lock.h>
16

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 30 31
	device_create_drvdata(mtd_class, NULL,
			      MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
			      NULL, "mtd%d", mtd->index);
32

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

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

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

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

52
/*
53 54
 * Data structure to hold the pointer to the mtd device as well
 * as mode information ofr various use cases.
55
 */
56 57 58 59
struct mtd_file_info {
	struct mtd_info *mtd;
	enum mtd_file_modes mode;
};
60

L
Linus Torvalds 已提交
61 62
static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
63 64
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
65 66

	switch (orig) {
67
	case SEEK_SET:
L
Linus Torvalds 已提交
68
		break;
69
	case SEEK_CUR:
70
		offset += file->f_pos;
L
Linus Torvalds 已提交
71
		break;
72
	case SEEK_END:
73
		offset += mtd->size;
L
Linus Torvalds 已提交
74 75 76 77 78
		break;
	default:
		return -EINVAL;
	}

79
	if (offset >= 0 && offset <= mtd->size)
80
		return file->f_pos = offset;
L
Linus Torvalds 已提交
81

82
	return -EINVAL;
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90
}



static int mtd_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	int devnum = minor >> 1;
91
	int ret = 0;
L
Linus Torvalds 已提交
92
	struct mtd_info *mtd;
93
	struct mtd_file_info *mfi;
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103

	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;

104
	lock_kernel();
L
Linus Torvalds 已提交
105
	mtd = get_mtd_device(NULL, devnum);
106

107 108 109 110
	if (IS_ERR(mtd)) {
		ret = PTR_ERR(mtd);
		goto out;
	}
111

L
Linus Torvalds 已提交
112 113
	if (MTD_ABSENT == mtd->type) {
		put_mtd_device(mtd);
114 115
		ret = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
116 117 118 119 120
	}

	/* 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);
121 122
		ret = -EACCES;
		goto out;
L
Linus Torvalds 已提交
123
	}
124

125 126 127
	mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
	if (!mfi) {
		put_mtd_device(mtd);
128 129
		ret = -ENOMEM;
		goto out;
130 131 132 133
	}
	mfi->mtd = mtd;
	file->private_data = mfi;

134 135 136
out:
	unlock_kernel();
	return ret;
L
Linus Torvalds 已提交
137 138 139 140 141 142
} /* mtd_open */

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

static int mtd_close(struct inode *inode, struct file *file)
{
143 144
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
145 146 147

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");

148 149
	/* Only sync if opened RW */
	if ((file->f_mode & 2) && mtd->sync)
L
Linus Torvalds 已提交
150
		mtd->sync(mtd);
151

L
Linus Torvalds 已提交
152
	put_mtd_device(mtd);
153 154
	file->private_data = NULL;
	kfree(mfi);
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165

	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)
{
166 167
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
168 169 170 171 172
	size_t retlen=0;
	size_t total_retlen=0;
	int ret=0;
	int len;
	char *kbuf;
173

L
Linus Torvalds 已提交
174 175 176 177 178 179 180
	DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");

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

	if (!count)
		return 0;
181

L
Linus Torvalds 已提交
182 183
	/* FIXME: Use kiovec in 2.5 to lock down the user's buffers
	   and pass them directly to the MTD functions */
184 185 186 187 188 189 190 191 192

	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 已提交
193
	while (count) {
194

195
		if (count > MAX_KMALLOC_SIZE)
L
Linus Torvalds 已提交
196 197 198 199
			len = MAX_KMALLOC_SIZE;
		else
			len = count;

200 201
		switch (mfi->mode) {
		case MTD_MODE_OTP_FACTORY:
202 203 204 205 206
			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;
207 208 209 210 211 212 213 214 215 216 217 218 219
		case MTD_MODE_RAW:
		{
			struct mtd_oob_ops ops;

			ops.mode = MTD_OOB_RAW;
			ops.datbuf = kbuf;
			ops.oobbuf = NULL;
			ops.len = len;

			ret = mtd->read_oob(mtd, *ppos, &ops);
			retlen = ops.retlen;
			break;
		}
220
		default:
221
			ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
222
		}
L
Linus Torvalds 已提交
223 224
		/* Nand returns -EBADMSG on ecc errors, but it returns
		 * the data. For our userspace tools it is important
225
		 * to dump areas with ecc errors !
226 227 228
		 * 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 已提交
229 230 231
		 * Userspace software which accesses NAND this way
		 * must be aware of the fact that it deals with NAND
		 */
232
		if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
L
Linus Torvalds 已提交
233 234
			*ppos += retlen;
			if (copy_to_user(buf, kbuf, retlen)) {
235
				kfree(kbuf);
L
Linus Torvalds 已提交
236 237 238 239 240 241 242
				return -EFAULT;
			}
			else
				total_retlen += retlen;

			count -= retlen;
			buf += retlen;
243 244
			if (retlen == 0)
				count = 0;
L
Linus Torvalds 已提交
245 246 247 248 249
		}
		else {
			kfree(kbuf);
			return ret;
		}
250

L
Linus Torvalds 已提交
251 252
	}

253
	kfree(kbuf);
L
Linus Torvalds 已提交
254 255 256 257 258
	return total_retlen;
} /* mtd_read */

static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
{
259 260
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
261 262 263 264 265 266 267
	char *kbuf;
	size_t retlen;
	size_t total_retlen=0;
	int ret=0;
	int len;

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

L
Linus Torvalds 已提交
269 270
	if (*ppos == mtd->size)
		return -ENOSPC;
271

L
Linus Torvalds 已提交
272 273 274 275 276 277
	if (*ppos + count > mtd->size)
		count = mtd->size - *ppos;

	if (!count)
		return 0;

278 279 280 281 282 283 284 285
	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 已提交
286
	while (count) {
287

288
		if (count > MAX_KMALLOC_SIZE)
L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296
			len = MAX_KMALLOC_SIZE;
		else
			len = count;

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

298 299
		switch (mfi->mode) {
		case MTD_MODE_OTP_FACTORY:
300 301 302 303 304 305 306 307 308
			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;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

		case MTD_MODE_RAW:
		{
			struct mtd_oob_ops ops;

			ops.mode = MTD_OOB_RAW;
			ops.datbuf = kbuf;
			ops.oobbuf = NULL;
			ops.len = len;

			ret = mtd->write_oob(mtd, *ppos, &ops);
			retlen = ops.retlen;
			break;
		}

324 325 326
		default:
			ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
		}
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334 335 336 337 338
		if (!ret) {
			*ppos += retlen;
			total_retlen += retlen;
			count -= retlen;
			buf += retlen;
		}
		else {
			kfree(kbuf);
			return ret;
		}
	}

339
	kfree(kbuf);
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352
	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);
}

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
#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
{
	struct mtd_info *mtd = mfi->mtd;
	int ret = 0;

	switch (mode) {
	case MTD_OTP_FACTORY:
		if (!mtd->read_fact_prot_reg)
			ret = -EOPNOTSUPP;
		else
			mfi->mode = MTD_MODE_OTP_FACTORY;
		break;
	case MTD_OTP_USER:
		if (!mtd->read_fact_prot_reg)
			ret = -EOPNOTSUPP;
		else
			mfi->mode = MTD_MODE_OTP_USER;
		break;
	default:
		ret = -EINVAL;
	case MTD_OTP_OFF:
		break;
	}
	return ret;
}
#else
# define otp_select_filemode(f,m)	-EOPNOTSUPP
#endif

L
Linus Torvalds 已提交
383 384 385
static int mtd_ioctl(struct inode *inode, struct file *file,
		     u_int cmd, u_long arg)
{
386 387
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
388 389 390
	void __user *argp = (void __user *)arg;
	int ret = 0;
	u_long size;
391
	struct mtd_info_user info;
392

L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403
	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;
	}
404

L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	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:
427 428 429 430 431 432
		info.type	= mtd->type;
		info.flags	= mtd->flags;
		info.size	= mtd->size;
		info.erasesize	= mtd->erasesize;
		info.writesize	= mtd->writesize;
		info.oobsize	= mtd->oobsize;
433 434 435
		/* The below fields are obsolete */
		info.ecctype	= -1;
		info.eccsize	= 0;
436
		if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446
			return -EFAULT;
		break;

	case MEMERASE:
	{
		struct erase_info *erase;

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

447
		erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
L
Linus Torvalds 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		if (!erase)
			ret = -ENOMEM;
		else {
			wait_queue_head_t waitq;
			DECLARE_WAITQUEUE(wait, current);

			init_waitqueue_head(&waitq);

			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;
464

L
Linus Torvalds 已提交
465 466 467
			/*
			  FIXME: Allow INTERRUPTIBLE. Which means
			  not having the wait_queue head on the stack.
468

L
Linus Torvalds 已提交
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
			  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;
494
		struct mtd_oob_ops ops;
495
		struct mtd_oob_buf __user *user_buf = argp;
496
	        uint32_t retlen;
497

L
Linus Torvalds 已提交
498 499 500 501 502
		if(!(file->f_mode & 2))
			return -EPERM;

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

504
		if (buf.length > 4096)
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515
			return -EINVAL;

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

		if (ret)
			return ret;

516
		ops.ooblen = buf.length;
517 518 519 520
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

521
		if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
522 523 524 525
			return -EINVAL;

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

528 529
		if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
			kfree(ops.oobbuf);
L
Linus Torvalds 已提交
530 531 532
			return -EFAULT;
		}

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

536 537 538
		if (ops.oobretlen > 0xFFFFFFFFU)
			ret = -EOVERFLOW;
		retlen = ops.oobretlen;
539
		if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
L
Linus Torvalds 已提交
540 541
			ret = -EFAULT;

542
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549
		break;

	}

	case MEMREADOOB:
	{
		struct mtd_oob_buf buf;
550
		struct mtd_oob_ops ops;
L
Linus Torvalds 已提交
551 552 553

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

555
		if (buf.length > 4096)
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565
			return -EINVAL;

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

566
		ops.ooblen = buf.length;
567 568 569 570
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

571
		if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
572 573 574 575
			return -EINVAL;

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

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

581
		if (put_user(ops.oobretlen, (uint32_t __user *)argp))
L
Linus Torvalds 已提交
582
			ret = -EFAULT;
583 584
		else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
						    ops.oobretlen))
L
Linus Torvalds 已提交
585
			ret = -EFAULT;
586

587
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
588 589 590 591 592
		break;
	}

	case MEMLOCK:
	{
593
		struct erase_info_user einfo;
L
Linus Torvalds 已提交
594

595
		if (copy_from_user(&einfo, argp, sizeof(einfo)))
L
Linus Torvalds 已提交
596 597 598 599 600
			return -EFAULT;

		if (!mtd->lock)
			ret = -EOPNOTSUPP;
		else
601
			ret = mtd->lock(mtd, einfo.start, einfo.length);
L
Linus Torvalds 已提交
602 603 604 605 606
		break;
	}

	case MEMUNLOCK:
	{
607
		struct erase_info_user einfo;
L
Linus Torvalds 已提交
608

609
		if (copy_from_user(&einfo, argp, sizeof(einfo)))
L
Linus Torvalds 已提交
610 611 612 613 614
			return -EFAULT;

		if (!mtd->unlock)
			ret = -EOPNOTSUPP;
		else
615
			ret = mtd->unlock(mtd, einfo.start, einfo.length);
L
Linus Torvalds 已提交
616 617 618
		break;
	}

619
	/* Legacy interface */
L
Linus Torvalds 已提交
620 621
	case MEMGETOOBSEL:
	{
622 623 624 625 626 627 628 629 630 631 632
		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));
633
		oi.eccbytes = mtd->ecclayout->eccbytes;
634 635

		if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
L
Linus Torvalds 已提交
636 637 638 639 640 641 642
			return -EFAULT;
		break;
	}

	case MEMGETBADBLOCK:
	{
		loff_t offs;
643

L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		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;
	}

666
#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
667 668 669 670 671
	case OTPSELECT:
	{
		int mode;
		if (copy_from_user(&mode, argp, sizeof(int)))
			return -EFAULT;
672 673 674 675 676

		mfi->mode = MTD_MODE_NORMAL;

		ret = otp_select_filemode(mfi, mode);

677
		file->f_pos = 0;
678 679 680 681 682 683 684 685 686 687
		break;
	}

	case OTPGETREGIONCOUNT:
	case OTPGETREGIONINFO:
	{
		struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
		if (!buf)
			return -ENOMEM;
		ret = -EOPNOTSUPP;
688 689
		switch (mfi->mode) {
		case MTD_MODE_OTP_FACTORY:
690 691 692 693 694 695 696
			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;
697 698
		default:
			break;
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		}
		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:
	{
715
		struct otp_info oinfo;
716

717
		if (mfi->mode != MTD_MODE_OTP_USER)
718
			return -EINVAL;
719
		if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
720 721 722
			return -EFAULT;
		if (!mtd->lock_user_prot_reg)
			return -EOPNOTSUPP;
723
		ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
724 725 726 727
		break;
	}
#endif

728 729 730 731 732
	case ECCGETLAYOUT:
	{
		if (!mtd->ecclayout)
			return -EOPNOTSUPP;

733
		if (copy_to_user(argp, mtd->ecclayout,
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
				 sizeof(struct nand_ecclayout)))
			return -EFAULT;
		break;
	}

	case ECCGETSTATS:
	{
		if (copy_to_user(argp, &mtd->ecc_stats,
				 sizeof(struct mtd_ecc_stats)))
			return -EFAULT;
		break;
	}

	case MTDFILEMODE:
	{
		mfi->mode = 0;

		switch(arg) {
		case MTD_MODE_OTP_FACTORY:
		case MTD_MODE_OTP_USER:
			ret = otp_select_filemode(mfi, arg);
			break;

		case MTD_MODE_RAW:
			if (!mtd->read_oob || !mtd->write_oob)
				return -EOPNOTSUPP;
			mfi->mode = arg;

		case MTD_MODE_NORMAL:
			break;
		default:
			ret = -EINVAL;
		}
		file->f_pos = 0;
		break;
	}

L
Linus Torvalds 已提交
771 772 773 774 775 776 777
	default:
		ret = -ENOTTY;
	}

	return ret;
} /* memory_ioctl */

778
static const struct file_operations mtd_fops = {
L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	.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;
	}

796 797 798 799 800
	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");
801
		return PTR_ERR(mtd_class);
802 803 804
	}

	register_mtd_user(&notifier);
L
Linus Torvalds 已提交
805 806 807 808 809
	return 0;
}

static void __exit cleanup_mtdchar(void)
{
810 811
	unregister_mtd_user(&notifier);
	class_destroy(mtd_class);
L
Linus Torvalds 已提交
812 813 814 815 816 817 818 819 820 821
	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");