mtdchar.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. /*
  277. * Return -ENOSPC only if no data could be written at all.
  278. * Otherwise just return the number of bytes that actually
  279. * have been written.
  280. */
  281. if ((ret == -ENOSPC) && (total_retlen))
  282. break;
  283. if (!ret) {
  284. *ppos += retlen;
  285. total_retlen += retlen;
  286. count -= retlen;
  287. buf += retlen;
  288. }
  289. else {
  290. kfree(kbuf);
  291. return ret;
  292. }
  293. }
  294. kfree(kbuf);
  295. return total_retlen;
  296. } /* mtdchar_write */
  297. /*======================================================================
  298. IOCTL calls for getting device parameters.
  299. ======================================================================*/
  300. static void mtdchar_erase_callback (struct erase_info *instr)
  301. {
  302. wake_up((wait_queue_head_t *)instr->priv);
  303. }
  304. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  305. {
  306. struct mtd_info *mtd = mfi->mtd;
  307. size_t retlen;
  308. switch (mode) {
  309. case MTD_OTP_FACTORY:
  310. if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
  311. -EOPNOTSUPP)
  312. return -EOPNOTSUPP;
  313. mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
  314. break;
  315. case MTD_OTP_USER:
  316. if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
  317. -EOPNOTSUPP)
  318. return -EOPNOTSUPP;
  319. mfi->mode = MTD_FILE_MODE_OTP_USER;
  320. break;
  321. case MTD_OTP_OFF:
  322. mfi->mode = MTD_FILE_MODE_NORMAL;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. return 0;
  328. }
  329. static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
  330. uint64_t start, uint32_t length, void __user *ptr,
  331. uint32_t __user *retp)
  332. {
  333. struct mtd_file_info *mfi = file->private_data;
  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 = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  351. MTD_OPS_PLACE_OOB;
  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 mtdchar_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 (!access_ok(VERIFY_WRITE, ptr, length))
  377. return -EFAULT;
  378. ops.ooblen = length;
  379. ops.ooboffs = start & (mtd->writesize - 1);
  380. ops.datbuf = NULL;
  381. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  382. MTD_OPS_PLACE_OOB;
  383. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  384. return -EINVAL;
  385. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  386. if (!ops.oobbuf)
  387. return -ENOMEM;
  388. start &= ~((uint64_t)mtd->writesize - 1);
  389. ret = mtd_read_oob(mtd, start, &ops);
  390. if (put_user(ops.oobretlen, retp))
  391. ret = -EFAULT;
  392. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  393. ops.oobretlen))
  394. ret = -EFAULT;
  395. kfree(ops.oobbuf);
  396. /*
  397. * NAND returns -EBADMSG on ECC errors, but it returns the OOB
  398. * data. For our userspace tools it is important to dump areas
  399. * with ECC errors!
  400. * For kernel internal usage it also might return -EUCLEAN
  401. * to signal the caller that a bitflip has occured and has
  402. * been corrected by the ECC algorithm.
  403. *
  404. * Note: currently the standard NAND function, nand_read_oob_std,
  405. * does not calculate ECC for the OOB area, so do not rely on
  406. * this behavior unless you have replaced it with your own.
  407. */
  408. if (mtd_is_bitflip_or_eccerr(ret))
  409. return 0;
  410. return ret;
  411. }
  412. /*
  413. * Copies (and truncates, if necessary) data from the larger struct,
  414. * nand_ecclayout, to the smaller, deprecated layout struct,
  415. * nand_ecclayout_user. This is necessary only to support the deprecated
  416. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  417. * nand_ecclayout flexibly (i.e. the struct may change size in new
  418. * releases without requiring major rewrites).
  419. */
  420. static int shrink_ecclayout(const struct nand_ecclayout *from,
  421. struct nand_ecclayout_user *to)
  422. {
  423. int i;
  424. if (!from || !to)
  425. return -EINVAL;
  426. memset(to, 0, sizeof(*to));
  427. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  428. for (i = 0; i < to->eccbytes; i++)
  429. to->eccpos[i] = from->eccpos[i];
  430. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  431. if (from->oobfree[i].length == 0 &&
  432. from->oobfree[i].offset == 0)
  433. break;
  434. to->oobavail += from->oobfree[i].length;
  435. to->oobfree[i] = from->oobfree[i];
  436. }
  437. return 0;
  438. }
  439. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  440. struct blkpg_ioctl_arg __user *arg)
  441. {
  442. struct blkpg_ioctl_arg a;
  443. struct blkpg_partition p;
  444. if (!capable(CAP_SYS_ADMIN))
  445. return -EPERM;
  446. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  447. return -EFAULT;
  448. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  449. return -EFAULT;
  450. switch (a.op) {
  451. case BLKPG_ADD_PARTITION:
  452. /* Only master mtd device must be used to add partitions */
  453. if (mtd_is_partition(mtd))
  454. return -EINVAL;
  455. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  456. case BLKPG_DEL_PARTITION:
  457. if (p.pno < 0)
  458. return -EINVAL;
  459. return mtd_del_partition(mtd, p.pno);
  460. default:
  461. return -EINVAL;
  462. }
  463. }
  464. static int mtdchar_write_ioctl(struct mtd_info *mtd,
  465. struct mtd_write_req __user *argp)
  466. {
  467. struct mtd_write_req req;
  468. struct mtd_oob_ops ops;
  469. const void __user *usr_data, *usr_oob;
  470. int ret;
  471. if (copy_from_user(&req, argp, sizeof(req)))
  472. return -EFAULT;
  473. usr_data = (const void __user *)(uintptr_t)req.usr_data;
  474. usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
  475. if (!access_ok(VERIFY_READ, usr_data, req.len) ||
  476. !access_ok(VERIFY_READ, usr_oob, req.ooblen))
  477. return -EFAULT;
  478. if (!mtd->_write_oob)
  479. return -EOPNOTSUPP;
  480. ops.mode = req.mode;
  481. ops.len = (size_t)req.len;
  482. ops.ooblen = (size_t)req.ooblen;
  483. ops.ooboffs = 0;
  484. if (usr_data) {
  485. ops.datbuf = memdup_user(usr_data, ops.len);
  486. if (IS_ERR(ops.datbuf))
  487. return PTR_ERR(ops.datbuf);
  488. } else {
  489. ops.datbuf = NULL;
  490. }
  491. if (usr_oob) {
  492. ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
  493. if (IS_ERR(ops.oobbuf)) {
  494. kfree(ops.datbuf);
  495. return PTR_ERR(ops.oobbuf);
  496. }
  497. } else {
  498. ops.oobbuf = NULL;
  499. }
  500. ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
  501. kfree(ops.datbuf);
  502. kfree(ops.oobbuf);
  503. return ret;
  504. }
  505. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  506. {
  507. struct mtd_file_info *mfi = file->private_data;
  508. struct mtd_info *mtd = mfi->mtd;
  509. void __user *argp = (void __user *)arg;
  510. int ret = 0;
  511. u_long size;
  512. struct mtd_info_user info;
  513. pr_debug("MTD_ioctl\n");
  514. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  515. if (cmd & IOC_IN) {
  516. if (!access_ok(VERIFY_READ, argp, size))
  517. return -EFAULT;
  518. }
  519. if (cmd & IOC_OUT) {
  520. if (!access_ok(VERIFY_WRITE, argp, size))
  521. return -EFAULT;
  522. }
  523. switch (cmd) {
  524. case MEMGETREGIONCOUNT:
  525. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  526. return -EFAULT;
  527. break;
  528. case MEMGETREGIONINFO:
  529. {
  530. uint32_t ur_idx;
  531. struct mtd_erase_region_info *kr;
  532. struct region_info_user __user *ur = argp;
  533. if (get_user(ur_idx, &(ur->regionindex)))
  534. return -EFAULT;
  535. if (ur_idx >= mtd->numeraseregions)
  536. return -EINVAL;
  537. kr = &(mtd->eraseregions[ur_idx]);
  538. if (put_user(kr->offset, &(ur->offset))
  539. || put_user(kr->erasesize, &(ur->erasesize))
  540. || put_user(kr->numblocks, &(ur->numblocks)))
  541. return -EFAULT;
  542. break;
  543. }
  544. case MEMGETINFO:
  545. memset(&info, 0, sizeof(info));
  546. info.type = mtd->type;
  547. info.flags = mtd->flags;
  548. info.size = mtd->size;
  549. info.erasesize = mtd->erasesize;
  550. info.writesize = mtd->writesize;
  551. info.oobsize = mtd->oobsize;
  552. /* The below field is obsolete */
  553. info.padding = 0;
  554. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  555. return -EFAULT;
  556. break;
  557. case MEMERASE:
  558. case MEMERASE64:
  559. {
  560. struct erase_info *erase;
  561. if(!(file->f_mode & FMODE_WRITE))
  562. return -EPERM;
  563. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  564. if (!erase)
  565. ret = -ENOMEM;
  566. else {
  567. wait_queue_head_t waitq;
  568. DECLARE_WAITQUEUE(wait, current);
  569. init_waitqueue_head(&waitq);
  570. if (cmd == MEMERASE64) {
  571. struct erase_info_user64 einfo64;
  572. if (copy_from_user(&einfo64, argp,
  573. sizeof(struct erase_info_user64))) {
  574. kfree(erase);
  575. return -EFAULT;
  576. }
  577. erase->addr = einfo64.start;
  578. erase->len = einfo64.length;
  579. } else {
  580. struct erase_info_user einfo32;
  581. if (copy_from_user(&einfo32, argp,
  582. sizeof(struct erase_info_user))) {
  583. kfree(erase);
  584. return -EFAULT;
  585. }
  586. erase->addr = einfo32.start;
  587. erase->len = einfo32.length;
  588. }
  589. erase->mtd = mtd;
  590. erase->callback = mtdchar_erase_callback;
  591. erase->priv = (unsigned long)&waitq;
  592. /*
  593. FIXME: Allow INTERRUPTIBLE. Which means
  594. not having the wait_queue head on the stack.
  595. If the wq_head is on the stack, and we
  596. leave because we got interrupted, then the
  597. wq_head is no longer there when the
  598. callback routine tries to wake us up.
  599. */
  600. ret = mtd_erase(mtd, erase);
  601. if (!ret) {
  602. set_current_state(TASK_UNINTERRUPTIBLE);
  603. add_wait_queue(&waitq, &wait);
  604. if (erase->state != MTD_ERASE_DONE &&
  605. erase->state != MTD_ERASE_FAILED)
  606. schedule();
  607. remove_wait_queue(&waitq, &wait);
  608. set_current_state(TASK_RUNNING);
  609. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  610. }
  611. kfree(erase);
  612. }
  613. break;
  614. }
  615. case MEMWRITEOOB:
  616. {
  617. struct mtd_oob_buf buf;
  618. struct mtd_oob_buf __user *buf_user = argp;
  619. /* NOTE: writes return length to buf_user->length */
  620. if (copy_from_user(&buf, argp, sizeof(buf)))
  621. ret = -EFAULT;
  622. else
  623. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  624. buf.ptr, &buf_user->length);
  625. break;
  626. }
  627. case MEMREADOOB:
  628. {
  629. struct mtd_oob_buf buf;
  630. struct mtd_oob_buf __user *buf_user = argp;
  631. /* NOTE: writes return length to buf_user->start */
  632. if (copy_from_user(&buf, argp, sizeof(buf)))
  633. ret = -EFAULT;
  634. else
  635. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  636. buf.ptr, &buf_user->start);
  637. break;
  638. }
  639. case MEMWRITEOOB64:
  640. {
  641. struct mtd_oob_buf64 buf;
  642. struct mtd_oob_buf64 __user *buf_user = argp;
  643. if (copy_from_user(&buf, argp, sizeof(buf)))
  644. ret = -EFAULT;
  645. else
  646. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  647. (void __user *)(uintptr_t)buf.usr_ptr,
  648. &buf_user->length);
  649. break;
  650. }
  651. case MEMREADOOB64:
  652. {
  653. struct mtd_oob_buf64 buf;
  654. struct mtd_oob_buf64 __user *buf_user = argp;
  655. if (copy_from_user(&buf, argp, sizeof(buf)))
  656. ret = -EFAULT;
  657. else
  658. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  659. (void __user *)(uintptr_t)buf.usr_ptr,
  660. &buf_user->length);
  661. break;
  662. }
  663. case MEMWRITE:
  664. {
  665. ret = mtdchar_write_ioctl(mtd,
  666. (struct mtd_write_req __user *)arg);
  667. break;
  668. }
  669. case MEMLOCK:
  670. {
  671. struct erase_info_user einfo;
  672. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  673. return -EFAULT;
  674. ret = mtd_lock(mtd, einfo.start, einfo.length);
  675. break;
  676. }
  677. case MEMUNLOCK:
  678. {
  679. struct erase_info_user einfo;
  680. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  681. return -EFAULT;
  682. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  683. break;
  684. }
  685. case MEMISLOCKED:
  686. {
  687. struct erase_info_user einfo;
  688. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  689. return -EFAULT;
  690. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  691. break;
  692. }
  693. /* Legacy interface */
  694. case MEMGETOOBSEL:
  695. {
  696. struct nand_oobinfo oi;
  697. if (!mtd->ecclayout)
  698. return -EOPNOTSUPP;
  699. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  700. return -EINVAL;
  701. oi.useecc = MTD_NANDECC_AUTOPLACE;
  702. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  703. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  704. sizeof(oi.oobfree));
  705. oi.eccbytes = mtd->ecclayout->eccbytes;
  706. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  707. return -EFAULT;
  708. break;
  709. }
  710. case MEMGETBADBLOCK:
  711. {
  712. loff_t offs;
  713. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  714. return -EFAULT;
  715. return mtd_block_isbad(mtd, offs);
  716. break;
  717. }
  718. case MEMSETBADBLOCK:
  719. {
  720. loff_t offs;
  721. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  722. return -EFAULT;
  723. return mtd_block_markbad(mtd, offs);
  724. break;
  725. }
  726. case OTPSELECT:
  727. {
  728. int mode;
  729. if (copy_from_user(&mode, argp, sizeof(int)))
  730. return -EFAULT;
  731. mfi->mode = MTD_FILE_MODE_NORMAL;
  732. ret = otp_select_filemode(mfi, mode);
  733. file->f_pos = 0;
  734. break;
  735. }
  736. case OTPGETREGIONCOUNT:
  737. case OTPGETREGIONINFO:
  738. {
  739. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  740. size_t retlen;
  741. if (!buf)
  742. return -ENOMEM;
  743. switch (mfi->mode) {
  744. case MTD_FILE_MODE_OTP_FACTORY:
  745. ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
  746. break;
  747. case MTD_FILE_MODE_OTP_USER:
  748. ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
  749. break;
  750. default:
  751. ret = -EINVAL;
  752. break;
  753. }
  754. if (!ret) {
  755. if (cmd == OTPGETREGIONCOUNT) {
  756. int nbr = retlen / sizeof(struct otp_info);
  757. ret = copy_to_user(argp, &nbr, sizeof(int));
  758. } else
  759. ret = copy_to_user(argp, buf, retlen);
  760. if (ret)
  761. ret = -EFAULT;
  762. }
  763. kfree(buf);
  764. break;
  765. }
  766. case OTPLOCK:
  767. {
  768. struct otp_info oinfo;
  769. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  770. return -EINVAL;
  771. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  772. return -EFAULT;
  773. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  774. break;
  775. }
  776. /* This ioctl is being deprecated - it truncates the ECC layout */
  777. case ECCGETLAYOUT:
  778. {
  779. struct nand_ecclayout_user *usrlay;
  780. if (!mtd->ecclayout)
  781. return -EOPNOTSUPP;
  782. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  783. if (!usrlay)
  784. return -ENOMEM;
  785. shrink_ecclayout(mtd->ecclayout, usrlay);
  786. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  787. ret = -EFAULT;
  788. kfree(usrlay);
  789. break;
  790. }
  791. case ECCGETSTATS:
  792. {
  793. if (copy_to_user(argp, &mtd->ecc_stats,
  794. sizeof(struct mtd_ecc_stats)))
  795. return -EFAULT;
  796. break;
  797. }
  798. case MTDFILEMODE:
  799. {
  800. mfi->mode = 0;
  801. switch(arg) {
  802. case MTD_FILE_MODE_OTP_FACTORY:
  803. case MTD_FILE_MODE_OTP_USER:
  804. ret = otp_select_filemode(mfi, arg);
  805. break;
  806. case MTD_FILE_MODE_RAW:
  807. if (!mtd_has_oob(mtd))
  808. return -EOPNOTSUPP;
  809. mfi->mode = arg;
  810. case MTD_FILE_MODE_NORMAL:
  811. break;
  812. default:
  813. ret = -EINVAL;
  814. }
  815. file->f_pos = 0;
  816. break;
  817. }
  818. case BLKPG:
  819. {
  820. ret = mtdchar_blkpg_ioctl(mtd,
  821. (struct blkpg_ioctl_arg __user *)arg);
  822. break;
  823. }
  824. case BLKRRPART:
  825. {
  826. /* No reread partition feature. Just return ok */
  827. ret = 0;
  828. break;
  829. }
  830. default:
  831. ret = -ENOTTY;
  832. }
  833. return ret;
  834. } /* memory_ioctl */
  835. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  836. {
  837. int ret;
  838. mutex_lock(&mtd_mutex);
  839. ret = mtdchar_ioctl(file, cmd, arg);
  840. mutex_unlock(&mtd_mutex);
  841. return ret;
  842. }
  843. #ifdef CONFIG_COMPAT
  844. struct mtd_oob_buf32 {
  845. u_int32_t start;
  846. u_int32_t length;
  847. compat_caddr_t ptr; /* unsigned char* */
  848. };
  849. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  850. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  851. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  852. unsigned long arg)
  853. {
  854. struct mtd_file_info *mfi = file->private_data;
  855. struct mtd_info *mtd = mfi->mtd;
  856. void __user *argp = compat_ptr(arg);
  857. int ret = 0;
  858. mutex_lock(&mtd_mutex);
  859. switch (cmd) {
  860. case MEMWRITEOOB32:
  861. {
  862. struct mtd_oob_buf32 buf;
  863. struct mtd_oob_buf32 __user *buf_user = argp;
  864. if (copy_from_user(&buf, argp, sizeof(buf)))
  865. ret = -EFAULT;
  866. else
  867. ret = mtdchar_writeoob(file, mtd, buf.start,
  868. buf.length, compat_ptr(buf.ptr),
  869. &buf_user->length);
  870. break;
  871. }
  872. case MEMREADOOB32:
  873. {
  874. struct mtd_oob_buf32 buf;
  875. struct mtd_oob_buf32 __user *buf_user = argp;
  876. /* NOTE: writes return length to buf->start */
  877. if (copy_from_user(&buf, argp, sizeof(buf)))
  878. ret = -EFAULT;
  879. else
  880. ret = mtdchar_readoob(file, mtd, buf.start,
  881. buf.length, compat_ptr(buf.ptr),
  882. &buf_user->start);
  883. break;
  884. }
  885. default:
  886. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  887. }
  888. mutex_unlock(&mtd_mutex);
  889. return ret;
  890. }
  891. #endif /* CONFIG_COMPAT */
  892. /*
  893. * try to determine where a shared mapping can be made
  894. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  895. * mappings)
  896. */
  897. #ifndef CONFIG_MMU
  898. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  899. unsigned long addr,
  900. unsigned long len,
  901. unsigned long pgoff,
  902. unsigned long flags)
  903. {
  904. struct mtd_file_info *mfi = file->private_data;
  905. struct mtd_info *mtd = mfi->mtd;
  906. unsigned long offset;
  907. int ret;
  908. if (addr != 0)
  909. return (unsigned long) -EINVAL;
  910. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  911. return (unsigned long) -EINVAL;
  912. offset = pgoff << PAGE_SHIFT;
  913. if (offset > mtd->size - len)
  914. return (unsigned long) -EINVAL;
  915. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  916. return ret == -EOPNOTSUPP ? -ENODEV : ret;
  917. }
  918. #endif
  919. /*
  920. * set up a mapping for shared memory segments
  921. */
  922. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  923. {
  924. #ifdef CONFIG_MMU
  925. struct mtd_file_info *mfi = file->private_data;
  926. struct mtd_info *mtd = mfi->mtd;
  927. struct map_info *map = mtd->priv;
  928. /* This is broken because it assumes the MTD device is map-based
  929. and that mtd->priv is a valid struct map_info. It should be
  930. replaced with something that uses the mtd_get_unmapped_area()
  931. operation properly. */
  932. if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
  933. #ifdef pgprot_noncached
  934. if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
  935. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  936. #endif
  937. return vm_iomap_memory(vma, map->phys, map->size);
  938. }
  939. return -ENODEV;
  940. #else
  941. return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
  942. #endif
  943. }
  944. static const struct file_operations mtd_fops = {
  945. .owner = THIS_MODULE,
  946. .llseek = mtdchar_lseek,
  947. .read = mtdchar_read,
  948. .write = mtdchar_write,
  949. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  950. #ifdef CONFIG_COMPAT
  951. .compat_ioctl = mtdchar_compat_ioctl,
  952. #endif
  953. .open = mtdchar_open,
  954. .release = mtdchar_close,
  955. .mmap = mtdchar_mmap,
  956. #ifndef CONFIG_MMU
  957. .get_unmapped_area = mtdchar_get_unmapped_area,
  958. #endif
  959. };
  960. static const struct super_operations mtd_ops = {
  961. .drop_inode = generic_delete_inode,
  962. .statfs = simple_statfs,
  963. };
  964. static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
  965. int flags, const char *dev_name, void *data)
  966. {
  967. return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
  968. }
  969. static struct file_system_type mtd_inodefs_type = {
  970. .name = "mtd_inodefs",
  971. .mount = mtd_inodefs_mount,
  972. .kill_sb = kill_anon_super,
  973. };
  974. MODULE_ALIAS_FS("mtd_inodefs");
  975. int __init init_mtdchar(void)
  976. {
  977. int ret;
  978. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  979. "mtd", &mtd_fops);
  980. if (ret < 0) {
  981. pr_err("Can't allocate major number %d for MTD\n",
  982. MTD_CHAR_MAJOR);
  983. return ret;
  984. }
  985. ret = register_filesystem(&mtd_inodefs_type);
  986. if (ret) {
  987. pr_err("Can't register mtd_inodefs filesystem, error %d\n",
  988. ret);
  989. goto err_unregister_chdev;
  990. }
  991. return ret;
  992. err_unregister_chdev:
  993. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  994. return ret;
  995. }
  996. void __exit cleanup_mtdchar(void)
  997. {
  998. unregister_filesystem(&mtd_inodefs_type);
  999. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1000. }
  1001. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);