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
432 enum tpm_capabilities
{
433 TPM_CAP_FLAG
= cpu_to_be32(4),
434 TPM_CAP_PROP
= cpu_to_be32(5),
435 CAP_VERSION_1_1
= cpu_to_be32(0x06),
436 CAP_VERSION_1_2
= cpu_to_be32(0x1A)
439 enum tpm_sub_capabilities
{
440 TPM_CAP_PROP_PCR
= cpu_to_be32(0x101),
441 TPM_CAP_PROP_MANUFACTURER
= cpu_to_be32(0x103),
442 TPM_CAP_FLAG_PERM
= cpu_to_be32(0x108),
443 TPM_CAP_FLAG_VOL
= cpu_to_be32(0x109),
444 TPM_CAP_PROP_OWNER
= cpu_to_be32(0x111),
445 TPM_CAP_PROP_TIS_TIMEOUT
= cpu_to_be32(0x115),
446 TPM_CAP_PROP_TIS_DURATION
= cpu_to_be32(0x120),
450 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, struct tpm_cmd_t
*cmd
,
451 int len
, const char *desc
)
455 len
= tpm_transmit(chip
,(u8
*) cmd
, len
);
458 if (len
== TPM_ERROR_SIZE
) {
459 err
= be32_to_cpu(cmd
->header
.out
.return_code
);
460 dev_dbg(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
466 #define TPM_INTERNAL_RESULT_SIZE 200
467 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
468 #define TPM_ORD_GET_CAP cpu_to_be32(101)
470 static const struct tpm_input_header tpm_getcap_header
= {
471 .tag
= TPM_TAG_RQU_COMMAND
,
472 .length
= cpu_to_be32(22),
473 .ordinal
= TPM_ORD_GET_CAP
476 ssize_t
tpm_getcap(struct device
*dev
, __be32 subcap_id
, cap_t
*cap
,
479 struct tpm_cmd_t tpm_cmd
;
481 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
483 tpm_cmd
.header
.in
= tpm_getcap_header
;
484 if (subcap_id
== CAP_VERSION_1_1
|| subcap_id
== CAP_VERSION_1_2
) {
485 tpm_cmd
.params
.getcap_in
.cap
= subcap_id
;
486 /*subcap field not necessary */
487 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(0);
488 tpm_cmd
.header
.in
.length
-= cpu_to_be32(sizeof(__be32
));
490 if (subcap_id
== TPM_CAP_FLAG_PERM
||
491 subcap_id
== TPM_CAP_FLAG_VOL
)
492 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_FLAG
;
494 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
495 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
496 tpm_cmd
.params
.getcap_in
.subcap
= subcap_id
;
498 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
, desc
);
500 *cap
= tpm_cmd
.params
.getcap_out
.cap
;
504 void tpm_gen_interrupt(struct tpm_chip
*chip
)
506 struct tpm_cmd_t tpm_cmd
;
509 tpm_cmd
.header
.in
= tpm_getcap_header
;
510 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
511 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
512 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
514 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
515 "attempting to determine the timeouts");
517 EXPORT_SYMBOL_GPL(tpm_gen_interrupt
);
519 void tpm_get_timeouts(struct tpm_chip
*chip
)
521 struct tpm_cmd_t tpm_cmd
;
522 struct timeout_t
*timeout_cap
;
523 struct duration_t
*duration_cap
;
527 tpm_cmd
.header
.in
= tpm_getcap_header
;
528 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
529 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
530 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_TIMEOUT
;
532 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
533 "attempting to determine the timeouts");
537 if (be32_to_cpu(tpm_cmd
.header
.out
.length
)
541 timeout_cap
= &tpm_cmd
.params
.getcap_out
.cap
.timeout
;
542 /* Don't overwrite default if value is 0 */
543 timeout
= be32_to_cpu(timeout_cap
->a
);
545 chip
->vendor
.timeout_a
= usecs_to_jiffies(timeout
);
546 timeout
= be32_to_cpu(timeout_cap
->b
);
548 chip
->vendor
.timeout_b
= usecs_to_jiffies(timeout
);
549 timeout
= be32_to_cpu(timeout_cap
->c
);
551 chip
->vendor
.timeout_c
= usecs_to_jiffies(timeout
);
552 timeout
= be32_to_cpu(timeout_cap
->d
);
554 chip
->vendor
.timeout_d
= usecs_to_jiffies(timeout
);
557 tpm_cmd
.header
.in
= tpm_getcap_header
;
558 tpm_cmd
.params
.getcap_in
.cap
= TPM_CAP_PROP
;
559 tpm_cmd
.params
.getcap_in
.subcap_size
= cpu_to_be32(4);
560 tpm_cmd
.params
.getcap_in
.subcap
= TPM_CAP_PROP_TIS_DURATION
;
562 rc
= transmit_cmd(chip
, &tpm_cmd
, TPM_INTERNAL_RESULT_SIZE
,
563 "attempting to determine the durations");
567 if (be32_to_cpu(tpm_cmd
.header
.out
.return_code
)
570 duration_cap
= &tpm_cmd
.params
.getcap_out
.cap
.duration
;
571 chip
->vendor
.duration
[TPM_SHORT
] =
572 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_short
));
573 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
574 * value wrong and apparently reports msecs rather than usecs. So we
575 * fix up the resulting too-small TPM_SHORT value to make things work.
577 if (chip
->vendor
.duration
[TPM_SHORT
] < (HZ
/100))
578 chip
->vendor
.duration
[TPM_SHORT
] = HZ
;
580 chip
->vendor
.duration
[TPM_MEDIUM
] =
581 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_medium
));
582 chip
->vendor
.duration
[TPM_LONG
] =
583 usecs_to_jiffies(be32_to_cpu(duration_cap
->tpm_long
));
585 EXPORT_SYMBOL_GPL(tpm_get_timeouts
);
587 void tpm_continue_selftest(struct tpm_chip
*chip
)
590 0, 193, /* TPM_TAG_RQU_COMMAND */
591 0, 0, 0, 10, /* length */
592 0, 0, 0, 83, /* TPM_ORD_GetCapability */
595 tpm_transmit(chip
, data
, sizeof(data
));
597 EXPORT_SYMBOL_GPL(tpm_continue_selftest
);
599 ssize_t
tpm_show_enabled(struct device
* dev
, struct device_attribute
* attr
,
605 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
606 "attempting to determine the permanent enabled state");
610 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
613 EXPORT_SYMBOL_GPL(tpm_show_enabled
);
615 ssize_t
tpm_show_active(struct device
* dev
, struct device_attribute
* attr
,
621 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
622 "attempting to determine the permanent active state");
626 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
629 EXPORT_SYMBOL_GPL(tpm_show_active
);
631 ssize_t
tpm_show_owned(struct device
* dev
, struct device_attribute
* attr
,
637 rc
= tpm_getcap(dev
, TPM_CAP_PROP_OWNER
, &cap
,
638 "attempting to determine the owner state");
642 rc
= sprintf(buf
, "%d\n", cap
.owned
);
645 EXPORT_SYMBOL_GPL(tpm_show_owned
);
647 ssize_t
tpm_show_temp_deactivated(struct device
* dev
,
648 struct device_attribute
* attr
, char *buf
)
653 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_VOL
, &cap
,
654 "attempting to determine the temporary state");
658 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
661 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated
);
664 * tpm_chip_find_get - return tpm_chip for given chip number
666 static struct tpm_chip
*tpm_chip_find_get(int chip_num
)
668 struct tpm_chip
*pos
, *chip
= NULL
;
671 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
672 if (chip_num
!= TPM_ANY_NUM
&& chip_num
!= pos
->dev_num
)
675 if (try_module_get(pos
->dev
->driver
->owner
)) {
684 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
685 #define READ_PCR_RESULT_SIZE 30
686 static struct tpm_input_header pcrread_header
= {
687 .tag
= TPM_TAG_RQU_COMMAND
,
688 .length
= cpu_to_be32(14),
689 .ordinal
= TPM_ORDINAL_PCRREAD
692 int __tpm_pcr_read(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
)
695 struct tpm_cmd_t cmd
;
697 cmd
.header
.in
= pcrread_header
;
698 cmd
.params
.pcrread_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
699 rc
= transmit_cmd(chip
, &cmd
, READ_PCR_RESULT_SIZE
,
700 "attempting to read a pcr value");
703 memcpy(res_buf
, cmd
.params
.pcrread_out
.pcr_result
,
709 * tpm_pcr_read - read a pcr value
710 * @chip_num: tpm idx # or ANY
711 * @pcr_idx: pcr idx to retrieve
712 * @res_buf: TPM_PCR value
713 * size of res_buf is 20 bytes (or NULL if you don't care)
715 * The TPM driver should be built-in, but for whatever reason it
716 * isn't, protect against the chip disappearing, by incrementing
717 * the module usage count.
719 int tpm_pcr_read(u32 chip_num
, int pcr_idx
, u8
*res_buf
)
721 struct tpm_chip
*chip
;
724 chip
= tpm_chip_find_get(chip_num
);
727 rc
= __tpm_pcr_read(chip
, pcr_idx
, res_buf
);
728 module_put(chip
->dev
->driver
->owner
);
731 EXPORT_SYMBOL_GPL(tpm_pcr_read
);
734 * tpm_pcr_extend - extend pcr value with hash
735 * @chip_num: tpm idx # or AN&
736 * @pcr_idx: pcr idx to extend
737 * @hash: hash value used to extend pcr value
739 * The TPM driver should be built-in, but for whatever reason it
740 * isn't, protect against the chip disappearing, by incrementing
741 * the module usage count.
743 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
744 #define EXTEND_PCR_RESULT_SIZE 34
745 static struct tpm_input_header pcrextend_header
= {
746 .tag
= TPM_TAG_RQU_COMMAND
,
747 .length
= cpu_to_be32(34),
748 .ordinal
= TPM_ORD_PCR_EXTEND
751 int tpm_pcr_extend(u32 chip_num
, int pcr_idx
, const u8
*hash
)
753 struct tpm_cmd_t cmd
;
755 struct tpm_chip
*chip
;
757 chip
= tpm_chip_find_get(chip_num
);
761 cmd
.header
.in
= pcrextend_header
;
762 cmd
.params
.pcrextend_in
.pcr_idx
= cpu_to_be32(pcr_idx
);
763 memcpy(cmd
.params
.pcrextend_in
.hash
, hash
, TPM_DIGEST_SIZE
);
764 rc
= transmit_cmd(chip
, &cmd
, EXTEND_PCR_RESULT_SIZE
,
765 "attempting extend a PCR value");
767 module_put(chip
->dev
->driver
->owner
);
770 EXPORT_SYMBOL_GPL(tpm_pcr_extend
);
772 ssize_t
tpm_show_pcrs(struct device
*dev
, struct device_attribute
*attr
,
776 u8 digest
[TPM_DIGEST_SIZE
];
780 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
782 rc
= tpm_getcap(dev
, TPM_CAP_PROP_PCR
, &cap
,
783 "attempting to determine the number of PCRS");
787 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
788 for (i
= 0; i
< num_pcrs
; i
++) {
789 rc
= __tpm_pcr_read(chip
, i
, digest
);
792 str
+= sprintf(str
, "PCR-%02d: ", i
);
793 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
794 str
+= sprintf(str
, "%02X ", digest
[j
]);
795 str
+= sprintf(str
, "\n");
799 EXPORT_SYMBOL_GPL(tpm_show_pcrs
);
801 #define READ_PUBEK_RESULT_SIZE 314
802 #define TPM_ORD_READPUBEK cpu_to_be32(124)
803 struct tpm_input_header tpm_readpubek_header
= {
804 .tag
= TPM_TAG_RQU_COMMAND
,
805 .length
= cpu_to_be32(30),
806 .ordinal
= TPM_ORD_READPUBEK
809 ssize_t
tpm_show_pubek(struct device
*dev
, struct device_attribute
*attr
,
813 struct tpm_cmd_t tpm_cmd
;
818 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
820 tpm_cmd
.header
.in
= tpm_readpubek_header
;
821 err
= transmit_cmd(chip
, &tpm_cmd
, READ_PUBEK_RESULT_SIZE
,
822 "attempting to read the PUBEK");
827 ignore header 10 bytes
828 algorithm 32 bits (1 == RSA )
831 parameters (RSA 12->bytes: keybit, #primes, expbit)
834 ignore checksum 20 bytes
836 data
= tpm_cmd
.params
.readpubek_out_buffer
;
839 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
840 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
841 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
842 "Modulus length: %d\nModulus: \n",
843 data
[10], data
[11], data
[12], data
[13], data
[14],
844 data
[15], data
[16], data
[17], data
[22], data
[23],
845 data
[24], data
[25], data
[26], data
[27], data
[28],
846 data
[29], data
[30], data
[31], data
[32], data
[33],
847 be32_to_cpu(*((__be32
*) (data
+ 34))));
849 for (i
= 0; i
< 256; i
++) {
850 str
+= sprintf(str
, "%02X ", data
[i
+ 38]);
851 if ((i
+ 1) % 16 == 0)
852 str
+= sprintf(str
, "\n");
858 EXPORT_SYMBOL_GPL(tpm_show_pubek
);
861 ssize_t
tpm_show_caps(struct device
*dev
, struct device_attribute
*attr
,
868 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
869 "attempting to determine the manufacturer");
872 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
873 be32_to_cpu(cap
.manufacturer_id
));
875 rc
= tpm_getcap(dev
, CAP_VERSION_1_1
, &cap
,
876 "attempting to determine the 1.1 version");
880 "TCG version: %d.%d\nFirmware version: %d.%d\n",
881 cap
.tpm_version
.Major
, cap
.tpm_version
.Minor
,
882 cap
.tpm_version
.revMajor
, cap
.tpm_version
.revMinor
);
885 EXPORT_SYMBOL_GPL(tpm_show_caps
);
887 ssize_t
tpm_show_caps_1_2(struct device
* dev
,
888 struct device_attribute
* attr
, char *buf
)
894 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
895 "attempting to determine the manufacturer");
898 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
899 be32_to_cpu(cap
.manufacturer_id
));
900 rc
= tpm_getcap(dev
, CAP_VERSION_1_2
, &cap
,
901 "attempting to determine the 1.2 version");
905 "TCG version: %d.%d\nFirmware version: %d.%d\n",
906 cap
.tpm_version_1_2
.Major
, cap
.tpm_version_1_2
.Minor
,
907 cap
.tpm_version_1_2
.revMajor
,
908 cap
.tpm_version_1_2
.revMinor
);
911 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2
);
913 ssize_t
tpm_store_cancel(struct device
*dev
, struct device_attribute
*attr
,
914 const char *buf
, size_t count
)
916 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
920 chip
->vendor
.cancel(chip
);
923 EXPORT_SYMBOL_GPL(tpm_store_cancel
);
926 * Device file system interface to the TPM
928 * It's assured that the chip will be opened just once,
929 * by the check of is_open variable, which is protected
932 int tpm_open(struct inode
*inode
, struct file
*file
)
934 int minor
= iminor(inode
);
935 struct tpm_chip
*chip
= NULL
, *pos
;
938 list_for_each_entry_rcu(pos
, &tpm_chip_list
, list
) {
939 if (pos
->vendor
.miscdev
.minor
== minor
) {
941 get_device(chip
->dev
);
950 if (test_and_set_bit(0, &chip
->is_open
)) {
951 dev_dbg(chip
->dev
, "Another process owns this TPM\n");
952 put_device(chip
->dev
);
956 chip
->data_buffer
= kmalloc(TPM_BUFSIZE
* sizeof(u8
), GFP_KERNEL
);
957 if (chip
->data_buffer
== NULL
) {
958 clear_bit(0, &chip
->is_open
);
959 put_device(chip
->dev
);
963 atomic_set(&chip
->data_pending
, 0);
965 file
->private_data
= chip
;
968 EXPORT_SYMBOL_GPL(tpm_open
);
971 * Called on file close
973 int tpm_release(struct inode
*inode
, struct file
*file
)
975 struct tpm_chip
*chip
= file
->private_data
;
977 del_singleshot_timer_sync(&chip
->user_read_timer
);
978 flush_scheduled_work();
979 file
->private_data
= NULL
;
980 atomic_set(&chip
->data_pending
, 0);
981 kfree(chip
->data_buffer
);
982 clear_bit(0, &chip
->is_open
);
983 put_device(chip
->dev
);
986 EXPORT_SYMBOL_GPL(tpm_release
);
988 ssize_t
tpm_write(struct file
*file
, const char __user
*buf
,
989 size_t size
, loff_t
*off
)
991 struct tpm_chip
*chip
= file
->private_data
;
992 size_t in_size
= size
, out_size
;
994 /* cannot perform a write until the read has cleared
995 either via tpm_read or a user_read_timer timeout */
996 while (atomic_read(&chip
->data_pending
) != 0)
999 mutex_lock(&chip
->buffer_mutex
);
1001 if (in_size
> TPM_BUFSIZE
)
1002 in_size
= TPM_BUFSIZE
;
1005 (chip
->data_buffer
, (void __user
*) buf
, in_size
)) {
1006 mutex_unlock(&chip
->buffer_mutex
);
1010 /* atomic tpm command send and result receive */
1011 out_size
= tpm_transmit(chip
, chip
->data_buffer
, TPM_BUFSIZE
);
1013 atomic_set(&chip
->data_pending
, out_size
);
1014 mutex_unlock(&chip
->buffer_mutex
);
1016 /* Set a timeout by which the reader must come claim the result */
1017 mod_timer(&chip
->user_read_timer
, jiffies
+ (60 * HZ
));
1021 EXPORT_SYMBOL_GPL(tpm_write
);
1023 ssize_t
tpm_read(struct file
*file
, char __user
*buf
,
1024 size_t size
, loff_t
*off
)
1026 struct tpm_chip
*chip
= file
->private_data
;
1029 del_singleshot_timer_sync(&chip
->user_read_timer
);
1030 flush_scheduled_work();
1031 ret_size
= atomic_read(&chip
->data_pending
);
1032 atomic_set(&chip
->data_pending
, 0);
1033 if (ret_size
> 0) { /* relay data */
1034 if (size
< ret_size
)
1037 mutex_lock(&chip
->buffer_mutex
);
1038 if (copy_to_user(buf
, chip
->data_buffer
, ret_size
))
1040 mutex_unlock(&chip
->buffer_mutex
);
1045 EXPORT_SYMBOL_GPL(tpm_read
);
1047 void tpm_remove_hardware(struct device
*dev
)
1049 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1052 dev_err(dev
, "No device data found\n");
1056 spin_lock(&driver_lock
);
1057 list_del_rcu(&chip
->list
);
1058 spin_unlock(&driver_lock
);
1061 misc_deregister(&chip
->vendor
.miscdev
);
1062 sysfs_remove_group(&dev
->kobj
, chip
->vendor
.attr_group
);
1063 tpm_bios_log_teardown(chip
->bios_dir
);
1065 /* write it this way to be explicit (chip->dev == dev) */
1066 put_device(chip
->dev
);
1068 EXPORT_SYMBOL_GPL(tpm_remove_hardware
);
1071 * We are about to suspend. Save the TPM state
1072 * so that it can be restored.
1074 int tpm_pm_suspend(struct device
*dev
, pm_message_t pm_state
)
1076 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1078 0, 193, /* TPM_TAG_RQU_COMMAND */
1079 0, 0, 0, 10, /* blob length (in bytes) */
1080 0, 0, 0, 152 /* TPM_ORD_SaveState */
1086 tpm_transmit(chip
, savestate
, sizeof(savestate
));
1089 EXPORT_SYMBOL_GPL(tpm_pm_suspend
);
1092 * Resume from a power safe. The BIOS already restored
1095 int tpm_pm_resume(struct device
*dev
)
1097 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1104 EXPORT_SYMBOL_GPL(tpm_pm_resume
);
1106 /* In case vendor provided release function, call it too.*/
1108 void tpm_dev_vendor_release(struct tpm_chip
*chip
)
1110 if (chip
->vendor
.release
)
1111 chip
->vendor
.release(chip
->dev
);
1113 clear_bit(chip
->dev_num
, dev_mask
);
1114 kfree(chip
->vendor
.miscdev
.name
);
1116 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release
);
1120 * Once all references to platform device are down to 0,
1121 * release all allocated structures.
1123 void tpm_dev_release(struct device
*dev
)
1125 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1127 tpm_dev_vendor_release(chip
);
1132 EXPORT_SYMBOL_GPL(tpm_dev_release
);
1135 * Called from tpm_<specific>.c probe function only for devices
1136 * the driver has determined it should claim. Prior to calling
1137 * this function the specific probe function has called pci_enable_device
1138 * upon errant exit from this function specific probe function should call
1139 * pci_disable_device
1141 struct tpm_chip
*tpm_register_hardware(struct device
*dev
,
1142 const struct tpm_vendor_specific
*entry
)
1144 #define DEVNAME_SIZE 7
1147 struct tpm_chip
*chip
;
1149 /* Driver specific per-device data */
1150 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1151 devname
= kmalloc(DEVNAME_SIZE
, GFP_KERNEL
);
1153 if (chip
== NULL
|| devname
== NULL
)
1156 mutex_init(&chip
->buffer_mutex
);
1157 mutex_init(&chip
->tpm_mutex
);
1158 INIT_LIST_HEAD(&chip
->list
);
1160 INIT_WORK(&chip
->work
, timeout_work
);
1162 setup_timer(&chip
->user_read_timer
, user_reader_timeout
,
1163 (unsigned long)chip
);
1165 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
1167 chip
->dev_num
= find_first_zero_bit(dev_mask
, TPM_NUM_DEVICES
);
1169 if (chip
->dev_num
>= TPM_NUM_DEVICES
) {
1170 dev_err(dev
, "No available tpm device numbers\n");
1172 } else if (chip
->dev_num
== 0)
1173 chip
->vendor
.miscdev
.minor
= TPM_MINOR
;
1175 chip
->vendor
.miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1177 set_bit(chip
->dev_num
, dev_mask
);
1179 scnprintf(devname
, DEVNAME_SIZE
, "%s%d", "tpm", chip
->dev_num
);
1180 chip
->vendor
.miscdev
.name
= devname
;
1182 chip
->vendor
.miscdev
.parent
= dev
;
1183 chip
->dev
= get_device(dev
);
1184 chip
->release
= dev
->release
;
1185 dev
->release
= tpm_dev_release
;
1186 dev_set_drvdata(dev
, chip
);
1188 if (misc_register(&chip
->vendor
.miscdev
)) {
1190 "unable to misc_register %s, minor %d\n",
1191 chip
->vendor
.miscdev
.name
,
1192 chip
->vendor
.miscdev
.minor
);
1193 put_device(chip
->dev
);
1197 if (sysfs_create_group(&dev
->kobj
, chip
->vendor
.attr_group
)) {
1198 misc_deregister(&chip
->vendor
.miscdev
);
1199 put_device(chip
->dev
);
1204 chip
->bios_dir
= tpm_bios_log_setup(devname
);
1206 /* Make chip available */
1207 spin_lock(&driver_lock
);
1208 list_add_rcu(&chip
->list
, &tpm_chip_list
);
1209 spin_unlock(&driver_lock
);
1218 EXPORT_SYMBOL_GPL(tpm_register_hardware
);
1220 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1221 MODULE_DESCRIPTION("TPM Driver");
1222 MODULE_VERSION("2.0");
1223 MODULE_LICENSE("GPL");