mtdchar.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/mutex.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/compat.h>
  31. #include <linux/mount.h>
  32. #include <linux/blkpg.h>
  33. #include <linux/magic.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/mtd/map.h>
  37. #include <asm/uaccess.h>
  38. static DEFINE_MUTEX(mtd_mutex);
  39. /*
  40. * Data structure to hold the pointer to the mtd device as well
  41. * as mode information of various use cases.
  42. */
  43. struct mtd_file_info {
  44. struct mtd_info *mtd;
  45. struct inode *ino;
  46. enum mtd_file_modes mode;
  47. };
  48. static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
  49. {
  50. struct mtd_file_info *mfi = file->private_data;
  51. struct mtd_info *mtd = mfi->mtd;
  52. switch (orig) {
  53. case SEEK_SET:
  54. break;
  55. case SEEK_CUR:
  56. offset += file->f_pos;
  57. break;
  58. case SEEK_END:
  59. offset += mtd->size;
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. if (offset >= 0 && offset <= mtd->size)
  65. return file->f_pos = offset;
  66. return -EINVAL;
  67. }
  68. static int count;
  69. static struct vfsmount *mnt;
  70. static struct file_system_type mtd_inodefs_type;
  71. static int mtdchar_open(struct inode *inode, struct file *file)
  72. {
  73. int minor = iminor(inode);
  74. int devnum = minor >> 1;
  75. int ret = 0;
  76. struct mtd_info *mtd;
  77. struct mtd_file_info *mfi;
  78. struct inode *mtd_ino;
  79. pr_debug("MTD_open\n");
  80. /* You can't open the RO devices RW */
  81. if ((file->f_mode & FMODE_WRITE) && (minor & 1))
  82. return -EACCES;
  83. ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
  84. if (ret)
  85. return ret;
  86. mutex_lock(&mtd_mutex);
  87. mtd = get_mtd_device(NULL, devnum);
  88. if (IS_ERR(mtd)) {
  89. ret = PTR_ERR(mtd);
  90. goto out;
  91. }
  92. if (mtd->type == MTD_ABSENT) {
  93. ret = -ENODEV;
  94. goto out1;
  95. }
  96. mtd_ino = iget_locked(mnt->mnt_sb, devnum);
  97. if (!mtd_ino) {
  98. ret = -ENOMEM;
  99. goto out1;
  100. }
  101. if (mtd_ino->i_state & I_NEW) {
  102. mtd_ino->i_private = mtd;
  103. mtd_ino->i_mode = S_IFCHR;
  104. mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
  105. unlock_new_inode(mtd_ino);
  106. }
  107. file->f_mapping = mtd_ino->i_mapping;
  108. /* You can't open it RW if it's not a writeable device */
  109. if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
  110. ret = -EACCES;
  111. goto out2;
  112. }
  113. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  114. if (!mfi) {
  115. ret = -ENOMEM;
  116. goto out2;
  117. }
  118. mfi->ino = mtd_ino;
  119. mfi->mtd = mtd;
  120. file->private_data = mfi;
  121. mutex_unlock(&mtd_mutex);
  122. return 0;
  123. out2:
  124. iput(mtd_ino);
  125. out1:
  126. put_mtd_device(mtd);
  127. out:
  128. mutex_unlock(&mtd_mutex);
  129. simple_release_fs(&mnt, &count);
  130. return ret;
  131. } /* mtdchar_open */
  132. /*====================================================================*/
  133. static int mtdchar_close(struct inode *inode, struct file *file)
  134. {
  135. struct mtd_file_info *mfi = file->private_data;
  136. struct mtd_info *mtd = mfi->mtd;
  137. pr_debug("MTD_close\n");
  138. /* Only sync if opened RW */
  139. if ((file->f_mode & FMODE_WRITE))
  140. mtd_sync(mtd);
  141. iput(mfi->ino);
  142. put_mtd_device(mtd);
  143. file->private_data = NULL;
  144. kfree(mfi);
  145. simple_release_fs(&mnt, &count);
  146. return 0;
  147. } /* mtdchar_close */
  148. /* Back in June 2001, dwmw2 wrote:
  149. *
  150. * FIXME: This _really_ needs to die. In 2.5, we should lock the
  151. * userspace buffer down and use it directly with readv/writev.
  152. *
  153. * The implementation below, using mtd_kmalloc_up_to, mitigates
  154. * allocation failures when the system is under low-memory situations
  155. * or if memory is highly fragmented at the cost of reducing the
  156. * performance of the requested transfer due to a smaller buffer size.
  157. *
  158. * A more complex but more memory-efficient implementation based on
  159. * get_user_pages and iovecs to cover extents of those pages is a
  160. * longer-term goal, as intimated by dwmw2 above. However, for the
  161. * write case, this requires yet more complex head and tail transfer
  162. * handling when those head and tail offsets and sizes are such that
  163. * alignment requirements are not met in the NAND subdriver.
  164. */
  165. static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
  166. loff_t *ppos)
  167. {
  168. struct mtd_file_info *mfi = file->private_data;
  169. struct mtd_info *mtd = mfi->mtd;
  170. size_t retlen;
  171. size_t total_retlen=0;
  172. int ret=0;
  173. int len;
  174. size_t size = count;
  175. char *kbuf;
  176. pr_debug("MTD_read\n");
  177. if (*ppos + count > mtd->size)
  178. count = mtd->size - *ppos;
  179. if (!count)
  180. return 0;
  181. kbuf = mtd_kmalloc_up_to(mtd, &size);
  182. if (!kbuf)
  183. return -ENOMEM;
  184. while (count) {
  185. len = min_t(size_t, count, size);
  186. switch (mfi->mode) {
  187. case MTD_FILE_MODE_OTP_FACTORY:
  188. ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
  189. &retlen, kbuf);
  190. break;
  191. case MTD_FILE_MODE_OTP_USER:
  192. ret = mtd_read_user_prot_reg(mtd, *ppos, len,
  193. &retlen, kbuf);
  194. break;
  195. case MTD_FILE_MODE_RAW:
  196. {
  197. struct mtd_oob_ops ops;
  198. ops.mode = MTD_OPS_RAW;
  199. ops.datbuf = kbuf;
  200. ops.oobbuf = NULL;
  201. ops.len = len;
  202. ret = mtd_read_oob(mtd, *ppos, &ops);
  203. retlen = ops.retlen;
  204. break;
  205. }
  206. default:
  207. ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
  208. }
  209. /* Nand returns -EBADMSG on ECC errors, but it returns
  210. * the data. For our userspace tools it is important
  211. * to dump areas with ECC errors!
  212. * For kernel internal usage it also might return -EUCLEAN
  213. * to signal the caller that a bitflip has occurred and has
  214. * been corrected by the ECC algorithm.
  215. * Userspace software which accesses NAND this way
  216. * must be aware of the fact that it deals with NAND
  217. */
  218. if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
  219. *ppos += retlen;
  220. if (copy_to_user(buf, kbuf, retlen)) {
  221. kfree(kbuf);
  222. return -EFAULT;
  223. }
  224. else
  225. total_retlen += retlen;
  226. count -= retlen;
  227. buf += retlen;
  228. if (retlen == 0)
  229. count = 0;
  230. }
  231. else {
  232. kfree(kbuf);
  233. return ret;
  234. }
  235. }
  236. kfree(kbuf);
  237. return total_retlen;
  238. } /* mtdchar_read */
  239. static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
  240. loff_t *ppos)
  241. {
  242. struct mtd_file_info *mfi = file->private_data;
  243. struct mtd_info *mtd = mfi->mtd;
  244. size_t size = count;
  245. char *kbuf;
  246. size_t retlen;
  247. size_t total_retlen=0;
  248. int ret=0;
  249. int len;
  250. pr_debug("MTD_write\n");
  251. if (*ppos == mtd->size)
  252. return -ENOSPC;
  253. if (*ppos + count > mtd->size)
  254. count = mtd->size - *ppos;
  255. if (!count)
  256. return 0;
  257. kbuf = mtd_kmalloc_up_to(mtd, &size);
  258. if (!kbuf)
  259. return -ENOMEM;
  260. while (count) {
  261. len = min_t(size_t, count, size);
  262. if (copy_from_user(kbuf, buf, len)) {
  263. kfree(kbuf);
  264. return -EFAULT;
  265. }
  266. switch (mfi->mode) {
  267. case MTD_FILE_MODE_OTP_FACTORY:
  268. ret = -EROFS;
  269. break;
  270. case MTD_FILE_MODE_OTP_USER:
  271. ret = mtd_write_user_prot_reg(mtd, *ppos, len,
  272. &retlen, kbuf);
  273. break;
  274. case MTD_FILE_MODE_RAW:
  275. {
  276. struct mtd_oob_ops ops;
  277. ops.mode = MTD_OPS_RAW;
  278. ops.datbuf = kbuf;
  279. ops.oobbuf = NULL;
  280. ops.ooboffs = 0;
  281. ops.len = len;
  282. ret = mtd_write_oob(mtd, *ppos, &ops);
  283. retlen = ops.retlen;
  284. break;
  285. }
  286. default:
  287. ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
  288. }
  289. if (!ret) {
  290. *ppos += retlen;
  291. total_retlen += retlen;
  292. count -= retlen;
  293. buf += retlen;
  294. }
  295. else {
  296. kfree(kbuf);
  297. return ret;
  298. }
  299. }
  300. kfree(kbuf);
  301. return total_retlen;
  302. } /* mtdchar_write */
  303. /*======================================================================
  304. IOCTL calls for getting device parameters.
  305. ======================================================================*/
  306. static void mtdchar_erase_callback (struct erase_info *instr)
  307. {
  308. wake_up((wait_queue_head_t *)instr->priv);
  309. }
  310. #ifdef CONFIG_HAVE_MTD_OTP
  311. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  312. {
  313. struct mtd_info *mtd = mfi->mtd;
  314. size_t retlen;
  315. int ret = 0;
  316. /*
  317. * Make a fake call to mtd_read_fact_prot_reg() to check if OTP
  318. * operations are supported.
  319. */
  320. if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP)
  321. return -EOPNOTSUPP;
  322. switch (mode) {
  323. case MTD_OTP_FACTORY:
  324. mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
  325. break;
  326. case MTD_OTP_USER:
  327. mfi->mode = MTD_FILE_MODE_OTP_USER;
  328. break;
  329. default:
  330. ret = -EINVAL;
  331. case MTD_OTP_OFF:
  332. break;
  333. }
  334. return ret;
  335. }
  336. #else
  337. # define otp_select_filemode(f,m) -EOPNOTSUPP
  338. #endif
  339. static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
  340. uint64_t start, uint32_t length, void __user *ptr,
  341. uint32_t __user *retp)
  342. {
  343. struct mtd_file_info *mfi = file->private_data;
  344. struct mtd_oob_ops ops;
  345. uint32_t retlen;
  346. int ret = 0;
  347. if (!(file->f_mode & FMODE_WRITE))
  348. return -EPERM;
  349. if (length > 4096)
  350. return -EINVAL;
  351. if (!mtd->_write_oob)
  352. ret = -EOPNOTSUPP;
  353. else
  354. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  355. if (ret)
  356. return ret;
  357. ops.ooblen = length;
  358. ops.ooboffs = start & (mtd->writesize - 1);
  359. ops.datbuf = NULL;
  360. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  361. MTD_OPS_PLACE_OOB;
  362. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  363. return -EINVAL;
  364. ops.oobbuf = memdup_user(ptr, length);
  365. if (IS_ERR(ops.oobbuf))
  366. return PTR_ERR(ops.oobbuf);
  367. start &= ~((uint64_t)mtd->writesize - 1);
  368. ret = mtd_write_oob(mtd, start, &ops);
  369. if (ops.oobretlen > 0xFFFFFFFFU)
  370. ret = -EOVERFLOW;
  371. retlen = ops.oobretlen;
  372. if (copy_to_user(retp, &retlen, sizeof(length)))
  373. ret = -EFAULT;
  374. kfree(ops.oobbuf);
  375. return ret;
  376. }
  377. static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
  378. uint64_t start, uint32_t length, void __user *ptr,
  379. uint32_t __user *retp)
  380. {
  381. struct mtd_file_info *mfi = file->private_data;
  382. struct mtd_oob_ops ops;
  383. int ret = 0;
  384. if (length > 4096)
  385. return -EINVAL;
  386. if (!access_ok(VERIFY_WRITE, ptr, length))
  387. return -EFAULT;
  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. ret = mtd_lock(mtd, einfo.start, einfo.length);
  684. break;
  685. }
  686. case MEMUNLOCK:
  687. {
  688. struct erase_info_user einfo;
  689. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  690. return -EFAULT;
  691. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  692. break;
  693. }
  694. case MEMISLOCKED:
  695. {
  696. struct erase_info_user einfo;
  697. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  698. return -EFAULT;
  699. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  700. break;
  701. }
  702. /* Legacy interface */
  703. case MEMGETOOBSEL:
  704. {
  705. struct nand_oobinfo oi;
  706. if (!mtd->ecclayout)
  707. return -EOPNOTSUPP;
  708. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  709. return -EINVAL;
  710. oi.useecc = MTD_NANDECC_AUTOPLACE;
  711. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  712. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  713. sizeof(oi.oobfree));
  714. oi.eccbytes = mtd->ecclayout->eccbytes;
  715. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  716. return -EFAULT;
  717. break;
  718. }
  719. case MEMGETBADBLOCK:
  720. {
  721. loff_t offs;
  722. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  723. return -EFAULT;
  724. return mtd_block_isbad(mtd, offs);
  725. break;
  726. }
  727. case MEMSETBADBLOCK:
  728. {
  729. loff_t offs;
  730. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  731. return -EFAULT;
  732. return mtd_block_markbad(mtd, offs);
  733. break;
  734. }
  735. #ifdef CONFIG_HAVE_MTD_OTP
  736. case OTPSELECT:
  737. {
  738. int mode;
  739. if (copy_from_user(&mode, argp, sizeof(int)))
  740. return -EFAULT;
  741. mfi->mode = MTD_FILE_MODE_NORMAL;
  742. ret = otp_select_filemode(mfi, mode);
  743. file->f_pos = 0;
  744. break;
  745. }
  746. case OTPGETREGIONCOUNT:
  747. case OTPGETREGIONINFO:
  748. {
  749. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  750. if (!buf)
  751. return -ENOMEM;
  752. switch (mfi->mode) {
  753. case MTD_FILE_MODE_OTP_FACTORY:
  754. ret = mtd_get_fact_prot_info(mtd, buf, 4096);
  755. break;
  756. case MTD_FILE_MODE_OTP_USER:
  757. ret = mtd_get_user_prot_info(mtd, buf, 4096);
  758. break;
  759. default:
  760. ret = -EINVAL;
  761. break;
  762. }
  763. if (ret >= 0) {
  764. if (cmd == OTPGETREGIONCOUNT) {
  765. int nbr = ret / sizeof(struct otp_info);
  766. ret = copy_to_user(argp, &nbr, sizeof(int));
  767. } else
  768. ret = copy_to_user(argp, buf, ret);
  769. if (ret)
  770. ret = -EFAULT;
  771. }
  772. kfree(buf);
  773. break;
  774. }
  775. case OTPLOCK:
  776. {
  777. struct otp_info oinfo;
  778. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  779. return -EINVAL;
  780. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  781. return -EFAULT;
  782. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  783. break;
  784. }
  785. #endif
  786. /* This ioctl is being deprecated - it truncates the ECC layout */
  787. case ECCGETLAYOUT:
  788. {
  789. struct nand_ecclayout_user *usrlay;
  790. if (!mtd->ecclayout)
  791. return -EOPNOTSUPP;
  792. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  793. if (!usrlay)
  794. return -ENOMEM;
  795. shrink_ecclayout(mtd->ecclayout, usrlay);
  796. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  797. ret = -EFAULT;
  798. kfree(usrlay);
  799. break;
  800. }
  801. case ECCGETSTATS:
  802. {
  803. if (copy_to_user(argp, &mtd->ecc_stats,
  804. sizeof(struct mtd_ecc_stats)))
  805. return -EFAULT;
  806. break;
  807. }
  808. case MTDFILEMODE:
  809. {
  810. mfi->mode = 0;
  811. switch(arg) {
  812. case MTD_FILE_MODE_OTP_FACTORY:
  813. case MTD_FILE_MODE_OTP_USER:
  814. ret = otp_select_filemode(mfi, arg);
  815. break;
  816. case MTD_FILE_MODE_RAW:
  817. if (!mtd_has_oob(mtd))
  818. return -EOPNOTSUPP;
  819. mfi->mode = arg;
  820. case MTD_FILE_MODE_NORMAL:
  821. break;
  822. default:
  823. ret = -EINVAL;
  824. }
  825. file->f_pos = 0;
  826. break;
  827. }
  828. case BLKPG:
  829. {
  830. ret = mtdchar_blkpg_ioctl(mtd,
  831. (struct blkpg_ioctl_arg __user *)arg);
  832. break;
  833. }
  834. case BLKRRPART:
  835. {
  836. /* No reread partition feature. Just return ok */
  837. ret = 0;
  838. break;
  839. }
  840. default:
  841. ret = -ENOTTY;
  842. }
  843. return ret;
  844. } /* memory_ioctl */
  845. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  846. {
  847. int ret;
  848. mutex_lock(&mtd_mutex);
  849. ret = mtdchar_ioctl(file, cmd, arg);
  850. mutex_unlock(&mtd_mutex);
  851. return ret;
  852. }
  853. #ifdef CONFIG_COMPAT
  854. struct mtd_oob_buf32 {
  855. u_int32_t start;
  856. u_int32_t length;
  857. compat_caddr_t ptr; /* unsigned char* */
  858. };
  859. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  860. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  861. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  862. unsigned long arg)
  863. {
  864. struct mtd_file_info *mfi = file->private_data;
  865. struct mtd_info *mtd = mfi->mtd;
  866. void __user *argp = compat_ptr(arg);
  867. int ret = 0;
  868. mutex_lock(&mtd_mutex);
  869. switch (cmd) {
  870. case MEMWRITEOOB32:
  871. {
  872. struct mtd_oob_buf32 buf;
  873. struct mtd_oob_buf32 __user *buf_user = argp;
  874. if (copy_from_user(&buf, argp, sizeof(buf)))
  875. ret = -EFAULT;
  876. else
  877. ret = mtdchar_writeoob(file, mtd, buf.start,
  878. buf.length, compat_ptr(buf.ptr),
  879. &buf_user->length);
  880. break;
  881. }
  882. case MEMREADOOB32:
  883. {
  884. struct mtd_oob_buf32 buf;
  885. struct mtd_oob_buf32 __user *buf_user = argp;
  886. /* NOTE: writes return length to buf->start */
  887. if (copy_from_user(&buf, argp, sizeof(buf)))
  888. ret = -EFAULT;
  889. else
  890. ret = mtdchar_readoob(file, mtd, buf.start,
  891. buf.length, compat_ptr(buf.ptr),
  892. &buf_user->start);
  893. break;
  894. }
  895. default:
  896. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  897. }
  898. mutex_unlock(&mtd_mutex);
  899. return ret;
  900. }
  901. #endif /* CONFIG_COMPAT */
  902. /*
  903. * try to determine where a shared mapping can be made
  904. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  905. * mappings)
  906. */
  907. #ifndef CONFIG_MMU
  908. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  909. unsigned long addr,
  910. unsigned long len,
  911. unsigned long pgoff,
  912. unsigned long flags)
  913. {
  914. struct mtd_file_info *mfi = file->private_data;
  915. struct mtd_info *mtd = mfi->mtd;
  916. unsigned long offset;
  917. int ret;
  918. if (addr != 0)
  919. return (unsigned long) -EINVAL;
  920. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  921. return (unsigned long) -EINVAL;
  922. offset = pgoff << PAGE_SHIFT;
  923. if (offset > mtd->size - len)
  924. return (unsigned long) -EINVAL;
  925. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  926. return ret == -EOPNOTSUPP ? -ENOSYS : ret;
  927. }
  928. #endif
  929. static inline unsigned long get_vm_size(struct vm_area_struct *vma)
  930. {
  931. return vma->vm_end - vma->vm_start;
  932. }
  933. static inline resource_size_t get_vm_offset(struct vm_area_struct *vma)
  934. {
  935. return (resource_size_t) vma->vm_pgoff << PAGE_SHIFT;
  936. }
  937. /*
  938. * Set a new vm offset.
  939. *
  940. * Verify that the incoming offset really works as a page offset,
  941. * and that the offset and size fit in a resource_size_t.
  942. */
  943. static inline int set_vm_offset(struct vm_area_struct *vma, resource_size_t off)
  944. {
  945. pgoff_t pgoff = off >> PAGE_SHIFT;
  946. if (off != (resource_size_t) pgoff << PAGE_SHIFT)
  947. return -EINVAL;
  948. if (off + get_vm_size(vma) - 1 < off)
  949. return -EINVAL;
  950. vma->vm_pgoff = pgoff;
  951. return 0;
  952. }
  953. /*
  954. * set up a mapping for shared memory segments
  955. */
  956. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  957. {
  958. #ifdef CONFIG_MMU
  959. struct mtd_file_info *mfi = file->private_data;
  960. struct mtd_info *mtd = mfi->mtd;
  961. struct map_info *map = mtd->priv;
  962. resource_size_t start, off;
  963. unsigned long len, vma_len;
  964. /* This is broken because it assumes the MTD device is map-based
  965. and that mtd->priv is a valid struct map_info. It should be
  966. replaced with something that uses the mtd_get_unmapped_area()
  967. operation properly. */
  968. if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
  969. off = get_vm_offset(vma);
  970. start = map->phys;
  971. len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
  972. start &= PAGE_MASK;
  973. vma_len = get_vm_size(vma);
  974. /* Overflow in off+len? */
  975. if (vma_len + off < off)
  976. return -EINVAL;
  977. /* Does it fit in the mapping? */
  978. if (vma_len + off > len)
  979. return -EINVAL;
  980. off += start;
  981. /* Did that overflow? */
  982. if (off < start)
  983. return -EINVAL;
  984. if (set_vm_offset(vma, off) < 0)
  985. return -EINVAL;
  986. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  987. #ifdef pgprot_noncached
  988. if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
  989. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  990. #endif
  991. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  992. vma->vm_end - vma->vm_start,
  993. vma->vm_page_prot))
  994. return -EAGAIN;
  995. return 0;
  996. }
  997. return -ENOSYS;
  998. #else
  999. return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
  1000. #endif
  1001. }
  1002. static const struct file_operations mtd_fops = {
  1003. .owner = THIS_MODULE,
  1004. .llseek = mtdchar_lseek,
  1005. .read = mtdchar_read,
  1006. .write = mtdchar_write,
  1007. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  1008. #ifdef CONFIG_COMPAT
  1009. .compat_ioctl = mtdchar_compat_ioctl,
  1010. #endif
  1011. .open = mtdchar_open,
  1012. .release = mtdchar_close,
  1013. .mmap = mtdchar_mmap,
  1014. #ifndef CONFIG_MMU
  1015. .get_unmapped_area = mtdchar_get_unmapped_area,
  1016. #endif
  1017. };
  1018. static const struct super_operations mtd_ops = {
  1019. .drop_inode = generic_delete_inode,
  1020. .statfs = simple_statfs,
  1021. };
  1022. static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
  1023. int flags, const char *dev_name, void *data)
  1024. {
  1025. return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
  1026. }
  1027. static struct file_system_type mtd_inodefs_type = {
  1028. .name = "mtd_inodefs",
  1029. .mount = mtd_inodefs_mount,
  1030. .kill_sb = kill_anon_super,
  1031. };
  1032. MODULE_ALIAS_FS("mtd_inodefs");
  1033. static int __init init_mtdchar(void)
  1034. {
  1035. int ret;
  1036. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  1037. "mtd", &mtd_fops);
  1038. if (ret < 0) {
  1039. pr_notice("Can't allocate major number %d for "
  1040. "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
  1041. return ret;
  1042. }
  1043. ret = register_filesystem(&mtd_inodefs_type);
  1044. if (ret) {
  1045. pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
  1046. goto err_unregister_chdev;
  1047. }
  1048. return ret;
  1049. err_unregister_chdev:
  1050. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1051. return ret;
  1052. }
  1053. static void __exit cleanup_mtdchar(void)
  1054. {
  1055. unregister_filesystem(&mtd_inodefs_type);
  1056. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1057. }
  1058. module_init(init_mtdchar);
  1059. module_exit(cleanup_mtdchar);
  1060. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
  1061. MODULE_LICENSE("GPL");
  1062. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1063. MODULE_DESCRIPTION("Direct character-device access to MTD devices");
  1064. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);