Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp7692ybj; Mon, 4 May 2020 15:00:31 -0700 (PDT) X-Google-Smtp-Source: APiQypKDalHhOzDplJqMOQzehxcn2bRT26aoIOUGyv3A4TskwKfaDvoOnU2SVNLdaTATlmUsJNqg X-Received: by 2002:a05:6402:2d5:: with SMTP id b21mr93908edx.291.1588629631288; Mon, 04 May 2020 15:00:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588629631; cv=none; d=google.com; s=arc-20160816; b=Ujjpwqe/H0Wc12A4lT6RKA53XFmXc2yTvLLia6h28KQpGMQywJEWCmk74wbIPH50GW daD/ZaPtPhjDUAZqZL+Hsme1bOTdmMX4xuIrrsdiY7e6qHK495d+zfJ5fHGSOBBV7TYa dEagRLsfimZgw249IsmI3TxpdtoHUXSLw8Sq8LM6bUol8cLfQOEgjOBWYbBg1o97HiOd Owk9EM0EDH8A2qiV+Efv+8PY/oaoNQSOrUYnT0KCXkps5YfFezgUq2cZp8dtbFaJwLvv +teaeZruZa6+3afqJIKnqo4Z/gRtiBfpelR0TEj4+EQL4Wzf6xuajqMAhvvJAfQVhLZ0 yH0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=FZIkYuBAw0Vt07t90y7SmRompH2guq1hUwxisleO2Sg=; b=QdrCok576eLymvBy6Ta+bVtUWDAR1dAhHEcUBh3RcXEIzteTGo7OVn647f5I3c8LfA 7LEfiiIFI1HsBnfLmzpw1KuF0B1/vFPVMemgxdNEdAsBKeK1oqzGhkk/1bEqq3Nh1nGg UhBjnTM0VClC7bisq332ceJ9DOYCG0bn22jR9smNHrzdZz+aO8PPtFLZJH3JUvPFJIx2 7xl4VECgMDQzXphGzuWt1jeCkch2WnzHH0mv3MroQrNwONkZ3zGCCPaIJ+DuBc1yXGrr +JY8FVHhYFSQDSP+tgmXE5HShgykuJf4cSWjWeV2ApAiBnbuEh96fZMITL6h9qF6GIqb 2fRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JAHqLHf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p90si208924edd.215.2020.05.04.15.00.08; Mon, 04 May 2020 15:00:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JAHqLHf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728073AbgEDV6h (ORCPT + 99 others); Mon, 4 May 2020 17:58:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727855AbgEDV6e (ORCPT ); Mon, 4 May 2020 17:58:34 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 735FAC061A0E for ; Mon, 4 May 2020 14:58:34 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id mq3so87934pjb.1 for ; Mon, 04 May 2020 14:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FZIkYuBAw0Vt07t90y7SmRompH2guq1hUwxisleO2Sg=; b=JAHqLHf44k7Azt/vctQh14vA8d8qWKs+Q7+eY4ITBPmLi2zzRctJzqH4RhTp+L7ZFT Dv2fNBLPXMZZNELH2qiRhGnS7s4dl++bGWtYmcHDxzupygBOrrGlM6h5JD1pd0Z0/9zi rwJWumSRVRRyD9YkeYfrXLkzpj2xOogkNtOvPY+/h0O2XKE6Werc4Lv+BCpQNjiesu0t 47AOQJR3NcuV3k3f0U8/N8eFKsSoDjpEqg9k/UI14n+P6LkO38waw5ISyug2hrDjeImP xAXAyHmW3glwn5OaDiZLBWT4ahxbl70KCCHOLX92N2A0pZ0mYOgyt0s6LD23ifr/gNTP RDvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FZIkYuBAw0Vt07t90y7SmRompH2guq1hUwxisleO2Sg=; b=uLVnzAEw24C/ccatNJT3SNHsuFtmbIPWiq6oD+jwU4mkVwXcbGgFIeUn2sPTm/+28X HFtC5Zu6cOlEHKZ5Dd8v1wnFDpTaMGyiTStPvYHcXI1lFB8jBx9RbqFts/3wOjDsdgd4 pw7Zv3BgJ66mi8tPp4J0o6ITrfLH9RZI4G0+tuxeLDn8fHWnMfHahA+CqQHHqo75fYIh T/SOKHPjvGtYzcWr0iQrrYKd2G615omBaVyRPHxueOShYLAbUvVBrDRR9gpXBif7tHds DtCbhBrY60roMcsXuIT+FbCljAHEUrgRlWzaMyKNmr22Po1VXF9ZNj0PdwDDBZ+avumT Zozw== X-Gm-Message-State: AGi0PuZ8ZBB49lhROdPXZBkfH0VHlUdaOVV7vFPSdkCXS/x4nMiuH5XZ YzJMXf1xfzKF5FW6NgLIRnWBNQ== X-Received: by 2002:a17:902:59cc:: with SMTP id d12mr150100plj.237.1588629513927; Mon, 04 May 2020 14:58:33 -0700 (PDT) Received: from xps15.cg.shawcable.net (S0106002369de4dac.cg.shawcable.net. [68.147.8.254]) by smtp.gmail.com with ESMTPSA id g10sm60185pfk.103.2020.05.04.14.58.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 May 2020 14:58:33 -0700 (PDT) From: Mathieu Poirier To: ohad@wizery.com, bjorn.andersson@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/2] rpmsg: core: Add support to retrieve name extension Date: Mon, 4 May 2020 15:58:30 -0600 Message-Id: <20200504215830.31394-3-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200504215830.31394-1-mathieu.poirier@linaro.org> References: <20200504215830.31394-1-mathieu.poirier@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After adding support for rpmsg device name extension, this patch provides a function that returns the extension portion of an rpmsg device name. That way users of the name extension functionality don't have to write the same boiler plate code to extract the information. Suggested-by: Suman Anna ; Signed-off-by: Mathieu Poirier --- drivers/rpmsg/rpmsg_core.c | 92 ++++++++++++++++++++++++++++++++++++++ include/linux/rpmsg.h | 13 ++++++ 2 files changed, 105 insertions(+) diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c index 5e01e8dede6b..dae87c0cb73d 100644 --- a/drivers/rpmsg/rpmsg_core.c +++ b/drivers/rpmsg/rpmsg_core.c @@ -439,6 +439,98 @@ static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) return of_driver_match_device(dev, drv); } +/** + * rpmsg_device_get_name_extension() - get the name extension of a rpmsg device + * @rpdev: the rpmsg device to work with + * @skip: how many characters in the extension should be skipped over + * + * With function rpmsg_id_match() allowing for extension of the base driver name + * in order to differentiate services, this function returns the extension part + * of an rpmsg device name. As such and with the following rpmsg driver device + * id table and rpmsg device names: + * + * static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = { + * { .name = "rpmsg-client-sample" }, + * { }, + * } + * + * rpdev1->id.name == "rpmsg-client-sample"; + * rpdev2->id.name == "rpmsg-client-sample_instance0"; + * + * Calling rpmsg_device_get_name_extension() will yields the following: + * + * rpmsg_device_get_name_extension(rpdev1, 0) == NULL; + * rpmsg_device_get_name_extension(rpdev2, 0) == "_instance0"; + * rpmsg_device_get_name_extension(rpdev2, 1) == "instance0"; + * + * + * Note: The name extension should be free'd using kfree_const(). + * + * Return: the name extension if found, NULL if not found and an error + * code otherwise. + */ +const char *rpmsg_device_get_name_extension(struct rpmsg_device *rpdev, + unsigned int skip) +{ + const char *drv_name, *dev_name, *extension; + const struct rpmsg_device_id *ids; + struct device *dev = &rpdev->dev; + struct rpmsg_driver *rpdrv; + bool match = false; + unsigned int i; + + if (!dev->driver) + return ERR_PTR(-EINVAL); + + rpdrv = to_rpmsg_driver(dev->driver); + + /* + * No point in going further if the device and the driver don't + * have a name or a table to work with. + */ + if (!rpdev->id.name[0] || !rpdrv->id_table) + return ERR_PTR(-EINVAL); + + ids = rpdrv->id_table; + dev_name = rpdev->id.name; + + /* + * See if any name in the driver's table match the beginning + * of the rpmsg device's name. + */ + for (i = 0; ids[i].name[0]; i++) { + drv_name = ids[i].name; + if (strncmp(drv_name, + dev_name, strlen(drv_name)) == 0) { + match = true; + break; + } + } + + if (!match) + return NULL; + + /* No name extension to return if device and driver are the same */ + if (strlen(dev_name) == strlen(drv_name)) + return NULL; + + /* + * Make sure we were not requested to skip past the end + * of the device name. + */ + if (strlen(drv_name) + skip >= strlen(dev_name)) + return ERR_PTR(-EINVAL); + + /* + * Move past the base name published by the driver and + * skip any extra characters if needed. + */ + extension = dev_name + strlen(drv_name) + skip; + + return kstrdup_const(extension, GFP_KERNEL); +} +EXPORT_SYMBOL(rpmsg_device_get_name_extension); + static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env) { struct rpmsg_device *rpdev = to_rpmsg_device(dev); diff --git a/include/linux/rpmsg.h b/include/linux/rpmsg.h index 9fe156d1c018..9537b95ad30a 100644 --- a/include/linux/rpmsg.h +++ b/include/linux/rpmsg.h @@ -135,6 +135,9 @@ int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp, poll_table *wait); +const char *rpmsg_device_get_name_extension(struct rpmsg_device *dev, + unsigned int skip); + #else static inline int register_rpmsg_device(struct rpmsg_device *dev) @@ -242,6 +245,16 @@ static inline __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, return 0; } +static inline +const char *rpmsg_device_get_name_extension(struct rpmsg_device *dev, + unsigned int skip) +{ + /* This shouldn't be possible */ + WARN_ON(1); + + return NULL; +} + #endif /* IS_ENABLED(CONFIG_RPMSG) */ /* use a macro to avoid include chaining to get THIS_MODULE */ -- 2.20.1