mtdcore.c 15 KB

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