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,
38 TPM_TIMEOUT = 5, /* msecs */
43 TPM_SUPERIO_ADDR = 0x2E,
47 #define TPM_WARN_DOING_SELFTEST 0x802
48 #define TPM_ERR_DEACTIVATED 0x6
49 #define TPM_ERR_DISABLED 0x7
50 #define TPM_ERR_INVALID_POSTINIT 38
52 #define TPM_HEADER_SIZE 10
53 extern ssize_t tpm_show_pubek(struct device *, struct device_attribute *attr,
55 extern ssize_t tpm_show_pcrs(struct device *, struct device_attribute *attr,
57 extern ssize_t tpm_show_caps(struct device *, struct device_attribute *attr,
59 extern ssize_t tpm_show_caps_1_2(struct device *, struct device_attribute *attr,
61 extern ssize_t tpm_store_cancel(struct device *, struct device_attribute *attr,
62 const char *, size_t);
63 extern ssize_t tpm_show_enabled(struct device *, struct device_attribute *attr,
65 extern ssize_t tpm_show_active(struct device *, struct device_attribute *attr,
67 extern ssize_t tpm_show_owned(struct device *, struct device_attribute *attr,
69 extern ssize_t tpm_show_temp_deactivated(struct device *,
70 struct device_attribute *attr, char *);
71 extern ssize_t tpm_show_durations(struct device *,
72 struct device_attribute *attr, char *);
73 extern ssize_t tpm_show_timeouts(struct device *,
74 struct device_attribute *attr, char *);
78 struct tpm_vendor_specific {
79 const u8 req_complete_mask;
80 const u8 req_complete_val;
81 bool (*req_canceled)(struct tpm_chip *chip, u8 status);
82 void __iomem *iobase; /* ioremapped address */
83 unsigned long base; /* TPM base address */
91 int (*recv) (struct tpm_chip *, u8 *, size_t);
92 int (*send) (struct tpm_chip *, u8 *, size_t);
93 void (*cancel) (struct tpm_chip *);
94 u8 (*status) (struct tpm_chip *);
95 void (*release) (struct device *);
96 struct miscdevice miscdev;
97 struct attribute_group *attr_group;
98 struct list_head list;
100 unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
101 bool timeout_adjusted;
102 unsigned long duration[3]; /* jiffies */
103 bool duration_adjusted;
106 wait_queue_head_t read_queue;
107 wait_queue_head_t int_queue;
112 #define TPM_VPRIV(c) (c)->vendor.priv
114 #define TPM_VID_INTEL 0x8086
115 #define TPM_VID_WINBOND 0x1050
116 #define TPM_VID_STM 0x104A
119 struct device *dev; /* Device stuff */
121 int dev_num; /* /dev/tpm# */
122 unsigned long is_open; /* only one allowed */
125 /* Data passed to and from the tpm via the read/write calls */
127 atomic_t data_pending;
128 struct mutex buffer_mutex;
130 struct timer_list user_read_timer; /* user needs to claim result */
131 struct work_struct work;
132 struct mutex tpm_mutex; /* tpm is processing */
134 struct tpm_vendor_specific vendor;
136 struct dentry **bios_dir;
138 struct list_head list;
139 void (*release) (struct device *);
142 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
144 static inline void tpm_chip_put(struct tpm_chip *chip)
146 module_put(chip->dev->driver->owner);
149 static inline int tpm_read_index(int base, int index)
152 return inb(base+1) & 0xFF;
155 static inline void tpm_write_index(int base, int index, int value)
158 outb(value & 0xFF, base+1);
160 struct tpm_input_header {
166 struct tpm_output_header {
172 struct stclear_flags_t {
175 u8 disableForceClear;
177 u8 physicalPresenceLock;
181 struct tpm_version_t {
188 struct tpm_version_1_2_t {
209 struct permanent_flags_t {
215 u8 disableOwnerClear;
217 u8 physicalPresenceLifetimeLock;
218 u8 physicalPresenceHWEnable;
219 u8 physicalPresenceCMDEnable;
230 u8 disableFullDALogicInfo;
234 struct permanent_flags_t perm_flags;
235 struct stclear_flags_t stclear_flags;
238 struct tpm_version_t tpm_version;
239 struct tpm_version_1_2_t tpm_version_1_2;
240 __be32 manufacturer_id;
241 struct timeout_t timeout;
242 struct duration_t duration;
245 struct tpm_getcap_params_in {
251 struct tpm_getcap_params_out {
256 struct tpm_readpubek_params_out {
261 u8 parameters[12]; /*assuming RSA*/
268 struct tpm_input_header in;
269 struct tpm_output_header out;
272 #define TPM_DIGEST_SIZE 20
273 struct tpm_pcrread_out {
274 u8 pcr_result[TPM_DIGEST_SIZE];
277 struct tpm_pcrread_in {
281 struct tpm_pcrextend_in {
283 u8 hash[TPM_DIGEST_SIZE];
286 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
287 * bytes, but 128 is still a relatively large number of random bytes and
288 * anything much bigger causes users of struct tpm_cmd_t to start getting
289 * compiler warnings about stack frame size. */
290 #define TPM_MAX_RNG_DATA 128
292 struct tpm_getrandom_out {
294 u8 rng_data[TPM_MAX_RNG_DATA];
297 struct tpm_getrandom_in {
301 struct tpm_startup_in {
306 struct tpm_getcap_params_out getcap_out;
307 struct tpm_readpubek_params_out readpubek_out;
308 u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
309 struct tpm_getcap_params_in getcap_in;
310 struct tpm_pcrread_in pcrread_in;
311 struct tpm_pcrread_out pcrread_out;
312 struct tpm_pcrextend_in pcrextend_in;
313 struct tpm_getrandom_in getrandom_in;
314 struct tpm_getrandom_out getrandom_out;
315 struct tpm_startup_in startup_in;
319 tpm_cmd_header header;
320 tpm_cmd_params params;
323 ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
325 extern int tpm_get_timeouts(struct tpm_chip *);
326 extern void tpm_gen_interrupt(struct tpm_chip *);
327 extern int tpm_do_selftest(struct tpm_chip *);
328 extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
329 extern struct tpm_chip* tpm_register_hardware(struct device *,
330 const struct tpm_vendor_specific *);
331 extern int tpm_open(struct inode *, struct file *);
332 extern int tpm_release(struct inode *, struct file *);
333 extern void tpm_dev_vendor_release(struct tpm_chip *);
334 extern ssize_t tpm_write(struct file *, const char __user *, size_t,
336 extern ssize_t tpm_read(struct file *, char __user *, size_t, loff_t *);
337 extern void tpm_remove_hardware(struct device *);
338 extern int tpm_pm_suspend(struct device *);
339 extern int tpm_pm_resume(struct device *);
340 extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
341 wait_queue_head_t *, bool);
344 extern int tpm_add_ppi(struct kobject *);
345 extern void tpm_remove_ppi(struct kobject *);
347 static inline int tpm_add_ppi(struct kobject *parent)
352 static inline void tpm_remove_ppi(struct kobject *parent)