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>

+30 -27
+30 -27
sound/core/init.c
··· 131 #define init_info_for_card(card) 132 #endif 133 134 /** 135 * snd_card_create - create and initialize a soundcard structure 136 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] ··· 177 struct snd_card **card_ret) 178 { 179 struct snd_card *card; 180 - int err, idx2; 181 182 if (snd_BUG_ON(!card_ret)) 183 return -EINVAL; ··· 192 strlcpy(card->id, xid, sizeof(card->id)); 193 err = 0; 194 mutex_lock(&snd_card_mutex); 195 - if (idx < 0) { 196 - for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) { 197 - /* idx == -1 == 0xffff means: take any free slot */ 198 - if (idx2 < 32 && !(idx & (1U << idx2))) 199 - continue; 200 - if (!test_bit(idx2, snd_cards_lock)) { 201 - if (module_slot_match(module, idx2)) { 202 - idx = idx2; 203 - break; 204 - } 205 - } 206 - } 207 - } 208 - if (idx < 0) { 209 - for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) { 210 - /* idx == -1 == 0xffff means: take any free slot */ 211 - if (idx2 < 32 && !(idx & (1U << idx2))) 212 - continue; 213 - if (!test_bit(idx2, snd_cards_lock)) { 214 - if (!slots[idx2] || !*slots[idx2]) { 215 - idx = idx2; 216 - break; 217 - } 218 - } 219 - } 220 - } 221 if (idx < 0) 222 err = -ENODEV; 223 else if (idx < snd_ecards_limit) {
··· 131 #define init_info_for_card(card) 132 #endif 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 + 159 /** 160 * snd_card_create - create and initialize a soundcard structure 161 * @idx: card index (address) [0 ... (SNDRV_CARDS-1)] ··· 152 struct snd_card **card_ret) 153 { 154 struct snd_card *card; 155 + int err; 156 157 if (snd_BUG_ON(!card_ret)) 158 return -EINVAL; ··· 167 strlcpy(card->id, xid, sizeof(card->id)); 168 err = 0; 169 mutex_lock(&snd_card_mutex); 170 + if (idx < 0) /* first check the matching module-name slot */ 171 + idx = get_slot_from_bitmask(idx, module_slot_match, module); 172 + if (idx < 0) /* if not matched, assign an empty slot */ 173 + idx = get_slot_from_bitmask(idx, check_empty_slot, module); 174 if (idx < 0) 175 err = -ENODEV; 176 else if (idx < snd_ecards_limit) {