mtdchar.c 27 KB

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