diff options
| author | Takashi Iwai <tiwai@suse.de> | 2014-01-23 05:02:15 -0500 |
|---|---|---|
| committer | Takashi Iwai <tiwai@suse.de> | 2014-01-23 05:45:24 -0500 |
| commit | deb6596f163b7340ff8f5a7c23e25317e165c669 (patch) | |
| tree | 02b80b39a1867aeef2deb2b92d1a2e0f0b948729 | |
| parent | 4c3773eda49c872a3034382f8ec3080002e715bf (diff) | |
ALSA: Refactor slot assignment code
There are two loops that are almost identical but only with different
checks. Refactor them with a simple helper, and give a bit more
comments what's doing there.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
| -rw-r--r-- | sound/core/init.c | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/sound/core/init.c b/sound/core/init.c index e3c93cd77ee7..0d42fcda0de2 100644 --- a/sound/core/init.c +++ b/sound/core/init.c | |||
| @@ -131,6 +131,31 @@ static inline int init_info_for_card(struct snd_card *card) | |||
| 131 | #define init_info_for_card(card) | 131 | #define init_info_for_card(card) |
| 132 | #endif | 132 | #endif |
| 133 | 133 | ||
| 134 | static int check_empty_slot(struct module *module, int slot) | ||
| 135 | { | ||
| 136 | return !slots[slot] || !*slots[slot]; | ||
| 137 | } | ||
| 138 | |||
| 139 | /* return an empty slot number (>= 0) found in the given bitmask @mask. | ||
| 140 | * @mask == -1 == 0xffffffff means: take any free slot up to 32 | ||
| 141 | * when no slot is available, return the original @mask as is. | ||
| 142 | */ | ||
| 143 | static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int), | ||
| 144 | struct module *module) | ||
| 145 | { | ||
| 146 | int slot; | ||
| 147 | |||
| 148 | for (slot = 0; slot < SNDRV_CARDS; slot++) { | ||
| 149 | if (slot < 32 && !(mask & (1U << slot))) | ||
| 150 | continue; | ||
| 151 | if (!test_bit(slot, snd_cards_lock)) { | ||
| 152 | if (check(module, slot)) | ||
| 153 | return slot; /* found */ | ||
| 154 | } | ||
| 155 | } | ||
| 156 | return mask; /* unchanged */ | ||
| 157 | } | ||
| 158 | |||
| 134 | /** | 159 | /** |
| 135 | * snd_card_create - create and initialize a soundcard structure | 160 | * snd_card_create - create and initialize a soundcard structure |
| 136 | * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] | 161 | * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] |
| @@ -152,7 +177,7 @@ int snd_card_create(int idx, const char *xid, | |||
| 152 | struct snd_card **card_ret) | 177 | struct snd_card **card_ret) |
| 153 | { | 178 | { |
| 154 | struct snd_card *card; | 179 | struct snd_card *card; |
| 155 | int err, idx2; | 180 | int err; |
| 156 | 181 | ||
| 157 | if (snd_BUG_ON(!card_ret)) | 182 | if (snd_BUG_ON(!card_ret)) |
| 158 | return -EINVAL; | 183 | return -EINVAL; |
| @@ -167,32 +192,10 @@ int snd_card_create(int idx, const char *xid, | |||
| 167 | strlcpy(card->id, xid, sizeof(card->id)); | 192 | strlcpy(card->id, xid, sizeof(card->id)); |
| 168 | err = 0; | 193 | err = 0; |
| 169 | mutex_lock(&snd_card_mutex); | 194 | mutex_lock(&snd_card_mutex); |
| 170 | if (idx < 0) { | 195 | if (idx < 0) /* first check the matching module-name slot */ |
| 171 | for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) { | 196 | idx = get_slot_from_bitmask(idx, module_slot_match, module); |
| 172 | /* idx == -1 == 0xffff means: take any free slot */ | 197 | if (idx < 0) /* if not matched, assign an empty slot */ |
| 173 | if (idx2 < 32 && !(idx & (1U << idx2))) | 198 | idx = get_slot_from_bitmask(idx, check_empty_slot, module); |
| 174 | continue; | ||
| 175 | if (!test_bit(idx2, snd_cards_lock)) { | ||
| 176 | if (module_slot_match(module, idx2)) { | ||
| 177 | idx = idx2; | ||
| 178 | break; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | if (idx < 0) { | ||
| 184 | for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) { | ||
| 185 | /* idx == -1 == 0xffff means: take any free slot */ | ||
| 186 | if (idx2 < 32 && !(idx & (1U << idx2))) | ||
| 187 | continue; | ||
| 188 | if (!test_bit(idx2, snd_cards_lock)) { | ||
| 189 | if (!slots[idx2] || !*slots[idx2]) { | ||
| 190 | idx = idx2; | ||
| 191 | break; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | if (idx < 0) | 199 | if (idx < 0) |
| 197 | err = -ENODEV; | 200 | err = -ENODEV; |
| 198 | else if (idx < snd_ecards_limit) { | 201 | else if (idx < snd_ecards_limit) { |
