mtdchar.c 26 KB

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