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
;
93 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
95 return ERR_PTR(-ENOMEM
);
97 mutex_init(&chip
->tpm_mutex
);
98 INIT_LIST_HEAD(&chip
->list
);
102 spin_lock(&driver_lock
);
103 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
104 spin_unlock(&driver_lock
);
106 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
107 dev_err(dev
, "No available tpm device numbers\n");
109 return ERR_PTR(-ENOMEM
);
112 set_bit(chip
->dev_num
, dev_mask
);
114 scnprintf(chip
->devname
, sizeof(chip
->devname
), "tpm%d", chip
->dev_num
);
118 dev_set_drvdata(dev
, chip
);
120 chip
->dev
.class = tpm_class
;
121 chip
->dev
.release
= tpm_dev_release
;
122 chip
->dev
.parent
= chip
->pdev
;
124 chip
->dev
.groups
= chip
->groups
;
127 if (chip
->dev_num
== 0)
128 chip
->dev
.devt
= MKDEV(MISC_MAJOR
, TPM_MINOR
);
130 chip
->dev
.devt
= MKDEV(MAJOR(tpm_devt
), chip
->dev_num
);
132 dev_set_name(&chip
->dev
, "%s", chip
->devname
);
134 device_initialize(&chip
->dev
);
136 cdev_init(&chip
->cdev
, &tpm_fops
);
137 chip
->cdev
.owner
= chip
->pdev
->driver
->owner
;
138 chip
->cdev
.kobj
.parent
= &chip
->dev
.kobj
;
140 rc
= devm_add_action(dev
, (void (*)(void *)) put_device
, &chip
->dev
);
142 put_device(&chip
->dev
);
148 EXPORT_SYMBOL_GPL(tpmm_chip_alloc
);
150 static int tpm_add_char_device(struct tpm_chip
*chip
)
154 rc
= cdev_add(&chip
->cdev
, chip
->dev
.devt
, 1);
157 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
158 chip
->devname
, MAJOR(chip
->dev
.devt
),
159 MINOR(chip
->dev
.devt
), rc
);
164 rc
= device_add(&chip
->dev
);
167 "unable to device_register() %s, major %d, minor %d, err=%d\n",
168 chip
->devname
, MAJOR(chip
->dev
.devt
),
169 MINOR(chip
->dev
.devt
), rc
);
171 cdev_del(&chip
->cdev
);
178 static void tpm_del_char_device(struct tpm_chip
*chip
)
180 cdev_del(&chip
->cdev
);
181 device_del(&chip
->dev
);
184 static int tpm1_chip_register(struct tpm_chip
*chip
)
188 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
191 rc
= tpm_sysfs_add_device(chip
);
195 chip
->bios_dir
= tpm_bios_log_setup(chip
->devname
);
200 static void tpm1_chip_unregister(struct tpm_chip
*chip
)
202 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
206 tpm_bios_log_teardown(chip
->bios_dir
);
208 tpm_sysfs_del_device(chip
);
212 * tpm_chip_register() - create a character device for the TPM chip
213 * @chip: TPM chip to use.
215 * Creates a character device for the TPM chip and adds sysfs attributes for
216 * the device. As the last step this function adds the chip to the list of TPM
217 * chips available for in-kernel use.
219 * This function should be only called after the chip initialization is
222 int tpm_chip_register(struct tpm_chip
*chip
)
226 rc
= tpm1_chip_register(chip
);
232 rc
= tpm_add_char_device(chip
);
236 /* Make the chip available. */
237 spin_lock(&driver_lock
);
238 list_add_tail_rcu(&chip
->list
, &tpm_chip_list
);
239 spin_unlock(&driver_lock
);
241 chip
->flags
|= TPM_CHIP_FLAG_REGISTERED
;
243 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
244 rc
= __compat_only_sysfs_link_entry_to_kobj(&chip
->pdev
->kobj
,
247 if (rc
&& rc
!= -ENOENT
) {
248 tpm_chip_unregister(chip
);
255 tpm1_chip_unregister(chip
);
258 EXPORT_SYMBOL_GPL(tpm_chip_register
);
261 * tpm_chip_unregister() - release the TPM driver
262 * @chip: TPM chip to use.
264 * Takes the chip first away from the list of available TPM chips and then
265 * cleans up all the resources reserved by tpm_chip_register().
267 * NOTE: This function should be only called before deinitializing chip
270 void tpm_chip_unregister(struct tpm_chip
*chip
)
272 if (!(chip
->flags
& TPM_CHIP_FLAG_REGISTERED
))
275 spin_lock(&driver_lock
);
276 list_del_rcu(&chip
->list
);
277 spin_unlock(&driver_lock
);
280 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
281 sysfs_remove_link(&chip
->pdev
->kobj
, "ppi");
283 tpm1_chip_unregister(chip
);
284 tpm_del_char_device(chip
);
286 EXPORT_SYMBOL_GPL(tpm_chip_unregister
);