mtdchar.c 28 KB

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