123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- #include <linux/poll.h>
- #include <linux/slab.h>
- #include <linux/mutex.h>
- #include <linux/spinlock.h>
- #include <linux/freezer.h>
- #include <linux/major.h>
- #include "tpm.h"
- #include "tpm_eventlog.h"
- DEFINE_IDR(dev_nums_idr);
- static DEFINE_MUTEX(idr_lock);
- struct class *tpm_class;
- dev_t tpm_devt;
- int tpm_try_get_ops(struct tpm_chip *chip)
- {
- int rc = -EIO;
- get_device(&chip->dev);
- down_read(&chip->ops_sem);
- if (!chip->ops)
- goto out_lock;
- return 0;
- out_lock:
- up_read(&chip->ops_sem);
- put_device(&chip->dev);
- return rc;
- }
- EXPORT_SYMBOL_GPL(tpm_try_get_ops);
- void tpm_put_ops(struct tpm_chip *chip)
- {
- up_read(&chip->ops_sem);
- put_device(&chip->dev);
- }
- EXPORT_SYMBOL_GPL(tpm_put_ops);
- struct tpm_chip *tpm_chip_find_get(int chip_num)
- {
- struct tpm_chip *chip, *res = NULL;
- int chip_prev;
- mutex_lock(&idr_lock);
- if (chip_num == TPM_ANY_NUM) {
- chip_num = 0;
- do {
- chip_prev = chip_num;
- chip = idr_get_next(&dev_nums_idr, &chip_num);
- if (chip && !tpm_try_get_ops(chip)) {
- res = chip;
- break;
- }
- } while (chip_prev != chip_num);
- } else {
- chip = idr_find_slowpath(&dev_nums_idr, chip_num);
- if (chip && !tpm_try_get_ops(chip))
- res = chip;
- }
- mutex_unlock(&idr_lock);
- return res;
- }
- static void tpm_dev_release(struct device *dev)
- {
- struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
- mutex_lock(&idr_lock);
- idr_remove(&dev_nums_idr, chip->dev_num);
- mutex_unlock(&idr_lock);
- kfree(chip);
- }
- static int tpm_class_shutdown(struct device *dev)
- {
- struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
- if (chip->flags & TPM_CHIP_FLAG_TPM2) {
- down_write(&chip->ops_sem);
- tpm2_shutdown(chip, TPM2_SU_CLEAR);
- chip->ops = NULL;
- up_write(&chip->ops_sem);
- }
-
- if (dev->bus && dev->bus->shutdown)
- dev->bus->shutdown(dev);
- else if (dev->driver && dev->driver->shutdown)
- dev->driver->shutdown(dev);
- return 0;
- }
- struct tpm_chip *tpm_chip_alloc(struct device *pdev,
- const struct tpm_class_ops *ops)
- {
- struct tpm_chip *chip;
- int rc;
- chip = kzalloc(sizeof(*chip), GFP_KERNEL);
- if (chip == NULL)
- return ERR_PTR(-ENOMEM);
- mutex_init(&chip->tpm_mutex);
- init_rwsem(&chip->ops_sem);
- chip->ops = ops;
- mutex_lock(&idr_lock);
- rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
- mutex_unlock(&idr_lock);
- if (rc < 0) {
- dev_err(pdev, "No available tpm device numbers\n");
- kfree(chip);
- return ERR_PTR(rc);
- }
- chip->dev_num = rc;
- device_initialize(&chip->dev);
- chip->dev.class = tpm_class;
- chip->dev.class->shutdown = tpm_class_shutdown;
- chip->dev.release = tpm_dev_release;
- chip->dev.parent = pdev;
- chip->dev.groups = chip->groups;
- if (chip->dev_num == 0)
- chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
- else
- chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
- rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
- if (rc)
- goto out;
- if (!pdev)
- chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
- cdev_init(&chip->cdev, &tpm_fops);
- chip->cdev.owner = THIS_MODULE;
- chip->cdev.kobj.parent = &chip->dev.kobj;
- return chip;
- out:
- put_device(&chip->dev);
- return ERR_PTR(rc);
- }
- EXPORT_SYMBOL_GPL(tpm_chip_alloc);
- struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
- const struct tpm_class_ops *ops)
- {
- struct tpm_chip *chip;
- int rc;
- chip = tpm_chip_alloc(pdev, ops);
- if (IS_ERR(chip))
- return chip;
- rc = devm_add_action_or_reset(pdev,
- (void (*)(void *)) put_device,
- &chip->dev);
- if (rc)
- return ERR_PTR(rc);
- dev_set_drvdata(pdev, chip);
- return chip;
- }
- EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
- static int tpm_add_char_device(struct tpm_chip *chip)
- {
- int rc;
- rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
- if (rc) {
- dev_err(&chip->dev,
- "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
- dev_name(&chip->dev), MAJOR(chip->dev.devt),
- MINOR(chip->dev.devt), rc);
- return rc;
- }
- rc = device_add(&chip->dev);
- if (rc) {
- dev_err(&chip->dev,
- "unable to device_register() %s, major %d, minor %d, err=%d\n",
- dev_name(&chip->dev), MAJOR(chip->dev.devt),
- MINOR(chip->dev.devt), rc);
- cdev_del(&chip->cdev);
- return rc;
- }
-
- mutex_lock(&idr_lock);
- idr_replace(&dev_nums_idr, chip, chip->dev_num);
- mutex_unlock(&idr_lock);
- return rc;
- }
- static void tpm_del_char_device(struct tpm_chip *chip)
- {
- cdev_del(&chip->cdev);
- device_del(&chip->dev);
-
- mutex_lock(&idr_lock);
- idr_replace(&dev_nums_idr, NULL, chip->dev_num);
- mutex_unlock(&idr_lock);
-
- down_write(&chip->ops_sem);
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
- tpm2_shutdown(chip, TPM2_SU_CLEAR);
- chip->ops = NULL;
- up_write(&chip->ops_sem);
- }
- static int tpm1_chip_register(struct tpm_chip *chip)
- {
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
- return 0;
- tpm_sysfs_add_device(chip);
- chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
- return 0;
- }
- static void tpm1_chip_unregister(struct tpm_chip *chip)
- {
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
- return;
- if (chip->bios_dir)
- tpm_bios_log_teardown(chip->bios_dir);
- }
- static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
- {
- struct attribute **i;
- if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
- return;
- sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
- for (i = chip->groups[0]->attrs; *i != NULL; ++i)
- sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
- }
- static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
- {
- struct attribute **i;
- int rc;
- if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
- return 0;
- rc = __compat_only_sysfs_link_entry_to_kobj(
- &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
- if (rc && rc != -ENOENT)
- return rc;
-
- for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
- rc = __compat_only_sysfs_link_entry_to_kobj(
- &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
- if (rc) {
- tpm_del_legacy_sysfs(chip);
- return rc;
- }
- }
- return 0;
- }
- int tpm_chip_register(struct tpm_chip *chip)
- {
- int rc;
- if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
- rc = tpm2_auto_startup(chip);
- else
- rc = tpm1_auto_startup(chip);
- if (rc)
- return rc;
- }
- rc = tpm1_chip_register(chip);
- if (rc)
- return rc;
- tpm_add_ppi(chip);
- rc = tpm_add_char_device(chip);
- if (rc) {
- tpm1_chip_unregister(chip);
- return rc;
- }
- chip->flags |= TPM_CHIP_FLAG_REGISTERED;
- rc = tpm_add_legacy_sysfs(chip);
- if (rc) {
- tpm_chip_unregister(chip);
- return rc;
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(tpm_chip_register);
- void tpm_chip_unregister(struct tpm_chip *chip)
- {
- if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
- return;
- tpm_del_legacy_sysfs(chip);
- tpm1_chip_unregister(chip);
- tpm_del_char_device(chip);
- }
- EXPORT_SYMBOL_GPL(tpm_chip_unregister);
|