mtdcore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/timer.h>
  12. #include <linux/major.h>
  13. #include <linux/fs.h>
  14. #include <linux/err.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/init.h>
  17. #include <linux/mtd/compatmac.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/mtd/mtd.h>
  21. #include "internal.h"
  22. #include "mtdcore.h"
  23. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  24. static int mtd_cls_resume(struct device *dev);
  25. static struct class mtd_class = {
  26. .name = "mtd",
  27. .owner = THIS_MODULE,
  28. .suspend = mtd_cls_suspend,
  29. .resume = mtd_cls_resume,
  30. };
  31. static DEFINE_IDR(mtd_idr);
  32. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  33. should not use them for _anything_ else */
  34. DEFINE_MUTEX(mtd_table_mutex);
  35. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  36. struct mtd_info *__mtd_next_device(int i)
  37. {
  38. return idr_get_next(&mtd_idr, &i);
  39. }
  40. EXPORT_SYMBOL_GPL(__mtd_next_device);
  41. static LIST_HEAD(mtd_notifiers);
  42. #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
  43. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  44. #else
  45. #define MTD_DEVT(index) 0
  46. #endif
  47. /* REVISIT once MTD uses the driver model better, whoever allocates
  48. * the mtd_info will probably want to use the release() hook...
  49. */
  50. static void mtd_release(struct device *dev)
  51. {
  52. dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
  53. /* remove /dev/mtdXro node if needed */
  54. if (index)
  55. device_destroy(&mtd_class, index + 1);
  56. }
  57. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  58. {
  59. struct mtd_info *mtd = dev_to_mtd(dev);
  60. if (mtd && mtd->suspend)
  61. return mtd->suspend(mtd);
  62. else
  63. return 0;
  64. }
  65. static int mtd_cls_resume(struct device *dev)
  66. {
  67. struct mtd_info *mtd = dev_to_mtd(dev);
  68. if (mtd && mtd->resume)
  69. mtd->resume(mtd);
  70. return 0;
  71. }
  72. static ssize_t mtd_type_show(struct device *dev,
  73. struct device_attribute *attr, char *buf)
  74. {
  75. struct mtd_info *mtd = dev_to_mtd(dev);
  76. char *type;
  77. switch (mtd->type) {
  78. case MTD_ABSENT:
  79. type = "absent";
  80. break;
  81. case MTD_RAM:
  82. type = "ram";
  83. break;
  84. case MTD_ROM:
  85. type = "rom";
  86. break;
  87. case MTD_NORFLASH:
  88. type = "nor";
  89. break;
  90. case MTD_NANDFLASH:
  91. type = "nand";
  92. break;
  93. case MTD_DATAFLASH:
  94. type = "dataflash";
  95. break;
  96. case MTD_UBIVOLUME:
  97. type = "ubi";
  98. break;
  99. default:
  100. type = "unknown";
  101. }
  102. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  103. }
  104. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  105. static ssize_t mtd_flags_show(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct mtd_info *mtd = dev_to_mtd(dev);
  109. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  110. }
  111. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  112. static ssize_t mtd_size_show(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. struct mtd_info *mtd = dev_to_mtd(dev);
  116. return snprintf(buf, PAGE_SIZE, "%llu\n",
  117. (unsigned long long)mtd->size);
  118. }
  119. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  120. static ssize_t mtd_erasesize_show(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct mtd_info *mtd = dev_to_mtd(dev);
  124. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  125. }
  126. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  127. static ssize_t mtd_writesize_show(struct device *dev,
  128. struct device_attribute *attr, char *buf)
  129. {
  130. struct mtd_info *mtd = dev_to_mtd(dev);
  131. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  132. }
  133. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  134. static ssize_t mtd_subpagesize_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. struct mtd_info *mtd = dev_to_mtd(dev);
  138. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  139. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  140. }
  141. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  142. static ssize_t mtd_oobsize_show(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct mtd_info *mtd = dev_to_mtd(dev);
  146. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  147. }
  148. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  149. static ssize_t mtd_numeraseregions_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. struct mtd_info *mtd = dev_to_mtd(dev);
  153. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  154. }
  155. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  156. NULL);
  157. static ssize_t mtd_name_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. struct mtd_info *mtd = dev_to_mtd(dev);
  161. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  162. }
  163. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  164. static struct attribute *mtd_attrs[] = {
  165. &dev_attr_type.attr,
  166. &dev_attr_flags.attr,
  167. &dev_attr_size.attr,
  168. &dev_attr_erasesize.attr,
  169. &dev_attr_writesize.attr,
  170. &dev_attr_subpagesize.attr,
  171. &dev_attr_oobsize.attr,
  172. &dev_attr_numeraseregions.attr,
  173. &dev_attr_name.attr,
  174. NULL,
  175. };
  176. static struct attribute_group mtd_group = {
  177. .attrs = mtd_attrs,
  178. };
  179. static const struct attribute_group *mtd_groups[] = {
  180. &mtd_group,
  181. NULL,
  182. };
  183. static struct device_type mtd_devtype = {
  184. .name = "mtd",
  185. .groups = mtd_groups,
  186. .release = mtd_release,
  187. };
  188. /**
  189. * add_mtd_device - register an MTD device
  190. * @mtd: pointer to new MTD device info structure
  191. *
  192. * Add a device to the list of MTD devices present in the system, and
  193. * notify each currently active MTD 'user' of its arrival. Returns
  194. * zero on success or 1 on failure, which currently will only happen
  195. * if there is insufficient memory or a sysfs error.
  196. */
  197. int add_mtd_device(struct mtd_info *mtd)
  198. {
  199. struct mtd_notifier *not;
  200. int i, error;
  201. if (!mtd->backing_dev_info) {
  202. switch (mtd->type) {
  203. case MTD_RAM:
  204. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  205. break;
  206. case MTD_ROM:
  207. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  208. break;
  209. default:
  210. mtd->backing_dev_info = &mtd_bdi_unmappable;
  211. break;
  212. }
  213. }
  214. BUG_ON(mtd->writesize == 0);
  215. mutex_lock(&mtd_table_mutex);
  216. do {
  217. if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
  218. goto fail_locked;
  219. error = idr_get_new(&mtd_idr, mtd, &i);
  220. } while (error == -EAGAIN);
  221. if (error)
  222. goto fail_locked;
  223. mtd->index = i;
  224. mtd->usecount = 0;
  225. if (is_power_of_2(mtd->erasesize))
  226. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  227. else
  228. mtd->erasesize_shift = 0;
  229. if (is_power_of_2(mtd->writesize))
  230. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  231. else
  232. mtd->writesize_shift = 0;
  233. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  234. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  235. /* Some chips always power up locked. Unlock them now */
  236. if ((mtd->flags & MTD_WRITEABLE)
  237. && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
  238. if (mtd->unlock(mtd, 0, mtd->size))
  239. printk(KERN_WARNING
  240. "%s: unlock failed, writes may not work\n",
  241. mtd->name);
  242. }
  243. /* Caller should have set dev.parent to match the
  244. * physical device.
  245. */
  246. mtd->dev.type = &mtd_devtype;
  247. mtd->dev.class = &mtd_class;
  248. mtd->dev.devt = MTD_DEVT(i);
  249. dev_set_name(&mtd->dev, "mtd%d", i);
  250. dev_set_drvdata(&mtd->dev, mtd);
  251. if (device_register(&mtd->dev) != 0)
  252. goto fail_added;
  253. if (MTD_DEVT(i))
  254. device_create(&mtd_class, mtd->dev.parent,
  255. MTD_DEVT(i) + 1,
  256. NULL, "mtd%dro", i);
  257. DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
  258. /* No need to get a refcount on the module containing
  259. the notifier, since we hold the mtd_table_mutex */
  260. list_for_each_entry(not, &mtd_notifiers, list)
  261. not->add(mtd);
  262. mutex_unlock(&mtd_table_mutex);
  263. /* We _know_ we aren't being removed, because
  264. our caller is still holding us here. So none
  265. of this try_ nonsense, and no bitching about it
  266. either. :) */
  267. __module_get(THIS_MODULE);
  268. return 0;
  269. fail_added:
  270. idr_remove(&mtd_idr, i);
  271. fail_locked:
  272. mutex_unlock(&mtd_table_mutex);
  273. return 1;
  274. }
  275. /**
  276. * del_mtd_device - unregister an MTD device
  277. * @mtd: pointer to MTD device info structure
  278. *
  279. * Remove a device from the list of MTD devices present in the system,
  280. * and notify each currently active MTD 'user' of its departure.
  281. * Returns zero on success or 1 on failure, which currently will happen
  282. * if the requested device does not appear to be present in the list.
  283. */
  284. int del_mtd_device (struct mtd_info *mtd)
  285. {
  286. int ret;
  287. mutex_lock(&mtd_table_mutex);
  288. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  289. ret = -ENODEV;
  290. } else if (mtd->usecount) {
  291. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  292. mtd->index, mtd->name, mtd->usecount);
  293. ret = -EBUSY;
  294. } else {
  295. struct mtd_notifier *not;
  296. device_unregister(&mtd->dev);
  297. /* No need to get a refcount on the module containing
  298. the notifier, since we hold the mtd_table_mutex */
  299. list_for_each_entry(not, &mtd_notifiers, list)
  300. not->remove(mtd);
  301. idr_remove(&mtd_idr, mtd->index);
  302. module_put(THIS_MODULE);
  303. ret = 0;
  304. }
  305. mutex_unlock(&mtd_table_mutex);
  306. return ret;
  307. }
  308. /**
  309. * register_mtd_user - register a 'user' of MTD devices.
  310. * @new: pointer to notifier info structure
  311. *
  312. * Registers a pair of callbacks function to be called upon addition
  313. * or removal of MTD devices. Causes the 'add' callback to be immediately
  314. * invoked for each MTD device currently present in the system.
  315. */
  316. void register_mtd_user (struct mtd_notifier *new)
  317. {
  318. struct mtd_info *mtd;
  319. mutex_lock(&mtd_table_mutex);
  320. list_add(&new->list, &mtd_notifiers);
  321. __module_get(THIS_MODULE);
  322. mtd_for_each_device(mtd)
  323. new->add(mtd);
  324. mutex_unlock(&mtd_table_mutex);
  325. }
  326. /**
  327. * unregister_mtd_user - unregister a 'user' of MTD devices.
  328. * @old: pointer to notifier info structure
  329. *
  330. * Removes a callback function pair from the list of 'users' to be
  331. * notified upon addition or removal of MTD devices. Causes the
  332. * 'remove' callback to be immediately invoked for each MTD device
  333. * currently present in the system.
  334. */
  335. int unregister_mtd_user (struct mtd_notifier *old)
  336. {
  337. struct mtd_info *mtd;
  338. mutex_lock(&mtd_table_mutex);
  339. module_put(THIS_MODULE);
  340. mtd_for_each_device(mtd)
  341. old->remove(mtd);
  342. list_del(&old->list);
  343. mutex_unlock(&mtd_table_mutex);
  344. return 0;
  345. }
  346. /**
  347. * get_mtd_device - obtain a validated handle for an MTD device
  348. * @mtd: last known address of the required MTD device
  349. * @num: internal device number of the required MTD device
  350. *
  351. * Given a number and NULL address, return the num'th entry in the device
  352. * table, if any. Given an address and num == -1, search the device table
  353. * for a device with that address and return if it's still present. Given
  354. * both, return the num'th driver only if its address matches. Return
  355. * error code if not.
  356. */
  357. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  358. {
  359. struct mtd_info *ret = NULL, *other;
  360. int err = -ENODEV;
  361. mutex_lock(&mtd_table_mutex);
  362. if (num == -1) {
  363. mtd_for_each_device(other) {
  364. if (other == mtd) {
  365. ret = mtd;
  366. break;
  367. }
  368. }
  369. } else if (num >= 0) {
  370. ret = idr_find(&mtd_idr, num);
  371. if (mtd && mtd != ret)
  372. ret = NULL;
  373. }
  374. if (!ret)
  375. goto out_unlock;
  376. if (!try_module_get(ret->owner))
  377. goto out_unlock;
  378. if (ret->get_device) {
  379. err = ret->get_device(ret);
  380. if (err)
  381. goto out_put;
  382. }
  383. ret->usecount++;
  384. mutex_unlock(&mtd_table_mutex);
  385. return ret;
  386. out_put:
  387. module_put(ret->owner);
  388. out_unlock:
  389. mutex_unlock(&mtd_table_mutex);
  390. return ERR_PTR(err);
  391. }
  392. /**
  393. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  394. * device name
  395. * @name: MTD device name to open
  396. *
  397. * This function returns MTD device description structure in case of
  398. * success and an error code in case of failure.
  399. */
  400. struct mtd_info *get_mtd_device_nm(const char *name)
  401. {
  402. int err = -ENODEV;
  403. struct mtd_info *mtd = NULL, *other;
  404. mutex_lock(&mtd_table_mutex);
  405. mtd_for_each_device(other) {
  406. if (!strcmp(name, other->name)) {
  407. mtd = other;
  408. break;
  409. }
  410. }
  411. if (!mtd)
  412. goto out_unlock;
  413. if (!try_module_get(mtd->owner))
  414. goto out_unlock;
  415. if (mtd->get_device) {
  416. err = mtd->get_device(mtd);
  417. if (err)
  418. goto out_put;
  419. }
  420. mtd->usecount++;
  421. mutex_unlock(&mtd_table_mutex);
  422. return mtd;
  423. out_put:
  424. module_put(mtd->owner);
  425. out_unlock:
  426. mutex_unlock(&mtd_table_mutex);
  427. return ERR_PTR(err);
  428. }
  429. void put_mtd_device(struct mtd_info *mtd)
  430. {
  431. int c;
  432. mutex_lock(&mtd_table_mutex);
  433. c = --mtd->usecount;
  434. if (mtd->put_device)
  435. mtd->put_device(mtd);
  436. mutex_unlock(&mtd_table_mutex);
  437. BUG_ON(c < 0);
  438. module_put(mtd->owner);
  439. }
  440. /* default_mtd_writev - default mtd writev method for MTD devices that
  441. * don't implement their own
  442. */
  443. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  444. unsigned long count, loff_t to, size_t *retlen)
  445. {
  446. unsigned long i;
  447. size_t totlen = 0, thislen;
  448. int ret = 0;
  449. if(!mtd->write) {
  450. ret = -EROFS;
  451. } else {
  452. for (i=0; i<count; i++) {
  453. if (!vecs[i].iov_len)
  454. continue;
  455. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  456. totlen += thislen;
  457. if (ret || thislen != vecs[i].iov_len)
  458. break;
  459. to += vecs[i].iov_len;
  460. }
  461. }
  462. if (retlen)
  463. *retlen = totlen;
  464. return ret;
  465. }
  466. EXPORT_SYMBOL_GPL(add_mtd_device);
  467. EXPORT_SYMBOL_GPL(del_mtd_device);
  468. EXPORT_SYMBOL_GPL(get_mtd_device);
  469. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  470. EXPORT_SYMBOL_GPL(put_mtd_device);
  471. EXPORT_SYMBOL_GPL(register_mtd_user);
  472. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  473. EXPORT_SYMBOL_GPL(default_mtd_writev);
  474. #ifdef CONFIG_PROC_FS
  475. /*====================================================================*/
  476. /* Support for /proc/mtd */
  477. static struct proc_dir_entry *proc_mtd;
  478. static inline int mtd_proc_info(char *buf, struct mtd_info *this)
  479. {
  480. return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
  481. (unsigned long long)this->size,
  482. this->erasesize, this->name);
  483. }
  484. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  485. int *eof, void *data_unused)
  486. {
  487. struct mtd_info *mtd;
  488. int len, l;
  489. off_t begin = 0;
  490. mutex_lock(&mtd_table_mutex);
  491. len = sprintf(page, "dev: size erasesize name\n");
  492. mtd_for_each_device(mtd) {
  493. l = mtd_proc_info(page + len, mtd);
  494. len += l;
  495. if (len+begin > off+count)
  496. goto done;
  497. if (len+begin < off) {
  498. begin += len;
  499. len = 0;
  500. }
  501. }
  502. *eof = 1;
  503. done:
  504. mutex_unlock(&mtd_table_mutex);
  505. if (off >= len+begin)
  506. return 0;
  507. *start = page + (off-begin);
  508. return ((count < begin+len-off) ? count : begin+len-off);
  509. }
  510. #endif /* CONFIG_PROC_FS */
  511. /*====================================================================*/
  512. /* Init code */
  513. static int __init init_mtd(void)
  514. {
  515. int ret;
  516. ret = class_register(&mtd_class);
  517. if (ret) {
  518. pr_err("Error registering mtd class: %d\n", ret);
  519. return ret;
  520. }
  521. #ifdef CONFIG_PROC_FS
  522. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  523. proc_mtd->read_proc = mtd_read_proc;
  524. #endif /* CONFIG_PROC_FS */
  525. return 0;
  526. }
  527. static void __exit cleanup_mtd(void)
  528. {
  529. #ifdef CONFIG_PROC_FS
  530. if (proc_mtd)
  531. remove_proc_entry( "mtd", NULL);
  532. #endif /* CONFIG_PROC_FS */
  533. class_unregister(&mtd_class);
  534. }
  535. module_init(init_mtd);
  536. module_exit(cleanup_mtd);
  537. MODULE_LICENSE("GPL");
  538. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  539. MODULE_DESCRIPTION("Core MTD registration and access routines");