]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/char/tpm/tpm.h
tpm: Create a tpm_class_ops structure and use it in the drivers
[linux.git] / drivers / char / tpm / tpm.h
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  *
4  * Authors:
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>
9  *
10  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org       
14  *
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
18  * License.
19  * 
20  */
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/fs.h>
24 #include <linux/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/miscdevice.h>
27 #include <linux/platform_device.h>
28 #include <linux/io.h>
29 #include <linux/tpm.h>
30
31 enum tpm_const {
32         TPM_MINOR = 224,        /* officially assigned */
33         TPM_BUFSIZE = 4096,
34         TPM_NUM_DEVICES = 256,
35         TPM_RETRY = 50,         /* 5 seconds */
36 };
37
38 enum tpm_timeout {
39         TPM_TIMEOUT = 5,        /* msecs */
40         TPM_TIMEOUT_RETRY = 100 /* msecs */
41 };
42
43 /* TPM addresses */
44 enum tpm_addr {
45         TPM_SUPERIO_ADDR = 0x2E,
46         TPM_ADDR = 0x4E,
47 };
48
49 /* Indexes the duration array */
50 enum tpm_duration {
51         TPM_SHORT = 0,
52         TPM_MEDIUM = 1,
53         TPM_LONG = 2,
54         TPM_UNDEFINED,
55 };
56
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
62
63 #define TPM_HEADER_SIZE         10
64 struct tpm_chip;
65
66 struct tpm_vendor_specific {
67         u8 req_complete_mask;
68         u8 req_complete_val;
69         bool (*req_canceled)(struct tpm_chip *chip, u8 status);
70         void __iomem *iobase;           /* ioremapped address */
71         unsigned long base;             /* TPM base address */
72
73         int irq;
74         int probed_irq;
75
76         int region_size;
77         int have_region;
78
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;
86         int locality;
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;
91         void *priv;
92
93         wait_queue_head_t read_queue;
94         wait_queue_head_t int_queue;
95
96         u16 manufacturer_id;
97 };
98
99 #define TPM_VPRIV(c)    (c)->vendor.priv
100
101 #define TPM_VID_INTEL    0x8086
102 #define TPM_VID_WINBOND  0x1050
103 #define TPM_VID_STM      0x104A
104
105 struct tpm_chip {
106         struct device *dev;     /* Device stuff */
107
108         int dev_num;            /* /dev/tpm# */
109         char devname[7];
110         unsigned long is_open;  /* only one allowed */
111         int time_expired;
112
113         /* Data passed to and from the tpm via the read/write calls */
114         u8 *data_buffer;
115         atomic_t data_pending;
116         struct mutex buffer_mutex;
117
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 */
121
122         struct tpm_vendor_specific vendor;
123
124         struct dentry **bios_dir;
125
126         struct list_head list;
127         void (*release) (struct device *);
128 };
129
130 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
131
132 static inline void tpm_chip_put(struct tpm_chip *chip)
133 {
134         module_put(chip->dev->driver->owner);
135 }
136
137 static inline int tpm_read_index(int base, int index)
138 {
139         outb(index, base);
140         return inb(base+1) & 0xFF;
141 }
142
143 static inline void tpm_write_index(int base, int index, int value)
144 {
145         outb(index, base);
146         outb(value & 0xFF, base+1);
147 }
148 struct tpm_input_header {
149         __be16  tag;
150         __be32  length;
151         __be32  ordinal;
152 } __packed;
153
154 struct tpm_output_header {
155         __be16  tag;
156         __be32  length;
157         __be32  return_code;
158 } __packed;
159
160 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
161
162 struct  stclear_flags_t {
163         __be16  tag;
164         u8      deactivated;
165         u8      disableForceClear;
166         u8      physicalPresence;
167         u8      physicalPresenceLock;
168         u8      bGlobalLock;
169 } __packed;
170
171 struct  tpm_version_t {
172         u8      Major;
173         u8      Minor;
174         u8      revMajor;
175         u8      revMinor;
176 } __packed;
177
178 struct  tpm_version_1_2_t {
179         __be16  tag;
180         u8      Major;
181         u8      Minor;
182         u8      revMajor;
183         u8      revMinor;
184 } __packed;
185
186 struct  timeout_t {
187         __be32  a;
188         __be32  b;
189         __be32  c;
190         __be32  d;
191 } __packed;
192
193 struct duration_t {
194         __be32  tpm_short;
195         __be32  tpm_medium;
196         __be32  tpm_long;
197 } __packed;
198
199 struct permanent_flags_t {
200         __be16  tag;
201         u8      disable;
202         u8      ownership;
203         u8      deactivated;
204         u8      readPubek;
205         u8      disableOwnerClear;
206         u8      allowMaintenance;
207         u8      physicalPresenceLifetimeLock;
208         u8      physicalPresenceHWEnable;
209         u8      physicalPresenceCMDEnable;
210         u8      CEKPUsed;
211         u8      TPMpost;
212         u8      TPMpostLock;
213         u8      FIPS;
214         u8      operator;
215         u8      enableRevokeEK;
216         u8      nvLocked;
217         u8      readSRKPub;
218         u8      tpmEstablished;
219         u8      maintenanceDone;
220         u8      disableFullDALogicInfo;
221 } __packed;
222
223 typedef union {
224         struct  permanent_flags_t perm_flags;
225         struct  stclear_flags_t stclear_flags;
226         bool    owned;
227         __be32  num_pcrs;
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;
233 } cap_t;
234
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)
240 };
241
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),
250
251 };
252
253 struct  tpm_getcap_params_in {
254         __be32  cap;
255         __be32  subcap_size;
256         __be32  subcap;
257 } __packed;
258
259 struct  tpm_getcap_params_out {
260         __be32  cap_size;
261         cap_t   cap;
262 } __packed;
263
264 struct  tpm_readpubek_params_out {
265         u8      algorithm[4];
266         u8      encscheme[2];
267         u8      sigscheme[2];
268         __be32  paramsize;
269         u8      parameters[12]; /*assuming RSA*/
270         __be32  keysize;
271         u8      modulus[256];
272         u8      checksum[20];
273 } __packed;
274
275 typedef union {
276         struct  tpm_input_header in;
277         struct  tpm_output_header out;
278 } tpm_cmd_header;
279
280 struct tpm_pcrread_out {
281         u8      pcr_result[TPM_DIGEST_SIZE];
282 } __packed;
283
284 struct tpm_pcrread_in {
285         __be32  pcr_idx;
286 } __packed;
287
288 struct tpm_pcrextend_in {
289         __be32  pcr_idx;
290         u8      hash[TPM_DIGEST_SIZE];
291 } __packed;
292
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
298
299 struct tpm_getrandom_out {
300         __be32 rng_data_len;
301         u8     rng_data[TPM_MAX_RNG_DATA];
302 } __packed;
303
304 struct tpm_getrandom_in {
305         __be32 num_bytes;
306 } __packed;
307
308 struct tpm_startup_in {
309         __be16  startup_type;
310 } __packed;
311
312 typedef union {
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;
323 } tpm_cmd_params;
324
325 struct tpm_cmd_t {
326         tpm_cmd_header  header;
327         tpm_cmd_params  params;
328 } __packed;
329
330 ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
331
332 ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
333                      size_t bufsiz);
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);
346
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);
351
352 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
353
354 #ifdef CONFIG_ACPI
355 extern int tpm_add_ppi(struct kobject *);
356 extern void tpm_remove_ppi(struct kobject *);
357 #else
358 static inline int tpm_add_ppi(struct kobject *parent)
359 {
360         return 0;
361 }
362
363 static inline void tpm_remove_ppi(struct kobject *parent)
364 {
365 }
366 #endif