From patchwork Thu Sep 20 17:17:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Flatley X-Patchwork-Id: 10608245 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 9A55B6CB for ; Thu, 20 Sep 2018 17:18:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 803E52E27E for ; Thu, 20 Sep 2018 17:18:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7222C2E288; Thu, 20 Sep 2018 17:18:41 +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 072A12E27E for ; Thu, 20 Sep 2018 17:18:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727676AbeITXDI (ORCPT ); Thu, 20 Sep 2018 19:03:08 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:41277 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726193AbeITXDI (ORCPT ); Thu, 20 Sep 2018 19:03:08 -0400 Received: by mail-pl1-f194.google.com with SMTP id b12-v6so4651414plr.8 for ; Thu, 20 Sep 2018 10:18:39 -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=lNkqciEuZ6hiMr+L4KMcf897O78uBgz+DDsmsPnBKiI=; b=kfSmjct4kC0/Ob52l9tHKnQUl08SgHscDk4/JgHgdwHqTg9Wih9nmDdNy7AALv7va8 2Fp5ZwoYROyaD8KXghbbPhxrLkfNAXFD44qpQNmIFGzj5vFSyvu0zAnqGub5wdHp0KmW IzpS3VtMqGE3BBV4B/PWbUL9uaL2H2Fcg/ofE= 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=lNkqciEuZ6hiMr+L4KMcf897O78uBgz+DDsmsPnBKiI=; b=EIq8KBcKgL29dn6vei1fx4Q4NCw9jn7ayz5MtAjTHs/6Rxkd1o3ZWqrAY/r2uL3s9Q Vxsou/gu/X2wLNMDWLOsV7GwGx9Q0cZpvWAdbvHtVeUSPlhUF9MvRTGrP1t0M7H21Lng 8owcwVLlvPx8Vagd6/pDqQrSq1aajvRmsSX8yl3xbr/HAYJDVy2y6clO4cYOskCEZD0R AO4BowXeappi0Znm3SEZ2KO26MJ/qHRpMd0YJshYqk3vLNMyMqqn6qwGf3Aa+z6klaiA nFQja8IAkKkyGjv3iuD0x3+Y9L9eWq/Ytah/Pt/O8Qf3Zd2hyQFRwIFjuXI5SlDt/b7L v5zQ== X-Gm-Message-State: APzg51DjYv5jZcLRcdOchJmUYVBftZk6QMbN//Tr9ZcSTKcHr2OuGjuw pMU4V406AEuhkwMKY2G5kjj21OQIwpY= X-Google-Smtp-Source: ANB0VdZM4RU+JHi1NXNzdmTlGxJLMUwT8PNy2WetWVWSppq7efHD/ypwyweK8ir8eupGqKiBJ9FIKg== X-Received: by 2002:a17:902:204:: with SMTP id 4-v6mr37029250plc.281.1537463918804; Thu, 20 Sep 2018 10:18:38 -0700 (PDT) Received: from flatbox9001.mtv.corp.google.com ([2620:15c:202:201:8a81:a957:49d:5968]) by smtp.gmail.com with ESMTPSA id l21-v6sm6296748pgo.81.2018.09.20.10.18.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 10:18:37 -0700 (PDT) From: Jon Flatley To: linux-usb@vger.kernel.org Cc: gregkh@linuxfoundation.org, Jon Flatley Subject: [PATCH v3] usb: core: added uevent for over-current Date: Thu, 20 Sep 2018 10:17:54 -0700 Message-Id: <20180920171754.82787-1-jflat@chromium.org> X-Mailer: git-send-email 2.19.0.444.g18242da7ef-goog In-Reply-To: <20180920111137.GA16430@kroah.com> References: <20180920111137.GA16430@kroah.com> 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 08d456e07b53..c4a70f532ec3 100644 --- a/Documentation/ABI/testing/sysfs-bus-usb +++ b/Documentation/ABI/testing/sysfs-bus-usb @@ -219,7 +219,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/usb3_lpm_permit Date: November 2015 diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 462ce49f683a..7801bb30bdba 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -5147,6 +5148,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) { @@ -5189,6 +5224,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);