sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / char / tpm / tpm-chip.c
bloba77262d31911a1dc65c227f7d237fd3c61300bea
1 /*
2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
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>
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
19 * License.
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"
32 DEFINE_IDR(dev_nums_idr);
33 static DEFINE_MUTEX(idr_lock);
35 struct class *tpm_class;
36 dev_t tpm_devt;
38 /**
39 * tpm_try_get_ops() - Get a ref to the tpm_chip
40 * @chip: Chip to ref
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)
51 int rc = -EIO;
53 get_device(&chip->dev);
55 down_read(&chip->ops_sem);
56 if (!chip->ops)
57 goto out_lock;
59 return 0;
60 out_lock:
61 up_read(&chip->ops_sem);
62 put_device(&chip->dev);
63 return rc;
65 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
67 /**
68 * tpm_put_ops() - Release a ref to the tpm_chip
69 * @chip: Chip to put
71 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
72 * be kfree'd.
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);
81 /**
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
86 * tpm_put_ops
88 struct tpm_chip *tpm_chip_find_get(int chip_num)
90 struct tpm_chip *chip, *res = NULL;
91 int chip_prev;
93 mutex_lock(&idr_lock);
95 if (chip_num == TPM_ANY_NUM) {
96 chip_num = 0;
97 do {
98 chip_prev = chip_num;
99 chip = idr_get_next(&dev_nums_idr, &chip_num);
100 if (chip && !tpm_try_get_ops(chip)) {
101 res = chip;
102 break;
104 } while (chip_prev != chip_num);
105 } else {
106 chip = idr_find(&dev_nums_idr, chip_num);
107 if (chip && !tpm_try_get_ops(chip))
108 res = chip;
111 mutex_unlock(&idr_lock);
113 return res;
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);
130 kfree(chip->log.bios_event_log);
131 kfree(chip);
135 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
136 * @pdev: device to which the chip is associated
137 * At this point pdev mst be initialized, but does not have to
138 * be registered
139 * @ops: struct tpm_class_ops instance
141 * Allocates a new struct tpm_chip instance and assigns a free
142 * device number for it. Must be paired with put_device(&chip->dev).
144 struct tpm_chip *tpm_chip_alloc(struct device *dev,
145 const struct tpm_class_ops *ops)
147 struct tpm_chip *chip;
148 int rc;
150 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
151 if (chip == NULL)
152 return ERR_PTR(-ENOMEM);
154 mutex_init(&chip->tpm_mutex);
155 init_rwsem(&chip->ops_sem);
157 chip->ops = ops;
159 mutex_lock(&idr_lock);
160 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
161 mutex_unlock(&idr_lock);
162 if (rc < 0) {
163 dev_err(dev, "No available tpm device numbers\n");
164 kfree(chip);
165 return ERR_PTR(rc);
167 chip->dev_num = rc;
169 device_initialize(&chip->dev);
171 chip->dev.class = tpm_class;
172 chip->dev.release = tpm_dev_release;
173 chip->dev.parent = dev;
174 chip->dev.groups = chip->groups;
176 if (chip->dev_num == 0)
177 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
178 else
179 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
181 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
182 if (rc)
183 goto out;
185 if (!dev)
186 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
188 cdev_init(&chip->cdev, &tpm_fops);
189 chip->cdev.owner = THIS_MODULE;
190 chip->cdev.kobj.parent = &chip->dev.kobj;
192 return chip;
194 out:
195 put_device(&chip->dev);
196 return ERR_PTR(rc);
198 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
201 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
202 * @pdev: parent device to which the chip is associated
203 * @ops: struct tpm_class_ops instance
205 * Same as tpm_chip_alloc except devm is used to do the put_device
207 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
208 const struct tpm_class_ops *ops)
210 struct tpm_chip *chip;
211 int rc;
213 chip = tpm_chip_alloc(pdev, ops);
214 if (IS_ERR(chip))
215 return chip;
217 rc = devm_add_action_or_reset(pdev,
218 (void (*)(void *)) put_device,
219 &chip->dev);
220 if (rc)
221 return ERR_PTR(rc);
223 dev_set_drvdata(pdev, chip);
225 return chip;
227 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
229 static int tpm_add_char_device(struct tpm_chip *chip)
231 int rc;
233 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
234 if (rc) {
235 dev_err(&chip->dev,
236 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
237 dev_name(&chip->dev), MAJOR(chip->dev.devt),
238 MINOR(chip->dev.devt), rc);
240 return rc;
243 rc = device_add(&chip->dev);
244 if (rc) {
245 dev_err(&chip->dev,
246 "unable to device_register() %s, major %d, minor %d, err=%d\n",
247 dev_name(&chip->dev), MAJOR(chip->dev.devt),
248 MINOR(chip->dev.devt), rc);
250 cdev_del(&chip->cdev);
251 return rc;
254 /* Make the chip available. */
255 mutex_lock(&idr_lock);
256 idr_replace(&dev_nums_idr, chip, chip->dev_num);
257 mutex_unlock(&idr_lock);
259 return rc;
262 static void tpm_del_char_device(struct tpm_chip *chip)
264 cdev_del(&chip->cdev);
265 device_del(&chip->dev);
267 /* Make the chip unavailable. */
268 mutex_lock(&idr_lock);
269 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
270 mutex_unlock(&idr_lock);
272 /* Make the driver uncallable. */
273 down_write(&chip->ops_sem);
274 if (chip->flags & TPM_CHIP_FLAG_TPM2)
275 tpm2_shutdown(chip, TPM2_SU_CLEAR);
276 chip->ops = NULL;
277 up_write(&chip->ops_sem);
280 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
282 struct attribute **i;
284 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
285 return;
287 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
289 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
290 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
293 /* For compatibility with legacy sysfs paths we provide symlinks from the
294 * parent dev directory to selected names within the tpm chip directory. Old
295 * kernel versions created these files directly under the parent.
297 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
299 struct attribute **i;
300 int rc;
302 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
303 return 0;
305 rc = __compat_only_sysfs_link_entry_to_kobj(
306 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
307 if (rc && rc != -ENOENT)
308 return rc;
310 /* All the names from tpm-sysfs */
311 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
312 rc = __compat_only_sysfs_link_entry_to_kobj(
313 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
314 if (rc) {
315 tpm_del_legacy_sysfs(chip);
316 return rc;
320 return 0;
323 * tpm_chip_register() - create a character device for the TPM chip
324 * @chip: TPM chip to use.
326 * Creates a character device for the TPM chip and adds sysfs attributes for
327 * the device. As the last step this function adds the chip to the list of TPM
328 * chips available for in-kernel use.
330 * This function should be only called after the chip initialization is
331 * complete.
333 int tpm_chip_register(struct tpm_chip *chip)
335 int rc;
337 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
338 if (chip->flags & TPM_CHIP_FLAG_TPM2)
339 rc = tpm2_auto_startup(chip);
340 else
341 rc = tpm1_auto_startup(chip);
342 if (rc)
343 return rc;
346 tpm_sysfs_add_device(chip);
348 rc = tpm_bios_log_setup(chip);
349 if (rc != 0 && rc != -ENODEV)
350 return rc;
352 tpm_add_ppi(chip);
354 rc = tpm_add_char_device(chip);
355 if (rc) {
356 tpm_bios_log_teardown(chip);
357 return rc;
360 rc = tpm_add_legacy_sysfs(chip);
361 if (rc) {
362 tpm_chip_unregister(chip);
363 return rc;
366 return 0;
368 EXPORT_SYMBOL_GPL(tpm_chip_register);
371 * tpm_chip_unregister() - release the TPM driver
372 * @chip: TPM chip to use.
374 * Takes the chip first away from the list of available TPM chips and then
375 * cleans up all the resources reserved by tpm_chip_register().
377 * Once this function returns the driver call backs in 'op's will not be
378 * running and will no longer start.
380 * NOTE: This function should be only called before deinitializing chip
381 * resources.
383 void tpm_chip_unregister(struct tpm_chip *chip)
385 tpm_del_legacy_sysfs(chip);
386 tpm_bios_log_teardown(chip);
387 tpm_del_char_device(chip);
389 EXPORT_SYMBOL_GPL(tpm_chip_unregister);