initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / char / tpm / tpm.c
bloba0789f682e14d214ab75abc61bcf4694f88054df
1 /*
2 * Copyright (C) 2004 IBM Corporation
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>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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.
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
22 * calls to msleep.
26 #include <linux/poll.h>
27 #include <linux/mutex.h>
28 #include <linux/spinlock.h>
30 #include "tpm.h"
32 enum tpm_const {
33 TPM_MINOR = 224, /* officially assigned */
34 TPM_BUFSIZE = 2048,
35 TPM_NUM_DEVICES = 256,
38 enum tpm_duration {
39 TPM_SHORT = 0,
40 TPM_MEDIUM = 1,
41 TPM_LONG = 2,
42 TPM_UNDEFINED,
45 #define TPM_MAX_ORDINAL 243
46 #define TPM_MAX_PROTECTED_ORDINAL 12
47 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
49 static LIST_HEAD(tpm_chip_list);
50 static DEFINE_SPINLOCK(driver_lock);
51 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
54 * Array with one entry per ordinal defining the maximum amount
55 * of time the chip could take to return the result. The ordinal
56 * designation of short, medium or long is defined in a table in
57 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
58 * values of the SHORT, MEDIUM, and LONG durations are retrieved
59 * from the chip during initialization with a call to tpm_get_timeouts.
61 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
62 TPM_UNDEFINED, /* 0 */
63 TPM_UNDEFINED,
64 TPM_UNDEFINED,
65 TPM_UNDEFINED,
66 TPM_UNDEFINED,
67 TPM_UNDEFINED, /* 5 */
68 TPM_UNDEFINED,
69 TPM_UNDEFINED,
70 TPM_UNDEFINED,
71 TPM_UNDEFINED,
72 TPM_SHORT, /* 10 */
73 TPM_SHORT,
76 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
77 TPM_UNDEFINED, /* 0 */
78 TPM_UNDEFINED,
79 TPM_UNDEFINED,
80 TPM_UNDEFINED,
81 TPM_UNDEFINED,
82 TPM_UNDEFINED, /* 5 */
83 TPM_UNDEFINED,
84 TPM_UNDEFINED,
85 TPM_UNDEFINED,
86 TPM_UNDEFINED,
87 TPM_SHORT, /* 10 */
88 TPM_SHORT,
89 TPM_MEDIUM,
90 TPM_LONG,
91 TPM_LONG,
92 TPM_MEDIUM, /* 15 */
93 TPM_SHORT,
94 TPM_SHORT,
95 TPM_MEDIUM,
96 TPM_LONG,
97 TPM_SHORT, /* 20 */
98 TPM_SHORT,
99 TPM_MEDIUM,
100 TPM_MEDIUM,
101 TPM_MEDIUM,
102 TPM_SHORT, /* 25 */
103 TPM_SHORT,
104 TPM_MEDIUM,
105 TPM_SHORT,
106 TPM_SHORT,
107 TPM_MEDIUM, /* 30 */
108 TPM_LONG,
109 TPM_MEDIUM,
110 TPM_SHORT,
111 TPM_SHORT,
112 TPM_SHORT, /* 35 */
113 TPM_MEDIUM,
114 TPM_MEDIUM,
115 TPM_UNDEFINED,
116 TPM_UNDEFINED,
117 TPM_MEDIUM, /* 40 */
118 TPM_LONG,
119 TPM_MEDIUM,
120 TPM_SHORT,
121 TPM_SHORT,
122 TPM_SHORT, /* 45 */
123 TPM_SHORT,
124 TPM_SHORT,
125 TPM_SHORT,
126 TPM_LONG,
127 TPM_MEDIUM, /* 50 */
128 TPM_MEDIUM,
129 TPM_UNDEFINED,
130 TPM_UNDEFINED,
131 TPM_UNDEFINED,
132 TPM_UNDEFINED, /* 55 */
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_UNDEFINED,
137 TPM_MEDIUM, /* 60 */
138 TPM_MEDIUM,
139 TPM_MEDIUM,
140 TPM_SHORT,
141 TPM_SHORT,
142 TPM_MEDIUM, /* 65 */
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_SHORT, /* 70 */
148 TPM_SHORT,
149 TPM_UNDEFINED,
150 TPM_UNDEFINED,
151 TPM_UNDEFINED,
152 TPM_UNDEFINED, /* 75 */
153 TPM_UNDEFINED,
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_UNDEFINED,
157 TPM_LONG, /* 80 */
158 TPM_UNDEFINED,
159 TPM_MEDIUM,
160 TPM_LONG,
161 TPM_SHORT,
162 TPM_UNDEFINED, /* 85 */
163 TPM_UNDEFINED,
164 TPM_UNDEFINED,
165 TPM_UNDEFINED,
166 TPM_UNDEFINED,
167 TPM_SHORT, /* 90 */
168 TPM_SHORT,
169 TPM_SHORT,
170 TPM_SHORT,
171 TPM_SHORT,
172 TPM_UNDEFINED, /* 95 */
173 TPM_UNDEFINED,
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_UNDEFINED,
177 TPM_MEDIUM, /* 100 */
178 TPM_SHORT,
179 TPM_SHORT,
180 TPM_UNDEFINED,
181 TPM_UNDEFINED,
182 TPM_UNDEFINED, /* 105 */
183 TPM_UNDEFINED,
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_UNDEFINED,
187 TPM_SHORT, /* 110 */
188 TPM_SHORT,
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_SHORT,
192 TPM_SHORT, /* 115 */
193 TPM_SHORT,
194 TPM_SHORT,
195 TPM_UNDEFINED,
196 TPM_UNDEFINED,
197 TPM_LONG, /* 120 */
198 TPM_LONG,
199 TPM_MEDIUM,
200 TPM_UNDEFINED,
201 TPM_SHORT,
202 TPM_SHORT, /* 125 */
203 TPM_SHORT,
204 TPM_LONG,
205 TPM_SHORT,
206 TPM_SHORT,
207 TPM_SHORT, /* 130 */
208 TPM_MEDIUM,
209 TPM_UNDEFINED,
210 TPM_SHORT,
211 TPM_MEDIUM,
212 TPM_UNDEFINED, /* 135 */
213 TPM_UNDEFINED,
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_UNDEFINED,
217 TPM_SHORT, /* 140 */
218 TPM_SHORT,
219 TPM_UNDEFINED,
220 TPM_UNDEFINED,
221 TPM_UNDEFINED,
222 TPM_UNDEFINED, /* 145 */
223 TPM_UNDEFINED,
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_UNDEFINED,
227 TPM_SHORT, /* 150 */
228 TPM_MEDIUM,
229 TPM_MEDIUM,
230 TPM_SHORT,
231 TPM_SHORT,
232 TPM_UNDEFINED, /* 155 */
233 TPM_UNDEFINED,
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_UNDEFINED,
237 TPM_SHORT, /* 160 */
238 TPM_SHORT,
239 TPM_SHORT,
240 TPM_SHORT,
241 TPM_UNDEFINED,
242 TPM_UNDEFINED, /* 165 */
243 TPM_UNDEFINED,
244 TPM_UNDEFINED,
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_LONG, /* 170 */
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_UNDEFINED,
251 TPM_UNDEFINED,
252 TPM_UNDEFINED, /* 175 */
253 TPM_UNDEFINED,
254 TPM_UNDEFINED,
255 TPM_UNDEFINED,
256 TPM_UNDEFINED,
257 TPM_MEDIUM, /* 180 */
258 TPM_SHORT,
259 TPM_MEDIUM,
260 TPM_MEDIUM,
261 TPM_MEDIUM,
262 TPM_MEDIUM, /* 185 */
263 TPM_SHORT,
264 TPM_UNDEFINED,
265 TPM_UNDEFINED,
266 TPM_UNDEFINED,
267 TPM_UNDEFINED, /* 190 */
268 TPM_UNDEFINED,
269 TPM_UNDEFINED,
270 TPM_UNDEFINED,
271 TPM_UNDEFINED,
272 TPM_UNDEFINED, /* 195 */
273 TPM_UNDEFINED,
274 TPM_UNDEFINED,
275 TPM_UNDEFINED,
276 TPM_UNDEFINED,
277 TPM_SHORT, /* 200 */
278 TPM_UNDEFINED,
279 TPM_UNDEFINED,
280 TPM_UNDEFINED,
281 TPM_SHORT,
282 TPM_SHORT, /* 205 */
283 TPM_SHORT,
284 TPM_SHORT,
285 TPM_SHORT,
286 TPM_SHORT,
287 TPM_MEDIUM, /* 210 */
288 TPM_UNDEFINED,
289 TPM_MEDIUM,
290 TPM_MEDIUM,
291 TPM_MEDIUM,
292 TPM_UNDEFINED, /* 215 */
293 TPM_MEDIUM,
294 TPM_UNDEFINED,
295 TPM_UNDEFINED,
296 TPM_SHORT,
297 TPM_SHORT, /* 220 */
298 TPM_SHORT,
299 TPM_SHORT,
300 TPM_SHORT,
301 TPM_SHORT,
302 TPM_UNDEFINED, /* 225 */
303 TPM_UNDEFINED,
304 TPM_UNDEFINED,
305 TPM_UNDEFINED,
306 TPM_UNDEFINED,
307 TPM_SHORT, /* 230 */
308 TPM_LONG,
309 TPM_MEDIUM,
310 TPM_UNDEFINED,
311 TPM_UNDEFINED,
312 TPM_UNDEFINED, /* 235 */
313 TPM_UNDEFINED,
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_SHORT, /* 240 */
318 TPM_UNDEFINED,
319 TPM_MEDIUM,
322 static void user_reader_timeout(unsigned long ptr)
324 struct tpm_chip *chip = (struct tpm_chip *) ptr;
326 schedule_work(&chip->work);
329 static void timeout_work(struct work_struct *work)
331 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
333 mutex_lock(&chip->buffer_mutex);
334 atomic_set(&chip->data_pending, 0);
335 memset(chip->data_buffer, 0, TPM_BUFSIZE);
336 mutex_unlock(&chip->buffer_mutex);
340 * Returns max number of jiffies to wait
342 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
343 u32 ordinal)
345 int duration_idx = TPM_UNDEFINED;
346 int duration = 0;
348 if (ordinal < TPM_MAX_ORDINAL)
349 duration_idx = tpm_ordinal_duration[ordinal];
350 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
351 TPM_MAX_PROTECTED_ORDINAL)
352 duration_idx =
353 tpm_protected_ordinal_duration[ordinal &
354 TPM_PROTECTED_ORDINAL_MASK];
356 if (duration_idx != TPM_UNDEFINED) {
357 duration = chip->vendor.duration[duration_idx];
358 /* if duration is 0, it's because chip->vendor.duration wasn't */
359 /* filled yet, so we set the lowest timeout just to give enough */
360 /* time for tpm_get_timeouts() to succeed */
361 return (duration <= 0 ? HZ : duration);
362 } else
363 return 2 * 60 * HZ;
365 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
368 * Internal kernel interface to transmit TPM commands
370 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
371 size_t bufsiz)
373 ssize_t rc;
374 u32 count, ordinal;
375 unsigned long stop;
377 if (bufsiz > TPM_BUFSIZE)
378 bufsiz = TPM_BUFSIZE;
380 count = be32_to_cpu(*((__be32 *) (buf + 2)));
381 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
382 if (count == 0)
383 return -ENODATA;
384 if (count > bufsiz) {
385 dev_err(chip->dev,
386 "invalid count value %x %zx \n", count, bufsiz);
387 return -E2BIG;
390 mutex_lock(&chip->tpm_mutex);
392 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
393 dev_err(chip->dev,
394 "tpm_transmit: tpm_send: error %zd\n", rc);
395 goto out;
398 if (chip->vendor.irq)
399 goto out_recv;
401 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
402 do {
403 u8 status = chip->vendor.status(chip);
404 if ((status & chip->vendor.req_complete_mask) ==
405 chip->vendor.req_complete_val)
406 goto out_recv;
408 if ((status == chip->vendor.req_canceled)) {
409 dev_err(chip->dev, "Operation Canceled\n");
410 rc = -ECANCELED;
411 goto out;
414 msleep(TPM_TIMEOUT); /* CHECK */
415 rmb();
416 } while (time_before(jiffies, stop));
418 chip->vendor.cancel(chip);
419 dev_err(chip->dev, "Operation Timed out\n");
420 rc = -ETIME;
421 goto out;
423 out_recv:
424 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
425 if (rc < 0)
426 dev_err(chip->dev,
427 "tpm_transmit: tpm_recv: error %zd\n", rc);
428 out:
429 mutex_unlock(&chip->tpm_mutex);
430 return rc;
433 #define TPM_DIGEST_SIZE 20
434 #define TPM_ERROR_SIZE 10
435 #define TPM_RET_CODE_IDX 6
437 enum tpm_capabilities {
438 TPM_CAP_FLAG = cpu_to_be32(4),
439 TPM_CAP_PROP = cpu_to_be32(5),
440 CAP_VERSION_1_1 = cpu_to_be32(0x06),
441 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
444 enum tpm_sub_capabilities {
445 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
446 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
447 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
448 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
449 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
450 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
451 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
455 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
456 int len, const char *desc)
458 int err;
460 len = tpm_transmit(chip,(u8 *) cmd, len);
461 if (len < 0)
462 return len;
463 if (len == TPM_ERROR_SIZE) {
464 err = be32_to_cpu(cmd->header.out.return_code);
465 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
466 return err;
468 return 0;
471 #define TPM_INTERNAL_RESULT_SIZE 200
472 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
473 #define TPM_ORD_GET_CAP cpu_to_be32(101)
475 static const struct tpm_input_header tpm_getcap_header = {
476 .tag = TPM_TAG_RQU_COMMAND,
477 .length = cpu_to_be32(22),
478 .ordinal = TPM_ORD_GET_CAP
481 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
482 const char *desc)
484 struct tpm_cmd_t tpm_cmd;
485 int rc;
486 struct tpm_chip *chip = dev_get_drvdata(dev);
488 tpm_cmd.header.in = tpm_getcap_header;
489 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
490 tpm_cmd.params.getcap_in.cap = subcap_id;
491 /*subcap field not necessary */
492 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
493 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
494 } else {
495 if (subcap_id == TPM_CAP_FLAG_PERM ||
496 subcap_id == TPM_CAP_FLAG_VOL)
497 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
498 else
499 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
500 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
501 tpm_cmd.params.getcap_in.subcap = subcap_id;
503 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
504 if (!rc)
505 *cap = tpm_cmd.params.getcap_out.cap;
506 return rc;
509 void tpm_gen_interrupt(struct tpm_chip *chip)
511 struct tpm_cmd_t tpm_cmd;
512 ssize_t rc;
514 tpm_cmd.header.in = tpm_getcap_header;
515 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
516 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
517 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
519 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
520 "attempting to determine the timeouts");
522 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
524 void tpm_get_timeouts(struct tpm_chip *chip)
526 struct tpm_cmd_t tpm_cmd;
527 struct timeout_t *timeout_cap;
528 struct duration_t *duration_cap;
529 ssize_t rc;
530 u32 timeout;
532 tpm_cmd.header.in = tpm_getcap_header;
533 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
534 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
535 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
537 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
538 "attempting to determine the timeouts");
539 if (rc)
540 goto duration;
542 if (be32_to_cpu(tpm_cmd.header.out.length)
543 != 4 * sizeof(u32))
544 goto duration;
546 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
547 /* Don't overwrite default if value is 0 */
548 timeout = be32_to_cpu(timeout_cap->a);
549 if (timeout)
550 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
551 timeout = be32_to_cpu(timeout_cap->b);
552 if (timeout)
553 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
554 timeout = be32_to_cpu(timeout_cap->c);
555 if (timeout)
556 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
557 timeout = be32_to_cpu(timeout_cap->d);
558 if (timeout)
559 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
561 duration:
562 tpm_cmd.header.in = tpm_getcap_header;
563 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
564 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
565 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
567 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
568 "attempting to determine the durations");
569 if (rc)
570 return;
572 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
573 be32_to_cpu(tpm_cmd.header.out.length)
574 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
575 return;
577 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
578 chip->vendor.duration[TPM_SHORT] =
579 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
580 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
581 * value wrong and apparently reports msecs rather than usecs. So we
582 * fix up the resulting too-small TPM_SHORT value to make things work.
584 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
585 chip->vendor.duration[TPM_SHORT] = HZ;
587 chip->vendor.duration[TPM_MEDIUM] =
588 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
589 chip->vendor.duration[TPM_LONG] =
590 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
592 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
594 void tpm_continue_selftest(struct tpm_chip *chip)
596 u8 data[] = {
597 0, 193, /* TPM_TAG_RQU_COMMAND */
598 0, 0, 0, 10, /* length */
599 0, 0, 0, 83, /* TPM_ORD_GetCapability */
602 tpm_transmit(chip, data, sizeof(data));
604 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
606 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
607 char *buf)
609 cap_t cap;
610 ssize_t rc;
612 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
613 "attempting to determine the permanent enabled state");
614 if (rc)
615 return 0;
617 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
618 return rc;
620 EXPORT_SYMBOL_GPL(tpm_show_enabled);
622 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
623 char *buf)
625 cap_t cap;
626 ssize_t rc;
628 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
629 "attempting to determine the permanent active state");
630 if (rc)
631 return 0;
633 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
634 return rc;
636 EXPORT_SYMBOL_GPL(tpm_show_active);
638 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
639 char *buf)
641 cap_t cap;
642 ssize_t rc;
644 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
645 "attempting to determine the owner state");
646 if (rc)
647 return 0;
649 rc = sprintf(buf, "%d\n", cap.owned);
650 return rc;
652 EXPORT_SYMBOL_GPL(tpm_show_owned);
654 ssize_t tpm_show_temp_deactivated(struct device * dev,
655 struct device_attribute * attr, char *buf)
657 cap_t cap;
658 ssize_t rc;
660 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
661 "attempting to determine the temporary state");
662 if (rc)
663 return 0;
665 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
666 return rc;
668 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
671 * tpm_chip_find_get - return tpm_chip for given chip number
673 static struct tpm_chip *tpm_chip_find_get(int chip_num)
675 struct tpm_chip *pos, *chip = NULL;
677 rcu_read_lock();
678 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
679 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
680 continue;
682 if (try_module_get(pos->dev->driver->owner)) {
683 chip = pos;
684 break;
687 rcu_read_unlock();
688 return chip;
691 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
692 #define READ_PCR_RESULT_SIZE 30
693 static struct tpm_input_header pcrread_header = {
694 .tag = TPM_TAG_RQU_COMMAND,
695 .length = cpu_to_be32(14),
696 .ordinal = TPM_ORDINAL_PCRREAD
699 int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
701 int rc;
702 struct tpm_cmd_t cmd;
704 cmd.header.in = pcrread_header;
705 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
706 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
707 "attempting to read a pcr value");
709 if (rc == 0)
710 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
711 TPM_DIGEST_SIZE);
712 return rc;
716 * tpm_pcr_read - read a pcr value
717 * @chip_num: tpm idx # or ANY
718 * @pcr_idx: pcr idx to retrieve
719 * @res_buf: TPM_PCR value
720 * size of res_buf is 20 bytes (or NULL if you don't care)
722 * The TPM driver should be built-in, but for whatever reason it
723 * isn't, protect against the chip disappearing, by incrementing
724 * the module usage count.
726 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
728 struct tpm_chip *chip;
729 int rc;
731 chip = tpm_chip_find_get(chip_num);
732 if (chip == NULL)
733 return -ENODEV;
734 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
735 module_put(chip->dev->driver->owner);
736 return rc;
738 EXPORT_SYMBOL_GPL(tpm_pcr_read);
741 * tpm_pcr_extend - extend pcr value with hash
742 * @chip_num: tpm idx # or AN&
743 * @pcr_idx: pcr idx to extend
744 * @hash: hash value used to extend pcr value
746 * The TPM driver should be built-in, but for whatever reason it
747 * isn't, protect against the chip disappearing, by incrementing
748 * the module usage count.
750 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
751 #define EXTEND_PCR_RESULT_SIZE 34
752 static struct tpm_input_header pcrextend_header = {
753 .tag = TPM_TAG_RQU_COMMAND,
754 .length = cpu_to_be32(34),
755 .ordinal = TPM_ORD_PCR_EXTEND
758 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
760 struct tpm_cmd_t cmd;
761 int rc;
762 struct tpm_chip *chip;
764 chip = tpm_chip_find_get(chip_num);
765 if (chip == NULL)
766 return -ENODEV;
768 cmd.header.in = pcrextend_header;
769 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
770 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
771 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
772 "attempting extend a PCR value");
774 module_put(chip->dev->driver->owner);
775 return rc;
777 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
779 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
780 char *buf)
782 cap_t cap;
783 u8 digest[TPM_DIGEST_SIZE];
784 ssize_t rc;
785 int i, j, num_pcrs;
786 char *str = buf;
787 struct tpm_chip *chip = dev_get_drvdata(dev);
789 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
790 "attempting to determine the number of PCRS");
791 if (rc)
792 return 0;
794 num_pcrs = be32_to_cpu(cap.num_pcrs);
795 for (i = 0; i < num_pcrs; i++) {
796 rc = __tpm_pcr_read(chip, i, digest);
797 if (rc)
798 break;
799 str += sprintf(str, "PCR-%02d: ", i);
800 for (j = 0; j < TPM_DIGEST_SIZE; j++)
801 str += sprintf(str, "%02X ", digest[j]);
802 str += sprintf(str, "\n");
804 return str - buf;
806 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
808 #define READ_PUBEK_RESULT_SIZE 314
809 #define TPM_ORD_READPUBEK cpu_to_be32(124)
810 struct tpm_input_header tpm_readpubek_header = {
811 .tag = TPM_TAG_RQU_COMMAND,
812 .length = cpu_to_be32(30),
813 .ordinal = TPM_ORD_READPUBEK
816 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
817 char *buf)
819 u8 *data;
820 struct tpm_cmd_t tpm_cmd;
821 ssize_t err;
822 int i, rc;
823 char *str = buf;
825 struct tpm_chip *chip = dev_get_drvdata(dev);
827 tpm_cmd.header.in = tpm_readpubek_header;
828 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
829 "attempting to read the PUBEK");
830 if (err)
831 goto out;
834 ignore header 10 bytes
835 algorithm 32 bits (1 == RSA )
836 encscheme 16 bits
837 sigscheme 16 bits
838 parameters (RSA 12->bytes: keybit, #primes, expbit)
839 keylenbytes 32 bits
840 256 byte modulus
841 ignore checksum 20 bytes
843 data = tpm_cmd.params.readpubek_out_buffer;
844 str +=
845 sprintf(str,
846 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
847 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
848 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
849 "Modulus length: %d\nModulus: \n",
850 data[10], data[11], data[12], data[13], data[14],
851 data[15], data[16], data[17], data[22], data[23],
852 data[24], data[25], data[26], data[27], data[28],
853 data[29], data[30], data[31], data[32], data[33],
854 be32_to_cpu(*((__be32 *) (data + 34))));
856 for (i = 0; i < 256; i++) {
857 str += sprintf(str, "%02X ", data[i + 38]);
858 if ((i + 1) % 16 == 0)
859 str += sprintf(str, "\n");
861 out:
862 rc = str - buf;
863 return rc;
865 EXPORT_SYMBOL_GPL(tpm_show_pubek);
868 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
869 char *buf)
871 cap_t cap;
872 ssize_t rc;
873 char *str = buf;
875 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
876 "attempting to determine the manufacturer");
877 if (rc)
878 return 0;
879 str += sprintf(str, "Manufacturer: 0x%x\n",
880 be32_to_cpu(cap.manufacturer_id));
882 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
883 "attempting to determine the 1.1 version");
884 if (rc)
885 return 0;
886 str += sprintf(str,
887 "TCG version: %d.%d\nFirmware version: %d.%d\n",
888 cap.tpm_version.Major, cap.tpm_version.Minor,
889 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
890 return str - buf;
892 EXPORT_SYMBOL_GPL(tpm_show_caps);
894 ssize_t tpm_show_caps_1_2(struct device * dev,
895 struct device_attribute * attr, char *buf)
897 cap_t cap;
898 ssize_t rc;
899 char *str = buf;
901 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
902 "attempting to determine the manufacturer");
903 if (rc)
904 return 0;
905 str += sprintf(str, "Manufacturer: 0x%x\n",
906 be32_to_cpu(cap.manufacturer_id));
907 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
908 "attempting to determine the 1.2 version");
909 if (rc)
910 return 0;
911 str += sprintf(str,
912 "TCG version: %d.%d\nFirmware version: %d.%d\n",
913 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
914 cap.tpm_version_1_2.revMajor,
915 cap.tpm_version_1_2.revMinor);
916 return str - buf;
918 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
920 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
921 char *buf)
923 struct tpm_chip *chip = dev_get_drvdata(dev);
925 return sprintf(buf, "%d %d %d\n",
926 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
927 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
928 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]));
930 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
932 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
933 const char *buf, size_t count)
935 struct tpm_chip *chip = dev_get_drvdata(dev);
936 if (chip == NULL)
937 return 0;
939 chip->vendor.cancel(chip);
940 return count;
942 EXPORT_SYMBOL_GPL(tpm_store_cancel);
945 * Device file system interface to the TPM
947 * It's assured that the chip will be opened just once,
948 * by the check of is_open variable, which is protected
949 * by driver_lock.
951 int tpm_open(struct inode *inode, struct file *file)
953 int minor = iminor(inode);
954 struct tpm_chip *chip = NULL, *pos;
956 rcu_read_lock();
957 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
958 if (pos->vendor.miscdev.minor == minor) {
959 chip = pos;
960 get_device(chip->dev);
961 break;
964 rcu_read_unlock();
966 if (!chip)
967 return -ENODEV;
969 if (test_and_set_bit(0, &chip->is_open)) {
970 dev_dbg(chip->dev, "Another process owns this TPM\n");
971 put_device(chip->dev);
972 return -EBUSY;
975 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
976 if (chip->data_buffer == NULL) {
977 clear_bit(0, &chip->is_open);
978 put_device(chip->dev);
979 return -ENOMEM;
982 atomic_set(&chip->data_pending, 0);
984 file->private_data = chip;
985 return 0;
987 EXPORT_SYMBOL_GPL(tpm_open);
990 * Called on file close
992 int tpm_release(struct inode *inode, struct file *file)
994 struct tpm_chip *chip = file->private_data;
996 del_singleshot_timer_sync(&chip->user_read_timer);
997 flush_scheduled_work();
998 file->private_data = NULL;
999 atomic_set(&chip->data_pending, 0);
1000 kfree(chip->data_buffer);
1001 clear_bit(0, &chip->is_open);
1002 put_device(chip->dev);
1003 return 0;
1005 EXPORT_SYMBOL_GPL(tpm_release);
1007 ssize_t tpm_write(struct file *file, const char __user *buf,
1008 size_t size, loff_t *off)
1010 struct tpm_chip *chip = file->private_data;
1011 size_t in_size = size, out_size;
1013 /* cannot perform a write until the read has cleared
1014 either via tpm_read or a user_read_timer timeout */
1015 while (atomic_read(&chip->data_pending) != 0)
1016 msleep(TPM_TIMEOUT);
1018 mutex_lock(&chip->buffer_mutex);
1020 if (in_size > TPM_BUFSIZE)
1021 in_size = TPM_BUFSIZE;
1023 if (copy_from_user
1024 (chip->data_buffer, (void __user *) buf, in_size)) {
1025 mutex_unlock(&chip->buffer_mutex);
1026 return -EFAULT;
1029 /* atomic tpm command send and result receive */
1030 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1032 atomic_set(&chip->data_pending, out_size);
1033 mutex_unlock(&chip->buffer_mutex);
1035 /* Set a timeout by which the reader must come claim the result */
1036 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1038 return in_size;
1040 EXPORT_SYMBOL_GPL(tpm_write);
1042 ssize_t tpm_read(struct file *file, char __user *buf,
1043 size_t size, loff_t *off)
1045 struct tpm_chip *chip = file->private_data;
1046 ssize_t ret_size;
1047 int rc;
1049 del_singleshot_timer_sync(&chip->user_read_timer);
1050 flush_scheduled_work();
1051 ret_size = atomic_read(&chip->data_pending);
1052 atomic_set(&chip->data_pending, 0);
1053 if (ret_size > 0) { /* relay data */
1054 if (size < ret_size)
1055 ret_size = size;
1057 mutex_lock(&chip->buffer_mutex);
1058 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1059 memset(chip->data_buffer, 0, ret_size);
1060 if (rc)
1061 ret_size = -EFAULT;
1063 mutex_unlock(&chip->buffer_mutex);
1066 return ret_size;
1068 EXPORT_SYMBOL_GPL(tpm_read);
1070 void tpm_remove_hardware(struct device *dev)
1072 struct tpm_chip *chip = dev_get_drvdata(dev);
1074 if (chip == NULL) {
1075 dev_err(dev, "No device data found\n");
1076 return;
1079 spin_lock(&driver_lock);
1080 list_del_rcu(&chip->list);
1081 spin_unlock(&driver_lock);
1082 synchronize_rcu();
1084 misc_deregister(&chip->vendor.miscdev);
1085 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1086 tpm_bios_log_teardown(chip->bios_dir);
1088 /* write it this way to be explicit (chip->dev == dev) */
1089 put_device(chip->dev);
1091 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1094 * We are about to suspend. Save the TPM state
1095 * so that it can be restored.
1097 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1099 struct tpm_chip *chip = dev_get_drvdata(dev);
1100 u8 savestate[] = {
1101 0, 193, /* TPM_TAG_RQU_COMMAND */
1102 0, 0, 0, 10, /* blob length (in bytes) */
1103 0, 0, 0, 152 /* TPM_ORD_SaveState */
1106 if (chip == NULL)
1107 return -ENODEV;
1109 tpm_transmit(chip, savestate, sizeof(savestate));
1110 return 0;
1112 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1115 * Resume from a power safe. The BIOS already restored
1116 * the TPM state.
1118 int tpm_pm_resume(struct device *dev)
1120 struct tpm_chip *chip = dev_get_drvdata(dev);
1122 if (chip == NULL)
1123 return -ENODEV;
1125 return 0;
1127 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1129 /* In case vendor provided release function, call it too.*/
1131 void tpm_dev_vendor_release(struct tpm_chip *chip)
1133 if (chip->vendor.release)
1134 chip->vendor.release(chip->dev);
1136 clear_bit(chip->dev_num, dev_mask);
1137 kfree(chip->vendor.miscdev.name);
1139 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1143 * Once all references to platform device are down to 0,
1144 * release all allocated structures.
1146 void tpm_dev_release(struct device *dev)
1148 struct tpm_chip *chip = dev_get_drvdata(dev);
1150 tpm_dev_vendor_release(chip);
1152 chip->release(dev);
1153 kfree(chip);
1155 EXPORT_SYMBOL_GPL(tpm_dev_release);
1158 * Called from tpm_<specific>.c probe function only for devices
1159 * the driver has determined it should claim. Prior to calling
1160 * this function the specific probe function has called pci_enable_device
1161 * upon errant exit from this function specific probe function should call
1162 * pci_disable_device
1164 struct tpm_chip *tpm_register_hardware(struct device *dev,
1165 const struct tpm_vendor_specific *entry)
1167 #define DEVNAME_SIZE 7
1169 char *devname;
1170 struct tpm_chip *chip;
1172 /* Driver specific per-device data */
1173 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1174 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1176 if (chip == NULL || devname == NULL)
1177 goto out_free;
1179 mutex_init(&chip->buffer_mutex);
1180 mutex_init(&chip->tpm_mutex);
1181 INIT_LIST_HEAD(&chip->list);
1183 INIT_WORK(&chip->work, timeout_work);
1185 setup_timer(&chip->user_read_timer, user_reader_timeout,
1186 (unsigned long)chip);
1188 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1190 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1192 if (chip->dev_num >= TPM_NUM_DEVICES) {
1193 dev_err(dev, "No available tpm device numbers\n");
1194 goto out_free;
1195 } else if (chip->dev_num == 0)
1196 chip->vendor.miscdev.minor = TPM_MINOR;
1197 else
1198 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1200 set_bit(chip->dev_num, dev_mask);
1202 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1203 chip->vendor.miscdev.name = devname;
1205 chip->vendor.miscdev.parent = dev;
1206 chip->dev = get_device(dev);
1207 chip->release = dev->release;
1208 dev->release = tpm_dev_release;
1209 dev_set_drvdata(dev, chip);
1211 if (misc_register(&chip->vendor.miscdev)) {
1212 dev_err(chip->dev,
1213 "unable to misc_register %s, minor %d\n",
1214 chip->vendor.miscdev.name,
1215 chip->vendor.miscdev.minor);
1216 put_device(chip->dev);
1217 return NULL;
1220 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1221 misc_deregister(&chip->vendor.miscdev);
1222 put_device(chip->dev);
1224 return NULL;
1227 chip->bios_dir = tpm_bios_log_setup(devname);
1229 /* Make chip available */
1230 spin_lock(&driver_lock);
1231 list_add_rcu(&chip->list, &tpm_chip_list);
1232 spin_unlock(&driver_lock);
1234 return chip;
1236 out_free:
1237 kfree(chip);
1238 kfree(devname);
1239 return NULL;
1241 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1243 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1244 MODULE_DESCRIPTION("TPM Driver");
1245 MODULE_VERSION("2.0");
1246 MODULE_LICENSE("GPL");