Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (C) 2004 IBM Corporation
  3 * Copyright (C) 2014 Intel Corporation
  4 *
  5 * Authors:
  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>
 11 *
 12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 13 *
 14 * TPM chip management routines.
 15 *
 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
 19 * License.
 20 *
 21 */
 22
 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>
 
 
 29#include "tpm.h"
 30#include "tpm_eventlog.h"
 31
 32static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
 33static LIST_HEAD(tpm_chip_list);
 34static DEFINE_SPINLOCK(driver_lock);
 35
 36struct class *tpm_class;
 
 37dev_t tpm_devt;
 38
 39/*
 40 * tpm_chip_find_get - return tpm_chip for a given chip number
 41 * @chip_num the device number for the chip
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42 */
 43struct tpm_chip *tpm_chip_find_get(int chip_num)
 44{
 45	struct tpm_chip *pos, *chip = NULL;
 
 
 46
 47	rcu_read_lock();
 48	list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
 49		if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
 50			continue;
 
 
 
 51
 52		if (try_module_get(pos->pdev->driver->owner)) {
 53			chip = pos;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 54			break;
 55		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56	}
 57	rcu_read_unlock();
 
 
 
 
 
 
 
 
 58	return chip;
 59}
 60
 61/**
 62 * tpm_dev_release() - free chip memory and the device number
 63 * @dev: the character device for the TPM chip
 64 *
 65 * This is used as the release function for the character device.
 66 */
 67static void tpm_dev_release(struct device *dev)
 68{
 69	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
 70
 71	spin_lock(&driver_lock);
 72	clear_bit(chip->dev_num, dev_mask);
 73	spin_unlock(&driver_lock);
 
 
 
 
 
 74	kfree(chip);
 75}
 76
 
 
 
 
 
 
 
 
 77/**
 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
 81 *
 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.
 86 */
 87struct tpm_chip *tpmm_chip_alloc(struct device *dev,
 88				 const struct tpm_class_ops *ops)
 89{
 90	struct tpm_chip *chip;
 91	int rc;
 92
 93	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
 94	if (chip == NULL)
 95		return ERR_PTR(-ENOMEM);
 96
 97	mutex_init(&chip->tpm_mutex);
 98	INIT_LIST_HEAD(&chip->list);
 99
100	chip->ops = ops;
101
102	spin_lock(&driver_lock);
103	chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
104	spin_unlock(&driver_lock);
105
106	if (chip->dev_num >= TPM_NUM_DEVICES) {
107		dev_err(dev, "No available tpm device numbers\n");
108		kfree(chip);
109		return ERR_PTR(-ENOMEM);
110	}
 
111
112	set_bit(chip->dev_num, dev_mask);
113
114	scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
115
116	chip->pdev = dev;
117
118	dev_set_drvdata(dev, chip);
119
120	chip->dev.class = tpm_class;
 
121	chip->dev.release = tpm_dev_release;
122	chip->dev.parent = chip->pdev;
123#ifdef CONFIG_ACPI
124	chip->dev.groups = chip->groups;
125#endif
 
 
 
 
 
 
 
 
 
 
126
127	if (chip->dev_num == 0)
128		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
129	else
130		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
131
132	dev_set_name(&chip->dev, "%s", chip->devname);
 
133
134	device_initialize(&chip->dev);
 
 
 
 
 
 
 
 
135
136	cdev_init(&chip->cdev, &tpm_fops);
137	chip->cdev.owner = chip->pdev->driver->owner;
138	chip->cdev.kobj.parent = &chip->dev.kobj;
 
 
 
 
 
 
 
 
 
 
 
 
139
140	rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
141	if (rc) {
142		put_device(&chip->dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143		return ERR_PTR(rc);
144	}
 
145
146	return chip;
147}
148EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
149
150static int tpm_add_char_device(struct tpm_chip *chip)
151{
152	int rc;
153
154	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
155	if (rc) {
156		dev_err(&chip->dev,
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);
160
161		return rc;
162	}
163
164	rc = device_add(&chip->dev);
165	if (rc) {
166		dev_err(&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);
170
171		cdev_del(&chip->cdev);
172		return rc;
173	}
174
 
 
 
 
 
175	return rc;
176}
177
178static void tpm_del_char_device(struct tpm_chip *chip)
179{
180	cdev_del(&chip->cdev);
181	device_del(&chip->dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182}
183
184static int tpm1_chip_register(struct tpm_chip *chip)
 
 
 
 
185{
 
186	int rc;
187
188	if (chip->flags & TPM_CHIP_FLAG_TPM2)
189		return 0;
190
191	rc = tpm_sysfs_add_device(chip);
192	if (rc)
 
193		return rc;
194
195	chip->bios_dir = tpm_bios_log_setup(chip->devname);
 
 
 
 
 
 
 
 
196
197	return 0;
198}
199
200static void tpm1_chip_unregister(struct tpm_chip *chip)
201{
202	if (chip->flags & TPM_CHIP_FLAG_TPM2)
203		return;
204
205	if (chip->bios_dir)
206		tpm_bios_log_teardown(chip->bios_dir);
207
208	tpm_sysfs_del_device(chip);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209}
210
211/*
212 * tpm_chip_register() - create a character device for the TPM chip
213 * @chip: TPM chip to use.
214 *
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.
218 *
219 * This function should be only called after the chip initialization is
220 * complete.
221 */
222int tpm_chip_register(struct tpm_chip *chip)
223{
224	int rc;
225
226	rc = tpm1_chip_register(chip);
 
 
 
 
 
 
 
 
 
 
227	if (rc)
228		return rc;
229
 
 
 
 
 
 
230	tpm_add_ppi(chip);
231
 
 
 
 
232	rc = tpm_add_char_device(chip);
233	if (rc)
234		goto out_err;
235
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);
240
241	chip->flags |= TPM_CHIP_FLAG_REGISTERED;
242
243	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
244		rc = __compat_only_sysfs_link_entry_to_kobj(&chip->pdev->kobj,
245							    &chip->dev.kobj,
246							    "ppi");
247		if (rc && rc != -ENOENT) {
248			tpm_chip_unregister(chip);
249			return rc;
250		}
251	}
252
253	return 0;
254out_err:
255	tpm1_chip_unregister(chip);
 
 
 
 
 
256	return rc;
257}
258EXPORT_SYMBOL_GPL(tpm_chip_register);
259
260/*
261 * tpm_chip_unregister() - release the TPM driver
262 * @chip: TPM chip to use.
263 *
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().
266 *
 
 
 
267 * NOTE: This function should be only called before deinitializing chip
268 * resources.
269 */
270void tpm_chip_unregister(struct tpm_chip *chip)
271{
272	if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
273		return;
274
275	spin_lock(&driver_lock);
276	list_del_rcu(&chip->list);
277	spin_unlock(&driver_lock);
278	synchronize_rcu();
279
280	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
281		sysfs_remove_link(&chip->pdev->kobj, "ppi");
282
283	tpm1_chip_unregister(chip);
284	tpm_del_char_device(chip);
285}
286EXPORT_SYMBOL_GPL(tpm_chip_unregister);
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2004 IBM Corporation
  4 * Copyright (C) 2014 Intel Corporation
  5 *
  6 * Authors:
  7 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  8 * Leendert van Doorn <leendert@watson.ibm.com>
  9 * Dave Safford <safford@watson.ibm.com>
 10 * Reiner Sailer <sailer@watson.ibm.com>
 11 * Kylene Hall <kjhall@us.ibm.com>
 12 *
 13 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 14 *
 15 * TPM chip management routines.
 
 
 
 
 
 
 16 */
 17
 18#include <linux/poll.h>
 19#include <linux/slab.h>
 20#include <linux/mutex.h>
 21#include <linux/spinlock.h>
 22#include <linux/freezer.h>
 23#include <linux/major.h>
 24#include <linux/tpm_eventlog.h>
 25#include <linux/hw_random.h>
 26#include "tpm.h"
 
 27
 28DEFINE_IDR(dev_nums_idr);
 29static DEFINE_MUTEX(idr_lock);
 
 30
 31struct class *tpm_class;
 32struct class *tpmrm_class;
 33dev_t tpm_devt;
 34
 35static int tpm_request_locality(struct tpm_chip *chip)
 36{
 37	int rc;
 38
 39	if (!chip->ops->request_locality)
 40		return 0;
 41
 42	rc = chip->ops->request_locality(chip, 0);
 43	if (rc < 0)
 44		return rc;
 45
 46	chip->locality = rc;
 47	return 0;
 48}
 49
 50static void tpm_relinquish_locality(struct tpm_chip *chip)
 51{
 52	int rc;
 53
 54	if (!chip->ops->relinquish_locality)
 55		return;
 56
 57	rc = chip->ops->relinquish_locality(chip, chip->locality);
 58	if (rc)
 59		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
 60
 61	chip->locality = -1;
 62}
 63
 64static int tpm_cmd_ready(struct tpm_chip *chip)
 65{
 66	if (!chip->ops->cmd_ready)
 67		return 0;
 68
 69	return chip->ops->cmd_ready(chip);
 70}
 71
 72static int tpm_go_idle(struct tpm_chip *chip)
 73{
 74	if (!chip->ops->go_idle)
 75		return 0;
 76
 77	return chip->ops->go_idle(chip);
 78}
 79
 80static void tpm_clk_enable(struct tpm_chip *chip)
 81{
 82	if (chip->ops->clk_enable)
 83		chip->ops->clk_enable(chip, true);
 84}
 85
 86static void tpm_clk_disable(struct tpm_chip *chip)
 87{
 88	if (chip->ops->clk_enable)
 89		chip->ops->clk_enable(chip, false);
 90}
 91
 92/**
 93 * tpm_chip_start() - power on the TPM
 94 * @chip:	a TPM chip to use
 95 *
 96 * Return:
 97 * * The response length	- OK
 98 * * -errno			- A system error
 99 */
100int tpm_chip_start(struct tpm_chip *chip)
101{
102	int ret;
103
104	tpm_clk_enable(chip);
105
106	if (chip->locality == -1) {
107		ret = tpm_request_locality(chip);
108		if (ret) {
109			tpm_clk_disable(chip);
110			return ret;
111		}
112	}
113
114	ret = tpm_cmd_ready(chip);
115	if (ret) {
116		tpm_relinquish_locality(chip);
117		tpm_clk_disable(chip);
118		return ret;
119	}
120
121	return 0;
122}
123EXPORT_SYMBOL_GPL(tpm_chip_start);
124
125/**
126 * tpm_chip_stop() - power off the TPM
127 * @chip:	a TPM chip to use
128 *
129 * Return:
130 * * The response length	- OK
131 * * -errno			- A system error
132 */
133void tpm_chip_stop(struct tpm_chip *chip)
134{
135	tpm_go_idle(chip);
136	tpm_relinquish_locality(chip);
137	tpm_clk_disable(chip);
138}
139EXPORT_SYMBOL_GPL(tpm_chip_stop);
140
141/**
142 * tpm_try_get_ops() - Get a ref to the tpm_chip
143 * @chip: Chip to ref
144 *
145 * The caller must already have some kind of locking to ensure that chip is
146 * valid. This function will lock the chip so that the ops member can be
147 * accessed safely. The locking prevents tpm_chip_unregister from
148 * completing, so it should not be held for long periods.
149 *
150 * Returns -ERRNO if the chip could not be got.
151 */
152int tpm_try_get_ops(struct tpm_chip *chip)
153{
154	int rc = -EIO;
155
156	get_device(&chip->dev);
157
158	down_read(&chip->ops_sem);
159	if (!chip->ops)
160		goto out_ops;
161
162	mutex_lock(&chip->tpm_mutex);
163	rc = tpm_chip_start(chip);
164	if (rc)
165		goto out_lock;
166
167	return 0;
168out_lock:
169	mutex_unlock(&chip->tpm_mutex);
170out_ops:
171	up_read(&chip->ops_sem);
172	put_device(&chip->dev);
173	return rc;
174}
175EXPORT_SYMBOL_GPL(tpm_try_get_ops);
176
177/**
178 * tpm_put_ops() - Release a ref to the tpm_chip
179 * @chip: Chip to put
180 *
181 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
182 * be kfree'd.
183 */
184void tpm_put_ops(struct tpm_chip *chip)
185{
186	tpm_chip_stop(chip);
187	mutex_unlock(&chip->tpm_mutex);
188	up_read(&chip->ops_sem);
189	put_device(&chip->dev);
190}
191EXPORT_SYMBOL_GPL(tpm_put_ops);
192
193/**
194 * tpm_default_chip() - find a TPM chip and get a reference to it
195 */
196struct tpm_chip *tpm_default_chip(void)
197{
198	struct tpm_chip *chip, *res = NULL;
199	int chip_num = 0;
200	int chip_prev;
201
202	mutex_lock(&idr_lock);
203
204	do {
205		chip_prev = chip_num;
206		chip = idr_get_next(&dev_nums_idr, &chip_num);
207		if (chip) {
208			get_device(&chip->dev);
209			res = chip;
210			break;
211		}
212	} while (chip_prev != chip_num);
213
214	mutex_unlock(&idr_lock);
215
216	return res;
217}
218EXPORT_SYMBOL_GPL(tpm_default_chip);
219
220/**
221 * tpm_find_get_ops() - find and reserve a TPM chip
222 * @chip:	a &struct tpm_chip instance, %NULL for the default chip
223 *
224 * Finds a TPM chip and reserves its class device and operations. The chip must
225 * be released with tpm_put_ops() after use.
226 * This function is for internal use only. It supports existing TPM callers
227 * by accepting NULL, but those callers should be converted to pass in a chip
228 * directly.
229 *
230 * Return:
231 * A reserved &struct tpm_chip instance.
232 * %NULL if a chip is not found.
233 * %NULL if the chip is not available.
234 */
235struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
236{
237	int rc;
238
239	if (chip) {
240		if (!tpm_try_get_ops(chip))
241			return chip;
242		return NULL;
243	}
244
245	chip = tpm_default_chip();
246	if (!chip)
247		return NULL;
248	rc = tpm_try_get_ops(chip);
249	/* release additional reference we got from tpm_default_chip() */
250	put_device(&chip->dev);
251	if (rc)
252		return NULL;
253	return chip;
254}
255
256/**
257 * tpm_dev_release() - free chip memory and the device number
258 * @dev: the character device for the TPM chip
259 *
260 * This is used as the release function for the character device.
261 */
262static void tpm_dev_release(struct device *dev)
263{
264	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
265
266	mutex_lock(&idr_lock);
267	idr_remove(&dev_nums_idr, chip->dev_num);
268	mutex_unlock(&idr_lock);
269
270	kfree(chip->log.bios_event_log);
271	kfree(chip->work_space.context_buf);
272	kfree(chip->work_space.session_buf);
273	kfree(chip->allocated_banks);
274	kfree(chip);
275}
276
277static void tpm_devs_release(struct device *dev)
278{
279	struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
280
281	/* release the master device reference */
282	put_device(&chip->dev);
283}
284
285/**
286 * tpm_class_shutdown() - prepare the TPM device for loss of power.
287 * @dev: device to which the chip is associated.
288 *
289 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
290 * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
291 *
292 * Return: always 0 (i.e. success)
293 */
294static int tpm_class_shutdown(struct device *dev)
295{
296	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
297
298	down_write(&chip->ops_sem);
299	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
300		if (!tpm_chip_start(chip)) {
301			tpm2_shutdown(chip, TPM2_SU_CLEAR);
302			tpm_chip_stop(chip);
303		}
304	}
305	chip->ops = NULL;
306	up_write(&chip->ops_sem);
307
308	return 0;
309}
310
311/**
312 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
313 * @pdev: device to which the chip is associated
314 *        At this point pdev mst be initialized, but does not have to
315 *        be registered
316 * @ops: struct tpm_class_ops instance
317 *
318 * Allocates a new struct tpm_chip instance and assigns a free
319 * device number for it. Must be paired with put_device(&chip->dev).
 
 
320 */
321struct tpm_chip *tpm_chip_alloc(struct device *pdev,
322				const struct tpm_class_ops *ops)
323{
324	struct tpm_chip *chip;
325	int rc;
326
327	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
328	if (chip == NULL)
329		return ERR_PTR(-ENOMEM);
330
331	mutex_init(&chip->tpm_mutex);
332	init_rwsem(&chip->ops_sem);
333
334	chip->ops = ops;
335
336	mutex_lock(&idr_lock);
337	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
338	mutex_unlock(&idr_lock);
339	if (rc < 0) {
340		dev_err(pdev, "No available tpm device numbers\n");
 
341		kfree(chip);
342		return ERR_PTR(rc);
343	}
344	chip->dev_num = rc;
345
346	device_initialize(&chip->dev);
347	device_initialize(&chip->devs);
 
 
 
 
 
348
349	chip->dev.class = tpm_class;
350	chip->dev.class->shutdown_pre = tpm_class_shutdown;
351	chip->dev.release = tpm_dev_release;
352	chip->dev.parent = pdev;
 
353	chip->dev.groups = chip->groups;
354
355	chip->devs.parent = pdev;
356	chip->devs.class = tpmrm_class;
357	chip->devs.release = tpm_devs_release;
358	/* get extra reference on main device to hold on
359	 * behalf of devs.  This holds the chip structure
360	 * while cdevs is in use.  The corresponding put
361	 * is in the tpm_devs_release (TPM2 only)
362	 */
363	if (chip->flags & TPM_CHIP_FLAG_TPM2)
364		get_device(&chip->dev);
365
366	if (chip->dev_num == 0)
367		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
368	else
369		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
370
371	chip->devs.devt =
372		MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
373
374	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
375	if (rc)
376		goto out;
377	rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
378	if (rc)
379		goto out;
380
381	if (!pdev)
382		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
383
384	cdev_init(&chip->cdev, &tpm_fops);
385	cdev_init(&chip->cdevs, &tpmrm_fops);
386	chip->cdev.owner = THIS_MODULE;
387	chip->cdevs.owner = THIS_MODULE;
388
389	chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
390	if (!chip->work_space.context_buf) {
391		rc = -ENOMEM;
392		goto out;
393	}
394	chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
395	if (!chip->work_space.session_buf) {
396		rc = -ENOMEM;
397		goto out;
398	}
399
400	chip->locality = -1;
401	return chip;
402
403out:
404	put_device(&chip->devs);
405	put_device(&chip->dev);
406	return ERR_PTR(rc);
407}
408EXPORT_SYMBOL_GPL(tpm_chip_alloc);
409
410/**
411 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
412 * @pdev: parent device to which the chip is associated
413 * @ops: struct tpm_class_ops instance
414 *
415 * Same as tpm_chip_alloc except devm is used to do the put_device
416 */
417struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
418				 const struct tpm_class_ops *ops)
419{
420	struct tpm_chip *chip;
421	int rc;
422
423	chip = tpm_chip_alloc(pdev, ops);
424	if (IS_ERR(chip))
425		return chip;
426
427	rc = devm_add_action_or_reset(pdev,
428				      (void (*)(void *)) put_device,
429				      &chip->dev);
430	if (rc)
431		return ERR_PTR(rc);
432
433	dev_set_drvdata(pdev, chip);
434
435	return chip;
436}
437EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
438
439static int tpm_add_char_device(struct tpm_chip *chip)
440{
441	int rc;
442
443	rc = cdev_device_add(&chip->cdev, &chip->dev);
444	if (rc) {
445		dev_err(&chip->dev,
446			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
447			dev_name(&chip->dev), MAJOR(chip->dev.devt),
448			MINOR(chip->dev.devt), rc);
 
449		return rc;
450	}
451
452	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
453		rc = cdev_device_add(&chip->cdevs, &chip->devs);
454		if (rc) {
455			dev_err(&chip->devs,
456				"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
457				dev_name(&chip->devs), MAJOR(chip->devs.devt),
458				MINOR(chip->devs.devt), rc);
459			return rc;
460		}
461	}
462
463	/* Make the chip available. */
464	mutex_lock(&idr_lock);
465	idr_replace(&dev_nums_idr, chip, chip->dev_num);
466	mutex_unlock(&idr_lock);
467
468	return rc;
469}
470
471static void tpm_del_char_device(struct tpm_chip *chip)
472{
473	cdev_device_del(&chip->cdev, &chip->dev);
474
475	/* Make the chip unavailable. */
476	mutex_lock(&idr_lock);
477	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
478	mutex_unlock(&idr_lock);
479
480	/* Make the driver uncallable. */
481	down_write(&chip->ops_sem);
482	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
483		if (!tpm_chip_start(chip)) {
484			tpm2_shutdown(chip, TPM2_SU_CLEAR);
485			tpm_chip_stop(chip);
486		}
487	}
488	chip->ops = NULL;
489	up_write(&chip->ops_sem);
490}
491
492static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
493{
494	struct attribute **i;
495
496	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
497		return;
498
499	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
500
501	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
502		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
503}
504
505/* For compatibility with legacy sysfs paths we provide symlinks from the
506 * parent dev directory to selected names within the tpm chip directory. Old
507 * kernel versions created these files directly under the parent.
508 */
509static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
510{
511	struct attribute **i;
512	int rc;
513
514	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
515		return 0;
516
517	rc = __compat_only_sysfs_link_entry_to_kobj(
518		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
519	if (rc && rc != -ENOENT)
520		return rc;
521
522	/* All the names from tpm-sysfs */
523	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
524		rc = __compat_only_sysfs_link_entry_to_kobj(
525		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
526		if (rc) {
527			tpm_del_legacy_sysfs(chip);
528			return rc;
529		}
530	}
531
532	return 0;
533}
534
535static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
536{
537	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
 
538
539	return tpm_get_random(chip, data, max);
540}
541
542static int tpm_add_hwrng(struct tpm_chip *chip)
543{
544	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
545		return 0;
546
547	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
548		 "tpm-rng-%d", chip->dev_num);
549	chip->hwrng.name = chip->hwrng_name;
550	chip->hwrng.read = tpm_hwrng_read;
551	return hwrng_register(&chip->hwrng);
552}
553
554static int tpm_get_pcr_allocation(struct tpm_chip *chip)
555{
556	int rc;
557
558	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
559	     tpm2_get_pcr_allocation(chip) :
560	     tpm1_get_pcr_allocation(chip);
561
562	if (rc > 0)
563		return -ENODEV;
564
565	return rc;
566}
567
568/*
569 * tpm_chip_register() - create a character device for the TPM chip
570 * @chip: TPM chip to use.
571 *
572 * Creates a character device for the TPM chip and adds sysfs attributes for
573 * the device. As the last step this function adds the chip to the list of TPM
574 * chips available for in-kernel use.
575 *
576 * This function should be only called after the chip initialization is
577 * complete.
578 */
579int tpm_chip_register(struct tpm_chip *chip)
580{
581	int rc;
582
583	rc = tpm_chip_start(chip);
584	if (rc)
585		return rc;
586	rc = tpm_auto_startup(chip);
587	if (rc) {
588		tpm_chip_stop(chip);
589		return rc;
590	}
591
592	rc = tpm_get_pcr_allocation(chip);
593	tpm_chip_stop(chip);
594	if (rc)
595		return rc;
596
597	tpm_sysfs_add_device(chip);
598
599	rc = tpm_bios_log_setup(chip);
600	if (rc != 0 && rc != -ENODEV)
601		return rc;
602
603	tpm_add_ppi(chip);
604
605	rc = tpm_add_hwrng(chip);
606	if (rc)
607		goto out_ppi;
608
609	rc = tpm_add_char_device(chip);
610	if (rc)
611		goto out_hwrng;
612
613	rc = tpm_add_legacy_sysfs(chip);
614	if (rc) {
615		tpm_chip_unregister(chip);
616		return rc;
 
 
 
 
 
 
 
 
 
 
 
617	}
618
619	return 0;
620
621out_hwrng:
622	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
623		hwrng_unregister(&chip->hwrng);
624out_ppi:
625	tpm_bios_log_teardown(chip);
626
627	return rc;
628}
629EXPORT_SYMBOL_GPL(tpm_chip_register);
630
631/*
632 * tpm_chip_unregister() - release the TPM driver
633 * @chip: TPM chip to use.
634 *
635 * Takes the chip first away from the list of available TPM chips and then
636 * cleans up all the resources reserved by tpm_chip_register().
637 *
638 * Once this function returns the driver call backs in 'op's will not be
639 * running and will no longer start.
640 *
641 * NOTE: This function should be only called before deinitializing chip
642 * resources.
643 */
644void tpm_chip_unregister(struct tpm_chip *chip)
645{
646	tpm_del_legacy_sysfs(chip);
647	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
648		hwrng_unregister(&chip->hwrng);
649	tpm_bios_log_teardown(chip);
650	if (chip->flags & TPM_CHIP_FLAG_TPM2)
651		cdev_device_del(&chip->cdevs, &chip->devs);
 
 
 
 
 
 
652	tpm_del_char_device(chip);
653}
654EXPORT_SYMBOL_GPL(tpm_chip_unregister);