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>
33 TPM_MINOR = 224, /* officially assigned */
35 TPM_NUM_DEVICES = 256,
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 DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
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(struct work_struct *work)
331 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
333 mutex_lock(&chip->buffer_mutex);
334 atomic_set(&chip->data_pending, 0);
335 memset(chip->data_buffer, 0, TPM_BUFSIZE);
336 mutex_unlock(&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];
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 mutex_lock(&chip->tpm_mutex);
387 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
389 "tpm_transmit: tpm_send: error %zd\n", rc);
393 if (chip->vendor.irq)
396 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
398 u8 status = chip->vendor.status(chip);
399 if ((status & chip->vendor.req_complete_mask) ==
400 chip->vendor.req_complete_val)
403 if ((status == chip->vendor.req_canceled)) {
404 dev_err(chip->dev, "Operation Canceled\n");
409 msleep(TPM_TIMEOUT); /* CHECK */
411 } while (time_before(jiffies, stop));
413 chip->vendor.cancel(chip);
414 dev_err(chip->dev, "Operation Timed out\n");
419 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
422 "tpm_transmit: tpm_recv: error %zd\n", rc);
424 mutex_unlock(&chip->tpm_mutex);
428 #define TPM_DIGEST_SIZE 20
429 #define TPM_ERROR_SIZE 10
430 #define TPM_RET_CODE_IDX 6
431 #define TPM_GET_CAP_RET_SIZE_IDX 10
432 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
433 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
434 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
435 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
436 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
437 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
438 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
439 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
441 #define TPM_CAP_IDX 13
442 #define TPM_CAP_SUBCAP_IDX 21
444 enum tpm_capabilities {
449 enum tpm_sub_capabilities {
450 TPM_CAP_PROP_PCR = 0x1,
451 TPM_CAP_PROP_MANUFACTURER = 0x3,
452 TPM_CAP_FLAG_PERM = 0x8,
453 TPM_CAP_FLAG_VOL = 0x9,
454 TPM_CAP_PROP_OWNER = 0x11,
455 TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
456 TPM_CAP_PROP_TIS_DURATION = 0x20,
460 * This is a semi generic GetCapability command for use
461 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
462 * and their associated sub_capabilities.
465 static const u8 tpm_cap[] = {
466 0, 193, /* TPM_TAG_RQU_COMMAND */
467 0, 0, 0, 22, /* length */
468 0, 0, 0, 101, /* TPM_ORD_GetCapability */
469 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
470 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
471 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
474 static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
479 len = tpm_transmit(chip, data, len);
482 if (len == TPM_ERROR_SIZE) {
483 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
484 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
490 void tpm_gen_interrupt(struct tpm_chip *chip)
492 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
495 memcpy(data, tpm_cap, sizeof(tpm_cap));
496 data[TPM_CAP_IDX] = TPM_CAP_PROP;
497 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
499 rc = transmit_cmd(chip, data, sizeof(data),
500 "attempting to determine the timeouts");
502 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
504 void tpm_get_timeouts(struct tpm_chip *chip)
506 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
510 memcpy(data, tpm_cap, sizeof(tpm_cap));
511 data[TPM_CAP_IDX] = TPM_CAP_PROP;
512 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
514 rc = transmit_cmd(chip, data, sizeof(data),
515 "attempting to determine the timeouts");
519 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
523 /* Don't overwrite default if value is 0 */
525 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
527 chip->vendor.timeout_a = msecs_to_jiffies(timeout);
529 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
531 chip->vendor.timeout_b = msecs_to_jiffies(timeout);
533 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
535 chip->vendor.timeout_c = msecs_to_jiffies(timeout);
537 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
539 chip->vendor.timeout_d = msecs_to_jiffies(timeout);
542 memcpy(data, tpm_cap, sizeof(tpm_cap));
543 data[TPM_CAP_IDX] = TPM_CAP_PROP;
544 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
546 rc = transmit_cmd(chip, data, sizeof(data),
547 "attempting to determine the durations");
551 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
555 chip->vendor.duration[TPM_SHORT] =
556 msecs_to_jiffies(be32_to_cpu
557 (*((__be32 *) (data +
558 TPM_GET_CAP_RET_UINT32_1_IDX))));
559 chip->vendor.duration[TPM_MEDIUM] =
560 msecs_to_jiffies(be32_to_cpu
561 (*((__be32 *) (data +
562 TPM_GET_CAP_RET_UINT32_2_IDX))));
563 chip->vendor.duration[TPM_LONG] =
564 msecs_to_jiffies(be32_to_cpu
565 (*((__be32 *) (data +
566 TPM_GET_CAP_RET_UINT32_3_IDX))));
568 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
570 void tpm_continue_selftest(struct tpm_chip *chip)
573 0, 193, /* TPM_TAG_RQU_COMMAND */
574 0, 0, 0, 10, /* length */
575 0, 0, 0, 83, /* TPM_ORD_GetCapability */
578 tpm_transmit(chip, data, sizeof(data));
580 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
582 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
585 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
588 struct tpm_chip *chip = dev_get_drvdata(dev);
592 memcpy(data, tpm_cap, sizeof(tpm_cap));
593 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
594 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
596 rc = transmit_cmd(chip, data, sizeof(data),
597 "attemtping to determine the permanent state");
600 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
602 EXPORT_SYMBOL_GPL(tpm_show_enabled);
604 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
607 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
610 struct tpm_chip *chip = dev_get_drvdata(dev);
614 memcpy(data, tpm_cap, sizeof(tpm_cap));
615 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
616 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
618 rc = transmit_cmd(chip, data, sizeof(data),
619 "attemtping to determine the permanent state");
622 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
624 EXPORT_SYMBOL_GPL(tpm_show_active);
626 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
629 u8 data[sizeof(tpm_cap)];
632 struct tpm_chip *chip = dev_get_drvdata(dev);
636 memcpy(data, tpm_cap, sizeof(tpm_cap));
637 data[TPM_CAP_IDX] = TPM_CAP_PROP;
638 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
640 rc = transmit_cmd(chip, data, sizeof(data),
641 "attempting to determine the owner state");
644 return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
646 EXPORT_SYMBOL_GPL(tpm_show_owned);
648 ssize_t tpm_show_temp_deactivated(struct device * dev,
649 struct device_attribute * attr, char *buf)
651 u8 data[sizeof(tpm_cap)];
654 struct tpm_chip *chip = dev_get_drvdata(dev);
658 memcpy(data, tpm_cap, sizeof(tpm_cap));
659 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
660 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
662 rc = transmit_cmd(chip, data, sizeof(data),
663 "attempting to determine the temporary state");
666 return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
668 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
670 static const u8 pcrread[] = {
671 0, 193, /* TPM_TAG_RQU_COMMAND */
672 0, 0, 0, 14, /* length */
673 0, 0, 0, 21, /* TPM_ORD_PcrRead */
674 0, 0, 0, 0 /* PCR index */
677 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
680 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
686 struct tpm_chip *chip = dev_get_drvdata(dev);
690 memcpy(data, tpm_cap, sizeof(tpm_cap));
691 data[TPM_CAP_IDX] = TPM_CAP_PROP;
692 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
694 rc = transmit_cmd(chip, data, sizeof(data),
695 "attempting to determine the number of PCRS");
699 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
700 for (i = 0; i < num_pcrs; i++) {
701 memcpy(data, pcrread, sizeof(pcrread));
702 index = cpu_to_be32(i);
703 memcpy(data + 10, &index, 4);
704 rc = transmit_cmd(chip, data, sizeof(data),
705 "attempting to read a PCR");
708 str += sprintf(str, "PCR-%02d: ", i);
709 for (j = 0; j < TPM_DIGEST_SIZE; j++)
710 str += sprintf(str, "%02X ", *(data + 10 + j));
711 str += sprintf(str, "\n");
716 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
718 #define READ_PUBEK_RESULT_SIZE 314
719 static const u8 readpubek[] = {
720 0, 193, /* TPM_TAG_RQU_COMMAND */
721 0, 0, 0, 30, /* length */
722 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
725 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
733 struct tpm_chip *chip = dev_get_drvdata(dev);
737 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
741 memcpy(data, readpubek, sizeof(readpubek));
743 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
744 "attempting to read the PUBEK");
749 ignore header 10 bytes
750 algorithm 32 bits (1 == RSA )
753 parameters (RSA 12->bytes: keybit, #primes, expbit)
756 ignore checksum 20 bytes
761 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
762 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
763 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
764 "Modulus length: %d\nModulus: \n",
765 data[10], data[11], data[12], data[13], data[14],
766 data[15], data[16], data[17], data[22], data[23],
767 data[24], data[25], data[26], data[27], data[28],
768 data[29], data[30], data[31], data[32], data[33],
769 be32_to_cpu(*((__be32 *) (data + 34))));
771 for (i = 0; i < 256; i++) {
772 str += sprintf(str, "%02X ", data[i + 38]);
773 if ((i + 1) % 16 == 0)
774 str += sprintf(str, "\n");
781 EXPORT_SYMBOL_GPL(tpm_show_pubek);
783 #define CAP_VERSION_1_1 6
784 #define CAP_VERSION_1_2 0x1A
785 #define CAP_VERSION_IDX 13
786 static const u8 cap_version[] = {
787 0, 193, /* TPM_TAG_RQU_COMMAND */
788 0, 0, 0, 18, /* length */
789 0, 0, 0, 101, /* TPM_ORD_GetCapability */
794 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
797 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
801 struct tpm_chip *chip = dev_get_drvdata(dev);
805 memcpy(data, tpm_cap, sizeof(tpm_cap));
806 data[TPM_CAP_IDX] = TPM_CAP_PROP;
807 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
809 rc = transmit_cmd(chip, data, sizeof(data),
810 "attempting to determine the manufacturer");
814 str += sprintf(str, "Manufacturer: 0x%x\n",
815 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
817 memcpy(data, cap_version, sizeof(cap_version));
818 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
819 rc = transmit_cmd(chip, data, sizeof(data),
820 "attempting to determine the 1.1 version");
825 "TCG version: %d.%d\nFirmware version: %d.%d\n",
826 (int) data[14], (int) data[15], (int) data[16],
832 EXPORT_SYMBOL_GPL(tpm_show_caps);
834 ssize_t tpm_show_caps_1_2(struct device * dev,
835 struct device_attribute * attr, char *buf)
837 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
841 struct tpm_chip *chip = dev_get_drvdata(dev);
845 memcpy(data, tpm_cap, sizeof(tpm_cap));
846 data[TPM_CAP_IDX] = TPM_CAP_PROP;
847 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
849 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
851 dev_dbg(chip->dev, "A TPM error (%d) occurred "
852 "attempting to determine the manufacturer\n",
853 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
857 str += sprintf(str, "Manufacturer: 0x%x\n",
858 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
860 memcpy(data, cap_version, sizeof(cap_version));
861 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
863 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
865 dev_err(chip->dev, "A TPM error (%d) occurred "
866 "attempting to determine the 1.2 version\n",
867 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
871 "TCG version: %d.%d\nFirmware version: %d.%d\n",
872 (int) data[16], (int) data[17], (int) data[18],
878 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
880 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
881 const char *buf, size_t count)
883 struct tpm_chip *chip = dev_get_drvdata(dev);
887 chip->vendor.cancel(chip);
890 EXPORT_SYMBOL_GPL(tpm_store_cancel);
893 * Device file system interface to the TPM
895 int tpm_open(struct inode *inode, struct file *file)
897 int rc = 0, minor = iminor(inode);
898 struct tpm_chip *chip = NULL, *pos;
900 spin_lock(&driver_lock);
902 list_for_each_entry(pos, &tpm_chip_list, list) {
903 if (pos->vendor.miscdev.minor == minor) {
914 if (chip->num_opens) {
915 dev_dbg(chip->dev, "Another process owns this TPM\n");
921 get_device(chip->dev);
923 spin_unlock(&driver_lock);
925 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
926 if (chip->data_buffer == NULL) {
928 put_device(chip->dev);
932 atomic_set(&chip->data_pending, 0);
934 file->private_data = chip;
938 spin_unlock(&driver_lock);
941 EXPORT_SYMBOL_GPL(tpm_open);
943 int tpm_release(struct inode *inode, struct file *file)
945 struct tpm_chip *chip = file->private_data;
947 flush_scheduled_work();
948 spin_lock(&driver_lock);
949 file->private_data = NULL;
950 del_singleshot_timer_sync(&chip->user_read_timer);
951 atomic_set(&chip->data_pending, 0);
953 put_device(chip->dev);
954 kfree(chip->data_buffer);
955 spin_unlock(&driver_lock);
958 EXPORT_SYMBOL_GPL(tpm_release);
960 ssize_t tpm_write(struct file *file, const char __user *buf,
961 size_t size, loff_t *off)
963 struct tpm_chip *chip = file->private_data;
964 int in_size = size, out_size;
966 /* cannot perform a write until the read has cleared
967 either via tpm_read or a user_read_timer timeout */
968 while (atomic_read(&chip->data_pending) != 0)
971 mutex_lock(&chip->buffer_mutex);
973 if (in_size > TPM_BUFSIZE)
974 in_size = TPM_BUFSIZE;
977 (chip->data_buffer, (void __user *) buf, in_size)) {
978 mutex_unlock(&chip->buffer_mutex);
982 /* atomic tpm command send and result receive */
983 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
985 atomic_set(&chip->data_pending, out_size);
986 mutex_unlock(&chip->buffer_mutex);
988 /* Set a timeout by which the reader must come claim the result */
989 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
993 EXPORT_SYMBOL_GPL(tpm_write);
995 ssize_t tpm_read(struct file *file, char __user *buf,
996 size_t size, loff_t *off)
998 struct tpm_chip *chip = file->private_data;
1001 del_singleshot_timer_sync(&chip->user_read_timer);
1002 flush_scheduled_work();
1003 ret_size = atomic_read(&chip->data_pending);
1004 atomic_set(&chip->data_pending, 0);
1005 if (ret_size > 0) { /* relay data */
1006 if (size < ret_size)
1009 mutex_lock(&chip->buffer_mutex);
1010 if (copy_to_user(buf, chip->data_buffer, ret_size))
1012 mutex_unlock(&chip->buffer_mutex);
1017 EXPORT_SYMBOL_GPL(tpm_read);
1019 void tpm_remove_hardware(struct device *dev)
1021 struct tpm_chip *chip = dev_get_drvdata(dev);
1024 dev_err(dev, "No device data found\n");
1028 spin_lock(&driver_lock);
1030 list_del(&chip->list);
1032 spin_unlock(&driver_lock);
1034 dev_set_drvdata(dev, NULL);
1035 misc_deregister(&chip->vendor.miscdev);
1036 kfree(chip->vendor.miscdev.name);
1038 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1039 tpm_bios_log_teardown(chip->bios_dir);
1041 clear_bit(chip->dev_num, dev_mask);
1047 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1049 static u8 savestate[] = {
1050 0, 193, /* TPM_TAG_RQU_COMMAND */
1051 0, 0, 0, 10, /* blob length (in bytes) */
1052 0, 0, 0, 152 /* TPM_ORD_SaveState */
1056 * We are about to suspend. Save the TPM state
1057 * so that it can be restored.
1059 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1061 struct tpm_chip *chip = dev_get_drvdata(dev);
1065 tpm_transmit(chip, savestate, sizeof(savestate));
1068 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1071 * Resume from a power safe. The BIOS already restored
1074 int tpm_pm_resume(struct device *dev)
1076 struct tpm_chip *chip = dev_get_drvdata(dev);
1083 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1086 * Called from tpm_<specific>.c probe function only for devices
1087 * the driver has determined it should claim. Prior to calling
1088 * this function the specific probe function has called pci_enable_device
1089 * upon errant exit from this function specific probe function should call
1090 * pci_disable_device
1092 struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
1095 #define DEVNAME_SIZE 7
1098 struct tpm_chip *chip;
1100 /* Driver specific per-device data */
1101 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1102 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1104 if (chip == NULL || devname == NULL) {
1110 mutex_init(&chip->buffer_mutex);
1111 mutex_init(&chip->tpm_mutex);
1112 INIT_LIST_HEAD(&chip->list);
1114 INIT_WORK(&chip->work, timeout_work);
1116 setup_timer(&chip->user_read_timer, user_reader_timeout,
1117 (unsigned long)chip);
1119 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1121 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1123 if (chip->dev_num >= TPM_NUM_DEVICES) {
1124 dev_err(dev, "No available tpm device numbers\n");
1127 } else if (chip->dev_num == 0)
1128 chip->vendor.miscdev.minor = TPM_MINOR;
1130 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1132 set_bit(chip->dev_num, dev_mask);
1134 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1135 chip->vendor.miscdev.name = devname;
1137 chip->vendor.miscdev.parent = dev;
1138 chip->dev = get_device(dev);
1140 if (misc_register(&chip->vendor.miscdev)) {
1142 "unable to misc_register %s, minor %d\n",
1143 chip->vendor.miscdev.name,
1144 chip->vendor.miscdev.minor);
1146 clear_bit(chip->dev_num, dev_mask);
1152 spin_lock(&driver_lock);
1154 dev_set_drvdata(dev, chip);
1156 list_add(&chip->list, &tpm_chip_list);
1158 spin_unlock(&driver_lock);
1160 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1161 list_del(&chip->list);
1162 misc_deregister(&chip->vendor.miscdev);
1164 clear_bit(chip->dev_num, dev_mask);
1170 chip->bios_dir = tpm_bios_log_setup(devname);
1174 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1176 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1177 MODULE_DESCRIPTION("TPM Driver");
1178 MODULE_VERSION("2.0");
1179 MODULE_LICENSE("GPL");