mtdchar.c 26 KB

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