mtdchar.c 26 KB

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