From patchwork Wed Apr 13 10:53:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hsin-Yu Chao X-Patchwork-Id: 8820571 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7B78FC0553 for ; Wed, 13 Apr 2016 10:53:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7221A20373 for ; Wed, 13 Apr 2016 10:53:34 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id E53402035D for ; Wed, 13 Apr 2016 10:53:32 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 9788B2608CF; Wed, 13 Apr 2016 12:53:31 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_NONE, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=no version=3.3.1 Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 4F2662608CF; Wed, 13 Apr 2016 12:53:22 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id D627326167A; Wed, 13 Apr 2016 12:53:21 +0200 (CEST) Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com [209.85.220.47]) by alsa0.perex.cz (Postfix) with ESMTP id 9890C2608CF for ; Wed, 13 Apr 2016 12:53:16 +0200 (CEST) Received: by mail-pa0-f47.google.com with SMTP id er2so3048634pad.3 for ; Wed, 13 Apr 2016 03:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=l2BFRxmWZGOBfMCHxw06PhVcH8opwMFSkYkZz2oU/o4=; b=E9Ta32lK6e5gslYl7zJW+xiMzEb2KA/8PLY1jy+UedaR8KJ8C57/GnYUj0Zqsf+i35 6lJtUQ4XwTmEJsV8C/yxWrfD60+KAem8cH41TxMg7B9q57zgA4UIqmkNq+3pM6SYfiuD agSACy0ITDNGToIuXe78cMMIjFWgrywxCLdPI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=l2BFRxmWZGOBfMCHxw06PhVcH8opwMFSkYkZz2oU/o4=; b=QgO+40ijK67eqgchWrVMCmo8e/D15Y7+4/8080fCKP9ux1nWDu18ZH8X6mwsVxeUxm V6/zdBGu8u54PWuharLyq8jpMl1ocPvk0Al2DWuswcsJuuXJtT9PzzpL0hjo44CKqD8J Z4st2+imEhTJWypf56i9bftG04BruFti5HBTmt8J0BqGKIN43b1yhJfiorJljvZx9O9C r1EK2l6Uc8rAZVlgIUv5nBTY7AKNjSnTcogHYPNMCRNhbdJhtLwy+doZ/pFDVJPY57+G vdGGBm1Y8MOoT5GuvvgAtPGrNxu96SLMVjybh5SnxiDZJmd3ThEgvQU6HrZ/49waW/HT SQUg== X-Gm-Message-State: AOPr4FVl4dZDyHKtkxlYeeO5BtjT7oQWvg8AfJ/kLKQ0b5f8LCzvT9BOrifo0O09PyIEsg== X-Received: by 10.66.166.78 with SMTP id ze14mr11859320pab.149.1460544795471; Wed, 13 Apr 2016 03:53:15 -0700 (PDT) Received: from hychao-z620.tpe.corp.google.com ([172.30.210.187]) by smtp.gmail.com with ESMTPSA id b11sm50147811pfj.4.2016.04.13.03.53.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Apr 2016 03:53:14 -0700 (PDT) From: Hsin-Yu Chao To: alsa-devel@alsa-project.org Date: Wed, 13 Apr 2016 18:53:09 +0800 Message-Id: <1460544789-29169-1-git-send-email-hychao@chromium.org> X-Mailer: git-send-email 2.1.2 Cc: vinod.koul@intel.com, dgreid@chromium.org, Hsin-Yu Chao Subject: [alsa-devel] [PATCH v5] ucm: add cset-tlv X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 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: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP This patch enables UCM to set a file in TLV format to kcontrol by: cset-tlv "name='' " This new 'cset-tlv' command will be used to write audio DSP to specific alsa control, where the driver expectes a file in TLV format. The TLV file to set to kcontrol will be checked first by file size not larger than 16 MB, and then examine if the length field reports correct number of bytes in the TLV file. Signed-off-by: Hsin-Yu Chao Reviewed-by: Vinod Koul --- src/ucm/main.c | 94 +++++++++++++++++++++++++++++++++++++++++++++-------- src/ucm/parser.c | 10 ++++++ src/ucm/ucm_local.h | 1 + 3 files changed, 92 insertions(+), 13 deletions(-) diff --git a/src/ucm/main.c b/src/ucm/main.c index 7e44603..24d9510 100644 --- a/src/ucm/main.c +++ b/src/ucm/main.c @@ -161,6 +161,57 @@ static int open_ctl(snd_use_case_mgr_t *uc_mgr, return 0; } +static int read_tlv_file(unsigned int **res, + const char *filepath) +{ + int err = 0; + int fd; + struct stat st; + size_t sz; + ssize_t sz_read; + struct snd_ctl_tlv *tlv; + + fd = open(filepath, O_RDONLY); + if (fd < 0) { + err = -errno; + return err; + } + if (fstat(fd, &st) == -1) { + err = -errno; + goto __fail; + } + sz = st.st_size; + if (sz > 16 * 1024 * 1024 || sz < 8 || sz % 4) { + uc_error("File size should be less than 16 MB " + "and multiple of 4"); + err = -EINVAL; + goto __fail; + } + *res = malloc(sz); + if (res == NULL) { + err = -ENOMEM; + goto __fail; + } + sz_read = read(fd, *res, sz); + if (sz_read < 0 || (size_t)sz_read != sz) { + err = -EIO; + free(*res); + *res = NULL; + } + /* Check if the tlv file specifies valid size. */ + tlv = (struct snd_ctl_tlv *)(*res); + if (tlv->length + 2 * sizeof(unsigned int) != sz) { + uc_error("Invalid tlv size: %d", tlv->length); + err = -EINVAL; + free(*res); + *res = NULL; + } + +__fail: + close(fd); + return err; +} + static int binary_file_parse(snd_ctl_elem_value_t *dst, snd_ctl_elem_info_t *info, const char *filepath) @@ -226,6 +277,7 @@ static int execute_cset(snd_ctl_t *ctl, const char *cset, unsigned int type) snd_ctl_elem_id_t *id; snd_ctl_elem_value_t *value; snd_ctl_elem_info_t *info; + unsigned int *res = NULL; snd_ctl_elem_id_malloc(&id); snd_ctl_elem_value_malloc(&value); @@ -241,23 +293,36 @@ static int execute_cset(snd_ctl_t *ctl, const char *cset, unsigned int type) err = -EINVAL; goto __fail; } - snd_ctl_elem_value_set_id(value, id); snd_ctl_elem_info_set_id(info, id); - err = snd_ctl_elem_read(ctl, value); - if (err < 0) - goto __fail; err = snd_ctl_elem_info(ctl, info); if (err < 0) goto __fail; - if (type == SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE) - err = binary_file_parse(value, info, pos); - else - err = snd_ctl_ascii_value_parse(ctl, value, info, pos); - if (err < 0) - goto __fail; - err = snd_ctl_elem_write(ctl, value); - if (err < 0) - goto __fail; + if (type == SEQUENCE_ELEMENT_TYPE_CSET_TLV) { + if (!snd_ctl_elem_info_is_tlv_writable(info)) { + err = -EINVAL; + goto __fail; + } + err = read_tlv_file(&res, pos); + if (err < 0) + goto __fail; + err = snd_ctl_elem_tlv_write(ctl, id, res); + if (err < 0) + goto __fail; + } else { + snd_ctl_elem_value_set_id(value, id); + err = snd_ctl_elem_read(ctl, value); + if (err < 0) + goto __fail; + if (type == SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE) + err = binary_file_parse(value, info, pos); + else + err = snd_ctl_ascii_value_parse(ctl, value, info, pos); + if (err < 0) + goto __fail; + err = snd_ctl_elem_write(ctl, value); + if (err < 0) + goto __fail; + } err = 0; __fail: if (id != NULL) @@ -266,6 +331,8 @@ static int execute_cset(snd_ctl_t *ctl, const char *cset, unsigned int type) free(value); if (info != NULL) free(info); + if (res != NULL) + free(res); return err; } @@ -298,6 +365,7 @@ static int execute_sequence(snd_use_case_mgr_t *uc_mgr, break; case SEQUENCE_ELEMENT_TYPE_CSET: case SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE: + case SEQUENCE_ELEMENT_TYPE_CSET_TLV: if (cdev == NULL) { char *playback_ctl = NULL; char *capture_ctl = NULL; diff --git a/src/ucm/parser.c b/src/ucm/parser.c index 9e1cb41..d781e1b 100644 --- a/src/ucm/parser.c +++ b/src/ucm/parser.c @@ -316,6 +316,16 @@ static int parse_sequence(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED, continue; } + if (strcmp(cmd, "cset-tlv") == 0) { + curr->type = SEQUENCE_ELEMENT_TYPE_CSET_TLV; + err = parse_string(n, &curr->data.cset); + if (err < 0) { + uc_error("error: cset-tlv requires a string!"); + return err; + } + continue; + } + if (strcmp(cmd, "usleep") == 0) { curr->type = SEQUENCE_ELEMENT_TYPE_SLEEP; err = snd_config_get_integer(n, &curr->data.sleep); diff --git a/src/ucm/ucm_local.h b/src/ucm/ucm_local.h index 3a5d2c2..b89de2a 100644 --- a/src/ucm/ucm_local.h +++ b/src/ucm/ucm_local.h @@ -48,6 +48,7 @@ #define SEQUENCE_ELEMENT_TYPE_SLEEP 3 #define SEQUENCE_ELEMENT_TYPE_EXEC 4 #define SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE 5 +#define SEQUENCE_ELEMENT_TYPE_CSET_TLV 6 struct ucm_value { struct list_head list;