mtdchar.c 26 KB

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