mtdchar.c 26 KB

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