Lines Matching refs:chip
15 * TPM chip management routines.
35 static int tpm_request_locality(struct tpm_chip *chip)
39 if (!chip->ops->request_locality)
42 rc = chip->ops->request_locality(chip, 0);
46 chip->locality = rc;
50 static void tpm_relinquish_locality(struct tpm_chip *chip)
54 if (!chip->ops->relinquish_locality)
57 rc = chip->ops->relinquish_locality(chip, chip->locality);
59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
61 chip->locality = -1;
64 static int tpm_cmd_ready(struct tpm_chip *chip)
66 if (!chip->ops->cmd_ready)
69 return chip->ops->cmd_ready(chip);
72 static int tpm_go_idle(struct tpm_chip *chip)
74 if (!chip->ops->go_idle)
77 return chip->ops->go_idle(chip);
80 static void tpm_clk_enable(struct tpm_chip *chip)
82 if (chip->ops->clk_enable)
83 chip->ops->clk_enable(chip, true);
86 static void tpm_clk_disable(struct tpm_chip *chip)
88 if (chip->ops->clk_enable)
89 chip->ops->clk_enable(chip, false);
94 * @chip: a TPM chip to use
100 int tpm_chip_start(struct tpm_chip *chip)
104 tpm_clk_enable(chip);
106 if (chip->locality == -1) {
107 ret = tpm_request_locality(chip);
109 tpm_clk_disable(chip);
114 ret = tpm_cmd_ready(chip);
116 tpm_relinquish_locality(chip);
117 tpm_clk_disable(chip);
127 * @chip: a TPM chip to use
133 void tpm_chip_stop(struct tpm_chip *chip)
135 tpm_go_idle(chip);
136 tpm_relinquish_locality(chip);
137 tpm_clk_disable(chip);
143 * @chip: Chip to ref
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
150 * Returns -ERRNO if the chip could not be got.
152 int tpm_try_get_ops(struct tpm_chip *chip)
156 get_device(&chip->dev);
158 down_read(&chip->ops_sem);
159 if (!chip->ops)
162 mutex_lock(&chip->tpm_mutex);
163 rc = tpm_chip_start(chip);
169 mutex_unlock(&chip->tpm_mutex);
171 up_read(&chip->ops_sem);
172 put_device(&chip->dev);
179 * @chip: Chip to put
181 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
184 void tpm_put_ops(struct tpm_chip *chip)
186 tpm_chip_stop(chip);
187 mutex_unlock(&chip->tpm_mutex);
188 up_read(&chip->ops_sem);
189 put_device(&chip->dev);
194 * tpm_default_chip() - find a TPM chip and get a reference to it
198 struct tpm_chip *chip, *res = NULL;
206 chip = idr_get_next(&dev_nums_idr, &chip_num);
207 if (chip) {
208 get_device(&chip->dev);
209 res = chip;
221 * tpm_find_get_ops() - find and reserve a TPM chip
222 * @chip: a &struct tpm_chip instance, %NULL for the default chip
224 * Finds a TPM chip and reserves its class device and operations. The chip must
227 * by accepting NULL, but those callers should be converted to pass in a chip
232 * %NULL if a chip is not found.
233 * %NULL if the chip is not available.
235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
239 if (chip) {
240 if (!tpm_try_get_ops(chip))
241 return chip;
245 chip = tpm_default_chip();
246 if (!chip)
248 rc = tpm_try_get_ops(chip);
250 put_device(&chip->dev);
253 return chip;
257 * tpm_dev_release() - free chip memory and the device number
258 * @dev: the character device for the TPM chip
264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
267 idr_remove(&dev_nums_idr, chip->dev_num);
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);
279 * @dev: device to which the chip is associated.
288 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
290 down_write(&chip->ops_sem);
291 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
292 if (!tpm_chip_start(chip)) {
293 tpm2_shutdown(chip, TPM2_SU_CLEAR);
294 tpm_chip_stop(chip);
297 chip->ops = NULL;
298 up_write(&chip->ops_sem);
305 * @pdev: device to which the chip is associated
311 * device number for it. Must be paired with put_device(&chip->dev).
316 struct tpm_chip *chip;
319 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
320 if (chip == NULL)
323 mutex_init(&chip->tpm_mutex);
324 init_rwsem(&chip->ops_sem);
326 chip->ops = ops;
333 kfree(chip);
336 chip->dev_num = rc;
338 device_initialize(&chip->dev);
340 chip->dev.class = tpm_class;
341 chip->dev.class->shutdown_pre = tpm_class_shutdown;
342 chip->dev.release = tpm_dev_release;
343 chip->dev.parent = pdev;
344 chip->dev.groups = chip->groups;
346 if (chip->dev_num == 0)
347 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
349 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
351 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
356 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
358 cdev_init(&chip->cdev, &tpm_fops);
359 chip->cdev.owner = THIS_MODULE;
361 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
367 chip->locality = -1;
368 return chip;
371 put_device(&chip->dev);
378 * @pdev: parent device to which the chip is associated
386 struct tpm_chip *chip;
389 chip = tpm_chip_alloc(pdev, ops);
390 if (IS_ERR(chip))
391 return chip;
395 &chip->dev);
399 dev_set_drvdata(pdev, chip);
401 return chip;
405 static int tpm_add_char_device(struct tpm_chip *chip)
409 rc = cdev_device_add(&chip->cdev, &chip->dev);
411 dev_err(&chip->dev,
413 dev_name(&chip->dev), MAJOR(chip->dev.devt),
414 MINOR(chip->dev.devt), rc);
418 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
419 rc = tpm_devs_add(chip);
424 /* Make the chip available. */
426 idr_replace(&dev_nums_idr, chip, chip->dev_num);
432 cdev_device_del(&chip->cdev, &chip->dev);
436 static void tpm_del_char_device(struct tpm_chip *chip)
438 cdev_device_del(&chip->cdev, &chip->dev);
440 /* Make the chip unavailable. */
442 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
446 down_write(&chip->ops_sem);
447 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
448 if (!tpm_chip_start(chip)) {
449 tpm2_shutdown(chip, TPM2_SU_CLEAR);
450 tpm_chip_stop(chip);
453 chip->ops = NULL;
454 up_write(&chip->ops_sem);
457 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
461 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
464 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
466 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
467 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
471 * parent dev directory to selected names within the tpm chip directory. Old
474 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
479 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
483 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
488 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
490 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
492 tpm_del_legacy_sysfs(chip);
502 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
504 return tpm_get_random(chip, data, max);
507 static int tpm_add_hwrng(struct tpm_chip *chip)
512 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
513 "tpm-rng-%d", chip->dev_num);
514 chip->hwrng.name = chip->hwrng_name;
515 chip->hwrng.read = tpm_hwrng_read;
516 return hwrng_register(&chip->hwrng);
519 static int tpm_get_pcr_allocation(struct tpm_chip *chip)
523 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
524 tpm2_get_pcr_allocation(chip) :
525 tpm1_get_pcr_allocation(chip);
534 * tpm_chip_register() - create a character device for the TPM chip
535 * @chip: TPM chip to use.
537 * Creates a character device for the TPM chip and adds sysfs attributes for
538 * the device. As the last step this function adds the chip to the list of TPM
541 * This function should be only called after the chip initialization is
544 int tpm_chip_register(struct tpm_chip *chip)
548 rc = tpm_chip_start(chip);
551 rc = tpm_auto_startup(chip);
553 tpm_chip_stop(chip);
557 rc = tpm_get_pcr_allocation(chip);
558 tpm_chip_stop(chip);
562 tpm_sysfs_add_device(chip);
564 tpm_bios_log_setup(chip);
566 tpm_add_ppi(chip);
568 rc = tpm_add_hwrng(chip);
572 rc = tpm_add_char_device(chip);
576 rc = tpm_add_legacy_sysfs(chip);
578 tpm_chip_unregister(chip);
586 hwrng_unregister(&chip->hwrng);
588 tpm_bios_log_teardown(chip);
596 * @chip: TPM chip to use.
598 * Takes the chip first away from the list of available TPM chips and then
604 * NOTE: This function should be only called before deinitializing chip
607 void tpm_chip_unregister(struct tpm_chip *chip)
609 tpm_del_legacy_sysfs(chip);
611 hwrng_unregister(&chip->hwrng);
612 tpm_bios_log_teardown(chip);
613 if (chip->flags & TPM_CHIP_FLAG_TPM2)
614 tpm_devs_remove(chip);
615 tpm_del_char_device(chip);