2 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
26 #include <linux/poll.h>
27 #include <linux/mutex.h>
28 #include <linux/spinlock.h>
29 #include <linux/smp_lock.h>
34 TPM_MINOR = 224, /* officially assigned */
36 TPM_NUM_DEVICES = 256,
46 #define TPM_MAX_ORDINAL 243
47 #define TPM_MAX_PROTECTED_ORDINAL 12
48 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
50 static LIST_HEAD(tpm_chip_list);
51 static DEFINE_SPINLOCK(driver_lock);
52 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
55 * Array with one entry per ordinal defining the maximum amount
56 * of time the chip could take to return the result. The ordinal
57 * designation of short, medium or long is defined in a table in
58 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
59 * values of the SHORT, MEDIUM, and LONG durations are retrieved
60 * from the chip during initialization with a call to tpm_get_timeouts.
62 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
63 TPM_UNDEFINED, /* 0 */
68 TPM_UNDEFINED, /* 5 */
77 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
78 TPM_UNDEFINED, /* 0 */
83 TPM_UNDEFINED, /* 5 */
133 TPM_UNDEFINED, /* 55 */
153 TPM_UNDEFINED, /* 75 */
163 TPM_UNDEFINED, /* 85 */
173 TPM_UNDEFINED, /* 95 */
178 TPM_MEDIUM, /* 100 */
183 TPM_UNDEFINED, /* 105 */
213 TPM_UNDEFINED, /* 135 */
223 TPM_UNDEFINED, /* 145 */
233 TPM_UNDEFINED, /* 155 */
243 TPM_UNDEFINED, /* 165 */
253 TPM_UNDEFINED, /* 175 */
258 TPM_MEDIUM, /* 180 */
263 TPM_MEDIUM, /* 185 */
268 TPM_UNDEFINED, /* 190 */
273 TPM_UNDEFINED, /* 195 */
288 TPM_MEDIUM, /* 210 */
293 TPM_UNDEFINED, /* 215 */
303 TPM_UNDEFINED, /* 225 */
313 TPM_UNDEFINED, /* 235 */
323 static void user_reader_timeout(unsigned long ptr)
325 struct tpm_chip *chip = (struct tpm_chip *) ptr;
327 schedule_work(&chip->work);
330 static void timeout_work(struct work_struct *work)
332 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
334 mutex_lock(&chip->buffer_mutex);
335 atomic_set(&chip->data_pending, 0);
336 memset(chip->data_buffer, 0, TPM_BUFSIZE);
337 mutex_unlock(&chip->buffer_mutex);
341 * Returns max number of jiffies to wait
343 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
346 int duration_idx = TPM_UNDEFINED;
349 if (ordinal < TPM_MAX_ORDINAL)
350 duration_idx = tpm_ordinal_duration[ordinal];
351 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
352 TPM_MAX_PROTECTED_ORDINAL)
354 tpm_protected_ordinal_duration[ordinal &
355 TPM_PROTECTED_ORDINAL_MASK];
357 if (duration_idx != TPM_UNDEFINED)
358 duration = chip->vendor.duration[duration_idx];
364 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
367 * Internal kernel interface to transmit TPM commands
369 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
376 count = be32_to_cpu(*((__be32 *) (buf + 2)));
377 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
380 if (count > bufsiz) {
382 "invalid count value %x %zx \n", count, bufsiz);
386 mutex_lock(&chip->tpm_mutex);
388 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
390 "tpm_transmit: tpm_send: error %zd\n", rc);
394 if (chip->vendor.irq)
397 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
399 u8 status = chip->vendor.status(chip);
400 if ((status & chip->vendor.req_complete_mask) ==
401 chip->vendor.req_complete_val)
404 if ((status == chip->vendor.req_canceled)) {
405 dev_err(chip->dev, "Operation Canceled\n");
410 msleep(TPM_TIMEOUT); /* CHECK */
412 } while (time_before(jiffies, stop));
414 chip->vendor.cancel(chip);
415 dev_err(chip->dev, "Operation Timed out\n");
420 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
423 "tpm_transmit: tpm_recv: error %zd\n", rc);
425 mutex_unlock(&chip->tpm_mutex);
429 #define TPM_DIGEST_SIZE 20
430 #define TPM_ERROR_SIZE 10
431 #define TPM_RET_CODE_IDX 6
433 enum tpm_capabilities {
434 TPM_CAP_FLAG = cpu_to_be32(4),
435 TPM_CAP_PROP = cpu_to_be32(5),
436 CAP_VERSION_1_1 = cpu_to_be32(0x06),
437 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
440 enum tpm_sub_capabilities {
441 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
442 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
443 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
444 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
445 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
446 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
447 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
451 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
452 int len, const char *desc)
456 len = tpm_transmit(chip,(u8 *) cmd, len);
459 if (len == TPM_ERROR_SIZE) {
460 err = be32_to_cpu(cmd->header.out.return_code);
461 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
467 #define TPM_INTERNAL_RESULT_SIZE 200
468 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
469 #define TPM_ORD_GET_CAP cpu_to_be32(101)
471 static const struct tpm_input_header tpm_getcap_header = {
472 .tag = TPM_TAG_RQU_COMMAND,
473 .length = cpu_to_be32(22),
474 .ordinal = TPM_ORD_GET_CAP
477 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
480 struct tpm_cmd_t tpm_cmd;
482 struct tpm_chip *chip = dev_get_drvdata(dev);
484 tpm_cmd.header.in = tpm_getcap_header;
485 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
486 tpm_cmd.params.getcap_in.cap = subcap_id;
487 /*subcap field not necessary */
488 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
489 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
491 if (subcap_id == TPM_CAP_FLAG_PERM ||
492 subcap_id == TPM_CAP_FLAG_VOL)
493 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
495 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
496 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
497 tpm_cmd.params.getcap_in.subcap = subcap_id;
499 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
501 *cap = tpm_cmd.params.getcap_out.cap;
505 void tpm_gen_interrupt(struct tpm_chip *chip)
507 struct tpm_cmd_t tpm_cmd;
510 tpm_cmd.header.in = tpm_getcap_header;
511 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
512 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
513 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
515 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
516 "attempting to determine the timeouts");
518 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
520 void tpm_get_timeouts(struct tpm_chip *chip)
522 struct tpm_cmd_t tpm_cmd;
523 struct timeout_t *timeout_cap;
524 struct duration_t *duration_cap;
528 tpm_cmd.header.in = tpm_getcap_header;
529 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
530 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
531 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
533 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
534 "attempting to determine the timeouts");
538 if (be32_to_cpu(tpm_cmd.header.out.length)
542 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
543 /* Don't overwrite default if value is 0 */
544 timeout = be32_to_cpu(timeout_cap->a);
546 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
547 timeout = be32_to_cpu(timeout_cap->b);
549 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
550 timeout = be32_to_cpu(timeout_cap->c);
552 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
553 timeout = be32_to_cpu(timeout_cap->d);
555 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
558 tpm_cmd.header.in = tpm_getcap_header;
559 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
560 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
561 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
563 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
564 "attempting to determine the durations");
568 if (be32_to_cpu(tpm_cmd.header.out.return_code)
571 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
572 chip->vendor.duration[TPM_SHORT] =
573 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
574 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
575 * value wrong and apparently reports msecs rather than usecs. So we
576 * fix up the resulting too-small TPM_SHORT value to make things work.
578 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
579 chip->vendor.duration[TPM_SHORT] = HZ;
581 chip->vendor.duration[TPM_MEDIUM] =
582 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
583 chip->vendor.duration[TPM_LONG] =
584 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
586 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
588 void tpm_continue_selftest(struct tpm_chip *chip)
591 0, 193, /* TPM_TAG_RQU_COMMAND */
592 0, 0, 0, 10, /* length */
593 0, 0, 0, 83, /* TPM_ORD_GetCapability */
596 tpm_transmit(chip, data, sizeof(data));
598 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
600 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
606 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
607 "attempting to determine the permanent enabled state");
611 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
614 EXPORT_SYMBOL_GPL(tpm_show_enabled);
616 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
622 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
623 "attempting to determine the permanent active state");
627 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
630 EXPORT_SYMBOL_GPL(tpm_show_active);
632 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
638 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
639 "attempting to determine the owner state");
643 rc = sprintf(buf, "%d\n", cap.owned);
646 EXPORT_SYMBOL_GPL(tpm_show_owned);
648 ssize_t tpm_show_temp_deactivated(struct device * dev,
649 struct device_attribute * attr, char *buf)
654 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
655 "attempting to determine the temporary state");
659 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
662 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
664 static const u8 pcrread[] = {
665 0, 193, /* TPM_TAG_RQU_COMMAND */
666 0, 0, 0, 14, /* length */
667 0, 0, 0, 21, /* TPM_ORD_PcrRead */
668 0, 0, 0, 0 /* PCR index */
671 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
680 struct tpm_chip *chip = dev_get_drvdata(dev);
682 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
686 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
687 "attempting to determine the number of PCRS");
691 num_pcrs = be32_to_cpu(cap.num_pcrs);
692 for (i = 0; i < num_pcrs; i++) {
693 memcpy(data, pcrread, sizeof(pcrread));
694 index = cpu_to_be32(i);
695 memcpy(data + 10, &index, 4);
696 rc = transmit_cmd(chip, (struct tpm_cmd_t *)data,
697 TPM_INTERNAL_RESULT_SIZE,
698 "attempting to read a PCR");
701 str += sprintf(str, "PCR-%02d: ", i);
702 for (j = 0; j < TPM_DIGEST_SIZE; j++)
703 str += sprintf(str, "%02X ", *(data + 10 + j));
704 str += sprintf(str, "\n");
709 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
711 #define READ_PUBEK_RESULT_SIZE 314
712 #define TPM_ORD_READPUBEK cpu_to_be32(124)
713 struct tpm_input_header tpm_readpubek_header = {
714 .tag = TPM_TAG_RQU_COMMAND,
715 .length = cpu_to_be32(30),
716 .ordinal = TPM_ORD_READPUBEK
719 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
723 struct tpm_cmd_t tpm_cmd;
728 struct tpm_chip *chip = dev_get_drvdata(dev);
730 tpm_cmd.header.in = tpm_readpubek_header;
731 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
732 "attempting to read the PUBEK");
737 ignore header 10 bytes
738 algorithm 32 bits (1 == RSA )
741 parameters (RSA 12->bytes: keybit, #primes, expbit)
744 ignore checksum 20 bytes
746 data = tpm_cmd.params.readpubek_out_buffer;
749 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
750 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
751 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
752 "Modulus length: %d\nModulus: \n",
753 data[10], data[11], data[12], data[13], data[14],
754 data[15], data[16], data[17], data[22], data[23],
755 data[24], data[25], data[26], data[27], data[28],
756 data[29], data[30], data[31], data[32], data[33],
757 be32_to_cpu(*((__be32 *) (data + 34))));
759 for (i = 0; i < 256; i++) {
760 str += sprintf(str, "%02X ", data[i + 38]);
761 if ((i + 1) % 16 == 0)
762 str += sprintf(str, "\n");
768 EXPORT_SYMBOL_GPL(tpm_show_pubek);
771 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
778 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
779 "attempting to determine the manufacturer");
782 str += sprintf(str, "Manufacturer: 0x%x\n",
783 be32_to_cpu(cap.manufacturer_id));
785 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
786 "attempting to determine the 1.1 version");
790 "TCG version: %d.%d\nFirmware version: %d.%d\n",
791 cap.tpm_version.Major, cap.tpm_version.Minor,
792 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
795 EXPORT_SYMBOL_GPL(tpm_show_caps);
797 ssize_t tpm_show_caps_1_2(struct device * dev,
798 struct device_attribute * attr, char *buf)
804 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
805 "attempting to determine the manufacturer");
808 str += sprintf(str, "Manufacturer: 0x%x\n",
809 be32_to_cpu(cap.manufacturer_id));
810 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
811 "attempting to determine the 1.2 version");
815 "TCG version: %d.%d\nFirmware version: %d.%d\n",
816 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
817 cap.tpm_version_1_2.revMajor,
818 cap.tpm_version_1_2.revMinor);
821 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
823 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
824 const char *buf, size_t count)
826 struct tpm_chip *chip = dev_get_drvdata(dev);
830 chip->vendor.cancel(chip);
833 EXPORT_SYMBOL_GPL(tpm_store_cancel);
836 * Device file system interface to the TPM
838 * It's assured that the chip will be opened just once,
839 * by the check of is_open variable, which is protected
842 int tpm_open(struct inode *inode, struct file *file)
844 int minor = iminor(inode);
845 struct tpm_chip *chip = NULL, *pos;
848 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
849 if (pos->vendor.miscdev.minor == minor) {
851 get_device(chip->dev);
860 if (test_and_set_bit(0, &chip->is_open)) {
861 dev_dbg(chip->dev, "Another process owns this TPM\n");
862 put_device(chip->dev);
866 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
867 if (chip->data_buffer == NULL) {
868 clear_bit(0, &chip->is_open);
869 put_device(chip->dev);
873 atomic_set(&chip->data_pending, 0);
875 file->private_data = chip;
878 EXPORT_SYMBOL_GPL(tpm_open);
881 * Called on file close
883 int tpm_release(struct inode *inode, struct file *file)
885 struct tpm_chip *chip = file->private_data;
887 del_singleshot_timer_sync(&chip->user_read_timer);
888 flush_scheduled_work();
889 file->private_data = NULL;
890 atomic_set(&chip->data_pending, 0);
891 kfree(chip->data_buffer);
892 clear_bit(0, &chip->is_open);
893 put_device(chip->dev);
896 EXPORT_SYMBOL_GPL(tpm_release);
898 ssize_t tpm_write(struct file *file, const char __user *buf,
899 size_t size, loff_t *off)
901 struct tpm_chip *chip = file->private_data;
902 size_t in_size = size, out_size;
904 /* cannot perform a write until the read has cleared
905 either via tpm_read or a user_read_timer timeout */
906 while (atomic_read(&chip->data_pending) != 0)
909 mutex_lock(&chip->buffer_mutex);
911 if (in_size > TPM_BUFSIZE)
912 in_size = TPM_BUFSIZE;
915 (chip->data_buffer, (void __user *) buf, in_size)) {
916 mutex_unlock(&chip->buffer_mutex);
920 /* atomic tpm command send and result receive */
921 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
923 atomic_set(&chip->data_pending, out_size);
924 mutex_unlock(&chip->buffer_mutex);
926 /* Set a timeout by which the reader must come claim the result */
927 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
931 EXPORT_SYMBOL_GPL(tpm_write);
933 ssize_t tpm_read(struct file *file, char __user *buf,
934 size_t size, loff_t *off)
936 struct tpm_chip *chip = file->private_data;
939 del_singleshot_timer_sync(&chip->user_read_timer);
940 flush_scheduled_work();
941 ret_size = atomic_read(&chip->data_pending);
942 atomic_set(&chip->data_pending, 0);
943 if (ret_size > 0) { /* relay data */
947 mutex_lock(&chip->buffer_mutex);
948 if (copy_to_user(buf, chip->data_buffer, ret_size))
950 mutex_unlock(&chip->buffer_mutex);
955 EXPORT_SYMBOL_GPL(tpm_read);
957 void tpm_remove_hardware(struct device *dev)
959 struct tpm_chip *chip = dev_get_drvdata(dev);
962 dev_err(dev, "No device data found\n");
966 spin_lock(&driver_lock);
967 list_del_rcu(&chip->list);
968 spin_unlock(&driver_lock);
971 misc_deregister(&chip->vendor.miscdev);
972 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
973 tpm_bios_log_teardown(chip->bios_dir);
975 /* write it this way to be explicit (chip->dev == dev) */
976 put_device(chip->dev);
978 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
981 * We are about to suspend. Save the TPM state
982 * so that it can be restored.
984 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
986 struct tpm_chip *chip = dev_get_drvdata(dev);
988 0, 193, /* TPM_TAG_RQU_COMMAND */
989 0, 0, 0, 10, /* blob length (in bytes) */
990 0, 0, 0, 152 /* TPM_ORD_SaveState */
996 tpm_transmit(chip, savestate, sizeof(savestate));
999 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1002 * Resume from a power safe. The BIOS already restored
1005 int tpm_pm_resume(struct device *dev)
1007 struct tpm_chip *chip = dev_get_drvdata(dev);
1014 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1016 /* In case vendor provided release function, call it too.*/
1018 void tpm_dev_vendor_release(struct tpm_chip *chip)
1020 if (chip->vendor.release)
1021 chip->vendor.release(chip->dev);
1023 clear_bit(chip->dev_num, dev_mask);
1024 kfree(chip->vendor.miscdev.name);
1026 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1030 * Once all references to platform device are down to 0,
1031 * release all allocated structures.
1033 void tpm_dev_release(struct device *dev)
1035 struct tpm_chip *chip = dev_get_drvdata(dev);
1037 tpm_dev_vendor_release(chip);
1042 EXPORT_SYMBOL_GPL(tpm_dev_release);
1045 * Called from tpm_<specific>.c probe function only for devices
1046 * the driver has determined it should claim. Prior to calling
1047 * this function the specific probe function has called pci_enable_device
1048 * upon errant exit from this function specific probe function should call
1049 * pci_disable_device
1051 struct tpm_chip *tpm_register_hardware(struct device *dev,
1052 const struct tpm_vendor_specific *entry)
1054 #define DEVNAME_SIZE 7
1057 struct tpm_chip *chip;
1059 /* Driver specific per-device data */
1060 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1061 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1063 if (chip == NULL || devname == NULL)
1066 mutex_init(&chip->buffer_mutex);
1067 mutex_init(&chip->tpm_mutex);
1068 INIT_LIST_HEAD(&chip->list);
1070 INIT_WORK(&chip->work, timeout_work);
1072 setup_timer(&chip->user_read_timer, user_reader_timeout,
1073 (unsigned long)chip);
1075 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1077 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1079 if (chip->dev_num >= TPM_NUM_DEVICES) {
1080 dev_err(dev, "No available tpm device numbers\n");
1082 } else if (chip->dev_num == 0)
1083 chip->vendor.miscdev.minor = TPM_MINOR;
1085 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1087 set_bit(chip->dev_num, dev_mask);
1089 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1090 chip->vendor.miscdev.name = devname;
1092 chip->vendor.miscdev.parent = dev;
1093 chip->dev = get_device(dev);
1094 chip->release = dev->release;
1095 dev->release = tpm_dev_release;
1096 dev_set_drvdata(dev, chip);
1098 if (misc_register(&chip->vendor.miscdev)) {
1100 "unable to misc_register %s, minor %d\n",
1101 chip->vendor.miscdev.name,
1102 chip->vendor.miscdev.minor);
1103 put_device(chip->dev);
1107 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1108 misc_deregister(&chip->vendor.miscdev);
1109 put_device(chip->dev);
1114 chip->bios_dir = tpm_bios_log_setup(devname);
1116 /* Make chip available */
1117 spin_lock(&driver_lock);
1118 list_add_rcu(&chip->list, &tpm_chip_list);
1119 spin_unlock(&driver_lock);
1128 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1130 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1131 MODULE_DESCRIPTION("TPM Driver");
1132 MODULE_VERSION("2.0");
1133 MODULE_LICENSE("GPL");