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 DEFINE_IDR(dev_nums_idr);
33 static DEFINE_MUTEX(idr_lock);
35 struct class *tpm_class;
39 * tpm_try_get_ops() - Get a ref to the tpm_chip
42 * The caller must already have some kind of locking to ensure that chip is
43 * valid. This function will lock the chip so that the ops member can be
44 * accessed safely. The locking prevents tpm_chip_unregister from
45 * completing, so it should not be held for long periods.
47 * Returns -ERRNO if the chip could not be got.
49 int tpm_try_get_ops(struct tpm_chip *chip)
53 get_device(&chip->dev);
55 down_read(&chip->ops_sem);
61 up_read(&chip->ops_sem);
62 put_device(&chip->dev);
65 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
68 * tpm_put_ops() - Release a ref to the tpm_chip
71 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
74 void tpm_put_ops(struct tpm_chip *chip)
76 up_read(&chip->ops_sem);
77 put_device(&chip->dev);
79 EXPORT_SYMBOL_GPL(tpm_put_ops);
82 * tpm_chip_find_get() - return tpm_chip for a given chip number
83 * @chip_num: id to find
85 * The return'd chip has been tpm_try_get_ops'd and must be released via
88 struct tpm_chip *tpm_chip_find_get(int chip_num)
90 struct tpm_chip *chip, *res = NULL;
93 mutex_lock(&idr_lock);
95 if (chip_num == TPM_ANY_NUM) {
99 chip = idr_get_next(&dev_nums_idr, &chip_num);
100 if (chip && !tpm_try_get_ops(chip)) {
104 } while (chip_prev != chip_num);
106 chip = idr_find_slowpath(&dev_nums_idr, chip_num);
107 if (chip && !tpm_try_get_ops(chip))
111 mutex_unlock(&idr_lock);
117 * tpm_dev_release() - free chip memory and the device number
118 * @dev: the character device for the TPM chip
120 * This is used as the release function for the character device.
122 static void tpm_dev_release(struct device *dev)
124 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
126 mutex_lock(&idr_lock);
127 idr_remove(&dev_nums_idr, chip->dev_num);
128 mutex_unlock(&idr_lock);
134 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
135 * @pdev: device to which the chip is associated
136 * At this point pdev mst be initialized, but does not have to
138 * @ops: struct tpm_class_ops instance
140 * Allocates a new struct tpm_chip instance and assigns a free
141 * device number for it. Must be paired with put_device(&chip->dev).
143 struct tpm_chip *tpm_chip_alloc(struct device *dev,
144 const struct tpm_class_ops *ops)
146 struct tpm_chip *chip;
149 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
151 return ERR_PTR(-ENOMEM);
153 mutex_init(&chip->tpm_mutex);
154 init_rwsem(&chip->ops_sem);
158 mutex_lock(&idr_lock);
159 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
160 mutex_unlock(&idr_lock);
162 dev_err(dev, "No available tpm device numbers\n");
168 device_initialize(&chip->dev);
170 chip->dev.class = tpm_class;
171 chip->dev.release = tpm_dev_release;
172 chip->dev.parent = dev;
174 chip->dev.groups = chip->groups;
177 if (chip->dev_num == 0)
178 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
180 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
182 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
186 cdev_init(&chip->cdev, &tpm_fops);
187 chip->cdev.owner = THIS_MODULE;
188 chip->cdev.kobj.parent = &chip->dev.kobj;
193 put_device(&chip->dev);
196 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
199 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
200 * @pdev: parent device to which the chip is associated
201 * @ops: struct tpm_class_ops instance
203 * Same as tpm_chip_alloc except devm is used to do the put_device
205 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
206 const struct tpm_class_ops *ops)
208 struct tpm_chip *chip;
211 chip = tpm_chip_alloc(pdev, ops);
215 rc = devm_add_action(pdev, (void (*)(void *)) put_device, &chip->dev);
217 put_device(&chip->dev);
221 dev_set_drvdata(pdev, chip);
225 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
227 static int tpm_add_char_device(struct tpm_chip *chip)
231 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
234 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
235 dev_name(&chip->dev), MAJOR(chip->dev.devt),
236 MINOR(chip->dev.devt), rc);
241 rc = device_add(&chip->dev);
244 "unable to device_register() %s, major %d, minor %d, err=%d\n",
245 dev_name(&chip->dev), MAJOR(chip->dev.devt),
246 MINOR(chip->dev.devt), rc);
248 cdev_del(&chip->cdev);
252 /* Make the chip available. */
253 mutex_lock(&idr_lock);
254 idr_replace(&dev_nums_idr, chip, chip->dev_num);
255 mutex_unlock(&idr_lock);
260 static void tpm_del_char_device(struct tpm_chip *chip)
262 cdev_del(&chip->cdev);
263 device_del(&chip->dev);
265 /* Make the chip unavailable. */
266 mutex_lock(&idr_lock);
267 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
268 mutex_unlock(&idr_lock);
270 /* Make the driver uncallable. */
271 down_write(&chip->ops_sem);
272 if (chip->flags & TPM_CHIP_FLAG_TPM2)
273 tpm2_shutdown(chip, TPM2_SU_CLEAR);
275 up_write(&chip->ops_sem);
278 static int tpm1_chip_register(struct tpm_chip *chip)
282 if (chip->flags & TPM_CHIP_FLAG_TPM2)
285 rc = tpm_sysfs_add_device(chip);
289 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
294 static void tpm1_chip_unregister(struct tpm_chip *chip)
296 if (chip->flags & TPM_CHIP_FLAG_TPM2)
300 tpm_bios_log_teardown(chip->bios_dir);
302 tpm_sysfs_del_device(chip);
306 * tpm_chip_register() - create a character device for the TPM chip
307 * @chip: TPM chip to use.
309 * Creates a character device for the TPM chip and adds sysfs attributes for
310 * the device. As the last step this function adds the chip to the list of TPM
311 * chips available for in-kernel use.
313 * This function should be only called after the chip initialization is
316 int tpm_chip_register(struct tpm_chip *chip)
320 rc = tpm1_chip_register(chip);
326 rc = tpm_add_char_device(chip);
330 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
332 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
333 rc = __compat_only_sysfs_link_entry_to_kobj(
334 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
335 if (rc && rc != -ENOENT) {
336 tpm_chip_unregister(chip);
343 tpm1_chip_unregister(chip);
346 EXPORT_SYMBOL_GPL(tpm_chip_register);
349 * tpm_chip_unregister() - release the TPM driver
350 * @chip: TPM chip to use.
352 * Takes the chip first away from the list of available TPM chips and then
353 * cleans up all the resources reserved by tpm_chip_register().
355 * Once this function returns the driver call backs in 'op's will not be
356 * running and will no longer start.
358 * NOTE: This function should be only called before deinitializing chip
361 void tpm_chip_unregister(struct tpm_chip *chip)
363 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
366 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
367 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
369 tpm1_chip_unregister(chip);
370 tpm_del_char_device(chip);
372 EXPORT_SYMBOL_GPL(tpm_chip_unregister);