From patchwork Fri Aug 31 17:14:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Flatley X-Patchwork-Id: 10584251 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 C7950920 for ; Fri, 31 Aug 2018 17:16:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9A6F2BCDD for ; Fri, 31 Aug 2018 17:16:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ADF832C022; Fri, 31 Aug 2018 17:16:34 +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 520EA2BCDD for ; Fri, 31 Aug 2018 17:16:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727573AbeHaVZA (ORCPT ); Fri, 31 Aug 2018 17:25:00 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:40073 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727328AbeHaVZA (ORCPT ); Fri, 31 Aug 2018 17:25:00 -0400 Received: by mail-pl1-f193.google.com with SMTP id s17-v6so5749292plp.7 for ; Fri, 31 Aug 2018 10:16:32 -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:mime-version :content-transfer-encoding; bh=dMy4/km8uf51as542RlUjvhColuYSH7J9l+TG1L2VIg=; b=crxFJgA72HxiFM/nY8ISvqySDXdeejN2S/qJYy4C/+Rtx06qVlXDzY5vELEK5JZEjz xcqRxChNzTFhHwENHwfMgiGgV87fnCke/1x3PfJeZxWJMQCO8lr+iLPX9K2rUZhBj68x xg24w5vsd/kJU8tB2OfJjUKvPWeDaD2ncalaI= 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:mime-version :content-transfer-encoding; bh=dMy4/km8uf51as542RlUjvhColuYSH7J9l+TG1L2VIg=; b=pz6eFj55ukbMZdCEnrMFk/h5KNeb0SCp4V/aqNRaW47307KVwX38qI+OmDaFECT/hK au3NW53SNLZWJVWmsTALaQA3dkAV5mFllGN3A/rO9pz7HwnqSP2OSSr4MDACBXsOMQ/R N7TjLdN7matIKEEZhtdPyAIBKhsMggjDtrpa6bOor84CsMld5ts4vGKv1i3YRMaYV0/p ddF29Zmai+c3WRBXR6lUV9xKzn3dXokKREF5zIeBypIh595sm9iqTTBfHIsUKywwurB+ DtQCHZeynTT+/eRyUUtvLYoLNp5RkRKhF0vbuvEUuP/pWyscuYg5c8WaIU2tBZPH26Hu 8aUw== X-Gm-Message-State: APzg51CBG1f9jucva4sQucIDqOcJST2RPZoDgRMxi+61UnjmmQcIHOPl yLQFTHCjpxNfTrJ0RFgEye7ing0ZbD0= X-Google-Smtp-Source: ANB0VdZf0rZ0BDD5gBqRXNh9gbAmHSp2B+SnbT5xZr7BOdWJHScPyINaFDapToWs29mf+ZEmeoA3vg== X-Received: by 2002:a17:902:ba83:: with SMTP id k3-v6mr15967779pls.251.1535735792158; Fri, 31 Aug 2018 10:16:32 -0700 (PDT) Received: from flatbox9001.mtv.corp.google.com ([2620:15c:202:201:8a81:a957:49d:5968]) by smtp.gmail.com with ESMTPSA id d132-v6sm17457105pgc.93.2018.08.31.10.16.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 31 Aug 2018 10:16:31 -0700 (PDT) From: Jon Flatley To: linux-usb@vger.kernel.org Cc: gregkh@linuxfoundation.org, Jon Flatley Subject: [PATCH] usb: core: added uevent for over-current Date: Fri, 31 Aug 2018 10:14:19 -0700 Message-Id: <20180831171419.186830-1-jflat@chromium.org> X-Mailer: git-send-email 2.19.0.rc1.350.ge57e33dbd1-goog 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 --- drivers/usb/core/hub.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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);