mtdchar.c 28 KB

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