mtdchar.c 28 KB

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