From patchwork Thu Aug 28 09:55:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kiran Kumar Raparthy X-Patchwork-Id: 36170 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yk0-f200.google.com (mail-yk0-f200.google.com [209.85.160.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BA5C42054F for ; Thu, 28 Aug 2014 09:56:11 +0000 (UTC) Received: by mail-yk0-f200.google.com with SMTP id 9sf4331944ykp.7 for ; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :mime-version:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=9K9bZ+fdT29wQG2AjPlb978171IAUWIATs3XLpwsBV0=; b=YrTYgh2Sns/yJPFRbJIrjFbMEXNaQ+uvoOSITGurvjc9x1vffyfWuCSja4lwvYajJq ahGSHDgl9jZnGydQuDxa9UT8blSu1wwepttpgRHPE4hprPbYLdbZfO0A2Bd8GgFUNdd/ xfP/piWJURDFexHevki7DxYbE/Jkdj3mTPflrDn3SqWzeMdYIQs/+4un5nsrJslYDe/C SPKXFA7bpZIEeCQhyhMMIiKf1qrSlBjQiwsL8EgR80eLrgSRM26glCIoMfxlbV9rW9gv B6RWcXUm2Xpq3/zWw87Q+MwxRa35pqmK58krTUosnbynEzW68yl35DXFPF1pFIw8824V FyLw== X-Gm-Message-State: ALoCoQmK/jOsqAkFw+ZuqIzPW3JbTk2NfT8BorCG23x9ey03IHITPlcgenMfz/4YB94SqqmIw7qI X-Received: by 10.236.77.42 with SMTP id c30mr1659447yhe.6.1409219771582; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.46.100 with SMTP id j91ls451646qga.44.gmail; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) X-Received: by 10.52.168.134 with SMTP id zw6mr648875vdb.37.1409219771480; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) Received: from mail-vc0-f171.google.com (mail-vc0-f171.google.com [209.85.220.171]) by mx.google.com with ESMTPS id r2si2964349vda.106.2014.08.28.02.56.11 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Aug 2014 02:56:11 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.171 as permitted sender) client-ip=209.85.220.171; Received: by mail-vc0-f171.google.com with SMTP id id10so519755vcb.2 for ; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) X-Received: by 10.220.114.5 with SMTP id c5mr831311vcq.28.1409219771282; Thu, 28 Aug 2014 02:56:11 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp209372vcb; Thu, 28 Aug 2014 02:56:10 -0700 (PDT) X-Received: by 10.70.45.97 with SMTP id l1mr4180625pdm.148.1409219769991; Thu, 28 Aug 2014 02:56:09 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id rh5si5555003pbc.77.2014.08.28.02.55.39 for ; Thu, 28 Aug 2014 02:56:09 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936539AbaH1Jzi (ORCPT + 26 others); Thu, 28 Aug 2014 05:55:38 -0400 Received: from mail-pa0-f52.google.com ([209.85.220.52]:41721 "EHLO mail-pa0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934539AbaH1Jzg (ORCPT ); Thu, 28 Aug 2014 05:55:36 -0400 Received: by mail-pa0-f52.google.com with SMTP id eu11so1894680pac.25 for ; Thu, 28 Aug 2014 02:55:35 -0700 (PDT) X-Received: by 10.68.125.202 with SMTP id ms10mr4069207pbb.71.1409219735671; Thu, 28 Aug 2014 02:55:35 -0700 (PDT) Received: from c-krapar-linux.qualcomm.com ([202.46.23.54]) by mx.google.com with ESMTPSA id a15sm4661024pdj.33.2014.08.28.02.55.31 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 28 Aug 2014 02:55:35 -0700 (PDT) From: Kiran Kumar Raparthy To: linux-kernel@vger.kernel.org Cc: Todd Poynor , Felipe Balbi , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Android Kernel Team , John Stultz , =?UTF-8?q?Arve=20Hj=F8nnev=E5g?= , Benoit Goby , Kiran Raparthy Subject: [RFC v1 1/2] USB: OTG: Hold wakeupsource when VBUS present Date: Thu, 28 Aug 2014 15:25:10 +0530 Message-Id: <1409219710-26874-1-git-send-email-kiran.kumar@linaro.org> X-Mailer: git-send-email 1.8.2.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: kiran.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.171 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Todd Poynor USB: OTG: Hold wakeupsource when VBUS present Purpose of this is to prevent the system to enter into suspend state from USB peripheral traffic by hodling a wakeupsource when USB(otg) is connected and enumerated in peripheral mode(say adb). Disabled by default, can enable with: echo Y > /sys/module/otg_wakeupsource/parameters/enabled Cc: Felipe Balbi Cc: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org Cc: linux-usb@vger.kernel.org Cc: Android Kernel Team Cc: John Stultz Cc: Arve Hjřnnevĺg Cc: Benoit Goby Signed-off-by: Todd Poynor [kiran: Added context to commit message. Included build fix from Benoit Goby and Arve Hjřnnevĺg. Removed lock->held field in driver as this mechanism is provided in wakeupsource driver. wakelock(wl) terminology replaced with wakeup_source(ws). changed to disabled by default from "enable by default". sys entry(module param) field modified to otg_wakeupsource. included Todd's refactoring logic. Introduced get_phy_hook to handle otgws_xceiv per-PHY. otgws_nb moved to otg_wakeupsource_init function. __pm_stay_awake and __pm_relax called directly from the main. code instead of calling them via otgws_grab,otgws_drop. modified Kconfig help text] Signed-off-by: Kiran Raparthy --- drivers/usb/phy/Kconfig | 8 +++ drivers/usb/phy/Makefile | 1 + drivers/usb/phy/otg-wakeupsource.c | 144 +++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 drivers/usb/phy/otg-wakeupsource.c diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig index e253fa0..d9ddd85 100644 --- a/drivers/usb/phy/Kconfig +++ b/drivers/usb/phy/Kconfig @@ -6,6 +6,14 @@ menu "USB Physical Layer drivers" config USB_PHY def_bool n +config USB_OTG_WAKEUPSOURCE + bool "Hold wakeupsource when USB is enumerated in peripheral mode" + depends on PM_SLEEP + select USB_PHY + help + Prevent the system going into automatic suspend while + it is attached as a USB peripheral by holding a wakeupsource. + # # USB Transceiver Drivers # diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile index 24a9133..ca2fbaf 100644 --- a/drivers/usb/phy/Makefile +++ b/drivers/usb/phy/Makefile @@ -3,6 +3,7 @@ # obj-$(CONFIG_USB_PHY) += phy.o obj-$(CONFIG_OF) += of.o +obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o # transceiver drivers, keep the list sorted diff --git a/drivers/usb/phy/otg-wakeupsource.c b/drivers/usb/phy/otg-wakeupsource.c new file mode 100644 index 0000000..7c838d1 --- /dev/null +++ b/drivers/usb/phy/otg-wakeupsource.c @@ -0,0 +1,144 @@ +/* + * otg-wakeupsource.c + * + * Copyright (C) 2011 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include +#include +#include + +bool enabled = false; + +static DEFINE_SPINLOCK(otgws_spinlock); +static struct usb_phy *get_phy_hook(void); + +/* Only one lock, but since these 2 fields are associated with each other... */ + +struct otgws_lock { + char name[40]; + struct wakeup_source wsource; +}; + +/* VBUS present lock */ + +static struct otgws_lock vbus_lock; + +static void otgws_handle_event(unsigned long event) +{ + unsigned long irqflags; + + spin_lock_irqsave(&otgws_spinlock, irqflags); + + if (!enabled) { + __pm_relax(&vbus_lock.wsource); + spin_unlock_irqrestore(&otgws_spinlock, irqflags); + return; + } + + switch (event) { + case USB_EVENT_VBUS: + case USB_EVENT_ENUMERATED: + __pm_stay_awake(&vbus_lock.wsource); + break; + + case USB_EVENT_NONE: + case USB_EVENT_ID: + case USB_EVENT_CHARGER: + __pm_relax(&vbus_lock.wsource); + break; + + default: + break; + } + + spin_unlock_irqrestore(&otgws_spinlock, irqflags); +} +static struct usb_phy *get_phy_hook(void) +{ + struct usb_phy *phy; + + phy = usb_get_phy(USB_PHY_TYPE_USB2); + + if (IS_ERR(phy)) { + pr_err("%s: No OTG transceiver found\n", __func__); + return NULL; + } + + return phy; +} +static int otgws_otg_notifications(struct notifier_block *nb, + unsigned long event, void *unused) +{ + otgws_handle_event(event); + return NOTIFY_OK; +} + +static int set_enabled(const char *val, const struct kernel_param *kp) +{ + int rv = param_set_bool(val, kp); + static struct usb_phy *otgws_xceiv; + + if (rv) + return rv; + + otgws_xceiv = get_phy_hook(); + + if (otgws_xceiv) + otgws_handle_event(otgws_xceiv->last_event); + + return 0; +} + +static struct kernel_param_ops enabled_param_ops = { + .set = set_enabled, + .get = param_get_bool, +}; + +module_param_cb(enabled, &enabled_param_ops, &enabled, 0644); +MODULE_PARM_DESC(enabled, "Hold wakeupsource when VBUS present"); + +static int __init otg_wakeupsource_init(void) +{ + int ret; + static struct notifier_block otgws_nb; + static struct usb_phy *otgws_xceiv; + + otgws_xceiv = get_phy_hook(); + + if (NULL == otgws_xceiv) + return PTR_ERR(otgws_xceiv); + + snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s", + dev_name(otgws_xceiv->dev)); + wakeup_source_init(&vbus_lock.wsource, vbus_lock.name); + + otgws_nb.notifier_call = otgws_otg_notifications; + ret = usb_register_notifier(otgws_xceiv, &otgws_nb); + + if (ret) { + pr_err("%s: usb_register_notifier on transceiver %s failed\n", + __func__, dev_name(otgws_xceiv->dev)); + otgws_xceiv = NULL; + wakeup_source_trash(&vbus_lock.wsource); + return ret; + } + + return 0; +} + +late_initcall(otg_wakeupsource_init);