From patchwork Mon Dec 28 08:00:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hui Wang X-Patchwork-Id: 11990905 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F6A3C433DB for ; Mon, 28 Dec 2020 08:02:15 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5D3EC207B6 for ; Mon, 28 Dec 2020 08:02:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D3EC207B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 24E96170B; Mon, 28 Dec 2020 09:01:22 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 24E96170B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1609142532; bh=yC8uRdX64+jiwBcM47lNTFlEVTT/847Rak0fp3O9eis=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=BLrHgXhFEbazwmJhE4uBlLzqxAs8b3EZLbJ0b10rteCHHIbyBr4ytGUIO8o7hv55o iYm7MULb8x53MjXLclkNAfjNwUOse8RcDSRsySd3H2Z4/+Ev0NflA5aZjxDfEQT0I7 iURaEEs6qHfNY3SccrDBBq0Yw94Vc26zcrsTo9iI= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id CF648F80169; Mon, 28 Dec 2020 09:00:26 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id C6B3CF801F5; Mon, 28 Dec 2020 09:00:23 +0100 (CET) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 5290FF80143 for ; Mon, 28 Dec 2020 09:00:16 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 5290FF80143 Received: from [223.72.45.82] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ktnRv-0004UZ-E3; Mon, 28 Dec 2020 08:00:16 +0000 From: Hui Wang To: alsa-devel@alsa-project.org, tiwai@suse.de, perex@perex.cz, kai.vehmanen@linux.intel.com Subject: [RFC][PATCH v3 1/4] alsa: jack: implement software jack injection via debugfs Date: Mon, 28 Dec 2020 16:00:00 +0800 Message-Id: <20201228080003.19127-2-hui.wang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228080003.19127-1-hui.wang@canonical.com> References: <20201228080003.19127-1-hui.wang@canonical.com> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" We want to perform remote audio auto test, need the audio jack to change from plugout to plugin or vice versa by software ways. Here the design is creating a sound-core root folder in the debugfs dir, and each sound card will create a folder cardN under sound-core, then the sound jack will create folders by jack_ctrl->ctrl->id.name, and will create 2 file nodes jackin_inject and sw_inject_enable in the folder, this is the layout of folder on a machine with 2 sound cards: $tree $debugfs_mount_dir/sound-core sound-core/ ├── card0 │   ├── HDMI!DP,pcm=10 Jack │   │   ├── jackin_inject │   │   └── sw_inject_enable │   ├── HDMI!DP,pcm=11 Jack │   │   ├── jackin_inject │   │   └── sw_inject_enable │   ├── HDMI!DP,pcm=3 Jack │   │   ├── jackin_inject │   │   └── sw_inject_enable │   ├── HDMI!DP,pcm=7 Jack │   │   ├── jackin_inject │   │   └── sw_inject_enable │   ├── HDMI!DP,pcm=8 Jack │   │   ├── jackin_inject │   │   └── sw_inject_enable │   └── HDMI!DP,pcm=9 Jack │   ├── jackin_inject │   └── sw_inject_enable └── card1 ├── HDMI!DP,pcm=3 Jack │   ├── jackin_inject │   └── sw_inject_enable ├── HDMI!DP,pcm=4 Jack │   ├── jackin_inject │   └── sw_inject_enable ├── HDMI!DP,pcm=5 Jack │   ├── jackin_inject │   └── sw_inject_enable ├── Headphone Jack │   ├── jackin_inject │   └── sw_inject_enable ├── Headset Jack │   ├── jackin_inject │   └── sw_inject_enable └── Mic Jack ├── jackin_inject └── sw_inject_enable Suppose users want to enable jack injection for Headphone, they need to run $sudo sh -c 'echo 1 > 'Headphone Jack'/sw_inject_enable', then users could change the Headphone Jack state through jackin_inject and this Jack's state will not be changed by non-injection ways anymore until users echo 0 to sw_inject_enable. Users could run $sudo sh -c 'echo 1 > 'Headphone Jack'/jackin_inject' to trigger the Headphone jack to plugin or echo 0 to trigger it to plugout. If users finish their test, they could run $sudo sh -c 'echo 0 > 'Headphone Jack'/sw_inject_enable' to disable injection and let non-injection ways control this Jack. For the jack event report, the hw jack event will call snd_jack_report(), it will avoid to report the events if a jack_kctl's sw_inject is enabled, also the related input_dev's events will not be reproted. If users inject a event, it will call snd_jack_inject_report(), it only reports the events for this jack_kctl and reports the related input_dev's events. Signed-off-by: Hui Wang --- include/sound/core.h | 2 + sound/core/init.c | 7 ++ sound/core/jack.c | 181 ++++++++++++++++++++++++++++++++++++++++++- sound/core/sound.c | 8 ++ 4 files changed, 194 insertions(+), 4 deletions(-) diff --git a/include/sound/core.h b/include/sound/core.h index 0462c577d7a3..11d61e4248ee 100644 --- a/include/sound/core.h +++ b/include/sound/core.h @@ -122,6 +122,7 @@ struct snd_card { size_t total_pcm_alloc_bytes; /* total amount of allocated buffers */ struct mutex memory_mutex; /* protection for the above */ + struct dentry *debugfs_root; /* debugfs root for card */ #ifdef CONFIG_PM unsigned int power_state; /* power state */ @@ -180,6 +181,7 @@ static inline struct device *snd_card_get_device_link(struct snd_card *card) extern int snd_major; extern int snd_ecards_limit; extern struct class *sound_class; +extern struct dentry *sound_core_debugfs_root; void snd_request_card(int card); diff --git a/sound/core/init.c b/sound/core/init.c index 764dbe673d48..a9ceaf788019 100644 --- a/sound/core/init.c +++ b/sound/core/init.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -163,6 +164,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid, { struct snd_card *card; int err; + char name[8]; if (snd_BUG_ON(!card_ret)) return -EINVAL; @@ -246,6 +248,10 @@ int snd_card_new(struct device *parent, int idx, const char *xid, dev_err(parent, "unable to create card info\n"); goto __error_ctl; } + + sprintf(name, "card%d", idx); + card->debugfs_root = debugfs_create_dir(name, sound_core_debugfs_root); + *card_ret = card; return 0; @@ -418,6 +424,7 @@ int snd_card_disconnect(struct snd_card *card) /* notify all devices that we are disconnected */ snd_device_disconnect_all(card); + debugfs_remove(card->debugfs_root); snd_info_card_disconnect(card); if (card->registered) { device_del(&card->card_dev); diff --git a/sound/core/jack.c b/sound/core/jack.c index 503c8af79d55..0f232a806c3a 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -16,6 +18,9 @@ struct snd_jack_kctl { struct snd_kcontrol *kctl; struct list_head list; /* list of controls belong to the same jack */ unsigned int mask_bits; /* only masked status bits are reported via kctl */ + struct snd_jack *jack; /* pointer to struct snd_jack */ + bool sw_inject_enable; /* allow to inject plug event via debugfs */ + struct dentry *jack_debugfs_root; /* jack_kctl debugfs root */ }; #ifdef CONFIG_SND_JACK_INPUT_DEV @@ -109,12 +114,172 @@ static int snd_jack_dev_register(struct snd_device *device) } #endif /* CONFIG_SND_JACK_INPUT_DEV */ +#ifdef CONFIG_DEBUG_FS +static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status) +{ + struct snd_jack *jack; +#ifdef CONFIG_SND_JACK_INPUT_DEV + int i; +#endif + if (!jack_kctl) + return; + + jack = jack_kctl->jack; + + if (jack_kctl->sw_inject_enable) + snd_kctl_jack_report(jack->card, jack_kctl->kctl, + status & jack_kctl->mask_bits); + +#ifdef CONFIG_SND_JACK_INPUT_DEV + if (!jack->input_dev) + return; + + for (i = 0; i < ARRAY_SIZE(jack->key); i++) { + int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits); + + if (jack->type & testbit) + input_report_key(jack->input_dev, jack->key[i], + status & testbit); + } + + for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) { + int testbit = ((1 << i) & jack_kctl->mask_bits); + + if (jack->type & testbit) + input_report_switch(jack->input_dev, + jack_switch_types[i], + status & testbit); + } + + input_sync(jack->input_dev); +#endif /* CONFIG_SND_JACK_INPUT_DEV */ +} + +static ssize_t sw_inject_enable_read(struct file *file, + char __user *to, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int len, ret; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = scnprintf(buf, PAGE_SIZE, "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name, + "Inject Enabled", jack_kctl->sw_inject_enable); + ret = simple_read_from_buffer(to, count, ppos, buf, len); + + kfree(buf); + return ret; +} + +static ssize_t sw_inject_enable_write(struct file *file, + const char __user *from, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int ret, err; + unsigned long enable; + + buf = kzalloc(count, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret = simple_write_to_buffer(buf, count, ppos, from, count); + err = kstrtoul(buf, 0, &enable); + if (err) { + ret = err; + goto exit; + } + + jack_kctl->sw_inject_enable = !!enable; + + exit: + kfree(buf); + return ret; +} + +static ssize_t jackin_inject_write(struct file *file, + const char __user *from, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int ret, err; + unsigned long enable; + + if (!jack_kctl->sw_inject_enable) + return -EINVAL; + + buf = kzalloc(count, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret = simple_write_to_buffer(buf, count, ppos, from, count); + err = kstrtoul(buf, 0, &enable); + if (err) { + ret = err; + goto exit; + } + + snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0); + + exit: + kfree(buf); + return ret; +} + +static const struct file_operations sw_inject_enable_fops = { + .open = simple_open, + .read = sw_inject_enable_read, + .write = sw_inject_enable_write, + .llseek = default_llseek, +}; + +static const struct file_operations jackin_inject_fops = { + .open = simple_open, + .write = jackin_inject_write, + .llseek = default_llseek, +}; + +static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack, + struct snd_jack_kctl *jack_kctl) +{ + char *tname; + + /* the folder's name can't contains '/', need to replace it with '!' + * as lib/kobject.c does + */ + tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL); + if (!tname) + return -ENOMEM; + strreplace(tname, '/', '!'); + jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root); + kfree(tname); + + debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl, + &sw_inject_enable_fops); + + debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl, + &jackin_inject_fops); + + return 0; +} +#else /* CONFIG_DEBUG_FS */ +static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack, + struct snd_jack_kctl *jack_kctl) +{ + return 0; +} +#endif /* CONFIG_DEBUG_FS */ + static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl) { struct snd_jack_kctl *jack_kctl; jack_kctl = kctl->private_data; if (jack_kctl) { + debugfs_remove(jack_kctl->jack_debugfs_root); list_del(&jack_kctl->list); kfree(jack_kctl); } @@ -122,7 +287,10 @@ static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl) static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl) { + jack_kctl->jack = jack; list_add_tail(&jack_kctl->list, &jack->kctl_list); + if (!strstr(jack_kctl->kctl->id.name, "Phantom")) + snd_jack_debugfs_add_inject_node(jack, jack_kctl); } static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask) @@ -340,6 +508,7 @@ EXPORT_SYMBOL(snd_jack_set_key); void snd_jack_report(struct snd_jack *jack, int status) { struct snd_jack_kctl *jack_kctl; + unsigned int mask_bits = 0; #ifdef CONFIG_SND_JACK_INPUT_DEV int i; #endif @@ -348,15 +517,18 @@ void snd_jack_report(struct snd_jack *jack, int status) return; list_for_each_entry(jack_kctl, &jack->kctl_list, list) - snd_kctl_jack_report(jack->card, jack_kctl->kctl, - status & jack_kctl->mask_bits); + if (jack_kctl->sw_inject_enable) + mask_bits |= jack_kctl->mask_bits; + else + snd_kctl_jack_report(jack->card, jack_kctl->kctl, + status & jack_kctl->mask_bits); #ifdef CONFIG_SND_JACK_INPUT_DEV if (!jack->input_dev) return; for (i = 0; i < ARRAY_SIZE(jack->key); i++) { - int testbit = SND_JACK_BTN_0 >> i; + int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits); if (jack->type & testbit) input_report_key(jack->input_dev, jack->key[i], @@ -364,7 +536,8 @@ void snd_jack_report(struct snd_jack *jack, int status) } for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) { - int testbit = 1 << i; + int testbit = ((1 << i) & ~mask_bits); + if (jack->type & testbit) input_report_switch(jack->input_dev, jack_switch_types[i], diff --git a/sound/core/sound.c b/sound/core/sound.c index b75f78f2c4b8..3039e317df93 100644 --- a/sound/core/sound.c +++ b/sound/core/sound.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,9 @@ MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR); int snd_ecards_limit; EXPORT_SYMBOL(snd_ecards_limit); +struct dentry *sound_core_debugfs_root; +EXPORT_SYMBOL_GPL(sound_core_debugfs_root); + static struct snd_minor *snd_minors[SNDRV_OS_MINORS]; static DEFINE_MUTEX(sound_mutex); @@ -395,6 +399,9 @@ static int __init alsa_sound_init(void) unregister_chrdev(major, "alsa"); return -ENOMEM; } + + sound_core_debugfs_root = debugfs_create_dir("sound-core", NULL); + #ifndef MODULE pr_info("Advanced Linux Sound Architecture Driver Initialized.\n"); #endif @@ -403,6 +410,7 @@ static int __init alsa_sound_init(void) static void __exit alsa_sound_exit(void) { + debugfs_remove(sound_core_debugfs_root); snd_info_done(); unregister_chrdev(major, "alsa"); } From patchwork Mon Dec 28 08:00:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Wang X-Patchwork-Id: 11990907 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77D4CC433DB for ; Mon, 28 Dec 2020 08:02:19 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 81803207BC for ; Mon, 28 Dec 2020 08:02:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 81803207BC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 0BE541701; Mon, 28 Dec 2020 09:01:27 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 0BE541701 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1609142537; bh=BeN6tkAVYBBg3Gi7i3PsYukvsE0RdyyMq1RVcSGnQvQ=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Xc6Hc8772OscWU0kxUHXUefK82wTJldxTDDh6KpkBUcsMuJxiOXKnSaBFoZUopsmF thbHHS+Lpp9Gn8u44/ev5bn49xsPIUUpMSpz8265hclFphX+TjV72WRIdmPPaUllYs 3FKI3DGUsTizlXFldcA4ywlnWB9W0DYnPEn4s2YI= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id A6177F802A9; Mon, 28 Dec 2020 09:00:31 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 6A45DF80279; Mon, 28 Dec 2020 09:00:26 +0100 (CET) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 33852F80169 for ; Mon, 28 Dec 2020 09:00:20 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 33852F80169 Received: from [223.72.45.82] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ktnRy-0004UZ-N1; Mon, 28 Dec 2020 08:00:19 +0000 From: Hui Wang To: alsa-devel@alsa-project.org, tiwai@suse.de, perex@perex.cz, kai.vehmanen@linux.intel.com Subject: [RFC][PATCH v3 2/4] alsa: jack: adjust jack_kctl debugfs folder's name Date: Mon, 28 Dec 2020 16:00:01 +0800 Message-Id: <20201228080003.19127-3-hui.wang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228080003.19127-1-hui.wang@canonical.com> References: <20201228080003.19127-1-hui.wang@canonical.com> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" We used jack_kctl->kctl.id as the folder's name, but there are some characters which are not suitable for foler's name, for example, a HDMI/DP audio jack id contains '/', ',', '=' and ' ', this patch will remove them from folder's name. Before applying patch, the folders look like: 'HDMI!DP,pcm=3 Jack' 'Headphone Jack' 'Mic Jack' After applying the patch, the folders look like: HDMIDPpcm3Jack HeadphoneJack MicJack Signed-off-by: Hui Wang --- sound/core/jack.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/sound/core/jack.c b/sound/core/jack.c index 0f232a806c3a..62e9215fa0f0 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -242,18 +242,36 @@ static const struct file_operations jackin_inject_fops = { .llseek = default_llseek, }; +/* The substrings in the jack's name but not suitable for folder's name */ +static const char * const dropped_chars[] = { + "/", "=", ",", " ", +}; + +static char *strremove(char *s, const char *c) +{ + char *p; + + while ((p = strstr(s, c))) { + *p = '\0'; + strcat(s, p+strlen(c)); + } + + return s; +} + static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl) { char *tname; + int i; - /* the folder's name can't contains '/', need to replace it with '!' - * as lib/kobject.c does - */ tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL); if (!tname) return -ENOMEM; - strreplace(tname, '/', '!'); + + for (i = 0; i < ARRAY_SIZE(dropped_chars); i++) + tname = strremove(tname, dropped_chars[i]); + jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root); kfree(tname); From patchwork Mon Dec 28 08:00:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Wang X-Patchwork-Id: 11990909 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BC04C433DB for ; Mon, 28 Dec 2020 08:03:01 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 99870207B6 for ; Mon, 28 Dec 2020 08:03:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 99870207B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 323F51703; Mon, 28 Dec 2020 09:02:09 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 323F51703 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1609142579; bh=Sqhj94N61rb1g5cfu545mBKMIsrX/ya3kdQRGLhKcbs=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=OhS4Dgx2YFyL5mljEteRZJi+hcnj0N9c0UM6P6V7r0uWNclc/H2bIW0nRWKRt/DRF SsPmi9oStkBl7FQTMpNG8SHvmCLbCD1XTaWqWmCmoW6+OkB2zTTmMlomTL6VY7k6pA 6YGX4SvS9sc2674gNDzpi/G7fFRB+Ki8YQJQfsZ4= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 9534DF804C1; Mon, 28 Dec 2020 09:00:33 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 7FD50F804C1; Mon, 28 Dec 2020 09:00:29 +0100 (CET) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 3E600F801ED for ; Mon, 28 Dec 2020 09:00:23 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 3E600F801ED Received: from [223.72.45.82] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ktnS1-0004UZ-W2; Mon, 28 Dec 2020 08:00:22 +0000 From: Hui Wang To: alsa-devel@alsa-project.org, tiwai@suse.de, perex@perex.cz, kai.vehmanen@linux.intel.com Subject: [RFC][PATCH v3 3/4] alsa: jack: add more jack_kctl debugfs nodes Date: Mon, 28 Dec 2020 16:00:02 +0800 Message-Id: <20201228080003.19127-4-hui.wang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228080003.19127-1-hui.wang@canonical.com> References: <20201228080003.19127-1-hui.wang@canonical.com> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" Adding 4 more debugfs nodes, users could get more information about the jack_kctl from them: - kctl_id, read-only, get jack_kctl->kctl's id sound-core/card0/HeadphoneJack# cat kctl_id Headphone Jack - mask_bits, read-only, get jack_kctl's events mask_bits sound-core/card0/HeadphoneJack# cat mask_bits 0x0001 HEADPHONE(0x0001) - status, read-only, get jack_kctl's current status headphone unplugged: sound-core/card0/HeadphoneJack# cat status 0x0000 headphone plugged: sound-core/card0/HeadphoneJack# cat status 0x0001 HEADPHONE(0x0001) - type, read-only, get jack's supported events type sound-core/card0/HeadphoneJack# cat type 0x0001 HEADPHONE(0x0001) Signed-off-by: Hui Wang --- sound/core/jack.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/sound/core/jack.c b/sound/core/jack.c index 62e9215fa0f0..31c80883db2c 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -229,6 +229,119 @@ static ssize_t jackin_inject_write(struct file *file, return ret; } +static ssize_t jack_kctl_id_read(struct file *file, + char __user *to, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int len, ret; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = scnprintf(buf, PAGE_SIZE, "%s\n", jack_kctl->kctl->id.name); + ret = simple_read_from_buffer(to, count, ppos, buf, len); + + kfree(buf); + return ret; +} + +/* the bit definition is aligned with snd_jack_types in jack.h */ +static const char * const jack_events_name[] = { + "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)", + "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)", + "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)", + "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "", +}; + +static int parse_mask_bits(unsigned int mask_bits, char *s) +{ + char *buf; + int len, i; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = scnprintf(buf, PAGE_SIZE, "0x%04x", mask_bits); + + for (i = 0; i < 16; i++) + if (mask_bits & (1 << i)) + len += scnprintf(buf + strlen(buf), PAGE_SIZE - strlen(buf), + " %s", jack_events_name[i]); + + len += scnprintf(buf + strlen(buf), PAGE_SIZE - strlen(buf), "\n"); + + strcpy(s, buf); + + kfree(buf); + + return len; +} + +static ssize_t jack_kctl_mask_bits_read(struct file *file, + char __user *to, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int len, ret; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = parse_mask_bits(jack_kctl->mask_bits, buf); + ret = simple_read_from_buffer(to, count, ppos, buf, len); + + kfree(buf); + return ret; +} + +static ssize_t jack_kctl_status_read(struct file *file, + char __user *to, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int len, ret; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = parse_mask_bits(jack_kctl->kctl->private_value, buf); + ret = simple_read_from_buffer(to, count, ppos, buf, len); + + kfree(buf); + return ret; +} + +#ifdef CONFIG_SND_JACK_INPUT_DEV +static ssize_t jack_type_read(struct file *file, + char __user *to, size_t count, loff_t *ppos) +{ + struct snd_jack_kctl *jack_kctl = file->private_data; + char *buf; + int len, ret; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + len = parse_mask_bits(jack_kctl->jack->type, buf); + ret = simple_read_from_buffer(to, count, ppos, buf, len); + + kfree(buf); + return ret; +} + +static const struct file_operations jack_type_fops = { + .open = simple_open, + .read = jack_type_read, + .llseek = default_llseek, +}; +#endif + static const struct file_operations sw_inject_enable_fops = { .open = simple_open, .read = sw_inject_enable_read, @@ -242,6 +355,24 @@ static const struct file_operations jackin_inject_fops = { .llseek = default_llseek, }; +static const struct file_operations jack_kctl_id_fops = { + .open = simple_open, + .read = jack_kctl_id_read, + .llseek = default_llseek, +}; + +static const struct file_operations jack_kctl_mask_bits_fops = { + .open = simple_open, + .read = jack_kctl_mask_bits_read, + .llseek = default_llseek, +}; + +static const struct file_operations jack_kctl_status_fops = { + .open = simple_open, + .read = jack_kctl_status_read, + .llseek = default_llseek, +}; + /* The substrings in the jack's name but not suitable for folder's name */ static const char * const dropped_chars[] = { "/", "=", ",", " ", @@ -281,6 +412,19 @@ static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack, debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl, &jackin_inject_fops); + debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl, + &jack_kctl_id_fops); + + debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl, + &jack_kctl_mask_bits_fops); + + debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl, + &jack_kctl_status_fops); + +#ifdef CONFIG_SND_JACK_INPUT_DEV + debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl, + &jack_type_fops); +#endif return 0; } #else /* CONFIG_DEBUG_FS */ From patchwork Mon Dec 28 08:00:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hui Wang X-Patchwork-Id: 11990911 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF35CC433E0 for ; Mon, 28 Dec 2020 08:03:06 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 48C202242A for ; Mon, 28 Dec 2020 08:03:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 48C202242A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id CAFB91725; Mon, 28 Dec 2020 09:02:14 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz CAFB91725 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1609142584; bh=J3AKSXXQZPJEa2bAgKUXx0tOF3VNvxAV8kA6Za15L4s=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=fypgktF/MP8DcuAp0XRWUkNTg4uq3gCyutV4zL/NrobM3Ww+ar7Wkfs6UvsMkNZ3E 3LsppKBKaRX43F7hFQlXQ3y5KQN11PQyAwnEK+axYNX30fZHj+iUpQoUyqZYW45slI hQFY4/PCarEbfw4O8gYhzDdBm7MxqbP1L0EvAXho= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 7A2D5F804D2; Mon, 28 Dec 2020 09:00:35 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id A78AEF802A9; Mon, 28 Dec 2020 09:00:29 +0100 (CET) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 92BC5F804B0 for ; Mon, 28 Dec 2020 09:00:26 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 92BC5F804B0 Received: from [223.72.45.82] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1ktnS5-0004UZ-8a; Mon, 28 Dec 2020 08:00:26 +0000 From: Hui Wang To: alsa-devel@alsa-project.org, tiwai@suse.de, perex@perex.cz, kai.vehmanen@linux.intel.com Subject: [RFC][PATCH v3 4/4] alsa: jack: implement save-and-restore for jack's hw status Date: Mon, 28 Dec 2020 16:00:03 +0800 Message-Id: <20201228080003.19127-5-hui.wang@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228080003.19127-1-hui.wang@canonical.com> References: <20201228080003.19127-1-hui.wang@canonical.com> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" Once we enable the sw_inject for a jack_kctl, the hw status change will be blocked, but the hw status still could be reported to snd_jack_report() and be saved to hw_status_cache. After the sw_inject is disabled, we use the last saved hw_status_cache to restore jack's status. Signed-off-by: Hui Wang --- include/sound/jack.h | 1 + sound/core/jack.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/sound/jack.h b/include/sound/jack.h index 9eb2b5ec1ec4..1181f536557e 100644 --- a/include/sound/jack.h +++ b/include/sound/jack.h @@ -67,6 +67,7 @@ struct snd_jack { char name[100]; unsigned int key[6]; /* Keep in sync with definitions above */ #endif /* CONFIG_SND_JACK_INPUT_DEV */ + int hw_status_cache; void *private_data; void (*private_free)(struct snd_jack *); }; diff --git a/sound/core/jack.c b/sound/core/jack.c index 31c80883db2c..2c7fef94823d 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -193,8 +193,14 @@ static ssize_t sw_inject_enable_write(struct file *file, goto exit; } + if (jack_kctl->sw_inject_enable == (!!enable)) + goto exit; + jack_kctl->sw_inject_enable = !!enable; + if (!jack_kctl->sw_inject_enable) + snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache); + exit: kfree(buf); return ret; @@ -678,6 +684,8 @@ void snd_jack_report(struct snd_jack *jack, int status) if (!jack) return; + jack->hw_status_cache = status; + list_for_each_entry(jack_kctl, &jack->kctl_list, list) if (jack_kctl->sw_inject_enable) mask_bits |= jack_kctl->mask_bits;