Lines Matching refs:chip

15  * TPM chip management routines.
40 static int tpm_request_locality(struct tpm_chip *chip)
44 if (!chip->ops->request_locality)
47 rc = chip->ops->request_locality(chip, 0);
51 chip->locality = rc;
55 static void tpm_relinquish_locality(struct tpm_chip *chip)
59 if (!chip->ops->relinquish_locality)
62 rc = chip->ops->relinquish_locality(chip, chip->locality);
64 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
66 chip->locality = -1;
69 static int tpm_cmd_ready(struct tpm_chip *chip)
71 if (!chip->ops->cmd_ready)
74 return chip->ops->cmd_ready(chip);
77 static int tpm_go_idle(struct tpm_chip *chip)
79 if (!chip->ops->go_idle)
82 return chip->ops->go_idle(chip);
85 static void tpm_clk_enable(struct tpm_chip *chip)
87 if (chip->ops->clk_enable)
88 chip->ops->clk_enable(chip, true);
91 static void tpm_clk_disable(struct tpm_chip *chip)
93 if (chip->ops->clk_enable)
94 chip->ops->clk_enable(chip, false);
99 * @chip: a TPM chip to use
105 int tpm_chip_start(struct tpm_chip *chip)
109 tpm_clk_enable(chip);
111 if (chip->locality == -1) {
112 ret = tpm_request_locality(chip);
114 tpm_clk_disable(chip);
119 ret = tpm_cmd_ready(chip);
121 tpm_relinquish_locality(chip);
122 tpm_clk_disable(chip);
132 * @chip: a TPM chip to use
138 void tpm_chip_stop(struct tpm_chip *chip)
140 tpm_go_idle(chip);
141 tpm_relinquish_locality(chip);
142 tpm_clk_disable(chip);
148 * @chip: Chip to ref
150 * The caller must already have some kind of locking to ensure that chip is
151 * valid. This function will lock the chip so that the ops member can be
155 * Returns -ERRNO if the chip could not be got.
157 int tpm_try_get_ops(struct tpm_chip *chip)
161 get_device(&chip->dev);
163 down_read(&chip->ops_sem);
164 if (!chip->ops)
167 mutex_lock(&chip->tpm_mutex);
168 rc = tpm_chip_start(chip);
174 mutex_unlock(&chip->tpm_mutex);
176 up_read(&chip->ops_sem);
177 put_device(&chip->dev);
184 * @chip: Chip to put
186 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
189 void tpm_put_ops(struct tpm_chip *chip)
191 tpm_chip_stop(chip);
192 mutex_unlock(&chip->tpm_mutex);
193 up_read(&chip->ops_sem);
194 put_device(&chip->dev);
199 * tpm_default_chip() - find a TPM chip and get a reference to it
203 struct tpm_chip *chip, *res = NULL;
211 chip = idr_get_next(&dev_nums_idr, &chip_num);
212 if (chip) {
213 get_device(&chip->dev);
214 res = chip;
226 * tpm_find_get_ops() - find and reserve a TPM chip
227 * @chip: a &struct tpm_chip instance, %NULL for the default chip
229 * Finds a TPM chip and reserves its class device and operations. The chip must
232 * by accepting NULL, but those callers should be converted to pass in a chip
237 * %NULL if a chip is not found.
238 * %NULL if the chip is not available.
240 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
244 if (chip) {
245 if (!tpm_try_get_ops(chip))
246 return chip;
250 chip = tpm_default_chip();
251 if (!chip)
253 rc = tpm_try_get_ops(chip);
255 put_device(&chip->dev);
258 return chip;
262 * tpm_dev_release() - free chip memory and the device number
263 * @dev: the character device for the TPM chip
269 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
272 idr_remove(&dev_nums_idr, chip->dev_num);
275 kfree(chip->work_space.context_buf);
276 kfree(chip->work_space.session_buf);
277 kfree(chip->allocated_banks);
278 kfree(chip);
283 * @dev: device to which the chip is associated.
292 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
294 down_write(&chip->ops_sem);
295 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
296 if (!tpm_chip_start(chip)) {
297 tpm2_shutdown(chip, TPM2_SU_CLEAR);
298 tpm_chip_stop(chip);
301 chip->ops = NULL;
302 up_write(&chip->ops_sem);
309 * @pdev: device to which the chip is associated
315 * device number for it. Must be paired with put_device(&chip->dev).
320 struct tpm_chip *chip;
323 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
324 if (chip == NULL)
327 mutex_init(&chip->tpm_mutex);
328 init_rwsem(&chip->ops_sem);
330 chip->ops = ops;
337 kfree(chip);
340 chip->dev_num = rc;
342 device_initialize(&chip->dev);
344 chip->dev.class = &tpm_class;
345 chip->dev.release = tpm_dev_release;
346 chip->dev.parent = pdev;
347 chip->dev.groups = chip->groups;
349 if (chip->dev_num == 0)
350 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
352 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
354 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
359 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
361 cdev_init(&chip->cdev, &tpm_fops);
362 chip->cdev.owner = THIS_MODULE;
364 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
370 chip->locality = -1;
371 return chip;
374 put_device(&chip->dev);
386 * @pdev: parent device to which the chip is associated
394 struct tpm_chip *chip;
397 chip = tpm_chip_alloc(pdev, ops);
398 if (IS_ERR(chip))
399 return chip;
403 &chip->dev);
407 dev_set_drvdata(pdev, chip);
409 return chip;
413 static int tpm_add_char_device(struct tpm_chip *chip)
417 rc = cdev_device_add(&chip->cdev, &chip->dev);
419 dev_err(&chip->dev,
421 dev_name(&chip->dev), MAJOR(chip->dev.devt),
422 MINOR(chip->dev.devt), rc);
426 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
427 rc = tpm_devs_add(chip);
432 /* Make the chip available. */
434 idr_replace(&dev_nums_idr, chip, chip->dev_num);
440 cdev_device_del(&chip->cdev, &chip->dev);
444 static void tpm_del_char_device(struct tpm_chip *chip)
446 cdev_device_del(&chip->cdev, &chip->dev);
448 /* Make the chip unavailable. */
450 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
454 down_write(&chip->ops_sem);
457 * Check if chip->ops is still valid: In case that the controller
459 * shutdown handler we are called twice and chip->ops to NULL.
461 if (chip->ops) {
462 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
463 if (!tpm_chip_start(chip)) {
464 tpm2_shutdown(chip, TPM2_SU_CLEAR);
465 tpm_chip_stop(chip);
468 chip->ops = NULL;
470 up_write(&chip->ops_sem);
473 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
477 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
478 tpm_is_firmware_upgrade(chip))
481 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
483 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
484 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
488 * parent dev directory to selected names within the tpm chip directory. Old
491 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
496 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
497 tpm_is_firmware_upgrade(chip))
501 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
506 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
508 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
510 tpm_del_legacy_sysfs(chip);
520 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
522 /* Give back zero bytes, as TPM chip has not yet fully resumed: */
523 if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
526 return tpm_get_random(chip, data, max);
529 static bool tpm_is_hwrng_enabled(struct tpm_chip *chip)
533 if (tpm_is_firmware_upgrade(chip))
535 if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
540 static int tpm_add_hwrng(struct tpm_chip *chip)
542 if (!tpm_is_hwrng_enabled(chip))
545 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
546 "tpm-rng-%d", chip->dev_num);
547 chip->hwrng.name = chip->hwrng_name;
548 chip->hwrng.read = tpm_hwrng_read;
549 return hwrng_register(&chip->hwrng);
552 static int tpm_get_pcr_allocation(struct tpm_chip *chip)
556 if (tpm_is_firmware_upgrade(chip))
559 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
560 tpm2_get_pcr_allocation(chip) :
561 tpm1_get_pcr_allocation(chip);
570 * tpm_chip_bootstrap() - Boostrap TPM chip after power on
571 * @chip: TPM chip to use.
573 * Initialize TPM chip after power on. This a one-shot function: subsequent
576 int tpm_chip_bootstrap(struct tpm_chip *chip)
580 if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED)
583 rc = tpm_chip_start(chip);
587 rc = tpm_auto_startup(chip);
591 rc = tpm_get_pcr_allocation(chip);
593 tpm_chip_stop(chip);
599 chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED;
606 * tpm_chip_register() - create a character device for the TPM chip
607 * @chip: TPM chip to use.
609 * Creates a character device for the TPM chip and adds sysfs attributes for
610 * the device. As the last step this function adds the chip to the list of TPM
613 * This function should be only called after the chip initialization is
616 int tpm_chip_register(struct tpm_chip *chip)
620 rc = tpm_chip_bootstrap(chip);
624 tpm_sysfs_add_device(chip);
626 tpm_bios_log_setup(chip);
628 tpm_add_ppi(chip);
630 rc = tpm_add_hwrng(chip);
634 rc = tpm_add_char_device(chip);
638 rc = tpm_add_legacy_sysfs(chip);
640 tpm_chip_unregister(chip);
647 if (tpm_is_hwrng_enabled(chip))
648 hwrng_unregister(&chip->hwrng);
650 tpm_bios_log_teardown(chip);
658 * @chip: TPM chip to use.
660 * Takes the chip first away from the list of available TPM chips and then
666 * NOTE: This function should be only called before deinitializing chip
669 void tpm_chip_unregister(struct tpm_chip *chip)
671 tpm_del_legacy_sysfs(chip);
672 if (tpm_is_hwrng_enabled(chip))
673 hwrng_unregister(&chip->hwrng);
674 tpm_bios_log_teardown(chip);
675 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
676 tpm_devs_remove(chip);
677 tpm_del_char_device(chip);