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 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
70 list_del_init(&jack_kctl->list);
71 snd_ctl_remove(card, jack_kctl->kctl);
74 if (jack->private_free)
75 jack->private_free(jack);
77 snd_jack_dev_disconnect(device);
85 #ifdef CONFIG_SND_JACK_INPUT_DEV
86 static int snd_jack_dev_register(struct snd_device *device)
88 struct snd_jack *jack = device->device_data;
89 struct snd_card *card = device->card;
92 snprintf(jack->name, sizeof(jack->name), "%s %s",
93 card->shortname, jack->id);
95 mutex_lock(&jack->input_dev_lock);
96 if (!jack->input_dev) {
97 mutex_unlock(&jack->input_dev_lock);
101 jack->input_dev->name = jack->name;
103 /* Default to the sound card device. */
104 if (!jack->input_dev->dev.parent)
105 jack->input_dev->dev.parent = snd_card_get_device_link(card);
107 /* Add capabilities for any keys that are enabled */
108 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
109 int testbit = SND_JACK_BTN_0 >> i;
111 if (!(jack->type & testbit))
115 jack->key[i] = BTN_0 + i;
117 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
120 err = input_register_device(jack->input_dev);
122 jack->registered = 1;
124 mutex_unlock(&jack->input_dev_lock);
127 #endif /* CONFIG_SND_JACK_INPUT_DEV */
129 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
130 static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
132 struct snd_jack *jack;
133 #ifdef CONFIG_SND_JACK_INPUT_DEV
139 jack = jack_kctl->jack;
141 if (jack_kctl->sw_inject_enable)
142 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
143 status & jack_kctl->mask_bits);
145 #ifdef CONFIG_SND_JACK_INPUT_DEV
146 if (!jack->input_dev)
149 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
150 int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
152 if (jack->type & testbit)
153 input_report_key(jack->input_dev, jack->key[i],
157 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
158 int testbit = ((1 << i) & jack_kctl->mask_bits);
160 if (jack->type & testbit)
161 input_report_switch(jack->input_dev,
162 jack_switch_types[i],
166 input_sync(jack->input_dev);
167 #endif /* CONFIG_SND_JACK_INPUT_DEV */
170 static ssize_t sw_inject_enable_read(struct file *file,
171 char __user *to, size_t count, loff_t *ppos)
173 struct snd_jack_kctl *jack_kctl = file->private_data;
177 len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
178 "Inject Enabled", jack_kctl->sw_inject_enable);
179 ret = simple_read_from_buffer(to, count, ppos, buf, len);
184 static ssize_t sw_inject_enable_write(struct file *file,
185 const char __user *from, size_t count, loff_t *ppos)
187 struct snd_jack_kctl *jack_kctl = file->private_data;
189 unsigned long enable;
192 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
193 err = kstrtoul(buf, 0, &enable);
197 if (jack_kctl->sw_inject_enable == (!!enable))
200 jack_kctl->sw_inject_enable = !!enable;
202 if (!jack_kctl->sw_inject_enable)
203 snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
208 static ssize_t jackin_inject_write(struct file *file,
209 const char __user *from, size_t count, loff_t *ppos)
211 struct snd_jack_kctl *jack_kctl = file->private_data;
213 unsigned long enable;
216 if (!jack_kctl->sw_inject_enable)
219 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
220 err = kstrtoul(buf, 0, &enable);
224 snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
229 static ssize_t jack_kctl_id_read(struct file *file,
230 char __user *to, size_t count, loff_t *ppos)
232 struct snd_jack_kctl *jack_kctl = file->private_data;
236 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
237 ret = simple_read_from_buffer(to, count, ppos, buf, len);
242 /* the bit definition is aligned with snd_jack_types in jack.h */
243 static const char * const jack_events_name[] = {
244 "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
245 "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
246 "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)",
247 "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "",
250 /* the recommended buffer size is 256 */
251 static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
255 scnprintf(buf, buf_size, "0x%04x", mask_bits);
257 for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
258 if (mask_bits & (1 << i)) {
259 strlcat(buf, " ", buf_size);
260 strlcat(buf, jack_events_name[i], buf_size);
262 strlcat(buf, "\n", buf_size);
267 static ssize_t jack_kctl_mask_bits_read(struct file *file,
268 char __user *to, size_t count, loff_t *ppos)
270 struct snd_jack_kctl *jack_kctl = file->private_data;
274 len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
275 ret = simple_read_from_buffer(to, count, ppos, buf, len);
280 static ssize_t jack_kctl_status_read(struct file *file,
281 char __user *to, size_t count, loff_t *ppos)
283 struct snd_jack_kctl *jack_kctl = file->private_data;
287 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
288 "Plugged" : "Unplugged");
289 ret = simple_read_from_buffer(to, count, ppos, buf, len);
294 #ifdef CONFIG_SND_JACK_INPUT_DEV
295 static ssize_t jack_type_read(struct file *file,
296 char __user *to, size_t count, loff_t *ppos)
298 struct snd_jack_kctl *jack_kctl = file->private_data;
302 len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
303 ret = simple_read_from_buffer(to, count, ppos, buf, len);
308 static const struct file_operations jack_type_fops = {
310 .read = jack_type_read,
311 .llseek = default_llseek,
315 static const struct file_operations sw_inject_enable_fops = {
317 .read = sw_inject_enable_read,
318 .write = sw_inject_enable_write,
319 .llseek = default_llseek,
322 static const struct file_operations jackin_inject_fops = {
324 .write = jackin_inject_write,
325 .llseek = default_llseek,
328 static const struct file_operations jack_kctl_id_fops = {
330 .read = jack_kctl_id_read,
331 .llseek = default_llseek,
334 static const struct file_operations jack_kctl_mask_bits_fops = {
336 .read = jack_kctl_mask_bits_read,
337 .llseek = default_llseek,
340 static const struct file_operations jack_kctl_status_fops = {
342 .read = jack_kctl_status_read,
343 .llseek = default_llseek,
346 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
347 struct snd_jack_kctl *jack_kctl)
352 /* Don't create injection interface for Phantom jacks */
353 if (strstr(jack_kctl->kctl->id.name, "Phantom"))
356 tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
360 /* replace the chars which are not suitable for folder's name with _ */
361 for (i = 0; tname[i]; i++)
362 if (!isalnum(tname[i]))
365 jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
368 debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
369 &sw_inject_enable_fops);
371 debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
372 &jackin_inject_fops);
374 debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
377 debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
378 &jack_kctl_mask_bits_fops);
380 debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
381 &jack_kctl_status_fops);
383 #ifdef CONFIG_SND_JACK_INPUT_DEV
384 debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
390 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
392 debugfs_remove(jack_kctl->jack_debugfs_root);
393 jack_kctl->jack_debugfs_root = NULL;
395 #else /* CONFIG_SND_JACK_INJECTION_DEBUG */
396 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
397 struct snd_jack_kctl *jack_kctl)
402 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
405 #endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
407 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
409 struct snd_jack_kctl *jack_kctl;
411 jack_kctl = kctl->private_data;
413 snd_jack_debugfs_clear_inject_node(jack_kctl);
414 list_del(&jack_kctl->list);
419 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
421 jack_kctl->jack = jack;
422 list_add_tail(&jack_kctl->list, &jack->kctl_list);
423 snd_jack_debugfs_add_inject_node(jack, jack_kctl);
426 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
428 struct snd_kcontrol *kctl;
429 struct snd_jack_kctl *jack_kctl;
432 kctl = snd_kctl_jack_new(name, card);
436 err = snd_ctl_add(card, kctl);
440 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
445 jack_kctl->kctl = kctl;
446 jack_kctl->mask_bits = mask;
448 kctl->private_data = jack_kctl;
449 kctl->private_free = snd_jack_kctl_private_free;
453 snd_ctl_free_one(kctl);
458 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
459 * @jack: the jack instance which the kctl will attaching to
460 * @name: the name for the snd_kcontrol object
461 * @mask: a bitmask of enum snd_jack_type values that can be detected
462 * by this snd_jack_kctl object.
464 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
466 * Return: Zero if successful, or a negative error code on failure.
468 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
470 struct snd_jack_kctl *jack_kctl;
472 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
476 snd_jack_kctl_add(jack, jack_kctl);
479 EXPORT_SYMBOL(snd_jack_add_new_kctl);
482 * snd_jack_new - Create a new jack
483 * @card: the card instance
484 * @id: an identifying string for this jack
485 * @type: a bitmask of enum snd_jack_type values that can be detected by
487 * @jjack: Used to provide the allocated jack object to the caller.
488 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
489 * @phantom_jack: Don't create a input device for phantom jacks.
491 * Creates a new jack object.
493 * Return: Zero if successful, or a negative error code on failure.
494 * On success @jjack will be initialised.
496 int snd_jack_new(struct snd_card *card, const char *id, int type,
497 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
499 struct snd_jack *jack;
500 struct snd_jack_kctl *jack_kctl = NULL;
502 static const struct snd_device_ops ops = {
503 .dev_free = snd_jack_dev_free,
504 #ifdef CONFIG_SND_JACK_INPUT_DEV
505 .dev_register = snd_jack_dev_register,
506 .dev_disconnect = snd_jack_dev_disconnect,
507 #endif /* CONFIG_SND_JACK_INPUT_DEV */
511 jack_kctl = snd_jack_kctl_new(card, id, type);
516 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
520 jack->id = kstrdup(id, GFP_KERNEL);
521 if (jack->id == NULL) {
526 #ifdef CONFIG_SND_JACK_INPUT_DEV
527 mutex_init(&jack->input_dev_lock);
529 /* don't create input device for phantom jack */
533 jack->input_dev = input_allocate_device();
534 if (jack->input_dev == NULL) {
539 jack->input_dev->phys = "ALSA";
543 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
545 input_set_capability(jack->input_dev, EV_SW,
546 jack_switch_types[i]);
549 #endif /* CONFIG_SND_JACK_INPUT_DEV */
551 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
556 INIT_LIST_HEAD(&jack->kctl_list);
559 snd_jack_kctl_add(jack, jack_kctl);
566 #ifdef CONFIG_SND_JACK_INPUT_DEV
567 input_free_device(jack->input_dev);
573 EXPORT_SYMBOL(snd_jack_new);
575 #ifdef CONFIG_SND_JACK_INPUT_DEV
577 * snd_jack_set_parent - Set the parent device for a jack
579 * @jack: The jack to configure
580 * @parent: The device to set as parent for the jack.
582 * Set the parent for the jack devices in the device tree. This
583 * function is only valid prior to registration of the jack. If no
584 * parent is configured then the parent device will be the sound card.
586 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
588 WARN_ON(jack->registered);
589 mutex_lock(&jack->input_dev_lock);
590 if (!jack->input_dev) {
591 mutex_unlock(&jack->input_dev_lock);
595 jack->input_dev->dev.parent = parent;
596 mutex_unlock(&jack->input_dev_lock);
598 EXPORT_SYMBOL(snd_jack_set_parent);
601 * snd_jack_set_key - Set a key mapping on a jack
603 * @jack: The jack to configure
604 * @type: Jack report type for this key
605 * @keytype: Input layer key type to be reported
607 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
608 * reporting of keys on accessories via the jack abstraction. If no
609 * mapping is provided but keys are enabled in the jack type then
610 * BTN_n numeric buttons will be reported.
612 * If jacks are not reporting via the input API this call will have no
615 * Note that this is intended to be use by simple devices with small
616 * numbers of keys that can be reported. It is also possible to
617 * access the input device directly - devices with complex input
618 * capabilities on accessories should consider doing this rather than
619 * using this abstraction.
621 * This function may only be called prior to registration of the jack.
623 * Return: Zero if successful, or a negative error code on failure.
625 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
628 int key = fls(SND_JACK_BTN_0) - fls(type);
630 WARN_ON(jack->registered);
632 if (!keytype || key >= ARRAY_SIZE(jack->key))
636 jack->key[key] = keytype;
639 EXPORT_SYMBOL(snd_jack_set_key);
640 #endif /* CONFIG_SND_JACK_INPUT_DEV */
643 * snd_jack_report - Report the current status of a jack
644 * Note: This function uses mutexes and should be called from a
645 * context which can sleep (such as a workqueue).
647 * @jack: The jack to report status for
648 * @status: The current status of the jack
650 void snd_jack_report(struct snd_jack *jack, int status)
652 struct snd_jack_kctl *jack_kctl;
653 unsigned int mask_bits = 0;
654 #ifdef CONFIG_SND_JACK_INPUT_DEV
655 struct input_dev *idev;
662 jack->hw_status_cache = status;
664 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
665 if (jack_kctl->sw_inject_enable)
666 mask_bits |= jack_kctl->mask_bits;
668 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
669 status & jack_kctl->mask_bits);
671 #ifdef CONFIG_SND_JACK_INPUT_DEV
672 idev = input_get_device(jack->input_dev);
676 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
677 int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
679 if (jack->type & testbit)
680 input_report_key(idev, jack->key[i],
684 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
685 int testbit = ((1 << i) & ~mask_bits);
687 if (jack->type & testbit)
688 input_report_switch(idev,
689 jack_switch_types[i],
694 input_put_device(idev);
695 #endif /* CONFIG_SND_JACK_INPUT_DEV */
697 EXPORT_SYMBOL(snd_jack_report);