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>
28 #include <linux/major.h>
30 #include "tpm_eventlog.h"
32 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
33 static LIST_HEAD(tpm_chip_list);
34 static DEFINE_SPINLOCK(driver_lock);
36 struct class *tpm_class;
40 * tpm_chip_find_get - return tpm_chip for a given chip number
41 * @chip_num the device number for the chip
43 struct tpm_chip *tpm_chip_find_get(int chip_num)
45 struct tpm_chip *pos, *chip = NULL;
48 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
49 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
52 if (try_module_get(pos->pdev->driver->owner)) {
62 * tpm_dev_release() - free chip memory and the device number
63 * @dev: the character device for the TPM chip
65 * This is used as the release function for the character device.
67 static void tpm_dev_release(struct device *dev)
69 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
71 spin_lock(&driver_lock);
72 clear_bit(chip->dev_num, dev_mask);
73 spin_unlock(&driver_lock);
78 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
79 * @dev: device to which the chip is associated
80 * @ops: struct tpm_class_ops instance
82 * Allocates a new struct tpm_chip instance and assigns a free
83 * device number for it. Caller does not have to worry about
84 * freeing the allocated resources. When the devices is removed
85 * devres calls tpmm_chip_remove() to do the job.
87 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
88 const struct tpm_class_ops *ops)
90 struct tpm_chip *chip;
92 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
94 return ERR_PTR(-ENOMEM);
96 mutex_init(&chip->tpm_mutex);
97 INIT_LIST_HEAD(&chip->list);
101 spin_lock(&driver_lock);
102 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
103 spin_unlock(&driver_lock);
105 if (chip->dev_num >= TPM_NUM_DEVICES) {
106 dev_err(dev, "No available tpm device numbers\n");
108 return ERR_PTR(-ENOMEM);
111 set_bit(chip->dev_num, dev_mask);
113 scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
117 dev_set_drvdata(dev, chip);
119 chip->dev.class = tpm_class;
120 chip->dev.release = tpm_dev_release;
121 chip->dev.parent = chip->pdev;
123 chip->dev.groups = chip->groups;
126 if (chip->dev_num == 0)
127 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
129 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
131 dev_set_name(&chip->dev, "%s", chip->devname);
133 device_initialize(&chip->dev);
135 cdev_init(&chip->cdev, &tpm_fops);
136 chip->cdev.owner = chip->pdev->driver->owner;
137 chip->cdev.kobj.parent = &chip->dev.kobj;
141 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
143 static int tpm_dev_add_device(struct tpm_chip *chip)
147 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
150 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
151 chip->devname, MAJOR(chip->dev.devt),
152 MINOR(chip->dev.devt), rc);
154 device_unregister(&chip->dev);
158 rc = device_add(&chip->dev);
161 "unable to device_register() %s, major %d, minor %d, err=%d\n",
162 chip->devname, MAJOR(chip->dev.devt),
163 MINOR(chip->dev.devt), rc);
171 static void tpm_dev_del_device(struct tpm_chip *chip)
173 cdev_del(&chip->cdev);
174 device_unregister(&chip->dev);
177 static int tpm1_chip_register(struct tpm_chip *chip)
181 if (chip->flags & TPM_CHIP_FLAG_TPM2)
184 rc = tpm_sysfs_add_device(chip);
188 chip->bios_dir = tpm_bios_log_setup(chip->devname);
193 static void tpm1_chip_unregister(struct tpm_chip *chip)
195 if (chip->flags & TPM_CHIP_FLAG_TPM2)
199 tpm_bios_log_teardown(chip->bios_dir);
201 tpm_sysfs_del_device(chip);
205 * tpm_chip_register() - create a character device for the TPM chip
206 * @chip: TPM chip to use.
208 * Creates a character device for the TPM chip and adds sysfs attributes for
209 * the device. As the last step this function adds the chip to the list of TPM
210 * chips available for in-kernel use.
212 * This function should be only called after the chip initialization is
215 int tpm_chip_register(struct tpm_chip *chip)
219 rc = tpm1_chip_register(chip);
225 rc = tpm_dev_add_device(chip);
229 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
230 rc = __compat_only_sysfs_link_entry_to_kobj(&chip->pdev->kobj,
237 /* Make the chip available. */
238 spin_lock(&driver_lock);
239 list_add_rcu(&chip->list, &tpm_chip_list);
240 spin_unlock(&driver_lock);
242 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
246 tpm1_chip_unregister(chip);
249 EXPORT_SYMBOL_GPL(tpm_chip_register);
252 * tpm_chip_unregister() - release the TPM driver
253 * @chip: TPM chip to use.
255 * Takes the chip first away from the list of available TPM chips and then
256 * cleans up all the resources reserved by tpm_chip_register().
258 * NOTE: This function should be only called before deinitializing chip
261 void tpm_chip_unregister(struct tpm_chip *chip)
263 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
266 spin_lock(&driver_lock);
267 list_del_rcu(&chip->list);
268 spin_unlock(&driver_lock);
271 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
272 sysfs_remove_link(&chip->pdev->kobj, "ppi");
274 tpm1_chip_unregister(chip);
275 tpm_dev_del_device(chip);
277 EXPORT_SYMBOL_GPL(tpm_chip_unregister);