ALSA: usb-audio: Tidy up mixer_us16x08.c

A few more cleanups and improvements that have been overlooked:

- Use ARRAY_SIZE() macro appropriately
- Code shuffling for minor optimization
- Omit superfluous variable initializations
- Get rid of superfluous NULL checks
- Add const to snd_us16x08_control_params definitions

No functional changes.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2017-02-22 08:37:41 +01:00
parent e2810d76c5
commit 7086b7b3d1
1 changed files with 50 additions and 82 deletions

View File

@ -176,15 +176,9 @@ static int snd_us16x08_recv_urb(struct snd_usb_audio *chip,
*/ */
static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size) static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size)
{ {
int err = 0; return snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
if (chip) {
err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE, SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE,
0, 0, buf, size); 0, 0, buf, size);
}
return err;
} }
static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol, static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol,
@ -212,10 +206,7 @@ static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
struct snd_usb_audio *chip = elem->head.mixer->chip; struct snd_usb_audio *chip = elem->head.mixer->chip;
int index = ucontrol->id.index; int index = ucontrol->id.index;
char buf[sizeof(route_msg)]; char buf[sizeof(route_msg)];
int val, val_org, err = 0; int val, val_org, err;
/* prepare the message buffer from template */
memcpy(buf, route_msg, sizeof(route_msg));
/* get the new value (no bias for routes) */ /* get the new value (no bias for routes) */
val = ucontrol->value.enumerated.item[0]; val = ucontrol->value.enumerated.item[0];
@ -224,6 +215,9 @@ static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
if (val < 0 || val > 9) if (val < 0 || val > 9)
return -EINVAL; return -EINVAL;
/* prepare the message buffer from template */
memcpy(buf, route_msg, sizeof(route_msg));
if (val < 2) { if (val < 2) {
/* input comes from a master channel */ /* input comes from a master channel */
val_org = val; val_org = val;
@ -279,12 +273,9 @@ static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_usb_audio *chip = elem->head.mixer->chip; struct snd_usb_audio *chip = elem->head.mixer->chip;
char buf[sizeof(mix_msg_out)]; char buf[sizeof(mix_msg_out)];
int val, err = 0; int val, err;
int index = ucontrol->id.index; int index = ucontrol->id.index;
/* prepare the message buffer from template */
memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
/* new control value incl. bias*/ /* new control value incl. bias*/
val = ucontrol->value.integer.value[0]; val = ucontrol->value.integer.value[0];
@ -293,6 +284,9 @@ static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
|| val > SND_US16X08_KCMAX(kcontrol)) || val > SND_US16X08_KCMAX(kcontrol))
return -EINVAL; return -EINVAL;
/* prepare the message buffer from template */
memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
buf[8] = val - SND_US16X08_KCBIAS(kcontrol); buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
buf[6] = elem->head.id; buf[6] = elem->head.id;
@ -392,9 +386,6 @@ static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
int val, err; int val, err;
int index = ucontrol->id.index; int index = ucontrol->id.index;
/* prepare URB message from template */
memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
val = ucontrol->value.integer.value[0]; val = ucontrol->value.integer.value[0];
/* sanity check */ /* sanity check */
@ -402,6 +393,9 @@ static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
|| val > SND_US16X08_KCMAX(kcontrol)) || val > SND_US16X08_KCMAX(kcontrol))
return -EINVAL; return -EINVAL;
/* prepare URB message from template */
memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
/* add the bias to the new value */ /* add the bias to the new value */
buf[8] = val - SND_US16X08_KCBIAS(kcontrol); buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
buf[6] = elem->head.id; buf[6] = elem->head.id;
@ -434,8 +428,7 @@ static int snd_us16x08_comp_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol) struct snd_ctl_elem_value *ucontrol)
{ {
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_us16x08_comp_store *store = struct snd_us16x08_comp_store *store = elem->private_data;
((struct snd_us16x08_comp_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
int val_idx = COMP_STORE_IDX(elem->head.id); int val_idx = COMP_STORE_IDX(elem->head.id);
@ -449,18 +442,11 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
{ {
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_usb_audio *chip = elem->head.mixer->chip; struct snd_usb_audio *chip = elem->head.mixer->chip;
struct snd_us16x08_comp_store *store = struct snd_us16x08_comp_store *store = elem->private_data;
((struct snd_us16x08_comp_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
char buf[sizeof(comp_msg)]; char buf[sizeof(comp_msg)];
int val_idx, val; int val_idx, val;
int err = 0; int err;
/* prepare compressor URB message from template */
memcpy(buf, comp_msg, sizeof(comp_msg));
/* new control value incl. bias*/
val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
val = ucontrol->value.integer.value[0]; val = ucontrol->value.integer.value[0];
@ -469,8 +455,14 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
|| val > SND_US16X08_KCMAX(kcontrol)) || val > SND_US16X08_KCMAX(kcontrol))
return -EINVAL; return -EINVAL;
/* new control value incl. bias*/
val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
store->val[val_idx][index] = ucontrol->value.integer.value[0]; store->val[val_idx][index] = ucontrol->value.integer.value[0];
/* prepare compressor URB message from template */
memcpy(buf, comp_msg, sizeof(comp_msg));
/* place comp values in message buffer watch bias! */ /* place comp values in message buffer watch bias! */
buf[8] = store->val[ buf[8] = store->val[
COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index] COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index]
@ -502,10 +494,9 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol, static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol) struct snd_ctl_elem_value *ucontrol)
{ {
int val = 0; int val;
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_us16x08_eq_store *store = struct snd_us16x08_eq_store *store = elem->private_data;
((struct snd_us16x08_eq_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
/* get low switch from cache is enough, cause all bands are together */ /* get low switch from cache is enough, cause all bands are together */
@ -521,10 +512,8 @@ static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
{ {
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_usb_audio *chip = elem->head.mixer->chip; struct snd_usb_audio *chip = elem->head.mixer->chip;
struct snd_us16x08_eq_store *store = struct snd_us16x08_eq_store *store = elem->private_data;
((struct snd_us16x08_eq_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
char buf[sizeof(eqs_msq)]; char buf[sizeof(eqs_msq)];
int val, err = 0; int val, err = 0;
int b_idx; int b_idx;
@ -564,10 +553,9 @@ static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol, static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol) struct snd_ctl_elem_value *ucontrol)
{ {
int val = 0; int val;
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_us16x08_eq_store *store = struct snd_us16x08_eq_store *store = elem->private_data;
((struct snd_us16x08_eq_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1; int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
int p_idx = EQ_STORE_PARAM_IDX(elem->head.id); int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
@ -584,17 +572,13 @@ static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
{ {
struct usb_mixer_elem_info *elem = kcontrol->private_data; struct usb_mixer_elem_info *elem = kcontrol->private_data;
struct snd_usb_audio *chip = elem->head.mixer->chip; struct snd_usb_audio *chip = elem->head.mixer->chip;
struct snd_us16x08_eq_store *store = struct snd_us16x08_eq_store *store = elem->private_data;
((struct snd_us16x08_eq_store *) elem->private_data);
int index = ucontrol->id.index; int index = ucontrol->id.index;
char buf[sizeof(eqs_msq)]; char buf[sizeof(eqs_msq)];
int val, err = 0; int val, err;
int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1; int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
int p_idx = EQ_STORE_PARAM_IDX(elem->head.id); int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
/* copy URB buffer from EQ template */
memcpy(buf, eqs_msq, sizeof(eqs_msq));
val = ucontrol->value.integer.value[0]; val = ucontrol->value.integer.value[0];
/* sanity check */ /* sanity check */
@ -602,6 +586,9 @@ static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
|| val > SND_US16X08_KCMAX(kcontrol)) || val > SND_US16X08_KCMAX(kcontrol))
return -EINVAL; return -EINVAL;
/* copy URB buffer from EQ template */
memcpy(buf, eqs_msq, sizeof(eqs_msq));
store->val[b_idx][p_idx][index] = val; store->val[b_idx][p_idx][index] = val;
buf[20] = store->val[b_idx][3][index]; buf[20] = store->val[b_idx][3][index];
buf[17] = store->val[b_idx][2][index]; buf[17] = store->val[b_idx][2][index];
@ -713,12 +700,6 @@ static int snd_us16x08_meter_get(struct snd_kcontrol *kcontrol,
u8 meter_urb[64]; u8 meter_urb[64];
char tmp[sizeof(mix_init_msg2)] = {0}; char tmp[sizeof(mix_init_msg2)] = {0};
if (elem) {
store = (struct snd_us16x08_meter_store *) elem->private_data;
chip = elem->head.mixer->chip;
} else
return 0;
switch (kcontrol->private_value) { switch (kcontrol->private_value) {
case 0: case 0:
snd_us16x08_send_urb(chip, (char *)mix_init_msg1, snd_us16x08_send_urb(chip, (char *)mix_init_msg1,
@ -983,11 +964,11 @@ static struct snd_kcontrol_new snd_us16x08_meter_ctl = {
/* setup compressor store and assign default value */ /* setup compressor store and assign default value */
static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void) static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
{ {
int i = 0; int i;
struct snd_us16x08_comp_store *tmp = struct snd_us16x08_comp_store *tmp;
kmalloc(sizeof(struct snd_us16x08_comp_store), GFP_KERNEL);
if (tmp == NULL) tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
if (!tmp)
return NULL; return NULL;
for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) { for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
@ -1006,10 +987,10 @@ static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void) static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
{ {
int i, b_idx; int i, b_idx;
struct snd_us16x08_eq_store *tmp = struct snd_us16x08_eq_store *tmp;
kmalloc(sizeof(struct snd_us16x08_eq_store), GFP_KERNEL);
if (tmp == NULL) tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
if (!tmp)
return NULL; return NULL;
for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) { for (i = 0; i < SND_US16X08_MAX_CHANNELS; i++) {
@ -1042,15 +1023,14 @@ static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void) static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void)
{ {
struct snd_us16x08_meter_store *tmp = struct snd_us16x08_meter_store *tmp;
kzalloc(sizeof(struct snd_us16x08_meter_store), GFP_KERNEL);
tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
if (!tmp) if (!tmp)
return NULL; return NULL;
tmp->comp_index = 1; tmp->comp_index = 1;
tmp->comp_active_index = 0; tmp->comp_active_index = 0;
return tmp; return tmp;
} }
/* release elem->private_free as well; called only once for each *_store */ /* release elem->private_free as well; called only once for each *_store */
@ -1067,7 +1047,7 @@ static void elem_private_free(struct snd_kcontrol *kctl)
static int add_new_ctl(struct usb_mixer_interface *mixer, static int add_new_ctl(struct usb_mixer_interface *mixer,
const struct snd_kcontrol_new *ncontrol, const struct snd_kcontrol_new *ncontrol,
int index, int val_type, int channels, int index, int val_type, int channels,
const char *name, const void *opt, const char *name, void *opt,
bool do_private_free, bool do_private_free,
struct usb_mixer_elem_info **elem_ret) struct usb_mixer_elem_info **elem_ret)
{ {
@ -1088,7 +1068,7 @@ static int add_new_ctl(struct usb_mixer_interface *mixer,
elem->head.id = index; elem->head.id = index;
elem->val_type = val_type; elem->val_type = val_type;
elem->channels = channels; elem->channels = channels;
elem->private_data = (void *) opt; elem->private_data = opt;
kctl = snd_ctl_new1(ncontrol, elem); kctl = snd_ctl_new1(ncontrol, elem);
if (!kctl) { if (!kctl) {
@ -1113,10 +1093,8 @@ static int add_new_ctl(struct usb_mixer_interface *mixer,
return 0; return 0;
} }
static struct snd_us16x08_control_params control_params;
/* table of EQ controls */ /* table of EQ controls */
static struct snd_us16x08_control_params eq_controls[] = { static const struct snd_us16x08_control_params eq_controls[] = {
{ /* EQ switch */ { /* EQ switch */
.kcontrol_new = &snd_us16x08_eq_switch_ctl, .kcontrol_new = &snd_us16x08_eq_switch_ctl,
.control_id = SND_US16X08_ID_EQENABLE, .control_id = SND_US16X08_ID_EQENABLE,
@ -1197,7 +1175,7 @@ static struct snd_us16x08_control_params eq_controls[] = {
}; };
/* table of compressor controls */ /* table of compressor controls */
static struct snd_us16x08_control_params comp_controls[] = { static const struct snd_us16x08_control_params comp_controls[] = {
{ /* Comp enable */ { /* Comp enable */
.kcontrol_new = &snd_us16x08_compswitch_ctl, .kcontrol_new = &snd_us16x08_compswitch_ctl,
.control_id = SND_US16X08_ID_COMP_SWITCH, .control_id = SND_US16X08_ID_COMP_SWITCH,
@ -1243,7 +1221,7 @@ static struct snd_us16x08_control_params comp_controls[] = {
}; };
/* table of channel controls */ /* table of channel controls */
static struct snd_us16x08_control_params channel_controls[] = { static const struct snd_us16x08_control_params channel_controls[] = {
{ /* Phase */ { /* Phase */
.kcontrol_new = &snd_us16x08_ch_boolean_ctl, .kcontrol_new = &snd_us16x08_ch_boolean_ctl,
.control_id = SND_US16X08_ID_PHASE, .control_id = SND_US16X08_ID_PHASE,
@ -1279,7 +1257,7 @@ static struct snd_us16x08_control_params channel_controls[] = {
}; };
/* table of master controls */ /* table of master controls */
static struct snd_us16x08_control_params master_controls[] = { static const struct snd_us16x08_control_params master_controls[] = {
{ /* Master */ { /* Master */
.kcontrol_new = &snd_us16x08_master_ctl, .kcontrol_new = &snd_us16x08_master_ctl,
.control_id = SND_US16X08_ID_FADER, .control_id = SND_US16X08_ID_FADER,
@ -1347,10 +1325,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
return -ENOMEM; return -ENOMEM;
/* add master controls */ /* add master controls */
for (i = 0; for (i = 0; i < ARRAY_SIZE(master_controls); i++) {
i < sizeof(master_controls)
/ sizeof(control_params);
i++) {
err = add_new_ctl(mixer, err = add_new_ctl(mixer,
master_controls[i].kcontrol_new, master_controls[i].kcontrol_new,
@ -1368,10 +1343,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
} }
/* add channel controls */ /* add channel controls */
for (i = 0; for (i = 0; i < ARRAY_SIZE(channel_controls); i++) {
i < sizeof(channel_controls)
/ sizeof(control_params);
i++) {
err = add_new_ctl(mixer, err = add_new_ctl(mixer,
channel_controls[i].kcontrol_new, channel_controls[i].kcontrol_new,
@ -1396,8 +1368,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
return -ENOMEM; return -ENOMEM;
/* add EQ controls */ /* add EQ controls */
for (i = 0; i < sizeof(eq_controls) / for (i = 0; i < ARRAY_SIZE(eq_controls); i++) {
sizeof(control_params); i++) {
err = add_new_ctl(mixer, err = add_new_ctl(mixer,
eq_controls[i].kcontrol_new, eq_controls[i].kcontrol_new,
@ -1413,10 +1384,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
} }
/* add compressor controls */ /* add compressor controls */
for (i = 0; for (i = 0; i < ARRAY_SIZE(comp_controls); i++) {
i < sizeof(comp_controls)
/ sizeof(control_params);
i++) {
err = add_new_ctl(mixer, err = add_new_ctl(mixer,
comp_controls[i].kcontrol_new, comp_controls[i].kcontrol_new,