tpm: Get rid of devname
[linux-2.6-microblaze.git] / drivers / char / tpm / tpm-chip.c
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
32 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
33 static LIST_HEAD(tpm_chip_list);
34 static DEFINE_SPINLOCK(driver_lock);
35
36 struct class *tpm_class;
37 dev_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  */
43 struct 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->dev.parent->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  */
67 static 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  */
87 struct 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         device_initialize(&chip->dev);
115
116         dev_set_drvdata(dev, chip);
117
118         chip->dev.class = tpm_class;
119         chip->dev.release = tpm_dev_release;
120         chip->dev.parent = dev;
121 #ifdef CONFIG_ACPI
122         chip->dev.groups = chip->groups;
123 #endif
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
130         rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
131         if (rc)
132                 goto out;
133
134         cdev_init(&chip->cdev, &tpm_fops);
135         chip->cdev.owner = dev->driver->owner;
136         chip->cdev.kobj.parent = &chip->dev.kobj;
137
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         }
143
144         return chip;
145
146 out:
147         put_device(&chip->dev);
148         return ERR_PTR(rc);
149 }
150 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
151
152 static int tpm_add_char_device(struct tpm_chip *chip)
153 {
154         int rc;
155
156         rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
157         if (rc) {
158                 dev_err(&chip->dev,
159                         "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
160                         dev_name(&chip->dev), MAJOR(chip->dev.devt),
161                         MINOR(chip->dev.devt), rc);
162
163                 return rc;
164         }
165
166         rc = device_add(&chip->dev);
167         if (rc) {
168                 dev_err(&chip->dev,
169                         "unable to device_register() %s, major %d, minor %d, err=%d\n",
170                         dev_name(&chip->dev), MAJOR(chip->dev.devt),
171                         MINOR(chip->dev.devt), rc);
172
173                 cdev_del(&chip->cdev);
174                 return rc;
175         }
176
177         return rc;
178 }
179
180 static void tpm_del_char_device(struct tpm_chip *chip)
181 {
182         cdev_del(&chip->cdev);
183         device_del(&chip->dev);
184 }
185
186 static int tpm1_chip_register(struct tpm_chip *chip)
187 {
188         int rc;
189
190         if (chip->flags & TPM_CHIP_FLAG_TPM2)
191                 return 0;
192
193         rc = tpm_sysfs_add_device(chip);
194         if (rc)
195                 return rc;
196
197         chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
198
199         return 0;
200 }
201
202 static void tpm1_chip_unregister(struct tpm_chip *chip)
203 {
204         if (chip->flags & TPM_CHIP_FLAG_TPM2)
205                 return;
206
207         if (chip->bios_dir)
208                 tpm_bios_log_teardown(chip->bios_dir);
209
210         tpm_sysfs_del_device(chip);
211 }
212
213 /*
214  * tpm_chip_register() - create a character device for the TPM chip
215  * @chip: TPM chip to use.
216  *
217  * Creates a character device for the TPM chip and adds sysfs attributes for
218  * the device. As the last step this function adds the chip to the list of TPM
219  * chips available for in-kernel use.
220  *
221  * This function should be only called after the chip initialization is
222  * complete.
223  */
224 int tpm_chip_register(struct tpm_chip *chip)
225 {
226         int rc;
227
228         rc = tpm1_chip_register(chip);
229         if (rc)
230                 return rc;
231
232         tpm_add_ppi(chip);
233
234         rc = tpm_add_char_device(chip);
235         if (rc)
236                 goto out_err;
237
238         /* Make the chip available. */
239         spin_lock(&driver_lock);
240         list_add_tail_rcu(&chip->list, &tpm_chip_list);
241         spin_unlock(&driver_lock);
242
243         chip->flags |= TPM_CHIP_FLAG_REGISTERED;
244
245         if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
246                 rc = __compat_only_sysfs_link_entry_to_kobj(
247                     &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
248                 if (rc && rc != -ENOENT) {
249                         tpm_chip_unregister(chip);
250                         return rc;
251                 }
252         }
253
254         return 0;
255 out_err:
256         tpm1_chip_unregister(chip);
257         return rc;
258 }
259 EXPORT_SYMBOL_GPL(tpm_chip_register);
260
261 /*
262  * tpm_chip_unregister() - release the TPM driver
263  * @chip: TPM chip to use.
264  *
265  * Takes the chip first away from the list of available TPM chips and then
266  * cleans up all the resources reserved by tpm_chip_register().
267  *
268  * NOTE: This function should be only called before deinitializing chip
269  * resources.
270  */
271 void tpm_chip_unregister(struct tpm_chip *chip)
272 {
273         if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
274                 return;
275
276         spin_lock(&driver_lock);
277         list_del_rcu(&chip->list);
278         spin_unlock(&driver_lock);
279         synchronize_rcu();
280
281         if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
282                 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
283
284         tpm1_chip_unregister(chip);
285         tpm_del_char_device(chip);
286 }
287 EXPORT_SYMBOL_GPL(tpm_chip_unregister);