Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Core registration and callback routines for MTD |
| 3 | * drivers and users. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/mtd/mtd.h> |
| 11 | #include <linux/mtd/compat.h> |
| 12 | #include <ubi_uboot.h> |
| 13 | |
| 14 | struct mtd_info *mtd_table[MAX_MTD_DEVICES]; |
| 15 | |
| 16 | int add_mtd_device(struct mtd_info *mtd) |
| 17 | { |
| 18 | int i; |
| 19 | |
| 20 | BUG_ON(mtd->writesize == 0); |
| 21 | |
| 22 | for (i = 0; i < MAX_MTD_DEVICES; i++) |
| 23 | if (!mtd_table[i]) { |
| 24 | mtd_table[i] = mtd; |
| 25 | mtd->index = i; |
| 26 | mtd->usecount = 0; |
| 27 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 28 | /* No need to get a refcount on the module containing |
| 29 | the notifier, since we hold the mtd_table_mutex */ |
| 30 | |
| 31 | /* We _know_ we aren't being removed, because |
| 32 | our caller is still holding us here. So none |
| 33 | of this try_ nonsense, and no bitching about it |
| 34 | either. :) */ |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * del_mtd_device - unregister an MTD device |
| 43 | * @mtd: pointer to MTD device info structure |
| 44 | * |
| 45 | * Remove a device from the list of MTD devices present in the system, |
| 46 | * and notify each currently active MTD 'user' of its departure. |
| 47 | * Returns zero on success or 1 on failure, which currently will happen |
| 48 | * if the requested device does not appear to be present in the list. |
| 49 | */ |
| 50 | int del_mtd_device(struct mtd_info *mtd) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | if (mtd_table[mtd->index] != mtd) { |
| 55 | ret = -ENODEV; |
| 56 | } else if (mtd->usecount) { |
| 57 | printk(KERN_NOTICE "Removing MTD device #%d (%s)" |
| 58 | " with use count %d\n", |
| 59 | mtd->index, mtd->name, mtd->usecount); |
| 60 | ret = -EBUSY; |
| 61 | } else { |
| 62 | /* No need to get a refcount on the module containing |
| 63 | * the notifier, since we hold the mtd_table_mutex */ |
| 64 | mtd_table[mtd->index] = NULL; |
| 65 | |
| 66 | ret = 0; |
| 67 | } |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * get_mtd_device - obtain a validated handle for an MTD device |
| 74 | * @mtd: last known address of the required MTD device |
| 75 | * @num: internal device number of the required MTD device |
| 76 | * |
| 77 | * Given a number and NULL address, return the num'th entry in the device |
| 78 | * table, if any. Given an address and num == -1, search the device table |
| 79 | * for a device with that address and return if it's still present. Given |
| 80 | * both, return the num'th driver only if its address matches. Return |
| 81 | * error code if not. |
| 82 | */ |
| 83 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 84 | { |
| 85 | struct mtd_info *ret = NULL; |
| 86 | int i, err = -ENODEV; |
| 87 | |
| 88 | if (num == -1) { |
| 89 | for (i = 0; i < MAX_MTD_DEVICES; i++) |
| 90 | if (mtd_table[i] == mtd) |
| 91 | ret = mtd_table[i]; |
| 92 | } else if (num < MAX_MTD_DEVICES) { |
| 93 | ret = mtd_table[num]; |
| 94 | if (mtd && mtd != ret) |
| 95 | ret = NULL; |
| 96 | } |
| 97 | |
| 98 | if (!ret) |
| 99 | goto out_unlock; |
| 100 | |
| 101 | ret->usecount++; |
| 102 | return ret; |
| 103 | |
| 104 | out_unlock: |
| 105 | return ERR_PTR(err); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 110 | * device name |
| 111 | * @name: MTD device name to open |
| 112 | * |
| 113 | * This function returns MTD device description structure in case of |
| 114 | * success and an error code in case of failure. |
| 115 | */ |
| 116 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 117 | { |
| 118 | int i, err = -ENODEV; |
| 119 | struct mtd_info *mtd = NULL; |
| 120 | |
| 121 | for (i = 0; i < MAX_MTD_DEVICES; i++) { |
| 122 | if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { |
| 123 | mtd = mtd_table[i]; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!mtd) |
| 129 | goto out_unlock; |
| 130 | |
| 131 | mtd->usecount++; |
| 132 | return mtd; |
| 133 | |
| 134 | out_unlock: |
| 135 | return ERR_PTR(err); |
| 136 | } |
| 137 | |
| 138 | void put_mtd_device(struct mtd_info *mtd) |
| 139 | { |
| 140 | int c; |
| 141 | |
| 142 | c = --mtd->usecount; |
| 143 | BUG_ON(c < 0); |
| 144 | } |