mtdchar.c 28 KB

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