mtdchar.c 26 KB

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