Skip to content

Commit ebb812c

Browse files
committed
ALSA: Add support for key reporting via the jack interface
Some devices provide support for detection of a small number of buttons on their jacks. One common implementation provides a single button, implemented by shorting the microphone to ground and detected along with microphone presence detection by detecting varying current draws on the microphone bias signal. Provide support for up to three buttons via the jack interface. These default to reporting BTN_n but an API is provided to allow these to be remapped to other keys by the machine driver where it knows what the keys are. More keys can be added with ease if required. This is only intended to support simple accessory button designs. If the interface is limiting then either creating a child device for the accessory or accessing the input device in the jack directly is recommended. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
1 parent 1c6e555 commit ebb812c

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎include/sound/jack.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ enum snd_jack_types {
4242
SND_JACK_MECHANICAL = 0x0008, /* If detected separately */
4343
SND_JACK_VIDEOOUT = 0x0010,
4444
SND_JACK_AVOUT = SND_JACK_LINEOUT | SND_JACK_VIDEOOUT,
45+
46+
/* Kept separate from switches to facilitate implementation */
47+
SND_JACK_BTN_0 = 0x4000,
48+
SND_JACK_BTN_1 = 0x2000,
49+
SND_JACK_BTN_2 = 0x1000,
4550
};
4651

4752
struct snd_jack {
@@ -50,6 +55,7 @@ struct snd_jack {
5055
int type;
5156
const char *id;
5257
char name[100];
58+
unsigned int key[3]; /* Keep in sync with definitions above */
5359
void *private_data;
5460
void (*private_free)(struct snd_jack *);
5561
};
@@ -59,6 +65,8 @@ struct snd_jack {
5965
int snd_jack_new(struct snd_card *card, const char *id, int type,
6066
struct snd_jack **jack);
6167
void snd_jack_set_parent(struct snd_jack *jack, struct device *parent);
68+
int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
69+
int keytype);
6270

6371
void snd_jack_report(struct snd_jack *jack, int status);
6472

‎sound/core/jack.c‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static int snd_jack_dev_register(struct snd_device *device)
5555
{
5656
struct snd_jack *jack = device->device_data;
5757
struct snd_card *card = device->card;
58-
int err;
58+
int err, i;
5959

6060
snprintf(jack->name, sizeof(jack->name), "%s %s",
6161
card->shortname, jack->id);
@@ -65,6 +65,19 @@ static int snd_jack_dev_register(struct snd_device *device)
6565
if (!jack->input_dev->dev.parent)
6666
jack->input_dev->dev.parent = snd_card_get_device_link(card);
6767

68+
/* Add capabilities for any keys that are enabled */
69+
for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
70+
int testbit = SND_JACK_BTN_0 >> i;
71+
72+
if (!(jack->type & testbit))
73+
continue;
74+
75+
if (!jack->key[i])
76+
jack->key[i] = BTN_0 + i;
77+
78+
input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
79+
}
80+
6881
err = input_register_device(jack->input_dev);
6982
if (err == 0)
7083
jack->registered = 1;
@@ -150,6 +163,43 @@ void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
150163
}
151164
EXPORT_SYMBOL(snd_jack_set_parent);
152165

166+
/**
167+
* snd_jack_set_key - Set a key mapping on a jack
168+
*
169+
* @jack: The jack to configure
170+
* @type: Jack report type for this key
171+
* @keytype: Input layer key type to be reported
172+
*
173+
* Map a SND_JACK_BTN_ button type to an input layer key, allowing
174+
* reporting of keys on accessories via the jack abstraction. If no
175+
* mapping is provided but keys are enabled in the jack type then
176+
* BTN_n numeric buttons will be reported.
177+
*
178+
* Note that this is intended to be use by simple devices with small
179+
* numbers of keys that can be reported. It is also possible to
180+
* access the input device directly - devices with complex input
181+
* capabilities on accessories should consider doing this rather than
182+
* using this abstraction.
183+
*
184+
* This function may only be called prior to registration of the jack.
185+
*/
186+
int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
187+
int keytype)
188+
{
189+
int key = fls(SND_JACK_BTN_0) - fls(type);
190+
191+
WARN_ON(jack->registered);
192+
193+
if (!keytype || key >= ARRAY_SIZE(jack->key))
194+
return -EINVAL;
195+
196+
jack->type |= type;
197+
jack->key[key] = keytype;
198+
199+
return 0;
200+
}
201+
EXPORT_SYMBOL(snd_jack_set_key);
202+
153203
/**
154204
* snd_jack_report - Report the current status of a jack
155205
*
@@ -163,6 +213,14 @@ void snd_jack_report(struct snd_jack *jack, int status)
163213
if (!jack)
164214
return;
165215

216+
for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
217+
int testbit = SND_JACK_BTN_0 >> i;
218+
219+
if (jack->type & testbit)
220+
input_report_key(jack->input_dev, jack->key[i],
221+
status & testbit);
222+
}
223+
166224
for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
167225
int testbit = 1 << i;
168226
if (jack->type & testbit)

0 commit comments

Comments
 (0)