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_try_get_ops() - Get a ref to the tpm_chip
43 * The caller must already have some kind of locking to ensure that chip is
44 * valid. This function will lock the chip so that the ops member can be
45 * accessed safely. The locking prevents tpm_chip_unregister from
46 * completing, so it should not be held for long periods.
48 * Returns -ERRNO if the chip could not be got.
50 int tpm_try_get_ops(struct tpm_chip
*chip
)
54 get_device(&chip
->dev
);
56 down_read(&chip
->ops_sem
);
60 if (!try_module_get(chip
->dev
.parent
->driver
->owner
))
65 up_read(&chip
->ops_sem
);
66 put_device(&chip
->dev
);
69 EXPORT_SYMBOL_GPL(tpm_try_get_ops
);
72 * tpm_put_ops() - Release a ref to the tpm_chip
75 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
78 void tpm_put_ops(struct tpm_chip
*chip
)
80 module_put(chip
->dev
.parent
->driver
->owner
);
81 up_read(&chip
->ops_sem
);
82 put_device(&chip
->dev
);
84 EXPORT_SYMBOL_GPL(tpm_put_ops
);
87 * tpm_chip_find_get() - return tpm_chip for a given chip number
88 * @chip_num: id to find
90 * The return'd chip has been tpm_try_get_ops'd and must be released via
93 struct tpm_chip
*tpm_chip_find_get(int chip_num
)
95 struct tpm_chip
*pos
, *chip
= NULL
;
98 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
99 if (chip_num
!= TPM_ANY_NUM
&& chip_num
!= pos
->dev_num
)
102 /* rcu prevents chip from being free'd */
103 if (!tpm_try_get_ops(pos
))
112 * tpm_dev_release() - free chip memory and the device number
113 * @dev: the character device for the TPM chip
115 * This is used as the release function for the character device.
117 static void tpm_dev_release(struct device
*dev
)
119 struct tpm_chip
*chip
= container_of(dev
, struct tpm_chip
, dev
);
121 spin_lock(&driver_lock
);
122 clear_bit(chip
->dev_num
, dev_mask
);
123 spin_unlock(&driver_lock
);
128 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
129 * @dev: device to which the chip is associated
130 * @ops: struct tpm_class_ops instance
132 * Allocates a new struct tpm_chip instance and assigns a free
133 * device number for it. Caller does not have to worry about
134 * freeing the allocated resources. When the devices is removed
135 * devres calls tpmm_chip_remove() to do the job.
137 struct tpm_chip
*tpmm_chip_alloc(struct device
*dev
,
138 const struct tpm_class_ops
*ops
)
140 struct tpm_chip
*chip
;
143 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
145 return ERR_PTR(-ENOMEM
);
147 mutex_init(&chip
->tpm_mutex
);
148 init_rwsem(&chip
->ops_sem
);
149 INIT_LIST_HEAD(&chip
->list
);
153 spin_lock(&driver_lock
);
154 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
155 spin_unlock(&driver_lock
);
157 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
158 dev_err(dev
, "No available tpm device numbers\n");
160 return ERR_PTR(-ENOMEM
);
163 set_bit(chip
->dev_num
, dev_mask
);
165 device_initialize(&chip
->dev
);
167 dev_set_drvdata(dev
, chip
);
169 chip
->dev
.class = tpm_class
;
170 chip
->dev
.release
= tpm_dev_release
;
171 chip
->dev
.parent
= dev
;
173 chip
->dev
.groups
= chip
->groups
;
176 if (chip
->dev_num
== 0)
177 chip
->dev
.devt
= MKDEV(MISC_MAJOR
, TPM_MINOR
);
179 chip
->dev
.devt
= MKDEV(MAJOR(tpm_devt
), chip
->dev_num
);
181 rc
= dev_set_name(&chip
->dev
, "tpm%d", chip
->dev_num
);
185 cdev_init(&chip
->cdev
, &tpm_fops
);
186 chip
->cdev
.owner
= dev
->driver
->owner
;
187 chip
->cdev
.kobj
.parent
= &chip
->dev
.kobj
;
189 rc
= devm_add_action(dev
, (void (*)(void *)) put_device
, &chip
->dev
);
191 put_device(&chip
->dev
);
198 put_device(&chip
->dev
);
201 EXPORT_SYMBOL_GPL(tpmm_chip_alloc
);
203 static int tpm_add_char_device(struct tpm_chip
*chip
)
207 rc
= cdev_add(&chip
->cdev
, chip
->dev
.devt
, 1);
210 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
211 dev_name(&chip
->dev
), MAJOR(chip
->dev
.devt
),
212 MINOR(chip
->dev
.devt
), rc
);
217 rc
= device_add(&chip
->dev
);
220 "unable to device_register() %s, major %d, minor %d, err=%d\n",
221 dev_name(&chip
->dev
), MAJOR(chip
->dev
.devt
),
222 MINOR(chip
->dev
.devt
), rc
);
224 cdev_del(&chip
->cdev
);
231 static void tpm_del_char_device(struct tpm_chip
*chip
)
233 cdev_del(&chip
->cdev
);
235 /* Make the driver uncallable. */
236 down_write(&chip
->ops_sem
);
238 up_write(&chip
->ops_sem
);
240 device_del(&chip
->dev
);
243 static int tpm1_chip_register(struct tpm_chip
*chip
)
247 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
250 rc
= tpm_sysfs_add_device(chip
);
254 chip
->bios_dir
= tpm_bios_log_setup(dev_name(&chip
->dev
));
259 static void tpm1_chip_unregister(struct tpm_chip
*chip
)
261 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
265 tpm_bios_log_teardown(chip
->bios_dir
);
267 tpm_sysfs_del_device(chip
);
271 * tpm_chip_register() - create a character device for the TPM chip
272 * @chip: TPM chip to use.
274 * Creates a character device for the TPM chip and adds sysfs attributes for
275 * the device. As the last step this function adds the chip to the list of TPM
276 * chips available for in-kernel use.
278 * This function should be only called after the chip initialization is
281 int tpm_chip_register(struct tpm_chip
*chip
)
285 rc
= tpm1_chip_register(chip
);
291 rc
= tpm_add_char_device(chip
);
295 /* Make the chip available. */
296 spin_lock(&driver_lock
);
297 list_add_tail_rcu(&chip
->list
, &tpm_chip_list
);
298 spin_unlock(&driver_lock
);
300 chip
->flags
|= TPM_CHIP_FLAG_REGISTERED
;
302 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
303 rc
= __compat_only_sysfs_link_entry_to_kobj(
304 &chip
->dev
.parent
->kobj
, &chip
->dev
.kobj
, "ppi");
305 if (rc
&& rc
!= -ENOENT
) {
306 tpm_chip_unregister(chip
);
313 tpm1_chip_unregister(chip
);
316 EXPORT_SYMBOL_GPL(tpm_chip_register
);
319 * tpm_chip_unregister() - release the TPM driver
320 * @chip: TPM chip to use.
322 * Takes the chip first away from the list of available TPM chips and then
323 * cleans up all the resources reserved by tpm_chip_register().
325 * Once this function returns the driver call backs in 'op's will not be
326 * running and will no longer start.
328 * NOTE: This function should be only called before deinitializing chip
331 void tpm_chip_unregister(struct tpm_chip
*chip
)
333 if (!(chip
->flags
& TPM_CHIP_FLAG_REGISTERED
))
336 spin_lock(&driver_lock
);
337 list_del_rcu(&chip
->list
);
338 spin_unlock(&driver_lock
);
341 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
342 sysfs_remove_link(&chip
->dev
.parent
->kobj
, "ppi");
344 tpm1_chip_unregister(chip
);
345 tpm_del_char_device(chip
);
347 EXPORT_SYMBOL_GPL(tpm_chip_unregister
);
This page took 0.038437 seconds and 5 git commands to generate.