mtdchar.c 27 KB

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