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 <linux/ctype.h>
13 #include <linux/debugfs.h>
14 #include <sound/jack.h>
15 #include <sound/core.h>
16 #include <sound/control.h>
18 struct snd_jack_kctl {
19 struct snd_kcontrol *kctl;
20 struct list_head list; /* list of controls belong to the same jack */
21 unsigned int mask_bits; /* only masked status bits are reported via kctl */
22 struct snd_jack *jack; /* pointer to struct snd_jack */
23 bool sw_inject_enable; /* allow to inject plug event via debugfs */
24 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
25 struct dentry *jack_debugfs_root; /* jack_kctl debugfs root */
29 #ifdef CONFIG_SND_JACK_INPUT_DEV
30 static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
34 SW_JACK_PHYSICAL_INSERT,
38 #endif /* CONFIG_SND_JACK_INPUT_DEV */
40 static int snd_jack_dev_disconnect(struct snd_device *device)
42 #ifdef CONFIG_SND_JACK_INPUT_DEV
43 struct snd_jack *jack = device->device_data;
45 mutex_lock(&jack->input_dev_lock);
46 if (!jack->input_dev) {
47 mutex_unlock(&jack->input_dev_lock);
51 /* If the input device is registered with the input subsystem
52 * then we need to use a different deallocator. */
54 input_unregister_device(jack->input_dev);
56 input_free_device(jack->input_dev);
57 jack->input_dev = NULL;
58 mutex_unlock(&jack->input_dev_lock);
59 #endif /* CONFIG_SND_JACK_INPUT_DEV */
63 static int snd_jack_dev_free(struct snd_device *device)
65 struct snd_jack *jack = device->device_data;
66 struct snd_card *card = device->card;
67 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
69 down_write(&card->controls_rwsem);
70 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
71 list_del_init(&jack_kctl->list);
72 snd_ctl_remove(card, jack_kctl->kctl);
74 up_write(&card->controls_rwsem);
76 if (jack->private_free)
77 jack->private_free(jack);
79 snd_jack_dev_disconnect(device);
87 #ifdef CONFIG_SND_JACK_INPUT_DEV
88 static int snd_jack_dev_register(struct snd_device *device)
90 struct snd_jack *jack = device->device_data;
91 struct snd_card *card = device->card;
94 snprintf(jack->name, sizeof(jack->name), "%s %s",
95 card->shortname, jack->id);
97 mutex_lock(&jack->input_dev_lock);
98 if (!jack->input_dev) {
99 mutex_unlock(&jack->input_dev_lock);
103 jack->input_dev->name = jack->name;
105 /* Default to the sound card device. */
106 if (!jack->input_dev->dev.parent)
107 jack->input_dev->dev.parent = snd_card_get_device_link(card);
109 /* Add capabilities for any keys that are enabled */
110 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
111 int testbit = SND_JACK_BTN_0 >> i;
113 if (!(jack->type & testbit))
117 jack->key[i] = BTN_0 + i;
119 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
122 err = input_register_device(jack->input_dev);
124 jack->registered = 1;
126 mutex_unlock(&jack->input_dev_lock);
129 #endif /* CONFIG_SND_JACK_INPUT_DEV */
131 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
132 static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
134 struct snd_jack *jack;
135 #ifdef CONFIG_SND_JACK_INPUT_DEV
141 jack = jack_kctl->jack;
143 if (jack_kctl->sw_inject_enable)
144 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
145 status & jack_kctl->mask_bits);
147 #ifdef CONFIG_SND_JACK_INPUT_DEV
148 if (!jack->input_dev)
151 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
152 int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
154 if (jack->type & testbit)
155 input_report_key(jack->input_dev, jack->key[i],
159 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
160 int testbit = ((1 << i) & jack_kctl->mask_bits);
162 if (jack->type & testbit)
163 input_report_switch(jack->input_dev,
164 jack_switch_types[i],
168 input_sync(jack->input_dev);
169 #endif /* CONFIG_SND_JACK_INPUT_DEV */
172 static ssize_t sw_inject_enable_read(struct file *file,
173 char __user *to, size_t count, loff_t *ppos)
175 struct snd_jack_kctl *jack_kctl = file->private_data;
179 len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
180 "Inject Enabled", jack_kctl->sw_inject_enable);
181 ret = simple_read_from_buffer(to, count, ppos, buf, len);
186 static ssize_t sw_inject_enable_write(struct file *file,
187 const char __user *from, size_t count, loff_t *ppos)
189 struct snd_jack_kctl *jack_kctl = file->private_data;
191 unsigned long enable;
194 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
195 err = kstrtoul(buf, 0, &enable);
199 if (jack_kctl->sw_inject_enable == (!!enable))
202 jack_kctl->sw_inject_enable = !!enable;
204 if (!jack_kctl->sw_inject_enable)
205 snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
210 static ssize_t jackin_inject_write(struct file *file,
211 const char __user *from, size_t count, loff_t *ppos)
213 struct snd_jack_kctl *jack_kctl = file->private_data;
215 unsigned long enable;
218 if (!jack_kctl->sw_inject_enable)
221 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
222 err = kstrtoul(buf, 0, &enable);
226 snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
231 static ssize_t jack_kctl_id_read(struct file *file,
232 char __user *to, size_t count, loff_t *ppos)
234 struct snd_jack_kctl *jack_kctl = file->private_data;
238 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
239 ret = simple_read_from_buffer(to, count, ppos, buf, len);
244 /* the bit definition is aligned with snd_jack_types in jack.h */
245 static const char * const jack_events_name[] = {
246 "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
247 "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
248 "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)",
249 "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "",
252 /* the recommended buffer size is 256 */
253 static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
257 scnprintf(buf, buf_size, "0x%04x", mask_bits);
259 for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
260 if (mask_bits & (1 << i)) {
261 strlcat(buf, " ", buf_size);
262 strlcat(buf, jack_events_name[i], buf_size);
264 strlcat(buf, "\n", buf_size);
269 static ssize_t jack_kctl_mask_bits_read(struct file *file,
270 char __user *to, size_t count, loff_t *ppos)
272 struct snd_jack_kctl *jack_kctl = file->private_data;
276 len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
277 ret = simple_read_from_buffer(to, count, ppos, buf, len);
282 static ssize_t jack_kctl_status_read(struct file *file,
283 char __user *to, size_t count, loff_t *ppos)
285 struct snd_jack_kctl *jack_kctl = file->private_data;
289 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
290 "Plugged" : "Unplugged");
291 ret = simple_read_from_buffer(to, count, ppos, buf, len);
296 #ifdef CONFIG_SND_JACK_INPUT_DEV
297 static ssize_t jack_type_read(struct file *file,
298 char __user *to, size_t count, loff_t *ppos)
300 struct snd_jack_kctl *jack_kctl = file->private_data;
304 len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
305 ret = simple_read_from_buffer(to, count, ppos, buf, len);
310 static const struct file_operations jack_type_fops = {
312 .read = jack_type_read,
313 .llseek = default_llseek,
317 static const struct file_operations sw_inject_enable_fops = {
319 .read = sw_inject_enable_read,
320 .write = sw_inject_enable_write,
321 .llseek = default_llseek,
324 static const struct file_operations jackin_inject_fops = {
326 .write = jackin_inject_write,
327 .llseek = default_llseek,
330 static const struct file_operations jack_kctl_id_fops = {
332 .read = jack_kctl_id_read,
333 .llseek = default_llseek,
336 static const struct file_operations jack_kctl_mask_bits_fops = {
338 .read = jack_kctl_mask_bits_read,
339 .llseek = default_llseek,
342 static const struct file_operations jack_kctl_status_fops = {
344 .read = jack_kctl_status_read,
345 .llseek = default_llseek,
348 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
349 struct snd_jack_kctl *jack_kctl)
354 /* Don't create injection interface for Phantom jacks */
355 if (strstr(jack_kctl->kctl->id.name, "Phantom"))
358 tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
362 /* replace the chars which are not suitable for folder's name with _ */
363 for (i = 0; tname[i]; i++)
364 if (!isalnum(tname[i]))
367 jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
370 debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
371 &sw_inject_enable_fops);
373 debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
374 &jackin_inject_fops);
376 debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
379 debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
380 &jack_kctl_mask_bits_fops);
382 debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
383 &jack_kctl_status_fops);
385 #ifdef CONFIG_SND_JACK_INPUT_DEV
386 debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
392 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
394 debugfs_remove(jack_kctl->jack_debugfs_root);
395 jack_kctl->jack_debugfs_root = NULL;
397 #else /* CONFIG_SND_JACK_INJECTION_DEBUG */
398 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
399 struct snd_jack_kctl *jack_kctl)
404 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
407 #endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
409 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
411 struct snd_jack_kctl *jack_kctl;
413 jack_kctl = kctl->private_data;
415 snd_jack_debugfs_clear_inject_node(jack_kctl);
416 list_del(&jack_kctl->list);
421 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
423 jack_kctl->jack = jack;
424 list_add_tail(&jack_kctl->list, &jack->kctl_list);
425 snd_jack_debugfs_add_inject_node(jack, jack_kctl);
428 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
430 struct snd_kcontrol *kctl;
431 struct snd_jack_kctl *jack_kctl;
434 kctl = snd_kctl_jack_new(name, card);
438 err = snd_ctl_add(card, kctl);
442 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
447 jack_kctl->kctl = kctl;
448 jack_kctl->mask_bits = mask;
450 kctl->private_data = jack_kctl;
451 kctl->private_free = snd_jack_kctl_private_free;
455 snd_ctl_free_one(kctl);
460 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
461 * @jack: the jack instance which the kctl will attaching to
462 * @name: the name for the snd_kcontrol object
463 * @mask: a bitmask of enum snd_jack_type values that can be detected
464 * by this snd_jack_kctl object.
466 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
468 * Return: Zero if successful, or a negative error code on failure.
470 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
472 struct snd_jack_kctl *jack_kctl;
474 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
478 snd_jack_kctl_add(jack, jack_kctl);
481 EXPORT_SYMBOL(snd_jack_add_new_kctl);
484 * snd_jack_new - Create a new jack
485 * @card: the card instance
486 * @id: an identifying string for this jack
487 * @type: a bitmask of enum snd_jack_type values that can be detected by
489 * @jjack: Used to provide the allocated jack object to the caller.
490 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
491 * @phantom_jack: Don't create a input device for phantom jacks.
493 * Creates a new jack object.
495 * Return: Zero if successful, or a negative error code on failure.
496 * On success @jjack will be initialised.
498 int snd_jack_new(struct snd_card *card, const char *id, int type,
499 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
501 struct snd_jack *jack;
502 struct snd_jack_kctl *jack_kctl = NULL;
504 static const struct snd_device_ops ops = {
505 .dev_free = snd_jack_dev_free,
506 #ifdef CONFIG_SND_JACK_INPUT_DEV
507 .dev_register = snd_jack_dev_register,
508 .dev_disconnect = snd_jack_dev_disconnect,
509 #endif /* CONFIG_SND_JACK_INPUT_DEV */
513 jack_kctl = snd_jack_kctl_new(card, id, type);
518 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
522 jack->id = kstrdup(id, GFP_KERNEL);
523 if (jack->id == NULL) {
528 #ifdef CONFIG_SND_JACK_INPUT_DEV
529 mutex_init(&jack->input_dev_lock);
531 /* don't create input device for phantom jack */
535 jack->input_dev = input_allocate_device();
536 if (jack->input_dev == NULL) {
541 jack->input_dev->phys = "ALSA";
545 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
547 input_set_capability(jack->input_dev, EV_SW,
548 jack_switch_types[i]);
551 #endif /* CONFIG_SND_JACK_INPUT_DEV */
553 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
558 INIT_LIST_HEAD(&jack->kctl_list);
561 snd_jack_kctl_add(jack, jack_kctl);
568 #ifdef CONFIG_SND_JACK_INPUT_DEV
569 input_free_device(jack->input_dev);
575 EXPORT_SYMBOL(snd_jack_new);
577 #ifdef CONFIG_SND_JACK_INPUT_DEV
579 * snd_jack_set_parent - Set the parent device for a jack
581 * @jack: The jack to configure
582 * @parent: The device to set as parent for the jack.
584 * Set the parent for the jack devices in the device tree. This
585 * function is only valid prior to registration of the jack. If no
586 * parent is configured then the parent device will be the sound card.
588 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
590 WARN_ON(jack->registered);
591 mutex_lock(&jack->input_dev_lock);
592 if (!jack->input_dev) {
593 mutex_unlock(&jack->input_dev_lock);
597 jack->input_dev->dev.parent = parent;
598 mutex_unlock(&jack->input_dev_lock);
600 EXPORT_SYMBOL(snd_jack_set_parent);
603 * snd_jack_set_key - Set a key mapping on a jack
605 * @jack: The jack to configure
606 * @type: Jack report type for this key
607 * @keytype: Input layer key type to be reported
609 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
610 * reporting of keys on accessories via the jack abstraction. If no
611 * mapping is provided but keys are enabled in the jack type then
612 * BTN_n numeric buttons will be reported.
614 * If jacks are not reporting via the input API this call will have no
617 * Note that this is intended to be use by simple devices with small
618 * numbers of keys that can be reported. It is also possible to
619 * access the input device directly - devices with complex input
620 * capabilities on accessories should consider doing this rather than
621 * using this abstraction.
623 * This function may only be called prior to registration of the jack.
625 * Return: Zero if successful, or a negative error code on failure.
627 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
630 int key = fls(SND_JACK_BTN_0) - fls(type);
632 WARN_ON(jack->registered);
634 if (!keytype || key >= ARRAY_SIZE(jack->key))
638 jack->key[key] = keytype;
641 EXPORT_SYMBOL(snd_jack_set_key);
642 #endif /* CONFIG_SND_JACK_INPUT_DEV */
645 * snd_jack_report - Report the current status of a jack
646 * Note: This function uses mutexes and should be called from a
647 * context which can sleep (such as a workqueue).
649 * @jack: The jack to report status for
650 * @status: The current status of the jack
652 void snd_jack_report(struct snd_jack *jack, int status)
654 struct snd_jack_kctl *jack_kctl;
655 unsigned int mask_bits = 0;
656 #ifdef CONFIG_SND_JACK_INPUT_DEV
663 jack->hw_status_cache = status;
665 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
666 if (jack_kctl->sw_inject_enable)
667 mask_bits |= jack_kctl->mask_bits;
669 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
670 status & jack_kctl->mask_bits);
672 #ifdef CONFIG_SND_JACK_INPUT_DEV
673 mutex_lock(&jack->input_dev_lock);
674 if (!jack->input_dev) {
675 mutex_unlock(&jack->input_dev_lock);
679 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
680 int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
682 if (jack->type & testbit)
683 input_report_key(jack->input_dev, jack->key[i],
687 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
688 int testbit = ((1 << i) & ~mask_bits);
690 if (jack->type & testbit)
691 input_report_switch(jack->input_dev,
692 jack_switch_types[i],
696 input_sync(jack->input_dev);
697 mutex_unlock(&jack->input_dev_lock);
698 #endif /* CONFIG_SND_JACK_INPUT_DEV */
700 EXPORT_SYMBOL(snd_jack_report);