mtdchar.c 17.6 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
#include <linux/backing-dev.h>
17

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

21
#include <asm/uaccess.h>
22

L
Linus Torvalds 已提交
23

24
/*
25 26
 * Data structure to hold the pointer to the mtd device as well
 * as mode information ofr various use cases.
27
 */
28 29 30 31
struct mtd_file_info {
	struct mtd_info *mtd;
	enum mtd_file_modes mode;
};
32

L
Linus Torvalds 已提交
33 34
static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
{
35 36
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
37 38

	switch (orig) {
39
	case SEEK_SET:
L
Linus Torvalds 已提交
40
		break;
41
	case SEEK_CUR:
42
		offset += file->f_pos;
L
Linus Torvalds 已提交
43
		break;
44
	case SEEK_END:
45
		offset += mtd->size;
L
Linus Torvalds 已提交
46 47 48 49 50
		break;
	default:
		return -EINVAL;
	}

51
	if (offset >= 0 && offset <= mtd->size)
52
		return file->f_pos = offset;
L
Linus Torvalds 已提交
53

54
	return -EINVAL;
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62
}



static int mtd_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	int devnum = minor >> 1;
63
	int ret = 0;
L
Linus Torvalds 已提交
64
	struct mtd_info *mtd;
65
	struct mtd_file_info *mfi;
L
Linus Torvalds 已提交
66 67 68 69 70 71 72

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");

	if (devnum >= MAX_MTD_DEVICES)
		return -ENODEV;

	/* You can't open the RO devices RW */
73
	if ((file->f_mode & FMODE_WRITE) && (minor & 1))
L
Linus Torvalds 已提交
74 75
		return -EACCES;

76
	lock_kernel();
L
Linus Torvalds 已提交
77
	mtd = get_mtd_device(NULL, devnum);
78

79 80 81 82
	if (IS_ERR(mtd)) {
		ret = PTR_ERR(mtd);
		goto out;
	}
83

84
	if (mtd->type == MTD_ABSENT) {
L
Linus Torvalds 已提交
85
		put_mtd_device(mtd);
86 87
		ret = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
88 89
	}

90 91 92
	if (mtd->backing_dev_info)
		file->f_mapping->backing_dev_info = mtd->backing_dev_info;

L
Linus Torvalds 已提交
93
	/* You can't open it RW if it's not a writeable device */
94
	if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
L
Linus Torvalds 已提交
95
		put_mtd_device(mtd);
96 97
		ret = -EACCES;
		goto out;
L
Linus Torvalds 已提交
98
	}
99

100 101 102
	mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
	if (!mfi) {
		put_mtd_device(mtd);
103 104
		ret = -ENOMEM;
		goto out;
105 106 107 108
	}
	mfi->mtd = mtd;
	file->private_data = mfi;

109 110 111
out:
	unlock_kernel();
	return ret;
L
Linus Torvalds 已提交
112 113 114 115 116 117
} /* mtd_open */

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

static int mtd_close(struct inode *inode, struct file *file)
{
118 119
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
120 121 122

	DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");

123
	/* Only sync if opened RW */
124
	if ((file->f_mode & FMODE_WRITE) && mtd->sync)
L
Linus Torvalds 已提交
125
		mtd->sync(mtd);
126

L
Linus Torvalds 已提交
127
	put_mtd_device(mtd);
128 129
	file->private_data = NULL;
	kfree(mfi);
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140

	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)
{
141 142
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
143 144 145 146 147
	size_t retlen=0;
	size_t total_retlen=0;
	int ret=0;
	int len;
	char *kbuf;
148

L
Linus Torvalds 已提交
149 150 151 152 153 154 155
	DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");

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

	if (!count)
		return 0;
156

L
Linus Torvalds 已提交
157 158
	/* FIXME: Use kiovec in 2.5 to lock down the user's buffers
	   and pass them directly to the MTD functions */
159 160 161 162 163 164 165 166 167

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

170
		if (count > MAX_KMALLOC_SIZE)
L
Linus Torvalds 已提交
171 172 173 174
			len = MAX_KMALLOC_SIZE;
		else
			len = count;

175 176
		switch (mfi->mode) {
		case MTD_MODE_OTP_FACTORY:
177 178 179 180 181
			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;
182 183 184 185 186 187 188 189 190 191 192 193 194
		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;
		}
195
		default:
196
			ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
197
		}
L
Linus Torvalds 已提交
198 199
		/* Nand returns -EBADMSG on ecc errors, but it returns
		 * the data. For our userspace tools it is important
200
		 * to dump areas with ecc errors !
201 202 203
		 * 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 已提交
204 205 206
		 * Userspace software which accesses NAND this way
		 * must be aware of the fact that it deals with NAND
		 */
207
		if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
L
Linus Torvalds 已提交
208 209
			*ppos += retlen;
			if (copy_to_user(buf, kbuf, retlen)) {
210
				kfree(kbuf);
L
Linus Torvalds 已提交
211 212 213 214 215 216 217
				return -EFAULT;
			}
			else
				total_retlen += retlen;

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

L
Linus Torvalds 已提交
226 227
	}

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

static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
{
234 235
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
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 274
		switch (mfi->mode) {
		case MTD_MODE_OTP_FACTORY:
275 276 277 278 279 280 281 282 283
			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;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

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

299 300 301
		default:
			ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
		}
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313
		if (!ret) {
			*ppos += retlen;
			total_retlen += retlen;
			count -= retlen;
			buf += retlen;
		}
		else {
			kfree(kbuf);
			return ret;
		}
	}

314
	kfree(kbuf);
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327
	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);
}

D
David Brownell 已提交
328
#ifdef CONFIG_HAVE_MTD_OTP
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
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 已提交
358 359 360
static int mtd_ioctl(struct inode *inode, struct file *file,
		     u_int cmd, u_long arg)
{
361 362
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;
L
Linus Torvalds 已提交
363 364 365
	void __user *argp = (void __user *)arg;
	int ret = 0;
	u_long size;
366
	struct mtd_info_user info;
367

L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378
	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;
	}
379

L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387
	switch (cmd) {
	case MEMGETREGIONCOUNT:
		if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
			return -EFAULT;
		break;

	case MEMGETREGIONINFO:
	{
388 389 390
		uint32_t ur_idx;
		struct mtd_erase_region_info *kr;
		struct region_info_user *ur = (struct region_info_user *) argp;
L
Linus Torvalds 已提交
391

392
		if (get_user(ur_idx, &(ur->regionindex)))
L
Linus Torvalds 已提交
393 394
			return -EFAULT;

395 396 397 398 399
		kr = &(mtd->eraseregions[ur_idx]);

		if (put_user(kr->offset, &(ur->offset))
		    || put_user(kr->erasesize, &(ur->erasesize))
		    || put_user(kr->numblocks, &(ur->numblocks)))
L
Linus Torvalds 已提交
400
			return -EFAULT;
401

L
Linus Torvalds 已提交
402 403 404 405
		break;
	}

	case MEMGETINFO:
406 407 408 409 410 411
		info.type	= mtd->type;
		info.flags	= mtd->flags;
		info.size	= mtd->size;
		info.erasesize	= mtd->erasesize;
		info.writesize	= mtd->writesize;
		info.oobsize	= mtd->oobsize;
412 413 414
		/* The below fields are obsolete */
		info.ecctype	= -1;
		info.eccsize	= 0;
415
		if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
L
Linus Torvalds 已提交
416 417 418 419
			return -EFAULT;
		break;

	case MEMERASE:
420
	case MEMERASE64:
L
Linus Torvalds 已提交
421 422 423
	{
		struct erase_info *erase;

424
		if(!(file->f_mode & FMODE_WRITE))
L
Linus Torvalds 已提交
425 426
			return -EPERM;

427
		erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435
		if (!erase)
			ret = -ENOMEM;
		else {
			wait_queue_head_t waitq;
			DECLARE_WAITQUEUE(wait, current);

			init_waitqueue_head(&waitq);

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
			if (cmd == MEMERASE64) {
				struct erase_info_user64 einfo64;

				if (copy_from_user(&einfo64, argp,
					    sizeof(struct erase_info_user64))) {
					kfree(erase);
					return -EFAULT;
				}
				erase->addr = einfo64.start;
				erase->len = einfo64.length;
			} else {
				struct erase_info_user einfo32;

				if (copy_from_user(&einfo32, argp,
					    sizeof(struct erase_info_user))) {
					kfree(erase);
					return -EFAULT;
				}
				erase->addr = einfo32.start;
				erase->len = einfo32.length;
L
Linus Torvalds 已提交
456 457 458 459
			}
			erase->mtd = mtd;
			erase->callback = mtdchar_erase_callback;
			erase->priv = (unsigned long)&waitq;
460

L
Linus Torvalds 已提交
461 462 463
			/*
			  FIXME: Allow INTERRUPTIBLE. Which means
			  not having the wait_queue head on the stack.
464

L
Linus Torvalds 已提交
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
			  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;
490
		struct mtd_oob_ops ops;
491
		struct mtd_oob_buf __user *user_buf = argp;
492
	        uint32_t retlen;
493

494
		if(!(file->f_mode & FMODE_WRITE))
L
Linus Torvalds 已提交
495 496 497 498
			return -EPERM;

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

500
		if (buf.length > 4096)
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511
			return -EINVAL;

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

		if (ret)
			return ret;

512
		ops.ooblen = buf.length;
513 514 515 516
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

517
		if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
518 519 520 521
			return -EINVAL;

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

524 525
		if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
			kfree(ops.oobbuf);
L
Linus Torvalds 已提交
526 527 528
			return -EFAULT;
		}

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

532 533 534
		if (ops.oobretlen > 0xFFFFFFFFU)
			ret = -EOVERFLOW;
		retlen = ops.oobretlen;
535
		if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
L
Linus Torvalds 已提交
536 537
			ret = -EFAULT;

538
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
539 540 541 542 543 544 545
		break;

	}

	case MEMREADOOB:
	{
		struct mtd_oob_buf buf;
546
		struct mtd_oob_ops ops;
L
Linus Torvalds 已提交
547 548 549

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

551
		if (buf.length > 4096)
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560 561
			return -EINVAL;

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

562
		ops.ooblen = buf.length;
563 564 565 566
		ops.ooboffs = buf.start & (mtd->oobsize - 1);
		ops.datbuf = NULL;
		ops.mode = MTD_OOB_PLACE;

567
		if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
568 569 570 571
			return -EINVAL;

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

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

577
		if (put_user(ops.oobretlen, (uint32_t __user *)argp))
L
Linus Torvalds 已提交
578
			ret = -EFAULT;
579 580
		else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
						    ops.oobretlen))
L
Linus Torvalds 已提交
581
			ret = -EFAULT;
582

583
		kfree(ops.oobbuf);
L
Linus Torvalds 已提交
584 585 586 587 588
		break;
	}

	case MEMLOCK:
	{
589
		struct erase_info_user einfo;
L
Linus Torvalds 已提交
590

591
		if (copy_from_user(&einfo, argp, sizeof(einfo)))
L
Linus Torvalds 已提交
592 593 594 595 596
			return -EFAULT;

		if (!mtd->lock)
			ret = -EOPNOTSUPP;
		else
597
			ret = mtd->lock(mtd, einfo.start, einfo.length);
L
Linus Torvalds 已提交
598 599 600 601 602
		break;
	}

	case MEMUNLOCK:
	{
603
		struct erase_info_user einfo;
L
Linus Torvalds 已提交
604

605
		if (copy_from_user(&einfo, argp, sizeof(einfo)))
L
Linus Torvalds 已提交
606 607 608 609 610
			return -EFAULT;

		if (!mtd->unlock)
			ret = -EOPNOTSUPP;
		else
611
			ret = mtd->unlock(mtd, einfo.start, einfo.length);
L
Linus Torvalds 已提交
612 613 614
		break;
	}

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

		if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
L
Linus Torvalds 已提交
632 633 634 635 636 637 638
			return -EFAULT;
		break;
	}

	case MEMGETBADBLOCK:
	{
		loff_t offs;
639

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

D
David Brownell 已提交
662
#ifdef CONFIG_HAVE_MTD_OTP
663 664 665 666 667
	case OTPSELECT:
	{
		int mode;
		if (copy_from_user(&mode, argp, sizeof(int)))
			return -EFAULT;
668 669 670 671 672

		mfi->mode = MTD_MODE_NORMAL;

		ret = otp_select_filemode(mfi, mode);

673
		file->f_pos = 0;
674 675 676 677 678 679 680 681 682 683
		break;
	}

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

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

724 725 726 727 728
	case ECCGETLAYOUT:
	{
		if (!mtd->ecclayout)
			return -EOPNOTSUPP;

729
		if (copy_to_user(argp, mtd->ecclayout,
730 731 732 733 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
				 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 已提交
767 768 769 770 771 772 773
	default:
		ret = -ENOTTY;
	}

	return ret;
} /* memory_ioctl */

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
/*
 * try to determine where a shared mapping can be made
 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
 *   mappings)
 */
#ifndef CONFIG_MMU
static unsigned long mtd_get_unmapped_area(struct file *file,
					   unsigned long addr,
					   unsigned long len,
					   unsigned long pgoff,
					   unsigned long flags)
{
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;

	if (mtd->get_unmapped_area) {
		unsigned long offset;

		if (addr != 0)
			return (unsigned long) -EINVAL;

		if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
			return (unsigned long) -EINVAL;

		offset = pgoff << PAGE_SHIFT;
		if (offset > mtd->size - len)
			return (unsigned long) -EINVAL;

		return mtd->get_unmapped_area(mtd, len, offset, flags);
	}

	/* can't map directly */
	return (unsigned long) -ENOSYS;
}
#endif

/*
 * set up a mapping for shared memory segments
 */
static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
{
#ifdef CONFIG_MMU
	struct mtd_file_info *mfi = file->private_data;
	struct mtd_info *mtd = mfi->mtd;

	if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
		return 0;
	return -ENOSYS;
#else
	return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
#endif
}

827
static const struct file_operations mtd_fops = {
L
Linus Torvalds 已提交
828 829 830 831 832 833 834
	.owner		= THIS_MODULE,
	.llseek		= mtd_lseek,
	.read		= mtd_read,
	.write		= mtd_write,
	.ioctl		= mtd_ioctl,
	.open		= mtd_open,
	.release	= mtd_close,
835 836 837 838
	.mmap		= mtd_mmap,
#ifndef CONFIG_MMU
	.get_unmapped_area = mtd_get_unmapped_area,
#endif
L
Linus Torvalds 已提交
839 840 841 842
};

static int __init init_mtdchar(void)
{
D
David Brownell 已提交
843 844 845 846
	int status;

	status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops);
	if (status < 0) {
L
Linus Torvalds 已提交
847 848
		printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
		       MTD_CHAR_MAJOR);
849 850
	}

D
David Brownell 已提交
851
	return status;
L
Linus Torvalds 已提交
852 853 854 855 856 857 858 859 860 861
}

static void __exit cleanup_mtdchar(void)
{
	unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
}

module_init(init_mtdchar);
module_exit(cleanup_mtdchar);

D
David Brownell 已提交
862
MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
L
Linus Torvalds 已提交
863 864 865 866

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