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: 13063309 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 33FCEC4332F for ; Fri, 2 Dec 2022 22:48:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234759AbiLBWsC (ORCPT ); Fri, 2 Dec 2022 17:48:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234753AbiLBWsB (ORCPT ); Fri, 2 Dec 2022 17:48:01 -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 840807BC22 for ; Fri, 2 Dec 2022 14:47:59 -0800 (PST) Received: by mail-il1-x149.google.com with SMTP id c4-20020a056e020bc400b0030098df879dso6749561ilu.6 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=GnbvAbk4JZggnnVcMvbId/6EhDFCNDw70wbTaRHF7TB/wtvkcW2rios7gI2W+/DoCu aAoo5/rkoBECb0ic5GazJVnlohi3LTXduuNHduU3MGlGlGqrqVPwnyROZFllXExUSK5C d88s+x+UGt7h6ySytNzZhe/dvjof/1S+8H+Q+1VoTGWj4US64bQ+y9U8KoL11V7m66uB Hp2cX1aNLeoQaG98r10Ahl2tdwhvSkYQ0bHr4ZG7r9FAYV42b63oMdRKQfmzlsOXUc17 ZHx7nD5mWqOPvB4jVRmkYjkJD1tZniK+0/WORzD9N/mbNAF3mWiDeaQ+5Y4cTWcRReCZ S+oQ== X-Gm-Message-State: ANoB5pmpQfSBlpDZJMDYnFLs1nTmZ34s1cEfWya2iPK4LCTG2mFb4aqb uImkUs3JWuLES22bKwTbBCJvWYzI8g4t1/f6T4LmjugjFK5d/k3xmzcofNfZHfOJ6O9i18T1SMd JyJvnEb9h72xPAYux1+kEj1HI5Iwf+ZRy0r70qHI46nU9SYhje6AFgvJd0nL7hGm8WwxAzC5LAo oKtbM= 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: 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: 13063310 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 D07DAC4332F for ; Fri, 2 Dec 2022 22:48:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234318AbiLBWsE (ORCPT ); Fri, 2 Dec 2022 17:48:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234777AbiLBWsD (ORCPT ); Fri, 2 Dec 2022 17:48:03 -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 0AA31DECB for ; Fri, 2 Dec 2022 14:48:02 -0800 (PST) Received: by mail-io1-xd49.google.com with SMTP id k21-20020a5e8915000000b006de391b332fso5672924ioj.4 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=xDBK13CgRgho/o0IbYVeSdWQ0zmRh8ClslEPznURziiZ8ptLrfplhpHzhw3IfgU/9A d7rHvIWq/mD58fXTAmVWMUuY0cP9IjX/+LHOciBeh4MQlWH6p09jkinoU8YKud+SbbKG nGTABToLnIzwfDeOeD2vtxuSIPnBA4h+PeA7kIzJX+VqxKIDriVB75MtucjrYcVZhhXz dQNzyRxnYurOIPBhUIAwJz/ugmin2snBXKBrTS7ELQo/+lK9lP2C4/yS8zv98v6rH0Yi rj7TN4+zH1IPaxEbtBogtvqlv2zLf98Sxs9ZU3YnVwQ6YTGobAhzW4l6iMsVlJ+FPzVL oqug== X-Gm-Message-State: ANoB5plQIaSVEJeD1Ua/Q5OWJ0K3KBYdncOg1oKAQN0XkRt1SWw0fj91 waozEXWg5Q1xavLvrfoTgV5Be7Y0ccNag8WcDG/no4NQMM7cg/H1OcBj9r+kqLepTftl9tqxmz9 Lgn2/rrAgNAsf6fAApDuipyiSxfc8A9pCMl1VPtpElxkVe8zghN8rHfY9HjXbHrBCoVm8EEosie U1CJ4= 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: 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: 13063311 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 DEB74C4708D for ; Fri, 2 Dec 2022 22:48:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234914AbiLBWsL (ORCPT ); Fri, 2 Dec 2022 17:48:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234851AbiLBWsG (ORCPT ); Fri, 2 Dec 2022 17:48:06 -0500 Received: from mail-io1-xd4a.google.com (mail-io1-xd4a.google.com [IPv6:2607:f8b0:4864:20::d4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77AC4A320E for ; Fri, 2 Dec 2022 14:48:04 -0800 (PST) Received: by mail-io1-xd4a.google.com with SMTP id z6-20020a056602080600b006dfd603be92so5762451iow.11 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=kNiXFa6HEYywVCVXJf6vYHqkTngUHMxrrwFGBDNp/gmOWzh31uewQqo5HWc4wwcjWf IKrSCa+U3Gdl0H+YUt4WLeplXRL6oYIcSgMXclDwXVkGUZ8Qtcm67VpzUg2W+V0W8PGE F8uu4gYDlRDmjCp2uw//e6wwZbYJ1C4Qw3qw4HoDSJWdOx2ds/9aLbcTgwPbcC0Yd50V pj2aZ3W4tDarmnaOjpykW4eNHNuanLxeitwlzDXqYaUaEII63E9Ui1rtDlPRq/DBAux5 w5oLhf1B7PQifihhggvmq3tD9zttH3ks7YbBYYX+g9LvGDRlZfCW98wbJlQgn3HpIyCc dJEw== X-Gm-Message-State: ANoB5pmJjbXZYWQ2F2xXuCBYregdsd0MPCCC8dOPozuGGBj9YTAnF1oN KI3lf2qqFrPW34neO3p+0U/6/aufx+aorWIVrloW5OFsJDLfXWmtLr51BIgZxFN17bQB//KZ7/u TR0LvEsXPR6Sa/MPQkvuxhucS+2JuGumbqX7sjSJbt5TFV3Bhzf7d7nFGk0WV2Gq/8jnLpCKx9q hF278= 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: 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: 13063312 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 C9FD0C4332F for ; Fri, 2 Dec 2022 22:48:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234931AbiLBWsM (ORCPT ); Fri, 2 Dec 2022 17:48:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234883AbiLBWsI (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 DBD96A6072 for ; Fri, 2 Dec 2022 14:48:06 -0800 (PST) Received: by mail-il1-x149.google.com with SMTP id a11-20020a92c54b000000b003034a80704fso265609ilj.1 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=b26z4kfT5wEveUWTTxatRkfXTRXwCYe8yByO5ntB4hOmIj0QCPyqXIA1bN4oId6ohf 6zIX2okPq9A8FwB9rA3L8FWmGeoYVccXgi9uJ1LiCJkciIJ3XeFCWr1zYc9IZsbErnL9 3zTYwNal9mobjRhK2PVzFPmhyBT4uFmz+vq6kjAEnOjPonuICZZwpslkgAKA1SWbK3id 3RfBX+8tRwNfkbwlZMZ025oAjjZ3JZi5T0Gc9znE1l2XAopsuwp4dMfc606fM4kOp5x6 0iZ+k6ny0gWiNXO9gnLZJ0J2bcr7xxyruBAH4MhCUlXj26fSGhcZ1g7jzS/DbqQqa6BK 74yw== X-Gm-Message-State: ANoB5pkFr1m3jeT9YCoCxsdzank4vpIVZoKIj/05aztpgQU8EtpwbDIE DHylQrODMYIE1l1DPaWJKX+KfMIh++PfQjI0+7SkdVKYRE5nX4B0BmSxhne+keeWSlZsHMJWei8 vtZGRXK3osRhkvJODk+dHb/u+vIroZJEuaUnrGcLscZe0xUNEvKYEenx1h4knVlZbxaTrGcpLJt xW3FI= 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: 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: 13063313 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 6DF1DC4332F for ; Fri, 2 Dec 2022 22:48:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234945AbiLBWsX (ORCPT ); Fri, 2 Dec 2022 17:48:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234851AbiLBWsL (ORCPT ); Fri, 2 Dec 2022 17:48:11 -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 83C88A6CEB for ; Fri, 2 Dec 2022 14:48:08 -0800 (PST) Received: by mail-il1-x149.google.com with SMTP id j20-20020a056e02219400b00300a22a7fe0so6782838ila.3 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=bysHpcP0N+8C8T9P3HZoFK3OxQo6UUWvpNPOxAfFvNQiZm+7mrhyV4JimiOIMv13pk EkDyJ7LIcO4aK7GMpPvmKDKuCWXysLItABzCjU47yZuoBh2k9KokYW5CELqFROecu0xF FAR3Wli3V3c2HkRFzKbswr7nukhRCjiE+pL8g7GQY2h7zv5kBlvcUkBy8DetGL9iQ2NF LKFd08ySQB1D9L4SQqk5Vt+AQiUCyQW+SkJqHIzdL7Eug7+krei9Yicr9CJDG1obYYxT AAlDV1vUgdhS7hLtPgyC60PZIrJdpbhxQn7Fci1Il3I9O/mSG5nRVii221NTLU9A1ABS QcEw== X-Gm-Message-State: ANoB5pmBsAX8SeVU2pmzAg124qAc59ilZ7AoH8nBgEj/FPIeyMvMPXoT S/gutC9LcK3j4KdOeUUMYIiS/Qy5x/droEIeVdAIdL6UnHDUwpGucpnq5A4xfNQQ5m+iklHUej+ DEUFYL8T0j6+j6QaVEVSR7P39JicmSm9tN76PxHrW0VNrRI46+bIKTd/ODYVdB8T2Pw4BEsKAWI DA7cU= 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: 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, };