mtdchar.c 28 KB

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