From patchwork Tue Sep 11 17:43:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Flatley X-Patchwork-Id: 10596033 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7A84913B8 for ; Tue, 11 Sep 2018 17:44:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6654B29ADD for ; Tue, 11 Sep 2018 17:44:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58C8029B37; Tue, 11 Sep 2018 17:44:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E17C729ADD for ; Tue, 11 Sep 2018 17:44:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728071AbeIKWpR (ORCPT ); Tue, 11 Sep 2018 18:45:17 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:36732 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726782AbeIKWpQ (ORCPT ); Tue, 11 Sep 2018 18:45:16 -0400 Received: by mail-pg1-f193.google.com with SMTP id d1-v6so12630338pgo.3 for ; Tue, 11 Sep 2018 10:44:54 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=Pl3gfVL6zGdz3PEpdo4KIg36lbrYvrVd49opM5TVGiE=; b=UzP7G/CxdUI9AVSps2QQW1EunCsjitNg1BCYkMbuPZwu+kF4iX3WQv1jf5DHIyh4z6 yRM1r3BiS2KasDRRV4NnCzRY4VG3Y3YqYZZMfg/y0vO6FyW9G9O/wsEQjX5cbi4JbyVX vD7aT4/D3Lnr+Vm2yJ53jw66oJsqC5RJA4Syc= 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=Pl3gfVL6zGdz3PEpdo4KIg36lbrYvrVd49opM5TVGiE=; b=XEhcJZNJCmyDhi3aeKEPFF9u2JBSWdHe58IJ/6RqMZpOKCVJ6V573klEGHRVbxA5Rh YDURKm/D93cTdGVwtWgxKhgSbw6QdC2/XaU1BgcyHNMf7Y9LE8KXQBgUmeizgY+c+i8B gFM1P4h0B2cfCXW7/OPpFybl1GYM8GOkwdrUHj3C50LErFtlHiIUIc/9YJSHJlOa1wvt b+8o0EtsnEMrucuolOpjqqB1Ft0GGFdd0SHLRKpXZxqgj1HfqJ1jL33lSjhBrTBt0c2c OODAkLcEmQ6r1gqzBtTqH2SPanr1y302+0WJDm6963CaGeWGN5RYSqFJI/aZ7T6kFKyA jDkg== X-Gm-Message-State: APzg51DWiKV1BlpvpWu6TiP4vlT1SVqnAnX7K1ji8OG50bsk7pTrCcp8 3atNjZBqWtCAAoNZ+9Ogquo3UhTKtps= X-Google-Smtp-Source: ANB0VdZPZPmxaOMMOEY8T7Syv8DGdzZej8f+LF+tazhlATWLHawbdzZnvPMyWS1vgYSFlOGC4V7+iw== X-Received: by 2002:a63:8543:: with SMTP id u64-v6mr30502066pgd.248.1536687893486; Tue, 11 Sep 2018 10:44:53 -0700 (PDT) Received: from flatbox9001.mtv.corp.google.com ([2620:15c:202:201:8a81:a957:49d:5968]) by smtp.gmail.com with ESMTPSA id p1-v6sm24554251pfn.53.2018.09.11.10.44.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Sep 2018 10:44:52 -0700 (PDT) From: Jon Flatley To: linux-usb@vger.kernel.org Cc: gregkh@linuxfoundation.org, Jon Flatley Subject: [PATCH v2] usb: core: added uevent for over-current Date: Tue, 11 Sep 2018 10:43:10 -0700 Message-Id: <20180911174310.245393-1-jflat@chromium.org> X-Mailer: git-send-email 2.19.0.397.gdd90340f6a-goog In-Reply-To: <153660325315749> References: <153660325315749> MIME-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP After commit 1cbd53c8cd85 ("usb: core: introduce per-port over-current counters") usb ports expose a sysfs value 'over_current_count' to user space. This value on its own is not very useful as it requires manual polling. As a solution, fire a udev event from the usb hub device that specifies the values 'OVER_CURRENT_PORT' and 'OVER_CURRENT_COUNT' that indicate the path of the usb port where the over-current event occurred and the value of 'over_current_count' in sysfs. Additionally, call sysfs_notify() so the sysfs value supports poll(). Signed-off-by: Jon Flatley --- Documentation/ABI/testing/sysfs-bus-usb | 9 ++++++- drivers/usb/core/hub.c | 36 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb index c5586bb7b6a7..8677f9c041a3 100644 --- a/Documentation/ABI/testing/sysfs-bus-usb +++ b/Documentation/ABI/testing/sysfs-bus-usb @@ -197,7 +197,14 @@ Description: ports and report them to the kernel. This attribute is to expose the number of over-current situation occurred on a specific port to user space. This file will contain an unsigned 32 bit value - which wraps to 0 after its maximum is reached. + which wraps to 0 after its maximum is reached. This file supports + poll() for monitoring changes to this value in user space. + + Any time this value changes the corresponding hub device will send a + udev event with the following attributes: + + OVER_CURRENT_PORT=/sys/bus/usb/devices/.../(hub interface)/portX + OVER_CURRENT_COUNT=[current value of this sysfs attribute] What: /sys/bus/usb/devices/.../(hub interface)/portX/quirks Date: May 2018 diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 65feedd69323..c986b0fc2daa 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -5096,6 +5097,40 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1, usb_lock_port(port_dev); } +/* Handle notifying userspace about hub over-current events */ +static void port_over_current_notify(struct usb_port *port_dev) +{ + static char *envp[] = { NULL, NULL, NULL }; + struct device *hub_dev; + char *port_dev_path; + + sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); + + hub_dev = port_dev->dev.parent; + + if (!hub_dev) + return; + + port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); + if (!port_dev_path) + return; + + envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); + if (!envp[0]) + return; + + envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", + port_dev->over_current_count); + if (!envp[1]) + goto exit; + + kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); + + kfree(envp[1]); +exit: + kfree(envp[0]); +} + static void port_event(struct usb_hub *hub, int port1) __must_hold(&port_dev->status_lock) { @@ -5138,6 +5173,7 @@ static void port_event(struct usb_hub *hub, int port1) if (portchange & USB_PORT_STAT_C_OVERCURRENT) { u16 status = 0, unused; port_dev->over_current_count++; + port_over_current_notify(port_dev); dev_dbg(&port_dev->dev, "over-current change #%u\n", port_dev->over_current_count);