[alsa-devel] [PATCH 0/3] Cleaning up the hdspm driver
Hi!
Here's a series of patches to clean up more or less redundant code in the hdspm driver. To ease the review process, I've made three separate commits. Feel free to merge the patches if you think individual commits are not necessary.
Cheers
Adrian Knoth (3): ALSA: hdspm - Implement generic function to toggle settings ALSA: hdspm - Use HDSPM_TOGGLE_SETTING to alter settings ALSA: hdspm - Remove obsolete settings functions
sound/pci/rme9652/hdspm.c | 398 ++++----------------------------------------- 1 file changed, 31 insertions(+), 367 deletions(-)
The driver contains at least six similar functions that change only a single bit in the control register, only the bit position varies.
This patch implements a generic function to toggle a certain bit position that will be used to replace the old code.
Signed-off-by: Adrian Knoth adi@drcomp.erfurt.thur.de
diff --git a/sound/pci/rme9652/hdspm.c b/sound/pci/rme9652/hdspm.c index 748e36c..002e692 100644 --- a/sound/pci/rme9652/hdspm.c +++ b/sound/pci/rme9652/hdspm.c @@ -2887,6 +2887,63 @@ static int snd_hdspm_get_autosync_ref(struct snd_kcontrol *kcontrol, return 0; }
+#define HDSPM_TOGGLE_SETTING(xname, xindex) \ +{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ + .name = xname, \ + .private_value = xindex, \ + .info = snd_hdspm_info_toggle_setting, \ + .get = snd_hdspm_get_toggle_setting, \ + .put = snd_hdspm_put_toggle_setting \ +} + +static int hdspm_toggle_setting(struct hdspm *hdspm, u32 regmask) +{ + return (hdspm->control_register & regmask) ? 1 : 0; +} + +static int hdspm_set_toggle_setting(struct hdspm *hdspm, u32 regmask, int out) +{ + if (out) + hdspm->control_register |= regmask; + else + hdspm->control_register &= ~regmask; + hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); + + return 0; +} + +#define snd_hdspm_info_toggle_setting snd_ctl_boolean_mono_info + +static int snd_hdspm_get_toggle_setting(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); + u32 regmask = kcontrol->private_value; + + spin_lock_irq(&hdspm->lock); + ucontrol->value.integer.value[0] = hdspm_toggle_setting(hdspm, regmask); + spin_unlock_irq(&hdspm->lock); + return 0; +} + +static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); + u32 regmask = kcontrol->private_value; + int change; + unsigned int val; + + if (!snd_hdspm_use_is_exclusive(hdspm)) + return -EBUSY; + val = ucontrol->value.integer.value[0] & 1; + spin_lock_irq(&hdspm->lock); + change = (int) val != hdspm_toggle_setting(hdspm, regmask); + hdspm_set_toggle_setting(hdspm, regmask, val); + spin_unlock_irq(&hdspm->lock); + return change; +} +
#define HDSPM_LINE_OUT(xname, xindex) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
HDSPM_TOGGLE_SETTING and its corresponding functions allow to change settings in the control register. Instead of using the specialised functions, use the generic code to make the code DRY.
Signed-off-by: Adrian Knoth adi@drcomp.erfurt.thur.de
diff --git a/sound/pci/rme9652/hdspm.c b/sound/pci/rme9652/hdspm.c index 002e692..f958e20 100644 --- a/sound/pci/rme9652/hdspm.c +++ b/sound/pci/rme9652/hdspm.c @@ -4533,10 +4533,10 @@ static struct snd_kcontrol_new snd_hdspm_controls_madi[] = { HDSPM_SYNC_CHECK("MADI SyncCheck", 1), HDSPM_SYNC_CHECK("TCO SyncCheck", 2), HDSPM_SYNC_CHECK("SYNC IN SyncCheck", 3), - HDSPM_LINE_OUT("Line Out", 0), - HDSPM_TX_64("TX 64 channels mode", 0), - HDSPM_C_TMS("Clear Track Marker", 0), - HDSPM_SAFE_MODE("Safe Mode", 0), + HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut), + HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch), + HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms), + HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp), HDSPM_INPUT_SELECT("Input Select", 0), HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0) }; @@ -4549,9 +4549,9 @@ static struct snd_kcontrol_new snd_hdspm_controls_madiface[] = { HDSPM_SYSTEM_SAMPLE_RATE("System Sample Rate", 0), HDSPM_AUTOSYNC_SAMPLE_RATE("External Rate", 0), HDSPM_SYNC_CHECK("MADI SyncCheck", 0), - HDSPM_TX_64("TX 64 channels mode", 0), - HDSPM_C_TMS("Clear Track Marker", 0), - HDSPM_SAFE_MODE("Safe Mode", 0), + HDSPM_TOGGLE_SETTING("TX 64 channels mode", HDSPM_TX_64ch), + HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms), + HDSPM_TOGGLE_SETTING("Safe Mode", HDSPM_AutoInp), HDSPM_MADI_SPEEDMODE("MADI Speed Mode", 0) };
@@ -4644,11 +4644,11 @@ static struct snd_kcontrol_new snd_hdspm_controls_aes32[] = { HDSPM_AUTOSYNC_SAMPLE_RATE("AES8 Frequency", 8), HDSPM_AUTOSYNC_SAMPLE_RATE("TCO Frequency", 9), HDSPM_AUTOSYNC_SAMPLE_RATE("SYNC IN Frequency", 10), - HDSPM_LINE_OUT("Line Out", 0), - HDSPM_EMPHASIS("Emphasis", 0), - HDSPM_DOLBY("Non Audio", 0), - HDSPM_PROFESSIONAL("Professional", 0), - HDSPM_C_TMS("Clear Track Marker", 0), + HDSPM_TOGGLE_SETTING("Line Out", HDSPM_LineOut), + HDSPM_TOGGLE_SETTING("Emphasis", HDSPM_Emphasis), + HDSPM_TOGGLE_SETTING("Non Audio", HDSPM_Dolby), + HDSPM_TOGGLE_SETTING("Professional", HDSPM_Professional), + HDSPM_TOGGLE_SETTING("Clear Track Marker", HDSPM_clr_tms), HDSPM_DS_WIRE("Double Speed Wire Mode", 0), HDSPM_QS_WIRE("Quad Speed Wire Mode", 0), }; @@ -6323,7 +6323,7 @@ static int snd_hdspm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, info.system_clock_mode = hdspm_system_clock_mode(hdspm); info.clock_source = hdspm_clock_source(hdspm); info.autosync_ref = hdspm_autosync_ref(hdspm); - info.line_out = hdspm_line_out(hdspm); + info.line_out = hdspm_toggle_setting(hdspm, HDSPM_LineOut); info.passthru = 0; spin_unlock_irq(&hdspm->lock); if (copy_to_user(argp, &info, sizeof(info)))
With HDSPM_TOGGLE_SETTING in place, these functions are no longer required. Removing them makes the code DRY and considerably shorter.
Signed-off-by: Adrian Knoth adi@drcomp.erfurt.thur.de
diff --git a/sound/pci/rme9652/hdspm.c b/sound/pci/rme9652/hdspm.c index f958e20..a7e0de1 100644 --- a/sound/pci/rme9652/hdspm.c +++ b/sound/pci/rme9652/hdspm.c @@ -2944,399 +2944,6 @@ static int snd_hdspm_put_toggle_setting(struct snd_kcontrol *kcontrol, return change; }
- -#define HDSPM_LINE_OUT(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_line_out, \ - .get = snd_hdspm_get_line_out, \ - .put = snd_hdspm_put_line_out \ -} - -static int hdspm_line_out(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_LineOut) ? 1 : 0; -} - - -static int hdspm_set_line_output(struct hdspm * hdspm, int out) -{ - if (out) - hdspm->control_register |= HDSPM_LineOut; - else - hdspm->control_register &= ~HDSPM_LineOut; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_line_out snd_ctl_boolean_mono_info - -static int snd_hdspm_get_line_out(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.integer.value[0] = hdspm_line_out(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_line_out(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_line_out(hdspm); - hdspm_set_line_output(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_TX_64(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_tx_64, \ - .get = snd_hdspm_get_tx_64, \ - .put = snd_hdspm_put_tx_64 \ -} - -static int hdspm_tx_64(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_TX_64ch) ? 1 : 0; -} - -static int hdspm_set_tx_64(struct hdspm * hdspm, int out) -{ - if (out) - hdspm->control_register |= HDSPM_TX_64ch; - else - hdspm->control_register &= ~HDSPM_TX_64ch; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_tx_64 snd_ctl_boolean_mono_info - -static int snd_hdspm_get_tx_64(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.integer.value[0] = hdspm_tx_64(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_tx_64(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_tx_64(hdspm); - hdspm_set_tx_64(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_C_TMS(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_c_tms, \ - .get = snd_hdspm_get_c_tms, \ - .put = snd_hdspm_put_c_tms \ -} - -static int hdspm_c_tms(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_clr_tms) ? 1 : 0; -} - -static int hdspm_set_c_tms(struct hdspm * hdspm, int out) -{ - if (out) - hdspm->control_register |= HDSPM_clr_tms; - else - hdspm->control_register &= ~HDSPM_clr_tms; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_c_tms snd_ctl_boolean_mono_info - -static int snd_hdspm_get_c_tms(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.integer.value[0] = hdspm_c_tms(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_c_tms(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_c_tms(hdspm); - hdspm_set_c_tms(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_SAFE_MODE(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_safe_mode, \ - .get = snd_hdspm_get_safe_mode, \ - .put = snd_hdspm_put_safe_mode \ -} - -static int hdspm_safe_mode(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_AutoInp) ? 1 : 0; -} - -static int hdspm_set_safe_mode(struct hdspm * hdspm, int out) -{ - if (out) - hdspm->control_register |= HDSPM_AutoInp; - else - hdspm->control_register &= ~HDSPM_AutoInp; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_safe_mode snd_ctl_boolean_mono_info - -static int snd_hdspm_get_safe_mode(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.integer.value[0] = hdspm_safe_mode(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_safe_mode(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_safe_mode(hdspm); - hdspm_set_safe_mode(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_EMPHASIS(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_emphasis, \ - .get = snd_hdspm_get_emphasis, \ - .put = snd_hdspm_put_emphasis \ -} - -static int hdspm_emphasis(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_Emphasis) ? 1 : 0; -} - -static int hdspm_set_emphasis(struct hdspm * hdspm, int emp) -{ - if (emp) - hdspm->control_register |= HDSPM_Emphasis; - else - hdspm->control_register &= ~HDSPM_Emphasis; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_emphasis snd_ctl_boolean_mono_info - -static int snd_hdspm_get_emphasis(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.enumerated.item[0] = hdspm_emphasis(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_emphasis(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_emphasis(hdspm); - hdspm_set_emphasis(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_DOLBY(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_dolby, \ - .get = snd_hdspm_get_dolby, \ - .put = snd_hdspm_put_dolby \ -} - -static int hdspm_dolby(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_Dolby) ? 1 : 0; -} - -static int hdspm_set_dolby(struct hdspm * hdspm, int dol) -{ - if (dol) - hdspm->control_register |= HDSPM_Dolby; - else - hdspm->control_register &= ~HDSPM_Dolby; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_dolby snd_ctl_boolean_mono_info - -static int snd_hdspm_get_dolby(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.enumerated.item[0] = hdspm_dolby(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_dolby(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_dolby(hdspm); - hdspm_set_dolby(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - - -#define HDSPM_PROFESSIONAL(xname, xindex) \ -{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ - .name = xname, \ - .index = xindex, \ - .info = snd_hdspm_info_professional, \ - .get = snd_hdspm_get_professional, \ - .put = snd_hdspm_put_professional \ -} - -static int hdspm_professional(struct hdspm * hdspm) -{ - return (hdspm->control_register & HDSPM_Professional) ? 1 : 0; -} - -static int hdspm_set_professional(struct hdspm * hdspm, int dol) -{ - if (dol) - hdspm->control_register |= HDSPM_Professional; - else - hdspm->control_register &= ~HDSPM_Professional; - hdspm_write(hdspm, HDSPM_controlRegister, hdspm->control_register); - - return 0; -} - -#define snd_hdspm_info_professional snd_ctl_boolean_mono_info - -static int snd_hdspm_get_professional(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - - spin_lock_irq(&hdspm->lock); - ucontrol->value.enumerated.item[0] = hdspm_professional(hdspm); - spin_unlock_irq(&hdspm->lock); - return 0; -} - -static int snd_hdspm_put_professional(struct snd_kcontrol *kcontrol, - struct snd_ctl_elem_value *ucontrol) -{ - struct hdspm *hdspm = snd_kcontrol_chip(kcontrol); - int change; - unsigned int val; - - if (!snd_hdspm_use_is_exclusive(hdspm)) - return -EBUSY; - val = ucontrol->value.integer.value[0] & 1; - spin_lock_irq(&hdspm->lock); - change = (int) val != hdspm_professional(hdspm); - hdspm_set_professional(hdspm, val); - spin_unlock_irq(&hdspm->lock); - return change; -} - #define HDSPM_INPUT_SELECT(xname, xindex) \ { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ .name = xname, \
At Mon, 3 Dec 2012 14:55:48 +0100, Adrian Knoth wrote:
Hi!
Here's a series of patches to clean up more or less redundant code in the hdspm driver. To ease the review process, I've made three separate commits. Feel free to merge the patches if you think individual commits are not necessary.
Thanks, applied as is.
Takashi
Cheers
Adrian Knoth (3): ALSA: hdspm - Implement generic function to toggle settings ALSA: hdspm - Use HDSPM_TOGGLE_SETTING to alter settings ALSA: hdspm - Remove obsolete settings functions
sound/pci/rme9652/hdspm.c | 398 ++++----------------------------------------- 1 file changed, 31 insertions(+), 367 deletions(-)
-- 1.7.10.4
participants (2)
-
Adrian Knoth
-
Takashi Iwai