tpm: rename chip->dev to chip->pdev
[cascardo/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 #include <linux/acpi.h>
31
32 enum tpm_const {
33         TPM_MINOR = 224,        /* officially assigned */
34         TPM_BUFSIZE = 4096,
35         TPM_NUM_DEVICES = 256,
36         TPM_RETRY = 50,         /* 5 seconds */
37 };
38
39 enum tpm_timeout {
40         TPM_TIMEOUT = 5,        /* msecs */
41         TPM_TIMEOUT_RETRY = 100 /* msecs */
42 };
43
44 /* TPM addresses */
45 enum tpm_addr {
46         TPM_SUPERIO_ADDR = 0x2E,
47         TPM_ADDR = 0x4E,
48 };
49
50 /* Indexes the duration array */
51 enum tpm_duration {
52         TPM_SHORT = 0,
53         TPM_MEDIUM = 1,
54         TPM_LONG = 2,
55         TPM_UNDEFINED,
56 };
57
58 #define TPM_WARN_RETRY          0x800
59 #define TPM_WARN_DOING_SELFTEST 0x802
60 #define TPM_ERR_DEACTIVATED     0x6
61 #define TPM_ERR_DISABLED        0x7
62 #define TPM_ERR_INVALID_POSTINIT 38
63
64 #define TPM_HEADER_SIZE         10
65 struct tpm_chip;
66
67 struct tpm_vendor_specific {
68         void __iomem *iobase;           /* ioremapped address */
69         unsigned long base;             /* TPM base address */
70
71         int irq;
72         int probed_irq;
73
74         int region_size;
75         int have_region;
76
77         struct miscdevice miscdev;
78         struct list_head list;
79         int locality;
80         unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
81         bool timeout_adjusted;
82         unsigned long duration[3]; /* jiffies */
83         bool duration_adjusted;
84         void *priv;
85
86         wait_queue_head_t read_queue;
87         wait_queue_head_t int_queue;
88
89         u16 manufacturer_id;
90 };
91
92 #define TPM_VPRIV(c)    (c)->vendor.priv
93
94 #define TPM_VID_INTEL    0x8086
95 #define TPM_VID_WINBOND  0x1050
96 #define TPM_VID_STM      0x104A
97
98 #define TPM_PPI_VERSION_LEN             3
99
100 enum tpm_chip_flags {
101         TPM_CHIP_FLAG_REGISTERED        = BIT(0),
102         TPM_CHIP_FLAG_PPI               = BIT(1),
103 };
104
105 struct tpm_chip {
106         struct device *pdev;    /* Device stuff */
107         const struct tpm_class_ops *ops;
108         unsigned int flags;
109
110         int dev_num;            /* /dev/tpm# */
111         char devname[7];
112         unsigned long is_open;  /* only one allowed */
113         int time_expired;
114
115         struct mutex tpm_mutex; /* tpm is processing */
116
117         struct tpm_vendor_specific vendor;
118
119         struct dentry **bios_dir;
120
121 #ifdef CONFIG_ACPI
122         acpi_handle acpi_dev_handle;
123         char ppi_version[TPM_PPI_VERSION_LEN + 1];
124 #endif /* CONFIG_ACPI */
125
126         struct list_head list;
127 };
128
129 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
130
131 static inline void tpm_chip_put(struct tpm_chip *chip)
132 {
133         module_put(chip->pdev->driver->owner);
134 }
135
136 static inline int tpm_read_index(int base, int index)
137 {
138         outb(index, base);
139         return inb(base+1) & 0xFF;
140 }
141
142 static inline void tpm_write_index(int base, int index, int value)
143 {
144         outb(index, base);
145         outb(value & 0xFF, base+1);
146 }
147 struct tpm_input_header {
148         __be16  tag;
149         __be32  length;
150         __be32  ordinal;
151 } __packed;
152
153 struct tpm_output_header {
154         __be16  tag;
155         __be32  length;
156         __be32  return_code;
157 } __packed;
158
159 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
160
161 struct  stclear_flags_t {
162         __be16  tag;
163         u8      deactivated;
164         u8      disableForceClear;
165         u8      physicalPresence;
166         u8      physicalPresenceLock;
167         u8      bGlobalLock;
168 } __packed;
169
170 struct  tpm_version_t {
171         u8      Major;
172         u8      Minor;
173         u8      revMajor;
174         u8      revMinor;
175 } __packed;
176
177 struct  tpm_version_1_2_t {
178         __be16  tag;
179         u8      Major;
180         u8      Minor;
181         u8      revMajor;
182         u8      revMinor;
183 } __packed;
184
185 struct  timeout_t {
186         __be32  a;
187         __be32  b;
188         __be32  c;
189         __be32  d;
190 } __packed;
191
192 struct duration_t {
193         __be32  tpm_short;
194         __be32  tpm_medium;
195         __be32  tpm_long;
196 } __packed;
197
198 struct permanent_flags_t {
199         __be16  tag;
200         u8      disable;
201         u8      ownership;
202         u8      deactivated;
203         u8      readPubek;
204         u8      disableOwnerClear;
205         u8      allowMaintenance;
206         u8      physicalPresenceLifetimeLock;
207         u8      physicalPresenceHWEnable;
208         u8      physicalPresenceCMDEnable;
209         u8      CEKPUsed;
210         u8      TPMpost;
211         u8      TPMpostLock;
212         u8      FIPS;
213         u8      operator;
214         u8      enableRevokeEK;
215         u8      nvLocked;
216         u8      readSRKPub;
217         u8      tpmEstablished;
218         u8      maintenanceDone;
219         u8      disableFullDALogicInfo;
220 } __packed;
221
222 typedef union {
223         struct  permanent_flags_t perm_flags;
224         struct  stclear_flags_t stclear_flags;
225         bool    owned;
226         __be32  num_pcrs;
227         struct  tpm_version_t   tpm_version;
228         struct  tpm_version_1_2_t tpm_version_1_2;
229         __be32  manufacturer_id;
230         struct timeout_t  timeout;
231         struct duration_t duration;
232 } cap_t;
233
234 enum tpm_capabilities {
235         TPM_CAP_FLAG = cpu_to_be32(4),
236         TPM_CAP_PROP = cpu_to_be32(5),
237         CAP_VERSION_1_1 = cpu_to_be32(0x06),
238         CAP_VERSION_1_2 = cpu_to_be32(0x1A)
239 };
240
241 enum tpm_sub_capabilities {
242         TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
243         TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
244         TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
245         TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
246         TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
247         TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
248         TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
249
250 };
251
252 struct  tpm_getcap_params_in {
253         __be32  cap;
254         __be32  subcap_size;
255         __be32  subcap;
256 } __packed;
257
258 struct  tpm_getcap_params_out {
259         __be32  cap_size;
260         cap_t   cap;
261 } __packed;
262
263 struct  tpm_readpubek_params_out {
264         u8      algorithm[4];
265         u8      encscheme[2];
266         u8      sigscheme[2];
267         __be32  paramsize;
268         u8      parameters[12]; /*assuming RSA*/
269         __be32  keysize;
270         u8      modulus[256];
271         u8      checksum[20];
272 } __packed;
273
274 typedef union {
275         struct  tpm_input_header in;
276         struct  tpm_output_header out;
277 } tpm_cmd_header;
278
279 struct tpm_pcrread_out {
280         u8      pcr_result[TPM_DIGEST_SIZE];
281 } __packed;
282
283 struct tpm_pcrread_in {
284         __be32  pcr_idx;
285 } __packed;
286
287 struct tpm_pcrextend_in {
288         __be32  pcr_idx;
289         u8      hash[TPM_DIGEST_SIZE];
290 } __packed;
291
292 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
293  * bytes, but 128 is still a relatively large number of random bytes and
294  * anything much bigger causes users of struct tpm_cmd_t to start getting
295  * compiler warnings about stack frame size. */
296 #define TPM_MAX_RNG_DATA        128
297
298 struct tpm_getrandom_out {
299         __be32 rng_data_len;
300         u8     rng_data[TPM_MAX_RNG_DATA];
301 } __packed;
302
303 struct tpm_getrandom_in {
304         __be32 num_bytes;
305 } __packed;
306
307 struct tpm_startup_in {
308         __be16  startup_type;
309 } __packed;
310
311 typedef union {
312         struct  tpm_getcap_params_out getcap_out;
313         struct  tpm_readpubek_params_out readpubek_out;
314         u8      readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
315         struct  tpm_getcap_params_in getcap_in;
316         struct  tpm_pcrread_in  pcrread_in;
317         struct  tpm_pcrread_out pcrread_out;
318         struct  tpm_pcrextend_in pcrextend_in;
319         struct  tpm_getrandom_in getrandom_in;
320         struct  tpm_getrandom_out getrandom_out;
321         struct tpm_startup_in startup_in;
322 } tpm_cmd_params;
323
324 struct tpm_cmd_t {
325         tpm_cmd_header  header;
326         tpm_cmd_params  params;
327 } __packed;
328
329 ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
330 ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
331                      size_t bufsiz);
332 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
333                          const char *desc);
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 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);
342
343 struct tpm_chip *tpm_chip_find_get(int chip_num);
344 extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
345                                        const struct tpm_class_ops *ops);
346 extern int tpm_chip_register(struct tpm_chip *chip);
347 extern void tpm_chip_unregister(struct tpm_chip *chip);
348
349 int tpm_dev_add_device(struct tpm_chip *chip);
350 void tpm_dev_del_device(struct tpm_chip *chip);
351 int tpm_sysfs_add_device(struct tpm_chip *chip);
352 void tpm_sysfs_del_device(struct tpm_chip *chip);
353
354 int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
355
356 #ifdef CONFIG_ACPI
357 extern int tpm_add_ppi(struct tpm_chip *chip);
358 extern void tpm_remove_ppi(struct tpm_chip *chip);
359 #else
360 static inline int tpm_add_ppi(struct tpm_chip *chip)
361 {
362         return 0;
363 }
364
365 static inline void tpm_remove_ppi(struct tpm_chip *chip)
366 {
367 }
368 #endif