mtdcore.c 17 KB

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