1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Jack abstraction layer
5 * Copyright 2008 Wolfson Microelectronics
8 #include <linux/input.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <sound/jack.h>
12 #include <sound/core.h>
13 #include <sound/control.h>
15 struct snd_jack_kctl {
16 struct snd_kcontrol *kctl;
17 struct list_head list; /* list of controls belong to the same jack */
18 unsigned int mask_bits; /* only masked status bits are reported via kctl */
21 #ifdef CONFIG_SND_JACK_INPUT_DEV
22 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
26 SW_JACK_PHYSICAL_INSERT,
30 #endif /* CONFIG_SND_JACK_INPUT_DEV */
32 static int snd_jack_dev_disconnect(struct snd_device *device)
34 #ifdef CONFIG_SND_JACK_INPUT_DEV
35 struct snd_jack *jack = device->device_data;
37 mutex_lock(&jack->input_dev_lock);
38 if (!jack->input_dev) {
39 mutex_unlock(&jack->input_dev_lock);
43 /* If the input device is registered with the input subsystem
44 * then we need to use a different deallocator. */
46 input_unregister_device(jack->input_dev);
48 input_free_device(jack->input_dev);
49 jack->input_dev = NULL;
50 mutex_unlock(&jack->input_dev_lock);
51 #endif /* CONFIG_SND_JACK_INPUT_DEV */
55 static int snd_jack_dev_free(struct snd_device *device)
57 struct snd_jack *jack = device->device_data;
58 struct snd_card *card = device->card;
59 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
61 down_write(&card->controls_rwsem);
62 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
63 list_del_init(&jack_kctl->list);
64 snd_ctl_remove(card, jack_kctl->kctl);
66 up_write(&card->controls_rwsem);
68 if (jack->private_free)
69 jack->private_free(jack);
71 snd_jack_dev_disconnect(device);
79 #ifdef CONFIG_SND_JACK_INPUT_DEV
80 static int snd_jack_dev_register(struct snd_device *device)
82 struct snd_jack *jack = device->device_data;
83 struct snd_card *card = device->card;
86 snprintf(jack->name, sizeof(jack->name), "%s %s",
87 card->shortname, jack->id);
89 mutex_lock(&jack->input_dev_lock);
90 if (!jack->input_dev) {
91 mutex_unlock(&jack->input_dev_lock);
95 jack->input_dev->name = jack->name;
97 /* Default to the sound card device. */
98 if (!jack->input_dev->dev.parent)
99 jack->input_dev->dev.parent = snd_card_get_device_link(card);
101 /* Add capabilities for any keys that are enabled */
102 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
103 int testbit = SND_JACK_BTN_0 >> i;
105 if (!(jack->type & testbit))
109 jack->key[i] = BTN_0 + i;
111 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
114 err = input_register_device(jack->input_dev);
116 jack->registered = 1;
118 mutex_unlock(&jack->input_dev_lock);
121 #endif /* CONFIG_SND_JACK_INPUT_DEV */
123 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
125 struct snd_jack_kctl *jack_kctl;
127 jack_kctl = kctl->private_data;
129 list_del(&jack_kctl->list);
134 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
136 list_add_tail(&jack_kctl->list, &jack->kctl_list);
139 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
141 struct snd_kcontrol *kctl;
142 struct snd_jack_kctl *jack_kctl;
145 kctl = snd_kctl_jack_new(name, card);
149 err = snd_ctl_add(card, kctl);
153 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
158 jack_kctl->kctl = kctl;
159 jack_kctl->mask_bits = mask;
161 kctl->private_data = jack_kctl;
162 kctl->private_free = snd_jack_kctl_private_free;
166 snd_ctl_free_one(kctl);
171 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
172 * @jack: the jack instance which the kctl will attaching to
173 * @name: the name for the snd_kcontrol object
174 * @mask: a bitmask of enum snd_jack_type values that can be detected
175 * by this snd_jack_kctl object.
177 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
179 * Return: Zero if successful, or a negative error code on failure.
181 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
183 struct snd_jack_kctl *jack_kctl;
185 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
189 snd_jack_kctl_add(jack, jack_kctl);
192 EXPORT_SYMBOL(snd_jack_add_new_kctl);
195 * snd_jack_new - Create a new jack
196 * @card: the card instance
197 * @id: an identifying string for this jack
198 * @type: a bitmask of enum snd_jack_type values that can be detected by
200 * @jjack: Used to provide the allocated jack object to the caller.
201 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
202 * @phantom_jack: Don't create a input device for phantom jacks.
204 * Creates a new jack object.
206 * Return: Zero if successful, or a negative error code on failure.
207 * On success @jjack will be initialised.
209 int snd_jack_new(struct snd_card *card, const char *id, int type,
210 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
212 struct snd_jack *jack;
213 struct snd_jack_kctl *jack_kctl = NULL;
215 static struct snd_device_ops ops = {
216 .dev_free = snd_jack_dev_free,
217 #ifdef CONFIG_SND_JACK_INPUT_DEV
218 .dev_register = snd_jack_dev_register,
219 .dev_disconnect = snd_jack_dev_disconnect,
220 #endif /* CONFIG_SND_JACK_INPUT_DEV */
224 jack_kctl = snd_jack_kctl_new(card, id, type);
229 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
233 jack->id = kstrdup(id, GFP_KERNEL);
234 if (jack->id == NULL) {
239 #ifdef CONFIG_SND_JACK_INPUT_DEV
240 mutex_init(&jack->input_dev_lock);
242 /* don't create input device for phantom jack */
246 jack->input_dev = input_allocate_device();
247 if (jack->input_dev == NULL) {
252 jack->input_dev->phys = "ALSA";
256 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
258 input_set_capability(jack->input_dev, EV_SW,
259 jack_switch_types[i]);
262 #endif /* CONFIG_SND_JACK_INPUT_DEV */
264 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
269 INIT_LIST_HEAD(&jack->kctl_list);
272 snd_jack_kctl_add(jack, jack_kctl);
279 #ifdef CONFIG_SND_JACK_INPUT_DEV
280 input_free_device(jack->input_dev);
286 EXPORT_SYMBOL(snd_jack_new);
288 #ifdef CONFIG_SND_JACK_INPUT_DEV
290 * snd_jack_set_parent - Set the parent device for a jack
292 * @jack: The jack to configure
293 * @parent: The device to set as parent for the jack.
295 * Set the parent for the jack devices in the device tree. This
296 * function is only valid prior to registration of the jack. If no
297 * parent is configured then the parent device will be the sound card.
299 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
301 WARN_ON(jack->registered);
302 mutex_lock(&jack->input_dev_lock);
303 if (!jack->input_dev) {
304 mutex_unlock(&jack->input_dev_lock);
308 jack->input_dev->dev.parent = parent;
309 mutex_unlock(&jack->input_dev_lock);
311 EXPORT_SYMBOL(snd_jack_set_parent);
314 * snd_jack_set_key - Set a key mapping on a jack
316 * @jack: The jack to configure
317 * @type: Jack report type for this key
318 * @keytype: Input layer key type to be reported
320 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
321 * reporting of keys on accessories via the jack abstraction. If no
322 * mapping is provided but keys are enabled in the jack type then
323 * BTN_n numeric buttons will be reported.
325 * If jacks are not reporting via the input API this call will have no
328 * Note that this is intended to be use by simple devices with small
329 * numbers of keys that can be reported. It is also possible to
330 * access the input device directly - devices with complex input
331 * capabilities on accessories should consider doing this rather than
332 * using this abstraction.
334 * This function may only be called prior to registration of the jack.
336 * Return: Zero if successful, or a negative error code on failure.
338 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
341 int key = fls(SND_JACK_BTN_0) - fls(type);
343 WARN_ON(jack->registered);
345 if (!keytype || key >= ARRAY_SIZE(jack->key))
349 jack->key[key] = keytype;
352 EXPORT_SYMBOL(snd_jack_set_key);
353 #endif /* CONFIG_SND_JACK_INPUT_DEV */
356 * snd_jack_report - Report the current status of a jack
357 * Note: This function uses mutexes and should be called from a
358 * context which can sleep (such as a workqueue).
360 * @jack: The jack to report status for
361 * @status: The current status of the jack
363 void snd_jack_report(struct snd_jack *jack, int status)
365 struct snd_jack_kctl *jack_kctl;
366 #ifdef CONFIG_SND_JACK_INPUT_DEV
367 struct input_dev *idev;
374 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
375 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
376 status & jack_kctl->mask_bits);
378 #ifdef CONFIG_SND_JACK_INPUT_DEV
379 idev = input_get_device(jack->input_dev);
383 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
384 int testbit = SND_JACK_BTN_0 >> i;
386 if (jack->type & testbit)
387 input_report_key(idev, jack->key[i],
391 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
392 int testbit = 1 << i;
393 if (jack->type & testbit)
394 input_report_switch(idev,
395 jack_switch_types[i],
400 input_put_device(idev);
401 #endif /* CONFIG_SND_JACK_INPUT_DEV */
403 EXPORT_SYMBOL(snd_jack_report);