ALSA: hda - Use a macro for snd_array iteration loops
authorTakashi Iwai <tiwai@suse.de>
Mon, 23 Apr 2018 15:24:56 +0000 (17:24 +0200)
committerTakashi Iwai <tiwai@suse.de>
Tue, 24 Apr 2018 11:41:53 +0000 (13:41 +0200)
Introduce a new helper macro, snd_array_for_each(), to iterate for
each snd_array element.  It slightly improves the readability than
lengthy open codes at each place.

Along with it, add const prefix to some obvious places.

There should be no functional changes by this.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/hdaudio.h
sound/hda/hdac_regmap.c
sound/pci/hda/hda_auto_parser.c
sound/pci/hda/hda_codec.c
sound/pci/hda/hda_generic.c
sound/pci/hda/hda_sysfs.c
sound/pci/hda/patch_conexant.c
sound/pci/hda/patch_realtek.c

index 06536e01ed94b183246f7ea4245647857d43afa7..c052afc2754721a00e8afbc40fb73875e2962423 100644 (file)
@@ -571,4 +571,9 @@ static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
        return (unsigned long)(ptr - array->list) / array->elem_size;
 }
 
+/* a helper macro to iterate for each snd_array element */
+#define snd_array_for_each(array, idx, ptr) \
+       for ((idx) = 0, (ptr) = (array)->list; (idx) < (array)->used; \
+            (ptr) = snd_array_elem(array, ++(idx)))
+
 #endif /* __SOUND_HDAUDIO_H */
index 47a358fab13280acad02b319d012953eb0a57e7d..419e285e0226d64176a0140537d10c29b66356e5 100644 (file)
@@ -65,10 +65,10 @@ static bool hda_writeable_reg(struct device *dev, unsigned int reg)
 {
        struct hdac_device *codec = dev_to_hdac_dev(dev);
        unsigned int verb = get_verb(reg);
+       const unsigned int *v;
        int i;
 
-       for (i = 0; i < codec->vendor_verbs.used; i++) {
-               unsigned int *v = snd_array_elem(&codec->vendor_verbs, i);
+       snd_array_for_each(&codec->vendor_verbs, i, v) {
                if (verb == *v)
                        return true;
        }
index d3ea73171a3d22a2e41554e7df8d1fedc72a9751..b9a6b66aeb0ef75b2087468c65e1686c0a1cdd52 100644 (file)
@@ -793,11 +793,11 @@ EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
  */
 void snd_hda_apply_verbs(struct hda_codec *codec)
 {
+       const struct hda_verb **v;
        int i;
-       for (i = 0; i < codec->verbs.used; i++) {
-               struct hda_verb **v = snd_array_elem(&codec->verbs, i);
+
+       snd_array_for_each(&codec->verbs, i, v)
                snd_hda_sequence_write(codec, *v);
-       }
 }
 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
 
@@ -890,10 +890,10 @@ EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
 static bool pin_config_match(struct hda_codec *codec,
                             const struct hda_pintbl *pins)
 {
+       const struct hda_pincfg *pin;
        int i;
 
-       for (i = 0; i < codec->init_pins.used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
+       snd_array_for_each(&codec->init_pins, i, pin) {
                hda_nid_t nid = pin->nid;
                u32 cfg = pin->cfg;
                const struct hda_pintbl *t_pins;
index 5bc3a7468e1716bb27113d0b7b1563625e14e134..0aa923d129f5ed97db97c7a84e088fcb86716ccf 100644 (file)
@@ -481,9 +481,10 @@ static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
                                         struct snd_array *array,
                                         hda_nid_t nid)
 {
+       struct hda_pincfg *pin;
        int i;
-       for (i = 0; i < array->used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(array, i);
+
+       snd_array_for_each(array, i, pin) {
                if (pin->nid == nid)
                        return pin;
        }
@@ -618,14 +619,15 @@ EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
  */
 void snd_hda_shutup_pins(struct hda_codec *codec)
 {
+       const struct hda_pincfg *pin;
        int i;
+
        /* don't shut up pins when unloading the driver; otherwise it breaks
         * the default pin setup at the next load of the driver
         */
        if (codec->bus->shutdown)
                return;
-       for (i = 0; i < codec->init_pins.used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
+       snd_array_for_each(&codec->init_pins, i, pin) {
                /* use read here for syncing after issuing each verb */
                snd_hda_codec_read(codec, pin->nid, 0,
                                   AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
@@ -638,13 +640,14 @@ EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
 static void restore_shutup_pins(struct hda_codec *codec)
 {
+       const struct hda_pincfg *pin;
        int i;
+
        if (!codec->pins_shutup)
                return;
        if (codec->bus->shutdown)
                return;
-       for (i = 0; i < codec->init_pins.used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
+       snd_array_for_each(&codec->init_pins, i, pin) {
                snd_hda_codec_write(codec, pin->nid, 0,
                                    AC_VERB_SET_PIN_WIDGET_CONTROL,
                                    pin->ctrl);
@@ -697,8 +700,7 @@ get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
        struct hda_cvt_setup *p;
        int i;
 
-       for (i = 0; i < codec->cvt_setups.used; i++) {
-               p = snd_array_elem(&codec->cvt_setups, i);
+       snd_array_for_each(&codec->cvt_setups, i, p) {
                if (p->nid == nid)
                        return p;
        }
@@ -1076,8 +1078,7 @@ void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
        /* make other inactive cvts with the same stream-tag dirty */
        type = get_wcaps_type(get_wcaps(codec, nid));
        list_for_each_codec(c, codec->bus) {
-               for (i = 0; i < c->cvt_setups.used; i++) {
-                       p = snd_array_elem(&c->cvt_setups, i);
+               snd_array_for_each(&c->cvt_setups, i, p) {
                        if (!p->active && p->stream_tag == stream_tag &&
                            get_wcaps_type(get_wcaps(c, p->nid)) == type)
                                p->dirty = 1;
@@ -1140,12 +1141,11 @@ static void really_cleanup_stream(struct hda_codec *codec,
 static void purify_inactive_streams(struct hda_codec *codec)
 {
        struct hda_codec *c;
+       struct hda_cvt_setup *p;
        int i;
 
        list_for_each_codec(c, codec->bus) {
-               for (i = 0; i < c->cvt_setups.used; i++) {
-                       struct hda_cvt_setup *p;
-                       p = snd_array_elem(&c->cvt_setups, i);
+               snd_array_for_each(&c->cvt_setups, i, p) {
                        if (p->dirty)
                                really_cleanup_stream(c, p);
                }
@@ -1156,10 +1156,10 @@ static void purify_inactive_streams(struct hda_codec *codec)
 /* clean up all streams; called from suspend */
 static void hda_cleanup_all_streams(struct hda_codec *codec)
 {
+       struct hda_cvt_setup *p;
        int i;
 
-       for (i = 0; i < codec->cvt_setups.used; i++) {
-               struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
+       snd_array_for_each(&codec->cvt_setups, i, p) {
                if (p->stream_tag)
                        really_cleanup_stream(codec, p);
        }
@@ -2461,10 +2461,10 @@ EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
                                               hda_nid_t nid)
 {
+       struct hda_spdif_out *spdif;
        int i;
-       for (i = 0; i < codec->spdif_out.used; i++) {
-               struct hda_spdif_out *spdif =
-                               snd_array_elem(&codec->spdif_out, i);
+
+       snd_array_for_each(&codec->spdif_out, i, spdif) {
                if (spdif->nid == nid)
                        return spdif;
        }
index 5cc65093d9417b6647639ebb05204d85b1f8f381..51030f040745c696e2ec05c893ad4e966551c2f2 100644 (file)
@@ -264,10 +264,10 @@ static struct nid_path *get_nid_path(struct hda_codec *codec,
                                     int anchor_nid)
 {
        struct hda_gen_spec *spec = codec->spec;
+       struct nid_path *path;
        int i;
 
-       for (i = 0; i < spec->paths.used; i++) {
-               struct nid_path *path = snd_array_elem(&spec->paths, i);
+       snd_array_for_each(&spec->paths, i, path) {
                if (path->depth <= 0)
                        continue;
                if ((!from_nid || path->path[0] == from_nid) &&
@@ -325,10 +325,10 @@ EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
 {
        struct hda_gen_spec *spec = codec->spec;
+       const struct nid_path *path;
        int i;
 
-       for (i = 0; i < spec->paths.used; i++) {
-               struct nid_path *path = snd_array_elem(&spec->paths, i);
+       snd_array_for_each(&spec->paths, i, path) {
                if (path->path[0] == nid)
                        return true;
        }
@@ -351,11 +351,11 @@ static bool is_reachable_path(struct hda_codec *codec,
 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
 {
        struct hda_gen_spec *spec = codec->spec;
+       const struct nid_path *path;
        int i;
 
        val &= AMP_VAL_COMPARE_MASK;
-       for (i = 0; i < spec->paths.used; i++) {
-               struct nid_path *path = snd_array_elem(&spec->paths, i);
+       snd_array_for_each(&spec->paths, i, path) {
                if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
                        return true;
        }
@@ -638,13 +638,13 @@ static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
 {
        struct hda_gen_spec *spec = codec->spec;
        int type = get_wcaps_type(get_wcaps(codec, nid));
+       const struct nid_path *path;
        int i, n;
 
        if (nid == codec->core.afg)
                return true;
 
-       for (n = 0; n < spec->paths.used; n++) {
-               struct nid_path *path = snd_array_elem(&spec->paths, n);
+       snd_array_for_each(&spec->paths, n, path) {
                if (!path->active)
                        continue;
                if (codec->power_save_node) {
@@ -2696,10 +2696,10 @@ static const struct snd_kcontrol_new out_jack_mode_enum = {
 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
 {
        struct hda_gen_spec *spec = codec->spec;
+       const struct snd_kcontrol_new *kctl;
        int i;
 
-       for (i = 0; i < spec->kctls.used; i++) {
-               struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
+       snd_array_for_each(&spec->kctls, i, kctl) {
                if (!strcmp(kctl->name, name) && kctl->index == idx)
                        return true;
        }
@@ -4021,8 +4021,7 @@ static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
        struct nid_path *path;
        int n;
 
-       for (n = 0; n < spec->paths.used; n++) {
-               path = snd_array_elem(&spec->paths, n);
+       snd_array_for_each(&spec->paths, n, path) {
                if (!path->depth)
                        continue;
                if (path->path[0] == nid ||
@@ -5831,10 +5830,10 @@ static void init_digital(struct hda_codec *codec)
  */
 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
 {
+       const struct hda_pincfg *pin;
        int i;
 
-       for (i = 0; i < codec->init_pins.used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
+       snd_array_for_each(&codec->init_pins, i, pin) {
                hda_nid_t nid = pin->nid;
                if (is_jack_detectable(codec, nid) &&
                    !snd_hda_jack_tbl_get(codec, nid))
index 9b7efece4484a65eda479511dd00d3a02abacf00..6ec79c58d48de7e93504f2176aabd0c6dc0530c4 100644 (file)
@@ -80,10 +80,10 @@ static ssize_t pin_configs_show(struct hda_codec *codec,
                                struct snd_array *list,
                                char *buf)
 {
+       const struct hda_pincfg *pin;
        int i, len = 0;
        mutex_lock(&codec->user_mutex);
-       for (i = 0; i < list->used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(list, i);
+       snd_array_for_each(list, i, pin) {
                len += sprintf(buf + len, "0x%02x 0x%08x\n",
                               pin->nid, pin->cfg);
        }
@@ -217,10 +217,10 @@ static ssize_t init_verbs_show(struct device *dev,
                               char *buf)
 {
        struct hda_codec *codec = dev_get_drvdata(dev);
+       const struct hda_verb *v;
        int i, len = 0;
        mutex_lock(&codec->user_mutex);
-       for (i = 0; i < codec->init_verbs.used; i++) {
-               struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
+       snd_array_for_each(&codec->init_verbs, i, v) {
                len += snprintf(buf + len, PAGE_SIZE - len,
                                "0x%02x 0x%03x 0x%04x\n",
                                v->nid, v->verb, v->param);
@@ -267,10 +267,10 @@ static ssize_t hints_show(struct device *dev,
                          char *buf)
 {
        struct hda_codec *codec = dev_get_drvdata(dev);
+       const struct hda_hint *hint;
        int i, len = 0;
        mutex_lock(&codec->user_mutex);
-       for (i = 0; i < codec->hints.used; i++) {
-               struct hda_hint *hint = snd_array_elem(&codec->hints, i);
+       snd_array_for_each(&codec->hints, i, hint) {
                len += snprintf(buf + len, PAGE_SIZE - len,
                                "%s = %s\n", hint->key, hint->val);
        }
@@ -280,10 +280,10 @@ static ssize_t hints_show(struct device *dev,
 
 static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
 {
+       struct hda_hint *hint;
        int i;
 
-       for (i = 0; i < codec->hints.used; i++) {
-               struct hda_hint *hint = snd_array_elem(&codec->hints, i);
+       snd_array_for_each(&codec->hints, i, hint) {
                if (!strcmp(hint->key, key))
                        return hint;
        }
@@ -783,13 +783,13 @@ void snd_hda_sysfs_init(struct hda_codec *codec)
 void snd_hda_sysfs_clear(struct hda_codec *codec)
 {
 #ifdef CONFIG_SND_HDA_RECONFIG
+       struct hda_hint *hint;
        int i;
 
        /* clear init verbs */
        snd_array_free(&codec->init_verbs);
        /* clear hints */
-       for (i = 0; i < codec->hints.used; i++) {
-               struct hda_hint *hint = snd_array_elem(&codec->hints, i);
+       snd_array_for_each(&codec->hints, i, hint) {
                kfree(hint->key); /* we don't need to free hint->val */
        }
        snd_array_free(&codec->hints);
index 5b4dbcec6de8dab957f045786d4808b4edfd573f..093d2a9ece859b9d37f4a562b3305aa1728ca1b3 100644 (file)
@@ -588,6 +588,7 @@ static void cxt_fixup_olpc_xo(struct hda_codec *codec,
                                    const struct hda_fixup *fix, int action)
 {
        struct conexant_spec *spec = codec->spec;
+       struct snd_kcontrol_new *kctl;
        int i;
 
        if (action != HDA_FIXUP_ACT_PROBE)
@@ -606,9 +607,7 @@ static void cxt_fixup_olpc_xo(struct hda_codec *codec,
        snd_hda_codec_set_pin_target(codec, 0x1a, PIN_VREF50);
 
        /* override mic boost control */
-       for (i = 0; i < spec->gen.kctls.used; i++) {
-               struct snd_kcontrol_new *kctl =
-                       snd_array_elem(&spec->gen.kctls, i);
+       snd_array_for_each(&spec->gen.kctls, i, kctl) {
                if (!strcmp(kctl->name, "Mic Boost Volume")) {
                        kctl->put = olpc_xo_mic_boost_put;
                        break;
index aef1f52db7d9e5264fdcdb382dac477edc098514..7f2d5b157b75f41076292e7862144fbc2a5e3f58 100644 (file)
@@ -2828,6 +2828,7 @@ static int find_ext_mic_pin(struct hda_codec *codec);
 
 static void alc286_shutup(struct hda_codec *codec)
 {
+       const struct hda_pincfg *pin;
        int i;
        int mic_pin = find_ext_mic_pin(codec);
        /* don't shut up pins when unloading the driver; otherwise it breaks
@@ -2835,8 +2836,7 @@ static void alc286_shutup(struct hda_codec *codec)
         */
        if (codec->bus->shutdown)
                return;
-       for (i = 0; i < codec->init_pins.used; i++) {
-               struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
+       snd_array_for_each(&codec->init_pins, i, pin) {
                /* use read here for syncing after issuing each verb */
                if (pin->nid != mic_pin)
                        snd_hda_codec_read(codec, pin->nid, 0,