mtdchar.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/compat.h>
  31. #include <linux/mount.h>
  32. #include <linux/mtd/mtd.h>
  33. #include <linux/mtd/map.h>
  34. #include <asm/uaccess.h>
  35. #define MTD_INODE_FS_MAGIC 0x11307854
  36. static struct vfsmount *mtd_inode_mnt __read_mostly;
  37. /*
  38. * Data structure to hold the pointer to the mtd device as well
  39. * as mode information ofr various use cases.
  40. */
  41. struct mtd_file_info {
  42. struct mtd_info *mtd;
  43. struct inode *ino;
  44. enum mtd_file_modes mode;
  45. };
  46. static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
  47. {
  48. struct mtd_file_info *mfi = file->private_data;
  49. struct mtd_info *mtd = mfi->mtd;
  50. switch (orig) {
  51. case SEEK_SET:
  52. break;
  53. case SEEK_CUR:
  54. offset += file->f_pos;
  55. break;
  56. case SEEK_END:
  57. offset += mtd->size;
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. if (offset >= 0 && offset <= mtd->size)
  63. return file->f_pos = offset;
  64. return -EINVAL;
  65. }
  66. static int mtd_open(struct inode *inode, struct file *file)
  67. {
  68. int minor = iminor(inode);
  69. int devnum = minor >> 1;
  70. int ret = 0;
  71. struct mtd_info *mtd;
  72. struct mtd_file_info *mfi;
  73. struct inode *mtd_ino;
  74. DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
  75. /* You can't open the RO devices RW */
  76. if ((file->f_mode & FMODE_WRITE) && (minor & 1))
  77. return -EACCES;
  78. lock_kernel();
  79. mtd = get_mtd_device(NULL, devnum);
  80. if (IS_ERR(mtd)) {
  81. ret = PTR_ERR(mtd);
  82. goto out;
  83. }
  84. if (mtd->type == MTD_ABSENT) {
  85. put_mtd_device(mtd);
  86. ret = -ENODEV;
  87. goto out;
  88. }
  89. mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
  90. if (!mtd_ino) {
  91. put_mtd_device(mtd);
  92. ret = -ENOMEM;
  93. goto out;
  94. }
  95. if (mtd_ino->i_state & I_NEW) {
  96. mtd_ino->i_private = mtd;
  97. mtd_ino->i_mode = S_IFCHR;
  98. mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
  99. unlock_new_inode(mtd_ino);
  100. }
  101. file->f_mapping = mtd_ino->i_mapping;
  102. /* You can't open it RW if it's not a writeable device */
  103. if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
  104. iput(mtd_ino);
  105. put_mtd_device(mtd);
  106. ret = -EACCES;
  107. goto out;
  108. }
  109. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  110. if (!mfi) {
  111. iput(mtd_ino);
  112. put_mtd_device(mtd);
  113. ret = -ENOMEM;
  114. goto out;
  115. }
  116. mfi->ino = mtd_ino;
  117. mfi->mtd = mtd;
  118. file->private_data = mfi;
  119. out:
  120. unlock_kernel();
  121. return ret;
  122. } /* mtd_open */
  123. /*====================================================================*/
  124. static int mtd_close(struct inode *inode, struct file *file)
  125. {
  126. struct mtd_file_info *mfi = file->private_data;
  127. struct mtd_info *mtd = mfi->mtd;
  128. DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
  129. /* Only sync if opened RW */
  130. if ((file->f_mode & FMODE_WRITE) && mtd->sync)
  131. mtd->sync(mtd);
  132. iput(mfi->ino);
  133. put_mtd_device(mtd);
  134. file->private_data = NULL;
  135. kfree(mfi);
  136. return 0;
  137. } /* mtd_close */
  138. /* FIXME: This _really_ needs to die. In 2.5, we should lock the
  139. userspace buffer down and use it directly with readv/writev.
  140. */
  141. #define MAX_KMALLOC_SIZE 0x20000
  142. static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
  143. {
  144. struct mtd_file_info *mfi = file->private_data;
  145. struct mtd_info *mtd = mfi->mtd;
  146. size_t retlen=0;
  147. size_t total_retlen=0;
  148. int ret=0;
  149. int len;
  150. char *kbuf;
  151. DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
  152. if (*ppos + count > mtd->size)
  153. count = mtd->size - *ppos;
  154. if (!count)
  155. return 0;
  156. /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
  157. and pass them directly to the MTD functions */
  158. if (count > MAX_KMALLOC_SIZE)
  159. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  160. else
  161. kbuf=kmalloc(count, GFP_KERNEL);
  162. if (!kbuf)
  163. return -ENOMEM;
  164. while (count) {
  165. if (count > MAX_KMALLOC_SIZE)
  166. len = MAX_KMALLOC_SIZE;
  167. else
  168. len = count;
  169. switch (mfi->mode) {
  170. case MTD_MODE_OTP_FACTORY:
  171. ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  172. break;
  173. case MTD_MODE_OTP_USER:
  174. ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  175. break;
  176. case MTD_MODE_RAW:
  177. {
  178. struct mtd_oob_ops ops;
  179. ops.mode = MTD_OOB_RAW;
  180. ops.datbuf = kbuf;
  181. ops.oobbuf = NULL;
  182. ops.len = len;
  183. ret = mtd->read_oob(mtd, *ppos, &ops);
  184. retlen = ops.retlen;
  185. break;
  186. }
  187. default:
  188. ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
  189. }
  190. /* Nand returns -EBADMSG on ecc errors, but it returns
  191. * the data. For our userspace tools it is important
  192. * to dump areas with ecc errors !
  193. * For kernel internal usage it also might return -EUCLEAN
  194. * to signal the caller that a bitflip has occured and has
  195. * been corrected by the ECC algorithm.
  196. * Userspace software which accesses NAND this way
  197. * must be aware of the fact that it deals with NAND
  198. */
  199. if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
  200. *ppos += retlen;
  201. if (copy_to_user(buf, kbuf, retlen)) {
  202. kfree(kbuf);
  203. return -EFAULT;
  204. }
  205. else
  206. total_retlen += retlen;
  207. count -= retlen;
  208. buf += retlen;
  209. if (retlen == 0)
  210. count = 0;
  211. }
  212. else {
  213. kfree(kbuf);
  214. return ret;
  215. }
  216. }
  217. kfree(kbuf);
  218. return total_retlen;
  219. } /* mtd_read */
  220. static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
  221. {
  222. struct mtd_file_info *mfi = file->private_data;
  223. struct mtd_info *mtd = mfi->mtd;
  224. char *kbuf;
  225. size_t retlen;
  226. size_t total_retlen=0;
  227. int ret=0;
  228. int len;
  229. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  230. if (*ppos == mtd->size)
  231. return -ENOSPC;
  232. if (*ppos + count > mtd->size)
  233. count = mtd->size - *ppos;
  234. if (!count)
  235. return 0;
  236. if (count > MAX_KMALLOC_SIZE)
  237. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  238. else
  239. kbuf=kmalloc(count, GFP_KERNEL);
  240. if (!kbuf)
  241. return -ENOMEM;
  242. while (count) {
  243. if (count > MAX_KMALLOC_SIZE)
  244. len = MAX_KMALLOC_SIZE;
  245. else
  246. len = count;
  247. if (copy_from_user(kbuf, buf, len)) {
  248. kfree(kbuf);
  249. return -EFAULT;
  250. }
  251. switch (mfi->mode) {
  252. case MTD_MODE_OTP_FACTORY:
  253. ret = -EROFS;
  254. break;
  255. case MTD_MODE_OTP_USER:
  256. if (!mtd->write_user_prot_reg) {
  257. ret = -EOPNOTSUPP;
  258. break;
  259. }
  260. ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  261. break;
  262. case MTD_MODE_RAW:
  263. {
  264. struct mtd_oob_ops ops;
  265. ops.mode = MTD_OOB_RAW;
  266. ops.datbuf = kbuf;
  267. ops.oobbuf = NULL;
  268. ops.len = len;
  269. ret = mtd->write_oob(mtd, *ppos, &ops);
  270. retlen = ops.retlen;
  271. break;
  272. }
  273. default:
  274. ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
  275. }
  276. if (!ret) {
  277. *ppos += retlen;
  278. total_retlen += retlen;
  279. count -= retlen;
  280. buf += retlen;
  281. }
  282. else {
  283. kfree(kbuf);
  284. return ret;
  285. }
  286. }
  287. kfree(kbuf);
  288. return total_retlen;
  289. } /* mtd_write */
  290. /*======================================================================
  291. IOCTL calls for getting device parameters.
  292. ======================================================================*/
  293. static void mtdchar_erase_callback (struct erase_info *instr)
  294. {
  295. wake_up((wait_queue_head_t *)instr->priv);
  296. }
  297. #ifdef CONFIG_HAVE_MTD_OTP
  298. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  299. {
  300. struct mtd_info *mtd = mfi->mtd;
  301. int ret = 0;
  302. switch (mode) {
  303. case MTD_OTP_FACTORY:
  304. if (!mtd->read_fact_prot_reg)
  305. ret = -EOPNOTSUPP;
  306. else
  307. mfi->mode = MTD_MODE_OTP_FACTORY;
  308. break;
  309. case MTD_OTP_USER:
  310. if (!mtd->read_fact_prot_reg)
  311. ret = -EOPNOTSUPP;
  312. else
  313. mfi->mode = MTD_MODE_OTP_USER;
  314. break;
  315. default:
  316. ret = -EINVAL;
  317. case MTD_OTP_OFF:
  318. break;
  319. }
  320. return ret;
  321. }
  322. #else
  323. # define otp_select_filemode(f,m) -EOPNOTSUPP
  324. #endif
  325. static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
  326. uint64_t start, uint32_t length, void __user *ptr,
  327. uint32_t __user *retp)
  328. {
  329. struct mtd_oob_ops ops;
  330. uint32_t retlen;
  331. int ret = 0;
  332. if (!(file->f_mode & FMODE_WRITE))
  333. return -EPERM;
  334. if (length > 4096)
  335. return -EINVAL;
  336. if (!mtd->write_oob)
  337. ret = -EOPNOTSUPP;
  338. else
  339. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  340. if (ret)
  341. return ret;
  342. ops.ooblen = length;
  343. ops.ooboffs = start & (mtd->oobsize - 1);
  344. ops.datbuf = NULL;
  345. ops.mode = MTD_OOB_PLACE;
  346. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  347. return -EINVAL;
  348. ops.oobbuf = memdup_user(ptr, length);
  349. if (IS_ERR(ops.oobbuf))
  350. return PTR_ERR(ops.oobbuf);
  351. start &= ~((uint64_t)mtd->oobsize - 1);
  352. ret = mtd->write_oob(mtd, start, &ops);
  353. if (ops.oobretlen > 0xFFFFFFFFU)
  354. ret = -EOVERFLOW;
  355. retlen = ops.oobretlen;
  356. if (copy_to_user(retp, &retlen, sizeof(length)))
  357. ret = -EFAULT;
  358. kfree(ops.oobbuf);
  359. return ret;
  360. }
  361. static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
  362. uint32_t length, void __user *ptr, uint32_t __user *retp)
  363. {
  364. struct mtd_oob_ops ops;
  365. int ret = 0;
  366. if (length > 4096)
  367. return -EINVAL;
  368. if (!mtd->read_oob)
  369. ret = -EOPNOTSUPP;
  370. else
  371. ret = access_ok(VERIFY_WRITE, ptr,
  372. length) ? 0 : -EFAULT;
  373. if (ret)
  374. return ret;
  375. ops.ooblen = length;
  376. ops.ooboffs = start & (mtd->oobsize - 1);
  377. ops.datbuf = NULL;
  378. ops.mode = MTD_OOB_PLACE;
  379. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  380. return -EINVAL;
  381. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  382. if (!ops.oobbuf)
  383. return -ENOMEM;
  384. start &= ~((uint64_t)mtd->oobsize - 1);
  385. ret = mtd->read_oob(mtd, start, &ops);
  386. if (put_user(ops.oobretlen, retp))
  387. ret = -EFAULT;
  388. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  389. ops.oobretlen))
  390. ret = -EFAULT;
  391. kfree(ops.oobbuf);
  392. return ret;
  393. }
  394. /*
  395. * Copies (and truncates, if necessary) data from the larger struct,
  396. * nand_ecclayout, to the smaller, deprecated layout struct,
  397. * nand_ecclayout_user. This is necessary only to suppport the deprecated
  398. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  399. * nand_ecclayout flexibly (i.e. the struct may change size in new
  400. * releases without requiring major rewrites).
  401. */
  402. static int shrink_ecclayout(const struct nand_ecclayout *from,
  403. struct nand_ecclayout_user *to)
  404. {
  405. int i;
  406. if (!from || !to)
  407. return -EINVAL;
  408. memset(to, 0, sizeof(*to));
  409. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  410. for (i = 0; i < to->eccbytes; i++)
  411. to->eccpos[i] = from->eccpos[i];
  412. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  413. if (from->oobfree[i].length == 0 &&
  414. from->oobfree[i].offset == 0)
  415. break;
  416. to->oobavail += from->oobfree[i].length;
  417. to->oobfree[i] = from->oobfree[i];
  418. }
  419. return 0;
  420. }
  421. static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
  422. {
  423. struct mtd_file_info *mfi = file->private_data;
  424. struct mtd_info *mtd = mfi->mtd;
  425. void __user *argp = (void __user *)arg;
  426. int ret = 0;
  427. u_long size;
  428. struct mtd_info_user info;
  429. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  430. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  431. if (cmd & IOC_IN) {
  432. if (!access_ok(VERIFY_READ, argp, size))
  433. return -EFAULT;
  434. }
  435. if (cmd & IOC_OUT) {
  436. if (!access_ok(VERIFY_WRITE, argp, size))
  437. return -EFAULT;
  438. }
  439. switch (cmd) {
  440. case MEMGETREGIONCOUNT:
  441. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  442. return -EFAULT;
  443. break;
  444. case MEMGETREGIONINFO:
  445. {
  446. uint32_t ur_idx;
  447. struct mtd_erase_region_info *kr;
  448. struct region_info_user __user *ur = argp;
  449. if (get_user(ur_idx, &(ur->regionindex)))
  450. return -EFAULT;
  451. kr = &(mtd->eraseregions[ur_idx]);
  452. if (put_user(kr->offset, &(ur->offset))
  453. || put_user(kr->erasesize, &(ur->erasesize))
  454. || put_user(kr->numblocks, &(ur->numblocks)))
  455. return -EFAULT;
  456. break;
  457. }
  458. case MEMGETINFO:
  459. info.type = mtd->type;
  460. info.flags = mtd->flags;
  461. info.size = mtd->size;
  462. info.erasesize = mtd->erasesize;
  463. info.writesize = mtd->writesize;
  464. info.oobsize = mtd->oobsize;
  465. /* The below fields are obsolete */
  466. info.ecctype = -1;
  467. info.eccsize = 0;
  468. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  469. return -EFAULT;
  470. break;
  471. case MEMERASE:
  472. case MEMERASE64:
  473. {
  474. struct erase_info *erase;
  475. if(!(file->f_mode & FMODE_WRITE))
  476. return -EPERM;
  477. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  478. if (!erase)
  479. ret = -ENOMEM;
  480. else {
  481. wait_queue_head_t waitq;
  482. DECLARE_WAITQUEUE(wait, current);
  483. init_waitqueue_head(&waitq);
  484. if (cmd == MEMERASE64) {
  485. struct erase_info_user64 einfo64;
  486. if (copy_from_user(&einfo64, argp,
  487. sizeof(struct erase_info_user64))) {
  488. kfree(erase);
  489. return -EFAULT;
  490. }
  491. erase->addr = einfo64.start;
  492. erase->len = einfo64.length;
  493. } else {
  494. struct erase_info_user einfo32;
  495. if (copy_from_user(&einfo32, argp,
  496. sizeof(struct erase_info_user))) {
  497. kfree(erase);
  498. return -EFAULT;
  499. }
  500. erase->addr = einfo32.start;
  501. erase->len = einfo32.length;
  502. }
  503. erase->mtd = mtd;
  504. erase->callback = mtdchar_erase_callback;
  505. erase->priv = (unsigned long)&waitq;
  506. /*
  507. FIXME: Allow INTERRUPTIBLE. Which means
  508. not having the wait_queue head on the stack.
  509. If the wq_head is on the stack, and we
  510. leave because we got interrupted, then the
  511. wq_head is no longer there when the
  512. callback routine tries to wake us up.
  513. */
  514. ret = mtd->erase(mtd, erase);
  515. if (!ret) {
  516. set_current_state(TASK_UNINTERRUPTIBLE);
  517. add_wait_queue(&waitq, &wait);
  518. if (erase->state != MTD_ERASE_DONE &&
  519. erase->state != MTD_ERASE_FAILED)
  520. schedule();
  521. remove_wait_queue(&waitq, &wait);
  522. set_current_state(TASK_RUNNING);
  523. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  524. }
  525. kfree(erase);
  526. }
  527. break;
  528. }
  529. case MEMWRITEOOB:
  530. {
  531. struct mtd_oob_buf buf;
  532. struct mtd_oob_buf __user *buf_user = argp;
  533. /* NOTE: writes return length to buf_user->length */
  534. if (copy_from_user(&buf, argp, sizeof(buf)))
  535. ret = -EFAULT;
  536. else
  537. ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
  538. buf.ptr, &buf_user->length);
  539. break;
  540. }
  541. case MEMREADOOB:
  542. {
  543. struct mtd_oob_buf buf;
  544. struct mtd_oob_buf __user *buf_user = argp;
  545. /* NOTE: writes return length to buf_user->start */
  546. if (copy_from_user(&buf, argp, sizeof(buf)))
  547. ret = -EFAULT;
  548. else
  549. ret = mtd_do_readoob(mtd, buf.start, buf.length,
  550. buf.ptr, &buf_user->start);
  551. break;
  552. }
  553. case MEMWRITEOOB64:
  554. {
  555. struct mtd_oob_buf64 buf;
  556. struct mtd_oob_buf64 __user *buf_user = argp;
  557. if (copy_from_user(&buf, argp, sizeof(buf)))
  558. ret = -EFAULT;
  559. else
  560. ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
  561. (void __user *)(uintptr_t)buf.usr_ptr,
  562. &buf_user->length);
  563. break;
  564. }
  565. case MEMREADOOB64:
  566. {
  567. struct mtd_oob_buf64 buf;
  568. struct mtd_oob_buf64 __user *buf_user = argp;
  569. if (copy_from_user(&buf, argp, sizeof(buf)))
  570. ret = -EFAULT;
  571. else
  572. ret = mtd_do_readoob(mtd, buf.start, buf.length,
  573. (void __user *)(uintptr_t)buf.usr_ptr,
  574. &buf_user->length);
  575. break;
  576. }
  577. case MEMLOCK:
  578. {
  579. struct erase_info_user einfo;
  580. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  581. return -EFAULT;
  582. if (!mtd->lock)
  583. ret = -EOPNOTSUPP;
  584. else
  585. ret = mtd->lock(mtd, einfo.start, einfo.length);
  586. break;
  587. }
  588. case MEMUNLOCK:
  589. {
  590. struct erase_info_user einfo;
  591. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  592. return -EFAULT;
  593. if (!mtd->unlock)
  594. ret = -EOPNOTSUPP;
  595. else
  596. ret = mtd->unlock(mtd, einfo.start, einfo.length);
  597. break;
  598. }
  599. case MEMISLOCKED:
  600. {
  601. struct erase_info_user einfo;
  602. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  603. return -EFAULT;
  604. if (!mtd->is_locked)
  605. ret = -EOPNOTSUPP;
  606. else
  607. ret = mtd->is_locked(mtd, einfo.start, einfo.length);
  608. break;
  609. }
  610. /* Legacy interface */
  611. case MEMGETOOBSEL:
  612. {
  613. struct nand_oobinfo oi;
  614. if (!mtd->ecclayout)
  615. return -EOPNOTSUPP;
  616. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  617. return -EINVAL;
  618. oi.useecc = MTD_NANDECC_AUTOPLACE;
  619. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  620. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  621. sizeof(oi.oobfree));
  622. oi.eccbytes = mtd->ecclayout->eccbytes;
  623. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  624. return -EFAULT;
  625. break;
  626. }
  627. case MEMGETBADBLOCK:
  628. {
  629. loff_t offs;
  630. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  631. return -EFAULT;
  632. if (!mtd->block_isbad)
  633. ret = -EOPNOTSUPP;
  634. else
  635. return mtd->block_isbad(mtd, offs);
  636. break;
  637. }
  638. case MEMSETBADBLOCK:
  639. {
  640. loff_t offs;
  641. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  642. return -EFAULT;
  643. if (!mtd->block_markbad)
  644. ret = -EOPNOTSUPP;
  645. else
  646. return mtd->block_markbad(mtd, offs);
  647. break;
  648. }
  649. #ifdef CONFIG_HAVE_MTD_OTP
  650. case OTPSELECT:
  651. {
  652. int mode;
  653. if (copy_from_user(&mode, argp, sizeof(int)))
  654. return -EFAULT;
  655. mfi->mode = MTD_MODE_NORMAL;
  656. ret = otp_select_filemode(mfi, mode);
  657. file->f_pos = 0;
  658. break;
  659. }
  660. case OTPGETREGIONCOUNT:
  661. case OTPGETREGIONINFO:
  662. {
  663. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  664. if (!buf)
  665. return -ENOMEM;
  666. ret = -EOPNOTSUPP;
  667. switch (mfi->mode) {
  668. case MTD_MODE_OTP_FACTORY:
  669. if (mtd->get_fact_prot_info)
  670. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  671. break;
  672. case MTD_MODE_OTP_USER:
  673. if (mtd->get_user_prot_info)
  674. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  675. break;
  676. default:
  677. break;
  678. }
  679. if (ret >= 0) {
  680. if (cmd == OTPGETREGIONCOUNT) {
  681. int nbr = ret / sizeof(struct otp_info);
  682. ret = copy_to_user(argp, &nbr, sizeof(int));
  683. } else
  684. ret = copy_to_user(argp, buf, ret);
  685. if (ret)
  686. ret = -EFAULT;
  687. }
  688. kfree(buf);
  689. break;
  690. }
  691. case OTPLOCK:
  692. {
  693. struct otp_info oinfo;
  694. if (mfi->mode != MTD_MODE_OTP_USER)
  695. return -EINVAL;
  696. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  697. return -EFAULT;
  698. if (!mtd->lock_user_prot_reg)
  699. return -EOPNOTSUPP;
  700. ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  701. break;
  702. }
  703. #endif
  704. /* This ioctl is being deprecated - it truncates the ecc layout */
  705. case ECCGETLAYOUT:
  706. {
  707. struct nand_ecclayout_user *usrlay;
  708. if (!mtd->ecclayout)
  709. return -EOPNOTSUPP;
  710. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  711. if (!usrlay)
  712. return -ENOMEM;
  713. shrink_ecclayout(mtd->ecclayout, usrlay);
  714. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  715. ret = -EFAULT;
  716. kfree(usrlay);
  717. break;
  718. }
  719. case ECCGETSTATS:
  720. {
  721. if (copy_to_user(argp, &mtd->ecc_stats,
  722. sizeof(struct mtd_ecc_stats)))
  723. return -EFAULT;
  724. break;
  725. }
  726. case MTDFILEMODE:
  727. {
  728. mfi->mode = 0;
  729. switch(arg) {
  730. case MTD_MODE_OTP_FACTORY:
  731. case MTD_MODE_OTP_USER:
  732. ret = otp_select_filemode(mfi, arg);
  733. break;
  734. case MTD_MODE_RAW:
  735. if (!mtd->read_oob || !mtd->write_oob)
  736. return -EOPNOTSUPP;
  737. mfi->mode = arg;
  738. case MTD_MODE_NORMAL:
  739. break;
  740. default:
  741. ret = -EINVAL;
  742. }
  743. file->f_pos = 0;
  744. break;
  745. }
  746. default:
  747. ret = -ENOTTY;
  748. }
  749. return ret;
  750. } /* memory_ioctl */
  751. static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  752. {
  753. int ret;
  754. lock_kernel();
  755. ret = mtd_ioctl(file, cmd, arg);
  756. unlock_kernel();
  757. return ret;
  758. }
  759. #ifdef CONFIG_COMPAT
  760. struct mtd_oob_buf32 {
  761. u_int32_t start;
  762. u_int32_t length;
  763. compat_caddr_t ptr; /* unsigned char* */
  764. };
  765. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  766. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  767. static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
  768. unsigned long arg)
  769. {
  770. struct mtd_file_info *mfi = file->private_data;
  771. struct mtd_info *mtd = mfi->mtd;
  772. void __user *argp = compat_ptr(arg);
  773. int ret = 0;
  774. lock_kernel();
  775. switch (cmd) {
  776. case MEMWRITEOOB32:
  777. {
  778. struct mtd_oob_buf32 buf;
  779. struct mtd_oob_buf32 __user *buf_user = argp;
  780. if (copy_from_user(&buf, argp, sizeof(buf)))
  781. ret = -EFAULT;
  782. else
  783. ret = mtd_do_writeoob(file, mtd, buf.start,
  784. buf.length, compat_ptr(buf.ptr),
  785. &buf_user->length);
  786. break;
  787. }
  788. case MEMREADOOB32:
  789. {
  790. struct mtd_oob_buf32 buf;
  791. struct mtd_oob_buf32 __user *buf_user = argp;
  792. /* NOTE: writes return length to buf->start */
  793. if (copy_from_user(&buf, argp, sizeof(buf)))
  794. ret = -EFAULT;
  795. else
  796. ret = mtd_do_readoob(mtd, buf.start,
  797. buf.length, compat_ptr(buf.ptr),
  798. &buf_user->start);
  799. break;
  800. }
  801. default:
  802. ret = mtd_ioctl(file, cmd, (unsigned long)argp);
  803. }
  804. unlock_kernel();
  805. return ret;
  806. }
  807. #endif /* CONFIG_COMPAT */
  808. /*
  809. * try to determine where a shared mapping can be made
  810. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  811. * mappings)
  812. */
  813. #ifndef CONFIG_MMU
  814. static unsigned long mtd_get_unmapped_area(struct file *file,
  815. unsigned long addr,
  816. unsigned long len,
  817. unsigned long pgoff,
  818. unsigned long flags)
  819. {
  820. struct mtd_file_info *mfi = file->private_data;
  821. struct mtd_info *mtd = mfi->mtd;
  822. if (mtd->get_unmapped_area) {
  823. unsigned long offset;
  824. if (addr != 0)
  825. return (unsigned long) -EINVAL;
  826. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  827. return (unsigned long) -EINVAL;
  828. offset = pgoff << PAGE_SHIFT;
  829. if (offset > mtd->size - len)
  830. return (unsigned long) -EINVAL;
  831. return mtd->get_unmapped_area(mtd, len, offset, flags);
  832. }
  833. /* can't map directly */
  834. return (unsigned long) -ENOSYS;
  835. }
  836. #endif
  837. /*
  838. * set up a mapping for shared memory segments
  839. */
  840. static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
  841. {
  842. #ifdef CONFIG_MMU
  843. struct mtd_file_info *mfi = file->private_data;
  844. struct mtd_info *mtd = mfi->mtd;
  845. struct map_info *map = mtd->priv;
  846. unsigned long start;
  847. unsigned long off;
  848. u32 len;
  849. if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
  850. off = vma->vm_pgoff << PAGE_SHIFT;
  851. start = map->phys;
  852. len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
  853. start &= PAGE_MASK;
  854. if ((vma->vm_end - vma->vm_start + off) > len)
  855. return -EINVAL;
  856. off += start;
  857. vma->vm_pgoff = off >> PAGE_SHIFT;
  858. vma->vm_flags |= VM_IO | VM_RESERVED;
  859. #ifdef pgprot_noncached
  860. if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
  861. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  862. #endif
  863. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  864. vma->vm_end - vma->vm_start,
  865. vma->vm_page_prot))
  866. return -EAGAIN;
  867. return 0;
  868. }
  869. return -ENOSYS;
  870. #else
  871. return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
  872. #endif
  873. }
  874. static const struct file_operations mtd_fops = {
  875. .owner = THIS_MODULE,
  876. .llseek = mtd_lseek,
  877. .read = mtd_read,
  878. .write = mtd_write,
  879. .unlocked_ioctl = mtd_unlocked_ioctl,
  880. #ifdef CONFIG_COMPAT
  881. .compat_ioctl = mtd_compat_ioctl,
  882. #endif
  883. .open = mtd_open,
  884. .release = mtd_close,
  885. .mmap = mtd_mmap,
  886. #ifndef CONFIG_MMU
  887. .get_unmapped_area = mtd_get_unmapped_area,
  888. #endif
  889. };
  890. static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
  891. const char *dev_name, void *data,
  892. struct vfsmount *mnt)
  893. {
  894. return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
  895. mnt);
  896. }
  897. static struct file_system_type mtd_inodefs_type = {
  898. .name = "mtd_inodefs",
  899. .get_sb = mtd_inodefs_get_sb,
  900. .kill_sb = kill_anon_super,
  901. };
  902. static void mtdchar_notify_add(struct mtd_info *mtd)
  903. {
  904. }
  905. static void mtdchar_notify_remove(struct mtd_info *mtd)
  906. {
  907. struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
  908. if (mtd_ino) {
  909. /* Destroy the inode if it exists */
  910. mtd_ino->i_nlink = 0;
  911. iput(mtd_ino);
  912. }
  913. }
  914. static struct mtd_notifier mtdchar_notifier = {
  915. .add = mtdchar_notify_add,
  916. .remove = mtdchar_notify_remove,
  917. };
  918. static int __init init_mtdchar(void)
  919. {
  920. int ret;
  921. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  922. "mtd", &mtd_fops);
  923. if (ret < 0) {
  924. pr_notice("Can't allocate major number %d for "
  925. "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
  926. return ret;
  927. }
  928. ret = register_filesystem(&mtd_inodefs_type);
  929. if (ret) {
  930. pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
  931. goto err_unregister_chdev;
  932. }
  933. mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
  934. if (IS_ERR(mtd_inode_mnt)) {
  935. ret = PTR_ERR(mtd_inode_mnt);
  936. pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
  937. goto err_unregister_filesystem;
  938. }
  939. register_mtd_user(&mtdchar_notifier);
  940. return ret;
  941. err_unregister_filesystem:
  942. unregister_filesystem(&mtd_inodefs_type);
  943. err_unregister_chdev:
  944. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  945. return ret;
  946. }
  947. static void __exit cleanup_mtdchar(void)
  948. {
  949. unregister_mtd_user(&mtdchar_notifier);
  950. mntput(mtd_inode_mnt);
  951. unregister_filesystem(&mtd_inodefs_type);
  952. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  953. }
  954. module_init(init_mtdchar);
  955. module_exit(cleanup_mtdchar);
  956. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
  957. MODULE_LICENSE("GPL");
  958. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  959. MODULE_DESCRIPTION("Direct character-device access to MTD devices");
  960. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);