mtdchar.c 27 KB

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