From patchwork Fri Dec 2 22:47:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 13063304 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48F0AC47088 for ; Fri, 2 Dec 2022 22:48:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234770AbiLBWsC (ORCPT ); Fri, 2 Dec 2022 17:48:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233308AbiLBWsB (ORCPT ); Fri, 2 Dec 2022 17:48:01 -0500 Received: from mail-io1-xd49.google.com (mail-io1-xd49.google.com [IPv6:2607:f8b0:4864:20::d49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6005F7BC01 for ; Fri, 2 Dec 2022 14:47:59 -0800 (PST) Received: by mail-io1-xd49.google.com with SMTP id k21-20020a5e8915000000b006de391b332fso5672835ioj.4 for ; Fri, 02 Dec 2022 14:47:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kr2FFYjrlmKQpl0FW9cRYWBpZCQQ4WxEePYVPH7PqT0=; b=aig6Am3XKh1f74fIoP78XmyYoMV06xOmH0SRrk9TCuJ30vcxCB6RIZVxlkWc4IW82q 2uIqiG3js6ixvzMtrv2w3qAX1gMUNHU9Jpd+qiPXSNFHKbehWNF7Yhw2hnsYzBVHiz+U /xJtJBId/qyG3lUq5Gebw3wMCtj97OgR8Iuh+NX2oUJTao+RSD97NbqgA6jJbdPMvs0d v0DCQHtlp3kq5Ddfnf3WHfDLXQoDLyzfX18K4SfCv7rdPhWhlGu0YOEWfi4OkK0rmIKe Vz8L0l0NKQWQ8QpriqE46JttBua6QTtUfOf6j36qUTSKJGD76Wfrp8mqX71XIjDSw2Ur J52w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kr2FFYjrlmKQpl0FW9cRYWBpZCQQ4WxEePYVPH7PqT0=; b=FkEKQ/0Jvcg3fuD97sgnG+s1HGhwjS3pCe2r0d0YLXJ+QXVRAzAbbvbMxIIvQZiwlM egG/Za9VXunblTaj8/jcf3spnSTpYos4je4zsD9thNh0HiffmREbul5AU6/XH1dqkSs7 q50/4EU5df2PA+XX8hDn3a5hSA9sStRaEUaUfuJk5tH1AJUMKByEnFVRS9wTJbpUdSan WOh089TnykW3gG9M0S9Nv9/B0QYj0mOJuC+DW3wnBbxqf6KDLTivAjXgY4BSLJ+CHHEJ IRZccZYwqNqHGJ9Onb57vSLvOuVuBWFPxyCiuvMJLvy1LW6uKwinb3S+q7/3Q58Mu6ht NSig== X-Gm-Message-State: ANoB5pmQHvHMaysTpe28whRTv9hKb6mzPho8JLv1crY4OiO4Pq/t2yAC 5pYaC7vQH8E2dzYi6h0+ThHXJLdnOMAp7+w= X-Google-Smtp-Source: AA0mqf6Pfb/78vXpVbkKCzqWVQv6p/o5CGfleflTWNDSxkmzuLudt9k+BC1QXoNkn4IL0y9f0t/YX3baNUddM3A= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a6b:fb13:0:b0:6de:383e:4146 with SMTP id h19-20020a6bfb13000000b006de383e4146mr27314176iog.48.1670021278767; Fri, 02 Dec 2022 14:47:58 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:40 -0600 In-Reply-To: <20221202224540.1446952-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-1-allenwebb@google.com> Subject: [PATCH v6 1/5] module: Add empty modalias sysfs attribute From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This adds the modalias sysfs attribute in preparation for its implementation. Signed-off-by: Allen Webb --- include/linux/module.h | 1 + kernel/module/internal.h | 2 ++ kernel/module/sysfs.c | 33 +++++++++++++++++++++++++++++++++ kernel/params.c | 7 +++++++ 4 files changed, 43 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index ec61fb53979a9..0bfa859a21566 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -47,6 +47,7 @@ struct module_kobject { struct kobject *drivers_dir; struct module_param_attrs *mp; struct completion *kobj_completion; + struct bin_attribute modalias_attr; } __randomize_layout; struct module_attribute { diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2e2bf236f5582..8d7ae37584868 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -259,11 +259,13 @@ static inline void add_kallsyms(struct module *mod, const struct load_info *info #endif /* CONFIG_KALLSYMS */ #ifdef CONFIG_SYSFS +void add_modalias_attr(struct module_kobject *mk); int mod_sysfs_setup(struct module *mod, const struct load_info *info, struct kernel_param *kparam, unsigned int num_params); void mod_sysfs_teardown(struct module *mod); void init_param_lock(struct module *mod); #else /* !CONFIG_SYSFS */ +static inline void add_modalias_attr(struct module_kobject *mk) {} static inline int mod_sysfs_setup(struct module *mod, const struct load_info *info, struct kernel_param *kparam, diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c index ce68f821dcd12..8dafec7455fbe 100644 --- a/kernel/module/sysfs.c +++ b/kernel/module/sysfs.c @@ -240,6 +240,37 @@ static inline void add_notes_attrs(struct module *mod, const struct load_info *i static inline void remove_notes_attrs(struct module *mod) { } #endif /* CONFIG_KALLSYMS */ +static ssize_t module_modalias_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buf, loff_t pos, size_t count) +{ + return 0; +} + +/* Used in kernel/params.c for builtin modules. + * + * `struct module_kobject` is used instead of `struct module` because for + * builtin modules, the `struct module` is not available when this is called. + */ +void add_modalias_attr(struct module_kobject *mk) +{ + sysfs_bin_attr_init(&mk->modalias_attr); + mk->modalias_attr.attr.name = "modalias"; + mk->modalias_attr.attr.mode = 0444; + mk->modalias_attr.read = module_modalias_read; + if (sysfs_create_bin_file(&mk->kobj, &mk->modalias_attr)) { + /* We shouldn't ignore the return type, but there is nothing to + * do. + */ + return; + } +} + +static void remove_modalias_attr(struct module_kobject *mk) +{ + sysfs_remove_bin_file(&mk->kobj, &mk->modalias_attr); +} + static void del_usage_links(struct module *mod) { #ifdef CONFIG_MODULE_UNLOAD @@ -398,6 +429,7 @@ int mod_sysfs_setup(struct module *mod, add_sect_attrs(mod, info); add_notes_attrs(mod, info); + add_modalias_attr(&mod->mkobj); return 0; @@ -415,6 +447,7 @@ int mod_sysfs_setup(struct module *mod, static void mod_sysfs_fini(struct module *mod) { + remove_modalias_attr(&mod->mkobj); remove_notes_attrs(mod); remove_sect_attrs(mod); mod_kobject_put(mod); diff --git a/kernel/params.c b/kernel/params.c index 5b92310425c50..b7fd5313a3118 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -14,6 +14,12 @@ #include #include +#ifdef CONFIG_MODULES +#include "module/internal.h" +#else +static inline void add_modalias_attr(struct module_kobject *mk) {} +#endif /* !CONFIG_MODULES */ + #ifdef CONFIG_SYSFS /* Protects all built-in parameters, modules use their own param_lock */ static DEFINE_MUTEX(param_lock); @@ -815,6 +821,7 @@ static void __init kernel_add_sysfs_param(const char *name, BUG_ON(err); kobject_uevent(&mk->kobj, KOBJ_ADD); kobject_put(&mk->kobj); + add_modalias_attr(mk); } /* From patchwork Fri Dec 2 22:47:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 13063305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0311AC4708D for ; Fri, 2 Dec 2022 22:48:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234620AbiLBWsG (ORCPT ); Fri, 2 Dec 2022 17:48:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234797AbiLBWsE (ORCPT ); Fri, 2 Dec 2022 17:48:04 -0500 Received: from mail-il1-x14a.google.com (mail-il1-x14a.google.com [IPv6:2607:f8b0:4864:20::14a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5234E7DA50 for ; Fri, 2 Dec 2022 14:48:02 -0800 (PST) Received: by mail-il1-x14a.google.com with SMTP id i1-20020a056e021d0100b00303357fe402so6744321ila.10 for ; Fri, 02 Dec 2022 14:48:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FQ0NLGuGNf2nmEf2q9NS2Egdev1pTrmorLb5o1IyVfI=; b=LqvilsrT0WDYFMxva3mnL4sDbBeOOycVYSCdCucnJhIhrH/n9yxuXV+02TVV2VTbYL C3tXr3mtGqS1uTaf2qt2nIG54dW90K6KCQWQRCIQebrSEmkRA9GH+WRYwFgUaj1/AS/U bP3yaiQ7keRJEf4hqI+3CJVYe1wWfEAIsGBM0lheuwdxGXoTwpDViGnXQ+z96GWZGoV3 0ZhqWuoL3eIhZbVoc1shTY6tP3oGAi+mm6HJrnRI3ESNEs7PGrMgcO+ftYqbPXH5rX3m c1rk6O928RIWKg/OUXfHoVYJ5eNFrNI+FIPcekp2eGBdxv3WTsMP78mSGXgfMLOIDISH gF7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FQ0NLGuGNf2nmEf2q9NS2Egdev1pTrmorLb5o1IyVfI=; b=VsOgUZcHZwR0ewa/evyXkqouec8m9VKdu/kL5Djpc09BMM2fgbFRWKHr1uvXLY7Wcy HFM2o2piMfm4kw1P2d3TX0uCEGgk6GtRZWFb4cWMEfWyqZCPseBN6XfSN+T4caz3zd1H Ws3GkCM2z4rGFzM5ZJByyuYd0KmhjdFnHemcDy/sicSotNcs2O+Udt+A1F1m5UVYmVbu O4ocSOamsr/5T1MkXddxwuABNsg89IlvF5dgBLqSoAxgfEhVrekamn/iNz+anobMOmuk uNTURkPuONzQzOszBnjuBkX0OsOH2lzkAumpnmDuUvkjJD9JgTq2AVXERG4rviclrSn+ DDbg== X-Gm-Message-State: ANoB5pnORyFOIp/AGOV+rN6bye0RGEiLopofGehgPFXpuQBe09Vhspcu SpKbR8Oc2pnh5Co0ojRS7r65U+qh8xQvrzc= X-Google-Smtp-Source: AA0mqf7L8bU8vEGHPG0t4DmWSIrUy40io8GqFAppQrTU+eKacA28oUGbAXAgTHeLSmBBeYrAav3GPwiKH75v4ko= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a05:6638:4806:b0:389:da33:5460 with SMTP id cp6-20020a056638480600b00389da335460mr13676291jab.109.1670021281656; Fri, 02 Dec 2022 14:48:01 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:41 -0600 In-Reply-To: <20221202224744.1447448-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> <20221202224744.1447448-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-2-allenwebb@google.com> Subject: [PATCH v6 2/5] drivers: Add bus_for_each for iterating over the subsystems From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In order to print the match-id-based modaliases it must be possible to reach the match id tables of each driver. With this function it becomes possible to iterate over each subsystem which can be paired with iterating over each driver. Signed-off-by: Allen Webb --- drivers/base/bus.c | 42 ++++++++++++++++++++++++++++++++++++++ include/linux/device/bus.h | 1 + 2 files changed, 43 insertions(+) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 7ca47e5b3c1f4..4e0c5925545e5 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -178,6 +178,48 @@ static const struct kset_uevent_ops bus_uevent_ops = { static struct kset *bus_kset; +/** + * bus_for_each - bus iterator. + * @start: bus to start iterating from. + * @data: data for the callback. + * @fn: function to be called for each device. + * + * Iterate over list of buses, and call @fn for each, + * passing it @data. If @start is not NULL, we use that bus to + * begin iterating from. + * + * We check the return of @fn each time. If it returns anything + * other than 0, we break out and return that value. + * + * NOTE: The bus that returns a non-zero value is not retained + * in any way, nor is its refcount incremented. If the caller needs + * to retain this data, it should do so, and increment the reference + * count in the supplied callback. + */ +int bus_for_each(void *data, int (*fn)(struct bus_type *, void *)) +{ + int error = 0; + struct bus_type *bus; + struct subsys_private *bus_prv; + struct kset *subsys; + struct kobject *k; + + spin_lock(&bus_kset->list_lock); + + list_for_each_entry(k, &bus_kset->list, entry) { + subsys = container_of(k, struct kset, kobj); + bus_prv = container_of(subsys, struct subsys_private, subsys); + bus = bus_prv->bus; + error = fn(bus, data); + if (error) + break; + } + + spin_unlock(&bus_kset->list_lock); + return error; +} +EXPORT_SYMBOL_GPL(bus_for_each); + /* Manually detach a device from its associated driver. */ static ssize_t unbind_store(struct device_driver *drv, const char *buf, size_t count) diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index d8b29ccd07e56..82a5583437099 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -161,6 +161,7 @@ void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); void subsys_dev_iter_exit(struct subsys_dev_iter *iter); +int bus_for_each(void *data, int (*fn)(struct bus_type *, void *)); int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *dev, void *data)); struct device *bus_find_device(struct bus_type *bus, struct device *start, From patchwork Fri Dec 2 22:47:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 13063306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DD4DC4321E for ; Fri, 2 Dec 2022 22:48:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234855AbiLBWsL (ORCPT ); Fri, 2 Dec 2022 17:48:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234843AbiLBWsG (ORCPT ); Fri, 2 Dec 2022 17:48:06 -0500 Received: from mail-io1-xd49.google.com (mail-io1-xd49.google.com [IPv6:2607:f8b0:4864:20::d49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A18DA06CF for ; Fri, 2 Dec 2022 14:48:04 -0800 (PST) Received: by mail-io1-xd49.google.com with SMTP id l21-20020a5d9315000000b006df7697880aso5849238ion.23 for ; Fri, 02 Dec 2022 14:48:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=w0T1pgiMYpCvdig9Y/8aobVm5Xu54pAwpfqLNShdHMY=; b=Z7v+z+IwVUZK/AlbMeBHs81PzaHdZaOivRiZ9GgiMwNfs2wwPL53ecIB4AEN/DjARc 8tJhA4wzXXBNUOnMja42hDHidI3OXv/rkAVJC6CrchNrRCFL38S+hoX+YIKXezmDseTD lOIY0w95Fa1nChba2iZIflZMF8qjsDWr9FhimNVGAz0qAGE1K1hgefhtCMjfwv1LX+HN EvNBWmVa7A5+EOjVkDc+XIZKhJ6SdaUPlW8ZLfYK3uWI9Rzl3nkzMNag2pxB94BZuPnm VVB/P5stQ+dVTBXS1bZsSaMrnnsoJfstqIldT6ruSdRurJjx6rP56RZTTQhBBfG1qVXi WZYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=w0T1pgiMYpCvdig9Y/8aobVm5Xu54pAwpfqLNShdHMY=; b=JBLfPWxw1e+M6zd3kfqbCkMnrYcR4QDn44QkbmZC6D7qiDy9xrt/mvIjZn0R6cNlPX gMLICfw98iPEGC6JNoorfAQWljFn9cGR8KYWtAV92UoF7z/MfL2wJWH5j1MNDbYcl29i 0kj6rwzERVwlYgCAWpDrP3dMNYrqIgNeQRw2CQPejm75I+rLvgQcSSvzZiM7XyRcSNoT Hea1Wn1lYJesd+6BBBt93E8ho3tsiY7h5FmNoImspspY/oa1ax4IQesVlH1AgJ7NsXmy eQ7+WDVdYM6xLAdp/F3t7uBo6eTt7h4edEfuKmVeZiN5YMuA9I8KLudmrrYLCCwphr5K XpzQ== X-Gm-Message-State: ANoB5pngqUJ2pf1lE7hzqudYdi+3+lj7C7kauK5yuZeDsgyFB7mS7oY0 8u6SFWsKKpXn2dEZy8p7en5qyBoYcsIQpXw= X-Google-Smtp-Source: AA0mqf454uOaBRdpMH7tQImIKU8IeKBYSabjea74gyZ46dDF++UaofNevPRXdyplXUheey6pXGyw7mFiDxiEwU4= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a6b:f414:0:b0:6d1:88ee:a64f with SMTP id i20-20020a6bf414000000b006d188eea64fmr25213041iog.61.1670021283902; Fri, 02 Dec 2022 14:48:03 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:42 -0600 In-Reply-To: <20221202224744.1447448-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> <20221202224744.1447448-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-3-allenwebb@google.com> Subject: [PATCH v6 3/5] Implement modalias sysfs attribute for modules From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org When the modalias attribute is read, invoke a subsystem-specific callback for each driver registered by the specific module. The intent of the new modalias attribute is to expose the match-id-based modaliases to userspace for builtin and loaded kernel modules. Signed-off-by: Allen Webb --- include/linux/device/bus.h | 7 +++++ kernel/module/sysfs.c | 57 +++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index 82a5583437099..cce0bedec63d9 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -61,6 +61,10 @@ struct fwnode_handle; * this bus. * @dma_cleanup: Called to cleanup DMA configuration on a device on * this bus. + * @drv_to_modalias: Called to convert the matching IDs in a + * struct device_driver to their corresponding modaliases. + * Note that the struct device_driver is expected to belong + * to this bus. * @pm: Power management operations of this bus, callback the specific * device driver's pm-ops. * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU @@ -107,6 +111,9 @@ struct bus_type { int (*dma_configure)(struct device *dev); void (*dma_cleanup)(struct device *dev); + ssize_t (*drv_to_modalias)(struct device_driver *drv, char *buf, + size_t count); + const struct dev_pm_ops *pm; const struct iommu_ops *iommu_ops; diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c index 8dafec7455fbe..651c677c4ab96 100644 --- a/kernel/module/sysfs.c +++ b/kernel/module/sysfs.c @@ -5,6 +5,8 @@ * Copyright (C) 2008 Rusty Russell */ +#include +#include #include #include #include @@ -240,11 +242,64 @@ static inline void add_notes_attrs(struct module *mod, const struct load_info *i static inline void remove_notes_attrs(struct module *mod) { } #endif /* CONFIG_KALLSYMS */ +/* Track of the buffer and module identity in callbacks when walking the list of + * drivers for each bus. + */ +struct modalias_bus_print_state { + struct module_kobject *mk; + char *buf; + size_t count; + ssize_t len; +}; + +static int print_modalias_for_drv(struct device_driver *drv, void *p) +{ + struct modalias_bus_print_state *s = p; + struct module_kobject *mk = s->mk; + ssize_t len; + /* Skip drivers that do not match this module. */ + if (mk->mod) { + if (mk->mod != drv->owner) + return 0; + } else if (!mk->kobj.name || !drv->mod_name || + strcmp(mk->kobj.name, drv->mod_name)) + return 0; + + if (drv->bus && drv->bus->drv_to_modalias) { + len = drv->bus->drv_to_modalias(drv, s->buf + s->len, + s->count - s->len); + if (len < 0) + return len; + s->len += len; + } + return 0; +} + +static int print_modalias_for_bus(struct bus_type *type, void *p) +{ + return bus_for_each_drv(type, NULL, p, print_modalias_for_drv); +} + static ssize_t module_modalias_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count) { - return 0; + struct module_kobject *mk = container_of(kobj, struct module_kobject, + kobj); + struct modalias_bus_print_state state = {mk, buf, count, 0}; + int error = 0; + + if (pos != 0) + return -EINVAL; + + error = bus_for_each(&state, print_modalias_for_bus); + if (error) + return error; + + /* + * The caller checked the pos and count against our size. + */ + return state.len; } /* Used in kernel/params.c for builtin modules. From patchwork Fri Dec 2 22:47:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 13063307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9559C47090 for ; Fri, 2 Dec 2022 22:48:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234930AbiLBWsM (ORCPT ); Fri, 2 Dec 2022 17:48:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234470AbiLBWsI (ORCPT ); Fri, 2 Dec 2022 17:48:08 -0500 Received: from mail-il1-x149.google.com (mail-il1-x149.google.com [IPv6:2607:f8b0:4864:20::149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EB29A6041 for ; Fri, 2 Dec 2022 14:48:06 -0800 (PST) Received: by mail-il1-x149.google.com with SMTP id o10-20020a056e02102a00b003006328df7bso6817356ilj.17 for ; Fri, 02 Dec 2022 14:48:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=k0imnhvm+kf10bcAGQciUCVzWuZXXiWmFQ1RsUVVuTw=; b=kbarmBE+ReiJB9n4xwMFX3H4tvxZI00hfxXiwLhf0hrlmG2lwDbA8WlcUfypHXN8JJ pKIGzmgCvpn4GJpYHfZkUybDRk6PjziKEaJEeWOGiTD5hukxzuNrHEnOhz4BYL1cSmfc s7oVuDB3eE/SrF3COCc9IcLXY6Lqd2LcObrC7ANVe1U9JOHadhhqEnNzoNh8DBrR3rpt 5uOipw2Dbp2B4WqkFoMTrLWl0zvhsSmxwnZK2+s6tKSI4LPU5goCwqmmEYzAc6ala8EK e3CPcjBJ7iOAW0Vt7UD+6EiEzQ7vI3kaU/U37HAFbDSy8563dPwaadO26auVwGzPVEMT md7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=k0imnhvm+kf10bcAGQciUCVzWuZXXiWmFQ1RsUVVuTw=; b=ZPhJVa1VH2bapB2IAmn6JMvh8ZMAK/jM0xi0Sc4S9mkkQ1F4lXmM23uN+RNs+ZfJBI wdoMm/tJiDpfjTcdbn1gpakOCMsDYuBFmH9QrUBVU8wt5ijAQTnoj8ojtiiyRDU7VxMm 44tYFuAM3YEKzKjh/zYd5P10lJdgWBdkJ1uZlThpAaZ6Y/4e+C6tvikQc/ExnjgaKFUt CMGRlcxYSq5EMSGaco0r8W5PcUz/DNQsnjBxNI8Iaq4AGfP6xEBzAyWMS4T9zSHe3PpH D7KuB1/mzx8tECmskqFYEXPAV6r2mFIgLyc1FiF/R3ACKI7SeibwELaQvpV/s9ujBRvJ D9TA== X-Gm-Message-State: ANoB5pmDlzLNuH2j0MdpoeKTToYiaeYsHyaiKc3UPxZzk6spd8Z2hU8O jfO8bY7MF1PmQ3XmRddsS8ALtAhdIq2GEFs= X-Google-Smtp-Source: AA0mqf6TF+b4GR3uPUIetXie9acj2yjiT18qv25+y5jB+jOCKS4ZBR4GRR5YFcJnHLJASTSAmyHAF+UgGFyjegc= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a05:6638:480a:b0:38a:3357:8a4 with SMTP id cp10-20020a056638480a00b0038a335708a4mr18794jab.53.1670021286086; Fri, 02 Dec 2022 14:48:06 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:43 -0600 In-Reply-To: <20221202224744.1447448-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> <20221202224744.1447448-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-4-allenwebb@google.com> Subject: [PATCH v6 4/5] docs: Add entry for /sys/module/*/modalias From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Update the documentation to include the modalias sysfs attribute for modules. Signed-off-by: Allen Webb --- Documentation/ABI/testing/sysfs-module | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-module b/Documentation/ABI/testing/sysfs-module index 08886367d0470..1244a0e8d133e 100644 --- a/Documentation/ABI/testing/sysfs-module +++ b/Documentation/ABI/testing/sysfs-module @@ -48,6 +48,18 @@ Contact: Kay Sievers Description: Show the initialization state(live, coming, going) of the module. +What: /sys/module/*/modalias +Date: Nov 2022 +KernelVersion: 6.2 +Contact: Allen Webb +Description: Module match-id-based modaliases + + These match against MODALIAS values included in the uevent of + devices when they are created. The attribute is implemented for + subsystems with the authorized attribute such as USB so + userspace can make authorization decisions based on which + modules match the device. + What: /sys/module/*/taint Date: Jan 2012 KernelVersion: 3.3 From patchwork Fri Dec 2 22:47:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 13063308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3638EC4321E for ; Fri, 2 Dec 2022 22:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234912AbiLBWsM (ORCPT ); Fri, 2 Dec 2022 17:48:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234913AbiLBWsL (ORCPT ); Fri, 2 Dec 2022 17:48:11 -0500 Received: from mail-il1-x14a.google.com (mail-il1-x14a.google.com [IPv6:2607:f8b0:4864:20::14a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83DF2A7A8A for ; Fri, 2 Dec 2022 14:48:08 -0800 (PST) Received: by mail-il1-x14a.google.com with SMTP id i7-20020a056e021b0700b003033a763270so5398641ilv.19 for ; Fri, 02 Dec 2022 14:48:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GZkL2mzVFhVdShj7BdixsMt9iJ1crReLgzm32R5ZX4A=; b=eTxd1if7I2PlmTTqNfma8EthWz1pu5SyQ/Trbnp3mDe+8QjCa+w7N7/qQuwYTnsYAe xDm+pjLN/1+uOxoghtWda8DcBxyEZZ/atuAYO34dgOBsPLtC6uYVLdF8v6lfDn8YUvRJ Q8X0p3Vmr26UD6VFov0sxjtaGZJLlMcfRzUrzvIM95S9T+oKE51PiQZvrxkIh/pZjQuS 98sVQ9xr4bPQqwBExHo9uR+GzT86VRV0bhmd+lWqvWWSPrWN/NbIpw+dltxWTe709qzj ib9y2Y9oe43/FDrWFq6s8HTOZd+ibhul981XarIZoHGTjjjU/K3+9Lw2xh+rnZjtpaXf 0fYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GZkL2mzVFhVdShj7BdixsMt9iJ1crReLgzm32R5ZX4A=; b=mi9BoUUyJsowfr/hEKwtbRTFXqoGCdfBo9loCOkZTxRhZ4gWV69JOxyMqb2ZJ62L7C sGqAfgGZ48GN0krjIk2LgNZ82MnvGVSxbSseC1HmLu4Lc9fSoN9m3OsPr3/Ln7zoKMTK dkiYcXLoxzOlE/tDllLpiytacDBYjSzPZsfDu1cfhFywlgj/PINGoMy9kUDInC9ghz+D 5E9SwMzERlwfvy64/70uTdz7u/n0Gbqj2Z0LPYiqfiu6yzJlZrvu7tzZHHOB7e+DYSzt c2gjxLqXi58fNM/RUKyAZbmCX/K6Ax7ksNEQ89xAM30+HsgaDrrXmTmFYDN3GbIn34kV zvdw== X-Gm-Message-State: ANoB5pmuQy3GpNM7pjh7mrUDSgnXYua9lVV/nKzNc+N5hnQCdsD7zEhl 1rmH/1zoQV/0ENt5zi75/3ea+TZOaklGXeU= X-Google-Smtp-Source: AA0mqf4CUEbp5f3KOGSPXC6yqYWsYvWS7F4SXJGXcppDhUu8bi4voAssmnRsRF4p1fkxFN+LhhnRUF4J3/qbJmk= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a05:6602:408b:b0:6bb:f9be:3691 with SMTP id bl11-20020a056602408b00b006bbf9be3691mr31961743iob.123.1670021288075; Fri, 02 Dec 2022 14:48:08 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:44 -0600 In-Reply-To: <20221202224744.1447448-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> <20221202224744.1447448-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-5-allenwebb@google.com> Subject: [PATCH v6 5/5] drivers: Implement module modaliases for USB From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Add the per-subsystem logic needed to print match-based modaliases to the USB subsystem, so the modalias sysfs attribute for modules will function for modules that register USB drivers. Signed-off-by: Allen Webb --- drivers/base/Makefile | 2 +- drivers/base/base.h | 8 + drivers/base/mod_devicetable.c | 257 +++++++++++++++++++++++++++++++++ drivers/usb/core/driver.c | 2 + 4 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 drivers/base/mod_devicetable.c diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 83217d243c25b..924d46ae987f4 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile @@ -15,7 +15,7 @@ obj-y += firmware_loader/ obj-$(CONFIG_NUMA) += node.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory.o ifeq ($(CONFIG_SYSFS),y) -obj-$(CONFIG_MODULES) += module.o +obj-$(CONFIG_MODULES) += mod_devicetable.o module.o endif obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o obj-$(CONFIG_REGMAP) += regmap/ diff --git a/drivers/base/base.h b/drivers/base/base.h index b902d1ecc247f..fec56271104fa 100644 --- a/drivers/base/base.h +++ b/drivers/base/base.h @@ -173,6 +173,14 @@ static inline void module_add_driver(struct module *mod, static inline void module_remove_driver(struct device_driver *drv) { } #endif +#if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES) +ssize_t usb_drv_to_modalias(struct device_driver *drv, char *buf, + size_t count); +#else +static inline ssize_t usb_drv_to_modalias(struct device_driver *drv, char *buf, + size_t count) { return -EINVAL; } +#endif + #ifdef CONFIG_DEVTMPFS extern int devtmpfs_init(void); #else diff --git a/drivers/base/mod_devicetable.c b/drivers/base/mod_devicetable.c new file mode 100644 index 0000000000000..d7f198aad430f --- /dev/null +++ b/drivers/base/mod_devicetable.c @@ -0,0 +1,257 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * mod_devicetable.c - helpers for displaying modaliases through sysfs. + * + * This borrows a lot from file2alias.c + */ + +#include +#include +#include + +#include "base.h" +#include "../usb/core/usb.h" + +/* Helper macro to add a modalias field to the string buffer associated with + * a match id. + * + * Note that: + * + len should be a ssize_t and is modified in the macro + * + sep should be a string literal and is concatenated as part of a format + * string + * + field is the struct field of the match id + */ +#define ADD(buf, count, len, sep, cond, field) \ +do { \ + char *buf_ = buf; \ + size_t count_ = count; \ + if (cond) \ + (len) += scnprintf(&buf_[len], \ + count_ - (len), \ + sizeof(field) == 1 ? (sep "%02X") : \ + sizeof(field) == 2 ? (sep "%04X") : \ + sizeof(field) == 4 ? (sep "%08X") : "", \ + (field)); \ + else \ + (len) += scnprintf(&buf_[len], count_ - (len), (sep "*")); \ +} while (0) + +#ifdef CONFIG_USB +/* USB related modaliases can be split because of device number matching, so + * this function handles individual modaliases for one segment of the range. + */ +static ssize_t usb_id_to_modalias(const struct usb_device_id *id, + unsigned int bcdDevice_initial, + int bcdDevice_initial_digits, + unsigned char range_lo, + unsigned char range_hi, + unsigned char max, const char *mod_name, + char *buf, size_t count) +{ + ssize_t len = 0; + + ADD(buf, count, len, "alias usb:v", + id->match_flags & USB_DEVICE_ID_MATCH_VENDOR, id->idVendor); + ADD(buf, count, len, "p", id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT, + id->idProduct); + + len += scnprintf(&buf[len], count - len, "d"); + if (bcdDevice_initial_digits) + len += scnprintf(&buf[len], count - len, "%0*X", + bcdDevice_initial_digits, bcdDevice_initial); + if (range_lo == range_hi) { + len += scnprintf(&buf[len], count - len, "%X", range_lo); + } else if (range_lo > 0 || range_hi < max) { + if (range_lo > 0x9 || range_hi < 0xA) { + len += scnprintf(&buf[len], count - len, "[%X-%X]", + range_lo, range_hi); + } else { + len += scnprintf(&buf[len], count - len, + range_lo < 0x9 ? "[%X-9" : "[%X", + range_lo); + len += scnprintf(&buf[len], count - len, + range_hi > 0xA ? "A-%X]" : "%X]", + range_hi); + } + } + if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1)) + len += scnprintf(&buf[len], count - len, "*"); + + ADD(buf, count, len, "dc", + id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS, id->bDeviceClass); + ADD(buf, count, len, "dsc", + id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS, + id->bDeviceSubClass); + ADD(buf, count, len, "dp", + id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL, + id->bDeviceProtocol); + ADD(buf, count, len, "ic", + id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS, + id->bInterfaceClass); + ADD(buf, count, len, "isc", + id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS, + id->bInterfaceSubClass); + ADD(buf, count, len, "ip", + id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL, + id->bInterfaceProtocol); + ADD(buf, count, len, "in", + id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER, + id->bInterfaceNumber); + + len += scnprintf(&buf[len], count - len, " %s\n", mod_name); + return len; +} + +/* Handles increment/decrement of BCD formatted integers */ +/* Returns the previous value, so it works like i++ or i-- */ +static unsigned int incbcd(unsigned int *bcd, + int inc, + unsigned char max, + size_t chars) +{ + unsigned int init = *bcd, i, j; + unsigned long long c, dec = 0, div; + + /* If bcd is not in BCD format, just increment */ + if (max > 0x9) { + *bcd += inc; + return init; + } + + /* Convert BCD to Decimal */ + for (i = 0 ; i < chars ; i++) { + c = (*bcd >> (i << 2)) & 0xf; + c = c > 9 ? 9 : c; /* force to bcd just in case */ + for (j = 0 ; j < i ; j++) + c = c * 10; + dec += c; + } + + /* Do our increment/decrement */ + dec += inc; + *bcd = 0; + + /* Convert back to BCD */ + for (i = 0 ; i < chars ; i++) { + for (c = 1, j = 0 ; j < i ; j++) + c = c * 10; + div = dec; + (void)do_div(div, c); /* div = div / c */ + c = do_div(div, 10); /* c = div % 10; div = div / 10 */ + *bcd += c << (i << 2); + } + return init; +} + +/* Print the modaliases for the specified struct usb_device_id. */ +static ssize_t usb_id_to_modalias_multi(const struct usb_device_id *id, + const char *mod_name, char *buf, + size_t count) +{ + ssize_t len = 0; + unsigned int devlo, devhi; + unsigned char chi, clo, max; + int ndigits; + + devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ? + id->bcdDevice_lo : 0x0U; + devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ? + id->bcdDevice_hi : ~0x0U; + + /* Figure out if this entry is in bcd or hex format */ + max = 0x9; /* Default to decimal format */ + for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) { + clo = (devlo >> (ndigits << 2)) & 0xf; + chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> + (ndigits << 2)) & 0xf; + if (clo > max || chi > max) { + max = 0xf; + break; + } + } + + /* + * Some modules (visor) have empty slots as placeholder for + * run-time specification that results in catch-all alias + */ + if (!(id->idVendor || id->idProduct || id->bDeviceClass || + id->bInterfaceClass)) + return len; + + /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */ + for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; + ndigits--) { + clo = devlo & 0xf; + chi = devhi & 0xf; + /* If we are in bcd mode, truncate if necessary */ + if (chi > max) + chi = max; + devlo >>= 4; + devhi >>= 4; + + if (devlo == devhi || !ndigits) { + len += usb_id_to_modalias(id, devlo, ndigits, clo, chi, + max, mod_name, buf + len, + count - len); + break; + } + + if (clo > 0x0) + len += usb_id_to_modalias(id, + incbcd(&devlo, 1, max, + sizeof(id->bcdDevice_lo) * 2), + ndigits, clo, max, max, mod_name, buf + len, + count - len); + + if (chi < max) + len += usb_id_to_modalias(id, + incbcd(&devhi, -1, max, + sizeof(id->bcdDevice_lo) * 2), + ndigits, 0x0, chi, max, mod_name, buf + len, + count - len); + } + return len; +} + +/* Print the modaliases for the given driver assumed to be an usb_driver or + * usb_device_driver. + * + * "alias" is prepended and the module name is appended to each modalias to + * match the format in modules.aliases. + * + * The modaliases will be written out to @buf with @count being the maximum + * bytes to write. The return value is a negative errno on error or the number + * of bytes written to @buf on success. + */ +ssize_t usb_drv_to_modalias(struct device_driver *drv, char *buf, + size_t count) +{ + ssize_t len = 0; + const struct usb_device_id *id; + const char *mod_name; + + if (drv->bus != &usb_bus_type) + return -EINVAL; + + if (drv->owner) + mod_name = drv->owner->name; + else + mod_name = drv->mod_name; + + if (is_usb_device_driver(drv)) + id = to_usb_device_driver(drv)->id_table; + else + id = to_usb_driver(drv)->id_table; + if (!id) + return len; + + for (; id->match_flags; id++) { + len += usb_id_to_modalias_multi(id, mod_name, buf + len, + count - len); + } + return len; +} +#else +inline ssize_t usb_drv_to_modalias(struct device_driver *drv, char *buf, + size_t count){ return 0; } +#endif diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c index 7e7e119c253fb..fdbc197b64c9c 100644 --- a/drivers/usb/core/driver.c +++ b/drivers/usb/core/driver.c @@ -32,6 +32,7 @@ #include #include +#include "../../base/base.h" #include "usb.h" @@ -2030,4 +2031,5 @@ struct bus_type usb_bus_type = { .match = usb_device_match, .uevent = usb_uevent, .need_parent_lock = true, + .drv_to_modalias = usb_drv_to_modalias, };