Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.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 #define TPM_WARN_RETRY          0x800
50 #define TPM_WARN_DOING_SELFTEST 0x802
51 #define TPM_ERR_DEACTIVATED     0x6
52 #define TPM_ERR_DISABLED        0x7
53 #define TPM_ERR_INVALID_POSTINIT 38
54
55 #define TPM_HEADER_SIZE         10
56 extern ssize_t tpm_show_pubek(struct device *, struct device_attribute *attr,
57                                 char *);
58 extern ssize_t tpm_show_pcrs(struct device *, struct device_attribute *attr,
59                                 char *);
60 extern ssize_t tpm_show_caps(struct device *, struct device_attribute *attr,
61                                 char *);
62 extern ssize_t tpm_store_cancel(struct device *, struct device_attribute *attr,
63                                 const char *, size_t);
64 extern ssize_t tpm_show_enabled(struct device *, struct device_attribute *attr,
65                                 char *);
66 extern ssize_t tpm_show_active(struct device *, struct device_attribute *attr,
67                                 char *);
68 extern ssize_t tpm_show_owned(struct device *, struct device_attribute *attr,
69                                 char *);
70 extern ssize_t tpm_show_temp_deactivated(struct device *,
71                                          struct device_attribute *attr, char *);
72 extern ssize_t tpm_show_durations(struct device *,
73                                   struct device_attribute *attr, char *);
74 extern ssize_t tpm_show_timeouts(struct device *,
75                                  struct device_attribute *attr, char *);
76
77 struct tpm_chip;
78
79 struct tpm_vendor_specific {
80         const u8 req_complete_mask;
81         const u8 req_complete_val;
82         bool (*req_canceled)(struct tpm_chip *chip, u8 status);
83         void __iomem *iobase;           /* ioremapped address */
84         unsigned long base;             /* TPM base address */
85
86         int irq;
87         int probed_irq;
88
89         int region_size;
90         int have_region;
91
92         int (*recv) (struct tpm_chip *, u8 *, size_t);
93         int (*send) (struct tpm_chip *, u8 *, size_t);
94         void (*cancel) (struct tpm_chip *);
95         u8 (*status) (struct tpm_chip *);
96         void (*release) (struct device *);
97         struct miscdevice miscdev;
98         struct attribute_group *attr_group;
99         struct list_head list;
100         int locality;
101         unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
102         bool timeout_adjusted;
103         unsigned long duration[3]; /* jiffies */
104         bool duration_adjusted;
105         void *priv;
106
107         wait_queue_head_t read_queue;
108         wait_queue_head_t int_queue;
109
110         u16 manufacturer_id;
111 };
112
113 #define TPM_VPRIV(c)    (c)->vendor.priv
114
115 #define TPM_VID_INTEL    0x8086
116 #define TPM_VID_WINBOND  0x1050
117 #define TPM_VID_STM      0x104A
118
119 struct tpm_chip {
120         struct device *dev;     /* Device stuff */
121
122         int dev_num;            /* /dev/tpm# */
123         char devname[7];
124         unsigned long is_open;  /* only one allowed */
125         int time_expired;
126
127         /* Data passed to and from the tpm via the read/write calls */
128         u8 *data_buffer;
129         atomic_t data_pending;
130         struct mutex buffer_mutex;
131
132         struct timer_list user_read_timer;      /* user needs to claim result */
133         struct work_struct work;
134         struct mutex tpm_mutex; /* tpm is processing */
135
136         struct tpm_vendor_specific vendor;
137
138         struct dentry **bios_dir;
139
140         struct list_head list;
141         void (*release) (struct device *);
142 };
143
144 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
145
146 static inline void tpm_chip_put(struct tpm_chip *chip)
147 {
148         module_put(chip->dev->driver->owner);
149 }
150
151 static inline int tpm_read_index(int base, int index)
152 {
153         outb(index, base);
154         return inb(base+1) & 0xFF;
155 }
156
157 static inline void tpm_write_index(int base, int index, int value)
158 {
159         outb(index, base);
160         outb(value & 0xFF, base+1);
161 }
162 struct tpm_input_header {
163         __be16  tag;
164         __be32  length;
165         __be32  ordinal;
166 } __packed;
167
168 struct tpm_output_header {
169         __be16  tag;
170         __be32  length;
171         __be32  return_code;
172 } __packed;
173
174 struct  stclear_flags_t {
175         __be16  tag;
176         u8      deactivated;
177         u8      disableForceClear;
178         u8      physicalPresence;
179         u8      physicalPresenceLock;
180         u8      bGlobalLock;
181 } __packed;
182
183 struct  tpm_version_t {
184         u8      Major;
185         u8      Minor;
186         u8      revMajor;
187         u8      revMinor;
188 } __packed;
189
190 struct  tpm_version_1_2_t {
191         __be16  tag;
192         u8      Major;
193         u8      Minor;
194         u8      revMajor;
195         u8      revMinor;
196 } __packed;
197
198 struct  timeout_t {
199         __be32  a;
200         __be32  b;
201         __be32  c;
202         __be32  d;
203 } __packed;
204
205 struct duration_t {
206         __be32  tpm_short;
207         __be32  tpm_medium;
208         __be32  tpm_long;
209 } __packed;
210
211 struct permanent_flags_t {
212         __be16  tag;
213         u8      disable;
214         u8      ownership;
215         u8      deactivated;
216         u8      readPubek;
217         u8      disableOwnerClear;
218         u8      allowMaintenance;
219         u8      physicalPresenceLifetimeLock;
220         u8      physicalPresenceHWEnable;
221         u8      physicalPresenceCMDEnable;
222         u8      CEKPUsed;
223         u8      TPMpost;
224         u8      TPMpostLock;
225         u8      FIPS;
226         u8      operator;
227         u8      enableRevokeEK;
228         u8      nvLocked;
229         u8      readSRKPub;
230         u8      tpmEstablished;
231         u8      maintenanceDone;
232         u8      disableFullDALogicInfo;
233 } __packed;
234
235 typedef union {
236         struct  permanent_flags_t perm_flags;
237         struct  stclear_flags_t stclear_flags;
238         bool    owned;
239         __be32  num_pcrs;
240         struct  tpm_version_t   tpm_version;
241         struct  tpm_version_1_2_t tpm_version_1_2;
242         __be32  manufacturer_id;
243         struct timeout_t  timeout;
244         struct duration_t duration;
245 } cap_t;
246
247 struct  tpm_getcap_params_in {
248         __be32  cap;
249         __be32  subcap_size;
250         __be32  subcap;
251 } __packed;
252
253 struct  tpm_getcap_params_out {
254         __be32  cap_size;
255         cap_t   cap;
256 } __packed;
257
258 struct  tpm_readpubek_params_out {
259         u8      algorithm[4];
260         u8      encscheme[2];
261         u8      sigscheme[2];
262         __be32  paramsize;
263         u8      parameters[12]; /*assuming RSA*/
264         __be32  keysize;
265         u8      modulus[256];
266         u8      checksum[20];
267 } __packed;
268
269 typedef union {
270         struct  tpm_input_header in;
271         struct  tpm_output_header out;
272 } tpm_cmd_header;
273
274 struct tpm_pcrread_out {
275         u8      pcr_result[TPM_DIGEST_SIZE];
276 } __packed;
277
278 struct tpm_pcrread_in {
279         __be32  pcr_idx;
280 } __packed;
281
282 struct tpm_pcrextend_in {
283         __be32  pcr_idx;
284         u8      hash[TPM_DIGEST_SIZE];
285 } __packed;
286
287 /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
288  * bytes, but 128 is still a relatively large number of random bytes and
289  * anything much bigger causes users of struct tpm_cmd_t to start getting
290  * compiler warnings about stack frame size. */
291 #define TPM_MAX_RNG_DATA        128
292
293 struct tpm_getrandom_out {
294         __be32 rng_data_len;
295         u8     rng_data[TPM_MAX_RNG_DATA];
296 } __packed;
297
298 struct tpm_getrandom_in {
299         __be32 num_bytes;
300 } __packed;
301
302 struct tpm_startup_in {
303         __be16  startup_type;
304 } __packed;
305
306 typedef union {
307         struct  tpm_getcap_params_out getcap_out;
308         struct  tpm_readpubek_params_out readpubek_out;
309         u8      readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
310         struct  tpm_getcap_params_in getcap_in;
311         struct  tpm_pcrread_in  pcrread_in;
312         struct  tpm_pcrread_out pcrread_out;
313         struct  tpm_pcrextend_in pcrextend_in;
314         struct  tpm_getrandom_in getrandom_in;
315         struct  tpm_getrandom_out getrandom_out;
316         struct tpm_startup_in startup_in;
317 } tpm_cmd_params;
318
319 struct tpm_cmd_t {
320         tpm_cmd_header  header;
321         tpm_cmd_params  params;
322 } __packed;
323
324 ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
325
326 extern int tpm_get_timeouts(struct tpm_chip *);
327 extern void tpm_gen_interrupt(struct tpm_chip *);
328 extern int tpm_do_selftest(struct tpm_chip *);
329 extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
330 extern struct tpm_chip* tpm_register_hardware(struct device *,
331                                  const struct tpm_vendor_specific *);
332 extern int tpm_open(struct inode *, struct file *);
333 extern int tpm_release(struct inode *, struct file *);
334 extern void tpm_dev_release(struct device *dev);
335 extern void tpm_dev_vendor_release(struct tpm_chip *);
336 extern ssize_t tpm_write(struct file *, const char __user *, size_t,
337                          loff_t *);
338 extern ssize_t tpm_read(struct file *, char __user *, size_t, loff_t *);
339 extern void tpm_remove_hardware(struct device *);
340 extern int tpm_pm_suspend(struct device *);
341 extern int tpm_pm_resume(struct device *);
342 extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
343                              wait_queue_head_t *, bool);
344
345 #ifdef CONFIG_ACPI
346 extern int tpm_add_ppi(struct kobject *);
347 extern void tpm_remove_ppi(struct kobject *);
348 #else
349 static inline int tpm_add_ppi(struct kobject *parent)
350 {
351         return 0;
352 }
353
354 static inline void tpm_remove_ppi(struct kobject *parent)
355 {
356 }
357 #endif