From patchwork Fri Jul 11 07:08:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 4531001 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 04C979F4F5 for ; Fri, 11 Jul 2014 07:08:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E2E532015E for ; Fri, 11 Jul 2014 07:08:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D8032201D5 for ; Fri, 11 Jul 2014 07:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751420AbaGKHIb (ORCPT ); Fri, 11 Jul 2014 03:08:31 -0400 Received: from mail-wg0-f42.google.com ([74.125.82.42]:34436 "EHLO mail-wg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751154AbaGKHIb (ORCPT ); Fri, 11 Jul 2014 03:08:31 -0400 Received: by mail-wg0-f42.google.com with SMTP id l18so583677wgh.25 for ; Fri, 11 Jul 2014 00:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=rRjQgSock42BBfZ6RHvwTb+YpVxwtOCnM3BoGdEdlSc=; b=mQgP0piJxCgUkrd5xzykibcM1Tafy7Milx1z4Ou/tIQvbR1nG/Bjn9SmXZpQ4BRJPL rb78Hw2y+Q0/rnaQrz4UuOGV3IfI4FotsZfIILIlHPqDHch8alqmyk1dUTRHojh9JGvU v4lCNP9dZy4H0rldzva8oB4M5WyVes1p7t+b47GiegxnuUbg+5IEF0PCMEfhy+LGtG4Q iCEzUa25+dCCBgf5etVcrwmfQQd1m/WBCvI5WUqeUPMVQQ66hU72ySA4Jchjwkkv9OvK 8AxuqU6/J8q/l9uDsNMhC/K+4EIcVY6wKTtYxEbPXr9BLlSNqeSXBOgMKd4/9qmzeO0T G4ng== X-Received: by 10.180.188.50 with SMTP id fx18mr2656364wic.76.1405062507206; Fri, 11 Jul 2014 00:08:27 -0700 (PDT) Received: from localhost (port-3164.pppoe.wtnet.de. [84.46.12.104]) by mx.google.com with ESMTPSA id pq9sm3250377wjc.35.2014.07.11.00.08.26 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Jul 2014 00:08:26 -0700 (PDT) From: Thierry Reding To: Bjorn Helgaas Cc: Stephen Warren , linux-pci@vger.kernel.org, linux-tegra@vger.kernel.org Subject: [PATCH 1/2] resource: Add device-managed request/release_resource() Date: Fri, 11 Jul 2014 09:08:24 +0200 Message-Id: <1405062505-2606-1-git-send-email-thierry.reding@gmail.com> X-Mailer: git-send-email 2.0.1 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Thierry Reding Provide device-managed implementations of the request_resource() and release_resource() functions. Upon failure to request a resource, the new devm_request_resource() function will output an error message for consistent error reporting. Signed-off-by: Thierry Reding --- Documentation/driver-model/devres.txt | 2 + include/linux/ioport.h | 5 +++ kernel/resource.c | 73 +++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 034d32b00846..b466f3d19bcb 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -252,6 +252,8 @@ IIO devm_iio_device_unregister() IO region + devm_request_resource() + devm_release_resource() devm_request_region() devm_request_mem_region() devm_release_region() diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 142ec544167c..2c5250222278 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -215,6 +215,11 @@ static inline int __deprecated check_region(resource_size_t s, /* Wrappers for managed devices */ struct device; + +extern int devm_request_resource(struct device *dev, struct resource *root, + struct resource *new); +extern void devm_release_resource(struct device *dev, struct resource *new); + #define devm_request_region(dev,start,n,name) \ __devm_request_region(dev, &ioport_resource, (start), (n), (name)) #define devm_request_mem_region(dev,start,n,name) \ diff --git a/kernel/resource.c b/kernel/resource.c index da14b8d09296..39d99aa401a6 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1248,6 +1248,79 @@ int release_mem_region_adjustable(struct resource *parent, /* * Managed region resource */ +static void devm_resource_release(struct device *dev, void *ptr) +{ + struct resource **r = ptr; + + release_resource(*r); +} + +/** + * devm_request_resource() - request and reserve an I/O or memory resource + * @dev: device for which to request the resource + * @root: root of the resource tree from which to request the resource + * @new: descriptor of the resource to request + * + * This is a device-managed version of request_resource(). There is usually + * no need to release resources requested by this function explicitly since + * that will be taken care of when the device is unbound from its driver. + * If for some reason the resource needs to be released explicitly, because + * of ordering issues for example, drivers must call devm_release_resource() + * rather than the regular release_resource(). + * + * When a conflict is detected between any existing resources and the newly + * requested resource, an error message will be printed. + * + * Returns 0 on success or a negative error code on failure. + */ +int devm_request_resource(struct device *dev, struct resource *root, + struct resource *new) +{ + struct resource *conflict, **ptr; + + ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + *ptr = new; + + conflict = request_resource_conflict(root, new); + if (!conflict) { + devres_add(dev, ptr); + return 0; + } + + dev_err(dev, "resource collision: %pR conflicts with %s %pR\n", new, + conflict->name, conflict); + devres_free(ptr); + return -EBUSY; +} +EXPORT_SYMBOL(devm_request_resource); + +static int devm_resource_match(struct device *dev, void *res, void *data) +{ + struct resource **ptr = res; + + if (WARN_ON(!ptr || !*ptr)) + return 0; + + return *ptr == data; +} + +/** + * devm_release_resource() - release a previously requested resource + * @dev: device for which to release the resource + * @new: descriptor of the resource to release + * + * Releases a resource previously requested using devm_request_resource(). + */ +void devm_release_resource(struct device *dev, struct resource *new) +{ + WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match, + new)); +} +EXPORT_SYMBOL(devm_release_resource); + struct region_devres { struct resource *parent; resource_size_t start;