ALSA: ak411x: Use array instead of offsetof()
authorTakashi Iwai <tiwai@suse.de>
Fri, 12 May 2017 08:47:16 +0000 (10:47 +0200)
committerTakashi Iwai <tiwai@suse.de>
Wed, 17 May 2017 05:13:03 +0000 (07:13 +0200)
The ak4113, ak4114 and ak4117 i2c drivers have some tricky codes to
access the struct fields in the callback.  This can be simplified by
replacing the struct fields with the array.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/ak4113.h
include/sound/ak4114.h
include/sound/ak4117.h
sound/i2c/other/ak4113.c
sound/i2c/other/ak4114.c
sound/i2c/other/ak4117.c

index 58c145620c3c0c853bce37d0f8bac8903e95e318..b2d09fd09559e2894d4c132fc71f2c66008cbce7 100644 (file)
@@ -281,6 +281,14 @@ typedef void (ak4113_write_t)(void *private_data, unsigned char addr,
                unsigned char data);
 typedef unsigned char (ak4113_read_t)(void *private_data, unsigned char addr);
 
+enum {
+       AK4113_PARITY_ERRORS,
+       AK4113_V_BIT_ERRORS,
+       AK4113_QCRC_ERRORS,
+       AK4113_CCRC_ERRORS,
+       AK4113_NUM_ERRORS
+};
+
 struct ak4113 {
        struct snd_card *card;
        ak4113_write_t *write;
@@ -292,10 +300,7 @@ struct ak4113 {
        unsigned char regmap[AK4113_WRITABLE_REGS];
        struct snd_kcontrol *kctls[AK4113_CONTROLS];
        struct snd_pcm_substream *substream;
-       unsigned long parity_errors;
-       unsigned long v_bit_errors;
-       unsigned long qcrc_errors;
-       unsigned long ccrc_errors;
+       unsigned long errors[AK4113_NUM_ERRORS];
        unsigned char rcs0;
        unsigned char rcs1;
        unsigned char rcs2;
index b6feb7e225f2624570ace761e22ad3faf73f2c44..39df064c82fc2e6332d896866b3e7f3e6f907846 100644 (file)
 typedef void (ak4114_write_t)(void *private_data, unsigned char addr, unsigned char data);
 typedef unsigned char (ak4114_read_t)(void *private_data, unsigned char addr);
 
+enum {
+       AK4114_PARITY_ERRORS,
+       AK4114_V_BIT_ERRORS,
+       AK4114_QCRC_ERRORS,
+       AK4114_CCRC_ERRORS,
+       AK4114_NUM_ERRORS
+};
+
 struct ak4114 {
        struct snd_card *card;
        ak4114_write_t * write;
@@ -176,10 +184,7 @@ struct ak4114 {
        struct snd_kcontrol *kctls[AK4114_CONTROLS];
        struct snd_pcm_substream *playback_substream;
        struct snd_pcm_substream *capture_substream;
-       unsigned long parity_errors;
-       unsigned long v_bit_errors;
-       unsigned long qcrc_errors;
-       unsigned long ccrc_errors;
+       unsigned long errors[AK4114_NUM_ERRORS];
        unsigned char rcs0;
        unsigned char rcs1;
        struct delayed_work work;
index 1e8178171baf0a61bebba17c1d1d6d44d33a9cdf..5fab517cfe4668bb1d094c6edf7acad51cb3b4d3 100644 (file)
 typedef void (ak4117_write_t)(void *private_data, unsigned char addr, unsigned char data);
 typedef unsigned char (ak4117_read_t)(void *private_data, unsigned char addr);
 
+enum {
+       AK4117_PARITY_ERRORS,
+       AK4117_V_BIT_ERRORS,
+       AK4117_QCRC_ERRORS,
+       AK4117_CCRC_ERRORS,
+       AK4117_NUM_ERRORS
+};
+
 struct ak4117 {
        struct snd_card *card;
        ak4117_write_t * write;
@@ -165,10 +173,7 @@ struct ak4117 {
        unsigned char regmap[5];
        struct snd_kcontrol *kctls[AK4117_CONTROLS];
        struct snd_pcm_substream *substream;
-       unsigned long parity_errors;
-       unsigned long v_bit_errors;
-       unsigned long qcrc_errors;
-       unsigned long ccrc_errors;
+       unsigned long errors[AK4117_NUM_ERRORS];
        unsigned char rcs0;
        unsigned char rcs1;
        unsigned char rcs2;
index 2183e9ebaa6df14f7fbc7ce3a45353c81ce7d186..4099e6062d3c4992970bbb740c04e7d931b76516 100644 (file)
@@ -199,12 +199,11 @@ static int snd_ak4113_in_error_get(struct snd_kcontrol *kcontrol,
                                   struct snd_ctl_elem_value *ucontrol)
 {
        struct ak4113 *chip = snd_kcontrol_chip(kcontrol);
-       long *ptr;
 
        spin_lock_irq(&chip->lock);
-       ptr = (long *)(((char *)chip) + kcontrol->private_value);
-       ucontrol->value.integer.value[0] = *ptr;
-       *ptr = 0;
+       ucontrol->value.integer.value[0] =
+               chip->errors[kcontrol->private_value];
+       chip->errors[kcontrol->private_value] = 0;
        spin_unlock_irq(&chip->lock);
        return 0;
 }
@@ -373,7 +372,7 @@ static struct snd_kcontrol_new snd_ak4113_iec958_controls[] = {
                SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4113_in_error_info,
        .get =          snd_ak4113_in_error_get,
-       .private_value = offsetof(struct ak4113, parity_errors),
+       .private_value = AK4113_PARITY_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -382,7 +381,7 @@ static struct snd_kcontrol_new snd_ak4113_iec958_controls[] = {
                SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4113_in_error_info,
        .get =          snd_ak4113_in_error_get,
-       .private_value = offsetof(struct ak4113, v_bit_errors),
+       .private_value = AK4113_V_BIT_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -391,7 +390,7 @@ static struct snd_kcontrol_new snd_ak4113_iec958_controls[] = {
                SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4113_in_error_info,
        .get =          snd_ak4113_in_error_get,
-       .private_value = offsetof(struct ak4113, ccrc_errors),
+       .private_value = AK4113_CCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -400,7 +399,7 @@ static struct snd_kcontrol_new snd_ak4113_iec958_controls[] = {
                SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4113_in_error_info,
        .get =          snd_ak4113_in_error_get,
-       .private_value = offsetof(struct ak4113, qcrc_errors),
+       .private_value = AK4113_QCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -551,13 +550,13 @@ int snd_ak4113_check_rate_and_errors(struct ak4113 *ak4113, unsigned int flags)
        rcs2 = reg_read(ak4113, AK4113_REG_RCS2);
        spin_lock_irqsave(&ak4113->lock, _flags);
        if (rcs0 & AK4113_PAR)
-               ak4113->parity_errors++;
+               ak4113->errors[AK4113_PARITY_ERRORS]++;
        if (rcs0 & AK4113_V)
-               ak4113->v_bit_errors++;
+               ak4113->errors[AK4113_V_BIT_ERRORS]++;
        if (rcs2 & AK4113_CCRC)
-               ak4113->ccrc_errors++;
+               ak4113->errors[AK4113_CCRC_ERRORS]++;
        if (rcs2 & AK4113_QCRC)
-               ak4113->qcrc_errors++;
+               ak4113->errors[AK4113_QCRC_ERRORS]++;
        c0 = (ak4113->rcs0 & (AK4113_QINT | AK4113_CINT | AK4113_STC |
                                AK4113_AUDION | AK4113_AUTO | AK4113_UNLCK)) ^
                (rcs0 & (AK4113_QINT | AK4113_CINT | AK4113_STC |
index d53c9bb36281f8a1f8827c769c1d19ca494fbf50..7fb1aeb46915fa435c7d4934e22bca87f6a07cb3 100644 (file)
@@ -194,12 +194,11 @@ static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol,
                                   struct snd_ctl_elem_value *ucontrol)
 {
        struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
-       long *ptr;
 
        spin_lock_irq(&chip->lock);
-       ptr = (long *)(((char *)chip) + kcontrol->private_value);
-       ucontrol->value.integer.value[0] = *ptr;
-       *ptr = 0;
+       ucontrol->value.integer.value[0] =
+               chip->errors[kcontrol->private_value];
+       chip->errors[kcontrol->private_value] = 0;
        spin_unlock_irq(&chip->lock);
        return 0;
 }
@@ -341,7 +340,7 @@ static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4114_in_error_info,
        .get =          snd_ak4114_in_error_get,
-       .private_value = offsetof(struct ak4114, parity_errors),
+       .private_value = AK4114_PARITY_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -349,7 +348,7 @@ static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4114_in_error_info,
        .get =          snd_ak4114_in_error_get,
-       .private_value = offsetof(struct ak4114, v_bit_errors),
+       .private_value = AK4114_V_BIT_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -357,7 +356,7 @@ static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4114_in_error_info,
        .get =          snd_ak4114_in_error_get,
-       .private_value = offsetof(struct ak4114, ccrc_errors),
+       .private_value = AK4114_CCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -365,7 +364,7 @@ static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4114_in_error_info,
        .get =          snd_ak4114_in_error_get,
-       .private_value = offsetof(struct ak4114, qcrc_errors),
+       .private_value = AK4114_QCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -581,13 +580,13 @@ int snd_ak4114_check_rate_and_errors(struct ak4114 *ak4114, unsigned int flags)
        rcs0 = reg_read(ak4114, AK4114_REG_RCS0);
        spin_lock_irqsave(&ak4114->lock, _flags);
        if (rcs0 & AK4114_PAR)
-               ak4114->parity_errors++;
+               ak4114->errors[AK4114_PARITY_ERRORS]++;
        if (rcs1 & AK4114_V)
-               ak4114->v_bit_errors++;
+               ak4114->errors[AK4114_V_BIT_ERRORS]++;
        if (rcs1 & AK4114_CCRC)
-               ak4114->ccrc_errors++;
+               ak4114->errors[AK4114_CCRC_ERRORS]++;
        if (rcs1 & AK4114_QCRC)
-               ak4114->qcrc_errors++;
+               ak4114->errors[AK4114_QCRC_ERRORS]++;
        c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^
                      (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK));
        c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0);
index 0702f0552d196c4daa50c8eb3039c5cf2488764c..3ab099fb8c15fc913b73e0267e409a74bec7df65 100644 (file)
@@ -168,12 +168,11 @@ static int snd_ak4117_in_error_get(struct snd_kcontrol *kcontrol,
                                   struct snd_ctl_elem_value *ucontrol)
 {
        struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
-       long *ptr;
 
        spin_lock_irq(&chip->lock);
-       ptr = (long *)(((char *)chip) + kcontrol->private_value);
-       ucontrol->value.integer.value[0] = *ptr;
-       *ptr = 0;
+       ucontrol->value.integer.value[0] =
+                      chip->errors[kcontrol->private_value];
+       chip->errors[kcontrol->private_value] = 0;
        spin_unlock_irq(&chip->lock);
        return 0;
 }
@@ -328,7 +327,7 @@ static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4117_in_error_info,
        .get =          snd_ak4117_in_error_get,
-       .private_value = offsetof(struct ak4117, parity_errors),
+       .private_value = AK4117_PARITY_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -336,7 +335,7 @@ static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4117_in_error_info,
        .get =          snd_ak4117_in_error_get,
-       .private_value = offsetof(struct ak4117, v_bit_errors),
+       .private_value = AK4117_V_BIT_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -344,7 +343,7 @@ static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4117_in_error_info,
        .get =          snd_ak4117_in_error_get,
-       .private_value = offsetof(struct ak4117, ccrc_errors),
+       .private_value = AK4117_CCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -352,7 +351,7 @@ static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = {
        .access =       SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
        .info =         snd_ak4117_in_error_info,
        .get =          snd_ak4117_in_error_get,
-       .private_value = offsetof(struct ak4117, qcrc_errors),
+       .private_value = AK4117_QCRC_ERRORS,
 },
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
@@ -470,13 +469,13 @@ int snd_ak4117_check_rate_and_errors(struct ak4117 *ak4117, unsigned int flags)
        // printk(KERN_DEBUG "AK IRQ: rcs0 = 0x%x, rcs1 = 0x%x, rcs2 = 0x%x\n", rcs0, rcs1, rcs2);
        spin_lock_irqsave(&ak4117->lock, _flags);
        if (rcs0 & AK4117_PAR)
-               ak4117->parity_errors++;
+               ak4117->errors[AK4117_PARITY_ERRORS]++;
        if (rcs0 & AK4117_V)
-               ak4117->v_bit_errors++;
+               ak4117->errors[AK4117_V_BIT_ERRORS]++;
        if (rcs2 & AK4117_CCRC)
-               ak4117->ccrc_errors++;
+               ak4117->errors[AK4117_CCRC_ERRORS]++;
        if (rcs2 & AK4117_QCRC)
-               ak4117->qcrc_errors++;
+               ak4117->errors[AK4117_QCRC_ERRORS]++;
        c0 = (ak4117->rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK)) ^
                      (rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK));
        c1 = (ak4117->rcs1 & (AK4117_DTSCD | AK4117_NPCM | AK4117_PEM | 0x0f)) ^