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;
48 /* If the input device is registered with the input subsystem
49 * then we need to use a different deallocator. */
51 input_unregister_device(jack->input_dev);
53 input_free_device(jack->input_dev);
54 jack->input_dev = NULL;
55 #endif /* CONFIG_SND_JACK_INPUT_DEV */
59 static int snd_jack_dev_free(struct snd_device *device)
61 struct snd_jack *jack = device->device_data;
62 struct snd_card *card = device->card;
63 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
65 down_write(&card->controls_rwsem);
66 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
67 list_del_init(&jack_kctl->list);
68 snd_ctl_remove(card, jack_kctl->kctl);
70 up_write(&card->controls_rwsem);
72 if (jack->private_free)
73 jack->private_free(jack);
75 snd_jack_dev_disconnect(device);
83 #ifdef CONFIG_SND_JACK_INPUT_DEV
84 static int snd_jack_dev_register(struct snd_device *device)
86 struct snd_jack *jack = device->device_data;
87 struct snd_card *card = device->card;
90 snprintf(jack->name, sizeof(jack->name), "%s %s",
91 card->shortname, jack->id);
96 jack->input_dev->name = jack->name;
98 /* Default to the sound card device. */
99 if (!jack->input_dev->dev.parent)
100 jack->input_dev->dev.parent = snd_card_get_device_link(card);
102 /* Add capabilities for any keys that are enabled */
103 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
104 int testbit = SND_JACK_BTN_0 >> i;
106 if (!(jack->type & testbit))
110 jack->key[i] = BTN_0 + i;
112 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
115 err = input_register_device(jack->input_dev);
117 jack->registered = 1;
121 #endif /* CONFIG_SND_JACK_INPUT_DEV */
123 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
124 static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
126 struct snd_jack *jack;
127 #ifdef CONFIG_SND_JACK_INPUT_DEV
133 jack = jack_kctl->jack;
135 if (jack_kctl->sw_inject_enable)
136 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
137 status & jack_kctl->mask_bits);
139 #ifdef CONFIG_SND_JACK_INPUT_DEV
140 if (!jack->input_dev)
143 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
144 int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
146 if (jack->type & testbit)
147 input_report_key(jack->input_dev, jack->key[i],
151 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
152 int testbit = ((1 << i) & jack_kctl->mask_bits);
154 if (jack->type & testbit)
155 input_report_switch(jack->input_dev,
156 jack_switch_types[i],
160 input_sync(jack->input_dev);
161 #endif /* CONFIG_SND_JACK_INPUT_DEV */
164 static ssize_t sw_inject_enable_read(struct file *file,
165 char __user *to, size_t count, loff_t *ppos)
167 struct snd_jack_kctl *jack_kctl = file->private_data;
171 len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
172 "Inject Enabled", jack_kctl->sw_inject_enable);
173 ret = simple_read_from_buffer(to, count, ppos, buf, len);
178 static ssize_t sw_inject_enable_write(struct file *file,
179 const char __user *from, size_t count, loff_t *ppos)
181 struct snd_jack_kctl *jack_kctl = file->private_data;
183 unsigned long enable;
186 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
187 err = kstrtoul(buf, 0, &enable);
191 if (jack_kctl->sw_inject_enable == (!!enable))
194 jack_kctl->sw_inject_enable = !!enable;
196 if (!jack_kctl->sw_inject_enable)
197 snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
202 static ssize_t jackin_inject_write(struct file *file,
203 const char __user *from, size_t count, loff_t *ppos)
205 struct snd_jack_kctl *jack_kctl = file->private_data;
207 unsigned long enable;
210 if (!jack_kctl->sw_inject_enable)
213 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
214 err = kstrtoul(buf, 0, &enable);
218 snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
223 static ssize_t jack_kctl_id_read(struct file *file,
224 char __user *to, size_t count, loff_t *ppos)
226 struct snd_jack_kctl *jack_kctl = file->private_data;
230 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
231 ret = simple_read_from_buffer(to, count, ppos, buf, len);
236 /* the bit definition is aligned with snd_jack_types in jack.h */
237 static const char * const jack_events_name[] = {
238 "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
239 "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
240 "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)",
241 "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "",
244 /* the recommended buffer size is 256 */
245 static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
249 scnprintf(buf, buf_size, "0x%04x", mask_bits);
251 for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
252 if (mask_bits & (1 << i)) {
253 strlcat(buf, " ", buf_size);
254 strlcat(buf, jack_events_name[i], buf_size);
256 strlcat(buf, "\n", buf_size);
261 static ssize_t jack_kctl_mask_bits_read(struct file *file,
262 char __user *to, size_t count, loff_t *ppos)
264 struct snd_jack_kctl *jack_kctl = file->private_data;
268 len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
269 ret = simple_read_from_buffer(to, count, ppos, buf, len);
274 static ssize_t jack_kctl_status_read(struct file *file,
275 char __user *to, size_t count, loff_t *ppos)
277 struct snd_jack_kctl *jack_kctl = file->private_data;
281 len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
282 "Plugged" : "Unplugged");
283 ret = simple_read_from_buffer(to, count, ppos, buf, len);
288 #ifdef CONFIG_SND_JACK_INPUT_DEV
289 static ssize_t jack_type_read(struct file *file,
290 char __user *to, size_t count, loff_t *ppos)
292 struct snd_jack_kctl *jack_kctl = file->private_data;
296 len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
297 ret = simple_read_from_buffer(to, count, ppos, buf, len);
302 static const struct file_operations jack_type_fops = {
304 .read = jack_type_read,
305 .llseek = default_llseek,
309 static const struct file_operations sw_inject_enable_fops = {
311 .read = sw_inject_enable_read,
312 .write = sw_inject_enable_write,
313 .llseek = default_llseek,
316 static const struct file_operations jackin_inject_fops = {
318 .write = jackin_inject_write,
319 .llseek = default_llseek,
322 static const struct file_operations jack_kctl_id_fops = {
324 .read = jack_kctl_id_read,
325 .llseek = default_llseek,
328 static const struct file_operations jack_kctl_mask_bits_fops = {
330 .read = jack_kctl_mask_bits_read,
331 .llseek = default_llseek,
334 static const struct file_operations jack_kctl_status_fops = {
336 .read = jack_kctl_status_read,
337 .llseek = default_llseek,
340 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
341 struct snd_jack_kctl *jack_kctl)
346 /* Don't create injection interface for Phantom jacks */
347 if (strstr(jack_kctl->kctl->id.name, "Phantom"))
350 tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
354 /* replace the chars which are not suitable for folder's name with _ */
355 for (i = 0; tname[i]; i++)
356 if (!isalnum(tname[i]))
359 jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
362 debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
363 &sw_inject_enable_fops);
365 debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
366 &jackin_inject_fops);
368 debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
371 debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
372 &jack_kctl_mask_bits_fops);
374 debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
375 &jack_kctl_status_fops);
377 #ifdef CONFIG_SND_JACK_INPUT_DEV
378 debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
384 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
386 debugfs_remove(jack_kctl->jack_debugfs_root);
387 jack_kctl->jack_debugfs_root = NULL;
389 #else /* CONFIG_SND_JACK_INJECTION_DEBUG */
390 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
391 struct snd_jack_kctl *jack_kctl)
396 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
399 #endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
401 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
403 struct snd_jack_kctl *jack_kctl;
405 jack_kctl = kctl->private_data;
407 snd_jack_debugfs_clear_inject_node(jack_kctl);
408 list_del(&jack_kctl->list);
413 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
415 jack_kctl->jack = jack;
416 list_add_tail(&jack_kctl->list, &jack->kctl_list);
417 snd_jack_debugfs_add_inject_node(jack, jack_kctl);
420 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
422 struct snd_kcontrol *kctl;
423 struct snd_jack_kctl *jack_kctl;
426 kctl = snd_kctl_jack_new(name, card);
430 err = snd_ctl_add(card, kctl);
434 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
439 jack_kctl->kctl = kctl;
440 jack_kctl->mask_bits = mask;
442 kctl->private_data = jack_kctl;
443 kctl->private_free = snd_jack_kctl_private_free;
447 snd_ctl_free_one(kctl);
452 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
453 * @jack: the jack instance which the kctl will attaching to
454 * @name: the name for the snd_kcontrol object
455 * @mask: a bitmask of enum snd_jack_type values that can be detected
456 * by this snd_jack_kctl object.
458 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
460 * Return: Zero if successful, or a negative error code on failure.
462 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
464 struct snd_jack_kctl *jack_kctl;
466 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
470 snd_jack_kctl_add(jack, jack_kctl);
473 EXPORT_SYMBOL(snd_jack_add_new_kctl);
476 * snd_jack_new - Create a new jack
477 * @card: the card instance
478 * @id: an identifying string for this jack
479 * @type: a bitmask of enum snd_jack_type values that can be detected by
481 * @jjack: Used to provide the allocated jack object to the caller.
482 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
483 * @phantom_jack: Don't create a input device for phantom jacks.
485 * Creates a new jack object.
487 * Return: Zero if successful, or a negative error code on failure.
488 * On success @jjack will be initialised.
490 int snd_jack_new(struct snd_card *card, const char *id, int type,
491 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
493 struct snd_jack *jack;
494 struct snd_jack_kctl *jack_kctl = NULL;
496 static const struct snd_device_ops ops = {
497 .dev_free = snd_jack_dev_free,
498 #ifdef CONFIG_SND_JACK_INPUT_DEV
499 .dev_register = snd_jack_dev_register,
500 .dev_disconnect = snd_jack_dev_disconnect,
501 #endif /* CONFIG_SND_JACK_INPUT_DEV */
505 jack_kctl = snd_jack_kctl_new(card, id, type);
510 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
514 jack->id = kstrdup(id, GFP_KERNEL);
515 if (jack->id == NULL) {
520 /* don't creat input device for phantom jack */
522 #ifdef CONFIG_SND_JACK_INPUT_DEV
525 jack->input_dev = input_allocate_device();
526 if (jack->input_dev == NULL) {
531 jack->input_dev->phys = "ALSA";
535 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
537 input_set_capability(jack->input_dev, EV_SW,
538 jack_switch_types[i]);
540 #endif /* CONFIG_SND_JACK_INPUT_DEV */
543 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
548 INIT_LIST_HEAD(&jack->kctl_list);
551 snd_jack_kctl_add(jack, jack_kctl);
558 #ifdef CONFIG_SND_JACK_INPUT_DEV
559 input_free_device(jack->input_dev);
565 EXPORT_SYMBOL(snd_jack_new);
567 #ifdef CONFIG_SND_JACK_INPUT_DEV
569 * snd_jack_set_parent - Set the parent device for a jack
571 * @jack: The jack to configure
572 * @parent: The device to set as parent for the jack.
574 * Set the parent for the jack devices in the device tree. This
575 * function is only valid prior to registration of the jack. If no
576 * parent is configured then the parent device will be the sound card.
578 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
580 WARN_ON(jack->registered);
581 if (!jack->input_dev)
584 jack->input_dev->dev.parent = parent;
586 EXPORT_SYMBOL(snd_jack_set_parent);
589 * snd_jack_set_key - Set a key mapping on a jack
591 * @jack: The jack to configure
592 * @type: Jack report type for this key
593 * @keytype: Input layer key type to be reported
595 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
596 * reporting of keys on accessories via the jack abstraction. If no
597 * mapping is provided but keys are enabled in the jack type then
598 * BTN_n numeric buttons will be reported.
600 * If jacks are not reporting via the input API this call will have no
603 * Note that this is intended to be use by simple devices with small
604 * numbers of keys that can be reported. It is also possible to
605 * access the input device directly - devices with complex input
606 * capabilities on accessories should consider doing this rather than
607 * using this abstraction.
609 * This function may only be called prior to registration of the jack.
611 * Return: Zero if successful, or a negative error code on failure.
613 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
616 int key = fls(SND_JACK_BTN_0) - fls(type);
618 WARN_ON(jack->registered);
620 if (!keytype || key >= ARRAY_SIZE(jack->key))
624 jack->key[key] = keytype;
627 EXPORT_SYMBOL(snd_jack_set_key);
628 #endif /* CONFIG_SND_JACK_INPUT_DEV */
631 * snd_jack_report - Report the current status of a jack
633 * @jack: The jack to report status for
634 * @status: The current status of the jack
636 void snd_jack_report(struct snd_jack *jack, int status)
638 struct snd_jack_kctl *jack_kctl;
639 unsigned int mask_bits = 0;
640 #ifdef CONFIG_SND_JACK_INPUT_DEV
647 jack->hw_status_cache = status;
649 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
650 if (jack_kctl->sw_inject_enable)
651 mask_bits |= jack_kctl->mask_bits;
653 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
654 status & jack_kctl->mask_bits);
656 #ifdef CONFIG_SND_JACK_INPUT_DEV
657 if (!jack->input_dev)
660 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
661 int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
663 if (jack->type & testbit)
664 input_report_key(jack->input_dev, jack->key[i],
668 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
669 int testbit = ((1 << i) & ~mask_bits);
671 if (jack->type & testbit)
672 input_report_switch(jack->input_dev,
673 jack_switch_types[i],
677 input_sync(jack->input_dev);
678 #endif /* CONFIG_SND_JACK_INPUT_DEV */
680 EXPORT_SYMBOL(snd_jack_report);