tpm: Get rid of chip->pdev
[deliverable/linux.git] / drivers / char / tpm / tpm-chip.c
CommitLineData
afb5abc2
JS
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>
313d21ee 28#include <linux/major.h>
afb5abc2
JS
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
313d21ee
JS
36struct class *tpm_class;
37dev_t tpm_devt;
38
afb5abc2
JS
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
8cfffc9d 52 if (try_module_get(pos->dev.parent->driver->owner)) {
afb5abc2
JS
53 chip = pos;
54 break;
55 }
56 }
57 rcu_read_unlock();
58 return chip;
59}
60
61/**
313d21ee
JS
62 * tpm_dev_release() - free chip memory and the device number
63 * @dev: the character device for the TPM chip
afb5abc2 64 *
313d21ee 65 * This is used as the release function for the character device.
afb5abc2 66 */
313d21ee 67static void tpm_dev_release(struct device *dev)
afb5abc2 68{
313d21ee 69 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
afb5abc2
JS
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;
4f3b193d 91 int rc;
afb5abc2
JS
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
afb5abc2
JS
116 dev_set_drvdata(dev, chip);
117
313d21ee
JS
118 chip->dev.class = tpm_class;
119 chip->dev.release = tpm_dev_release;
8cfffc9d 120 chip->dev.parent = dev;
9b774d5c
JS
121#ifdef CONFIG_ACPI
122 chip->dev.groups = chip->groups;
123#endif
313d21ee
JS
124
125 if (chip->dev_num == 0)
126 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
127 else
128 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
129
743410a0 130 dev_set_name(&chip->dev, "%s", chip->devname);
313d21ee
JS
131
132 device_initialize(&chip->dev);
133
313d21ee 134 cdev_init(&chip->cdev, &tpm_fops);
8cfffc9d 135 chip->cdev.owner = dev->driver->owner;
ba0ef854 136 chip->cdev.kobj.parent = &chip->dev.kobj;
313d21ee 137
4f3b193d
JS
138 rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
139 if (rc) {
140 put_device(&chip->dev);
141 return ERR_PTR(rc);
142 }
8e0ee3c9 143
afb5abc2
JS
144 return chip;
145}
146EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
147
72c91ce8 148static int tpm_add_char_device(struct tpm_chip *chip)
313d21ee
JS
149{
150 int rc;
151
d972b052 152 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
313d21ee
JS
153 if (rc) {
154 dev_err(&chip->dev,
d972b052 155 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
313d21ee
JS
156 chip->devname, MAJOR(chip->dev.devt),
157 MINOR(chip->dev.devt), rc);
158
159 return rc;
160 }
161
d972b052 162 rc = device_add(&chip->dev);
313d21ee
JS
163 if (rc) {
164 dev_err(&chip->dev,
d972b052 165 "unable to device_register() %s, major %d, minor %d, err=%d\n",
313d21ee
JS
166 chip->devname, MAJOR(chip->dev.devt),
167 MINOR(chip->dev.devt), rc);
168
72c91ce8 169 cdev_del(&chip->cdev);
313d21ee
JS
170 return rc;
171 }
172
173 return rc;
174}
175
72c91ce8 176static void tpm_del_char_device(struct tpm_chip *chip)
313d21ee
JS
177{
178 cdev_del(&chip->cdev);
8e0ee3c9 179 device_del(&chip->dev);
313d21ee
JS
180}
181
34d47b63
JS
182static int tpm1_chip_register(struct tpm_chip *chip)
183{
184 int rc;
185
186 if (chip->flags & TPM_CHIP_FLAG_TPM2)
187 return 0;
188
189 rc = tpm_sysfs_add_device(chip);
190 if (rc)
191 return rc;
192
34d47b63
JS
193 chip->bios_dir = tpm_bios_log_setup(chip->devname);
194
195 return 0;
196}
197
198static void tpm1_chip_unregister(struct tpm_chip *chip)
199{
200 if (chip->flags & TPM_CHIP_FLAG_TPM2)
201 return;
202
203 if (chip->bios_dir)
204 tpm_bios_log_teardown(chip->bios_dir);
205
34d47b63
JS
206 tpm_sysfs_del_device(chip);
207}
208
afb5abc2 209/*
313d21ee 210 * tpm_chip_register() - create a character device for the TPM chip
afb5abc2
JS
211 * @chip: TPM chip to use.
212 *
d972b052
JS
213 * Creates a character device for the TPM chip and adds sysfs attributes for
214 * the device. As the last step this function adds the chip to the list of TPM
215 * chips available for in-kernel use.
afb5abc2 216 *
d972b052
JS
217 * This function should be only called after the chip initialization is
218 * complete.
afb5abc2
JS
219 */
220int tpm_chip_register(struct tpm_chip *chip)
221{
222 int rc;
223
34d47b63
JS
224 rc = tpm1_chip_register(chip);
225 if (rc)
226 return rc;
afb5abc2 227
9b774d5c
JS
228 tpm_add_ppi(chip);
229
72c91ce8 230 rc = tpm_add_char_device(chip);
d972b052 231 if (rc)
34d47b63 232 goto out_err;
d972b052 233
afb5abc2
JS
234 /* Make the chip available. */
235 spin_lock(&driver_lock);
b1a4144a 236 list_add_tail_rcu(&chip->list, &tpm_chip_list);
afb5abc2
JS
237 spin_unlock(&driver_lock);
238
239 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
240
d56e4f75 241 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
8cfffc9d
JG
242 rc = __compat_only_sysfs_link_entry_to_kobj(
243 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
d56e4f75
JS
244 if (rc && rc != -ENOENT) {
245 tpm_chip_unregister(chip);
246 return rc;
247 }
248 }
249
afb5abc2 250 return 0;
34d47b63
JS
251out_err:
252 tpm1_chip_unregister(chip);
afb5abc2
JS
253 return rc;
254}
255EXPORT_SYMBOL_GPL(tpm_chip_register);
256
257/*
258 * tpm_chip_unregister() - release the TPM driver
259 * @chip: TPM chip to use.
260 *
261 * Takes the chip first away from the list of available TPM chips and then
262 * cleans up all the resources reserved by tpm_chip_register().
263 *
264 * NOTE: This function should be only called before deinitializing chip
265 * resources.
266 */
267void tpm_chip_unregister(struct tpm_chip *chip)
268{
269 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
270 return;
271
272 spin_lock(&driver_lock);
273 list_del_rcu(&chip->list);
274 spin_unlock(&driver_lock);
275 synchronize_rcu();
276
9b774d5c 277 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
8cfffc9d 278 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
9b774d5c 279
34d47b63 280 tpm1_chip_unregister(chip);
72c91ce8 281 tpm_del_char_device(chip);
afb5abc2
JS
282}
283EXPORT_SYMBOL_GPL(tpm_chip_unregister);
This page took 0.174513 seconds and 5 git commands to generate.