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];
358 /* if duration is 0, it's because chip->vendor.duration wasn't */
359 /* filled yet, so we set the lowest timeout just to give enough */
360 /* time for tpm_get_timeouts() to succeed */
361 return (duration <= 0 ? HZ : duration);
365 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
368 * Internal kernel interface to transmit TPM commands
370 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
377 count = be32_to_cpu(*((__be32 *) (buf + 2)));
378 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
381 if (count > bufsiz) {
383 "invalid count value %x %zx \n", count, bufsiz);
387 mutex_lock(&chip->tpm_mutex);
389 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
391 "tpm_transmit: tpm_send: error %zd\n", rc);
395 if (chip->vendor.irq)
398 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
400 u8 status = chip->vendor.status(chip);
401 if ((status & chip->vendor.req_complete_mask) ==
402 chip->vendor.req_complete_val)
405 if ((status == chip->vendor.req_canceled)) {
406 dev_err(chip->dev, "Operation Canceled\n");
411 msleep(TPM_TIMEOUT); /* CHECK */
413 } while (time_before(jiffies, stop));
415 chip->vendor.cancel(chip);
416 dev_err(chip->dev, "Operation Timed out\n");
421 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
424 "tpm_transmit: tpm_recv: error %zd\n", rc);
426 mutex_unlock(&chip->tpm_mutex);
430 #define TPM_DIGEST_SIZE 20
431 #define TPM_ERROR_SIZE 10
432 #define TPM_RET_CODE_IDX 6
434 enum tpm_capabilities {
435 TPM_CAP_FLAG = cpu_to_be32(4),
436 TPM_CAP_PROP = cpu_to_be32(5),
437 CAP_VERSION_1_1 = cpu_to_be32(0x06),
438 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
441 enum tpm_sub_capabilities {
442 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
443 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
444 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
445 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
446 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
447 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
448 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
452 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
453 int len, const char *desc)
457 len = tpm_transmit(chip,(u8 *) cmd, len);
460 if (len == TPM_ERROR_SIZE) {
461 err = be32_to_cpu(cmd->header.out.return_code);
462 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
468 #define TPM_INTERNAL_RESULT_SIZE 200
469 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
470 #define TPM_ORD_GET_CAP cpu_to_be32(101)
472 static const struct tpm_input_header tpm_getcap_header = {
473 .tag = TPM_TAG_RQU_COMMAND,
474 .length = cpu_to_be32(22),
475 .ordinal = TPM_ORD_GET_CAP
478 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
481 struct tpm_cmd_t tpm_cmd;
483 struct tpm_chip *chip = dev_get_drvdata(dev);
485 tpm_cmd.header.in = tpm_getcap_header;
486 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
487 tpm_cmd.params.getcap_in.cap = subcap_id;
488 /*subcap field not necessary */
489 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
490 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
492 if (subcap_id == TPM_CAP_FLAG_PERM ||
493 subcap_id == TPM_CAP_FLAG_VOL)
494 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
496 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
497 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
498 tpm_cmd.params.getcap_in.subcap = subcap_id;
500 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
502 *cap = tpm_cmd.params.getcap_out.cap;
506 void tpm_gen_interrupt(struct tpm_chip *chip)
508 struct tpm_cmd_t tpm_cmd;
511 tpm_cmd.header.in = tpm_getcap_header;
512 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
513 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
514 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
516 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
517 "attempting to determine the timeouts");
519 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
521 void tpm_get_timeouts(struct tpm_chip *chip)
523 struct tpm_cmd_t tpm_cmd;
524 struct timeout_t *timeout_cap;
525 struct duration_t *duration_cap;
529 tpm_cmd.header.in = tpm_getcap_header;
530 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
531 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
532 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
534 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
535 "attempting to determine the timeouts");
539 if (be32_to_cpu(tpm_cmd.header.out.length)
543 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
544 /* Don't overwrite default if value is 0 */
545 timeout = be32_to_cpu(timeout_cap->a);
547 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
548 timeout = be32_to_cpu(timeout_cap->b);
550 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
551 timeout = be32_to_cpu(timeout_cap->c);
553 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
554 timeout = be32_to_cpu(timeout_cap->d);
556 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
559 tpm_cmd.header.in = tpm_getcap_header;
560 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
561 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
562 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
564 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
565 "attempting to determine the durations");
569 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
570 be32_to_cpu(tpm_cmd.header.out.length)
571 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
574 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
575 chip->vendor.duration[TPM_SHORT] =
576 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
577 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
578 * value wrong and apparently reports msecs rather than usecs. So we
579 * fix up the resulting too-small TPM_SHORT value to make things work.
581 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
582 chip->vendor.duration[TPM_SHORT] = HZ;
584 chip->vendor.duration[TPM_MEDIUM] =
585 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
586 chip->vendor.duration[TPM_LONG] =
587 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
589 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
591 void tpm_continue_selftest(struct tpm_chip *chip)
594 0, 193, /* TPM_TAG_RQU_COMMAND */
595 0, 0, 0, 10, /* length */
596 0, 0, 0, 83, /* TPM_ORD_GetCapability */
599 tpm_transmit(chip, data, sizeof(data));
601 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
603 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
609 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
610 "attempting to determine the permanent enabled state");
614 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
617 EXPORT_SYMBOL_GPL(tpm_show_enabled);
619 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
625 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
626 "attempting to determine the permanent active state");
630 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
633 EXPORT_SYMBOL_GPL(tpm_show_active);
635 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
641 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
642 "attempting to determine the owner state");
646 rc = sprintf(buf, "%d\n", cap.owned);
649 EXPORT_SYMBOL_GPL(tpm_show_owned);
651 ssize_t tpm_show_temp_deactivated(struct device * dev,
652 struct device_attribute * attr, char *buf)
657 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
658 "attempting to determine the temporary state");
662 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
665 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
668 * tpm_chip_find_get - return tpm_chip for given chip number
670 static struct tpm_chip *tpm_chip_find_get(int chip_num)
672 struct tpm_chip *pos, *chip = NULL;
675 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
676 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
679 if (try_module_get(pos->dev->driver->owner)) {
688 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
689 #define READ_PCR_RESULT_SIZE 30
690 static struct tpm_input_header pcrread_header = {
691 .tag = TPM_TAG_RQU_COMMAND,
692 .length = cpu_to_be32(14),
693 .ordinal = TPM_ORDINAL_PCRREAD
696 int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
699 struct tpm_cmd_t cmd;
701 cmd.header.in = pcrread_header;
702 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
703 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
704 "attempting to read a pcr value");
707 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
713 * tpm_pcr_read - read a pcr value
714 * @chip_num: tpm idx # or ANY
715 * @pcr_idx: pcr idx to retrieve
716 * @res_buf: TPM_PCR value
717 * size of res_buf is 20 bytes (or NULL if you don't care)
719 * The TPM driver should be built-in, but for whatever reason it
720 * isn't, protect against the chip disappearing, by incrementing
721 * the module usage count.
723 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
725 struct tpm_chip *chip;
728 chip = tpm_chip_find_get(chip_num);
731 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
732 module_put(chip->dev->driver->owner);
735 EXPORT_SYMBOL_GPL(tpm_pcr_read);
738 * tpm_pcr_extend - extend pcr value with hash
739 * @chip_num: tpm idx # or AN&
740 * @pcr_idx: pcr idx to extend
741 * @hash: hash value used to extend pcr value
743 * The TPM driver should be built-in, but for whatever reason it
744 * isn't, protect against the chip disappearing, by incrementing
745 * the module usage count.
747 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
748 #define EXTEND_PCR_RESULT_SIZE 34
749 static struct tpm_input_header pcrextend_header = {
750 .tag = TPM_TAG_RQU_COMMAND,
751 .length = cpu_to_be32(34),
752 .ordinal = TPM_ORD_PCR_EXTEND
755 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
757 struct tpm_cmd_t cmd;
759 struct tpm_chip *chip;
761 chip = tpm_chip_find_get(chip_num);
765 cmd.header.in = pcrextend_header;
766 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
767 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
768 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
769 "attempting extend a PCR value");
771 module_put(chip->dev->driver->owner);
774 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
776 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
780 u8 digest[TPM_DIGEST_SIZE];
784 struct tpm_chip *chip = dev_get_drvdata(dev);
786 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
787 "attempting to determine the number of PCRS");
791 num_pcrs = be32_to_cpu(cap.num_pcrs);
792 for (i = 0; i < num_pcrs; i++) {
793 rc = __tpm_pcr_read(chip, i, digest);
796 str += sprintf(str, "PCR-%02d: ", i);
797 for (j = 0; j < TPM_DIGEST_SIZE; j++)
798 str += sprintf(str, "%02X ", digest[j]);
799 str += sprintf(str, "\n");
803 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
805 #define READ_PUBEK_RESULT_SIZE 314
806 #define TPM_ORD_READPUBEK cpu_to_be32(124)
807 struct tpm_input_header tpm_readpubek_header = {
808 .tag = TPM_TAG_RQU_COMMAND,
809 .length = cpu_to_be32(30),
810 .ordinal = TPM_ORD_READPUBEK
813 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
817 struct tpm_cmd_t tpm_cmd;
822 struct tpm_chip *chip = dev_get_drvdata(dev);
824 tpm_cmd.header.in = tpm_readpubek_header;
825 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
826 "attempting to read the PUBEK");
831 ignore header 10 bytes
832 algorithm 32 bits (1 == RSA )
835 parameters (RSA 12->bytes: keybit, #primes, expbit)
838 ignore checksum 20 bytes
840 data = tpm_cmd.params.readpubek_out_buffer;
843 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
844 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
845 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
846 "Modulus length: %d\nModulus: \n",
847 data[10], data[11], data[12], data[13], data[14],
848 data[15], data[16], data[17], data[22], data[23],
849 data[24], data[25], data[26], data[27], data[28],
850 data[29], data[30], data[31], data[32], data[33],
851 be32_to_cpu(*((__be32 *) (data + 34))));
853 for (i = 0; i < 256; i++) {
854 str += sprintf(str, "%02X ", data[i + 38]);
855 if ((i + 1) % 16 == 0)
856 str += sprintf(str, "\n");
862 EXPORT_SYMBOL_GPL(tpm_show_pubek);
865 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
872 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
873 "attempting to determine the manufacturer");
876 str += sprintf(str, "Manufacturer: 0x%x\n",
877 be32_to_cpu(cap.manufacturer_id));
879 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
880 "attempting to determine the 1.1 version");
884 "TCG version: %d.%d\nFirmware version: %d.%d\n",
885 cap.tpm_version.Major, cap.tpm_version.Minor,
886 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
889 EXPORT_SYMBOL_GPL(tpm_show_caps);
891 ssize_t tpm_show_caps_1_2(struct device * dev,
892 struct device_attribute * attr, char *buf)
898 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
899 "attempting to determine the manufacturer");
902 str += sprintf(str, "Manufacturer: 0x%x\n",
903 be32_to_cpu(cap.manufacturer_id));
904 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
905 "attempting to determine the 1.2 version");
909 "TCG version: %d.%d\nFirmware version: %d.%d\n",
910 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
911 cap.tpm_version_1_2.revMajor,
912 cap.tpm_version_1_2.revMinor);
915 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
917 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
920 struct tpm_chip *chip = dev_get_drvdata(dev);
922 return sprintf(buf, "%d %d %d\n",
923 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
924 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
925 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]));
927 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
929 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
930 const char *buf, size_t count)
932 struct tpm_chip *chip = dev_get_drvdata(dev);
936 chip->vendor.cancel(chip);
939 EXPORT_SYMBOL_GPL(tpm_store_cancel);
942 * Device file system interface to the TPM
944 * It's assured that the chip will be opened just once,
945 * by the check of is_open variable, which is protected
948 int tpm_open(struct inode *inode, struct file *file)
950 int minor = iminor(inode);
951 struct tpm_chip *chip = NULL, *pos;
954 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
955 if (pos->vendor.miscdev.minor == minor) {
957 get_device(chip->dev);
966 if (test_and_set_bit(0, &chip->is_open)) {
967 dev_dbg(chip->dev, "Another process owns this TPM\n");
968 put_device(chip->dev);
972 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
973 if (chip->data_buffer == NULL) {
974 clear_bit(0, &chip->is_open);
975 put_device(chip->dev);
979 atomic_set(&chip->data_pending, 0);
981 file->private_data = chip;
984 EXPORT_SYMBOL_GPL(tpm_open);
987 * Called on file close
989 int tpm_release(struct inode *inode, struct file *file)
991 struct tpm_chip *chip = file->private_data;
993 del_singleshot_timer_sync(&chip->user_read_timer);
994 flush_scheduled_work();
995 file->private_data = NULL;
996 atomic_set(&chip->data_pending, 0);
997 kfree(chip->data_buffer);
998 clear_bit(0, &chip->is_open);
999 put_device(chip->dev);
1002 EXPORT_SYMBOL_GPL(tpm_release);
1004 ssize_t tpm_write(struct file *file, const char __user *buf,
1005 size_t size, loff_t *off)
1007 struct tpm_chip *chip = file->private_data;
1008 size_t in_size = size, out_size;
1010 /* cannot perform a write until the read has cleared
1011 either via tpm_read or a user_read_timer timeout */
1012 while (atomic_read(&chip->data_pending) != 0)
1013 msleep(TPM_TIMEOUT);
1015 mutex_lock(&chip->buffer_mutex);
1017 if (in_size > TPM_BUFSIZE)
1018 in_size = TPM_BUFSIZE;
1021 (chip->data_buffer, (void __user *) buf, in_size)) {
1022 mutex_unlock(&chip->buffer_mutex);
1026 /* atomic tpm command send and result receive */
1027 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1029 atomic_set(&chip->data_pending, out_size);
1030 mutex_unlock(&chip->buffer_mutex);
1032 /* Set a timeout by which the reader must come claim the result */
1033 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1037 EXPORT_SYMBOL_GPL(tpm_write);
1039 ssize_t tpm_read(struct file *file, char __user *buf,
1040 size_t size, loff_t *off)
1042 struct tpm_chip *chip = file->private_data;
1045 del_singleshot_timer_sync(&chip->user_read_timer);
1046 flush_scheduled_work();
1047 ret_size = atomic_read(&chip->data_pending);
1048 atomic_set(&chip->data_pending, 0);
1049 if (ret_size > 0) { /* relay data */
1050 if (size < ret_size)
1053 mutex_lock(&chip->buffer_mutex);
1054 if (copy_to_user(buf, chip->data_buffer, ret_size))
1056 mutex_unlock(&chip->buffer_mutex);
1061 EXPORT_SYMBOL_GPL(tpm_read);
1063 void tpm_remove_hardware(struct device *dev)
1065 struct tpm_chip *chip = dev_get_drvdata(dev);
1068 dev_err(dev, "No device data found\n");
1072 spin_lock(&driver_lock);
1073 list_del_rcu(&chip->list);
1074 spin_unlock(&driver_lock);
1077 misc_deregister(&chip->vendor.miscdev);
1078 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1079 tpm_bios_log_teardown(chip->bios_dir);
1081 /* write it this way to be explicit (chip->dev == dev) */
1082 put_device(chip->dev);
1084 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1087 * We are about to suspend. Save the TPM state
1088 * so that it can be restored.
1090 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1092 struct tpm_chip *chip = dev_get_drvdata(dev);
1094 0, 193, /* TPM_TAG_RQU_COMMAND */
1095 0, 0, 0, 10, /* blob length (in bytes) */
1096 0, 0, 0, 152 /* TPM_ORD_SaveState */
1102 tpm_transmit(chip, savestate, sizeof(savestate));
1105 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1108 * Resume from a power safe. The BIOS already restored
1111 int tpm_pm_resume(struct device *dev)
1113 struct tpm_chip *chip = dev_get_drvdata(dev);
1120 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1122 /* In case vendor provided release function, call it too.*/
1124 void tpm_dev_vendor_release(struct tpm_chip *chip)
1126 if (chip->vendor.release)
1127 chip->vendor.release(chip->dev);
1129 clear_bit(chip->dev_num, dev_mask);
1130 kfree(chip->vendor.miscdev.name);
1132 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1136 * Once all references to platform device are down to 0,
1137 * release all allocated structures.
1139 void tpm_dev_release(struct device *dev)
1141 struct tpm_chip *chip = dev_get_drvdata(dev);
1143 tpm_dev_vendor_release(chip);
1148 EXPORT_SYMBOL_GPL(tpm_dev_release);
1151 * Called from tpm_<specific>.c probe function only for devices
1152 * the driver has determined it should claim. Prior to calling
1153 * this function the specific probe function has called pci_enable_device
1154 * upon errant exit from this function specific probe function should call
1155 * pci_disable_device
1157 struct tpm_chip *tpm_register_hardware(struct device *dev,
1158 const struct tpm_vendor_specific *entry)
1160 #define DEVNAME_SIZE 7
1163 struct tpm_chip *chip;
1165 /* Driver specific per-device data */
1166 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1167 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1169 if (chip == NULL || devname == NULL)
1172 mutex_init(&chip->buffer_mutex);
1173 mutex_init(&chip->tpm_mutex);
1174 INIT_LIST_HEAD(&chip->list);
1176 INIT_WORK(&chip->work, timeout_work);
1178 setup_timer(&chip->user_read_timer, user_reader_timeout,
1179 (unsigned long)chip);
1181 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1183 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1185 if (chip->dev_num >= TPM_NUM_DEVICES) {
1186 dev_err(dev, "No available tpm device numbers\n");
1188 } else if (chip->dev_num == 0)
1189 chip->vendor.miscdev.minor = TPM_MINOR;
1191 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1193 set_bit(chip->dev_num, dev_mask);
1195 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1196 chip->vendor.miscdev.name = devname;
1198 chip->vendor.miscdev.parent = dev;
1199 chip->dev = get_device(dev);
1200 chip->release = dev->release;
1201 dev->release = tpm_dev_release;
1202 dev_set_drvdata(dev, chip);
1204 if (misc_register(&chip->vendor.miscdev)) {
1206 "unable to misc_register %s, minor %d\n",
1207 chip->vendor.miscdev.name,
1208 chip->vendor.miscdev.minor);
1209 put_device(chip->dev);
1213 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1214 misc_deregister(&chip->vendor.miscdev);
1215 put_device(chip->dev);
1220 chip->bios_dir = tpm_bios_log_setup(devname);
1222 /* Make chip available */
1223 spin_lock(&driver_lock);
1224 list_add_rcu(&chip->list, &tpm_chip_list);
1225 spin_unlock(&driver_lock);
1234 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1236 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1237 MODULE_DESCRIPTION("TPM Driver");
1238 MODULE_VERSION("2.0");
1239 MODULE_LICENSE("GPL");