mtdchar.c 27 KB

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