2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
6 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Dave Safford <safford@watson.ibm.com>
9 * Reiner Sailer <sailer@watson.ibm.com>
10 * Kylene Hall <kjhall@us.ibm.com>
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14 * TPM chip management routines.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <linux/freezer.h>
29 #include "tpm_eventlog.h"
31 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
32 static LIST_HEAD(tpm_chip_list);
33 static DEFINE_SPINLOCK(driver_lock);
36 * tpm_chip_find_get - return tpm_chip for a given chip number
37 * @chip_num the device number for the chip
39 struct tpm_chip *tpm_chip_find_get(int chip_num)
41 struct tpm_chip *pos, *chip = NULL;
44 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
45 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
48 if (try_module_get(pos->dev->driver->owner)) {
58 * tpmm_chip_remove() - free chip memory and device number
59 * @data: points to struct tpm_chip instance
61 * This is used internally by tpmm_chip_alloc() and called by devres
62 * when the device is released. This function does the opposite of
63 * tpmm_chip_alloc() freeing memory and the device number.
65 static void tpmm_chip_remove(void *data)
67 struct tpm_chip *chip = (struct tpm_chip *) data;
69 spin_lock(&driver_lock);
70 clear_bit(chip->dev_num, dev_mask);
71 spin_unlock(&driver_lock);
76 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
77 * @dev: device to which the chip is associated
78 * @ops: struct tpm_class_ops instance
80 * Allocates a new struct tpm_chip instance and assigns a free
81 * device number for it. Caller does not have to worry about
82 * freeing the allocated resources. When the devices is removed
83 * devres calls tpmm_chip_remove() to do the job.
85 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
86 const struct tpm_class_ops *ops)
88 struct tpm_chip *chip;
90 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
94 mutex_init(&chip->tpm_mutex);
95 INIT_LIST_HEAD(&chip->list);
99 spin_lock(&driver_lock);
100 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
101 spin_unlock(&driver_lock);
103 if (chip->dev_num >= TPM_NUM_DEVICES) {
104 dev_err(dev, "No available tpm device numbers\n");
106 return ERR_PTR(-ENOMEM);
109 set_bit(chip->dev_num, dev_mask);
111 scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
114 devm_add_action(dev, tpmm_chip_remove, chip);
115 dev_set_drvdata(dev, chip);
119 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
122 * tpm_chip_register() - create a misc driver for the TPM chip
123 * @chip: TPM chip to use.
125 * Creates a misc driver for the TPM chip and adds sysfs interfaces for
126 * the device, PPI and TCPA. As the last step this function adds the
127 * chip to the list of TPM chips available for use.
129 * NOTE: This function should be only called after the chip initialization
132 * Called from tpm_<specific>.c probe function only for devices
133 * the driver has determined it should claim. Prior to calling
134 * this function the specific probe function has called pci_enable_device
135 * upon errant exit from this function specific probe function should call
138 int tpm_chip_register(struct tpm_chip *chip)
142 rc = tpm_dev_add_device(chip);
146 rc = tpm_sysfs_add_device(chip);
150 rc = tpm_add_ppi(chip);
154 chip->bios_dir = tpm_bios_log_setup(chip->devname);
156 /* Make the chip available. */
157 spin_lock(&driver_lock);
158 list_add_rcu(&chip->list, &tpm_chip_list);
159 spin_unlock(&driver_lock);
161 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
165 tpm_sysfs_del_device(chip);
167 tpm_dev_del_device(chip);
170 EXPORT_SYMBOL_GPL(tpm_chip_register);
173 * tpm_chip_unregister() - release the TPM driver
174 * @chip: TPM chip to use.
176 * Takes the chip first away from the list of available TPM chips and then
177 * cleans up all the resources reserved by tpm_chip_register().
179 * NOTE: This function should be only called before deinitializing chip
182 void tpm_chip_unregister(struct tpm_chip *chip)
184 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
187 spin_lock(&driver_lock);
188 list_del_rcu(&chip->list);
189 spin_unlock(&driver_lock);
193 tpm_bios_log_teardown(chip->bios_dir);
194 tpm_remove_ppi(chip);
195 tpm_sysfs_del_device(chip);
197 tpm_dev_del_device(chip);
199 EXPORT_SYMBOL_GPL(tpm_chip_unregister);