mtdchar.c 28 KB

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