mtdchar.c 27 KB

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