mtdchar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. uint32_t ur_idx;
  339. struct mtd_erase_region_info *kr;
  340. struct region_info_user *ur = (struct region_info_user *) argp;
  341. if (get_user(ur_idx, &(ur->regionindex)))
  342. return -EFAULT;
  343. kr = &(mtd->eraseregions[ur_idx]);
  344. if (put_user(kr->offset, &(ur->offset))
  345. || put_user(kr->erasesize, &(ur->erasesize))
  346. || put_user(kr->numblocks, &(ur->numblocks)))
  347. return -EFAULT;
  348. break;
  349. }
  350. case MEMGETINFO:
  351. info.type = mtd->type;
  352. info.flags = mtd->flags;
  353. info.size = mtd->size;
  354. info.erasesize = mtd->erasesize;
  355. info.writesize = mtd->writesize;
  356. info.oobsize = mtd->oobsize;
  357. /* The below fields are obsolete */
  358. info.ecctype = -1;
  359. info.eccsize = 0;
  360. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  361. return -EFAULT;
  362. break;
  363. case MEMERASE:
  364. {
  365. struct erase_info *erase;
  366. if(!(file->f_mode & 2))
  367. return -EPERM;
  368. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  369. if (!erase)
  370. ret = -ENOMEM;
  371. else {
  372. wait_queue_head_t waitq;
  373. DECLARE_WAITQUEUE(wait, current);
  374. init_waitqueue_head(&waitq);
  375. if (copy_from_user(&erase->addr, argp,
  376. sizeof(struct erase_info_user))) {
  377. kfree(erase);
  378. return -EFAULT;
  379. }
  380. erase->mtd = mtd;
  381. erase->callback = mtdchar_erase_callback;
  382. erase->priv = (unsigned long)&waitq;
  383. /*
  384. FIXME: Allow INTERRUPTIBLE. Which means
  385. not having the wait_queue head on the stack.
  386. If the wq_head is on the stack, and we
  387. leave because we got interrupted, then the
  388. wq_head is no longer there when the
  389. callback routine tries to wake us up.
  390. */
  391. ret = mtd->erase(mtd, erase);
  392. if (!ret) {
  393. set_current_state(TASK_UNINTERRUPTIBLE);
  394. add_wait_queue(&waitq, &wait);
  395. if (erase->state != MTD_ERASE_DONE &&
  396. erase->state != MTD_ERASE_FAILED)
  397. schedule();
  398. remove_wait_queue(&waitq, &wait);
  399. set_current_state(TASK_RUNNING);
  400. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  401. }
  402. kfree(erase);
  403. }
  404. break;
  405. }
  406. case MEMWRITEOOB:
  407. {
  408. struct mtd_oob_buf buf;
  409. struct mtd_oob_ops ops;
  410. struct mtd_oob_buf __user *user_buf = argp;
  411. uint32_t retlen;
  412. if(!(file->f_mode & 2))
  413. return -EPERM;
  414. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  415. return -EFAULT;
  416. if (buf.length > 4096)
  417. return -EINVAL;
  418. if (!mtd->write_oob)
  419. ret = -EOPNOTSUPP;
  420. else
  421. ret = access_ok(VERIFY_READ, buf.ptr,
  422. buf.length) ? 0 : EFAULT;
  423. if (ret)
  424. return ret;
  425. ops.ooblen = buf.length;
  426. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  427. ops.datbuf = NULL;
  428. ops.mode = MTD_OOB_PLACE;
  429. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  430. return -EINVAL;
  431. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  432. if (!ops.oobbuf)
  433. return -ENOMEM;
  434. if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
  435. kfree(ops.oobbuf);
  436. return -EFAULT;
  437. }
  438. buf.start &= ~(mtd->oobsize - 1);
  439. ret = mtd->write_oob(mtd, buf.start, &ops);
  440. if (ops.oobretlen > 0xFFFFFFFFU)
  441. ret = -EOVERFLOW;
  442. retlen = ops.oobretlen;
  443. if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
  444. ret = -EFAULT;
  445. kfree(ops.oobbuf);
  446. break;
  447. }
  448. case MEMREADOOB:
  449. {
  450. struct mtd_oob_buf buf;
  451. struct mtd_oob_ops ops;
  452. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  453. return -EFAULT;
  454. if (buf.length > 4096)
  455. return -EINVAL;
  456. if (!mtd->read_oob)
  457. ret = -EOPNOTSUPP;
  458. else
  459. ret = access_ok(VERIFY_WRITE, buf.ptr,
  460. buf.length) ? 0 : -EFAULT;
  461. if (ret)
  462. return ret;
  463. ops.ooblen = buf.length;
  464. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  465. ops.datbuf = NULL;
  466. ops.mode = MTD_OOB_PLACE;
  467. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  468. return -EINVAL;
  469. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  470. if (!ops.oobbuf)
  471. return -ENOMEM;
  472. buf.start &= ~(mtd->oobsize - 1);
  473. ret = mtd->read_oob(mtd, buf.start, &ops);
  474. if (put_user(ops.oobretlen, (uint32_t __user *)argp))
  475. ret = -EFAULT;
  476. else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
  477. ops.oobretlen))
  478. ret = -EFAULT;
  479. kfree(ops.oobbuf);
  480. break;
  481. }
  482. case MEMLOCK:
  483. {
  484. struct erase_info_user einfo;
  485. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  486. return -EFAULT;
  487. if (!mtd->lock)
  488. ret = -EOPNOTSUPP;
  489. else
  490. ret = mtd->lock(mtd, einfo.start, einfo.length);
  491. break;
  492. }
  493. case MEMUNLOCK:
  494. {
  495. struct erase_info_user einfo;
  496. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  497. return -EFAULT;
  498. if (!mtd->unlock)
  499. ret = -EOPNOTSUPP;
  500. else
  501. ret = mtd->unlock(mtd, einfo.start, einfo.length);
  502. break;
  503. }
  504. /* Legacy interface */
  505. case MEMGETOOBSEL:
  506. {
  507. struct nand_oobinfo oi;
  508. if (!mtd->ecclayout)
  509. return -EOPNOTSUPP;
  510. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  511. return -EINVAL;
  512. oi.useecc = MTD_NANDECC_AUTOPLACE;
  513. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  514. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  515. sizeof(oi.oobfree));
  516. oi.eccbytes = mtd->ecclayout->eccbytes;
  517. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  518. return -EFAULT;
  519. break;
  520. }
  521. case MEMGETBADBLOCK:
  522. {
  523. loff_t offs;
  524. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  525. return -EFAULT;
  526. if (!mtd->block_isbad)
  527. ret = -EOPNOTSUPP;
  528. else
  529. return mtd->block_isbad(mtd, offs);
  530. break;
  531. }
  532. case MEMSETBADBLOCK:
  533. {
  534. loff_t offs;
  535. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  536. return -EFAULT;
  537. if (!mtd->block_markbad)
  538. ret = -EOPNOTSUPP;
  539. else
  540. return mtd->block_markbad(mtd, offs);
  541. break;
  542. }
  543. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
  544. case OTPSELECT:
  545. {
  546. int mode;
  547. if (copy_from_user(&mode, argp, sizeof(int)))
  548. return -EFAULT;
  549. mfi->mode = MTD_MODE_NORMAL;
  550. ret = otp_select_filemode(mfi, mode);
  551. file->f_pos = 0;
  552. break;
  553. }
  554. case OTPGETREGIONCOUNT:
  555. case OTPGETREGIONINFO:
  556. {
  557. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  558. if (!buf)
  559. return -ENOMEM;
  560. ret = -EOPNOTSUPP;
  561. switch (mfi->mode) {
  562. case MTD_MODE_OTP_FACTORY:
  563. if (mtd->get_fact_prot_info)
  564. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  565. break;
  566. case MTD_MODE_OTP_USER:
  567. if (mtd->get_user_prot_info)
  568. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  569. break;
  570. default:
  571. break;
  572. }
  573. if (ret >= 0) {
  574. if (cmd == OTPGETREGIONCOUNT) {
  575. int nbr = ret / sizeof(struct otp_info);
  576. ret = copy_to_user(argp, &nbr, sizeof(int));
  577. } else
  578. ret = copy_to_user(argp, buf, ret);
  579. if (ret)
  580. ret = -EFAULT;
  581. }
  582. kfree(buf);
  583. break;
  584. }
  585. case OTPLOCK:
  586. {
  587. struct otp_info oinfo;
  588. if (mfi->mode != MTD_MODE_OTP_USER)
  589. return -EINVAL;
  590. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  591. return -EFAULT;
  592. if (!mtd->lock_user_prot_reg)
  593. return -EOPNOTSUPP;
  594. ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  595. break;
  596. }
  597. #endif
  598. case ECCGETLAYOUT:
  599. {
  600. if (!mtd->ecclayout)
  601. return -EOPNOTSUPP;
  602. if (copy_to_user(argp, mtd->ecclayout,
  603. sizeof(struct nand_ecclayout)))
  604. return -EFAULT;
  605. break;
  606. }
  607. case ECCGETSTATS:
  608. {
  609. if (copy_to_user(argp, &mtd->ecc_stats,
  610. sizeof(struct mtd_ecc_stats)))
  611. return -EFAULT;
  612. break;
  613. }
  614. case MTDFILEMODE:
  615. {
  616. mfi->mode = 0;
  617. switch(arg) {
  618. case MTD_MODE_OTP_FACTORY:
  619. case MTD_MODE_OTP_USER:
  620. ret = otp_select_filemode(mfi, arg);
  621. break;
  622. case MTD_MODE_RAW:
  623. if (!mtd->read_oob || !mtd->write_oob)
  624. return -EOPNOTSUPP;
  625. mfi->mode = arg;
  626. case MTD_MODE_NORMAL:
  627. break;
  628. default:
  629. ret = -EINVAL;
  630. }
  631. file->f_pos = 0;
  632. break;
  633. }
  634. default:
  635. ret = -ENOTTY;
  636. }
  637. return ret;
  638. } /* memory_ioctl */
  639. static const struct file_operations mtd_fops = {
  640. .owner = THIS_MODULE,
  641. .llseek = mtd_lseek,
  642. .read = mtd_read,
  643. .write = mtd_write,
  644. .ioctl = mtd_ioctl,
  645. .open = mtd_open,
  646. .release = mtd_close,
  647. };
  648. static int __init init_mtdchar(void)
  649. {
  650. if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
  651. printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
  652. MTD_CHAR_MAJOR);
  653. return -EAGAIN;
  654. }
  655. mtd_class = class_create(THIS_MODULE, "mtd");
  656. if (IS_ERR(mtd_class)) {
  657. printk(KERN_ERR "Error creating mtd class.\n");
  658. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  659. return PTR_ERR(mtd_class);
  660. }
  661. register_mtd_user(&notifier);
  662. return 0;
  663. }
  664. static void __exit cleanup_mtdchar(void)
  665. {
  666. unregister_mtd_user(&notifier);
  667. class_destroy(mtd_class);
  668. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  669. }
  670. module_init(init_mtdchar);
  671. module_exit(cleanup_mtdchar);
  672. MODULE_LICENSE("GPL");
  673. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  674. MODULE_DESCRIPTION("Direct character-device access to MTD devices");