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