From patchwork Fri Feb 19 12:42:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 585188 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A688614033B for ; Fri, 19 Feb 2016 23:42:33 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aWkOC-00074X-PG; Fri, 19 Feb 2016 12:42:28 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1aWkOB-000740-T3 for tpmdd-devel@lists.sourceforge.net; Fri, 19 Feb 2016 12:42:27 +0000 X-ACL-Warn: Received: from e35.co.us.ibm.com ([32.97.110.153]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1aWkO7-0005Pp-SX for tpmdd-devel@lists.sourceforge.net; Fri, 19 Feb 2016 12:42:27 +0000 Received: from localhost by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 19 Feb 2016 05:42:18 -0700 Received: from d03dlp01.boulder.ibm.com (9.17.202.177) by e35.co.us.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 19 Feb 2016 05:42:15 -0700 X-IBM-Helo: d03dlp01.boulder.ibm.com X-IBM-MailFrom: stefanb@linux.vnet.ibm.com X-IBM-RcptTo: tpmdd-devel@lists.sourceforge.net Received: from b03cxnp08028.gho.boulder.ibm.com (b03cxnp08028.gho.boulder.ibm.com [9.17.130.20]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id AB4E61FF0021 for ; Fri, 19 Feb 2016 05:30:24 -0700 (MST) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u1JCgE0U25952500 for ; Fri, 19 Feb 2016 05:42:14 -0700 Received: from d03av03.boulder.ibm.com (localhost [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u1JCgEYP029132 for ; Fri, 19 Feb 2016 05:42:14 -0700 Received: from dhcp-9-2-140-43.watson.ibm.com (dhcp-9-2-140-28.watson.ibm.com [9.2.140.28]) by d03av03.boulder.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u1JCgALJ028880; Fri, 19 Feb 2016 05:42:14 -0700 From: Stefan Berger To: tpmdd-devel@lists.sourceforge.net Date: Fri, 19 Feb 2016 07:42:03 -0500 Message-Id: <1455885728-10315-7-git-send-email-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1455885728-10315-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1455885728-10315-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16021912-0013-0000-0000-00001D051922 X-Spam-Score: -0.0 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain X-Headers-End: 1aWkO7-0005Pp-SX Subject: [tpmdd-devel] [PATCH v3 06/11] tpm: Split out the devm stuff from tpmm_chip_alloc X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net From: Jason Gunthorpe tpm_chip_alloc becomes a typical subsystem allocate call. Signed-off-by: Jason Gunthorpe --- drivers/char/tpm/tpm-chip.c | 57 +++++++++++++++++++++++++++++++-------------- drivers/char/tpm/tpm.h | 4 +++- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index fe15637..2270e47 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -120,17 +120,17 @@ static void tpm_dev_release(struct device *dev) } /** - * tpmm_chip_alloc() - allocate a new struct tpm_chip instance - * @dev: device to which the chip is associated + * tpm_chip_alloc() - allocate a new struct tpm_chip instance + * @pdev: device to which the chip is associated + * At this point pdev mst be initialized, but does not have to + * be registered * @ops: struct tpm_class_ops instance * * Allocates a new struct tpm_chip instance and assigns a free - * device number for it. Caller does not have to worry about - * freeing the allocated resources. When the devices is removed - * devres calls tpmm_chip_remove() to do the job. + * device number for it. Must be paired with put_device(&chip->dev). */ -struct tpm_chip *tpmm_chip_alloc(struct device *dev, - const struct tpm_class_ops *ops) +struct tpm_chip *tpm_chip_alloc(struct device *dev, + const struct tpm_class_ops *ops) { struct tpm_chip *chip; int err; @@ -143,10 +143,10 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev, mutex_init(&chip->tpm_mutex); INIT_LIST_HEAD(&chip->list); - chip->ops = ops; - spin_lock(&driver_lock); chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); + if (chip->dev_num < TPM_NUM_DEVICES) + set_bit(chip->dev_num, dev_mask); spin_unlock(&driver_lock); if (chip->dev_num >= TPM_NUM_DEVICES) { @@ -155,9 +155,9 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev, return ERR_PTR(-ENOMEM); } - set_bit(chip->dev_num, dev_mask); + chip->ops = ops; - dev_set_drvdata(dev, chip); + device_initialize(&chip->dev); chip->dev.class = tpm_class; chip->dev.release = tpm_dev_release; @@ -175,23 +175,44 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev, if (err) goto out; - device_initialize(&chip->dev); - cdev_init(&chip->cdev, &tpm_fops); chip->cdev.owner = dev->driver->owner; chip->cdev.kobj.parent = &chip->dev.kobj; - err = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev); + return chip; + +out: + put_device(&chip->dev); + return ERR_PTR(err); +} +EXPORT_SYMBOL_GPL(tpm_chip_alloc); + +/** + * tpmm_chip_alloc() - allocate a new struct tpm_chip instance + * @pdev: parent device to which the chip is associated + * @ops: struct tpm_class_ops instance + * + * Same as tpm_chip_alloc except devm is used to do the put_device + */ +struct tpm_chip *tpmm_chip_alloc(struct device *pdev, + const struct tpm_class_ops *ops) +{ + struct tpm_chip *chip; + int err; + + chip = tpm_chip_alloc(pdev, ops); + if (IS_ERR(chip)) + return chip; + + err = devm_add_action(pdev, (void (*)(void *)) put_device, &chip->dev); if (err) { put_device(&chip->dev); return ERR_PTR(err); } - return chip; + dev_set_drvdata(pdev, chip); -out: - put_device(&chip->dev); - return ERR_PTR(err); + return chip; } EXPORT_SYMBOL_GPL(tpmm_chip_alloc); diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 2a8373e..25efe8f 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -515,7 +515,9 @@ struct tpm_chip *tpm_chip_find_get(int chip_num); __must_check int tpm_try_get_ops(struct tpm_chip *chip); void tpm_put_ops(struct tpm_chip *chip); -extern struct tpm_chip *tpmm_chip_alloc(struct device *dev, +extern struct tpm_chip *tpm_chip_alloc(struct device *dev, + const struct tpm_class_ops *ops); +extern struct tpm_chip *tpmm_chip_alloc(struct device *pdev, const struct tpm_class_ops *ops); extern int tpm_chip_register(struct tpm_chip *chip); extern void tpm_chip_unregister(struct tpm_chip *chip);