mtdchar.c 26 KB

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