File Index
Files and directories
Sign in
FILE

jack.c

File preview with syntax highlighting.

linux-6.19.14/sound/core/jack.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Jack abstraction layer
  4 *
  5 *  Copyright 2008 Wolfson Microelectronics
  6 */
  7
  8#include <linux/input.h>
  9#include <linux/slab.h>
 10#include <linux/module.h>
 11#include <linux/ctype.h>
 12#include <linux/mm.h>
 13#include <linux/debugfs.h>
 14#include <sound/jack.h>
 15#include <sound/core.h>
 16#include <sound/control.h>
 17
 18struct 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 */
 26#endif
 27};
 28
 29#ifdef CONFIG_SND_JACK_INPUT_DEV
 30static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
 31	SW_HEADPHONE_INSERT,
 32	SW_MICROPHONE_INSERT,
 33	SW_LINEOUT_INSERT,
 34	SW_JACK_PHYSICAL_INSERT,
 35	SW_VIDEOOUT_INSERT,
 36	SW_LINEIN_INSERT,
 37	SW_USB_INSERT,
 38};
 39#endif /* CONFIG_SND_JACK_INPUT_DEV */
 40
 41static void snd_jack_remove_debugfs(struct snd_jack *jack);
 42
 43static int snd_jack_dev_disconnect(struct snd_device *device)
 44{
 45	struct snd_jack *jack = device->device_data;
 46
 47	snd_jack_remove_debugfs(jack);
 48
 49#ifdef CONFIG_SND_JACK_INPUT_DEV
 50	guard(mutex)(&jack->input_dev_lock);
 51	if (!jack->input_dev)
 52		return 0;
 53
 54	/* If the input device is registered with the input subsystem
 55	 * then we need to use a different deallocator. */
 56	if (jack->registered)
 57		input_unregister_device(jack->input_dev);
 58	else
 59		input_free_device(jack->input_dev);
 60	jack->input_dev = NULL;
 61#endif /* CONFIG_SND_JACK_INPUT_DEV */
 62	return 0;
 63}
 64
 65static int snd_jack_dev_free(struct snd_device *device)
 66{
 67	struct snd_jack *jack = device->device_data;
 68	struct snd_card *card = device->card;
 69	struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
 70
 71	list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
 72		list_del_init(&jack_kctl->list);
 73		snd_ctl_remove(card, jack_kctl->kctl);
 74	}
 75
 76	if (jack->private_free)
 77		jack->private_free(jack);
 78
 79	snd_jack_dev_disconnect(device);
 80
 81	kfree(jack->id);
 82	kfree(jack);
 83
 84	return 0;
 85}
 86
 87#ifdef CONFIG_SND_JACK_INPUT_DEV
 88static int snd_jack_dev_register(struct snd_device *device)
 89{
 90	struct snd_jack *jack = device->device_data;
 91	struct snd_card *card = device->card;
 92	int err, i;
 93
 94	snprintf(jack->name, sizeof(jack->name), "%s %s",
 95		 card->shortname, jack->id);
 96
 97	guard(mutex)(&jack->input_dev_lock);
 98	if (!jack->input_dev)
 99		return 0;
100
101	jack->input_dev->name = jack->name;
102
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);
106
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;
110
111		if (!(jack->type & testbit))
112			continue;
113
114		if (!jack->key[i])
115			jack->key[i] = BTN_0 + i;
116
117		input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
118	}
119
120	err = input_register_device(jack->input_dev);
121	if (err == 0)
122		jack->registered = 1;
123
124	return err;
125}
126#endif /* CONFIG_SND_JACK_INPUT_DEV */
127
128#ifdef CONFIG_SND_JACK_INJECTION_DEBUG
129static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
130{
131	struct snd_jack *jack;
132#ifdef CONFIG_SND_JACK_INPUT_DEV
133	int i;
134#endif
135	if (!jack_kctl)
136		return;
137
138	jack = jack_kctl->jack;
139
140	if (jack_kctl->sw_inject_enable)
141		snd_kctl_jack_report(jack->card, jack_kctl->kctl,
142				     status & jack_kctl->mask_bits);
143
144#ifdef CONFIG_SND_JACK_INPUT_DEV
145	if (!jack->input_dev)
146		return;
147
148	for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
149		int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
150
151		if (jack->type & testbit)
152			input_report_key(jack->input_dev, jack->key[i],
153					 status & testbit);
154	}
155
156	for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
157		int testbit = ((1 << i) & jack_kctl->mask_bits);
158
159		if (jack->type & testbit)
160			input_report_switch(jack->input_dev,
161					    jack_switch_types[i],
162					    status & testbit);
163	}
164
165	input_sync(jack->input_dev);
166#endif /* CONFIG_SND_JACK_INPUT_DEV */
167}
168
169static ssize_t sw_inject_enable_read(struct file *file,
170				     char __user *to, size_t count, loff_t *ppos)
171{
172	struct snd_jack_kctl *jack_kctl = file->private_data;
173	int len, ret;
174	char buf[128];
175
176	len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
177			"Inject Enabled", jack_kctl->sw_inject_enable);
178	ret = simple_read_from_buffer(to, count, ppos, buf, len);
179
180	return ret;
181}
182
183static ssize_t sw_inject_enable_write(struct file *file,
184				      const char __user *from, size_t count, loff_t *ppos)
185{
186	struct snd_jack_kctl *jack_kctl = file->private_data;
187	int ret, err;
188	unsigned long enable;
189	char buf[8] = { 0 };
190
191	ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
192	err = kstrtoul(buf, 0, &enable);
193	if (err)
194		return err;
195
196	if (jack_kctl->sw_inject_enable == (!!enable))
197		return ret;
198
199	jack_kctl->sw_inject_enable = !!enable;
200
201	if (!jack_kctl->sw_inject_enable)
202		snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
203
204	return ret;
205}
206
207static ssize_t jackin_inject_write(struct file *file,
208				   const char __user *from, size_t count, loff_t *ppos)
209{
210	struct snd_jack_kctl *jack_kctl = file->private_data;
211	int ret, err;
212	unsigned long enable;
213	char buf[8] = { 0 };
214
215	if (!jack_kctl->sw_inject_enable)
216		return -EINVAL;
217
218	ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
219	err = kstrtoul(buf, 0, &enable);
220	if (err)
221		return err;
222
223	snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
224
225	return ret;
226}
227
228static ssize_t jack_kctl_id_read(struct file *file,
229				 char __user *to, size_t count, loff_t *ppos)
230{
231	struct snd_jack_kctl *jack_kctl = file->private_data;
232	char buf[64];
233	int len, ret;
234
235	len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
236	ret = simple_read_from_buffer(to, count, ppos, buf, len);
237
238	return ret;
239}
240
241/* the bit definition is aligned with snd_jack_types in jack.h */
242static const char * const jack_events_name[] = {
243	"HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
244	"MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
245	"USB(0x0040)", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)",
246	"BTN_3(0x0800)", "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)",
247	"",
248};
249
250/* the recommended buffer size is 256 */
251static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
252{
253	int i;
254
255	scnprintf(buf, buf_size, "0x%04x", mask_bits);
256
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);
261		}
262	strlcat(buf, "\n", buf_size);
263
264	return strlen(buf);
265}
266
267static ssize_t jack_kctl_mask_bits_read(struct file *file,
268					char __user *to, size_t count, loff_t *ppos)
269{
270	struct snd_jack_kctl *jack_kctl = file->private_data;
271	char buf[256];
272	int len, ret;
273
274	len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
275	ret = simple_read_from_buffer(to, count, ppos, buf, len);
276
277	return ret;
278}
279
280static ssize_t jack_kctl_status_read(struct file *file,
281				     char __user *to, size_t count, loff_t *ppos)
282{
283	struct snd_jack_kctl *jack_kctl = file->private_data;
284	char buf[16];
285	int len, ret;
286
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);
290
291	return ret;
292}
293
294#ifdef CONFIG_SND_JACK_INPUT_DEV
295static ssize_t jack_type_read(struct file *file,
296			      char __user *to, size_t count, loff_t *ppos)
297{
298	struct snd_jack_kctl *jack_kctl = file->private_data;
299	char buf[256];
300	int len, ret;
301
302	len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
303	ret = simple_read_from_buffer(to, count, ppos, buf, len);
304
305	return ret;
306}
307
308static const struct file_operations jack_type_fops = {
309	.open = simple_open,
310	.read = jack_type_read,
311	.llseek = default_llseek,
312};
313#endif
314
315static const struct file_operations sw_inject_enable_fops = {
316	.open = simple_open,
317	.read = sw_inject_enable_read,
318	.write = sw_inject_enable_write,
319	.llseek = default_llseek,
320};
321
322static const struct file_operations jackin_inject_fops = {
323	.open = simple_open,
324	.write = jackin_inject_write,
325	.llseek = default_llseek,
326};
327
328static const struct file_operations jack_kctl_id_fops = {
329	.open = simple_open,
330	.read = jack_kctl_id_read,
331	.llseek = default_llseek,
332};
333
334static const struct file_operations jack_kctl_mask_bits_fops = {
335	.open = simple_open,
336	.read = jack_kctl_mask_bits_read,
337	.llseek = default_llseek,
338};
339
340static const struct file_operations jack_kctl_status_fops = {
341	.open = simple_open,
342	.read = jack_kctl_status_read,
343	.llseek = default_llseek,
344};
345
346static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
347					    struct snd_jack_kctl *jack_kctl)
348{
349	char *tname;
350	int i;
351
352	/* Don't create injection interface for Phantom jacks */
353	if (strstr(jack_kctl->kctl->id.name, "Phantom"))
354		return 0;
355
356	tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
357	if (!tname)
358		return -ENOMEM;
359
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]))
363			tname[i] = '_';
364
365	jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
366	kfree(tname);
367
368	debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
369			    &sw_inject_enable_fops);
370
371	debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
372			    &jackin_inject_fops);
373
374	debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
375			    &jack_kctl_id_fops);
376
377	debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
378			    &jack_kctl_mask_bits_fops);
379
380	debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
381			    &jack_kctl_status_fops);
382
383#ifdef CONFIG_SND_JACK_INPUT_DEV
384	debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
385			    &jack_type_fops);
386#endif
387	return 0;
388}
389
390static void snd_jack_remove_debugfs(struct snd_jack *jack)
391{
392	struct snd_jack_kctl *jack_kctl;
393
394	list_for_each_entry(jack_kctl, &jack->kctl_list, list) {
395		debugfs_remove(jack_kctl->jack_debugfs_root);
396		jack_kctl->jack_debugfs_root = NULL;
397	}
398}
399#else /* CONFIG_SND_JACK_INJECTION_DEBUG */
400static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
401					    struct snd_jack_kctl *jack_kctl)
402{
403	return 0;
404}
405
406static void snd_jack_remove_debugfs(struct snd_jack *jack)
407{
408}
409#endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
410
411static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
412{
413	struct snd_jack_kctl *jack_kctl;
414
415	jack_kctl = kctl->private_data;
416	if (jack_kctl) {
417		list_del(&jack_kctl->list);
418		kfree(jack_kctl);
419	}
420}
421
422static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
423{
424	jack_kctl->jack = jack;
425	list_add_tail(&jack_kctl->list, &jack->kctl_list);
426	snd_jack_debugfs_add_inject_node(jack, jack_kctl);
427}
428
429static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
430{
431	struct snd_kcontrol *kctl;
432	struct snd_jack_kctl *jack_kctl;
433	int err;
434
435	kctl = snd_kctl_jack_new(name, card);
436	if (!kctl)
437		return NULL;
438
439	err = snd_ctl_add(card, kctl);
440	if (err < 0)
441		return NULL;
442
443	jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
444
445	if (!jack_kctl)
446		goto error;
447
448	jack_kctl->kctl = kctl;
449	jack_kctl->mask_bits = mask;
450
451	kctl->private_data = jack_kctl;
452	kctl->private_free = snd_jack_kctl_private_free;
453
454	return jack_kctl;
455error:
456	snd_ctl_free_one(kctl);
457	return NULL;
458}
459
460/**
461 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
462 * @jack:  the jack instance which the kctl will attaching to
463 * @name:  the name for the snd_kcontrol object
464 * @mask:  a bitmask of enum snd_jack_type values that can be detected
465 *         by this snd_jack_kctl object.
466 *
467 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
468 *
469 * Return: Zero if successful, or a negative error code on failure.
470 */
471int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
472{
473	struct snd_jack_kctl *jack_kctl;
474
475	jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
476	if (!jack_kctl)
477		return -ENOMEM;
478
479	snd_jack_kctl_add(jack, jack_kctl);
480	return 0;
481}
482EXPORT_SYMBOL(snd_jack_add_new_kctl);
483
484/**
485 * snd_jack_new - Create a new jack
486 * @card:  the card instance
487 * @id:    an identifying string for this jack
488 * @type:  a bitmask of enum snd_jack_type values that can be detected by
489 *         this jack
490 * @jjack: Used to provide the allocated jack object to the caller.
491 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
492 * @phantom_jack: Don't create a input device for phantom jacks.
493 *
494 * Creates a new jack object.
495 *
496 * Return: Zero if successful, or a negative error code on failure.
497 * On success @jjack will be initialised.
498 */
499int snd_jack_new(struct snd_card *card, const char *id, int type,
500		 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
501{
502	struct snd_jack *jack;
503	struct snd_jack_kctl *jack_kctl = NULL;
504	int err;
505	static const struct snd_device_ops ops = {
506		.dev_free = snd_jack_dev_free,
507#ifdef CONFIG_SND_JACK_INPUT_DEV
508		.dev_register = snd_jack_dev_register,
509#endif /* CONFIG_SND_JACK_INPUT_DEV */
510		.dev_disconnect = snd_jack_dev_disconnect,
511	};
512
513	if (initial_kctl) {
514		jack_kctl = snd_jack_kctl_new(card, id, type);
515		if (!jack_kctl)
516			return -ENOMEM;
517	}
518
519	jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
520	if (jack == NULL)
521		return -ENOMEM;
522
523	jack->id = kstrdup(id, GFP_KERNEL);
524	if (jack->id == NULL) {
525		kfree(jack);
526		return -ENOMEM;
527	}
528
529#ifdef CONFIG_SND_JACK_INPUT_DEV
530	mutex_init(&jack->input_dev_lock);
531
532	/* don't create input device for phantom jack */
533	if (!phantom_jack) {
534		int i;
535
536		jack->input_dev = input_allocate_device();
537		if (jack->input_dev == NULL) {
538			err = -ENOMEM;
539			goto fail_input;
540		}
541
542		jack->input_dev->phys = "ALSA";
543
544		jack->type = type;
545
546		for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
547			if (type & (1 << i))
548				input_set_capability(jack->input_dev, EV_SW,
549						     jack_switch_types[i]);
550
551	}
552#endif /* CONFIG_SND_JACK_INPUT_DEV */
553
554	err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
555	if (err < 0)
556		goto fail_input;
557
558	jack->card = card;
559	INIT_LIST_HEAD(&jack->kctl_list);
560
561	if (initial_kctl)
562		snd_jack_kctl_add(jack, jack_kctl);
563
564	*jjack = jack;
565
566	return 0;
567
568fail_input:
569#ifdef CONFIG_SND_JACK_INPUT_DEV
570	input_free_device(jack->input_dev);
571#endif
572	kfree(jack->id);
573	kfree(jack);
574	return err;
575}
576EXPORT_SYMBOL(snd_jack_new);
577
578#ifdef CONFIG_SND_JACK_INPUT_DEV
579/**
580 * snd_jack_set_key - Set a key mapping on a jack
581 *
582 * @jack:    The jack to configure
583 * @type:    Jack report type for this key
584 * @keytype: Input layer key type to be reported
585 *
586 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
587 * reporting of keys on accessories via the jack abstraction.  If no
588 * mapping is provided but keys are enabled in the jack type then
589 * BTN_n numeric buttons will be reported.
590 *
591 * If jacks are not reporting via the input API this call will have no
592 * effect.
593 *
594 * Note that this is intended to be use by simple devices with small
595 * numbers of keys that can be reported.  It is also possible to
596 * access the input device directly - devices with complex input
597 * capabilities on accessories should consider doing this rather than
598 * using this abstraction.
599 *
600 * This function may only be called prior to registration of the jack.
601 *
602 * Return: Zero if successful, or a negative error code on failure.
603 */
604int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
605		     int keytype)
606{
607	int key = fls(SND_JACK_BTN_0) - fls(type);
608
609	WARN_ON(jack->registered);
610
611	if (!keytype || key >= ARRAY_SIZE(jack->key))
612		return -EINVAL;
613
614	jack->type |= type;
615	jack->key[key] = keytype;
616	return 0;
617}
618EXPORT_SYMBOL(snd_jack_set_key);
619#endif /* CONFIG_SND_JACK_INPUT_DEV */
620
621/**
622 * snd_jack_report - Report the current status of a jack
623 * Note: This function uses mutexes and should be called from a
624 * context which can sleep (such as a workqueue).
625 *
626 * @jack:   The jack to report status for
627 * @status: The current status of the jack
628 */
629void snd_jack_report(struct snd_jack *jack, int status)
630{
631	struct snd_jack_kctl *jack_kctl;
632	unsigned int mask_bits = 0;
633#ifdef CONFIG_SND_JACK_INPUT_DEV
634	struct input_dev *idev;
635	int i;
636#endif
637
638	if (!jack)
639		return;
640
641	jack->hw_status_cache = status;
642
643	list_for_each_entry(jack_kctl, &jack->kctl_list, list)
644		if (jack_kctl->sw_inject_enable)
645			mask_bits |= jack_kctl->mask_bits;
646		else
647			snd_kctl_jack_report(jack->card, jack_kctl->kctl,
648					     status & jack_kctl->mask_bits);
649
650#ifdef CONFIG_SND_JACK_INPUT_DEV
651	idev = input_get_device(jack->input_dev);
652	if (!idev)
653		return;
654
655	for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
656		int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
657
658		if (jack->type & testbit)
659			input_report_key(idev, jack->key[i],
660					 status & testbit);
661	}
662
663	for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
664		int testbit = ((1 << i) & ~mask_bits);
665
666		if (jack->type & testbit)
667			input_report_switch(idev,
668					    jack_switch_types[i],
669					    status & testbit);
670	}
671
672	input_sync(idev);
673	input_put_device(idev);
674#endif /* CONFIG_SND_JACK_INPUT_DEV */
675}
676EXPORT_SYMBOL(snd_jack_report);
~/ Read-only file preview.