Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groec...
[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
c4ff4b82 367 if (duration_idx != TPM_UNDEFINED) {
36b20020 368 duration = chip->vendor.duration[duration_idx];
c4ff4b82
RA
369 /* if duration is 0, it's because chip->vendor.duration wasn't */
370 /* filled yet, so we set the lowest timeout just to give enough */
371 /* time for tpm_get_timeouts() to succeed */
372 return (duration <= 0 ? HZ : duration);
373 } else
9e18ee19 374 return 2 * 60 * HZ;
9e18ee19
KJH
375}
376EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
377
1da177e4
LT
378/*
379 * Internal kernel interface to transmit TPM commands
380 */
381static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
382 size_t bufsiz)
383{
d9e5b6bf 384 ssize_t rc;
9e18ee19 385 u32 count, ordinal;
700d8bdc 386 unsigned long stop;
1da177e4 387
81179bb6 388 count = be32_to_cpu(*((__be32 *) (buf + 2)));
9e18ee19 389 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
1da177e4
LT
390 if (count == 0)
391 return -ENODATA;
392 if (count > bufsiz) {
e659a3fe 393 dev_err(chip->dev,
b76be681 394 "invalid count value %x %zx \n", count, bufsiz);
1da177e4
LT
395 return -E2BIG;
396 }
397
d081d470 398 mutex_lock(&chip->tpm_mutex);
1da177e4 399
90dda520 400 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
e659a3fe 401 dev_err(chip->dev,
d9e5b6bf
KH
402 "tpm_transmit: tpm_send: error %zd\n", rc);
403 goto out;
1da177e4
LT
404 }
405
27084efe
LD
406 if (chip->vendor.irq)
407 goto out_recv;
408
9e18ee19 409 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
1da177e4 410 do {
90dda520
KJH
411 u8 status = chip->vendor.status(chip);
412 if ((status & chip->vendor.req_complete_mask) ==
413 chip->vendor.req_complete_val)
1da177e4 414 goto out_recv;
d9e5b6bf 415
90dda520 416 if ((status == chip->vendor.req_canceled)) {
e659a3fe 417 dev_err(chip->dev, "Operation Canceled\n");
d9e5b6bf
KH
418 rc = -ECANCELED;
419 goto out;
420 }
421
422 msleep(TPM_TIMEOUT); /* CHECK */
1da177e4 423 rmb();
700d8bdc 424 } while (time_before(jiffies, stop));
1da177e4 425
90dda520 426 chip->vendor.cancel(chip);
e659a3fe 427 dev_err(chip->dev, "Operation Timed out\n");
d9e5b6bf
KH
428 rc = -ETIME;
429 goto out;
1da177e4
LT
430
431out_recv:
90dda520 432 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
d9e5b6bf 433 if (rc < 0)
e659a3fe 434 dev_err(chip->dev,
d9e5b6bf
KH
435 "tpm_transmit: tpm_recv: error %zd\n", rc);
436out:
d081d470 437 mutex_unlock(&chip->tpm_mutex);
d9e5b6bf 438 return rc;
1da177e4
LT
439}
440
441#define TPM_DIGEST_SIZE 20
beed53a1
KJH
442#define TPM_ERROR_SIZE 10
443#define TPM_RET_CODE_IDX 6
beed53a1
KJH
444
445enum tpm_capabilities {
08837438
RA
446 TPM_CAP_FLAG = cpu_to_be32(4),
447 TPM_CAP_PROP = cpu_to_be32(5),
448 CAP_VERSION_1_1 = cpu_to_be32(0x06),
449 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
beed53a1
KJH
450};
451
452enum tpm_sub_capabilities {
08837438
RA
453 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
454 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
455 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
456 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
457 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
458 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
459 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
beed53a1 460
1da177e4
LT
461};
462
08837438
RA
463static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
464 int len, const char *desc)
beed53a1
KJH
465{
466 int err;
467
08837438 468 len = tpm_transmit(chip,(u8 *) cmd, len);
beed53a1
KJH
469 if (len < 0)
470 return len;
471 if (len == TPM_ERROR_SIZE) {
08837438 472 err = be32_to_cpu(cmd->header.out.return_code);
beed53a1
KJH
473 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
474 return err;
475 }
476 return 0;
477}
478
08837438
RA
479#define TPM_INTERNAL_RESULT_SIZE 200
480#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
481#define TPM_ORD_GET_CAP cpu_to_be32(101)
482
483static const struct tpm_input_header tpm_getcap_header = {
484 .tag = TPM_TAG_RQU_COMMAND,
485 .length = cpu_to_be32(22),
486 .ordinal = TPM_ORD_GET_CAP
487};
488
489ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
490 const char *desc)
491{
492 struct tpm_cmd_t tpm_cmd;
493 int rc;
494 struct tpm_chip *chip = dev_get_drvdata(dev);
495
496 tpm_cmd.header.in = tpm_getcap_header;
497 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
498 tpm_cmd.params.getcap_in.cap = subcap_id;
499 /*subcap field not necessary */
500 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
501 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
502 } else {
503 if (subcap_id == TPM_CAP_FLAG_PERM ||
504 subcap_id == TPM_CAP_FLAG_VOL)
505 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
506 else
507 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
508 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
509 tpm_cmd.params.getcap_in.subcap = subcap_id;
510 }
511 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
512 if (!rc)
513 *cap = tpm_cmd.params.getcap_out.cap;
514 return rc;
515}
516
08e96e48
KJH
517void tpm_gen_interrupt(struct tpm_chip *chip)
518{
08837438 519 struct tpm_cmd_t tpm_cmd;
08e96e48
KJH
520 ssize_t rc;
521
08837438
RA
522 tpm_cmd.header.in = tpm_getcap_header;
523 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
524 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
525 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
08e96e48 526
08837438 527 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
528 "attempting to determine the timeouts");
529}
530EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
531
532void tpm_get_timeouts(struct tpm_chip *chip)
533{
08837438
RA
534 struct tpm_cmd_t tpm_cmd;
535 struct timeout_t *timeout_cap;
536 struct duration_t *duration_cap;
08e96e48
KJH
537 ssize_t rc;
538 u32 timeout;
539
08837438
RA
540 tpm_cmd.header.in = tpm_getcap_header;
541 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
542 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
543 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
08e96e48 544
08837438 545 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
546 "attempting to determine the timeouts");
547 if (rc)
548 goto duration;
549
08837438 550 if (be32_to_cpu(tpm_cmd.header.out.length)
08e96e48
KJH
551 != 4 * sizeof(u32))
552 goto duration;
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);
08e96e48 557 if (timeout)
9e5b1b12 558 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
08837438 559 timeout = be32_to_cpu(timeout_cap->b);
08e96e48 560 if (timeout)
9e5b1b12 561 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
08837438 562 timeout = be32_to_cpu(timeout_cap->c);
08e96e48 563 if (timeout)
9e5b1b12 564 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
08837438 565 timeout = be32_to_cpu(timeout_cap->d);
08e96e48 566 if (timeout)
9e5b1b12 567 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
08e96e48
KJH
568
569duration:
08837438
RA
570 tpm_cmd.header.in = tpm_getcap_header;
571 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
572 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
573 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
08e96e48 574
08837438 575 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
08e96e48
KJH
576 "attempting to determine the durations");
577 if (rc)
578 return;
579
e5871372
LT
580 if (be32_to_cpu(tpm_cmd.header.out.return_code)
581 != 3 * sizeof(u32))
08e96e48 582 return;
08837438 583 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
08e96e48 584 chip->vendor.duration[TPM_SHORT] =
08837438 585 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
292cf4a8
VK
586 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
587 * value wrong and apparently reports msecs rather than usecs. So we
588 * fix up the resulting too-small TPM_SHORT value to make things work.
589 */
590 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
591 chip->vendor.duration[TPM_SHORT] = HZ;
592
08e96e48 593 chip->vendor.duration[TPM_MEDIUM] =
08837438 594 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
08e96e48 595 chip->vendor.duration[TPM_LONG] =
08837438 596 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
08e96e48
KJH
597}
598EXPORT_SYMBOL_GPL(tpm_get_timeouts);
599
600void tpm_continue_selftest(struct tpm_chip *chip)
601{
602 u8 data[] = {
603 0, 193, /* TPM_TAG_RQU_COMMAND */
604 0, 0, 0, 10, /* length */
605 0, 0, 0, 83, /* TPM_ORD_GetCapability */
606 };
607
608 tpm_transmit(chip, data, sizeof(data));
609}
610EXPORT_SYMBOL_GPL(tpm_continue_selftest);
611
612ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
613 char *buf)
614{
08837438 615 cap_t cap;
08e96e48
KJH
616 ssize_t rc;
617
08837438
RA
618 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
619 "attempting to determine the permanent enabled state");
620 if (rc)
08e96e48 621 return 0;
ec288bd3 622
08837438 623 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
ec288bd3 624 return rc;
08e96e48
KJH
625}
626EXPORT_SYMBOL_GPL(tpm_show_enabled);
627
628ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
629 char *buf)
630{
08837438 631 cap_t cap;
08e96e48
KJH
632 ssize_t rc;
633
08837438
RA
634 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
635 "attempting to determine the permanent active state");
636 if (rc)
08e96e48 637 return 0;
ec288bd3 638
08837438 639 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
ec288bd3 640 return rc;
08e96e48
KJH
641}
642EXPORT_SYMBOL_GPL(tpm_show_active);
643
644ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
645 char *buf)
646{
08837438 647 cap_t cap;
08e96e48
KJH
648 ssize_t rc;
649
08837438
RA
650 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
651 "attempting to determine the owner state");
652 if (rc)
08e96e48 653 return 0;
ec288bd3 654
08837438 655 rc = sprintf(buf, "%d\n", cap.owned);
ec288bd3 656 return rc;
08e96e48
KJH
657}
658EXPORT_SYMBOL_GPL(tpm_show_owned);
659
660ssize_t tpm_show_temp_deactivated(struct device * dev,
661 struct device_attribute * attr, char *buf)
662{
08837438 663 cap_t cap;
08e96e48
KJH
664 ssize_t rc;
665
08837438
RA
666 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
667 "attempting to determine the temporary state");
668 if (rc)
08e96e48 669 return 0;
ec288bd3 670
08837438 671 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
ec288bd3 672 return rc;
08e96e48
KJH
673}
674EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
675
659aaf2b
RA
676/*
677 * tpm_chip_find_get - return tpm_chip for given chip number
678 */
679static struct tpm_chip *tpm_chip_find_get(int chip_num)
680{
8920d5ad 681 struct tpm_chip *pos, *chip = NULL;
659aaf2b
RA
682
683 rcu_read_lock();
684 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
685 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
686 continue;
687
8920d5ad
RA
688 if (try_module_get(pos->dev->driver->owner)) {
689 chip = pos;
659aaf2b 690 break;
8920d5ad 691 }
659aaf2b
RA
692 }
693 rcu_read_unlock();
8920d5ad 694 return chip;
659aaf2b
RA
695}
696
697#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
698#define READ_PCR_RESULT_SIZE 30
699static struct tpm_input_header pcrread_header = {
700 .tag = TPM_TAG_RQU_COMMAND,
701 .length = cpu_to_be32(14),
702 .ordinal = TPM_ORDINAL_PCRREAD
1da177e4
LT
703};
704
659aaf2b
RA
705int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
706{
707 int rc;
708 struct tpm_cmd_t cmd;
709
710 cmd.header.in = pcrread_header;
711 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
23acb98d 712 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
659aaf2b
RA
713 "attempting to read a pcr value");
714
715 if (rc == 0)
716 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
717 TPM_DIGEST_SIZE);
718 return rc;
719}
720
721/**
722 * tpm_pcr_read - read a pcr value
723 * @chip_num: tpm idx # or ANY
724 * @pcr_idx: pcr idx to retrieve
725 * @res_buf: TPM_PCR value
726 * size of res_buf is 20 bytes (or NULL if you don't care)
727 *
728 * The TPM driver should be built-in, but for whatever reason it
729 * isn't, protect against the chip disappearing, by incrementing
730 * the module usage count.
731 */
732int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
733{
734 struct tpm_chip *chip;
735 int rc;
736
737 chip = tpm_chip_find_get(chip_num);
738 if (chip == NULL)
739 return -ENODEV;
740 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
a0e39349 741 tpm_chip_put(chip);
659aaf2b
RA
742 return rc;
743}
744EXPORT_SYMBOL_GPL(tpm_pcr_read);
745
746/**
747 * tpm_pcr_extend - extend pcr value with hash
748 * @chip_num: tpm idx # or AN&
749 * @pcr_idx: pcr idx to extend
750 * @hash: hash value used to extend pcr value
751 *
752 * The TPM driver should be built-in, but for whatever reason it
753 * isn't, protect against the chip disappearing, by incrementing
754 * the module usage count.
755 */
756#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
0afd9056 757#define EXTEND_PCR_RESULT_SIZE 34
659aaf2b
RA
758static struct tpm_input_header pcrextend_header = {
759 .tag = TPM_TAG_RQU_COMMAND,
760 .length = cpu_to_be32(34),
761 .ordinal = TPM_ORD_PCR_EXTEND
762};
763
764int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
765{
766 struct tpm_cmd_t cmd;
767 int rc;
768 struct tpm_chip *chip;
769
770 chip = tpm_chip_find_get(chip_num);
771 if (chip == NULL)
772 return -ENODEV;
773
774 cmd.header.in = pcrextend_header;
659aaf2b
RA
775 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
776 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
0afd9056 777 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
659aaf2b
RA
778 "attempting extend a PCR value");
779
a0e39349 780 tpm_chip_put(chip);
659aaf2b
RA
781 return rc;
782}
783EXPORT_SYMBOL_GPL(tpm_pcr_extend);
784
c749ba91
MZ
785int tpm_send(u32 chip_num, void *cmd, size_t buflen)
786{
787 struct tpm_chip *chip;
788 int rc;
789
790 chip = tpm_chip_find_get(chip_num);
791 if (chip == NULL)
792 return -ENODEV;
793
794 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
795
796 tpm_chip_put(chip);
797 return rc;
798}
799EXPORT_SYMBOL_GPL(tpm_send);
800
6659ca2a
KH
801ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
802 char *buf)
1da177e4 803{
08837438 804 cap_t cap;
659aaf2b 805 u8 digest[TPM_DIGEST_SIZE];
beed53a1 806 ssize_t rc;
81179bb6 807 int i, j, num_pcrs;
1da177e4 808 char *str = buf;
e659a3fe 809 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4 810
08837438 811 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
beed53a1 812 "attempting to determine the number of PCRS");
08837438 813 if (rc)
e234bc97 814 return 0;
1da177e4 815
08837438 816 num_pcrs = be32_to_cpu(cap.num_pcrs);
1da177e4 817 for (i = 0; i < num_pcrs; i++) {
659aaf2b 818 rc = __tpm_pcr_read(chip, i, digest);
beed53a1 819 if (rc)
08837438 820 break;
1da177e4
LT
821 str += sprintf(str, "PCR-%02d: ", i);
822 for (j = 0; j < TPM_DIGEST_SIZE; j++)
659aaf2b 823 str += sprintf(str, "%02X ", digest[j]);
1da177e4
LT
824 str += sprintf(str, "\n");
825 }
826 return str - buf;
827}
6659ca2a 828EXPORT_SYMBOL_GPL(tpm_show_pcrs);
1da177e4
LT
829
830#define READ_PUBEK_RESULT_SIZE 314
08837438
RA
831#define TPM_ORD_READPUBEK cpu_to_be32(124)
832struct tpm_input_header tpm_readpubek_header = {
833 .tag = TPM_TAG_RQU_COMMAND,
834 .length = cpu_to_be32(30),
835 .ordinal = TPM_ORD_READPUBEK
1da177e4
LT
836};
837
6659ca2a
KH
838ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
839 char *buf)
1da177e4 840{
2df7111f 841 u8 *data;
08837438 842 struct tpm_cmd_t tpm_cmd;
beed53a1 843 ssize_t err;
81179bb6 844 int i, rc;
1da177e4
LT
845 char *str = buf;
846
e659a3fe 847 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4 848
08837438
RA
849 tpm_cmd.header.in = tpm_readpubek_header;
850 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
beed53a1
KJH
851 "attempting to read the PUBEK");
852 if (err)
34d6e075 853 goto out;
1da177e4
LT
854
855 /*
856 ignore header 10 bytes
857 algorithm 32 bits (1 == RSA )
858 encscheme 16 bits
859 sigscheme 16 bits
860 parameters (RSA 12->bytes: keybit, #primes, expbit)
861 keylenbytes 32 bits
862 256 byte modulus
863 ignore checksum 20 bytes
864 */
08837438 865 data = tpm_cmd.params.readpubek_out_buffer;
1da177e4
LT
866 str +=
867 sprintf(str,
868 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
869 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
870 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
871 "Modulus length: %d\nModulus: \n",
872 data[10], data[11], data[12], data[13], data[14],
873 data[15], data[16], data[17], data[22], data[23],
874 data[24], data[25], data[26], data[27], data[28],
875 data[29], data[30], data[31], data[32], data[33],
1dda8abe 876 be32_to_cpu(*((__be32 *) (data + 34))));
1da177e4
LT
877
878 for (i = 0; i < 256; i++) {
1dda8abe 879 str += sprintf(str, "%02X ", data[i + 38]);
1da177e4
LT
880 if ((i + 1) % 16 == 0)
881 str += sprintf(str, "\n");
882 }
34d6e075 883out:
beed53a1 884 rc = str - buf;
2df7111f 885 return rc;
1da177e4 886}
6659ca2a 887EXPORT_SYMBOL_GPL(tpm_show_pubek);
1da177e4 888
1da177e4 889
6659ca2a
KH
890ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
891 char *buf)
1da177e4 892{
08837438 893 cap_t cap;
beed53a1 894 ssize_t rc;
1da177e4
LT
895 char *str = buf;
896
08837438 897 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
beed53a1 898 "attempting to determine the manufacturer");
08837438 899 if (rc)
beed53a1 900 return 0;
1da177e4 901 str += sprintf(str, "Manufacturer: 0x%x\n",
08837438 902 be32_to_cpu(cap.manufacturer_id));
1da177e4 903
08837438
RA
904 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
905 "attempting to determine the 1.1 version");
beed53a1 906 if (rc)
08837438 907 return 0;
beed53a1
KJH
908 str += sprintf(str,
909 "TCG version: %d.%d\nFirmware version: %d.%d\n",
08837438
RA
910 cap.tpm_version.Major, cap.tpm_version.Minor,
911 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1da177e4
LT
912 return str - buf;
913}
6659ca2a
KH
914EXPORT_SYMBOL_GPL(tpm_show_caps);
915
08e96e48
KJH
916ssize_t tpm_show_caps_1_2(struct device * dev,
917 struct device_attribute * attr, char *buf)
918{
08837438
RA
919 cap_t cap;
920 ssize_t rc;
08e96e48
KJH
921 char *str = buf;
922
08837438
RA
923 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
924 "attempting to determine the manufacturer");
925 if (rc)
08e96e48 926 return 0;
08e96e48 927 str += sprintf(str, "Manufacturer: 0x%x\n",
08837438
RA
928 be32_to_cpu(cap.manufacturer_id));
929 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
930 "attempting to determine the 1.2 version");
931 if (rc)
932 return 0;
08e96e48
KJH
933 str += sprintf(str,
934 "TCG version: %d.%d\nFirmware version: %d.%d\n",
08837438
RA
935 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
936 cap.tpm_version_1_2.revMajor,
937 cap.tpm_version_1_2.revMinor);
08e96e48
KJH
938 return str - buf;
939}
940EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
941
6659ca2a
KH
942ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
943 const char *buf, size_t count)
944{
945 struct tpm_chip *chip = dev_get_drvdata(dev);
946 if (chip == NULL)
947 return 0;
948
90dda520 949 chip->vendor.cancel(chip);
6659ca2a
KH
950 return count;
951}
952EXPORT_SYMBOL_GPL(tpm_store_cancel);
1da177e4 953
1da177e4
LT
954/*
955 * Device file system interface to the TPM
f89c5edb
RA
956 *
957 * It's assured that the chip will be opened just once,
958 * by the check of is_open variable, which is protected
959 * by driver_lock.
1da177e4
LT
960 */
961int tpm_open(struct inode *inode, struct file *file)
962{
f02a9364 963 int minor = iminor(inode);
1da177e4
LT
964 struct tpm_chip *chip = NULL, *pos;
965
f02a9364
RA
966 rcu_read_lock();
967 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
90dda520 968 if (pos->vendor.miscdev.minor == minor) {
1da177e4 969 chip = pos;
f02a9364 970 get_device(chip->dev);
1da177e4
LT
971 break;
972 }
973 }
f02a9364 974 rcu_read_unlock();
1da177e4 975
f02a9364
RA
976 if (!chip)
977 return -ENODEV;
1da177e4 978
dc36d32c 979 if (test_and_set_bit(0, &chip->is_open)) {
b888c87b 980 dev_dbg(chip->dev, "Another process owns this TPM\n");
f02a9364
RA
981 put_device(chip->dev);
982 return -EBUSY;
1da177e4
LT
983 }
984
1da177e4
LT
985 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
986 if (chip->data_buffer == NULL) {
dc36d32c 987 clear_bit(0, &chip->is_open);
e659a3fe 988 put_device(chip->dev);
1da177e4
LT
989 return -ENOMEM;
990 }
991
992 atomic_set(&chip->data_pending, 0);
993
994 file->private_data = chip;
995 return 0;
1da177e4 996}
1da177e4
LT
997EXPORT_SYMBOL_GPL(tpm_open);
998
f02a9364
RA
999/*
1000 * Called on file close
1001 */
1da177e4
LT
1002int tpm_release(struct inode *inode, struct file *file)
1003{
1004 struct tpm_chip *chip = file->private_data;
1da177e4 1005
4bdec11f 1006 del_singleshot_timer_sync(&chip->user_read_timer);
2e5c44c9 1007 flush_work_sync(&chip->work);
5e976d55 1008 file->private_data = NULL;
1da177e4 1009 atomic_set(&chip->data_pending, 0);
f02a9364 1010 kfree(chip->data_buffer);
dc36d32c 1011 clear_bit(0, &chip->is_open);
e659a3fe 1012 put_device(chip->dev);
1da177e4
LT
1013 return 0;
1014}
1da177e4
LT
1015EXPORT_SYMBOL_GPL(tpm_release);
1016
b888c87b 1017ssize_t tpm_write(struct file *file, const char __user *buf,
3c2f606a 1018 size_t size, loff_t *off)
1da177e4
LT
1019{
1020 struct tpm_chip *chip = file->private_data;
01476001 1021 size_t in_size = size, out_size;
1da177e4
LT
1022
1023 /* cannot perform a write until the read has cleared
1024 either via tpm_read or a user_read_timer timeout */
700d8bdc
NA
1025 while (atomic_read(&chip->data_pending) != 0)
1026 msleep(TPM_TIMEOUT);
1da177e4 1027
d081d470 1028 mutex_lock(&chip->buffer_mutex);
1da177e4
LT
1029
1030 if (in_size > TPM_BUFSIZE)
1031 in_size = TPM_BUFSIZE;
1032
1033 if (copy_from_user
1034 (chip->data_buffer, (void __user *) buf, in_size)) {
d081d470 1035 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1036 return -EFAULT;
1037 }
1038
1039 /* atomic tpm command send and result receive */
1040 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1041
1042 atomic_set(&chip->data_pending, out_size);
d081d470 1043 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1044
1045 /* Set a timeout by which the reader must come claim the result */
fe3fd483 1046 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1da177e4
LT
1047
1048 return in_size;
1049}
1da177e4
LT
1050EXPORT_SYMBOL_GPL(tpm_write);
1051
3c2f606a
KJH
1052ssize_t tpm_read(struct file *file, char __user *buf,
1053 size_t size, loff_t *off)
1da177e4
LT
1054{
1055 struct tpm_chip *chip = file->private_data;
01476001 1056 ssize_t ret_size;
1da177e4 1057
5b44bd58 1058 del_singleshot_timer_sync(&chip->user_read_timer);
2e5c44c9 1059 flush_work_sync(&chip->work);
5b44bd58
KH
1060 ret_size = atomic_read(&chip->data_pending);
1061 atomic_set(&chip->data_pending, 0);
1062 if (ret_size > 0) { /* relay data */
1063 if (size < ret_size)
1064 ret_size = size;
1da177e4 1065
d081d470 1066 mutex_lock(&chip->buffer_mutex);
f6a2382c 1067 if (copy_to_user(buf, chip->data_buffer, ret_size))
5b44bd58 1068 ret_size = -EFAULT;
d081d470 1069 mutex_unlock(&chip->buffer_mutex);
1da177e4
LT
1070 }
1071
1072 return ret_size;
1073}
1da177e4
LT
1074EXPORT_SYMBOL_GPL(tpm_read);
1075
e659a3fe 1076void tpm_remove_hardware(struct device *dev)
1da177e4 1077{
e659a3fe 1078 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4
LT
1079
1080 if (chip == NULL) {
e659a3fe 1081 dev_err(dev, "No device data found\n");
1da177e4
LT
1082 return;
1083 }
1084
1085 spin_lock(&driver_lock);
f02a9364 1086 list_del_rcu(&chip->list);
1da177e4 1087 spin_unlock(&driver_lock);
f02a9364 1088 synchronize_rcu();
1da177e4 1089
90dda520 1090 misc_deregister(&chip->vendor.miscdev);
90dda520 1091 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
55a82ab3 1092 tpm_bios_log_teardown(chip->bios_dir);
1da177e4 1093
5bd91f18
RM
1094 /* write it this way to be explicit (chip->dev == dev) */
1095 put_device(chip->dev);
1da177e4 1096}
e659a3fe 1097EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1da177e4 1098
225a9be2
RA
1099#define TPM_ORD_SAVESTATE cpu_to_be32(152)
1100#define SAVESTATE_RESULT_SIZE 10
1101
1102static struct tpm_input_header savestate_header = {
1103 .tag = TPM_TAG_RQU_COMMAND,
1104 .length = cpu_to_be32(10),
1105 .ordinal = TPM_ORD_SAVESTATE
1106};
1107
1da177e4
LT
1108/*
1109 * We are about to suspend. Save the TPM state
1110 * so that it can be restored.
1111 */
ce2c87d4 1112int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1da177e4 1113{
ce2c87d4 1114 struct tpm_chip *chip = dev_get_drvdata(dev);
225a9be2
RA
1115 struct tpm_cmd_t cmd;
1116 int rc;
1117
1118 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
2490c681 1119
1da177e4
LT
1120 if (chip == NULL)
1121 return -ENODEV;
1122
225a9be2
RA
1123 /* for buggy tpm, flush pcrs with extend to selected dummy */
1124 if (tpm_suspend_pcr) {
1125 cmd.header.in = pcrextend_header;
1126 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1127 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1128 TPM_DIGEST_SIZE);
1129 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1130 "extending dummy pcr before suspend");
1131 }
1132
1133 /* now do the actual savestate */
1134 cmd.header.in = savestate_header;
1135 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1136 "sending savestate before suspend");
1137 return rc;
1da177e4 1138}
1da177e4
LT
1139EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1140
1141/*
1142 * Resume from a power safe. The BIOS already restored
1143 * the TPM state.
1144 */
ce2c87d4 1145int tpm_pm_resume(struct device *dev)
1da177e4 1146{
ce2c87d4 1147 struct tpm_chip *chip = dev_get_drvdata(dev);
1da177e4
LT
1148
1149 if (chip == NULL)
1150 return -ENODEV;
1151
1da177e4
LT
1152 return 0;
1153}
1da177e4
LT
1154EXPORT_SYMBOL_GPL(tpm_pm_resume);
1155
253115b7
RA
1156/* In case vendor provided release function, call it too.*/
1157
1158void tpm_dev_vendor_release(struct tpm_chip *chip)
1159{
1160 if (chip->vendor.release)
1161 chip->vendor.release(chip->dev);
1162
1163 clear_bit(chip->dev_num, dev_mask);
1164 kfree(chip->vendor.miscdev.name);
1165}
1166EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1167
1168
5bd91f18
RM
1169/*
1170 * Once all references to platform device are down to 0,
1171 * release all allocated structures.
5bd91f18 1172 */
cbb2ed4a 1173void tpm_dev_release(struct device *dev)
5bd91f18
RM
1174{
1175 struct tpm_chip *chip = dev_get_drvdata(dev);
1176
253115b7 1177 tpm_dev_vendor_release(chip);
5bd91f18 1178
253115b7 1179 chip->release(dev);
5bd91f18
RM
1180 kfree(chip);
1181}
253115b7 1182EXPORT_SYMBOL_GPL(tpm_dev_release);
5bd91f18 1183
1da177e4
LT
1184/*
1185 * Called from tpm_<specific>.c probe function only for devices
1186 * the driver has determined it should claim. Prior to calling
1187 * this function the specific probe function has called pci_enable_device
1188 * upon errant exit from this function specific probe function should call
1189 * pci_disable_device
1190 */
f02a9364
RA
1191struct tpm_chip *tpm_register_hardware(struct device *dev,
1192 const struct tpm_vendor_specific *entry)
1da177e4 1193{
6f9beccb
KJH
1194#define DEVNAME_SIZE 7
1195
1196 char *devname;
1da177e4 1197 struct tpm_chip *chip;
1da177e4
LT
1198
1199 /* Driver specific per-device data */
b888c87b 1200 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
8e39c933
PW
1201 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1202
dd78c943
AM
1203 if (chip == NULL || devname == NULL)
1204 goto out_free;
1da177e4 1205
d081d470
MK
1206 mutex_init(&chip->buffer_mutex);
1207 mutex_init(&chip->tpm_mutex);
1da177e4
LT
1208 INIT_LIST_HEAD(&chip->list);
1209
c4028958 1210 INIT_WORK(&chip->work, timeout_work);
09e12f9f 1211
40565f19
JS
1212 setup_timer(&chip->user_read_timer, user_reader_timeout,
1213 (unsigned long)chip);
fe3fd483 1214
90dda520 1215 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1da177e4 1216
10685a95 1217 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1da177e4 1218
10685a95 1219 if (chip->dev_num >= TPM_NUM_DEVICES) {
b888c87b 1220 dev_err(dev, "No available tpm device numbers\n");
dd78c943 1221 goto out_free;
1da177e4 1222 } else if (chip->dev_num == 0)
90dda520 1223 chip->vendor.miscdev.minor = TPM_MINOR;
1da177e4 1224 else
90dda520 1225 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1da177e4 1226
10685a95
KJH
1227 set_bit(chip->dev_num, dev_mask);
1228
6f9beccb 1229 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
90dda520 1230 chip->vendor.miscdev.name = devname;
1da177e4 1231
94fbcded 1232 chip->vendor.miscdev.parent = dev;
e659a3fe 1233 chip->dev = get_device(dev);
5bd91f18
RM
1234 chip->release = dev->release;
1235 dev->release = tpm_dev_release;
1236 dev_set_drvdata(dev, chip);
1da177e4 1237
90dda520 1238 if (misc_register(&chip->vendor.miscdev)) {
e659a3fe 1239 dev_err(chip->dev,
1da177e4 1240 "unable to misc_register %s, minor %d\n",
90dda520
KJH
1241 chip->vendor.miscdev.name,
1242 chip->vendor.miscdev.minor);
5bd91f18 1243 put_device(chip->dev);
e0dd03ca 1244 return NULL;
1da177e4
LT
1245 }
1246
f33d9bd5 1247 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
5d469ec0 1248 misc_deregister(&chip->vendor.miscdev);
5bd91f18 1249 put_device(chip->dev);
f02a9364 1250
f33d9bd5
JG
1251 return NULL;
1252 }
1da177e4 1253
55a82ab3
KJH
1254 chip->bios_dir = tpm_bios_log_setup(devname);
1255
f02a9364
RA
1256 /* Make chip available */
1257 spin_lock(&driver_lock);
1258 list_add_rcu(&chip->list, &tpm_chip_list);
1259 spin_unlock(&driver_lock);
1260
e0dd03ca 1261 return chip;
dd78c943
AM
1262
1263out_free:
1264 kfree(chip);
1265 kfree(devname);
1266 return NULL;
1da177e4 1267}
1da177e4
LT
1268EXPORT_SYMBOL_GPL(tpm_register_hardware);
1269
1da177e4
LT
1270MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1271MODULE_DESCRIPTION("TPM Driver");
1272MODULE_VERSION("2.0");
1273MODULE_LICENSE("GPL");