From mboxrd@z Thu Jan 1 00:00:00 1970 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=-10.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 8F12CC433E0 for ; Sun, 24 Jan 2021 08:31:08 +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 DDC0A22CF6 for ; Sun, 24 Jan 2021 08:31:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DDC0A22CF6 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 75AA118F9; Sun, 24 Jan 2021 09:30:14 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 75AA118F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1611477064; bh=t8naVe6Qi+rZyDRaan0+UYv5Ek5CpqfW5VxueR5ChR8=; h=Subject:To:References:From:Date:In-Reply-To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=eRcjJ2eVd17Kvo3bYf5WwaeFVVPDKu8Pj4ZQT4snKi94beKMApZC1+WmXGYeQebqF 2La6evBhOkpiJUyy1IUFUxLjExgXjzvMrIp05bRrYqkLGYN4KqcH0SxqgohYNRyHAW qdn8WSU+VanFCwtl+o7+2C3bte2Na0UFUQwIw9NU= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id C455DF80114; Sun, 24 Jan 2021 09:30:11 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id ACC1CF8015B; Sun, 24 Jan 2021 09:29: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 D87FBF80114 for ; Sun, 24 Jan 2021 09:29:20 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz D87FBF80114 Received: from [123.112.70.0] (helo=[192.168.0.106]) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l3al4-0003a1-Fx; Sun, 24 Jan 2021 08:28:41 +0000 Subject: Re: [RFC][PATCH v6 1/1] alsa: jack: implement software jack injection via debugfs To: Takashi Iwai References: <20210122141456.12460-1-hui.wang@canonical.com> <20210122141456.12460-2-hui.wang@canonical.com> From: Hui Wang Message-ID: <2b8088cb-37e3-c8c3-7371-1d320eabddf1@canonical.com> Date: Sun, 24 Jan 2021 16:27:55 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Cc: alsa-devel@alsa-project.org, kai.vehmanen@linux.intel.com 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" On 1/22/21 11:13 PM, Takashi Iwai wrote: > On Fri, 22 Jan 2021 15:14:56 +0100, > Hui Wang wrote: >> --- /dev/null >> +++ b/Documentation/sound/designs/jack-injection.rst >> + sound/card1/Headphone_Jack# echo 1 > jackin_inject >> + to inject plugout: >> + sound/card1/Headphone_Jack# echo 0 > jackin_inject > The lists could be better in a normal format, while only the examples > with cat and echo should be in verbose format. Will fix it in v7. >> diff --git a/sound/core/Kconfig b/sound/core/Kconfig >> index d4554f376160..a9189f58dc56 100644 >> --- a/sound/core/Kconfig >> +++ b/sound/core/Kconfig >> @@ -38,6 +38,15 @@ config SND_JACK_INPUT_DEV >> depends on SND_JACK >> default y if INPUT=y || INPUT=SND >> >> +config SND_JACK_INJECTION_DEBUG >> + bool "Sound jack injection interface via debugfs" >> + depends on SND_JACK && DEBUG_FS > Also, could depend on SND_DEBUG for consistency. OK, will add this dependence. > >> diff --git a/sound/core/init.c b/sound/core/init.c >> index 75aec71c48a8..e7f7cfe1143b 100644 >> --- a/sound/core/init.c >> +++ b/sound/core/init.c >> @@ -13,6 +13,7 @@ >> #include >> #include >> #include >> +#include >> #include >> >> #include >> @@ -161,6 +162,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; >> @@ -244,6 +246,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_debugfs_root); > It's still an open question whether we want to create the debugfs > always. But I guess it's OK, we might want to add more stuff to > debugfs later. Or, it makes sense to create only if > CONFIG_SND_DEBUG=y. Will add "#ifdef CONFIG_SND_DEBUG" to conditionally create debugfs_mount_dir/sound and debugfs_mount_dir/sound/cardN > >> +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; >> + int ret, err; >> + unsigned long enable; >> + char buf[8] = { 0 }; >> + >> + if (count >= 8) >> + return -EINVAL; >> + >> + ret = simple_write_to_buffer(buf, count, ppos, from, count); > The simple_write_to_buffer() doesn't terminate the string by itself, > hence you need to make sure the string termination before kstrtoul() > call. e.g. buf[sizeof(buf)-1] = 0; > > And maybe it's easier to make a helper function to that, since it's > called in multiple places. > OK, I will change it as below: char buf[8] = { 0 }; ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count); >> +static int parse_mask_bits(unsigned int mask_bits, char *s) >> +{ >> + char buf[256]; >> + int len, i; >> + >> + len = scnprintf(buf, sizeof(buf), "0x%04x", mask_bits); >> + >> + for (i = 0; i < 16; i++) >> + if (mask_bits & (1 << i)) >> + len += scnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), >> + " %s", jack_events_name[i]); >> + >> + len += scnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n"); >> + >> + strcpy(s, buf); > You need to intermediate buffer if you do a full copy here... > Just perform the string ops on s with a certain limit. > Also, you can use strncat() or strlcat() for simplicity. I will drop intermediate buffer and don't use strcpy() here, and use strlcat to replace scnprintf(), the changes like below: /* the recommended the buffer size is 256 */ static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size) {     int i;     scnprintf(buf, buf_size, "0x%04x", mask_bits);     for (i = 0; i < 16; i++)         if (mask_bits & (1 << i)) {             strlcat(buf, " ", buf_size);             strlcat(buf, jack_events_name[i], buf_size);         }     strlcat(buf, "\n", buf_size);     return strlen(buf); } Thanks, Hui. > > thanks, > > Takashi