Skip to content

Commit bd8a71a

Browse files
committed
ALSA: Reduce boilerplate for new jack types
Use a lookup table rather than explicit code to map input subsystem jack types into ASoC ones, implemented as suggested by Takashi Iwai. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
1 parent 6a94cb7 commit bd8a71a

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

‎include/sound/jack.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ struct input_dev;
3030
/**
3131
* Jack types which can be reported. These values are used as a
3232
* bitmask.
33+
*
34+
* Note that this must be kept in sync with the lookup table in
35+
* sound/core/jack.c.
3336
*/
3437
enum snd_jack_types {
3538
SND_JACK_HEADPHONE = 0x0001,

‎sound/core/jack.c‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
#include <sound/jack.h>
2424
#include <sound/core.h>
2525

26+
static int jack_types[] = {
27+
SW_HEADPHONE_INSERT,
28+
SW_MICROPHONE_INSERT,
29+
SW_LINEOUT_INSERT,
30+
SW_JACK_PHYSICAL_INSERT,
31+
};
32+
2633
static int snd_jack_dev_free(struct snd_device *device)
2734
{
2835
struct snd_jack *jack = device->device_data;
@@ -79,6 +86,7 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
7986
{
8087
struct snd_jack *jack;
8188
int err;
89+
int i;
8290
static struct snd_device_ops ops = {
8391
.dev_free = snd_jack_dev_free,
8492
.dev_register = snd_jack_dev_register,
@@ -100,18 +108,10 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
100108

101109
jack->type = type;
102110

103-
if (type & SND_JACK_HEADPHONE)
104-
input_set_capability(jack->input_dev, EV_SW,
105-
SW_HEADPHONE_INSERT);
106-
if (type & SND_JACK_LINEOUT)
107-
input_set_capability(jack->input_dev, EV_SW,
108-
SW_LINEOUT_INSERT);
109-
if (type & SND_JACK_MICROPHONE)
110-
input_set_capability(jack->input_dev, EV_SW,
111-
SW_MICROPHONE_INSERT);
112-
if (type & SND_JACK_MECHANICAL)
113-
input_set_capability(jack->input_dev, EV_SW,
114-
SW_JACK_PHYSICAL_INSERT);
111+
for (i = 0; i < ARRAY_SIZE(jack_types); i++)
112+
if (type & (1 << i))
113+
input_set_capability(jack->input_dev, EV_SW,
114+
jack_types[i]);
115115

116116
err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
117117
if (err < 0)
@@ -154,21 +154,17 @@ EXPORT_SYMBOL(snd_jack_set_parent);
154154
*/
155155
void snd_jack_report(struct snd_jack *jack, int status)
156156
{
157+
int i;
158+
157159
if (!jack)
158160
return;
159161

160-
if (jack->type & SND_JACK_HEADPHONE)
161-
input_report_switch(jack->input_dev, SW_HEADPHONE_INSERT,
162-
status & SND_JACK_HEADPHONE);
163-
if (jack->type & SND_JACK_LINEOUT)
164-
input_report_switch(jack->input_dev, SW_LINEOUT_INSERT,
165-
status & SND_JACK_LINEOUT);
166-
if (jack->type & SND_JACK_MICROPHONE)
167-
input_report_switch(jack->input_dev, SW_MICROPHONE_INSERT,
168-
status & SND_JACK_MICROPHONE);
169-
if (jack->type & SND_JACK_MECHANICAL)
170-
input_report_switch(jack->input_dev, SW_JACK_PHYSICAL_INSERT,
171-
status & SND_JACK_MECHANICAL);
162+
for (i = 0; i < ARRAY_SIZE(jack_types); i++) {
163+
int testbit = 1 << i;
164+
if (jack->type & testbit)
165+
input_report_switch(jack->input_dev, jack_types[i],
166+
status & testbit);
167+
}
172168

173169
input_sync(jack->input_dev);
174170
}

0 commit comments

Comments
 (0)