TPM: Use vendor specific function for status probe
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / char / tpm / tpm.c
CommitLineData
1da177e4
LT
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 *
8e81cc13 10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
1da177e4
LT
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 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
700d8bdc 22 * calls to msleep.
1da177e4
LT
23 *
24 */
25
1da177e4 26#include <linux/poll.h>
5a0e3ad6 27#include <linux/slab.h>
d081d470 28#include <linux/mutex.h>
1da177e4 29#include <linux/spinlock.h>
d081d470 30
1da177e4
LT
31#include "tpm.h"
32
3122a88a
KH
33enum tpm_const {
34 TPM_MINOR = 224, /* officially assigned */
7f366784 35 TPM_BUFSIZE = 4096,
3122a88a 36 TPM_NUM_DEVICES = 256,
3122a88a 37};
1da177e4 38
9e18ee19
KJH
39enum tpm_duration {
40 TPM_SHORT = 0,
41 TPM_MEDIUM = 1,
42 TPM_LONG = 2,
43 TPM_UNDEFINED,
44};
45
46#define TPM_MAX_ORDINAL 243
47#define TPM_MAX_PROTECTED_ORDINAL 12
48#define TPM_PROTECTED_ORDINAL_MASK 0xFF
49
9b3056cc
DT
50/*
51 * Bug workaround - some TPM's don't flush the most
52 * recently changed pcr on suspend, so force the flush
53 * with an extend to the selected _unused_ non-volatile pcr.
54 */
55static int tpm_suspend_pcr;
56module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
57MODULE_PARM_DESC(suspend_pcr,
58 "PCR to use for dummy writes to faciltate flush on suspend.");
59
1da177e4
LT
60static LIST_HEAD(tpm_chip_list);
61static DEFINE_SPINLOCK(driver_lock);
10685a95 62static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
1da177e4 63
9e18ee19
KJH
64/*
65 * Array with one entry per ordinal defining the maximum amount
66 * of time the chip could take to return the result. The ordinal
67 * designation of short, medium or long is defined in a table in
68 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
69 * values of the SHORT, MEDIUM, and LONG durations are retrieved
70 * from the chip during initialization with a call to tpm_get_timeouts.
71 */
72static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
73 TPM_UNDEFINED, /* 0 */
74 TPM_UNDEFINED,
75 TPM_UNDEFINED,
76 TPM_UNDEFINED,
77 TPM_UNDEFINED,
78 TPM_UNDEFINED, /* 5 */
79 TPM_UNDEFINED,
80 TPM_UNDEFINED,
81 TPM_UNDEFINED,
82 TPM_UNDEFINED,
83 TPM_SHORT, /* 10 */
84 TPM_SHORT,
85};
86
87static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
88 TPM_UNDEFINED, /* 0 */
89 TPM_UNDEFINED,
90 TPM_UNDEFINED,
91 TPM_UNDEFINED,
92 TPM_UNDEFINED,
93 TPM_UNDEFINED, /* 5 */
94 TPM_UNDEFINED,
95 TPM_UNDEFINED,
96 TPM_UNDEFINED,
97 TPM_UNDEFINED,
98 TPM_SHORT, /* 10 */
99 TPM_SHORT,
100 TPM_MEDIUM,
101 TPM_LONG,
102 TPM_LONG,
103 TPM_MEDIUM, /* 15 */
104 TPM_SHORT,
105 TPM_SHORT,
106 TPM_MEDIUM,
107 TPM_LONG,
108 TPM_SHORT, /* 20 */
109 TPM_SHORT,
110 TPM_MEDIUM,
111 TPM_MEDIUM,
112 TPM_MEDIUM,
113 TPM_SHORT, /* 25 */
114 TPM_SHORT,
115 TPM_MEDIUM,
116 TPM_SHORT,
117 TPM_SHORT,
118 TPM_MEDIUM, /* 30 */
119 TPM_LONG,
120 TPM_MEDIUM,
121 TPM_SHORT,
122 TPM_SHORT,
123 TPM_SHORT, /* 35 */
124 TPM_MEDIUM,
125 TPM_MEDIUM,
126 TPM_UNDEFINED,
127 TPM_UNDEFINED,
128 TPM_MEDIUM, /* 40 */
129 TPM_LONG,
130 TPM_MEDIUM,
131 TPM_SHORT,
132 TPM_SHORT,
133 TPM_SHORT, /* 45 */
134 TPM_SHORT,
135 TPM_SHORT,
136 TPM_SHORT,
137 TPM_LONG,
138 TPM_MEDIUM, /* 50 */
139 TPM_MEDIUM,
140 TPM_UNDEFINED,
141 TPM_UNDEFINED,
142 TPM_UNDEFINED,
143 TPM_UNDEFINED, /* 55 */
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_MEDIUM, /* 60 */
149 TPM_MEDIUM,
150 TPM_MEDIUM,
151 TPM_SHORT,
152 TPM_SHORT,
153 TPM_MEDIUM, /* 65 */
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_SHORT, /* 70 */
159 TPM_SHORT,
160 TPM_UNDEFINED,
161 TPM_UNDEFINED,
162 TPM_UNDEFINED,
163 TPM_UNDEFINED, /* 75 */
164 TPM_UNDEFINED,
165 TPM_UNDEFINED,
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_LONG, /* 80 */
169 TPM_UNDEFINED,
170 TPM_MEDIUM,
171 TPM_LONG,
172 TPM_SHORT,
173 TPM_UNDEFINED, /* 85 */
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_UNDEFINED,
177 TPM_UNDEFINED,
178 TPM_SHORT, /* 90 */
179 TPM_SHORT,
180 TPM_SHORT,
181 TPM_SHORT,
182 TPM_SHORT,
183 TPM_UNDEFINED, /* 95 */
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_UNDEFINED,
187 TPM_UNDEFINED,
188 TPM_MEDIUM, /* 100 */
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_UNDEFINED,
192 TPM_UNDEFINED,
193 TPM_UNDEFINED, /* 105 */
194 TPM_UNDEFINED,
195 TPM_UNDEFINED,
196 TPM_UNDEFINED,
197 TPM_UNDEFINED,
198 TPM_SHORT, /* 110 */
199 TPM_SHORT,
200 TPM_SHORT,
201 TPM_SHORT,
202 TPM_SHORT,
203 TPM_SHORT, /* 115 */
204 TPM_SHORT,
205 TPM_SHORT,
206 TPM_UNDEFINED,
207 TPM_UNDEFINED,
208 TPM_LONG, /* 120 */
209 TPM_LONG,
210 TPM_MEDIUM,
211 TPM_UNDEFINED,
212 TPM_SHORT,
213 TPM_SHORT, /* 125 */
214 TPM_SHORT,
215 TPM_LONG,
216 TPM_SHORT,
217 TPM_SHORT,
218 TPM_SHORT, /* 130 */
219 TPM_MEDIUM,
220 TPM_UNDEFINED,
221 TPM_SHORT,
222 TPM_MEDIUM,
223 TPM_UNDEFINED, /* 135 */
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_SHORT, /* 140 */
229 TPM_SHORT,
230 TPM_UNDEFINED,
231 TPM_UNDEFINED,
232 TPM_UNDEFINED,
233 TPM_UNDEFINED, /* 145 */
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_SHORT, /* 150 */
239 TPM_MEDIUM,
240 TPM_MEDIUM,
241 TPM_SHORT,
242 TPM_SHORT,
243 TPM_UNDEFINED, /* 155 */
244 TPM_UNDEFINED,
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_SHORT, /* 160 */
249 TPM_SHORT,
250 TPM_SHORT,
251 TPM_SHORT,
252 TPM_UNDEFINED,
253 TPM_UNDEFINED, /* 165 */
254 TPM_UNDEFINED,
255 TPM_UNDEFINED,
256 TPM_UNDEFINED,
257 TPM_UNDEFINED,
258 TPM_LONG, /* 170 */
259 TPM_UNDEFINED,
260 TPM_UNDEFINED,
261 TPM_UNDEFINED,
262 TPM_UNDEFINED,
263 TPM_UNDEFINED, /* 175 */
264 TPM_UNDEFINED,
265 TPM_UNDEFINED,
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_MEDIUM, /* 180 */
269 TPM_SHORT,
270 TPM_MEDIUM,
271 TPM_MEDIUM,
272 TPM_MEDIUM,
273 TPM_MEDIUM, /* 185 */
274 TPM_SHORT,
275 TPM_UNDEFINED,
276 TPM_UNDEFINED,
277 TPM_UNDEFINED,
278 TPM_UNDEFINED, /* 190 */
279 TPM_UNDEFINED,
280 TPM_UNDEFINED,
281 TPM_UNDEFINED,
282 TPM_UNDEFINED,
283 TPM_UNDEFINED, /* 195 */
284 TPM_UNDEFINED,
285 TPM_UNDEFINED,
286 TPM_UNDEFINED,
287 TPM_UNDEFINED,
288 TPM_SHORT, /* 200 */
289 TPM_UNDEFINED,
290 TPM_UNDEFINED,
291 TPM_UNDEFINED,
292 TPM_SHORT,
293 TPM_SHORT, /* 205 */
294 TPM_SHORT,
295 TPM_SHORT,
296 TPM_SHORT,
297 TPM_SHORT,
298 TPM_MEDIUM, /* 210 */
299 TPM_UNDEFINED,
300 TPM_MEDIUM,
301 TPM_MEDIUM,
302 TPM_MEDIUM,
303 TPM_UNDEFINED, /* 215 */
304 TPM_MEDIUM,
305 TPM_UNDEFINED,
306 TPM_UNDEFINED,
307 TPM_SHORT,
308 TPM_SHORT, /* 220 */
309 TPM_SHORT,
310 TPM_SHORT,
311 TPM_SHORT,
312 TPM_SHORT,
313 TPM_UNDEFINED, /* 225 */
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_UNDEFINED,
318 TPM_SHORT, /* 230 */
319 TPM_LONG,
320 TPM_MEDIUM,
321 TPM_UNDEFINED,
322 TPM_UNDEFINED,
323 TPM_UNDEFINED, /* 235 */
324 TPM_UNDEFINED,
325 TPM_UNDEFINED,
326 TPM_UNDEFINED,
327 TPM_UNDEFINED,
328 TPM_SHORT, /* 240 */
329 TPM_UNDEFINED,
330 TPM_MEDIUM,
331};
332
1da177e4
LT
333static void user_reader_timeout(unsigned long ptr)
334{
335 struct tpm_chip *chip = (struct tpm_chip *) ptr;
336
09e12f9f
KJH
337 schedule_work(&chip->work);
338}
339
c4028958 340static void timeout_work(struct work_struct *work)
09e12f9f 341{
c4028958 342 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
09e12f9f 343
d081d470 344 mutex_lock(&chip->buffer_mutex);
1da177e4
LT
345 atomic_set(&chip->data_pending, 0);
346 memset(chip->data_buffer, 0, TPM_BUFSIZE);
d081d470 347 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
348}
349
9e18ee19
KJH
350/*
351 * Returns max number of jiffies to wait
352 */
353unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
354 u32 ordinal)
355{
356 int duration_idx = TPM_UNDEFINED;
357 int duration = 0;
358
359 if (ordinal < TPM_MAX_ORDINAL)
360 duration_idx = tpm_ordinal_duration[ordinal];
361 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
362 TPM_MAX_PROTECTED_ORDINAL)
363 duration_idx =
364 tpm_protected_ordinal_duration[ordinal &
365 TPM_PROTECTED_ORDINAL_MASK];
366
8d1dc20e 367 if (duration_idx != TPM_UNDEFINED)
36b20020 368 duration = chip->vendor.duration[duration_idx];
8d1dc20e 369 if (duration <= 0)
9e18ee19 370 return 2 * 60 * HZ;
8d1dc20e
LT
371 else
372 return duration;
9e18ee19
KJH
373}
374EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
375
1da177e4
LT
376/*
377 * Internal kernel interface to transmit TPM commands
378 */
379static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
380 size_t bufsiz)
381{
d9e5b6bf 382 ssize_t rc;
9e18ee19 383 u32 count, ordinal;
700d8bdc 384 unsigned long stop;
1da177e4 385
81179bb6 386 count = be32_to_cpu(*((__be32 *) (buf + 2)));
9e18ee19 387 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
1da177e4
LT
388 if (count == 0)
389 return -ENODATA;
390 if (count > bufsiz) {
e659a3fe 391 dev_err(chip->dev,
b76be681 392 "invalid count value %x %zx \n", count, bufsiz);
1da177e4
LT
393 return -E2BIG;
394 }
395
d081d470 396 mutex_lock(&chip->tpm_mutex);
1da177e4 397
90dda520 398 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
e659a3fe 399 dev_err(chip->dev,
d9e5b6bf
KH
400 "tpm_transmit: tpm_send: error %zd\n", rc);
401 goto out;
1da177e4
LT
402 }
403
27084efe
LD
404 if (chip->vendor.irq)
405 goto out_recv;
406
9e18ee19 407 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
1da177e4 408 do {
90dda520
KJH
409 u8 status = chip->vendor.status(chip);
410 if ((status & chip->vendor.req_complete_mask) ==
411 chip->vendor.req_complete_val)
1da177e4 412 goto out_recv;
d9e5b6bf 413
90dda520 414 if ((status == chip->vendor.req_canceled)) {
e659a3fe 415 dev_err(chip->dev, "Operation Canceled\n");
d9e5b6bf
KH
416 rc = -ECANCELED;
417 goto out;
418 }
419
420 msleep(TPM_TIMEOUT); /* CHECK */
1da177e4 421 rmb();
700d8bdc 422 } while (time_before(jiffies, stop));
1da177e4 423
90dda520 424 chip->vendor.cancel(chip);
e659a3fe 425 dev_err(chip->dev, "Operation Timed out\n");
d9e5b6bf
KH
426 rc = -ETIME;
427 goto out;
1da177e4
LT
428
429out_recv:
90dda520 430 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
d9e5b6bf 431 if (rc < 0)
e659a3fe 432 dev_err(chip->dev,
d9e5b6bf
KH
433 "tpm_transmit: tpm_recv: error %zd\n", rc);
434out:
d081d470 435 mutex_unlock(&chip->tpm_mutex);
d9e5b6bf 436 return rc;
1da177e4
LT
437}
438
439#define TPM_DIGEST_SIZE 20
beed53a1
KJH
440#define TPM_ERROR_SIZE 10
441#define TPM_RET_CODE_IDX 6
beed53a1
KJH
442
443enum tpm_capabilities {
08837438
RA
444 TPM_CAP_FLAG = cpu_to_be32(4),
445 TPM_CAP_PROP = cpu_to_be32(5),
446 CAP_VERSION_1_1 = cpu_to_be32(0x06),
447 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
beed53a1
KJH
448};
449
450enum tpm_sub_capabilities {
08837438
RA
451 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
452 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
453 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
454 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
455 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
456 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
457 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
beed53a1 458
1da177e4
LT
459};
460
08837438
RA
461static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
462 int len, const char *desc)
beed53a1
KJH
463{
464 int err;
465
08837438 466 len = tpm_transmit(chip,(u8 *) cmd, len);
beed53a1
KJH
467 if (len < 0)
468 return len;
469 if (len == TPM_ERROR_SIZE) {
08837438 470 err = be32_to_cpu(cmd->header.out.return_code);
beed53a1
KJH
471 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
472 return err;
473 }
474 return 0;
475}
476
08837438
RA
477#define TPM_INTERNAL_RESULT_SIZE 200
478#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
479#define TPM_ORD_GET_CAP cpu_to_be32(101)
480
481static const struct tpm_input_header tpm_getcap_header = {
482 .tag = TPM_TAG_RQU_COMMAND,
483 .length = cpu_to_be32(22),
484 .ordinal = TPM_ORD_GET_CAP
485};
486
487ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
488 const char *desc)
489{
490 struct tpm_cmd_t tpm_cmd;
491 int rc;
492 struct tpm_chip *chip = dev_get_drvdata(dev);
493
494 tpm_cmd.header.in = tpm_getcap_header;
495 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
496 tpm_cmd.params.getcap_in.cap = subcap_id;
497 /*subcap field not necessary */
498 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
499 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
500 } else {
501 if (subcap_id == TPM_CAP_FLAG_PERM ||
502 subcap_id == TPM_CAP_FLAG_VOL)
503 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
504 else
505 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
506 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
507 tpm_cmd.params.getcap_in.subcap = subcap_id;
508 }
509 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
510 if (!rc)
511 *cap = tpm_cmd.params.getcap_out.cap;
512 return rc;
513}
514
08e96e48
KJH
515void tpm_gen_interrupt(struct tpm_chip *chip)
516{
08837438 517 struct tpm_cmd_t tpm_cmd;
08e96e48
KJH
518 ssize_t rc;
519
08837438
RA
520 tpm_cmd.header.in = tpm_getcap_header;
521 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
522 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
523 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
08e96e48 524
08837438 525 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
526 "attempting to determine the timeouts");
527}
528EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
529
2b30a90f 530int tpm_get_timeouts(struct tpm_chip *chip)
08e96e48 531{
08837438
RA
532 struct tpm_cmd_t tpm_cmd;
533 struct timeout_t *timeout_cap;
534 struct duration_t *duration_cap;
08e96e48
KJH
535 ssize_t rc;
536 u32 timeout;
e3e1a1e1 537 unsigned int scale = 1;
08e96e48 538
08837438
RA
539 tpm_cmd.header.in = tpm_getcap_header;
540 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
541 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
542 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
08e96e48 543
08837438 544 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
545 "attempting to determine the timeouts");
546 if (rc)
547 goto duration;
548
829bf067
SB
549 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
550 be32_to_cpu(tpm_cmd.header.out.length)
551 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
2b30a90f 552 return -EINVAL;
08e96e48 553
08837438 554 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
08e96e48 555 /* Don't overwrite default if value is 0 */
08837438 556 timeout = be32_to_cpu(timeout_cap->a);
e3e1a1e1
SB
557 if (timeout && timeout < 1000) {
558 /* timeouts in msec rather usec */
559 scale = 1000;
62592101 560 chip->vendor.timeout_adjusted = true;
e3e1a1e1 561 }
08e96e48 562 if (timeout)
e3e1a1e1 563 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
08837438 564 timeout = be32_to_cpu(timeout_cap->b);
08e96e48 565 if (timeout)
e3e1a1e1 566 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
08837438 567 timeout = be32_to_cpu(timeout_cap->c);
08e96e48 568 if (timeout)
e3e1a1e1 569 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
08837438 570 timeout = be32_to_cpu(timeout_cap->d);
08e96e48 571 if (timeout)
e3e1a1e1 572 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
08e96e48
KJH
573
574duration:
08837438
RA
575 tpm_cmd.header.in = tpm_getcap_header;
576 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
577 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
578 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
08e96e48 579
08837438 580 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
581 "attempting to determine the durations");
582 if (rc)
2b30a90f 583 return rc;
08e96e48 584
979b1406
SB
585 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
586 be32_to_cpu(tpm_cmd.header.out.length)
587 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
2b30a90f 588 return -EINVAL;
979b1406 589
08837438 590 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
08e96e48 591 chip->vendor.duration[TPM_SHORT] =
08837438 592 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
e934acca
SB
593 chip->vendor.duration[TPM_MEDIUM] =
594 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
595 chip->vendor.duration[TPM_LONG] =
596 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
597
292cf4a8
VK
598 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
599 * value wrong and apparently reports msecs rather than usecs. So we
600 * fix up the resulting too-small TPM_SHORT value to make things work.
e934acca 601 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
292cf4a8 602 */
e934acca 603 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
292cf4a8 604 chip->vendor.duration[TPM_SHORT] = HZ;
e934acca
SB
605 chip->vendor.duration[TPM_MEDIUM] *= 1000;
606 chip->vendor.duration[TPM_LONG] *= 1000;
04ab2293 607 chip->vendor.duration_adjusted = true;
e934acca
SB
608 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
609 }
2b30a90f 610 return 0;
08e96e48
KJH
611}
612EXPORT_SYMBOL_GPL(tpm_get_timeouts);
613
d97c6ade
SB
614#define TPM_ORD_CONTINUE_SELFTEST 83
615#define CONTINUE_SELFTEST_RESULT_SIZE 10
616
617static struct tpm_input_header continue_selftest_header = {
618 .tag = TPM_TAG_RQU_COMMAND,
619 .length = cpu_to_be32(10),
620 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
621};
622
623/**
624 * tpm_continue_selftest -- run TPM's selftest
625 * @chip: TPM chip to use
626 *
627 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
628 * a TPM error code.
629 */
68d6e671 630static int tpm_continue_selftest(struct tpm_chip *chip)
08e96e48 631{
d97c6ade
SB
632 int rc;
633 struct tpm_cmd_t cmd;
08e96e48 634
d97c6ade
SB
635 cmd.header.in = continue_selftest_header;
636 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
637 "continue selftest");
638 return rc;
08e96e48 639}
08e96e48
KJH
640
641ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
642 char *buf)
643{
08837438 644 cap_t cap;
08e96e48
KJH
645 ssize_t rc;
646
08837438
RA
647 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
648 "attempting to determine the permanent enabled state");
649 if (rc)
08e96e48 650 return 0;
ec288bd3 651
08837438 652 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
ec288bd3 653 return rc;
08e96e48
KJH
654}
655EXPORT_SYMBOL_GPL(tpm_show_enabled);
656
657ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
658 char *buf)
659{
08837438 660 cap_t cap;
08e96e48
KJH
661 ssize_t rc;
662
08837438
RA
663 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
664 "attempting to determine the permanent active state");
665 if (rc)
08e96e48 666 return 0;
ec288bd3 667
08837438 668 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
ec288bd3 669 return rc;
08e96e48
KJH
670}
671EXPORT_SYMBOL_GPL(tpm_show_active);
672
673ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
674 char *buf)
675{
08837438 676 cap_t cap;
08e96e48
KJH
677 ssize_t rc;
678
08837438
RA
679 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
680 "attempting to determine the owner state");
681 if (rc)
08e96e48 682 return 0;
ec288bd3 683
08837438 684 rc = sprintf(buf, "%d\n", cap.owned);
ec288bd3 685 return rc;
08e96e48
KJH
686}
687EXPORT_SYMBOL_GPL(tpm_show_owned);
688
689ssize_t tpm_show_temp_deactivated(struct device * dev,
690 struct device_attribute * attr, char *buf)
691{
08837438 692 cap_t cap;
08e96e48
KJH
693 ssize_t rc;
694
08837438
RA
695 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
696 "attempting to determine the temporary state");
697 if (rc)
08e96e48 698 return 0;
ec288bd3 699
08837438 700 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
ec288bd3 701 return rc;
08e96e48
KJH
702}
703EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
704
659aaf2b
RA
705/*
706 * tpm_chip_find_get - return tpm_chip for given chip number
707 */
708static struct tpm_chip *tpm_chip_find_get(int chip_num)
709{
8920d5ad 710 struct tpm_chip *pos, *chip = NULL;
659aaf2b
RA
711
712 rcu_read_lock();
713 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
714 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
715 continue;
716
8920d5ad
RA
717 if (try_module_get(pos->dev->driver->owner)) {
718 chip = pos;
659aaf2b 719 break;
8920d5ad 720 }
659aaf2b
RA
721 }
722 rcu_read_unlock();
8920d5ad 723 return chip;
659aaf2b
RA
724}
725
726#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
727#define READ_PCR_RESULT_SIZE 30
728static struct tpm_input_header pcrread_header = {
729 .tag = TPM_TAG_RQU_COMMAND,
730 .length = cpu_to_be32(14),
731 .ordinal = TPM_ORDINAL_PCRREAD
1da177e4
LT
732};
733
68d6e671 734static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
659aaf2b
RA
735{
736 int rc;
737 struct tpm_cmd_t cmd;
738
739 cmd.header.in = pcrread_header;
740 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
23acb98d 741 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
659aaf2b
RA
742 "attempting to read a pcr value");
743
744 if (rc == 0)
745 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
746 TPM_DIGEST_SIZE);
747 return rc;
748}
749
750/**
751 * tpm_pcr_read - read a pcr value
752 * @chip_num: tpm idx # or ANY
753 * @pcr_idx: pcr idx to retrieve
754 * @res_buf: TPM_PCR value
755 * size of res_buf is 20 bytes (or NULL if you don't care)
756 *
757 * The TPM driver should be built-in, but for whatever reason it
758 * isn't, protect against the chip disappearing, by incrementing
759 * the module usage count.
760 */
761int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
762{
763 struct tpm_chip *chip;
764 int rc;
765
766 chip = tpm_chip_find_get(chip_num);
767 if (chip == NULL)
768 return -ENODEV;
769 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
a0e39349 770 tpm_chip_put(chip);
659aaf2b
RA
771 return rc;
772}
773EXPORT_SYMBOL_GPL(tpm_pcr_read);
774
775/**
776 * tpm_pcr_extend - extend pcr value with hash
777 * @chip_num: tpm idx # or AN&
778 * @pcr_idx: pcr idx to extend
779 * @hash: hash value used to extend pcr value
780 *
781 * The TPM driver should be built-in, but for whatever reason it
782 * isn't, protect against the chip disappearing, by incrementing
783 * the module usage count.
784 */
785#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
0afd9056 786#define EXTEND_PCR_RESULT_SIZE 34
659aaf2b
RA
787static struct tpm_input_header pcrextend_header = {
788 .tag = TPM_TAG_RQU_COMMAND,
789 .length = cpu_to_be32(34),
790 .ordinal = TPM_ORD_PCR_EXTEND
791};
792
793int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
794{
795 struct tpm_cmd_t cmd;
796 int rc;
797 struct tpm_chip *chip;
798
799 chip = tpm_chip_find_get(chip_num);
800 if (chip == NULL)
801 return -ENODEV;
802
803 cmd.header.in = pcrextend_header;
659aaf2b
RA
804 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
805 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
0afd9056 806 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
659aaf2b
RA
807 "attempting extend a PCR value");
808
a0e39349 809 tpm_chip_put(chip);
659aaf2b
RA
810 return rc;
811}
812EXPORT_SYMBOL_GPL(tpm_pcr_extend);
813
68d6e671
SB
814/**
815 * tpm_do_selftest - have the TPM continue its selftest and wait until it
816 * can receive further commands
817 * @chip: TPM chip to use
818 *
819 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
820 * a TPM error code.
821 */
822int tpm_do_selftest(struct tpm_chip *chip)
823{
824 int rc;
825 u8 digest[TPM_DIGEST_SIZE];
826 unsigned int loops;
827 unsigned int delay_msec = 1000;
828 unsigned long duration;
829
830 duration = tpm_calc_ordinal_duration(chip,
831 TPM_ORD_CONTINUE_SELFTEST);
832
833 loops = jiffies_to_msecs(duration) / delay_msec;
834
835 rc = tpm_continue_selftest(chip);
836 /* This may fail if there was no TPM driver during a suspend/resume
837 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
838 */
839 if (rc)
840 return rc;
841
842 do {
843 rc = __tpm_pcr_read(chip, 0, digest);
844 if (rc != TPM_WARN_DOING_SELFTEST)
845 return rc;
846 msleep(delay_msec);
847 } while (--loops > 0);
848
849 return rc;
850}
851EXPORT_SYMBOL_GPL(tpm_do_selftest);
852
c749ba91
MZ
853int tpm_send(u32 chip_num, void *cmd, size_t buflen)
854{
855 struct tpm_chip *chip;
856 int rc;
857
858 chip = tpm_chip_find_get(chip_num);
859 if (chip == NULL)
860 return -ENODEV;
861
862 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
863
864 tpm_chip_put(chip);
865 return rc;
866}
867EXPORT_SYMBOL_GPL(tpm_send);
868
6659ca2a
KH
869ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
870 char *buf)
1da177e4 871{
08837438 872 cap_t cap;
659aaf2b 873 u8 digest[TPM_DIGEST_SIZE];
beed53a1 874 ssize_t rc;
81179bb6 875 int i, j, num_pcrs;
1da177e4 876 char *str = buf;
e659a3fe 877 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4 878
08837438 879 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
beed53a1 880 "attempting to determine the number of PCRS");
08837438 881 if (rc)
e234bc97 882 return 0;
1da177e4 883
08837438 884 num_pcrs = be32_to_cpu(cap.num_pcrs);
1da177e4 885 for (i = 0; i < num_pcrs; i++) {
659aaf2b 886 rc = __tpm_pcr_read(chip, i, digest);
beed53a1 887 if (rc)
08837438 888 break;
1da177e4
LT
889 str += sprintf(str, "PCR-%02d: ", i);
890 for (j = 0; j < TPM_DIGEST_SIZE; j++)
659aaf2b 891 str += sprintf(str, "%02X ", digest[j]);
1da177e4
LT
892 str += sprintf(str, "\n");
893 }
894 return str - buf;
895}
6659ca2a 896EXPORT_SYMBOL_GPL(tpm_show_pcrs);
1da177e4
LT
897
898#define READ_PUBEK_RESULT_SIZE 314
08837438
RA
899#define TPM_ORD_READPUBEK cpu_to_be32(124)
900struct tpm_input_header tpm_readpubek_header = {
901 .tag = TPM_TAG_RQU_COMMAND,
902 .length = cpu_to_be32(30),
903 .ordinal = TPM_ORD_READPUBEK
1da177e4
LT
904};
905
6659ca2a
KH
906ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
907 char *buf)
1da177e4 908{
2df7111f 909 u8 *data;
08837438 910 struct tpm_cmd_t tpm_cmd;
beed53a1 911 ssize_t err;
81179bb6 912 int i, rc;
1da177e4
LT
913 char *str = buf;
914
e659a3fe 915 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4 916
08837438
RA
917 tpm_cmd.header.in = tpm_readpubek_header;
918 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
beed53a1
KJH
919 "attempting to read the PUBEK");
920 if (err)
34d6e075 921 goto out;
1da177e4
LT
922
923 /*
924 ignore header 10 bytes
925 algorithm 32 bits (1 == RSA )
926 encscheme 16 bits
927 sigscheme 16 bits
928 parameters (RSA 12->bytes: keybit, #primes, expbit)
929 keylenbytes 32 bits
930 256 byte modulus
931 ignore checksum 20 bytes
932 */
08837438 933 data = tpm_cmd.params.readpubek_out_buffer;
1da177e4
LT
934 str +=
935 sprintf(str,
5a79444f
SB
936 "Algorithm: %02X %02X %02X %02X\n"
937 "Encscheme: %02X %02X\n"
938 "Sigscheme: %02X %02X\n"
939 "Parameters: %02X %02X %02X %02X "
940 "%02X %02X %02X %02X "
941 "%02X %02X %02X %02X\n"
942 "Modulus length: %d\n"
943 "Modulus:\n",
944 data[0], data[1], data[2], data[3],
945 data[4], data[5],
946 data[6], data[7],
947 data[12], data[13], data[14], data[15],
948 data[16], data[17], data[18], data[19],
949 data[20], data[21], data[22], data[23],
950 be32_to_cpu(*((__be32 *) (data + 24))));
1da177e4
LT
951
952 for (i = 0; i < 256; i++) {
5a79444f 953 str += sprintf(str, "%02X ", data[i + 28]);
1da177e4
LT
954 if ((i + 1) % 16 == 0)
955 str += sprintf(str, "\n");
956 }
34d6e075 957out:
beed53a1 958 rc = str - buf;
2df7111f 959 return rc;
1da177e4 960}
6659ca2a 961EXPORT_SYMBOL_GPL(tpm_show_pubek);
1da177e4 962
1da177e4 963
6659ca2a
KH
964ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
965 char *buf)
1da177e4 966{
08837438 967 cap_t cap;
beed53a1 968 ssize_t rc;
1da177e4
LT
969 char *str = buf;
970
08837438 971 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
beed53a1 972 "attempting to determine the manufacturer");
08837438 973 if (rc)
beed53a1 974 return 0;
1da177e4 975 str += sprintf(str, "Manufacturer: 0x%x\n",
08837438 976 be32_to_cpu(cap.manufacturer_id));
1da177e4 977
08837438
RA
978 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
979 "attempting to determine the 1.1 version");
beed53a1 980 if (rc)
08837438 981 return 0;
beed53a1
KJH
982 str += sprintf(str,
983 "TCG version: %d.%d\nFirmware version: %d.%d\n",
08837438
RA
984 cap.tpm_version.Major, cap.tpm_version.Minor,
985 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1da177e4
LT
986 return str - buf;
987}
6659ca2a
KH
988EXPORT_SYMBOL_GPL(tpm_show_caps);
989
08e96e48
KJH
990ssize_t tpm_show_caps_1_2(struct device * dev,
991 struct device_attribute * attr, char *buf)
992{
08837438
RA
993 cap_t cap;
994 ssize_t rc;
08e96e48
KJH
995 char *str = buf;
996
08837438
RA
997 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
998 "attempting to determine the manufacturer");
999 if (rc)
08e96e48 1000 return 0;
08e96e48 1001 str += sprintf(str, "Manufacturer: 0x%x\n",
08837438
RA
1002 be32_to_cpu(cap.manufacturer_id));
1003 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1004 "attempting to determine the 1.2 version");
1005 if (rc)
1006 return 0;
08e96e48
KJH
1007 str += sprintf(str,
1008 "TCG version: %d.%d\nFirmware version: %d.%d\n",
08837438
RA
1009 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1010 cap.tpm_version_1_2.revMajor,
1011 cap.tpm_version_1_2.revMinor);
08e96e48
KJH
1012 return str - buf;
1013}
1014EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1015
04ab2293
SB
1016ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1017 char *buf)
1018{
1019 struct tpm_chip *chip = dev_get_drvdata(dev);
1020
403d1d03
SB
1021 if (chip->vendor.duration[TPM_LONG] == 0)
1022 return 0;
1023
04ab2293
SB
1024 return sprintf(buf, "%d %d %d [%s]\n",
1025 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1026 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1027 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1028 chip->vendor.duration_adjusted
1029 ? "adjusted" : "original");
1030}
1031EXPORT_SYMBOL_GPL(tpm_show_durations);
1032
62592101
SB
1033ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1034 char *buf)
1035{
1036 struct tpm_chip *chip = dev_get_drvdata(dev);
1037
1038 return sprintf(buf, "%d %d %d %d [%s]\n",
1039 jiffies_to_usecs(chip->vendor.timeout_a),
1040 jiffies_to_usecs(chip->vendor.timeout_b),
1041 jiffies_to_usecs(chip->vendor.timeout_c),
1042 jiffies_to_usecs(chip->vendor.timeout_d),
1043 chip->vendor.timeout_adjusted
1044 ? "adjusted" : "original");
1045}
1046EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1047
6659ca2a
KH
1048ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1049 const char *buf, size_t count)
1050{
1051 struct tpm_chip *chip = dev_get_drvdata(dev);
1052 if (chip == NULL)
1053 return 0;
1054
90dda520 1055 chip->vendor.cancel(chip);
6659ca2a
KH
1056 return count;
1057}
1058EXPORT_SYMBOL_GPL(tpm_store_cancel);
1da177e4 1059
1da177e4
LT
1060/*
1061 * Device file system interface to the TPM
f89c5edb
RA
1062 *
1063 * It's assured that the chip will be opened just once,
1064 * by the check of is_open variable, which is protected
1065 * by driver_lock.
1da177e4
LT
1066 */
1067int tpm_open(struct inode *inode, struct file *file)
1068{
f02a9364 1069 int minor = iminor(inode);
1da177e4
LT
1070 struct tpm_chip *chip = NULL, *pos;
1071
f02a9364
RA
1072 rcu_read_lock();
1073 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
90dda520 1074 if (pos->vendor.miscdev.minor == minor) {
1da177e4 1075 chip = pos;
f02a9364 1076 get_device(chip->dev);
1da177e4
LT
1077 break;
1078 }
1079 }
f02a9364 1080 rcu_read_unlock();
1da177e4 1081
f02a9364
RA
1082 if (!chip)
1083 return -ENODEV;
1da177e4 1084
dc36d32c 1085 if (test_and_set_bit(0, &chip->is_open)) {
b888c87b 1086 dev_dbg(chip->dev, "Another process owns this TPM\n");
f02a9364
RA
1087 put_device(chip->dev);
1088 return -EBUSY;
1da177e4
LT
1089 }
1090
1309d7af 1091 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1da177e4 1092 if (chip->data_buffer == NULL) {
dc36d32c 1093 clear_bit(0, &chip->is_open);
e659a3fe 1094 put_device(chip->dev);
1da177e4
LT
1095 return -ENOMEM;
1096 }
1097
1098 atomic_set(&chip->data_pending, 0);
1099
1100 file->private_data = chip;
1101 return 0;
1da177e4 1102}
1da177e4
LT
1103EXPORT_SYMBOL_GPL(tpm_open);
1104
f02a9364
RA
1105/*
1106 * Called on file close
1107 */
1da177e4
LT
1108int tpm_release(struct inode *inode, struct file *file)
1109{
1110 struct tpm_chip *chip = file->private_data;
1da177e4 1111
4bdec11f 1112 del_singleshot_timer_sync(&chip->user_read_timer);
2e5c44c9 1113 flush_work_sync(&chip->work);
5e976d55 1114 file->private_data = NULL;
1da177e4 1115 atomic_set(&chip->data_pending, 0);
f02a9364 1116 kfree(chip->data_buffer);
dc36d32c 1117 clear_bit(0, &chip->is_open);
e659a3fe 1118 put_device(chip->dev);
1da177e4
LT
1119 return 0;
1120}
1da177e4
LT
1121EXPORT_SYMBOL_GPL(tpm_release);
1122
b888c87b 1123ssize_t tpm_write(struct file *file, const char __user *buf,
3c2f606a 1124 size_t size, loff_t *off)
1da177e4
LT
1125{
1126 struct tpm_chip *chip = file->private_data;
01476001 1127 size_t in_size = size, out_size;
1da177e4
LT
1128
1129 /* cannot perform a write until the read has cleared
1130 either via tpm_read or a user_read_timer timeout */
700d8bdc
NA
1131 while (atomic_read(&chip->data_pending) != 0)
1132 msleep(TPM_TIMEOUT);
1da177e4 1133
d081d470 1134 mutex_lock(&chip->buffer_mutex);
1da177e4
LT
1135
1136 if (in_size > TPM_BUFSIZE)
1137 in_size = TPM_BUFSIZE;
1138
1139 if (copy_from_user
1140 (chip->data_buffer, (void __user *) buf, in_size)) {
d081d470 1141 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1142 return -EFAULT;
1143 }
1144
1145 /* atomic tpm command send and result receive */
1146 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1147
1148 atomic_set(&chip->data_pending, out_size);
d081d470 1149 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1150
1151 /* Set a timeout by which the reader must come claim the result */
fe3fd483 1152 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1da177e4
LT
1153
1154 return in_size;
1155}
1da177e4
LT
1156EXPORT_SYMBOL_GPL(tpm_write);
1157
3c2f606a
KJH
1158ssize_t tpm_read(struct file *file, char __user *buf,
1159 size_t size, loff_t *off)
1da177e4
LT
1160{
1161 struct tpm_chip *chip = file->private_data;
01476001 1162 ssize_t ret_size;
1da177e4 1163
5b44bd58 1164 del_singleshot_timer_sync(&chip->user_read_timer);
2e5c44c9 1165 flush_work_sync(&chip->work);
5b44bd58
KH
1166 ret_size = atomic_read(&chip->data_pending);
1167 atomic_set(&chip->data_pending, 0);
1168 if (ret_size > 0) { /* relay data */
1169 if (size < ret_size)
1170 ret_size = size;
1da177e4 1171
d081d470 1172 mutex_lock(&chip->buffer_mutex);
f6a2382c 1173 if (copy_to_user(buf, chip->data_buffer, ret_size))
5b44bd58 1174 ret_size = -EFAULT;
d081d470 1175 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1176 }
1177
1178 return ret_size;
1179}
1da177e4
LT
1180EXPORT_SYMBOL_GPL(tpm_read);
1181
e659a3fe 1182void tpm_remove_hardware(struct device *dev)
1da177e4 1183{
e659a3fe 1184 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4
LT
1185
1186 if (chip == NULL) {
e659a3fe 1187 dev_err(dev, "No device data found\n");
1da177e4
LT
1188 return;
1189 }
1190
1191 spin_lock(&driver_lock);
f02a9364 1192 list_del_rcu(&chip->list);
1da177e4 1193 spin_unlock(&driver_lock);
f02a9364 1194 synchronize_rcu();
1da177e4 1195
90dda520 1196 misc_deregister(&chip->vendor.miscdev);
90dda520 1197 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
55a82ab3 1198 tpm_bios_log_teardown(chip->bios_dir);
1da177e4 1199
5bd91f18
RM
1200 /* write it this way to be explicit (chip->dev == dev) */
1201 put_device(chip->dev);
1da177e4 1202}
e659a3fe 1203EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1da177e4 1204
225a9be2
RA
1205#define TPM_ORD_SAVESTATE cpu_to_be32(152)
1206#define SAVESTATE_RESULT_SIZE 10
1207
1208static struct tpm_input_header savestate_header = {
1209 .tag = TPM_TAG_RQU_COMMAND,
1210 .length = cpu_to_be32(10),
1211 .ordinal = TPM_ORD_SAVESTATE
1212};
1213
1da177e4
LT
1214/*
1215 * We are about to suspend. Save the TPM state
1216 * so that it can be restored.
1217 */
ce2c87d4 1218int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1da177e4 1219{
ce2c87d4 1220 struct tpm_chip *chip = dev_get_drvdata(dev);
225a9be2
RA
1221 struct tpm_cmd_t cmd;
1222 int rc;
1223
1224 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
2490c681 1225
1da177e4
LT
1226 if (chip == NULL)
1227 return -ENODEV;
1228
225a9be2
RA
1229 /* for buggy tpm, flush pcrs with extend to selected dummy */
1230 if (tpm_suspend_pcr) {
1231 cmd.header.in = pcrextend_header;
1232 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1233 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1234 TPM_DIGEST_SIZE);
1235 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1236 "extending dummy pcr before suspend");
1237 }
1238
1239 /* now do the actual savestate */
1240 cmd.header.in = savestate_header;
1241 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1242 "sending savestate before suspend");
1243 return rc;
1da177e4 1244}
1da177e4
LT
1245EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1246
1247/*
1248 * Resume from a power safe. The BIOS already restored
1249 * the TPM state.
1250 */
ce2c87d4 1251int tpm_pm_resume(struct device *dev)
1da177e4 1252{
ce2c87d4 1253 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4
LT
1254
1255 if (chip == NULL)
1256 return -ENODEV;
1257
1da177e4
LT
1258 return 0;
1259}
1da177e4
LT
1260EXPORT_SYMBOL_GPL(tpm_pm_resume);
1261
253115b7
RA
1262/* In case vendor provided release function, call it too.*/
1263
1264void tpm_dev_vendor_release(struct tpm_chip *chip)
1265{
1266 if (chip->vendor.release)
1267 chip->vendor.release(chip->dev);
1268
1269 clear_bit(chip->dev_num, dev_mask);
1270 kfree(chip->vendor.miscdev.name);
1271}
1272EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1273
1274
5bd91f18
RM
1275/*
1276 * Once all references to platform device are down to 0,
1277 * release all allocated structures.
5bd91f18 1278 */
cbb2ed4a 1279void tpm_dev_release(struct device *dev)
5bd91f18
RM
1280{
1281 struct tpm_chip *chip = dev_get_drvdata(dev);
1282
253115b7 1283 tpm_dev_vendor_release(chip);
5bd91f18 1284
253115b7 1285 chip->release(dev);
5bd91f18
RM
1286 kfree(chip);
1287}
253115b7 1288EXPORT_SYMBOL_GPL(tpm_dev_release);
5bd91f18 1289
1da177e4
LT
1290/*
1291 * Called from tpm_<specific>.c probe function only for devices
1292 * the driver has determined it should claim. Prior to calling
1293 * this function the specific probe function has called pci_enable_device
1294 * upon errant exit from this function specific probe function should call
1295 * pci_disable_device
1296 */
f02a9364
RA
1297struct tpm_chip *tpm_register_hardware(struct device *dev,
1298 const struct tpm_vendor_specific *entry)
1da177e4 1299{
6f9beccb
KJH
1300#define DEVNAME_SIZE 7
1301
1302 char *devname;
1da177e4 1303 struct tpm_chip *chip;
1da177e4
LT
1304
1305 /* Driver specific per-device data */
b888c87b 1306 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
8e39c933
PW
1307 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1308
dd78c943
AM
1309 if (chip == NULL || devname == NULL)
1310 goto out_free;
1da177e4 1311
d081d470
MK
1312 mutex_init(&chip->buffer_mutex);
1313 mutex_init(&chip->tpm_mutex);
1da177e4
LT
1314 INIT_LIST_HEAD(&chip->list);
1315
c4028958 1316 INIT_WORK(&chip->work, timeout_work);
09e12f9f 1317
40565f19
JS
1318 setup_timer(&chip->user_read_timer, user_reader_timeout,
1319 (unsigned long)chip);
fe3fd483 1320
90dda520 1321 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1da177e4 1322
10685a95 1323 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1da177e4 1324
10685a95 1325 if (chip->dev_num >= TPM_NUM_DEVICES) {
b888c87b 1326 dev_err(dev, "No available tpm device numbers\n");
dd78c943 1327 goto out_free;
1da177e4 1328 } else if (chip->dev_num == 0)
90dda520 1329 chip->vendor.miscdev.minor = TPM_MINOR;
1da177e4 1330 else
90dda520 1331 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1da177e4 1332
10685a95
KJH
1333 set_bit(chip->dev_num, dev_mask);
1334
6f9beccb 1335 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
90dda520 1336 chip->vendor.miscdev.name = devname;
1da177e4 1337
94fbcded 1338 chip->vendor.miscdev.parent = dev;
e659a3fe 1339 chip->dev = get_device(dev);
5bd91f18
RM
1340 chip->release = dev->release;
1341 dev->release = tpm_dev_release;
1342 dev_set_drvdata(dev, chip);
1da177e4 1343
90dda520 1344 if (misc_register(&chip->vendor.miscdev)) {
e659a3fe 1345 dev_err(chip->dev,
1da177e4 1346 "unable to misc_register %s, minor %d\n",
90dda520
KJH
1347 chip->vendor.miscdev.name,
1348 chip->vendor.miscdev.minor);
5bd91f18 1349 put_device(chip->dev);
e0dd03ca 1350 return NULL;
1da177e4
LT
1351 }
1352
f33d9bd5 1353 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
5d469ec0 1354 misc_deregister(&chip->vendor.miscdev);
5bd91f18 1355 put_device(chip->dev);
f02a9364 1356
f33d9bd5
JG
1357 return NULL;
1358 }
1da177e4 1359
55a82ab3
KJH
1360 chip->bios_dir = tpm_bios_log_setup(devname);
1361
f02a9364
RA
1362 /* Make chip available */
1363 spin_lock(&driver_lock);
1364 list_add_rcu(&chip->list, &tpm_chip_list);
1365 spin_unlock(&driver_lock);
1366
e0dd03ca 1367 return chip;
dd78c943
AM
1368
1369out_free:
1370 kfree(chip);
1371 kfree(devname);
1372 return NULL;
1da177e4 1373}
1da177e4
LT
1374EXPORT_SYMBOL_GPL(tpm_register_hardware);
1375
1da177e4
LT
1376MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1377MODULE_DESCRIPTION("TPM Driver");
1378MODULE_VERSION("2.0");
1379MODULE_LICENSE("GPL");