Lines Matching full:chip

15  * TPM chip management routines.
35 static int tpm_request_locality(struct tpm_chip *chip) in tpm_request_locality() argument
39 if (!chip->ops->request_locality) in tpm_request_locality()
42 rc = chip->ops->request_locality(chip, 0); in tpm_request_locality()
46 chip->locality = rc; in tpm_request_locality()
50 static void tpm_relinquish_locality(struct tpm_chip *chip) in tpm_relinquish_locality() argument
54 if (!chip->ops->relinquish_locality) in tpm_relinquish_locality()
57 rc = chip->ops->relinquish_locality(chip, chip->locality); in tpm_relinquish_locality()
59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); in tpm_relinquish_locality()
61 chip->locality = -1; in tpm_relinquish_locality()
64 static int tpm_cmd_ready(struct tpm_chip *chip) in tpm_cmd_ready() argument
66 if (!chip->ops->cmd_ready) in tpm_cmd_ready()
69 return chip->ops->cmd_ready(chip); in tpm_cmd_ready()
72 static int tpm_go_idle(struct tpm_chip *chip) in tpm_go_idle() argument
74 if (!chip->ops->go_idle) in tpm_go_idle()
77 return chip->ops->go_idle(chip); in tpm_go_idle()
80 static void tpm_clk_enable(struct tpm_chip *chip) in tpm_clk_enable() argument
82 if (chip->ops->clk_enable) in tpm_clk_enable()
83 chip->ops->clk_enable(chip, true); in tpm_clk_enable()
86 static void tpm_clk_disable(struct tpm_chip *chip) in tpm_clk_disable() argument
88 if (chip->ops->clk_enable) in tpm_clk_disable()
89 chip->ops->clk_enable(chip, false); in tpm_clk_disable()
94 * @chip: a TPM chip to use
100 int tpm_chip_start(struct tpm_chip *chip) in tpm_chip_start() argument
104 tpm_clk_enable(chip); in tpm_chip_start()
106 if (chip->locality == -1) { in tpm_chip_start()
107 ret = tpm_request_locality(chip); in tpm_chip_start()
109 tpm_clk_disable(chip); in tpm_chip_start()
114 ret = tpm_cmd_ready(chip); in tpm_chip_start()
116 tpm_relinquish_locality(chip); in tpm_chip_start()
117 tpm_clk_disable(chip); in tpm_chip_start()
127 * @chip: a TPM chip to use
133 void tpm_chip_stop(struct tpm_chip *chip) in tpm_chip_stop() argument
135 tpm_go_idle(chip); in tpm_chip_stop()
136 tpm_relinquish_locality(chip); in tpm_chip_stop()
137 tpm_clk_disable(chip); in tpm_chip_stop()
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) in tpm_try_get_ops() argument
156 get_device(&chip->dev); in tpm_try_get_ops()
158 down_read(&chip->ops_sem); in tpm_try_get_ops()
159 if (!chip->ops) in tpm_try_get_ops()
162 mutex_lock(&chip->tpm_mutex); in tpm_try_get_ops()
163 rc = tpm_chip_start(chip); in tpm_try_get_ops()
169 mutex_unlock(&chip->tpm_mutex); in tpm_try_get_ops()
171 up_read(&chip->ops_sem); in tpm_try_get_ops()
172 put_device(&chip->dev); in tpm_try_get_ops()
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) in tpm_put_ops() argument
186 tpm_chip_stop(chip); in tpm_put_ops()
187 mutex_unlock(&chip->tpm_mutex); in tpm_put_ops()
188 up_read(&chip->ops_sem); in tpm_put_ops()
189 put_device(&chip->dev); in tpm_put_ops()
194 * tpm_default_chip() - find a TPM chip and get a reference to it
198 struct tpm_chip *chip, *res = NULL; in tpm_default_chip() local
206 chip = idr_get_next(&dev_nums_idr, &chip_num); in tpm_default_chip()
207 if (chip) { in tpm_default_chip()
208 get_device(&chip->dev); in tpm_default_chip()
209 res = chip; in tpm_default_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) in tpm_find_get_ops() argument
239 if (chip) { in tpm_find_get_ops()
240 if (!tpm_try_get_ops(chip)) in tpm_find_get_ops()
241 return chip; in tpm_find_get_ops()
245 chip = tpm_default_chip(); in tpm_find_get_ops()
246 if (!chip) in tpm_find_get_ops()
248 rc = tpm_try_get_ops(chip); in tpm_find_get_ops()
250 put_device(&chip->dev); in tpm_find_get_ops()
253 return chip; in tpm_find_get_ops()
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); in tpm_dev_release() local
267 idr_remove(&dev_nums_idr, chip->dev_num); in tpm_dev_release()
270 kfree(chip->log.bios_event_log); in tpm_dev_release()
271 kfree(chip->work_space.context_buf); in tpm_dev_release()
272 kfree(chip->work_space.session_buf); in tpm_dev_release()
273 kfree(chip->allocated_banks); in tpm_dev_release()
274 kfree(chip); in tpm_dev_release()
279 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); in tpm_devs_release() local
282 put_device(&chip->dev); in tpm_devs_release()
287 * @dev: device to which the chip is associated.
296 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
298 down_write(&chip->ops_sem); in tpm_class_shutdown()
299 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
300 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
301 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
302 tpm_chip_stop(chip); in tpm_class_shutdown()
305 chip->ops = NULL; in tpm_class_shutdown()
306 up_write(&chip->ops_sem); in tpm_class_shutdown()
313 * @pdev: device to which the chip is associated
319 * device number for it. Must be paired with put_device(&chip->dev).
324 struct tpm_chip *chip; in tpm_chip_alloc() local
327 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
328 if (chip == NULL) in tpm_chip_alloc()
331 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
332 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
334 chip->ops = ops; in tpm_chip_alloc()
341 kfree(chip); in tpm_chip_alloc()
344 chip->dev_num = rc; in tpm_chip_alloc()
346 device_initialize(&chip->dev); in tpm_chip_alloc()
347 device_initialize(&chip->devs); in tpm_chip_alloc()
349 chip->dev.class = tpm_class; in tpm_chip_alloc()
350 chip->dev.class->shutdown_pre = tpm_class_shutdown; in tpm_chip_alloc()
351 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
352 chip->dev.parent = pdev; in tpm_chip_alloc()
353 chip->dev.groups = chip->groups; in tpm_chip_alloc()
355 chip->devs.parent = pdev; in tpm_chip_alloc()
356 chip->devs.class = tpmrm_class; in tpm_chip_alloc()
357 chip->devs.release = tpm_devs_release; in tpm_chip_alloc()
359 * behalf of devs. This holds the chip structure in tpm_chip_alloc()
363 if (chip->flags & TPM_CHIP_FLAG_TPM2) in tpm_chip_alloc()
364 get_device(&chip->dev); in tpm_chip_alloc()
366 if (chip->dev_num == 0) in tpm_chip_alloc()
367 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
369 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
371 chip->devs.devt = in tpm_chip_alloc()
372 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); in tpm_chip_alloc()
374 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
377 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); in tpm_chip_alloc()
382 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
384 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
385 cdev_init(&chip->cdevs, &tpmrm_fops); in tpm_chip_alloc()
386 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
387 chip->cdevs.owner = THIS_MODULE; in tpm_chip_alloc()
389 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); in tpm_chip_alloc()
390 if (!chip->work_space.context_buf) { in tpm_chip_alloc()
394 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); in tpm_chip_alloc()
395 if (!chip->work_space.session_buf) { in tpm_chip_alloc()
400 chip->locality = -1; in tpm_chip_alloc()
401 return chip; in tpm_chip_alloc()
404 put_device(&chip->devs); in tpm_chip_alloc()
405 put_device(&chip->dev); in tpm_chip_alloc()
412 * @pdev: parent device to which the chip is associated
420 struct tpm_chip *chip; in tpmm_chip_alloc() local
423 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
424 if (IS_ERR(chip)) in tpmm_chip_alloc()
425 return chip; in tpmm_chip_alloc()
429 &chip->dev); in tpmm_chip_alloc()
433 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
435 return chip; in tpmm_chip_alloc()
439 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
443 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
445 dev_err(&chip->dev, in tpm_add_char_device()
447 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
448 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
452 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_add_char_device()
453 rc = cdev_device_add(&chip->cdevs, &chip->devs); in tpm_add_char_device()
455 dev_err(&chip->devs, in tpm_add_char_device()
457 dev_name(&chip->devs), MAJOR(chip->devs.devt), in tpm_add_char_device()
458 MINOR(chip->devs.devt), rc); in tpm_add_char_device()
463 /* Make the chip available. */ in tpm_add_char_device()
465 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
471 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
473 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
475 /* Make the chip unavailable. */ in tpm_del_char_device()
477 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
481 down_write(&chip->ops_sem); in tpm_del_char_device()
482 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
483 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
484 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
485 tpm_chip_stop(chip); in tpm_del_char_device()
488 chip->ops = NULL; in tpm_del_char_device()
489 up_write(&chip->ops_sem); in tpm_del_char_device()
492 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
496 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) in tpm_del_legacy_sysfs()
499 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
501 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
502 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
506 * parent dev directory to selected names within the tpm chip directory. Old
509 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
514 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) in tpm_add_legacy_sysfs()
518 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi"); in tpm_add_legacy_sysfs()
523 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
525 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name); in tpm_add_legacy_sysfs()
527 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
537 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
539 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
542 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
547 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
548 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
549 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
550 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
551 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
554 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
558 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
559 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
560 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
569 * tpm_chip_register() - create a character device for the TPM chip
570 * @chip: TPM chip to use.
572 * Creates a character device for the TPM chip and adds sysfs attributes for
573 * the device. As the last step this function adds the chip to the list of TPM
576 * This function should be only called after the chip initialization is
579 int tpm_chip_register(struct tpm_chip *chip) in tpm_chip_register() argument
583 rc = tpm_chip_start(chip); in tpm_chip_register()
586 rc = tpm_auto_startup(chip); in tpm_chip_register()
588 tpm_chip_stop(chip); in tpm_chip_register()
592 rc = tpm_get_pcr_allocation(chip); in tpm_chip_register()
593 tpm_chip_stop(chip); in tpm_chip_register()
597 tpm_sysfs_add_device(chip); in tpm_chip_register()
599 rc = tpm_bios_log_setup(chip); in tpm_chip_register()
603 tpm_add_ppi(chip); in tpm_chip_register()
605 rc = tpm_add_hwrng(chip); in tpm_chip_register()
609 rc = tpm_add_char_device(chip); in tpm_chip_register()
613 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
615 tpm_chip_unregister(chip); in tpm_chip_register()
623 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
625 tpm_bios_log_teardown(chip); in tpm_chip_register()
633 * @chip: TPM chip to use.
635 * Takes the chip first away from the list of available TPM chips and then
641 * NOTE: This function should be only called before deinitializing chip
644 void tpm_chip_unregister(struct tpm_chip *chip) in tpm_chip_unregister() argument
646 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
648 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
649 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
650 if (chip->flags & TPM_CHIP_FLAG_TPM2) in tpm_chip_unregister()
651 cdev_device_del(&chip->cdevs, &chip->devs); in tpm_chip_unregister()
652 tpm_del_char_device(chip); in tpm_chip_unregister()