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