mtdchar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Character-device access to raw MTD devices.
  3. *
  4. */
  5. #include <linux/device.h>
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/compatmac.h>
  17. #include <asm/uaccess.h>
  18. static struct class *mtd_class;
  19. static void mtd_notify_add(struct mtd_info* mtd)
  20. {
  21. if (!mtd)
  22. return;
  23. device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
  24. NULL, "mtd%d", mtd->index);
  25. device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
  26. NULL, "mtd%dro", mtd->index);
  27. }
  28. static void mtd_notify_remove(struct mtd_info* mtd)
  29. {
  30. if (!mtd)
  31. return;
  32. device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
  33. device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
  34. }
  35. static struct mtd_notifier notifier = {
  36. .add = mtd_notify_add,
  37. .remove = mtd_notify_remove,
  38. };
  39. /*
  40. * Data structure to hold the pointer to the mtd device as well
  41. * as mode information ofr various use cases.
  42. */
  43. struct mtd_file_info {
  44. struct mtd_info *mtd;
  45. enum mtd_file_modes mode;
  46. };
  47. static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
  48. {
  49. struct mtd_file_info *mfi = file->private_data;
  50. struct mtd_info *mtd = mfi->mtd;
  51. switch (orig) {
  52. case SEEK_SET:
  53. break;
  54. case SEEK_CUR:
  55. offset += file->f_pos;
  56. break;
  57. case SEEK_END:
  58. offset += mtd->size;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. if (offset >= 0 && offset <= mtd->size)
  64. return file->f_pos = offset;
  65. return -EINVAL;
  66. }
  67. static int mtd_open(struct inode *inode, struct file *file)
  68. {
  69. int minor = iminor(inode);
  70. int devnum = minor >> 1;
  71. int ret = 0;
  72. struct mtd_info *mtd;
  73. struct mtd_file_info *mfi;
  74. DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
  75. if (devnum >= MAX_MTD_DEVICES)
  76. return -ENODEV;
  77. /* You can't open the RO devices RW */
  78. if ((file->f_mode & FMODE_WRITE) && (minor & 1))
  79. return -EACCES;
  80. lock_kernel();
  81. mtd = get_mtd_device(NULL, devnum);
  82. if (IS_ERR(mtd)) {
  83. ret = PTR_ERR(mtd);
  84. goto out;
  85. }
  86. if (MTD_ABSENT == mtd->type) {
  87. put_mtd_device(mtd);
  88. ret = -ENODEV;
  89. goto out;
  90. }
  91. /* You can't open it RW if it's not a writeable device */
  92. if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
  93. put_mtd_device(mtd);
  94. ret = -EACCES;
  95. goto out;
  96. }
  97. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  98. if (!mfi) {
  99. put_mtd_device(mtd);
  100. ret = -ENOMEM;
  101. goto out;
  102. }
  103. mfi->mtd = mtd;
  104. file->private_data = mfi;
  105. out:
  106. unlock_kernel();
  107. return ret;
  108. } /* mtd_open */
  109. /*====================================================================*/
  110. static int mtd_close(struct inode *inode, struct file *file)
  111. {
  112. struct mtd_file_info *mfi = file->private_data;
  113. struct mtd_info *mtd = mfi->mtd;
  114. DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
  115. /* Only sync if opened RW */
  116. if ((file->f_mode & FMODE_WRITE) && mtd->sync)
  117. mtd->sync(mtd);
  118. put_mtd_device(mtd);
  119. file->private_data = NULL;
  120. kfree(mfi);
  121. return 0;
  122. } /* mtd_close */
  123. /* FIXME: This _really_ needs to die. In 2.5, we should lock the
  124. userspace buffer down and use it directly with readv/writev.
  125. */
  126. #define MAX_KMALLOC_SIZE 0x20000
  127. static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
  128. {
  129. struct mtd_file_info *mfi = file->private_data;
  130. struct mtd_info *mtd = mfi->mtd;
  131. size_t retlen=0;
  132. size_t total_retlen=0;
  133. int ret=0;
  134. int len;
  135. char *kbuf;
  136. DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
  137. if (*ppos + count > mtd->size)
  138. count = mtd->size - *ppos;
  139. if (!count)
  140. return 0;
  141. /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
  142. and pass them directly to the MTD functions */
  143. if (count > MAX_KMALLOC_SIZE)
  144. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  145. else
  146. kbuf=kmalloc(count, GFP_KERNEL);
  147. if (!kbuf)
  148. return -ENOMEM;
  149. while (count) {
  150. if (count > MAX_KMALLOC_SIZE)
  151. len = MAX_KMALLOC_SIZE;
  152. else
  153. len = count;
  154. switch (mfi->mode) {
  155. case MTD_MODE_OTP_FACTORY:
  156. ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  157. break;
  158. case MTD_MODE_OTP_USER:
  159. ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  160. break;
  161. case MTD_MODE_RAW:
  162. {
  163. struct mtd_oob_ops ops;
  164. ops.mode = MTD_OOB_RAW;
  165. ops.datbuf = kbuf;
  166. ops.oobbuf = NULL;
  167. ops.len = len;
  168. ret = mtd->read_oob(mtd, *ppos, &ops);
  169. retlen = ops.retlen;
  170. break;
  171. }
  172. default:
  173. ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
  174. }
  175. /* Nand returns -EBADMSG on ecc errors, but it returns
  176. * the data. For our userspace tools it is important
  177. * to dump areas with ecc errors !
  178. * For kernel internal usage it also might return -EUCLEAN
  179. * to signal the caller that a bitflip has occured and has
  180. * been corrected by the ECC algorithm.
  181. * Userspace software which accesses NAND this way
  182. * must be aware of the fact that it deals with NAND
  183. */
  184. if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
  185. *ppos += retlen;
  186. if (copy_to_user(buf, kbuf, retlen)) {
  187. kfree(kbuf);
  188. return -EFAULT;
  189. }
  190. else
  191. total_retlen += retlen;
  192. count -= retlen;
  193. buf += retlen;
  194. if (retlen == 0)
  195. count = 0;
  196. }
  197. else {
  198. kfree(kbuf);
  199. return ret;
  200. }
  201. }
  202. kfree(kbuf);
  203. return total_retlen;
  204. } /* mtd_read */
  205. static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
  206. {
  207. struct mtd_file_info *mfi = file->private_data;
  208. struct mtd_info *mtd = mfi->mtd;
  209. char *kbuf;
  210. size_t retlen;
  211. size_t total_retlen=0;
  212. int ret=0;
  213. int len;
  214. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  215. if (*ppos == mtd->size)
  216. return -ENOSPC;
  217. if (*ppos + count > mtd->size)
  218. count = mtd->size - *ppos;
  219. if (!count)
  220. return 0;
  221. if (count > MAX_KMALLOC_SIZE)
  222. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  223. else
  224. kbuf=kmalloc(count, GFP_KERNEL);
  225. if (!kbuf)
  226. return -ENOMEM;
  227. while (count) {
  228. if (count > MAX_KMALLOC_SIZE)
  229. len = MAX_KMALLOC_SIZE;
  230. else
  231. len = count;
  232. if (copy_from_user(kbuf, buf, len)) {
  233. kfree(kbuf);
  234. return -EFAULT;
  235. }
  236. switch (mfi->mode) {
  237. case MTD_MODE_OTP_FACTORY:
  238. ret = -EROFS;
  239. break;
  240. case MTD_MODE_OTP_USER:
  241. if (!mtd->write_user_prot_reg) {
  242. ret = -EOPNOTSUPP;
  243. break;
  244. }
  245. ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  246. break;
  247. case MTD_MODE_RAW:
  248. {
  249. struct mtd_oob_ops ops;
  250. ops.mode = MTD_OOB_RAW;
  251. ops.datbuf = kbuf;
  252. ops.oobbuf = NULL;
  253. ops.len = len;
  254. ret = mtd->write_oob(mtd, *ppos, &ops);
  255. retlen = ops.retlen;
  256. break;
  257. }
  258. default:
  259. ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
  260. }
  261. if (!ret) {
  262. *ppos += retlen;
  263. total_retlen += retlen;
  264. count -= retlen;
  265. buf += retlen;
  266. }
  267. else {
  268. kfree(kbuf);
  269. return ret;
  270. }
  271. }
  272. kfree(kbuf);
  273. return total_retlen;
  274. } /* mtd_write */
  275. /*======================================================================
  276. IOCTL calls for getting device parameters.
  277. ======================================================================*/
  278. static void mtdchar_erase_callback (struct erase_info *instr)
  279. {
  280. wake_up((wait_queue_head_t *)instr->priv);
  281. }
  282. #ifdef CONFIG_HAVE_MTD_OTP
  283. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  284. {
  285. struct mtd_info *mtd = mfi->mtd;
  286. int ret = 0;
  287. switch (mode) {
  288. case MTD_OTP_FACTORY:
  289. if (!mtd->read_fact_prot_reg)
  290. ret = -EOPNOTSUPP;
  291. else
  292. mfi->mode = MTD_MODE_OTP_FACTORY;
  293. break;
  294. case MTD_OTP_USER:
  295. if (!mtd->read_fact_prot_reg)
  296. ret = -EOPNOTSUPP;
  297. else
  298. mfi->mode = MTD_MODE_OTP_USER;
  299. break;
  300. default:
  301. ret = -EINVAL;
  302. case MTD_OTP_OFF:
  303. break;
  304. }
  305. return ret;
  306. }
  307. #else
  308. # define otp_select_filemode(f,m) -EOPNOTSUPP
  309. #endif
  310. static int mtd_ioctl(struct inode *inode, struct file *file,
  311. u_int cmd, u_long arg)
  312. {
  313. struct mtd_file_info *mfi = file->private_data;
  314. struct mtd_info *mtd = mfi->mtd;
  315. void __user *argp = (void __user *)arg;
  316. int ret = 0;
  317. u_long size;
  318. struct mtd_info_user info;
  319. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  320. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  321. if (cmd & IOC_IN) {
  322. if (!access_ok(VERIFY_READ, argp, size))
  323. return -EFAULT;
  324. }
  325. if (cmd & IOC_OUT) {
  326. if (!access_ok(VERIFY_WRITE, argp, size))
  327. return -EFAULT;
  328. }
  329. switch (cmd) {
  330. case MEMGETREGIONCOUNT:
  331. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  332. return -EFAULT;
  333. break;
  334. case MEMGETREGIONINFO:
  335. {
  336. uint32_t ur_idx;
  337. struct mtd_erase_region_info *kr;
  338. struct region_info_user *ur = (struct region_info_user *) argp;
  339. if (get_user(ur_idx, &(ur->regionindex)))
  340. return -EFAULT;
  341. kr = &(mtd->eraseregions[ur_idx]);
  342. if (put_user(kr->offset, &(ur->offset))
  343. || put_user(kr->erasesize, &(ur->erasesize))
  344. || put_user(kr->numblocks, &(ur->numblocks)))
  345. return -EFAULT;
  346. break;
  347. }
  348. case MEMGETINFO:
  349. info.type = mtd->type;
  350. info.flags = mtd->flags;
  351. info.size = mtd->size;
  352. info.erasesize = mtd->erasesize;
  353. info.writesize = mtd->writesize;
  354. info.oobsize = mtd->oobsize;
  355. /* The below fields are obsolete */
  356. info.ecctype = -1;
  357. info.eccsize = 0;
  358. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  359. return -EFAULT;
  360. break;
  361. case MEMERASE:
  362. {
  363. struct erase_info *erase;
  364. if(!(file->f_mode & FMODE_WRITE))
  365. return -EPERM;
  366. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  367. if (!erase)
  368. ret = -ENOMEM;
  369. else {
  370. wait_queue_head_t waitq;
  371. DECLARE_WAITQUEUE(wait, current);
  372. init_waitqueue_head(&waitq);
  373. if (copy_from_user(&erase->addr, argp,
  374. sizeof(struct erase_info_user))) {
  375. kfree(erase);
  376. return -EFAULT;
  377. }
  378. erase->mtd = mtd;
  379. erase->callback = mtdchar_erase_callback;
  380. erase->priv = (unsigned long)&waitq;
  381. /*
  382. FIXME: Allow INTERRUPTIBLE. Which means
  383. not having the wait_queue head on the stack.
  384. If the wq_head is on the stack, and we
  385. leave because we got interrupted, then the
  386. wq_head is no longer there when the
  387. callback routine tries to wake us up.
  388. */
  389. ret = mtd->erase(mtd, erase);
  390. if (!ret) {
  391. set_current_state(TASK_UNINTERRUPTIBLE);
  392. add_wait_queue(&waitq, &wait);
  393. if (erase->state != MTD_ERASE_DONE &&
  394. erase->state != MTD_ERASE_FAILED)
  395. schedule();
  396. remove_wait_queue(&waitq, &wait);
  397. set_current_state(TASK_RUNNING);
  398. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  399. }
  400. kfree(erase);
  401. }
  402. break;
  403. }
  404. case MEMWRITEOOB:
  405. {
  406. struct mtd_oob_buf buf;
  407. struct mtd_oob_ops ops;
  408. struct mtd_oob_buf __user *user_buf = argp;
  409. uint32_t retlen;
  410. if(!(file->f_mode & FMODE_WRITE))
  411. return -EPERM;
  412. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  413. return -EFAULT;
  414. if (buf.length > 4096)
  415. return -EINVAL;
  416. if (!mtd->write_oob)
  417. ret = -EOPNOTSUPP;
  418. else
  419. ret = access_ok(VERIFY_READ, buf.ptr,
  420. buf.length) ? 0 : EFAULT;
  421. if (ret)
  422. return ret;
  423. ops.ooblen = buf.length;
  424. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  425. ops.datbuf = NULL;
  426. ops.mode = MTD_OOB_PLACE;
  427. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  428. return -EINVAL;
  429. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  430. if (!ops.oobbuf)
  431. return -ENOMEM;
  432. if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
  433. kfree(ops.oobbuf);
  434. return -EFAULT;
  435. }
  436. buf.start &= ~(mtd->oobsize - 1);
  437. ret = mtd->write_oob(mtd, buf.start, &ops);
  438. if (ops.oobretlen > 0xFFFFFFFFU)
  439. ret = -EOVERFLOW;
  440. retlen = ops.oobretlen;
  441. if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
  442. ret = -EFAULT;
  443. kfree(ops.oobbuf);
  444. break;
  445. }
  446. case MEMREADOOB:
  447. {
  448. struct mtd_oob_buf buf;
  449. struct mtd_oob_ops ops;
  450. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  451. return -EFAULT;
  452. if (buf.length > 4096)
  453. return -EINVAL;
  454. if (!mtd->read_oob)
  455. ret = -EOPNOTSUPP;
  456. else
  457. ret = access_ok(VERIFY_WRITE, buf.ptr,
  458. buf.length) ? 0 : -EFAULT;
  459. if (ret)
  460. return ret;
  461. ops.ooblen = buf.length;
  462. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  463. ops.datbuf = NULL;
  464. ops.mode = MTD_OOB_PLACE;
  465. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  466. return -EINVAL;
  467. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  468. if (!ops.oobbuf)
  469. return -ENOMEM;
  470. buf.start &= ~(mtd->oobsize - 1);
  471. ret = mtd->read_oob(mtd, buf.start, &ops);
  472. if (put_user(ops.oobretlen, (uint32_t __user *)argp))
  473. ret = -EFAULT;
  474. else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
  475. ops.oobretlen))
  476. ret = -EFAULT;
  477. kfree(ops.oobbuf);
  478. break;
  479. }
  480. case MEMLOCK:
  481. {
  482. struct erase_info_user einfo;
  483. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  484. return -EFAULT;
  485. if (!mtd->lock)
  486. ret = -EOPNOTSUPP;
  487. else
  488. ret = mtd->lock(mtd, einfo.start, einfo.length);
  489. break;
  490. }
  491. case MEMUNLOCK:
  492. {
  493. struct erase_info_user einfo;
  494. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  495. return -EFAULT;
  496. if (!mtd->unlock)
  497. ret = -EOPNOTSUPP;
  498. else
  499. ret = mtd->unlock(mtd, einfo.start, einfo.length);
  500. break;
  501. }
  502. /* Legacy interface */
  503. case MEMGETOOBSEL:
  504. {
  505. struct nand_oobinfo oi;
  506. if (!mtd->ecclayout)
  507. return -EOPNOTSUPP;
  508. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  509. return -EINVAL;
  510. oi.useecc = MTD_NANDECC_AUTOPLACE;
  511. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  512. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  513. sizeof(oi.oobfree));
  514. oi.eccbytes = mtd->ecclayout->eccbytes;
  515. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  516. return -EFAULT;
  517. break;
  518. }
  519. case MEMGETBADBLOCK:
  520. {
  521. loff_t offs;
  522. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  523. return -EFAULT;
  524. if (!mtd->block_isbad)
  525. ret = -EOPNOTSUPP;
  526. else
  527. return mtd->block_isbad(mtd, offs);
  528. break;
  529. }
  530. case MEMSETBADBLOCK:
  531. {
  532. loff_t offs;
  533. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  534. return -EFAULT;
  535. if (!mtd->block_markbad)
  536. ret = -EOPNOTSUPP;
  537. else
  538. return mtd->block_markbad(mtd, offs);
  539. break;
  540. }
  541. #ifdef CONFIG_HAVE_MTD_OTP
  542. case OTPSELECT:
  543. {
  544. int mode;
  545. if (copy_from_user(&mode, argp, sizeof(int)))
  546. return -EFAULT;
  547. mfi->mode = MTD_MODE_NORMAL;
  548. ret = otp_select_filemode(mfi, mode);
  549. file->f_pos = 0;
  550. break;
  551. }
  552. case OTPGETREGIONCOUNT:
  553. case OTPGETREGIONINFO:
  554. {
  555. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  556. if (!buf)
  557. return -ENOMEM;
  558. ret = -EOPNOTSUPP;
  559. switch (mfi->mode) {
  560. case MTD_MODE_OTP_FACTORY:
  561. if (mtd->get_fact_prot_info)
  562. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  563. break;
  564. case MTD_MODE_OTP_USER:
  565. if (mtd->get_user_prot_info)
  566. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  567. break;
  568. default:
  569. break;
  570. }
  571. if (ret >= 0) {
  572. if (cmd == OTPGETREGIONCOUNT) {
  573. int nbr = ret / sizeof(struct otp_info);
  574. ret = copy_to_user(argp, &nbr, sizeof(int));
  575. } else
  576. ret = copy_to_user(argp, buf, ret);
  577. if (ret)
  578. ret = -EFAULT;
  579. }
  580. kfree(buf);
  581. break;
  582. }
  583. case OTPLOCK:
  584. {
  585. struct otp_info oinfo;
  586. if (mfi->mode != MTD_MODE_OTP_USER)
  587. return -EINVAL;
  588. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  589. return -EFAULT;
  590. if (!mtd->lock_user_prot_reg)
  591. return -EOPNOTSUPP;
  592. ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  593. break;
  594. }
  595. #endif
  596. case ECCGETLAYOUT:
  597. {
  598. if (!mtd->ecclayout)
  599. return -EOPNOTSUPP;
  600. if (copy_to_user(argp, mtd->ecclayout,
  601. sizeof(struct nand_ecclayout)))
  602. return -EFAULT;
  603. break;
  604. }
  605. case ECCGETSTATS:
  606. {
  607. if (copy_to_user(argp, &mtd->ecc_stats,
  608. sizeof(struct mtd_ecc_stats)))
  609. return -EFAULT;
  610. break;
  611. }
  612. case MTDFILEMODE:
  613. {
  614. mfi->mode = 0;
  615. switch(arg) {
  616. case MTD_MODE_OTP_FACTORY:
  617. case MTD_MODE_OTP_USER:
  618. ret = otp_select_filemode(mfi, arg);
  619. break;
  620. case MTD_MODE_RAW:
  621. if (!mtd->read_oob || !mtd->write_oob)
  622. return -EOPNOTSUPP;
  623. mfi->mode = arg;
  624. case MTD_MODE_NORMAL:
  625. break;
  626. default:
  627. ret = -EINVAL;
  628. }
  629. file->f_pos = 0;
  630. break;
  631. }
  632. default:
  633. ret = -ENOTTY;
  634. }
  635. return ret;
  636. } /* memory_ioctl */
  637. static const struct file_operations mtd_fops = {
  638. .owner = THIS_MODULE,
  639. .llseek = mtd_lseek,
  640. .read = mtd_read,
  641. .write = mtd_write,
  642. .ioctl = mtd_ioctl,
  643. .open = mtd_open,
  644. .release = mtd_close,
  645. };
  646. static int __init init_mtdchar(void)
  647. {
  648. if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
  649. printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
  650. MTD_CHAR_MAJOR);
  651. return -EAGAIN;
  652. }
  653. mtd_class = class_create(THIS_MODULE, "mtd");
  654. if (IS_ERR(mtd_class)) {
  655. printk(KERN_ERR "Error creating mtd class.\n");
  656. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  657. return PTR_ERR(mtd_class);
  658. }
  659. register_mtd_user(&notifier);
  660. return 0;
  661. }
  662. static void __exit cleanup_mtdchar(void)
  663. {
  664. unregister_mtd_user(&notifier);
  665. class_destroy(mtd_class);
  666. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  667. }
  668. module_init(init_mtdchar);
  669. module_exit(cleanup_mtdchar);
  670. MODULE_LICENSE("GPL");
  671. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  672. MODULE_DESCRIPTION("Direct character-device access to MTD devices");