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
21 #include <linux/module.h>
22 #include <linux/delay.h>
24 #include <linux/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/miscdevice.h>
27 #include <linux/platform_device.h>
29 #include <linux/tpm.h>
32 TPM_MINOR
= 224, /* officially assigned */
34 TPM_NUM_DEVICES
= 256,
35 TPM_RETRY
= 50, /* 5 seconds */
39 TPM_TIMEOUT
= 5, /* msecs */
40 TPM_TIMEOUT_RETRY
= 100 /* msecs */
45 TPM_SUPERIO_ADDR
= 0x2E,
49 /* Indexes the duration array */
57 #define TPM_WARN_RETRY 0x800
58 #define TPM_WARN_DOING_SELFTEST 0x802
59 #define TPM_ERR_DEACTIVATED 0x6
60 #define TPM_ERR_DISABLED 0x7
61 #define TPM_ERR_INVALID_POSTINIT 38
63 #define TPM_HEADER_SIZE 10
66 struct tpm_vendor_specific
{
69 bool (*req_canceled
)(struct tpm_chip
*chip
, u8 status
);
70 void __iomem
*iobase
; /* ioremapped address */
71 unsigned long base
; /* TPM base address */
79 int (*recv
) (struct tpm_chip
*, u8
*, size_t);
80 int (*send
) (struct tpm_chip
*, u8
*, size_t);
81 void (*cancel
) (struct tpm_chip
*);
82 u8 (*status
) (struct tpm_chip
*);
83 void (*release
) (struct device
*);
84 struct miscdevice miscdev
;
85 struct list_head list
;
87 unsigned long timeout_a
, timeout_b
, timeout_c
, timeout_d
; /* jiffies */
88 bool timeout_adjusted
;
89 unsigned long duration
[3]; /* jiffies */
90 bool duration_adjusted
;
93 wait_queue_head_t read_queue
;
94 wait_queue_head_t int_queue
;
99 #define TPM_VPRIV(c) (c)->vendor.priv
101 #define TPM_VID_INTEL 0x8086
102 #define TPM_VID_WINBOND 0x1050
103 #define TPM_VID_STM 0x104A
106 struct device
*dev
; /* Device stuff */
108 int dev_num
; /* /dev/tpm# */
110 unsigned long is_open
; /* only one allowed */
113 /* Data passed to and from the tpm via the read/write calls */
115 atomic_t data_pending
;
116 struct mutex buffer_mutex
;
118 struct timer_list user_read_timer
; /* user needs to claim result */
119 struct work_struct work
;
120 struct mutex tpm_mutex
; /* tpm is processing */
122 struct tpm_vendor_specific vendor
;
124 struct dentry
**bios_dir
;
126 struct list_head list
;
127 void (*release
) (struct device
*);
130 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
132 static inline void tpm_chip_put(struct tpm_chip
*chip
)
134 module_put(chip
->dev
->driver
->owner
);
137 static inline int tpm_read_index(int base
, int index
)
140 return inb(base
+1) & 0xFF;
143 static inline void tpm_write_index(int base
, int index
, int value
)
146 outb(value
& 0xFF, base
+1);
148 struct tpm_input_header
{
154 struct tpm_output_header
{
160 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
162 struct stclear_flags_t
{
165 u8 disableForceClear
;
167 u8 physicalPresenceLock
;
171 struct tpm_version_t
{
178 struct tpm_version_1_2_t
{
199 struct permanent_flags_t
{
205 u8 disableOwnerClear
;
207 u8 physicalPresenceLifetimeLock
;
208 u8 physicalPresenceHWEnable
;
209 u8 physicalPresenceCMDEnable
;
220 u8 disableFullDALogicInfo
;
224 struct permanent_flags_t perm_flags
;
225 struct stclear_flags_t stclear_flags
;
228 struct tpm_version_t tpm_version
;
229 struct tpm_version_1_2_t tpm_version_1_2
;
230 __be32 manufacturer_id
;
231 struct timeout_t timeout
;
232 struct duration_t duration
;
235 enum tpm_capabilities
{
236 TPM_CAP_FLAG
= cpu_to_be32(4),
237 TPM_CAP_PROP
= cpu_to_be32(5),
238 CAP_VERSION_1_1
= cpu_to_be32(0x06),
239 CAP_VERSION_1_2
= cpu_to_be32(0x1A)
242 enum tpm_sub_capabilities
{
243 TPM_CAP_PROP_PCR
= cpu_to_be32(0x101),
244 TPM_CAP_PROP_MANUFACTURER
= cpu_to_be32(0x103),
245 TPM_CAP_FLAG_PERM
= cpu_to_be32(0x108),
246 TPM_CAP_FLAG_VOL
= cpu_to_be32(0x109),
247 TPM_CAP_PROP_OWNER
= cpu_to_be32(0x111),
248 TPM_CAP_PROP_TIS_TIMEOUT
= cpu_to_be32(0x115),
249 TPM_CAP_PROP_TIS_DURATION
= cpu_to_be32(0x120),
253 struct tpm_getcap_params_in
{
259 struct tpm_getcap_params_out
{
264 struct tpm_readpubek_params_out
{
269 u8 parameters
[12]; /*assuming RSA*/
276 struct tpm_input_header in
;
277 struct tpm_output_header out
;
280 struct tpm_pcrread_out
{
281 u8 pcr_result
[TPM_DIGEST_SIZE
];
284 struct tpm_pcrread_in
{
288 struct tpm_pcrextend_in
{
290 u8 hash
[TPM_DIGEST_SIZE
];
293 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
294 * bytes, but 128 is still a relatively large number of random bytes and
295 * anything much bigger causes users of struct tpm_cmd_t to start getting
296 * compiler warnings about stack frame size. */
297 #define TPM_MAX_RNG_DATA 128
299 struct tpm_getrandom_out
{
301 u8 rng_data
[TPM_MAX_RNG_DATA
];
304 struct tpm_getrandom_in
{
308 struct tpm_startup_in
{
313 struct tpm_getcap_params_out getcap_out
;
314 struct tpm_readpubek_params_out readpubek_out
;
315 u8 readpubek_out_buffer
[sizeof(struct tpm_readpubek_params_out
)];
316 struct tpm_getcap_params_in getcap_in
;
317 struct tpm_pcrread_in pcrread_in
;
318 struct tpm_pcrread_out pcrread_out
;
319 struct tpm_pcrextend_in pcrextend_in
;
320 struct tpm_getrandom_in getrandom_in
;
321 struct tpm_getrandom_out getrandom_out
;
322 struct tpm_startup_in startup_in
;
326 tpm_cmd_header header
;
327 tpm_cmd_params params
;
330 ssize_t
tpm_getcap(struct device
*, __be32
, cap_t
*, const char *);
332 ssize_t
tpm_transmit(struct tpm_chip
*chip
, const char *buf
,
334 extern int tpm_get_timeouts(struct tpm_chip
*);
335 extern void tpm_gen_interrupt(struct tpm_chip
*);
336 extern int tpm_do_selftest(struct tpm_chip
*);
337 extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*, u32
);
338 extern struct tpm_chip
* tpm_register_hardware(struct device
*,
339 const struct tpm_class_ops
*ops
);
340 extern void tpm_dev_vendor_release(struct tpm_chip
*);
341 extern void tpm_remove_hardware(struct device
*);
342 extern int tpm_pm_suspend(struct device
*);
343 extern int tpm_pm_resume(struct device
*);
344 extern int wait_for_tpm_stat(struct tpm_chip
*, u8
, unsigned long,
345 wait_queue_head_t
*, bool);
347 int tpm_dev_add_device(struct tpm_chip
*chip
);
348 void tpm_dev_del_device(struct tpm_chip
*chip
);
349 int tpm_sysfs_add_device(struct tpm_chip
*chip
);
350 void tpm_sysfs_del_device(struct tpm_chip
*chip
);
352 int tpm_pcr_read_dev(struct tpm_chip
*chip
, int pcr_idx
, u8
*res_buf
);
355 extern int tpm_add_ppi(struct kobject
*);
356 extern void tpm_remove_ppi(struct kobject
*);
358 static inline int tpm_add_ppi(struct kobject
*parent
)
363 static inline void tpm_remove_ppi(struct kobject
*parent
)