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/slab.h>
28 #include <linux/mutex.h>
29 #include <linux/spinlock.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];
359 /* if duration is 0, it's because chip->vendor.duration wasn't */
360 /* filled yet, so we set the lowest timeout just to give enough */
361 /* time for tpm_get_timeouts() to succeed */
362 return (duration <= 0 ? HZ : duration);
366 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
369 * Internal kernel interface to transmit TPM commands
371 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
378 count = be32_to_cpu(*((__be32 *) (buf + 2)));
379 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
382 if (count > bufsiz) {
384 "invalid count value %x %zx \n", count, bufsiz);
388 mutex_lock(&chip->tpm_mutex);
390 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
392 "tpm_transmit: tpm_send: error %zd\n", rc);
396 if (chip->vendor.irq)
399 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
401 u8 status = chip->vendor.status(chip);
402 if ((status & chip->vendor.req_complete_mask) ==
403 chip->vendor.req_complete_val)
406 if ((status == chip->vendor.req_canceled)) {
407 dev_err(chip->dev, "Operation Canceled\n");
412 msleep(TPM_TIMEOUT); /* CHECK */
414 } while (time_before(jiffies, stop));
416 chip->vendor.cancel(chip);
417 dev_err(chip->dev, "Operation Timed out\n");
422 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
425 "tpm_transmit: tpm_recv: error %zd\n", rc);
427 mutex_unlock(&chip->tpm_mutex);
431 #define TPM_DIGEST_SIZE 20
432 #define TPM_ERROR_SIZE 10
433 #define TPM_RET_CODE_IDX 6
435 enum tpm_capabilities {
436 TPM_CAP_FLAG = cpu_to_be32(4),
437 TPM_CAP_PROP = cpu_to_be32(5),
438 CAP_VERSION_1_1 = cpu_to_be32(0x06),
439 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
442 enum tpm_sub_capabilities {
443 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
444 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
445 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
446 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
447 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
448 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
449 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
453 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
454 int len, const char *desc)
458 len = tpm_transmit(chip,(u8 *) cmd, len);
461 if (len == TPM_ERROR_SIZE) {
462 err = be32_to_cpu(cmd->header.out.return_code);
463 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
469 #define TPM_INTERNAL_RESULT_SIZE 200
470 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
471 #define TPM_ORD_GET_CAP cpu_to_be32(101)
473 static const struct tpm_input_header tpm_getcap_header = {
474 .tag = TPM_TAG_RQU_COMMAND,
475 .length = cpu_to_be32(22),
476 .ordinal = TPM_ORD_GET_CAP
479 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
482 struct tpm_cmd_t tpm_cmd;
484 struct tpm_chip *chip = dev_get_drvdata(dev);
486 tpm_cmd.header.in = tpm_getcap_header;
487 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
488 tpm_cmd.params.getcap_in.cap = subcap_id;
489 /*subcap field not necessary */
490 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
491 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
493 if (subcap_id == TPM_CAP_FLAG_PERM ||
494 subcap_id == TPM_CAP_FLAG_VOL)
495 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
497 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
498 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
499 tpm_cmd.params.getcap_in.subcap = subcap_id;
501 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
503 *cap = tpm_cmd.params.getcap_out.cap;
507 void tpm_gen_interrupt(struct tpm_chip *chip)
509 struct tpm_cmd_t tpm_cmd;
512 tpm_cmd.header.in = tpm_getcap_header;
513 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
514 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
515 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
517 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
518 "attempting to determine the timeouts");
520 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
522 void tpm_get_timeouts(struct tpm_chip *chip)
524 struct tpm_cmd_t tpm_cmd;
525 struct timeout_t *timeout_cap;
526 struct duration_t *duration_cap;
530 tpm_cmd.header.in = tpm_getcap_header;
531 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
532 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
533 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
535 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
536 "attempting to determine the timeouts");
540 if (be32_to_cpu(tpm_cmd.header.out.length)
544 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
545 /* Don't overwrite default if value is 0 */
546 timeout = be32_to_cpu(timeout_cap->a);
548 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
549 timeout = be32_to_cpu(timeout_cap->b);
551 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
552 timeout = be32_to_cpu(timeout_cap->c);
554 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
555 timeout = be32_to_cpu(timeout_cap->d);
557 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
560 tpm_cmd.header.in = tpm_getcap_header;
561 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
562 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
563 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
565 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
566 "attempting to determine the durations");
570 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
571 be32_to_cpu(tpm_cmd.header.out.length)
572 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
575 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
576 chip->vendor.duration[TPM_SHORT] =
577 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
578 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
579 * value wrong and apparently reports msecs rather than usecs. So we
580 * fix up the resulting too-small TPM_SHORT value to make things work.
582 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
583 chip->vendor.duration[TPM_SHORT] = HZ;
585 chip->vendor.duration[TPM_MEDIUM] =
586 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
587 chip->vendor.duration[TPM_LONG] =
588 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
590 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
592 void tpm_continue_selftest(struct tpm_chip *chip)
595 0, 193, /* TPM_TAG_RQU_COMMAND */
596 0, 0, 0, 10, /* length */
597 0, 0, 0, 83, /* TPM_ORD_GetCapability */
600 tpm_transmit(chip, data, sizeof(data));
602 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
604 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
610 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
611 "attempting to determine the permanent enabled state");
615 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
618 EXPORT_SYMBOL_GPL(tpm_show_enabled);
620 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
626 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
627 "attempting to determine the permanent active state");
631 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
634 EXPORT_SYMBOL_GPL(tpm_show_active);
636 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
642 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
643 "attempting to determine the owner state");
647 rc = sprintf(buf, "%d\n", cap.owned);
650 EXPORT_SYMBOL_GPL(tpm_show_owned);
652 ssize_t tpm_show_temp_deactivated(struct device * dev,
653 struct device_attribute * attr, char *buf)
658 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
659 "attempting to determine the temporary state");
663 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
666 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
669 * tpm_chip_find_get - return tpm_chip for given chip number
671 static struct tpm_chip *tpm_chip_find_get(int chip_num)
673 struct tpm_chip *pos, *chip = NULL;
676 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
677 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
680 if (try_module_get(pos->dev->driver->owner)) {
689 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
690 #define READ_PCR_RESULT_SIZE 30
691 static struct tpm_input_header pcrread_header = {
692 .tag = TPM_TAG_RQU_COMMAND,
693 .length = cpu_to_be32(14),
694 .ordinal = TPM_ORDINAL_PCRREAD
697 int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
700 struct tpm_cmd_t cmd;
702 cmd.header.in = pcrread_header;
703 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
704 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
705 "attempting to read a pcr value");
708 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
714 * tpm_pcr_read - read a pcr value
715 * @chip_num: tpm idx # or ANY
716 * @pcr_idx: pcr idx to retrieve
717 * @res_buf: TPM_PCR value
718 * size of res_buf is 20 bytes (or NULL if you don't care)
720 * The TPM driver should be built-in, but for whatever reason it
721 * isn't, protect against the chip disappearing, by incrementing
722 * the module usage count.
724 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
726 struct tpm_chip *chip;
729 chip = tpm_chip_find_get(chip_num);
732 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
733 module_put(chip->dev->driver->owner);
736 EXPORT_SYMBOL_GPL(tpm_pcr_read);
739 * tpm_pcr_extend - extend pcr value with hash
740 * @chip_num: tpm idx # or AN&
741 * @pcr_idx: pcr idx to extend
742 * @hash: hash value used to extend pcr value
744 * The TPM driver should be built-in, but for whatever reason it
745 * isn't, protect against the chip disappearing, by incrementing
746 * the module usage count.
748 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
749 #define EXTEND_PCR_RESULT_SIZE 34
750 static struct tpm_input_header pcrextend_header = {
751 .tag = TPM_TAG_RQU_COMMAND,
752 .length = cpu_to_be32(34),
753 .ordinal = TPM_ORD_PCR_EXTEND
756 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
758 struct tpm_cmd_t cmd;
760 struct tpm_chip *chip;
762 chip = tpm_chip_find_get(chip_num);
766 cmd.header.in = pcrextend_header;
767 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
768 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
769 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
770 "attempting extend a PCR value");
772 module_put(chip->dev->driver->owner);
775 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
777 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
781 u8 digest[TPM_DIGEST_SIZE];
785 struct tpm_chip *chip = dev_get_drvdata(dev);
787 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
788 "attempting to determine the number of PCRS");
792 num_pcrs = be32_to_cpu(cap.num_pcrs);
793 for (i = 0; i < num_pcrs; i++) {
794 rc = __tpm_pcr_read(chip, i, digest);
797 str += sprintf(str, "PCR-%02d: ", i);
798 for (j = 0; j < TPM_DIGEST_SIZE; j++)
799 str += sprintf(str, "%02X ", digest[j]);
800 str += sprintf(str, "\n");
804 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
806 #define READ_PUBEK_RESULT_SIZE 314
807 #define TPM_ORD_READPUBEK cpu_to_be32(124)
808 struct tpm_input_header tpm_readpubek_header = {
809 .tag = TPM_TAG_RQU_COMMAND,
810 .length = cpu_to_be32(30),
811 .ordinal = TPM_ORD_READPUBEK
814 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
818 struct tpm_cmd_t tpm_cmd;
823 struct tpm_chip *chip = dev_get_drvdata(dev);
825 tpm_cmd.header.in = tpm_readpubek_header;
826 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
827 "attempting to read the PUBEK");
832 ignore header 10 bytes
833 algorithm 32 bits (1 == RSA )
836 parameters (RSA 12->bytes: keybit, #primes, expbit)
839 ignore checksum 20 bytes
841 data = tpm_cmd.params.readpubek_out_buffer;
844 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
845 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
846 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
847 "Modulus length: %d\nModulus: \n",
848 data[10], data[11], data[12], data[13], data[14],
849 data[15], data[16], data[17], data[22], data[23],
850 data[24], data[25], data[26], data[27], data[28],
851 data[29], data[30], data[31], data[32], data[33],
852 be32_to_cpu(*((__be32 *) (data + 34))));
854 for (i = 0; i < 256; i++) {
855 str += sprintf(str, "%02X ", data[i + 38]);
856 if ((i + 1) % 16 == 0)
857 str += sprintf(str, "\n");
863 EXPORT_SYMBOL_GPL(tpm_show_pubek);
866 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
873 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
874 "attempting to determine the manufacturer");
877 str += sprintf(str, "Manufacturer: 0x%x\n",
878 be32_to_cpu(cap.manufacturer_id));
880 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
881 "attempting to determine the 1.1 version");
885 "TCG version: %d.%d\nFirmware version: %d.%d\n",
886 cap.tpm_version.Major, cap.tpm_version.Minor,
887 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
890 EXPORT_SYMBOL_GPL(tpm_show_caps);
892 ssize_t tpm_show_caps_1_2(struct device * dev,
893 struct device_attribute * attr, char *buf)
899 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
900 "attempting to determine the manufacturer");
903 str += sprintf(str, "Manufacturer: 0x%x\n",
904 be32_to_cpu(cap.manufacturer_id));
905 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
906 "attempting to determine the 1.2 version");
910 "TCG version: %d.%d\nFirmware version: %d.%d\n",
911 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
912 cap.tpm_version_1_2.revMajor,
913 cap.tpm_version_1_2.revMinor);
916 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
918 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
921 struct tpm_chip *chip = dev_get_drvdata(dev);
923 return sprintf(buf, "%d %d %d\n",
924 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
925 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
926 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]));
928 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
930 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
931 const char *buf, size_t count)
933 struct tpm_chip *chip = dev_get_drvdata(dev);
937 chip->vendor.cancel(chip);
940 EXPORT_SYMBOL_GPL(tpm_store_cancel);
943 * Device file system interface to the TPM
945 * It's assured that the chip will be opened just once,
946 * by the check of is_open variable, which is protected
949 int tpm_open(struct inode *inode, struct file *file)
951 int minor = iminor(inode);
952 struct tpm_chip *chip = NULL, *pos;
955 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
956 if (pos->vendor.miscdev.minor == minor) {
958 get_device(chip->dev);
967 if (test_and_set_bit(0, &chip->is_open)) {
968 dev_dbg(chip->dev, "Another process owns this TPM\n");
969 put_device(chip->dev);
973 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
974 if (chip->data_buffer == NULL) {
975 clear_bit(0, &chip->is_open);
976 put_device(chip->dev);
980 atomic_set(&chip->data_pending, 0);
982 file->private_data = chip;
985 EXPORT_SYMBOL_GPL(tpm_open);
988 * Called on file close
990 int tpm_release(struct inode *inode, struct file *file)
992 struct tpm_chip *chip = file->private_data;
994 del_singleshot_timer_sync(&chip->user_read_timer);
995 flush_scheduled_work();
996 file->private_data = NULL;
997 atomic_set(&chip->data_pending, 0);
998 kfree(chip->data_buffer);
999 clear_bit(0, &chip->is_open);
1000 put_device(chip->dev);
1003 EXPORT_SYMBOL_GPL(tpm_release);
1005 ssize_t tpm_write(struct file *file, const char __user *buf,
1006 size_t size, loff_t *off)
1008 struct tpm_chip *chip = file->private_data;
1009 size_t in_size = size, out_size;
1011 /* cannot perform a write until the read has cleared
1012 either via tpm_read or a user_read_timer timeout */
1013 while (atomic_read(&chip->data_pending) != 0)
1014 msleep(TPM_TIMEOUT);
1016 mutex_lock(&chip->buffer_mutex);
1018 if (in_size > TPM_BUFSIZE)
1019 in_size = TPM_BUFSIZE;
1022 (chip->data_buffer, (void __user *) buf, in_size)) {
1023 mutex_unlock(&chip->buffer_mutex);
1027 /* atomic tpm command send and result receive */
1028 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1030 atomic_set(&chip->data_pending, out_size);
1031 mutex_unlock(&chip->buffer_mutex);
1033 /* Set a timeout by which the reader must come claim the result */
1034 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1038 EXPORT_SYMBOL_GPL(tpm_write);
1040 ssize_t tpm_read(struct file *file, char __user *buf,
1041 size_t size, loff_t *off)
1043 struct tpm_chip *chip = file->private_data;
1046 del_singleshot_timer_sync(&chip->user_read_timer);
1047 flush_scheduled_work();
1048 ret_size = atomic_read(&chip->data_pending);
1049 atomic_set(&chip->data_pending, 0);
1050 if (ret_size > 0) { /* relay data */
1051 if (size < ret_size)
1054 mutex_lock(&chip->buffer_mutex);
1055 if (copy_to_user(buf, chip->data_buffer, ret_size))
1057 mutex_unlock(&chip->buffer_mutex);
1062 EXPORT_SYMBOL_GPL(tpm_read);
1064 void tpm_remove_hardware(struct device *dev)
1066 struct tpm_chip *chip = dev_get_drvdata(dev);
1069 dev_err(dev, "No device data found\n");
1073 spin_lock(&driver_lock);
1074 list_del_rcu(&chip->list);
1075 spin_unlock(&driver_lock);
1078 misc_deregister(&chip->vendor.miscdev);
1079 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1080 tpm_bios_log_teardown(chip->bios_dir);
1082 /* write it this way to be explicit (chip->dev == dev) */
1083 put_device(chip->dev);
1085 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1087 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1088 #define SAVESTATE_RESULT_SIZE 10
1090 static struct tpm_input_header savestate_header = {
1091 .tag = TPM_TAG_RQU_COMMAND,
1092 .length = cpu_to_be32(10),
1093 .ordinal = TPM_ORD_SAVESTATE
1096 /* Bug workaround - some TPM's don't flush the most
1097 * recently changed pcr on suspend, so force the flush
1098 * with an extend to the selected _unused_ non-volatile pcr.
1100 static int tpm_suspend_pcr;
1101 static int __init tpm_suspend_setup(char *str)
1103 get_option(&str, &tpm_suspend_pcr);
1106 __setup("tpm_suspend_pcr=", tpm_suspend_setup);
1109 * We are about to suspend. Save the TPM state
1110 * so that it can be restored.
1112 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1114 struct tpm_chip *chip = dev_get_drvdata(dev);
1115 struct tpm_cmd_t cmd;
1118 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1123 /* for buggy tpm, flush pcrs with extend to selected dummy */
1124 if (tpm_suspend_pcr) {
1125 cmd.header.in = pcrextend_header;
1126 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1127 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1129 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1130 "extending dummy pcr before suspend");
1133 /* now do the actual savestate */
1134 cmd.header.in = savestate_header;
1135 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1136 "sending savestate before suspend");
1139 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1142 * Resume from a power safe. The BIOS already restored
1145 int tpm_pm_resume(struct device *dev)
1147 struct tpm_chip *chip = dev_get_drvdata(dev);
1154 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1156 /* In case vendor provided release function, call it too.*/
1158 void tpm_dev_vendor_release(struct tpm_chip *chip)
1160 if (chip->vendor.release)
1161 chip->vendor.release(chip->dev);
1163 clear_bit(chip->dev_num, dev_mask);
1164 kfree(chip->vendor.miscdev.name);
1166 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1170 * Once all references to platform device are down to 0,
1171 * release all allocated structures.
1173 void tpm_dev_release(struct device *dev)
1175 struct tpm_chip *chip = dev_get_drvdata(dev);
1177 tpm_dev_vendor_release(chip);
1182 EXPORT_SYMBOL_GPL(tpm_dev_release);
1185 * Called from tpm_<specific>.c probe function only for devices
1186 * the driver has determined it should claim. Prior to calling
1187 * this function the specific probe function has called pci_enable_device
1188 * upon errant exit from this function specific probe function should call
1189 * pci_disable_device
1191 struct tpm_chip *tpm_register_hardware(struct device *dev,
1192 const struct tpm_vendor_specific *entry)
1194 #define DEVNAME_SIZE 7
1197 struct tpm_chip *chip;
1199 /* Driver specific per-device data */
1200 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1201 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1203 if (chip == NULL || devname == NULL)
1206 mutex_init(&chip->buffer_mutex);
1207 mutex_init(&chip->tpm_mutex);
1208 INIT_LIST_HEAD(&chip->list);
1210 INIT_WORK(&chip->work, timeout_work);
1212 setup_timer(&chip->user_read_timer, user_reader_timeout,
1213 (unsigned long)chip);
1215 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1217 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1219 if (chip->dev_num >= TPM_NUM_DEVICES) {
1220 dev_err(dev, "No available tpm device numbers\n");
1222 } else if (chip->dev_num == 0)
1223 chip->vendor.miscdev.minor = TPM_MINOR;
1225 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1227 set_bit(chip->dev_num, dev_mask);
1229 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1230 chip->vendor.miscdev.name = devname;
1232 chip->vendor.miscdev.parent = dev;
1233 chip->dev = get_device(dev);
1234 chip->release = dev->release;
1235 dev->release = tpm_dev_release;
1236 dev_set_drvdata(dev, chip);
1238 if (misc_register(&chip->vendor.miscdev)) {
1240 "unable to misc_register %s, minor %d\n",
1241 chip->vendor.miscdev.name,
1242 chip->vendor.miscdev.minor);
1243 put_device(chip->dev);
1247 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1248 misc_deregister(&chip->vendor.miscdev);
1249 put_device(chip->dev);
1254 chip->bios_dir = tpm_bios_log_setup(devname);
1256 /* Make chip available */
1257 spin_lock(&driver_lock);
1258 list_add_rcu(&chip->list, &tpm_chip_list);
1259 spin_unlock(&driver_lock);
1268 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1270 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1271 MODULE_DESCRIPTION("TPM Driver");
1272 MODULE_VERSION("2.0");
1273 MODULE_LICENSE("GPL");