Skip to content

Commit 7086b7b

Browse files
committed
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 <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent e2810d7 commit 7086b7b

File tree

1 file changed

+50
-82
lines changed

1 file changed

+50
-82
lines changed

sound/usb/mixer_us16x08.c

Lines changed: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,9 @@ static int snd_us16x08_recv_urb(struct snd_usb_audio *chip,
176176
*/
177177
static int snd_us16x08_send_urb(struct snd_usb_audio *chip, char *buf, int size)
178178
{
179-
int err = 0;
180-
181-
if (chip) {
182-
err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
179+
return snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
183180
SND_US16X08_URB_REQUEST, SND_US16X08_URB_REQUESTTYPE,
184181
0, 0, buf, size);
185-
}
186-
187-
return err;
188182
}
189183

190184
static int snd_us16x08_route_info(struct snd_kcontrol *kcontrol,
@@ -212,10 +206,7 @@ static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
212206
struct snd_usb_audio *chip = elem->head.mixer->chip;
213207
int index = ucontrol->id.index;
214208
char buf[sizeof(route_msg)];
215-
int val, val_org, err = 0;
216-
217-
/* prepare the message buffer from template */
218-
memcpy(buf, route_msg, sizeof(route_msg));
209+
int val, val_org, err;
219210

220211
/* get the new value (no bias for routes) */
221212
val = ucontrol->value.enumerated.item[0];
@@ -224,6 +215,9 @@ static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,
224215
if (val < 0 || val > 9)
225216
return -EINVAL;
226217

218+
/* prepare the message buffer from template */
219+
memcpy(buf, route_msg, sizeof(route_msg));
220+
227221
if (val < 2) {
228222
/* input comes from a master channel */
229223
val_org = val;
@@ -279,12 +273,9 @@ static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
279273
struct usb_mixer_elem_info *elem = kcontrol->private_data;
280274
struct snd_usb_audio *chip = elem->head.mixer->chip;
281275
char buf[sizeof(mix_msg_out)];
282-
int val, err = 0;
276+
int val, err;
283277
int index = ucontrol->id.index;
284278

285-
/* prepare the message buffer from template */
286-
memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
287-
288279
/* new control value incl. bias*/
289280
val = ucontrol->value.integer.value[0];
290281

@@ -293,6 +284,9 @@ static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,
293284
|| val > SND_US16X08_KCMAX(kcontrol))
294285
return -EINVAL;
295286

287+
/* prepare the message buffer from template */
288+
memcpy(buf, mix_msg_out, sizeof(mix_msg_out));
289+
296290
buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
297291
buf[6] = elem->head.id;
298292

@@ -392,16 +386,16 @@ static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,
392386
int val, err;
393387
int index = ucontrol->id.index;
394388

395-
/* prepare URB message from template */
396-
memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
397-
398389
val = ucontrol->value.integer.value[0];
399390

400391
/* sanity check */
401392
if (val < SND_US16X08_KCMIN(kcontrol)
402393
|| val > SND_US16X08_KCMAX(kcontrol))
403394
return -EINVAL;
404395

396+
/* prepare URB message from template */
397+
memcpy(buf, mix_msg_in, sizeof(mix_msg_in));
398+
405399
/* add the bias to the new value */
406400
buf[8] = val - SND_US16X08_KCBIAS(kcontrol);
407401
buf[6] = elem->head.id;
@@ -434,8 +428,7 @@ static int snd_us16x08_comp_get(struct snd_kcontrol *kcontrol,
434428
struct snd_ctl_elem_value *ucontrol)
435429
{
436430
struct usb_mixer_elem_info *elem = kcontrol->private_data;
437-
struct snd_us16x08_comp_store *store =
438-
((struct snd_us16x08_comp_store *) elem->private_data);
431+
struct snd_us16x08_comp_store *store = elem->private_data;
439432
int index = ucontrol->id.index;
440433
int val_idx = COMP_STORE_IDX(elem->head.id);
441434

@@ -449,18 +442,11 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
449442
{
450443
struct usb_mixer_elem_info *elem = kcontrol->private_data;
451444
struct snd_usb_audio *chip = elem->head.mixer->chip;
452-
struct snd_us16x08_comp_store *store =
453-
((struct snd_us16x08_comp_store *) elem->private_data);
445+
struct snd_us16x08_comp_store *store = elem->private_data;
454446
int index = ucontrol->id.index;
455447
char buf[sizeof(comp_msg)];
456448
int val_idx, val;
457-
int err = 0;
458-
459-
/* prepare compressor URB message from template */
460-
memcpy(buf, comp_msg, sizeof(comp_msg));
461-
462-
/* new control value incl. bias*/
463-
val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
449+
int err;
464450

465451
val = ucontrol->value.integer.value[0];
466452

@@ -469,8 +455,14 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
469455
|| val > SND_US16X08_KCMAX(kcontrol))
470456
return -EINVAL;
471457

458+
/* new control value incl. bias*/
459+
val_idx = elem->head.id - SND_US16X08_ID_COMP_BASE;
460+
472461
store->val[val_idx][index] = ucontrol->value.integer.value[0];
473462

463+
/* prepare compressor URB message from template */
464+
memcpy(buf, comp_msg, sizeof(comp_msg));
465+
474466
/* place comp values in message buffer watch bias! */
475467
buf[8] = store->val[
476468
COMP_STORE_IDX(SND_US16X08_ID_COMP_THRESHOLD)][index]
@@ -502,10 +494,9 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol,
502494
static int snd_us16x08_eqswitch_get(struct snd_kcontrol *kcontrol,
503495
struct snd_ctl_elem_value *ucontrol)
504496
{
505-
int val = 0;
497+
int val;
506498
struct usb_mixer_elem_info *elem = kcontrol->private_data;
507-
struct snd_us16x08_eq_store *store =
508-
((struct snd_us16x08_eq_store *) elem->private_data);
499+
struct snd_us16x08_eq_store *store = elem->private_data;
509500
int index = ucontrol->id.index;
510501

511502
/* 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,
521512
{
522513
struct usb_mixer_elem_info *elem = kcontrol->private_data;
523514
struct snd_usb_audio *chip = elem->head.mixer->chip;
524-
struct snd_us16x08_eq_store *store =
525-
((struct snd_us16x08_eq_store *) elem->private_data);
515+
struct snd_us16x08_eq_store *store = elem->private_data;
526516
int index = ucontrol->id.index;
527-
528517
char buf[sizeof(eqs_msq)];
529518
int val, err = 0;
530519
int b_idx;
@@ -564,10 +553,9 @@ static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,
564553
static int snd_us16x08_eq_get(struct snd_kcontrol *kcontrol,
565554
struct snd_ctl_elem_value *ucontrol)
566555
{
567-
int val = 0;
556+
int val;
568557
struct usb_mixer_elem_info *elem = kcontrol->private_data;
569-
struct snd_us16x08_eq_store *store =
570-
((struct snd_us16x08_eq_store *) elem->private_data);
558+
struct snd_us16x08_eq_store *store = elem->private_data;
571559
int index = ucontrol->id.index;
572560
int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
573561
int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
@@ -584,24 +572,23 @@ static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol,
584572
{
585573
struct usb_mixer_elem_info *elem = kcontrol->private_data;
586574
struct snd_usb_audio *chip = elem->head.mixer->chip;
587-
struct snd_us16x08_eq_store *store =
588-
((struct snd_us16x08_eq_store *) elem->private_data);
575+
struct snd_us16x08_eq_store *store = elem->private_data;
589576
int index = ucontrol->id.index;
590577
char buf[sizeof(eqs_msq)];
591-
int val, err = 0;
578+
int val, err;
592579
int b_idx = EQ_STORE_BAND_IDX(elem->head.id) - 1;
593580
int p_idx = EQ_STORE_PARAM_IDX(elem->head.id);
594581

595-
/* copy URB buffer from EQ template */
596-
memcpy(buf, eqs_msq, sizeof(eqs_msq));
597-
598582
val = ucontrol->value.integer.value[0];
599583

600584
/* sanity check */
601585
if (val < SND_US16X08_KCMIN(kcontrol)
602586
|| val > SND_US16X08_KCMAX(kcontrol))
603587
return -EINVAL;
604588

589+
/* copy URB buffer from EQ template */
590+
memcpy(buf, eqs_msq, sizeof(eqs_msq));
591+
605592
store->val[b_idx][p_idx][index] = val;
606593
buf[20] = store->val[b_idx][3][index];
607594
buf[17] = store->val[b_idx][2][index];
@@ -713,12 +700,6 @@ static int snd_us16x08_meter_get(struct snd_kcontrol *kcontrol,
713700
u8 meter_urb[64];
714701
char tmp[sizeof(mix_init_msg2)] = {0};
715702

716-
if (elem) {
717-
store = (struct snd_us16x08_meter_store *) elem->private_data;
718-
chip = elem->head.mixer->chip;
719-
} else
720-
return 0;
721-
722703
switch (kcontrol->private_value) {
723704
case 0:
724705
snd_us16x08_send_urb(chip, (char *)mix_init_msg1,
@@ -983,11 +964,11 @@ static struct snd_kcontrol_new snd_us16x08_meter_ctl = {
983964
/* setup compressor store and assign default value */
984965
static struct snd_us16x08_comp_store *snd_us16x08_create_comp_store(void)
985966
{
986-
int i = 0;
987-
struct snd_us16x08_comp_store *tmp =
988-
kmalloc(sizeof(struct snd_us16x08_comp_store), GFP_KERNEL);
967+
int i;
968+
struct snd_us16x08_comp_store *tmp;
989969

990-
if (tmp == NULL)
970+
tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
971+
if (!tmp)
991972
return NULL;
992973

993974
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)
1006987
static struct snd_us16x08_eq_store *snd_us16x08_create_eq_store(void)
1007988
{
1008989
int i, b_idx;
1009-
struct snd_us16x08_eq_store *tmp =
1010-
kmalloc(sizeof(struct snd_us16x08_eq_store), GFP_KERNEL);
990+
struct snd_us16x08_eq_store *tmp;
1011991

1012-
if (tmp == NULL)
992+
tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
993+
if (!tmp)
1013994
return NULL;
1014995

1015996
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)
10421023

10431024
static struct snd_us16x08_meter_store *snd_us16x08_create_meter_store(void)
10441025
{
1045-
struct snd_us16x08_meter_store *tmp =
1046-
kzalloc(sizeof(struct snd_us16x08_meter_store), GFP_KERNEL);
1026+
struct snd_us16x08_meter_store *tmp;
10471027

1028+
tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
10481029
if (!tmp)
10491030
return NULL;
10501031
tmp->comp_index = 1;
10511032
tmp->comp_active_index = 0;
10521033
return tmp;
1053-
10541034
}
10551035

10561036
/* 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)
10671047
static int add_new_ctl(struct usb_mixer_interface *mixer,
10681048
const struct snd_kcontrol_new *ncontrol,
10691049
int index, int val_type, int channels,
1070-
const char *name, const void *opt,
1050+
const char *name, void *opt,
10711051
bool do_private_free,
10721052
struct usb_mixer_elem_info **elem_ret)
10731053
{
@@ -1088,7 +1068,7 @@ static int add_new_ctl(struct usb_mixer_interface *mixer,
10881068
elem->head.id = index;
10891069
elem->val_type = val_type;
10901070
elem->channels = channels;
1091-
elem->private_data = (void *) opt;
1071+
elem->private_data = opt;
10921072

10931073
kctl = snd_ctl_new1(ncontrol, elem);
10941074
if (!kctl) {
@@ -1113,10 +1093,8 @@ static int add_new_ctl(struct usb_mixer_interface *mixer,
11131093
return 0;
11141094
}
11151095

1116-
static struct snd_us16x08_control_params control_params;
1117-
11181096
/* table of EQ controls */
1119-
static struct snd_us16x08_control_params eq_controls[] = {
1097+
static const struct snd_us16x08_control_params eq_controls[] = {
11201098
{ /* EQ switch */
11211099
.kcontrol_new = &snd_us16x08_eq_switch_ctl,
11221100
.control_id = SND_US16X08_ID_EQENABLE,
@@ -1197,7 +1175,7 @@ static struct snd_us16x08_control_params eq_controls[] = {
11971175
};
11981176

11991177
/* table of compressor controls */
1200-
static struct snd_us16x08_control_params comp_controls[] = {
1178+
static const struct snd_us16x08_control_params comp_controls[] = {
12011179
{ /* Comp enable */
12021180
.kcontrol_new = &snd_us16x08_compswitch_ctl,
12031181
.control_id = SND_US16X08_ID_COMP_SWITCH,
@@ -1243,7 +1221,7 @@ static struct snd_us16x08_control_params comp_controls[] = {
12431221
};
12441222

12451223
/* table of channel controls */
1246-
static struct snd_us16x08_control_params channel_controls[] = {
1224+
static const struct snd_us16x08_control_params channel_controls[] = {
12471225
{ /* Phase */
12481226
.kcontrol_new = &snd_us16x08_ch_boolean_ctl,
12491227
.control_id = SND_US16X08_ID_PHASE,
@@ -1279,7 +1257,7 @@ static struct snd_us16x08_control_params channel_controls[] = {
12791257
};
12801258

12811259
/* table of master controls */
1282-
static struct snd_us16x08_control_params master_controls[] = {
1260+
static const struct snd_us16x08_control_params master_controls[] = {
12831261
{ /* Master */
12841262
.kcontrol_new = &snd_us16x08_master_ctl,
12851263
.control_id = SND_US16X08_ID_FADER,
@@ -1347,10 +1325,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
13471325
return -ENOMEM;
13481326

13491327
/* add master controls */
1350-
for (i = 0;
1351-
i < sizeof(master_controls)
1352-
/ sizeof(control_params);
1353-
i++) {
1328+
for (i = 0; i < ARRAY_SIZE(master_controls); i++) {
13541329

13551330
err = add_new_ctl(mixer,
13561331
master_controls[i].kcontrol_new,
@@ -1368,10 +1343,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
13681343
}
13691344

13701345
/* add channel controls */
1371-
for (i = 0;
1372-
i < sizeof(channel_controls)
1373-
/ sizeof(control_params);
1374-
i++) {
1346+
for (i = 0; i < ARRAY_SIZE(channel_controls); i++) {
13751347

13761348
err = add_new_ctl(mixer,
13771349
channel_controls[i].kcontrol_new,
@@ -1396,8 +1368,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
13961368
return -ENOMEM;
13971369

13981370
/* add EQ controls */
1399-
for (i = 0; i < sizeof(eq_controls) /
1400-
sizeof(control_params); i++) {
1371+
for (i = 0; i < ARRAY_SIZE(eq_controls); i++) {
14011372

14021373
err = add_new_ctl(mixer,
14031374
eq_controls[i].kcontrol_new,
@@ -1413,10 +1384,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)
14131384
}
14141385

14151386
/* add compressor controls */
1416-
for (i = 0;
1417-
i < sizeof(comp_controls)
1418-
/ sizeof(control_params);
1419-
i++) {
1387+
for (i = 0; i < ARRAY_SIZE(comp_controls); i++) {
14201388

14211389
err = add_new_ctl(mixer,
14221390
comp_controls[i].kcontrol_new,

0 commit comments

Comments
 (0)