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/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
32 TPM_MINOR = 224, /* officially assigned */
34 TPM_NUM_DEVICES = 256,
35 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
45 #define TPM_MAX_ORDINAL 243
46 #define TPM_MAX_PROTECTED_ORDINAL 12
47 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
49 static LIST_HEAD(tpm_chip_list);
50 static DEFINE_SPINLOCK(driver_lock);
51 static int dev_mask[TPM_NUM_MASK_ENTRIES];
54 * Array with one entry per ordinal defining the maximum amount
55 * of time the chip could take to return the result. The ordinal
56 * designation of short, medium or long is defined in a table in
57 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
58 * values of the SHORT, MEDIUM, and LONG durations are retrieved
59 * from the chip during initialization with a call to tpm_get_timeouts.
61 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
62 TPM_UNDEFINED, /* 0 */
67 TPM_UNDEFINED, /* 5 */
76 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
77 TPM_UNDEFINED, /* 0 */
82 TPM_UNDEFINED, /* 5 */
132 TPM_UNDEFINED, /* 55 */
152 TPM_UNDEFINED, /* 75 */
162 TPM_UNDEFINED, /* 85 */
172 TPM_UNDEFINED, /* 95 */
177 TPM_MEDIUM, /* 100 */
182 TPM_UNDEFINED, /* 105 */
212 TPM_UNDEFINED, /* 135 */
222 TPM_UNDEFINED, /* 145 */
232 TPM_UNDEFINED, /* 155 */
242 TPM_UNDEFINED, /* 165 */
252 TPM_UNDEFINED, /* 175 */
257 TPM_MEDIUM, /* 180 */
262 TPM_MEDIUM, /* 185 */
267 TPM_UNDEFINED, /* 190 */
272 TPM_UNDEFINED, /* 195 */
287 TPM_MEDIUM, /* 210 */
292 TPM_UNDEFINED, /* 215 */
302 TPM_UNDEFINED, /* 225 */
312 TPM_UNDEFINED, /* 235 */
322 static void user_reader_timeout(unsigned long ptr)
324 struct tpm_chip *chip = (struct tpm_chip *) ptr;
326 schedule_work(&chip->work);
329 static void timeout_work(void *ptr)
331 struct tpm_chip *chip = ptr;
333 down(&chip->buffer_mutex);
334 atomic_set(&chip->data_pending, 0);
335 memset(chip->data_buffer, 0, TPM_BUFSIZE);
336 up(&chip->buffer_mutex);
340 * Returns max number of jiffies to wait
342 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
345 int duration_idx = TPM_UNDEFINED;
348 if (ordinal < TPM_MAX_ORDINAL)
349 duration_idx = tpm_ordinal_duration[ordinal];
350 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
351 TPM_MAX_PROTECTED_ORDINAL)
353 tpm_protected_ordinal_duration[ordinal &
354 TPM_PROTECTED_ORDINAL_MASK];
356 if (duration_idx != TPM_UNDEFINED)
357 duration = chip->vendor.duration[duration_idx] * HZ / 1000;
363 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
366 * Internal kernel interface to transmit TPM commands
368 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
375 count = be32_to_cpu(*((__be32 *) (buf + 2)));
376 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
379 if (count > bufsiz) {
381 "invalid count value %x %zx \n", count, bufsiz);
385 down(&chip->tpm_mutex);
387 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
389 "tpm_transmit: tpm_send: error %zd\n", rc);
393 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
395 u8 status = chip->vendor.status(chip);
396 if ((status & chip->vendor.req_complete_mask) ==
397 chip->vendor.req_complete_val)
400 if ((status == chip->vendor.req_canceled)) {
401 dev_err(chip->dev, "Operation Canceled\n");
406 msleep(TPM_TIMEOUT); /* CHECK */
408 } while (time_before(jiffies, stop));
410 chip->vendor.cancel(chip);
411 dev_err(chip->dev, "Operation Timed out\n");
416 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
419 "tpm_transmit: tpm_recv: error %zd\n", rc);
421 up(&chip->tpm_mutex);
425 #define TPM_DIGEST_SIZE 20
426 #define TPM_ERROR_SIZE 10
427 #define TPM_RET_CODE_IDX 6
428 #define TPM_GET_CAP_RET_SIZE_IDX 10
429 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
430 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
431 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
432 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
433 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
434 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
435 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
436 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
438 #define TPM_CAP_IDX 13
439 #define TPM_CAP_SUBCAP_IDX 21
441 enum tpm_capabilities {
446 enum tpm_sub_capabilities {
447 TPM_CAP_PROP_PCR = 0x1,
448 TPM_CAP_PROP_MANUFACTURER = 0x3,
449 TPM_CAP_FLAG_PERM = 0x8,
450 TPM_CAP_FLAG_VOL = 0x9,
451 TPM_CAP_PROP_OWNER = 0x11,
452 TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
453 TPM_CAP_PROP_TIS_DURATION = 0x20,
457 * This is a semi generic GetCapability command for use
458 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
459 * and their associated sub_capabilities.
462 static const u8 tpm_cap[] = {
463 0, 193, /* TPM_TAG_RQU_COMMAND */
464 0, 0, 0, 22, /* length */
465 0, 0, 0, 101, /* TPM_ORD_GetCapability */
466 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
467 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
468 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
471 static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
476 len = tpm_transmit(chip, data, len);
479 if (len == TPM_ERROR_SIZE) {
480 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
481 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
487 void tpm_gen_interrupt(struct tpm_chip *chip)
489 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
492 memcpy(data, tpm_cap, sizeof(tpm_cap));
493 data[TPM_CAP_IDX] = TPM_CAP_PROP;
494 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
496 rc = transmit_cmd(chip, data, sizeof(data),
497 "attempting to determine the timeouts");
499 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
501 void tpm_get_timeouts(struct tpm_chip *chip)
503 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
507 memcpy(data, tpm_cap, sizeof(tpm_cap));
508 data[TPM_CAP_IDX] = TPM_CAP_PROP;
509 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
511 rc = transmit_cmd(chip, data, sizeof(data),
512 "attempting to determine the timeouts");
516 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
520 /* Don't overwrite default if value is 0 */
522 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
524 chip->vendor.timeout_a = timeout;
526 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
528 chip->vendor.timeout_b = timeout;
530 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
532 chip->vendor.timeout_c = timeout;
534 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
536 chip->vendor.timeout_d = timeout;
539 memcpy(data, tpm_cap, sizeof(tpm_cap));
540 data[TPM_CAP_IDX] = TPM_CAP_PROP;
541 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
543 rc = transmit_cmd(chip, data, sizeof(data),
544 "attempting to determine the durations");
548 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
552 chip->vendor.duration[TPM_SHORT] =
553 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
554 chip->vendor.duration[TPM_MEDIUM] =
555 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
556 chip->vendor.duration[TPM_LONG] =
557 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
559 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
561 void tpm_continue_selftest(struct tpm_chip *chip)
564 0, 193, /* TPM_TAG_RQU_COMMAND */
565 0, 0, 0, 10, /* length */
566 0, 0, 0, 83, /* TPM_ORD_GetCapability */
569 tpm_transmit(chip, data, sizeof(data));
571 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
573 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
576 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
579 struct tpm_chip *chip = dev_get_drvdata(dev);
583 memcpy(data, tpm_cap, sizeof(tpm_cap));
584 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
585 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
587 rc = transmit_cmd(chip, data, sizeof(data),
588 "attemtping to determine the permanent state");
591 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
593 EXPORT_SYMBOL_GPL(tpm_show_enabled);
595 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
598 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
601 struct tpm_chip *chip = dev_get_drvdata(dev);
605 memcpy(data, tpm_cap, sizeof(tpm_cap));
606 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
607 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
609 rc = transmit_cmd(chip, data, sizeof(data),
610 "attemtping to determine the permanent state");
613 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
615 EXPORT_SYMBOL_GPL(tpm_show_active);
617 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
620 u8 data[sizeof(tpm_cap)];
623 struct tpm_chip *chip = dev_get_drvdata(dev);
627 memcpy(data, tpm_cap, sizeof(tpm_cap));
628 data[TPM_CAP_IDX] = TPM_CAP_PROP;
629 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
631 rc = transmit_cmd(chip, data, sizeof(data),
632 "attempting to determine the owner state");
635 return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
637 EXPORT_SYMBOL_GPL(tpm_show_owned);
639 ssize_t tpm_show_temp_deactivated(struct device * dev,
640 struct device_attribute * attr, char *buf)
642 u8 data[sizeof(tpm_cap)];
645 struct tpm_chip *chip = dev_get_drvdata(dev);
649 memcpy(data, tpm_cap, sizeof(tpm_cap));
650 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
651 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
653 rc = transmit_cmd(chip, data, sizeof(data),
654 "attempting to determine the temporary state");
657 return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
659 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
661 static const u8 pcrread[] = {
662 0, 193, /* TPM_TAG_RQU_COMMAND */
663 0, 0, 0, 14, /* length */
664 0, 0, 0, 21, /* TPM_ORD_PcrRead */
665 0, 0, 0, 0 /* PCR index */
668 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
671 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
677 struct tpm_chip *chip = dev_get_drvdata(dev);
681 memcpy(data, tpm_cap, sizeof(tpm_cap));
682 data[TPM_CAP_IDX] = TPM_CAP_PROP;
683 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
685 rc = transmit_cmd(chip, data, sizeof(data),
686 "attempting to determine the number of PCRS");
690 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
691 for (i = 0; i < num_pcrs; i++) {
692 memcpy(data, pcrread, sizeof(pcrread));
693 index = cpu_to_be32(i);
694 memcpy(data + 10, &index, 4);
695 rc = transmit_cmd(chip, data, sizeof(data),
696 "attempting to read a PCR");
699 str += sprintf(str, "PCR-%02d: ", i);
700 for (j = 0; j < TPM_DIGEST_SIZE; j++)
701 str += sprintf(str, "%02X ", *(data + 10 + j));
702 str += sprintf(str, "\n");
707 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
709 #define READ_PUBEK_RESULT_SIZE 314
710 static const u8 readpubek[] = {
711 0, 193, /* TPM_TAG_RQU_COMMAND */
712 0, 0, 0, 30, /* length */
713 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
716 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
724 struct tpm_chip *chip = dev_get_drvdata(dev);
728 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
732 memcpy(data, readpubek, sizeof(readpubek));
734 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
735 "attempting to read the PUBEK");
740 ignore header 10 bytes
741 algorithm 32 bits (1 == RSA )
744 parameters (RSA 12->bytes: keybit, #primes, expbit)
747 ignore checksum 20 bytes
752 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
753 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
754 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
755 "Modulus length: %d\nModulus: \n",
756 data[10], data[11], data[12], data[13], data[14],
757 data[15], data[16], data[17], data[22], data[23],
758 data[24], data[25], data[26], data[27], data[28],
759 data[29], data[30], data[31], data[32], data[33],
760 be32_to_cpu(*((__be32 *) (data + 34))));
762 for (i = 0; i < 256; i++) {
763 str += sprintf(str, "%02X ", data[i + 38]);
764 if ((i + 1) % 16 == 0)
765 str += sprintf(str, "\n");
772 EXPORT_SYMBOL_GPL(tpm_show_pubek);
774 #define CAP_VERSION_1_1 6
775 #define CAP_VERSION_1_2 0x1A
776 #define CAP_VERSION_IDX 13
777 static const u8 cap_version[] = {
778 0, 193, /* TPM_TAG_RQU_COMMAND */
779 0, 0, 0, 18, /* length */
780 0, 0, 0, 101, /* TPM_ORD_GetCapability */
785 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
788 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
792 struct tpm_chip *chip = dev_get_drvdata(dev);
796 memcpy(data, tpm_cap, sizeof(tpm_cap));
797 data[TPM_CAP_IDX] = TPM_CAP_PROP;
798 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
800 rc = transmit_cmd(chip, data, sizeof(data),
801 "attempting to determine the manufacturer");
805 str += sprintf(str, "Manufacturer: 0x%x\n",
806 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
808 memcpy(data, cap_version, sizeof(cap_version));
809 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
810 rc = transmit_cmd(chip, data, sizeof(data),
811 "attempting to determine the 1.1 version");
816 "TCG version: %d.%d\nFirmware version: %d.%d\n",
817 (int) data[14], (int) data[15], (int) data[16],
823 EXPORT_SYMBOL_GPL(tpm_show_caps);
825 ssize_t tpm_show_caps_1_2(struct device * dev,
826 struct device_attribute * attr, char *buf)
828 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
832 struct tpm_chip *chip = dev_get_drvdata(dev);
836 memcpy(data, tpm_cap, sizeof(tpm_cap));
837 data[TPM_CAP_IDX] = TPM_CAP_PROP;
838 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
840 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
842 dev_dbg(chip->dev, "A TPM error (%d) occurred "
843 "attempting to determine the manufacturer\n",
844 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
848 str += sprintf(str, "Manufacturer: 0x%x\n",
849 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
851 memcpy(data, cap_version, sizeof(cap_version));
852 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
854 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
856 dev_err(chip->dev, "A TPM error (%d) occurred "
857 "attempting to determine the 1.2 version\n",
858 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
862 "TCG version: %d.%d\nFirmware version: %d.%d\n",
863 (int) data[16], (int) data[17], (int) data[18],
869 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
871 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
872 const char *buf, size_t count)
874 struct tpm_chip *chip = dev_get_drvdata(dev);
878 chip->vendor.cancel(chip);
881 EXPORT_SYMBOL_GPL(tpm_store_cancel);
884 * Device file system interface to the TPM
886 int tpm_open(struct inode *inode, struct file *file)
888 int rc = 0, minor = iminor(inode);
889 struct tpm_chip *chip = NULL, *pos;
891 spin_lock(&driver_lock);
893 list_for_each_entry(pos, &tpm_chip_list, list) {
894 if (pos->vendor.miscdev.minor == minor) {
905 if (chip->num_opens) {
906 dev_dbg(chip->dev, "Another process owns this TPM\n");
912 get_device(chip->dev);
914 spin_unlock(&driver_lock);
916 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
917 if (chip->data_buffer == NULL) {
919 put_device(chip->dev);
923 atomic_set(&chip->data_pending, 0);
925 file->private_data = chip;
929 spin_unlock(&driver_lock);
932 EXPORT_SYMBOL_GPL(tpm_open);
934 int tpm_release(struct inode *inode, struct file *file)
936 struct tpm_chip *chip = file->private_data;
938 spin_lock(&driver_lock);
939 file->private_data = NULL;
941 del_singleshot_timer_sync(&chip->user_read_timer);
942 flush_scheduled_work();
943 atomic_set(&chip->data_pending, 0);
944 put_device(chip->dev);
945 kfree(chip->data_buffer);
946 spin_unlock(&driver_lock);
949 EXPORT_SYMBOL_GPL(tpm_release);
951 ssize_t tpm_write(struct file *file, const char __user *buf,
952 size_t size, loff_t *off)
954 struct tpm_chip *chip = file->private_data;
955 int in_size = size, out_size;
957 /* cannot perform a write until the read has cleared
958 either via tpm_read or a user_read_timer timeout */
959 while (atomic_read(&chip->data_pending) != 0)
962 down(&chip->buffer_mutex);
964 if (in_size > TPM_BUFSIZE)
965 in_size = TPM_BUFSIZE;
968 (chip->data_buffer, (void __user *) buf, in_size)) {
969 up(&chip->buffer_mutex);
973 /* atomic tpm command send and result receive */
974 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
976 atomic_set(&chip->data_pending, out_size);
977 up(&chip->buffer_mutex);
979 /* Set a timeout by which the reader must come claim the result */
980 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
984 EXPORT_SYMBOL_GPL(tpm_write);
986 ssize_t tpm_read(struct file *file, char __user *buf,
987 size_t size, loff_t *off)
989 struct tpm_chip *chip = file->private_data;
992 del_singleshot_timer_sync(&chip->user_read_timer);
993 flush_scheduled_work();
994 ret_size = atomic_read(&chip->data_pending);
995 atomic_set(&chip->data_pending, 0);
996 if (ret_size > 0) { /* relay data */
1000 down(&chip->buffer_mutex);
1001 if (copy_to_user(buf, chip->data_buffer, ret_size))
1003 up(&chip->buffer_mutex);
1008 EXPORT_SYMBOL_GPL(tpm_read);
1010 void tpm_remove_hardware(struct device *dev)
1012 struct tpm_chip *chip = dev_get_drvdata(dev);
1015 dev_err(dev, "No device data found\n");
1019 spin_lock(&driver_lock);
1021 list_del(&chip->list);
1023 spin_unlock(&driver_lock);
1025 dev_set_drvdata(dev, NULL);
1026 misc_deregister(&chip->vendor.miscdev);
1027 kfree(chip->vendor.miscdev.name);
1029 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1030 tpm_bios_log_teardown(chip->bios_dir);
1032 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES] &=
1033 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
1039 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1041 static u8 savestate[] = {
1042 0, 193, /* TPM_TAG_RQU_COMMAND */
1043 0, 0, 0, 10, /* blob length (in bytes) */
1044 0, 0, 0, 152 /* TPM_ORD_SaveState */
1048 * We are about to suspend. Save the TPM state
1049 * so that it can be restored.
1051 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1053 struct tpm_chip *chip = dev_get_drvdata(dev);
1057 tpm_transmit(chip, savestate, sizeof(savestate));
1060 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1063 * Resume from a power safe. The BIOS already restored
1066 int tpm_pm_resume(struct device *dev)
1068 struct tpm_chip *chip = dev_get_drvdata(dev);
1075 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1078 * Called from tpm_<specific>.c probe function only for devices
1079 * the driver has determined it should claim. Prior to calling
1080 * this function the specific probe function has called pci_enable_device
1081 * upon errant exit from this function specific probe function should call
1082 * pci_disable_device
1084 struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
1087 #define DEVNAME_SIZE 7
1090 struct tpm_chip *chip;
1093 /* Driver specific per-device data */
1094 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1098 init_MUTEX(&chip->buffer_mutex);
1099 init_MUTEX(&chip->tpm_mutex);
1100 INIT_LIST_HEAD(&chip->list);
1102 INIT_WORK(&chip->work, timeout_work, chip);
1104 init_timer(&chip->user_read_timer);
1105 chip->user_read_timer.function = user_reader_timeout;
1106 chip->user_read_timer.data = (unsigned long) chip;
1108 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1112 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
1113 for (j = 0; j < 8 * sizeof(int); j++)
1114 if ((dev_mask[i] & (1 << j)) == 0) {
1116 i * TPM_NUM_MASK_ENTRIES + j;
1117 dev_mask[i] |= 1 << j;
1118 goto dev_num_search_complete;
1121 dev_num_search_complete:
1122 if (chip->dev_num < 0) {
1123 dev_err(dev, "No available tpm device numbers\n");
1126 } else if (chip->dev_num == 0)
1127 chip->vendor.miscdev.minor = TPM_MINOR;
1129 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1131 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1132 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1133 chip->vendor.miscdev.name = devname;
1135 chip->vendor.miscdev.dev = dev;
1136 chip->dev = get_device(dev);
1138 if (misc_register(&chip->vendor.miscdev)) {
1140 "unable to misc_register %s, minor %d\n",
1141 chip->vendor.miscdev.name,
1142 chip->vendor.miscdev.minor);
1145 dev_mask[i] &= !(1 << j);
1149 spin_lock(&driver_lock);
1151 dev_set_drvdata(dev, chip);
1153 list_add(&chip->list, &tpm_chip_list);
1155 spin_unlock(&driver_lock);
1157 sysfs_create_group(&dev->kobj, chip->vendor.attr_group);
1159 chip->bios_dir = tpm_bios_log_setup(devname);
1163 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1165 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1166 MODULE_DESCRIPTION("TPM Driver");
1167 MODULE_VERSION("2.0");
1168 MODULE_LICENSE("GPL");