mtdchar.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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) data from the larger struct,
  388. * nand_ecclayout, to the smaller, deprecated layout struct,
  389. * nand_ecclayout_user. This is necessary only to support the deprecated
  390. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  391. * nand_ecclayout flexibly (i.e. the struct may change size in new
  392. * releases without requiring major rewrites).
  393. */
  394. static int shrink_ecclayout(const struct nand_ecclayout *from,
  395. struct nand_ecclayout_user *to)
  396. {
  397. int i;
  398. if (!from || !to)
  399. return -EINVAL;
  400. memset(to, 0, sizeof(*to));
  401. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  402. for (i = 0; i < to->eccbytes; i++)
  403. to->eccpos[i] = from->eccpos[i];
  404. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  405. if (from->oobfree[i].length == 0 &&
  406. from->oobfree[i].offset == 0)
  407. break;
  408. to->oobavail += from->oobfree[i].length;
  409. to->oobfree[i] = from->oobfree[i];
  410. }
  411. return 0;
  412. }
  413. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  414. struct blkpg_ioctl_arg *arg)
  415. {
  416. struct blkpg_partition p;
  417. if (!capable(CAP_SYS_ADMIN))
  418. return -EPERM;
  419. if (copy_from_user(&p, arg->data, sizeof(p)))
  420. return -EFAULT;
  421. switch (arg->op) {
  422. case BLKPG_ADD_PARTITION:
  423. /* Only master mtd device must be used to add partitions */
  424. if (mtd_is_partition(mtd))
  425. return -EINVAL;
  426. /* Sanitize user input */
  427. p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
  428. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  429. case BLKPG_DEL_PARTITION:
  430. if (p.pno < 0)
  431. return -EINVAL;
  432. return mtd_del_partition(mtd, p.pno);
  433. default:
  434. return -EINVAL;
  435. }
  436. }
  437. static int mtdchar_write_ioctl(struct mtd_info *mtd,
  438. struct mtd_write_req __user *argp)
  439. {
  440. struct mtd_write_req req;
  441. struct mtd_oob_ops ops;
  442. const void __user *usr_data, *usr_oob;
  443. int ret;
  444. if (copy_from_user(&req, argp, sizeof(req)))
  445. return -EFAULT;
  446. usr_data = (const void __user *)(uintptr_t)req.usr_data;
  447. usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
  448. if (!access_ok(VERIFY_READ, usr_data, req.len) ||
  449. !access_ok(VERIFY_READ, usr_oob, req.ooblen))
  450. return -EFAULT;
  451. if (!mtd->_write_oob)
  452. return -EOPNOTSUPP;
  453. ops.mode = req.mode;
  454. ops.len = (size_t)req.len;
  455. ops.ooblen = (size_t)req.ooblen;
  456. ops.ooboffs = 0;
  457. if (usr_data) {
  458. ops.datbuf = memdup_user(usr_data, ops.len);
  459. if (IS_ERR(ops.datbuf))
  460. return PTR_ERR(ops.datbuf);
  461. } else {
  462. ops.datbuf = NULL;
  463. }
  464. if (usr_oob) {
  465. ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
  466. if (IS_ERR(ops.oobbuf)) {
  467. kfree(ops.datbuf);
  468. return PTR_ERR(ops.oobbuf);
  469. }
  470. } else {
  471. ops.oobbuf = NULL;
  472. }
  473. ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
  474. kfree(ops.datbuf);
  475. kfree(ops.oobbuf);
  476. return ret;
  477. }
  478. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  479. {
  480. struct mtd_file_info *mfi = file->private_data;
  481. struct mtd_info *mtd = mfi->mtd;
  482. void __user *argp = (void __user *)arg;
  483. int ret = 0;
  484. u_long size;
  485. struct mtd_info_user info;
  486. pr_debug("MTD_ioctl\n");
  487. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  488. if (cmd & IOC_IN) {
  489. if (!access_ok(VERIFY_READ, argp, size))
  490. return -EFAULT;
  491. }
  492. if (cmd & IOC_OUT) {
  493. if (!access_ok(VERIFY_WRITE, argp, size))
  494. return -EFAULT;
  495. }
  496. switch (cmd) {
  497. case MEMGETREGIONCOUNT:
  498. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  499. return -EFAULT;
  500. break;
  501. case MEMGETREGIONINFO:
  502. {
  503. uint32_t ur_idx;
  504. struct mtd_erase_region_info *kr;
  505. struct region_info_user __user *ur = argp;
  506. if (get_user(ur_idx, &(ur->regionindex)))
  507. return -EFAULT;
  508. if (ur_idx >= mtd->numeraseregions)
  509. return -EINVAL;
  510. kr = &(mtd->eraseregions[ur_idx]);
  511. if (put_user(kr->offset, &(ur->offset))
  512. || put_user(kr->erasesize, &(ur->erasesize))
  513. || put_user(kr->numblocks, &(ur->numblocks)))
  514. return -EFAULT;
  515. break;
  516. }
  517. case MEMGETINFO:
  518. memset(&info, 0, sizeof(info));
  519. info.type = mtd->type;
  520. info.flags = mtd->flags;
  521. info.size = mtd->size;
  522. info.erasesize = mtd->erasesize;
  523. info.writesize = mtd->writesize;
  524. info.oobsize = mtd->oobsize;
  525. /* The below field is obsolete */
  526. info.padding = 0;
  527. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  528. return -EFAULT;
  529. break;
  530. case MEMERASE:
  531. case MEMERASE64:
  532. {
  533. struct erase_info *erase;
  534. if(!(file->f_mode & FMODE_WRITE))
  535. return -EPERM;
  536. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  537. if (!erase)
  538. ret = -ENOMEM;
  539. else {
  540. wait_queue_head_t waitq;
  541. DECLARE_WAITQUEUE(wait, current);
  542. init_waitqueue_head(&waitq);
  543. if (cmd == MEMERASE64) {
  544. struct erase_info_user64 einfo64;
  545. if (copy_from_user(&einfo64, argp,
  546. sizeof(struct erase_info_user64))) {
  547. kfree(erase);
  548. return -EFAULT;
  549. }
  550. erase->addr = einfo64.start;
  551. erase->len = einfo64.length;
  552. } else {
  553. struct erase_info_user einfo32;
  554. if (copy_from_user(&einfo32, argp,
  555. sizeof(struct erase_info_user))) {
  556. kfree(erase);
  557. return -EFAULT;
  558. }
  559. erase->addr = einfo32.start;
  560. erase->len = einfo32.length;
  561. }
  562. erase->mtd = mtd;
  563. erase->callback = mtdchar_erase_callback;
  564. erase->priv = (unsigned long)&waitq;
  565. /*
  566. FIXME: Allow INTERRUPTIBLE. Which means
  567. not having the wait_queue head on the stack.
  568. If the wq_head is on the stack, and we
  569. leave because we got interrupted, then the
  570. wq_head is no longer there when the
  571. callback routine tries to wake us up.
  572. */
  573. ret = mtd_erase(mtd, erase);
  574. if (!ret) {
  575. set_current_state(TASK_UNINTERRUPTIBLE);
  576. add_wait_queue(&waitq, &wait);
  577. if (erase->state != MTD_ERASE_DONE &&
  578. erase->state != MTD_ERASE_FAILED)
  579. schedule();
  580. remove_wait_queue(&waitq, &wait);
  581. set_current_state(TASK_RUNNING);
  582. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  583. }
  584. kfree(erase);
  585. }
  586. break;
  587. }
  588. case MEMWRITEOOB:
  589. {
  590. struct mtd_oob_buf buf;
  591. struct mtd_oob_buf __user *buf_user = argp;
  592. /* NOTE: writes return length to buf_user->length */
  593. if (copy_from_user(&buf, argp, sizeof(buf)))
  594. ret = -EFAULT;
  595. else
  596. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  597. buf.ptr, &buf_user->length);
  598. break;
  599. }
  600. case MEMREADOOB:
  601. {
  602. struct mtd_oob_buf buf;
  603. struct mtd_oob_buf __user *buf_user = argp;
  604. /* NOTE: writes return length to buf_user->start */
  605. if (copy_from_user(&buf, argp, sizeof(buf)))
  606. ret = -EFAULT;
  607. else
  608. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  609. buf.ptr, &buf_user->start);
  610. break;
  611. }
  612. case MEMWRITEOOB64:
  613. {
  614. struct mtd_oob_buf64 buf;
  615. struct mtd_oob_buf64 __user *buf_user = argp;
  616. if (copy_from_user(&buf, argp, sizeof(buf)))
  617. ret = -EFAULT;
  618. else
  619. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  620. (void __user *)(uintptr_t)buf.usr_ptr,
  621. &buf_user->length);
  622. break;
  623. }
  624. case MEMREADOOB64:
  625. {
  626. struct mtd_oob_buf64 buf;
  627. struct mtd_oob_buf64 __user *buf_user = argp;
  628. if (copy_from_user(&buf, argp, sizeof(buf)))
  629. ret = -EFAULT;
  630. else
  631. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  632. (void __user *)(uintptr_t)buf.usr_ptr,
  633. &buf_user->length);
  634. break;
  635. }
  636. case MEMWRITE:
  637. {
  638. ret = mtdchar_write_ioctl(mtd,
  639. (struct mtd_write_req __user *)arg);
  640. break;
  641. }
  642. case MEMLOCK:
  643. {
  644. struct erase_info_user einfo;
  645. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  646. return -EFAULT;
  647. ret = mtd_lock(mtd, einfo.start, einfo.length);
  648. break;
  649. }
  650. case MEMUNLOCK:
  651. {
  652. struct erase_info_user einfo;
  653. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  654. return -EFAULT;
  655. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  656. break;
  657. }
  658. case MEMISLOCKED:
  659. {
  660. struct erase_info_user einfo;
  661. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  662. return -EFAULT;
  663. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  664. break;
  665. }
  666. /* Legacy interface */
  667. case MEMGETOOBSEL:
  668. {
  669. struct nand_oobinfo oi;
  670. if (!mtd->ecclayout)
  671. return -EOPNOTSUPP;
  672. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  673. return -EINVAL;
  674. oi.useecc = MTD_NANDECC_AUTOPLACE;
  675. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  676. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  677. sizeof(oi.oobfree));
  678. oi.eccbytes = mtd->ecclayout->eccbytes;
  679. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  680. return -EFAULT;
  681. break;
  682. }
  683. case MEMGETBADBLOCK:
  684. {
  685. loff_t offs;
  686. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  687. return -EFAULT;
  688. return mtd_block_isbad(mtd, offs);
  689. break;
  690. }
  691. case MEMSETBADBLOCK:
  692. {
  693. loff_t offs;
  694. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  695. return -EFAULT;
  696. return mtd_block_markbad(mtd, offs);
  697. break;
  698. }
  699. case OTPSELECT:
  700. {
  701. int mode;
  702. if (copy_from_user(&mode, argp, sizeof(int)))
  703. return -EFAULT;
  704. mfi->mode = MTD_FILE_MODE_NORMAL;
  705. ret = otp_select_filemode(mfi, mode);
  706. file->f_pos = 0;
  707. break;
  708. }
  709. case OTPGETREGIONCOUNT:
  710. case OTPGETREGIONINFO:
  711. {
  712. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  713. size_t retlen;
  714. if (!buf)
  715. return -ENOMEM;
  716. switch (mfi->mode) {
  717. case MTD_FILE_MODE_OTP_FACTORY:
  718. ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
  719. break;
  720. case MTD_FILE_MODE_OTP_USER:
  721. ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
  722. break;
  723. default:
  724. ret = -EINVAL;
  725. break;
  726. }
  727. if (!ret) {
  728. if (cmd == OTPGETREGIONCOUNT) {
  729. int nbr = retlen / sizeof(struct otp_info);
  730. ret = copy_to_user(argp, &nbr, sizeof(int));
  731. } else
  732. ret = copy_to_user(argp, buf, retlen);
  733. if (ret)
  734. ret = -EFAULT;
  735. }
  736. kfree(buf);
  737. break;
  738. }
  739. case OTPLOCK:
  740. {
  741. struct otp_info oinfo;
  742. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  743. return -EINVAL;
  744. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  745. return -EFAULT;
  746. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  747. break;
  748. }
  749. /* This ioctl is being deprecated - it truncates the ECC layout */
  750. case ECCGETLAYOUT:
  751. {
  752. struct nand_ecclayout_user *usrlay;
  753. if (!mtd->ecclayout)
  754. return -EOPNOTSUPP;
  755. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  756. if (!usrlay)
  757. return -ENOMEM;
  758. shrink_ecclayout(mtd->ecclayout, usrlay);
  759. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  760. ret = -EFAULT;
  761. kfree(usrlay);
  762. break;
  763. }
  764. case ECCGETSTATS:
  765. {
  766. if (copy_to_user(argp, &mtd->ecc_stats,
  767. sizeof(struct mtd_ecc_stats)))
  768. return -EFAULT;
  769. break;
  770. }
  771. case MTDFILEMODE:
  772. {
  773. mfi->mode = 0;
  774. switch(arg) {
  775. case MTD_FILE_MODE_OTP_FACTORY:
  776. case MTD_FILE_MODE_OTP_USER:
  777. ret = otp_select_filemode(mfi, arg);
  778. break;
  779. case MTD_FILE_MODE_RAW:
  780. if (!mtd_has_oob(mtd))
  781. return -EOPNOTSUPP;
  782. mfi->mode = arg;
  783. case MTD_FILE_MODE_NORMAL:
  784. break;
  785. default:
  786. ret = -EINVAL;
  787. }
  788. file->f_pos = 0;
  789. break;
  790. }
  791. case BLKPG:
  792. {
  793. struct blkpg_ioctl_arg __user *blk_arg = argp;
  794. struct blkpg_ioctl_arg a;
  795. if (copy_from_user(&a, blk_arg, sizeof(a)))
  796. ret = -EFAULT;
  797. else
  798. ret = mtdchar_blkpg_ioctl(mtd, &a);
  799. break;
  800. }
  801. case BLKRRPART:
  802. {
  803. /* No reread partition feature. Just return ok */
  804. ret = 0;
  805. break;
  806. }
  807. default:
  808. ret = -ENOTTY;
  809. }
  810. return ret;
  811. } /* memory_ioctl */
  812. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  813. {
  814. int ret;
  815. mutex_lock(&mtd_mutex);
  816. ret = mtdchar_ioctl(file, cmd, arg);
  817. mutex_unlock(&mtd_mutex);
  818. return ret;
  819. }
  820. #ifdef CONFIG_COMPAT
  821. struct mtd_oob_buf32 {
  822. u_int32_t start;
  823. u_int32_t length;
  824. compat_caddr_t ptr; /* unsigned char* */
  825. };
  826. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  827. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  828. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  829. unsigned long arg)
  830. {
  831. struct mtd_file_info *mfi = file->private_data;
  832. struct mtd_info *mtd = mfi->mtd;
  833. void __user *argp = compat_ptr(arg);
  834. int ret = 0;
  835. mutex_lock(&mtd_mutex);
  836. switch (cmd) {
  837. case MEMWRITEOOB32:
  838. {
  839. struct mtd_oob_buf32 buf;
  840. struct mtd_oob_buf32 __user *buf_user = argp;
  841. if (copy_from_user(&buf, argp, sizeof(buf)))
  842. ret = -EFAULT;
  843. else
  844. ret = mtdchar_writeoob(file, mtd, buf.start,
  845. buf.length, compat_ptr(buf.ptr),
  846. &buf_user->length);
  847. break;
  848. }
  849. case MEMREADOOB32:
  850. {
  851. struct mtd_oob_buf32 buf;
  852. struct mtd_oob_buf32 __user *buf_user = argp;
  853. /* NOTE: writes return length to buf->start */
  854. if (copy_from_user(&buf, argp, sizeof(buf)))
  855. ret = -EFAULT;
  856. else
  857. ret = mtdchar_readoob(file, mtd, buf.start,
  858. buf.length, compat_ptr(buf.ptr),
  859. &buf_user->start);
  860. break;
  861. }
  862. case BLKPG:
  863. {
  864. /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
  865. struct blkpg_compat_ioctl_arg __user *uarg = argp;
  866. struct blkpg_compat_ioctl_arg compat_arg;
  867. struct blkpg_ioctl_arg a;
  868. if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
  869. ret = -EFAULT;
  870. break;
  871. }
  872. memset(&a, 0, sizeof(a));
  873. a.op = compat_arg.op;
  874. a.flags = compat_arg.flags;
  875. a.datalen = compat_arg.datalen;
  876. a.data = compat_ptr(compat_arg.data);
  877. ret = mtdchar_blkpg_ioctl(mtd, &a);
  878. break;
  879. }
  880. default:
  881. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  882. }
  883. mutex_unlock(&mtd_mutex);
  884. return ret;
  885. }
  886. #endif /* CONFIG_COMPAT */
  887. /*
  888. * try to determine where a shared mapping can be made
  889. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  890. * mappings)
  891. */
  892. #ifndef CONFIG_MMU
  893. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  894. unsigned long addr,
  895. unsigned long len,
  896. unsigned long pgoff,
  897. unsigned long flags)
  898. {
  899. struct mtd_file_info *mfi = file->private_data;
  900. struct mtd_info *mtd = mfi->mtd;
  901. unsigned long offset;
  902. int ret;
  903. if (addr != 0)
  904. return (unsigned long) -EINVAL;
  905. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  906. return (unsigned long) -EINVAL;
  907. offset = pgoff << PAGE_SHIFT;
  908. if (offset > mtd->size - len)
  909. return (unsigned long) -EINVAL;
  910. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  911. return ret == -EOPNOTSUPP ? -ENODEV : ret;
  912. }
  913. static unsigned mtdchar_mmap_capabilities(struct file *file)
  914. {
  915. struct mtd_file_info *mfi = file->private_data;
  916. return mtd_mmap_capabilities(mfi->mtd);
  917. }
  918. #endif
  919. /*
  920. * set up a mapping for shared memory segments
  921. */
  922. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  923. {
  924. #ifdef CONFIG_MMU
  925. struct mtd_file_info *mfi = file->private_data;
  926. struct mtd_info *mtd = mfi->mtd;
  927. struct map_info *map = mtd->priv;
  928. /* This is broken because it assumes the MTD device is map-based
  929. and that mtd->priv is a valid struct map_info. It should be
  930. replaced with something that uses the mtd_get_unmapped_area()
  931. operation properly. */
  932. if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
  933. #ifdef pgprot_noncached
  934. if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
  935. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  936. #endif
  937. return vm_iomap_memory(vma, map->phys, map->size);
  938. }
  939. return -ENODEV;
  940. #else
  941. return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
  942. #endif
  943. }
  944. static const struct file_operations mtd_fops = {
  945. .owner = THIS_MODULE,
  946. .llseek = mtdchar_lseek,
  947. .read = mtdchar_read,
  948. .write = mtdchar_write,
  949. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  950. #ifdef CONFIG_COMPAT
  951. .compat_ioctl = mtdchar_compat_ioctl,
  952. #endif
  953. .open = mtdchar_open,
  954. .release = mtdchar_close,
  955. .mmap = mtdchar_mmap,
  956. #ifndef CONFIG_MMU
  957. .get_unmapped_area = mtdchar_get_unmapped_area,
  958. .mmap_capabilities = mtdchar_mmap_capabilities,
  959. #endif
  960. };
  961. int __init init_mtdchar(void)
  962. {
  963. int ret;
  964. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  965. "mtd", &mtd_fops);
  966. if (ret < 0) {
  967. pr_err("Can't allocate major number %d for MTD\n",
  968. MTD_CHAR_MAJOR);
  969. return ret;
  970. }
  971. return ret;
  972. }
  973. void __exit cleanup_mtdchar(void)
  974. {
  975. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  976. }
  977. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);