• Home
  • Raw
  • Download

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 * @dev: device to which the chip is associated.
288 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
290 down_write(&chip->ops_sem); in tpm_class_shutdown()
291 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
292 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
293 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
294 tpm_chip_stop(chip); in tpm_class_shutdown()
297 chip->ops = NULL; in tpm_class_shutdown()
298 up_write(&chip->ops_sem); in tpm_class_shutdown()
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; in tpm_chip_alloc() local
319 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
320 if (chip == NULL) in tpm_chip_alloc()
323 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
324 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
326 chip->ops = ops; in tpm_chip_alloc()
333 kfree(chip); in tpm_chip_alloc()
336 chip->dev_num = rc; in tpm_chip_alloc()
338 device_initialize(&chip->dev); in tpm_chip_alloc()
340 chip->dev.class = tpm_class; in tpm_chip_alloc()
341 chip->dev.class->shutdown_pre = tpm_class_shutdown; in tpm_chip_alloc()
342 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
343 chip->dev.parent = pdev; in tpm_chip_alloc()
344 chip->dev.groups = chip->groups; in tpm_chip_alloc()
346 if (chip->dev_num == 0) in tpm_chip_alloc()
347 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
349 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
351 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
356 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
358 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
359 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
361 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); in tpm_chip_alloc()
367 chip->locality = -1; in tpm_chip_alloc()
368 return chip; in tpm_chip_alloc()
371 put_device(&chip->dev); in tpm_chip_alloc()
378 * @pdev: parent device to which the chip is associated
386 struct tpm_chip *chip; in tpmm_chip_alloc() local
389 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
390 if (IS_ERR(chip)) in tpmm_chip_alloc()
391 return chip; in tpmm_chip_alloc()
395 &chip->dev); in tpmm_chip_alloc()
399 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
401 return chip; in tpmm_chip_alloc()
405 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
409 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
411 dev_err(&chip->dev, in tpm_add_char_device()
413 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
414 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
418 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_add_char_device()
419 rc = tpm_devs_add(chip); in tpm_add_char_device()
424 /* Make the chip available. */ in tpm_add_char_device()
426 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
432 cdev_device_del(&chip->cdev, &chip->dev); in tpm_add_char_device()
436 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
438 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
440 /* Make the chip unavailable. */ in tpm_del_char_device()
442 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
446 down_write(&chip->ops_sem); in tpm_del_char_device()
447 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
448 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
449 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
450 tpm_chip_stop(chip); in tpm_del_char_device()
453 chip->ops = NULL; in tpm_del_char_device()
454 up_write(&chip->ops_sem); in tpm_del_char_device()
457 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
461 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) in tpm_del_legacy_sysfs()
464 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
466 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
467 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
471 * parent dev directory to selected names within the tpm chip directory. Old
474 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
479 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) in tpm_add_legacy_sysfs()
483 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); in tpm_add_legacy_sysfs()
488 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
490 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); in tpm_add_legacy_sysfs()
492 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
502 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
504 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
507 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
512 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
513 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
514 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
515 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
516 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
519 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
523 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
524 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
525 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
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) in tpm_chip_register() argument
548 rc = tpm_chip_start(chip); in tpm_chip_register()
551 rc = tpm_auto_startup(chip); in tpm_chip_register()
553 tpm_chip_stop(chip); in tpm_chip_register()
557 rc = tpm_get_pcr_allocation(chip); in tpm_chip_register()
558 tpm_chip_stop(chip); in tpm_chip_register()
562 tpm_sysfs_add_device(chip); in tpm_chip_register()
564 tpm_bios_log_setup(chip); in tpm_chip_register()
566 tpm_add_ppi(chip); in tpm_chip_register()
568 rc = tpm_add_hwrng(chip); in tpm_chip_register()
572 rc = tpm_add_char_device(chip); in tpm_chip_register()
576 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
578 tpm_chip_unregister(chip); in tpm_chip_register()
586 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
588 tpm_bios_log_teardown(chip); in tpm_chip_register()
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) in tpm_chip_unregister() argument
609 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
611 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
612 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
613 if (chip->flags & TPM_CHIP_FLAG_TPM2) in tpm_chip_unregister()
614 tpm_devs_remove(chip); in tpm_chip_unregister()
615 tpm_del_char_device(chip); in tpm_chip_unregister()