mtdchar.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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_file_info *mfi = file->private_data;
  335. struct mtd_oob_ops ops;
  336. uint32_t retlen;
  337. int ret = 0;
  338. if (!(file->f_mode & FMODE_WRITE))
  339. return -EPERM;
  340. if (length > 4096)
  341. return -EINVAL;
  342. if (!mtd->write_oob)
  343. ret = -EOPNOTSUPP;
  344. else
  345. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  346. if (ret)
  347. return ret;
  348. ops.ooblen = length;
  349. ops.ooboffs = start & (mtd->writesize - 1);
  350. ops.datbuf = NULL;
  351. ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
  352. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  353. return -EINVAL;
  354. ops.oobbuf = memdup_user(ptr, length);
  355. if (IS_ERR(ops.oobbuf))
  356. return PTR_ERR(ops.oobbuf);
  357. start &= ~((uint64_t)mtd->writesize - 1);
  358. ret = mtd->write_oob(mtd, start, &ops);
  359. if (ops.oobretlen > 0xFFFFFFFFU)
  360. ret = -EOVERFLOW;
  361. retlen = ops.oobretlen;
  362. if (copy_to_user(retp, &retlen, sizeof(length)))
  363. ret = -EFAULT;
  364. kfree(ops.oobbuf);
  365. return ret;
  366. }
  367. static int mtd_do_readoob(struct file *file, struct mtd_info *mtd,
  368. uint64_t start, uint32_t length, void __user *ptr,
  369. uint32_t __user *retp)
  370. {
  371. struct mtd_file_info *mfi = file->private_data;
  372. struct mtd_oob_ops ops;
  373. int ret = 0;
  374. if (length > 4096)
  375. return -EINVAL;
  376. if (!mtd->read_oob)
  377. ret = -EOPNOTSUPP;
  378. else
  379. ret = access_ok(VERIFY_WRITE, ptr,
  380. length) ? 0 : -EFAULT;
  381. if (ret)
  382. return ret;
  383. ops.ooblen = length;
  384. ops.ooboffs = start & (mtd->writesize - 1);
  385. ops.datbuf = NULL;
  386. ops.mode = (mfi->mode == MTD_MODE_RAW) ? MTD_OOB_RAW : MTD_OOB_PLACE;
  387. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  388. return -EINVAL;
  389. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  390. if (!ops.oobbuf)
  391. return -ENOMEM;
  392. start &= ~((uint64_t)mtd->writesize - 1);
  393. ret = mtd->read_oob(mtd, start, &ops);
  394. if (put_user(ops.oobretlen, retp))
  395. ret = -EFAULT;
  396. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  397. ops.oobretlen))
  398. ret = -EFAULT;
  399. kfree(ops.oobbuf);
  400. /*
  401. * NAND returns -EBADMSG on ECC errors, but it returns the OOB
  402. * data. For our userspace tools it is important to dump areas
  403. * with ECC errors!
  404. * For kernel internal usage it also might return -EUCLEAN
  405. * to signal the caller that a bitflip has occured and has
  406. * been corrected by the ECC algorithm.
  407. *
  408. * Note: currently the standard NAND function, nand_read_oob_std,
  409. * does not calculate ECC for the OOB area, so do not rely on
  410. * this behavior unless you have replaced it with your own.
  411. */
  412. if (ret == -EUCLEAN || ret == -EBADMSG)
  413. return 0;
  414. return ret;
  415. }
  416. /*
  417. * Copies (and truncates, if necessary) data from the larger struct,
  418. * nand_ecclayout, to the smaller, deprecated layout struct,
  419. * nand_ecclayout_user. This is necessary only to support the deprecated
  420. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  421. * nand_ecclayout flexibly (i.e. the struct may change size in new
  422. * releases without requiring major rewrites).
  423. */
  424. static int shrink_ecclayout(const struct nand_ecclayout *from,
  425. struct nand_ecclayout_user *to)
  426. {
  427. int i;
  428. if (!from || !to)
  429. return -EINVAL;
  430. memset(to, 0, sizeof(*to));
  431. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  432. for (i = 0; i < to->eccbytes; i++)
  433. to->eccpos[i] = from->eccpos[i];
  434. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  435. if (from->oobfree[i].length == 0 &&
  436. from->oobfree[i].offset == 0)
  437. break;
  438. to->oobavail += from->oobfree[i].length;
  439. to->oobfree[i] = from->oobfree[i];
  440. }
  441. return 0;
  442. }
  443. static int mtd_blkpg_ioctl(struct mtd_info *mtd,
  444. struct blkpg_ioctl_arg __user *arg)
  445. {
  446. struct blkpg_ioctl_arg a;
  447. struct blkpg_partition p;
  448. if (!capable(CAP_SYS_ADMIN))
  449. return -EPERM;
  450. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  451. return -EFAULT;
  452. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  453. return -EFAULT;
  454. switch (a.op) {
  455. case BLKPG_ADD_PARTITION:
  456. /* Only master mtd device must be used to add partitions */
  457. if (mtd_is_partition(mtd))
  458. return -EINVAL;
  459. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  460. case BLKPG_DEL_PARTITION:
  461. if (p.pno < 0)
  462. return -EINVAL;
  463. return mtd_del_partition(mtd, p.pno);
  464. default:
  465. return -EINVAL;
  466. }
  467. }
  468. static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
  469. {
  470. struct mtd_file_info *mfi = file->private_data;
  471. struct mtd_info *mtd = mfi->mtd;
  472. void __user *argp = (void __user *)arg;
  473. int ret = 0;
  474. u_long size;
  475. struct mtd_info_user info;
  476. pr_debug("MTD_ioctl\n");
  477. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  478. if (cmd & IOC_IN) {
  479. if (!access_ok(VERIFY_READ, argp, size))
  480. return -EFAULT;
  481. }
  482. if (cmd & IOC_OUT) {
  483. if (!access_ok(VERIFY_WRITE, argp, size))
  484. return -EFAULT;
  485. }
  486. switch (cmd) {
  487. case MEMGETREGIONCOUNT:
  488. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  489. return -EFAULT;
  490. break;
  491. case MEMGETREGIONINFO:
  492. {
  493. uint32_t ur_idx;
  494. struct mtd_erase_region_info *kr;
  495. struct region_info_user __user *ur = argp;
  496. if (get_user(ur_idx, &(ur->regionindex)))
  497. return -EFAULT;
  498. if (ur_idx >= mtd->numeraseregions)
  499. return -EINVAL;
  500. kr = &(mtd->eraseregions[ur_idx]);
  501. if (put_user(kr->offset, &(ur->offset))
  502. || put_user(kr->erasesize, &(ur->erasesize))
  503. || put_user(kr->numblocks, &(ur->numblocks)))
  504. return -EFAULT;
  505. break;
  506. }
  507. case MEMGETINFO:
  508. memset(&info, 0, sizeof(info));
  509. info.type = mtd->type;
  510. info.flags = mtd->flags;
  511. info.size = mtd->size;
  512. info.erasesize = mtd->erasesize;
  513. info.writesize = mtd->writesize;
  514. info.oobsize = mtd->oobsize;
  515. /* The below fields are obsolete */
  516. info.ecctype = -1;
  517. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  518. return -EFAULT;
  519. break;
  520. case MEMERASE:
  521. case MEMERASE64:
  522. {
  523. struct erase_info *erase;
  524. if(!(file->f_mode & FMODE_WRITE))
  525. return -EPERM;
  526. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  527. if (!erase)
  528. ret = -ENOMEM;
  529. else {
  530. wait_queue_head_t waitq;
  531. DECLARE_WAITQUEUE(wait, current);
  532. init_waitqueue_head(&waitq);
  533. if (cmd == MEMERASE64) {
  534. struct erase_info_user64 einfo64;
  535. if (copy_from_user(&einfo64, argp,
  536. sizeof(struct erase_info_user64))) {
  537. kfree(erase);
  538. return -EFAULT;
  539. }
  540. erase->addr = einfo64.start;
  541. erase->len = einfo64.length;
  542. } else {
  543. struct erase_info_user einfo32;
  544. if (copy_from_user(&einfo32, argp,
  545. sizeof(struct erase_info_user))) {
  546. kfree(erase);
  547. return -EFAULT;
  548. }
  549. erase->addr = einfo32.start;
  550. erase->len = einfo32.length;
  551. }
  552. erase->mtd = mtd;
  553. erase->callback = mtdchar_erase_callback;
  554. erase->priv = (unsigned long)&waitq;
  555. /*
  556. FIXME: Allow INTERRUPTIBLE. Which means
  557. not having the wait_queue head on the stack.
  558. If the wq_head is on the stack, and we
  559. leave because we got interrupted, then the
  560. wq_head is no longer there when the
  561. callback routine tries to wake us up.
  562. */
  563. ret = mtd->erase(mtd, erase);
  564. if (!ret) {
  565. set_current_state(TASK_UNINTERRUPTIBLE);
  566. add_wait_queue(&waitq, &wait);
  567. if (erase->state != MTD_ERASE_DONE &&
  568. erase->state != MTD_ERASE_FAILED)
  569. schedule();
  570. remove_wait_queue(&waitq, &wait);
  571. set_current_state(TASK_RUNNING);
  572. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  573. }
  574. kfree(erase);
  575. }
  576. break;
  577. }
  578. case MEMWRITEOOB:
  579. {
  580. struct mtd_oob_buf buf;
  581. struct mtd_oob_buf __user *buf_user = argp;
  582. /* NOTE: writes return length to buf_user->length */
  583. if (copy_from_user(&buf, argp, sizeof(buf)))
  584. ret = -EFAULT;
  585. else
  586. ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
  587. buf.ptr, &buf_user->length);
  588. break;
  589. }
  590. case MEMREADOOB:
  591. {
  592. struct mtd_oob_buf buf;
  593. struct mtd_oob_buf __user *buf_user = argp;
  594. /* NOTE: writes return length to buf_user->start */
  595. if (copy_from_user(&buf, argp, sizeof(buf)))
  596. ret = -EFAULT;
  597. else
  598. ret = mtd_do_readoob(file, mtd, buf.start, buf.length,
  599. buf.ptr, &buf_user->start);
  600. break;
  601. }
  602. case MEMWRITEOOB64:
  603. {
  604. struct mtd_oob_buf64 buf;
  605. struct mtd_oob_buf64 __user *buf_user = argp;
  606. if (copy_from_user(&buf, argp, sizeof(buf)))
  607. ret = -EFAULT;
  608. else
  609. ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
  610. (void __user *)(uintptr_t)buf.usr_ptr,
  611. &buf_user->length);
  612. break;
  613. }
  614. case MEMREADOOB64:
  615. {
  616. struct mtd_oob_buf64 buf;
  617. struct mtd_oob_buf64 __user *buf_user = argp;
  618. if (copy_from_user(&buf, argp, sizeof(buf)))
  619. ret = -EFAULT;
  620. else
  621. ret = mtd_do_readoob(file, mtd, buf.start, buf.length,
  622. (void __user *)(uintptr_t)buf.usr_ptr,
  623. &buf_user->length);
  624. break;
  625. }
  626. case MEMLOCK:
  627. {
  628. struct erase_info_user einfo;
  629. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  630. return -EFAULT;
  631. if (!mtd->lock)
  632. ret = -EOPNOTSUPP;
  633. else
  634. ret = mtd->lock(mtd, einfo.start, einfo.length);
  635. break;
  636. }
  637. case MEMUNLOCK:
  638. {
  639. struct erase_info_user einfo;
  640. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  641. return -EFAULT;
  642. if (!mtd->unlock)
  643. ret = -EOPNOTSUPP;
  644. else
  645. ret = mtd->unlock(mtd, einfo.start, einfo.length);
  646. break;
  647. }
  648. case MEMISLOCKED:
  649. {
  650. struct erase_info_user einfo;
  651. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  652. return -EFAULT;
  653. if (!mtd->is_locked)
  654. ret = -EOPNOTSUPP;
  655. else
  656. ret = mtd->is_locked(mtd, einfo.start, einfo.length);
  657. break;
  658. }
  659. /* Legacy interface */
  660. case MEMGETOOBSEL:
  661. {
  662. struct nand_oobinfo oi;
  663. if (!mtd->ecclayout)
  664. return -EOPNOTSUPP;
  665. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  666. return -EINVAL;
  667. oi.useecc = MTD_NANDECC_AUTOPLACE;
  668. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  669. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  670. sizeof(oi.oobfree));
  671. oi.eccbytes = mtd->ecclayout->eccbytes;
  672. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  673. return -EFAULT;
  674. break;
  675. }
  676. case MEMGETBADBLOCK:
  677. {
  678. loff_t offs;
  679. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  680. return -EFAULT;
  681. if (!mtd->block_isbad)
  682. ret = -EOPNOTSUPP;
  683. else
  684. return mtd->block_isbad(mtd, offs);
  685. break;
  686. }
  687. case MEMSETBADBLOCK:
  688. {
  689. loff_t offs;
  690. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  691. return -EFAULT;
  692. if (!mtd->block_markbad)
  693. ret = -EOPNOTSUPP;
  694. else
  695. return mtd->block_markbad(mtd, offs);
  696. break;
  697. }
  698. #ifdef CONFIG_HAVE_MTD_OTP
  699. case OTPSELECT:
  700. {
  701. int mode;
  702. if (copy_from_user(&mode, argp, sizeof(int)))
  703. return -EFAULT;
  704. mfi->mode = MTD_MODE_NORMAL;
  705. ret = otp_select_filemode(mfi, mode);
  706. file->f_pos = 0;
  707. break;
  708. }
  709. case OTPGETREGIONCOUNT:
  710. case OTPGETREGIONINFO:
  711. {
  712. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  713. if (!buf)
  714. return -ENOMEM;
  715. ret = -EOPNOTSUPP;
  716. switch (mfi->mode) {
  717. case MTD_MODE_OTP_FACTORY:
  718. if (mtd->get_fact_prot_info)
  719. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  720. break;
  721. case MTD_MODE_OTP_USER:
  722. if (mtd->get_user_prot_info)
  723. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  724. break;
  725. default:
  726. break;
  727. }
  728. if (ret >= 0) {
  729. if (cmd == OTPGETREGIONCOUNT) {
  730. int nbr = ret / sizeof(struct otp_info);
  731. ret = copy_to_user(argp, &nbr, sizeof(int));
  732. } else
  733. ret = copy_to_user(argp, buf, ret);
  734. if (ret)
  735. ret = -EFAULT;
  736. }
  737. kfree(buf);
  738. break;
  739. }
  740. case OTPLOCK:
  741. {
  742. struct otp_info oinfo;
  743. if (mfi->mode != MTD_MODE_OTP_USER)
  744. return -EINVAL;
  745. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  746. return -EFAULT;
  747. if (!mtd->lock_user_prot_reg)
  748. return -EOPNOTSUPP;
  749. ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  750. break;
  751. }
  752. #endif
  753. /* This ioctl is being deprecated - it truncates the ECC layout */
  754. case ECCGETLAYOUT:
  755. {
  756. struct nand_ecclayout_user *usrlay;
  757. if (!mtd->ecclayout)
  758. return -EOPNOTSUPP;
  759. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  760. if (!usrlay)
  761. return -ENOMEM;
  762. shrink_ecclayout(mtd->ecclayout, usrlay);
  763. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  764. ret = -EFAULT;
  765. kfree(usrlay);
  766. break;
  767. }
  768. case ECCGETSTATS:
  769. {
  770. if (copy_to_user(argp, &mtd->ecc_stats,
  771. sizeof(struct mtd_ecc_stats)))
  772. return -EFAULT;
  773. break;
  774. }
  775. case MTDFILEMODE:
  776. {
  777. mfi->mode = 0;
  778. switch(arg) {
  779. case MTD_MODE_OTP_FACTORY:
  780. case MTD_MODE_OTP_USER:
  781. ret = otp_select_filemode(mfi, arg);
  782. break;
  783. case MTD_MODE_RAW:
  784. if (!mtd->read_oob || !mtd->write_oob)
  785. return -EOPNOTSUPP;
  786. mfi->mode = arg;
  787. case MTD_MODE_NORMAL:
  788. break;
  789. default:
  790. ret = -EINVAL;
  791. }
  792. file->f_pos = 0;
  793. break;
  794. }
  795. case BLKPG:
  796. {
  797. ret = mtd_blkpg_ioctl(mtd,
  798. (struct blkpg_ioctl_arg __user *)arg);
  799. break;
  800. }
  801. case BLKRRPART:
  802. {
  803. /* No reread partition feature. Just return ok */
  804. ret = 0;
  805. break;
  806. }
  807. default:
  808. ret = -ENOTTY;
  809. }
  810. return ret;
  811. } /* memory_ioctl */
  812. static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  813. {
  814. int ret;
  815. mutex_lock(&mtd_mutex);
  816. ret = mtd_ioctl(file, cmd, arg);
  817. mutex_unlock(&mtd_mutex);
  818. return ret;
  819. }
  820. #ifdef CONFIG_COMPAT
  821. struct mtd_oob_buf32 {
  822. u_int32_t start;
  823. u_int32_t length;
  824. compat_caddr_t ptr; /* unsigned char* */
  825. };
  826. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  827. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  828. static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
  829. unsigned long arg)
  830. {
  831. struct mtd_file_info *mfi = file->private_data;
  832. struct mtd_info *mtd = mfi->mtd;
  833. void __user *argp = compat_ptr(arg);
  834. int ret = 0;
  835. mutex_lock(&mtd_mutex);
  836. switch (cmd) {
  837. case MEMWRITEOOB32:
  838. {
  839. struct mtd_oob_buf32 buf;
  840. struct mtd_oob_buf32 __user *buf_user = argp;
  841. if (copy_from_user(&buf, argp, sizeof(buf)))
  842. ret = -EFAULT;
  843. else
  844. ret = mtd_do_writeoob(file, mtd, buf.start,
  845. buf.length, compat_ptr(buf.ptr),
  846. &buf_user->length);
  847. break;
  848. }
  849. case MEMREADOOB32:
  850. {
  851. struct mtd_oob_buf32 buf;
  852. struct mtd_oob_buf32 __user *buf_user = argp;
  853. /* NOTE: writes return length to buf->start */
  854. if (copy_from_user(&buf, argp, sizeof(buf)))
  855. ret = -EFAULT;
  856. else
  857. ret = mtd_do_readoob(file, mtd, buf.start,
  858. buf.length, compat_ptr(buf.ptr),
  859. &buf_user->start);
  860. break;
  861. }
  862. default:
  863. ret = mtd_ioctl(file, cmd, (unsigned long)argp);
  864. }
  865. mutex_unlock(&mtd_mutex);
  866. return ret;
  867. }
  868. #endif /* CONFIG_COMPAT */
  869. /*
  870. * try to determine where a shared mapping can be made
  871. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  872. * mappings)
  873. */
  874. #ifndef CONFIG_MMU
  875. static unsigned long mtd_get_unmapped_area(struct file *file,
  876. unsigned long addr,
  877. unsigned long len,
  878. unsigned long pgoff,
  879. unsigned long flags)
  880. {
  881. struct mtd_file_info *mfi = file->private_data;
  882. struct mtd_info *mtd = mfi->mtd;
  883. if (mtd->get_unmapped_area) {
  884. unsigned long offset;
  885. if (addr != 0)
  886. return (unsigned long) -EINVAL;
  887. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  888. return (unsigned long) -EINVAL;
  889. offset = pgoff << PAGE_SHIFT;
  890. if (offset > mtd->size - len)
  891. return (unsigned long) -EINVAL;
  892. return mtd->get_unmapped_area(mtd, len, offset, flags);
  893. }
  894. /* can't map directly */
  895. return (unsigned long) -ENOSYS;
  896. }
  897. #endif
  898. /*
  899. * set up a mapping for shared memory segments
  900. */
  901. static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
  902. {
  903. #ifdef CONFIG_MMU
  904. struct mtd_file_info *mfi = file->private_data;
  905. struct mtd_info *mtd = mfi->mtd;
  906. struct map_info *map = mtd->priv;
  907. unsigned long start;
  908. unsigned long off;
  909. u32 len;
  910. if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
  911. off = vma->vm_pgoff << PAGE_SHIFT;
  912. start = map->phys;
  913. len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
  914. start &= PAGE_MASK;
  915. if ((vma->vm_end - vma->vm_start + off) > len)
  916. return -EINVAL;
  917. off += start;
  918. vma->vm_pgoff = off >> PAGE_SHIFT;
  919. vma->vm_flags |= VM_IO | VM_RESERVED;
  920. #ifdef pgprot_noncached
  921. if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
  922. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  923. #endif
  924. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  925. vma->vm_end - vma->vm_start,
  926. vma->vm_page_prot))
  927. return -EAGAIN;
  928. return 0;
  929. }
  930. return -ENOSYS;
  931. #else
  932. return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
  933. #endif
  934. }
  935. static const struct file_operations mtd_fops = {
  936. .owner = THIS_MODULE,
  937. .llseek = mtd_lseek,
  938. .read = mtd_read,
  939. .write = mtd_write,
  940. .unlocked_ioctl = mtd_unlocked_ioctl,
  941. #ifdef CONFIG_COMPAT
  942. .compat_ioctl = mtd_compat_ioctl,
  943. #endif
  944. .open = mtd_open,
  945. .release = mtd_close,
  946. .mmap = mtd_mmap,
  947. #ifndef CONFIG_MMU
  948. .get_unmapped_area = mtd_get_unmapped_area,
  949. #endif
  950. };
  951. static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
  952. int flags, const char *dev_name, void *data)
  953. {
  954. return mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
  955. }
  956. static struct file_system_type mtd_inodefs_type = {
  957. .name = "mtd_inodefs",
  958. .mount = mtd_inodefs_mount,
  959. .kill_sb = kill_anon_super,
  960. };
  961. static void mtdchar_notify_add(struct mtd_info *mtd)
  962. {
  963. }
  964. static void mtdchar_notify_remove(struct mtd_info *mtd)
  965. {
  966. struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
  967. if (mtd_ino) {
  968. /* Destroy the inode if it exists */
  969. mtd_ino->i_nlink = 0;
  970. iput(mtd_ino);
  971. }
  972. }
  973. static struct mtd_notifier mtdchar_notifier = {
  974. .add = mtdchar_notify_add,
  975. .remove = mtdchar_notify_remove,
  976. };
  977. static int __init init_mtdchar(void)
  978. {
  979. int ret;
  980. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  981. "mtd", &mtd_fops);
  982. if (ret < 0) {
  983. pr_notice("Can't allocate major number %d for "
  984. "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
  985. return ret;
  986. }
  987. ret = register_filesystem(&mtd_inodefs_type);
  988. if (ret) {
  989. pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
  990. goto err_unregister_chdev;
  991. }
  992. mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
  993. if (IS_ERR(mtd_inode_mnt)) {
  994. ret = PTR_ERR(mtd_inode_mnt);
  995. pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
  996. goto err_unregister_filesystem;
  997. }
  998. register_mtd_user(&mtdchar_notifier);
  999. return ret;
  1000. err_unregister_filesystem:
  1001. unregister_filesystem(&mtd_inodefs_type);
  1002. err_unregister_chdev:
  1003. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1004. return ret;
  1005. }
  1006. static void __exit cleanup_mtdchar(void)
  1007. {
  1008. unregister_mtd_user(&mtdchar_notifier);
  1009. kern_unmount(mtd_inode_mnt);
  1010. unregister_filesystem(&mtd_inodefs_type);
  1011. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1012. }
  1013. module_init(init_mtdchar);
  1014. module_exit(cleanup_mtdchar);
  1015. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
  1016. MODULE_LICENSE("GPL");
  1017. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1018. MODULE_DESCRIPTION("Direct character-device access to MTD devices");
  1019. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);