mtdchar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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_drvdata(mtd_class, NULL,
  24. MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
  25. NULL, "mtd%d", mtd->index);
  26. device_create_drvdata(mtd_class, NULL,
  27. MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
  28. NULL, "mtd%dro", mtd->index);
  29. }
  30. static void mtd_notify_remove(struct mtd_info* mtd)
  31. {
  32. if (!mtd)
  33. return;
  34. device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
  35. device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
  36. }
  37. static struct mtd_notifier notifier = {
  38. .add = mtd_notify_add,
  39. .remove = mtd_notify_remove,
  40. };
  41. /*
  42. * Data structure to hold the pointer to the mtd device as well
  43. * as mode information ofr various use cases.
  44. */
  45. struct mtd_file_info {
  46. struct mtd_info *mtd;
  47. enum mtd_file_modes mode;
  48. };
  49. static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
  50. {
  51. struct mtd_file_info *mfi = file->private_data;
  52. struct mtd_info *mtd = mfi->mtd;
  53. switch (orig) {
  54. case SEEK_SET:
  55. break;
  56. case SEEK_CUR:
  57. offset += file->f_pos;
  58. break;
  59. case SEEK_END:
  60. offset += mtd->size;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. if (offset >= 0 && offset <= mtd->size)
  66. return file->f_pos = offset;
  67. return -EINVAL;
  68. }
  69. static int mtd_open(struct inode *inode, struct file *file)
  70. {
  71. int minor = iminor(inode);
  72. int devnum = minor >> 1;
  73. int ret = 0;
  74. struct mtd_info *mtd;
  75. struct mtd_file_info *mfi;
  76. DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
  77. if (devnum >= MAX_MTD_DEVICES)
  78. return -ENODEV;
  79. /* You can't open the RO devices RW */
  80. if ((file->f_mode & 2) && (minor & 1))
  81. return -EACCES;
  82. lock_kernel();
  83. mtd = get_mtd_device(NULL, devnum);
  84. if (IS_ERR(mtd)) {
  85. ret = PTR_ERR(mtd);
  86. goto out;
  87. }
  88. if (MTD_ABSENT == mtd->type) {
  89. put_mtd_device(mtd);
  90. ret = -ENODEV;
  91. goto out;
  92. }
  93. /* You can't open it RW if it's not a writeable device */
  94. if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
  95. put_mtd_device(mtd);
  96. ret = -EACCES;
  97. goto out;
  98. }
  99. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  100. if (!mfi) {
  101. put_mtd_device(mtd);
  102. ret = -ENOMEM;
  103. goto out;
  104. }
  105. mfi->mtd = mtd;
  106. file->private_data = mfi;
  107. out:
  108. unlock_kernel();
  109. return ret;
  110. } /* mtd_open */
  111. /*====================================================================*/
  112. static int mtd_close(struct inode *inode, struct file *file)
  113. {
  114. struct mtd_file_info *mfi = file->private_data;
  115. struct mtd_info *mtd = mfi->mtd;
  116. DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
  117. /* Only sync if opened RW */
  118. if ((file->f_mode & 2) && mtd->sync)
  119. mtd->sync(mtd);
  120. put_mtd_device(mtd);
  121. file->private_data = NULL;
  122. kfree(mfi);
  123. return 0;
  124. } /* mtd_close */
  125. /* FIXME: This _really_ needs to die. In 2.5, we should lock the
  126. userspace buffer down and use it directly with readv/writev.
  127. */
  128. #define MAX_KMALLOC_SIZE 0x20000
  129. static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
  130. {
  131. struct mtd_file_info *mfi = file->private_data;
  132. struct mtd_info *mtd = mfi->mtd;
  133. size_t retlen=0;
  134. size_t total_retlen=0;
  135. int ret=0;
  136. int len;
  137. char *kbuf;
  138. DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
  139. if (*ppos + count > mtd->size)
  140. count = mtd->size - *ppos;
  141. if (!count)
  142. return 0;
  143. /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
  144. and pass them directly to the MTD functions */
  145. if (count > MAX_KMALLOC_SIZE)
  146. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  147. else
  148. kbuf=kmalloc(count, GFP_KERNEL);
  149. if (!kbuf)
  150. return -ENOMEM;
  151. while (count) {
  152. if (count > MAX_KMALLOC_SIZE)
  153. len = MAX_KMALLOC_SIZE;
  154. else
  155. len = count;
  156. switch (mfi->mode) {
  157. case MTD_MODE_OTP_FACTORY:
  158. ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  159. break;
  160. case MTD_MODE_OTP_USER:
  161. ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  162. break;
  163. case MTD_MODE_RAW:
  164. {
  165. struct mtd_oob_ops ops;
  166. ops.mode = MTD_OOB_RAW;
  167. ops.datbuf = kbuf;
  168. ops.oobbuf = NULL;
  169. ops.len = len;
  170. ret = mtd->read_oob(mtd, *ppos, &ops);
  171. retlen = ops.retlen;
  172. break;
  173. }
  174. default:
  175. ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
  176. }
  177. /* Nand returns -EBADMSG on ecc errors, but it returns
  178. * the data. For our userspace tools it is important
  179. * to dump areas with ecc errors !
  180. * For kernel internal usage it also might return -EUCLEAN
  181. * to signal the caller that a bitflip has occured and has
  182. * been corrected by the ECC algorithm.
  183. * Userspace software which accesses NAND this way
  184. * must be aware of the fact that it deals with NAND
  185. */
  186. if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
  187. *ppos += retlen;
  188. if (copy_to_user(buf, kbuf, retlen)) {
  189. kfree(kbuf);
  190. return -EFAULT;
  191. }
  192. else
  193. total_retlen += retlen;
  194. count -= retlen;
  195. buf += retlen;
  196. if (retlen == 0)
  197. count = 0;
  198. }
  199. else {
  200. kfree(kbuf);
  201. return ret;
  202. }
  203. }
  204. kfree(kbuf);
  205. return total_retlen;
  206. } /* mtd_read */
  207. static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
  208. {
  209. struct mtd_file_info *mfi = file->private_data;
  210. struct mtd_info *mtd = mfi->mtd;
  211. char *kbuf;
  212. size_t retlen;
  213. size_t total_retlen=0;
  214. int ret=0;
  215. int len;
  216. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  217. if (*ppos == mtd->size)
  218. return -ENOSPC;
  219. if (*ppos + count > mtd->size)
  220. count = mtd->size - *ppos;
  221. if (!count)
  222. return 0;
  223. if (count > MAX_KMALLOC_SIZE)
  224. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  225. else
  226. kbuf=kmalloc(count, GFP_KERNEL);
  227. if (!kbuf)
  228. return -ENOMEM;
  229. while (count) {
  230. if (count > MAX_KMALLOC_SIZE)
  231. len = MAX_KMALLOC_SIZE;
  232. else
  233. len = count;
  234. if (copy_from_user(kbuf, buf, len)) {
  235. kfree(kbuf);
  236. return -EFAULT;
  237. }
  238. switch (mfi->mode) {
  239. case MTD_MODE_OTP_FACTORY:
  240. ret = -EROFS;
  241. break;
  242. case MTD_MODE_OTP_USER:
  243. if (!mtd->write_user_prot_reg) {
  244. ret = -EOPNOTSUPP;
  245. break;
  246. }
  247. ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  248. break;
  249. case MTD_MODE_RAW:
  250. {
  251. struct mtd_oob_ops ops;
  252. ops.mode = MTD_OOB_RAW;
  253. ops.datbuf = kbuf;
  254. ops.oobbuf = NULL;
  255. ops.len = len;
  256. ret = mtd->write_oob(mtd, *ppos, &ops);
  257. retlen = ops.retlen;
  258. break;
  259. }
  260. default:
  261. ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
  262. }
  263. if (!ret) {
  264. *ppos += retlen;
  265. total_retlen += retlen;
  266. count -= retlen;
  267. buf += retlen;
  268. }
  269. else {
  270. kfree(kbuf);
  271. return ret;
  272. }
  273. }
  274. kfree(kbuf);
  275. return total_retlen;
  276. } /* mtd_write */
  277. /*======================================================================
  278. IOCTL calls for getting device parameters.
  279. ======================================================================*/
  280. static void mtdchar_erase_callback (struct erase_info *instr)
  281. {
  282. wake_up((wait_queue_head_t *)instr->priv);
  283. }
  284. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
  285. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  286. {
  287. struct mtd_info *mtd = mfi->mtd;
  288. int ret = 0;
  289. switch (mode) {
  290. case MTD_OTP_FACTORY:
  291. if (!mtd->read_fact_prot_reg)
  292. ret = -EOPNOTSUPP;
  293. else
  294. mfi->mode = MTD_MODE_OTP_FACTORY;
  295. break;
  296. case MTD_OTP_USER:
  297. if (!mtd->read_fact_prot_reg)
  298. ret = -EOPNOTSUPP;
  299. else
  300. mfi->mode = MTD_MODE_OTP_USER;
  301. break;
  302. default:
  303. ret = -EINVAL;
  304. case MTD_OTP_OFF:
  305. break;
  306. }
  307. return ret;
  308. }
  309. #else
  310. # define otp_select_filemode(f,m) -EOPNOTSUPP
  311. #endif
  312. static int mtd_ioctl(struct inode *inode, struct file *file,
  313. u_int cmd, u_long arg)
  314. {
  315. struct mtd_file_info *mfi = file->private_data;
  316. struct mtd_info *mtd = mfi->mtd;
  317. void __user *argp = (void __user *)arg;
  318. int ret = 0;
  319. u_long size;
  320. struct mtd_info_user info;
  321. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  322. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  323. if (cmd & IOC_IN) {
  324. if (!access_ok(VERIFY_READ, argp, size))
  325. return -EFAULT;
  326. }
  327. if (cmd & IOC_OUT) {
  328. if (!access_ok(VERIFY_WRITE, argp, size))
  329. return -EFAULT;
  330. }
  331. switch (cmd) {
  332. case MEMGETREGIONCOUNT:
  333. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  334. return -EFAULT;
  335. break;
  336. case MEMGETREGIONINFO:
  337. {
  338. struct region_info_user ur;
  339. if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
  340. return -EFAULT;
  341. if (ur.regionindex >= mtd->numeraseregions)
  342. return -EINVAL;
  343. if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
  344. sizeof(struct mtd_erase_region_info)))
  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 & 2))
  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 & 2))
  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. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_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");