mtdchar.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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 <asm/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 void mtdchar_erase_callback (struct erase_info *instr)
  275. {
  276. wake_up((wait_queue_head_t *)instr->priv);
  277. }
  278. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  279. {
  280. struct mtd_info *mtd = mfi->mtd;
  281. size_t retlen;
  282. switch (mode) {
  283. case MTD_OTP_FACTORY:
  284. if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
  285. -EOPNOTSUPP)
  286. return -EOPNOTSUPP;
  287. mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
  288. break;
  289. case MTD_OTP_USER:
  290. if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
  291. -EOPNOTSUPP)
  292. return -EOPNOTSUPP;
  293. mfi->mode = MTD_FILE_MODE_OTP_USER;
  294. break;
  295. case MTD_OTP_OFF:
  296. mfi->mode = MTD_FILE_MODE_NORMAL;
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. return 0;
  302. }
  303. static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
  304. uint64_t start, uint32_t length, void __user *ptr,
  305. uint32_t __user *retp)
  306. {
  307. struct mtd_file_info *mfi = file->private_data;
  308. struct mtd_oob_ops ops;
  309. uint32_t retlen;
  310. int ret = 0;
  311. if (!(file->f_mode & FMODE_WRITE))
  312. return -EPERM;
  313. if (length > 4096)
  314. return -EINVAL;
  315. if (!mtd->_write_oob)
  316. ret = -EOPNOTSUPP;
  317. else
  318. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  319. if (ret)
  320. return ret;
  321. ops.ooblen = length;
  322. ops.ooboffs = start & (mtd->writesize - 1);
  323. ops.datbuf = NULL;
  324. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  325. MTD_OPS_PLACE_OOB;
  326. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  327. return -EINVAL;
  328. ops.oobbuf = memdup_user(ptr, length);
  329. if (IS_ERR(ops.oobbuf))
  330. return PTR_ERR(ops.oobbuf);
  331. start &= ~((uint64_t)mtd->writesize - 1);
  332. ret = mtd_write_oob(mtd, start, &ops);
  333. if (ops.oobretlen > 0xFFFFFFFFU)
  334. ret = -EOVERFLOW;
  335. retlen = ops.oobretlen;
  336. if (copy_to_user(retp, &retlen, sizeof(length)))
  337. ret = -EFAULT;
  338. kfree(ops.oobbuf);
  339. return ret;
  340. }
  341. static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
  342. uint64_t start, uint32_t length, void __user *ptr,
  343. uint32_t __user *retp)
  344. {
  345. struct mtd_file_info *mfi = file->private_data;
  346. struct mtd_oob_ops ops;
  347. int ret = 0;
  348. if (length > 4096)
  349. return -EINVAL;
  350. if (!access_ok(VERIFY_WRITE, ptr, length))
  351. return -EFAULT;
  352. ops.ooblen = length;
  353. ops.ooboffs = start & (mtd->writesize - 1);
  354. ops.datbuf = NULL;
  355. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  356. MTD_OPS_PLACE_OOB;
  357. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  358. return -EINVAL;
  359. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  360. if (!ops.oobbuf)
  361. return -ENOMEM;
  362. start &= ~((uint64_t)mtd->writesize - 1);
  363. ret = mtd_read_oob(mtd, start, &ops);
  364. if (put_user(ops.oobretlen, retp))
  365. ret = -EFAULT;
  366. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  367. ops.oobretlen))
  368. ret = -EFAULT;
  369. kfree(ops.oobbuf);
  370. /*
  371. * NAND returns -EBADMSG on ECC errors, but it returns the OOB
  372. * data. For our userspace tools it is important to dump areas
  373. * with ECC errors!
  374. * For kernel internal usage it also might return -EUCLEAN
  375. * to signal the caller that a bitflip has occured and has
  376. * been corrected by the ECC algorithm.
  377. *
  378. * Note: currently the standard NAND function, nand_read_oob_std,
  379. * does not calculate ECC for the OOB area, so do not rely on
  380. * this behavior unless you have replaced it with your own.
  381. */
  382. if (mtd_is_bitflip_or_eccerr(ret))
  383. return 0;
  384. return ret;
  385. }
  386. /*
  387. * Copies (and truncates, if necessary) OOB layout information to the
  388. * deprecated layout struct, nand_ecclayout_user. This is necessary only to
  389. * support the deprecated API ioctl ECCGETLAYOUT while allowing all new
  390. * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
  391. * can describe any kind of OOB layout with almost zero overhead from a
  392. * memory usage point of view).
  393. */
  394. static int shrink_ecclayout(struct mtd_info *mtd,
  395. struct nand_ecclayout_user *to)
  396. {
  397. struct mtd_oob_region oobregion;
  398. int i, section = 0, ret;
  399. if (!mtd || !to)
  400. return -EINVAL;
  401. memset(to, 0, sizeof(*to));
  402. to->eccbytes = 0;
  403. for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
  404. u32 eccpos;
  405. ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
  406. if (ret < 0) {
  407. if (ret != -ERANGE)
  408. return ret;
  409. break;
  410. }
  411. eccpos = oobregion.offset;
  412. for (; i < MTD_MAX_ECCPOS_ENTRIES &&
  413. eccpos < oobregion.offset + oobregion.length; i++) {
  414. to->eccpos[i] = eccpos++;
  415. to->eccbytes++;
  416. }
  417. }
  418. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  419. ret = mtd_ooblayout_free(mtd, i, &oobregion);
  420. if (ret < 0) {
  421. if (ret != -ERANGE)
  422. return ret;
  423. break;
  424. }
  425. to->oobfree[i].offset = oobregion.offset;
  426. to->oobfree[i].length = oobregion.length;
  427. to->oobavail += to->oobfree[i].length;
  428. }
  429. return 0;
  430. }
  431. static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
  432. {
  433. struct mtd_oob_region oobregion;
  434. int i, section = 0, ret;
  435. if (!mtd || !to)
  436. return -EINVAL;
  437. memset(to, 0, sizeof(*to));
  438. to->eccbytes = 0;
  439. for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
  440. u32 eccpos;
  441. ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
  442. if (ret < 0) {
  443. if (ret != -ERANGE)
  444. return ret;
  445. break;
  446. }
  447. if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
  448. return -EINVAL;
  449. eccpos = oobregion.offset;
  450. for (; eccpos < oobregion.offset + oobregion.length; i++) {
  451. to->eccpos[i] = eccpos++;
  452. to->eccbytes++;
  453. }
  454. }
  455. for (i = 0; i < 8; i++) {
  456. ret = mtd_ooblayout_free(mtd, i, &oobregion);
  457. if (ret < 0) {
  458. if (ret != -ERANGE)
  459. return ret;
  460. break;
  461. }
  462. to->oobfree[i][0] = oobregion.offset;
  463. to->oobfree[i][1] = oobregion.length;
  464. }
  465. to->useecc = MTD_NANDECC_AUTOPLACE;
  466. return 0;
  467. }
  468. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  469. struct blkpg_ioctl_arg *arg)
  470. {
  471. struct blkpg_partition p;
  472. if (!capable(CAP_SYS_ADMIN))
  473. return -EPERM;
  474. if (copy_from_user(&p, arg->data, sizeof(p)))
  475. return -EFAULT;
  476. switch (arg->op) {
  477. case BLKPG_ADD_PARTITION:
  478. /* Only master mtd device must be used to add partitions */
  479. if (mtd_is_partition(mtd))
  480. return -EINVAL;
  481. /* Sanitize user input */
  482. p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
  483. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  484. case BLKPG_DEL_PARTITION:
  485. if (p.pno < 0)
  486. return -EINVAL;
  487. return mtd_del_partition(mtd, p.pno);
  488. default:
  489. return -EINVAL;
  490. }
  491. }
  492. static int mtdchar_write_ioctl(struct mtd_info *mtd,
  493. struct mtd_write_req __user *argp)
  494. {
  495. struct mtd_write_req req;
  496. struct mtd_oob_ops ops;
  497. const void __user *usr_data, *usr_oob;
  498. int ret;
  499. if (copy_from_user(&req, argp, sizeof(req)))
  500. return -EFAULT;
  501. usr_data = (const void __user *)(uintptr_t)req.usr_data;
  502. usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
  503. if (!access_ok(VERIFY_READ, usr_data, req.len) ||
  504. !access_ok(VERIFY_READ, usr_oob, req.ooblen))
  505. return -EFAULT;
  506. if (!mtd->_write_oob)
  507. return -EOPNOTSUPP;
  508. ops.mode = req.mode;
  509. ops.len = (size_t)req.len;
  510. ops.ooblen = (size_t)req.ooblen;
  511. ops.ooboffs = 0;
  512. if (usr_data) {
  513. ops.datbuf = memdup_user(usr_data, ops.len);
  514. if (IS_ERR(ops.datbuf))
  515. return PTR_ERR(ops.datbuf);
  516. } else {
  517. ops.datbuf = NULL;
  518. }
  519. if (usr_oob) {
  520. ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
  521. if (IS_ERR(ops.oobbuf)) {
  522. kfree(ops.datbuf);
  523. return PTR_ERR(ops.oobbuf);
  524. }
  525. } else {
  526. ops.oobbuf = NULL;
  527. }
  528. ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
  529. kfree(ops.datbuf);
  530. kfree(ops.oobbuf);
  531. return ret;
  532. }
  533. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  534. {
  535. struct mtd_file_info *mfi = file->private_data;
  536. struct mtd_info *mtd = mfi->mtd;
  537. void __user *argp = (void __user *)arg;
  538. int ret = 0;
  539. u_long size;
  540. struct mtd_info_user info;
  541. pr_debug("MTD_ioctl\n");
  542. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  543. if (cmd & IOC_IN) {
  544. if (!access_ok(VERIFY_READ, argp, size))
  545. return -EFAULT;
  546. }
  547. if (cmd & IOC_OUT) {
  548. if (!access_ok(VERIFY_WRITE, argp, size))
  549. return -EFAULT;
  550. }
  551. switch (cmd) {
  552. case MEMGETREGIONCOUNT:
  553. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  554. return -EFAULT;
  555. break;
  556. case MEMGETREGIONINFO:
  557. {
  558. uint32_t ur_idx;
  559. struct mtd_erase_region_info *kr;
  560. struct region_info_user __user *ur = argp;
  561. if (get_user(ur_idx, &(ur->regionindex)))
  562. return -EFAULT;
  563. if (ur_idx >= mtd->numeraseregions)
  564. return -EINVAL;
  565. kr = &(mtd->eraseregions[ur_idx]);
  566. if (put_user(kr->offset, &(ur->offset))
  567. || put_user(kr->erasesize, &(ur->erasesize))
  568. || put_user(kr->numblocks, &(ur->numblocks)))
  569. return -EFAULT;
  570. break;
  571. }
  572. case MEMGETINFO:
  573. memset(&info, 0, sizeof(info));
  574. info.type = mtd->type;
  575. info.flags = mtd->flags;
  576. info.size = mtd->size;
  577. info.erasesize = mtd->erasesize;
  578. info.writesize = mtd->writesize;
  579. info.oobsize = mtd->oobsize;
  580. /* The below field is obsolete */
  581. info.padding = 0;
  582. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  583. return -EFAULT;
  584. break;
  585. case MEMERASE:
  586. case MEMERASE64:
  587. {
  588. struct erase_info *erase;
  589. if(!(file->f_mode & FMODE_WRITE))
  590. return -EPERM;
  591. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  592. if (!erase)
  593. ret = -ENOMEM;
  594. else {
  595. wait_queue_head_t waitq;
  596. DECLARE_WAITQUEUE(wait, current);
  597. init_waitqueue_head(&waitq);
  598. if (cmd == MEMERASE64) {
  599. struct erase_info_user64 einfo64;
  600. if (copy_from_user(&einfo64, argp,
  601. sizeof(struct erase_info_user64))) {
  602. kfree(erase);
  603. return -EFAULT;
  604. }
  605. erase->addr = einfo64.start;
  606. erase->len = einfo64.length;
  607. } else {
  608. struct erase_info_user einfo32;
  609. if (copy_from_user(&einfo32, argp,
  610. sizeof(struct erase_info_user))) {
  611. kfree(erase);
  612. return -EFAULT;
  613. }
  614. erase->addr = einfo32.start;
  615. erase->len = einfo32.length;
  616. }
  617. erase->mtd = mtd;
  618. erase->callback = mtdchar_erase_callback;
  619. erase->priv = (unsigned long)&waitq;
  620. /*
  621. FIXME: Allow INTERRUPTIBLE. Which means
  622. not having the wait_queue head on the stack.
  623. If the wq_head is on the stack, and we
  624. leave because we got interrupted, then the
  625. wq_head is no longer there when the
  626. callback routine tries to wake us up.
  627. */
  628. ret = mtd_erase(mtd, erase);
  629. if (!ret) {
  630. set_current_state(TASK_UNINTERRUPTIBLE);
  631. add_wait_queue(&waitq, &wait);
  632. if (erase->state != MTD_ERASE_DONE &&
  633. erase->state != MTD_ERASE_FAILED)
  634. schedule();
  635. remove_wait_queue(&waitq, &wait);
  636. set_current_state(TASK_RUNNING);
  637. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  638. }
  639. kfree(erase);
  640. }
  641. break;
  642. }
  643. case MEMWRITEOOB:
  644. {
  645. struct mtd_oob_buf buf;
  646. struct mtd_oob_buf __user *buf_user = argp;
  647. /* NOTE: writes return length to buf_user->length */
  648. if (copy_from_user(&buf, argp, sizeof(buf)))
  649. ret = -EFAULT;
  650. else
  651. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  652. buf.ptr, &buf_user->length);
  653. break;
  654. }
  655. case MEMREADOOB:
  656. {
  657. struct mtd_oob_buf buf;
  658. struct mtd_oob_buf __user *buf_user = argp;
  659. /* NOTE: writes return length to buf_user->start */
  660. if (copy_from_user(&buf, argp, sizeof(buf)))
  661. ret = -EFAULT;
  662. else
  663. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  664. buf.ptr, &buf_user->start);
  665. break;
  666. }
  667. case MEMWRITEOOB64:
  668. {
  669. struct mtd_oob_buf64 buf;
  670. struct mtd_oob_buf64 __user *buf_user = argp;
  671. if (copy_from_user(&buf, argp, sizeof(buf)))
  672. ret = -EFAULT;
  673. else
  674. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  675. (void __user *)(uintptr_t)buf.usr_ptr,
  676. &buf_user->length);
  677. break;
  678. }
  679. case MEMREADOOB64:
  680. {
  681. struct mtd_oob_buf64 buf;
  682. struct mtd_oob_buf64 __user *buf_user = argp;
  683. if (copy_from_user(&buf, argp, sizeof(buf)))
  684. ret = -EFAULT;
  685. else
  686. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  687. (void __user *)(uintptr_t)buf.usr_ptr,
  688. &buf_user->length);
  689. break;
  690. }
  691. case MEMWRITE:
  692. {
  693. ret = mtdchar_write_ioctl(mtd,
  694. (struct mtd_write_req __user *)arg);
  695. break;
  696. }
  697. case MEMLOCK:
  698. {
  699. struct erase_info_user einfo;
  700. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  701. return -EFAULT;
  702. ret = mtd_lock(mtd, einfo.start, einfo.length);
  703. break;
  704. }
  705. case MEMUNLOCK:
  706. {
  707. struct erase_info_user einfo;
  708. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  709. return -EFAULT;
  710. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  711. break;
  712. }
  713. case MEMISLOCKED:
  714. {
  715. struct erase_info_user einfo;
  716. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  717. return -EFAULT;
  718. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  719. break;
  720. }
  721. /* Legacy interface */
  722. case MEMGETOOBSEL:
  723. {
  724. struct nand_oobinfo oi;
  725. if (!mtd->ooblayout)
  726. return -EOPNOTSUPP;
  727. ret = get_oobinfo(mtd, &oi);
  728. if (ret)
  729. return ret;
  730. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  731. return -EFAULT;
  732. break;
  733. }
  734. case MEMGETBADBLOCK:
  735. {
  736. loff_t offs;
  737. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  738. return -EFAULT;
  739. return mtd_block_isbad(mtd, offs);
  740. break;
  741. }
  742. case MEMSETBADBLOCK:
  743. {
  744. loff_t offs;
  745. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  746. return -EFAULT;
  747. return mtd_block_markbad(mtd, offs);
  748. break;
  749. }
  750. case OTPSELECT:
  751. {
  752. int mode;
  753. if (copy_from_user(&mode, argp, sizeof(int)))
  754. return -EFAULT;
  755. mfi->mode = MTD_FILE_MODE_NORMAL;
  756. ret = otp_select_filemode(mfi, mode);
  757. file->f_pos = 0;
  758. break;
  759. }
  760. case OTPGETREGIONCOUNT:
  761. case OTPGETREGIONINFO:
  762. {
  763. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  764. size_t retlen;
  765. if (!buf)
  766. return -ENOMEM;
  767. switch (mfi->mode) {
  768. case MTD_FILE_MODE_OTP_FACTORY:
  769. ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
  770. break;
  771. case MTD_FILE_MODE_OTP_USER:
  772. ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
  773. break;
  774. default:
  775. ret = -EINVAL;
  776. break;
  777. }
  778. if (!ret) {
  779. if (cmd == OTPGETREGIONCOUNT) {
  780. int nbr = retlen / sizeof(struct otp_info);
  781. ret = copy_to_user(argp, &nbr, sizeof(int));
  782. } else
  783. ret = copy_to_user(argp, buf, retlen);
  784. if (ret)
  785. ret = -EFAULT;
  786. }
  787. kfree(buf);
  788. break;
  789. }
  790. case OTPLOCK:
  791. {
  792. struct otp_info oinfo;
  793. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  794. return -EINVAL;
  795. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  796. return -EFAULT;
  797. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  798. break;
  799. }
  800. /* This ioctl is being deprecated - it truncates the ECC layout */
  801. case ECCGETLAYOUT:
  802. {
  803. struct nand_ecclayout_user *usrlay;
  804. if (!mtd->ooblayout)
  805. return -EOPNOTSUPP;
  806. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  807. if (!usrlay)
  808. return -ENOMEM;
  809. shrink_ecclayout(mtd, usrlay);
  810. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  811. ret = -EFAULT;
  812. kfree(usrlay);
  813. break;
  814. }
  815. case ECCGETSTATS:
  816. {
  817. if (copy_to_user(argp, &mtd->ecc_stats,
  818. sizeof(struct mtd_ecc_stats)))
  819. return -EFAULT;
  820. break;
  821. }
  822. case MTDFILEMODE:
  823. {
  824. mfi->mode = 0;
  825. switch(arg) {
  826. case MTD_FILE_MODE_OTP_FACTORY:
  827. case MTD_FILE_MODE_OTP_USER:
  828. ret = otp_select_filemode(mfi, arg);
  829. break;
  830. case MTD_FILE_MODE_RAW:
  831. if (!mtd_has_oob(mtd))
  832. return -EOPNOTSUPP;
  833. mfi->mode = arg;
  834. case MTD_FILE_MODE_NORMAL:
  835. break;
  836. default:
  837. ret = -EINVAL;
  838. }
  839. file->f_pos = 0;
  840. break;
  841. }
  842. case BLKPG:
  843. {
  844. struct blkpg_ioctl_arg __user *blk_arg = argp;
  845. struct blkpg_ioctl_arg a;
  846. if (copy_from_user(&a, blk_arg, sizeof(a)))
  847. ret = -EFAULT;
  848. else
  849. ret = mtdchar_blkpg_ioctl(mtd, &a);
  850. break;
  851. }
  852. case BLKRRPART:
  853. {
  854. /* No reread partition feature. Just return ok */
  855. ret = 0;
  856. break;
  857. }
  858. default:
  859. ret = -ENOTTY;
  860. }
  861. return ret;
  862. } /* memory_ioctl */
  863. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  864. {
  865. int ret;
  866. mutex_lock(&mtd_mutex);
  867. ret = mtdchar_ioctl(file, cmd, arg);
  868. mutex_unlock(&mtd_mutex);
  869. return ret;
  870. }
  871. #ifdef CONFIG_COMPAT
  872. struct mtd_oob_buf32 {
  873. u_int32_t start;
  874. u_int32_t length;
  875. compat_caddr_t ptr; /* unsigned char* */
  876. };
  877. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  878. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  879. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  880. unsigned long arg)
  881. {
  882. struct mtd_file_info *mfi = file->private_data;
  883. struct mtd_info *mtd = mfi->mtd;
  884. void __user *argp = compat_ptr(arg);
  885. int ret = 0;
  886. mutex_lock(&mtd_mutex);
  887. switch (cmd) {
  888. case MEMWRITEOOB32:
  889. {
  890. struct mtd_oob_buf32 buf;
  891. struct mtd_oob_buf32 __user *buf_user = argp;
  892. if (copy_from_user(&buf, argp, sizeof(buf)))
  893. ret = -EFAULT;
  894. else
  895. ret = mtdchar_writeoob(file, mtd, buf.start,
  896. buf.length, compat_ptr(buf.ptr),
  897. &buf_user->length);
  898. break;
  899. }
  900. case MEMREADOOB32:
  901. {
  902. struct mtd_oob_buf32 buf;
  903. struct mtd_oob_buf32 __user *buf_user = argp;
  904. /* NOTE: writes return length to buf->start */
  905. if (copy_from_user(&buf, argp, sizeof(buf)))
  906. ret = -EFAULT;
  907. else
  908. ret = mtdchar_readoob(file, mtd, buf.start,
  909. buf.length, compat_ptr(buf.ptr),
  910. &buf_user->start);
  911. break;
  912. }
  913. case BLKPG:
  914. {
  915. /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
  916. struct blkpg_compat_ioctl_arg __user *uarg = argp;
  917. struct blkpg_compat_ioctl_arg compat_arg;
  918. struct blkpg_ioctl_arg a;
  919. if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
  920. ret = -EFAULT;
  921. break;
  922. }
  923. memset(&a, 0, sizeof(a));
  924. a.op = compat_arg.op;
  925. a.flags = compat_arg.flags;
  926. a.datalen = compat_arg.datalen;
  927. a.data = compat_ptr(compat_arg.data);
  928. ret = mtdchar_blkpg_ioctl(mtd, &a);
  929. break;
  930. }
  931. default:
  932. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  933. }
  934. mutex_unlock(&mtd_mutex);
  935. return ret;
  936. }
  937. #endif /* CONFIG_COMPAT */
  938. /*
  939. * try to determine where a shared mapping can be made
  940. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  941. * mappings)
  942. */
  943. #ifndef CONFIG_MMU
  944. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  945. unsigned long addr,
  946. unsigned long len,
  947. unsigned long pgoff,
  948. unsigned long flags)
  949. {
  950. struct mtd_file_info *mfi = file->private_data;
  951. struct mtd_info *mtd = mfi->mtd;
  952. unsigned long offset;
  953. int ret;
  954. if (addr != 0)
  955. return (unsigned long) -EINVAL;
  956. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  957. return (unsigned long) -EINVAL;
  958. offset = pgoff << PAGE_SHIFT;
  959. if (offset > mtd->size - len)
  960. return (unsigned long) -EINVAL;
  961. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  962. return ret == -EOPNOTSUPP ? -ENODEV : ret;
  963. }
  964. static unsigned mtdchar_mmap_capabilities(struct file *file)
  965. {
  966. struct mtd_file_info *mfi = file->private_data;
  967. return mtd_mmap_capabilities(mfi->mtd);
  968. }
  969. #endif
  970. /*
  971. * set up a mapping for shared memory segments
  972. */
  973. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  974. {
  975. #ifdef CONFIG_MMU
  976. struct mtd_file_info *mfi = file->private_data;
  977. struct mtd_info *mtd = mfi->mtd;
  978. struct map_info *map = mtd->priv;
  979. /* This is broken because it assumes the MTD device is map-based
  980. and that mtd->priv is a valid struct map_info. It should be
  981. replaced with something that uses the mtd_get_unmapped_area()
  982. operation properly. */
  983. if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
  984. #ifdef pgprot_noncached
  985. if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
  986. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  987. #endif
  988. return vm_iomap_memory(vma, map->phys, map->size);
  989. }
  990. return -ENODEV;
  991. #else
  992. return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
  993. #endif
  994. }
  995. static const struct file_operations mtd_fops = {
  996. .owner = THIS_MODULE,
  997. .llseek = mtdchar_lseek,
  998. .read = mtdchar_read,
  999. .write = mtdchar_write,
  1000. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  1001. #ifdef CONFIG_COMPAT
  1002. .compat_ioctl = mtdchar_compat_ioctl,
  1003. #endif
  1004. .open = mtdchar_open,
  1005. .release = mtdchar_close,
  1006. .mmap = mtdchar_mmap,
  1007. #ifndef CONFIG_MMU
  1008. .get_unmapped_area = mtdchar_get_unmapped_area,
  1009. .mmap_capabilities = mtdchar_mmap_capabilities,
  1010. #endif
  1011. };
  1012. int __init init_mtdchar(void)
  1013. {
  1014. int ret;
  1015. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  1016. "mtd", &mtd_fops);
  1017. if (ret < 0) {
  1018. pr_err("Can't allocate major number %d for MTD\n",
  1019. MTD_CHAR_MAJOR);
  1020. return ret;
  1021. }
  1022. return ret;
  1023. }
  1024. void __exit cleanup_mtdchar(void)
  1025. {
  1026. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1027. }
  1028. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);