7f77ca3b6f1b2ce4080267afa5cd6b3a079fb033
[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 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 /**
446  * tpm_continue_selftest -- run TPM's selftest
447  * @chip: TPM chip to use
448  *
449  * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
450  * a TPM error code.
451  */
452 int tpm_continue_selftest(struct tpm_chip *chip)
453 {
454         int rc;
455         struct tpm_cmd_t cmd;
456
457         cmd.header.in = continue_selftest_header;
458         rc = tpm_transmit(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE);
459         return rc;
460 }
461 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
462
463 /* The maximum time in milliseconds that the TPM self test will take to
464  * complete.  TODO(semenzato): 1s should be plenty for all TPMs, but how can we
465  * ensure it?
466  */
467 #define TPM_SELF_TEST_DURATION_MSEC 1000
468
469 static void resume_if_needed(struct tpm_chip *chip)
470 {
471         mutex_lock(&chip->resume_mutex);
472         if (chip->needs_resume) {
473                 /* If it's been TPM_SELF_TEST_DURATION_MSEC msec since resume,
474                  * then selftest has completed and we don't need to wait.
475                  */
476                 if (jiffies - chip->resume_time <
477                     msecs_to_jiffies(TPM_SELF_TEST_DURATION_MSEC)) {
478                         dev_info(chip->dev, "waiting for TPM self test");
479                         tpm_continue_selftest(chip);
480                 }
481                 chip->needs_resume = 0;
482                 dev_info(chip->dev, "TPM delayed resume completed");
483         }
484         mutex_unlock(&chip->resume_mutex);
485 }
486
487 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
488                             int len, const char *desc)
489 {
490         int err;
491
492         resume_if_needed(chip);
493
494         len = tpm_transmit(chip,(u8 *) cmd, len);
495         if (len <  0)
496                 return len;
497         else if (len < TPM_HEADER_SIZE)
498                 return -EFAULT;
499
500         err = be32_to_cpu(cmd->header.out.return_code);
501         if (err != 0)
502                 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
503
504         return err;
505 }
506
507 /*
508  * Returns max number of jiffies to wait
509  */
510 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
511                                            u32 ordinal)
512 {
513         int duration_idx = TPM_UNDEFINED;
514         int duration = 0;
515
516         if (ordinal < TPM_MAX_ORDINAL)
517                 duration_idx = tpm_ordinal_duration[ordinal];
518         else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
519                  TPM_MAX_PROTECTED_ORDINAL)
520                 duration_idx =
521                     tpm_protected_ordinal_duration[ordinal &
522                                                    TPM_PROTECTED_ORDINAL_MASK];
523
524         if (duration_idx != TPM_UNDEFINED)
525                 duration = chip->vendor.duration[duration_idx];
526         if (duration <= 0)
527                 return 2 * 60 * HZ;
528         else
529                 return duration;
530 }
531 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
532
533 #define TPM_DIGEST_SIZE 20
534 #define TPM_RET_CODE_IDX 6
535
536 enum tpm_capabilities {
537         TPM_CAP_FLAG = cpu_to_be32(4),
538         TPM_CAP_PROP = cpu_to_be32(5),
539         CAP_VERSION_1_1 = cpu_to_be32(0x06),
540         CAP_VERSION_1_2 = cpu_to_be32(0x1A)
541 };
542
543 enum tpm_sub_capabilities {
544         TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
545         TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
546         TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
547         TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
548         TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
549         TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
550         TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
551
552 };
553
554 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
555                    const char *desc)
556 {
557         struct tpm_cmd_t tpm_cmd;
558         int rc;
559         struct tpm_chip *chip = dev_get_drvdata(dev);
560
561         tpm_cmd.header.in = tpm_getcap_header;
562         if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
563                 tpm_cmd.params.getcap_in.cap = subcap_id;
564                 /*subcap field not necessary */
565                 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
566                 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
567         } else {
568                 if (subcap_id == TPM_CAP_FLAG_PERM ||
569                     subcap_id == TPM_CAP_FLAG_VOL)
570                         tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
571                 else
572                         tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
573                 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
574                 tpm_cmd.params.getcap_in.subcap = subcap_id;
575         }
576         rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
577         if (!rc)
578                 *cap = tpm_cmd.params.getcap_out.cap;
579         return rc;
580 }
581
582 void tpm_gen_interrupt(struct tpm_chip *chip)
583 {
584         struct  tpm_cmd_t tpm_cmd;
585         ssize_t rc;
586
587         tpm_cmd.header.in = tpm_getcap_header;
588         tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
589         tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
590         tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
591
592         rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
593                         "attempting to determine the timeouts");
594 }
595 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
596
597 int tpm_get_timeouts(struct tpm_chip *chip)
598 {
599         struct tpm_cmd_t tpm_cmd;
600         struct timeout_t *timeout_cap;
601         struct duration_t *duration_cap;
602         ssize_t rc;
603         u32 timeout;
604         unsigned int scale = 1;
605
606         tpm_cmd.header.in = tpm_getcap_header;
607         tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
608         tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
609         tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
610
611         rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
612                         "attempting to determine the timeouts");
613         if (rc)
614                 goto duration;
615
616         if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
617             be32_to_cpu(tpm_cmd.header.out.length)
618             != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
619                 return -EINVAL;
620
621         timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
622         /* Don't overwrite default if value is 0 */
623         timeout = be32_to_cpu(timeout_cap->a);
624         if (timeout && timeout < 1000) {
625                 /* timeouts in msec rather usec */
626                 scale = 1000;
627                 chip->vendor.timeout_adjusted = true;
628         }
629         if (timeout)
630                 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
631         timeout = be32_to_cpu(timeout_cap->b);
632         if (timeout)
633                 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
634         timeout = be32_to_cpu(timeout_cap->c);
635         if (timeout)
636                 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
637         timeout = be32_to_cpu(timeout_cap->d);
638         if (timeout)
639                 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
640
641 duration:
642         tpm_cmd.header.in = tpm_getcap_header;
643         tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
644         tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
645         tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
646
647         rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
648                         "attempting to determine the durations");
649         if (rc)
650                 return rc;
651
652         if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
653             be32_to_cpu(tpm_cmd.header.out.length)
654             != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
655                 return -EINVAL;
656
657         duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
658         chip->vendor.duration[TPM_SHORT] =
659             usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
660         chip->vendor.duration[TPM_MEDIUM] =
661             usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
662         chip->vendor.duration[TPM_LONG] =
663             usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
664
665         /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
666          * value wrong and apparently reports msecs rather than usecs. So we
667          * fix up the resulting too-small TPM_SHORT value to make things work.
668          * We also scale the TPM_MEDIUM and -_LONG values by 1000.
669          */
670         if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
671                 chip->vendor.duration[TPM_SHORT] = HZ;
672                 chip->vendor.duration[TPM_MEDIUM] *= 1000;
673                 chip->vendor.duration[TPM_LONG] *= 1000;
674                 chip->vendor.duration_adjusted = true;
675                 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
676         }
677         return 0;
678 }
679 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
680
681 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
682                         char *buf)
683 {
684         cap_t cap;
685         ssize_t rc;
686
687         rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
688                          "attempting to determine the permanent enabled state");
689         if (rc)
690                 return 0;
691
692         rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
693         return rc;
694 }
695 EXPORT_SYMBOL_GPL(tpm_show_enabled);
696
697 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
698                         char *buf)
699 {
700         cap_t cap;
701         ssize_t rc;
702
703         rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
704                          "attempting to determine the permanent active state");
705         if (rc)
706                 return 0;
707
708         rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
709         return rc;
710 }
711 EXPORT_SYMBOL_GPL(tpm_show_active);
712
713 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
714                         char *buf)
715 {
716         cap_t cap;
717         ssize_t rc;
718
719         rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
720                          "attempting to determine the owner state");
721         if (rc)
722                 return 0;
723
724         rc = sprintf(buf, "%d\n", cap.owned);
725         return rc;
726 }
727 EXPORT_SYMBOL_GPL(tpm_show_owned);
728
729 ssize_t tpm_show_temp_deactivated(struct device * dev,
730                                 struct device_attribute * attr, char *buf)
731 {
732         cap_t cap;
733         ssize_t rc;
734
735         rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
736                          "attempting to determine the temporary state");
737         if (rc)
738                 return 0;
739
740         rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
741         return rc;
742 }
743 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
744
745 /*
746  * tpm_chip_find_get - return tpm_chip for given chip number
747  */
748 static struct tpm_chip *tpm_chip_find_get(int chip_num)
749 {
750         struct tpm_chip *pos, *chip = NULL;
751
752         rcu_read_lock();
753         list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
754                 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
755                         continue;
756
757                 if (try_module_get(pos->dev->driver->owner)) {
758                         chip = pos;
759                         break;
760                 }
761         }
762         rcu_read_unlock();
763         return chip;
764 }
765
766 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
767 #define READ_PCR_RESULT_SIZE 30
768 static struct tpm_input_header pcrread_header = {
769         .tag = TPM_TAG_RQU_COMMAND,
770         .length = cpu_to_be32(14),
771         .ordinal = TPM_ORDINAL_PCRREAD
772 };
773
774 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
775 {
776         int rc;
777         struct tpm_cmd_t cmd;
778
779         cmd.header.in = pcrread_header;
780         cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
781         rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
782                           "attempting to read a pcr value");
783
784         if (rc == 0)
785                 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
786                        TPM_DIGEST_SIZE);
787         return rc;
788 }
789
790 /**
791  * tpm_pcr_read - read a pcr value
792  * @chip_num:   tpm idx # or ANY
793  * @pcr_idx:    pcr idx to retrieve
794  * @res_buf:    TPM_PCR value
795  *              size of res_buf is 20 bytes (or NULL if you don't care)
796  *
797  * The TPM driver should be built-in, but for whatever reason it
798  * isn't, protect against the chip disappearing, by incrementing
799  * the module usage count.
800  */
801 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
802 {
803         struct tpm_chip *chip;
804         int rc;
805
806         chip = tpm_chip_find_get(chip_num);
807         if (chip == NULL)
808                 return -ENODEV;
809         rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
810         tpm_chip_put(chip);
811         return rc;
812 }
813 EXPORT_SYMBOL_GPL(tpm_pcr_read);
814
815 /**
816  * tpm_pcr_extend - extend pcr value with hash
817  * @chip_num:   tpm idx # or AN&
818  * @pcr_idx:    pcr idx to extend
819  * @hash:       hash value used to extend pcr value
820  *
821  * The TPM driver should be built-in, but for whatever reason it
822  * isn't, protect against the chip disappearing, by incrementing
823  * the module usage count.
824  */
825 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
826 #define EXTEND_PCR_RESULT_SIZE 34
827 static struct tpm_input_header pcrextend_header = {
828         .tag = TPM_TAG_RQU_COMMAND,
829         .length = cpu_to_be32(34),
830         .ordinal = TPM_ORD_PCR_EXTEND
831 };
832
833 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
834 {
835         struct tpm_cmd_t cmd;
836         int rc;
837         struct tpm_chip *chip;
838
839         chip = tpm_chip_find_get(chip_num);
840         if (chip == NULL)
841                 return -ENODEV;
842
843         cmd.header.in = pcrextend_header;
844         cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
845         memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
846         rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
847                           "attempting extend a PCR value");
848
849         tpm_chip_put(chip);
850         return rc;
851 }
852 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
853
854 /**
855  * tpm_do_selftest - have the TPM continue its selftest and wait until it
856  *                   can receive further commands
857  * @chip: TPM chip to use
858  *
859  * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
860  * a TPM error code.
861  */
862 int tpm_do_selftest(struct tpm_chip *chip)
863 {
864         int rc;
865         u8 digest[TPM_DIGEST_SIZE];
866         unsigned int loops;
867         unsigned int delay_msec = 1000;
868         unsigned long duration;
869
870         duration = tpm_calc_ordinal_duration(chip,
871                                              TPM_ORD_CONTINUE_SELFTEST);
872
873         loops = jiffies_to_msecs(duration) / delay_msec;
874
875         rc = tpm_continue_selftest(chip);
876         /* This may fail if there was no TPM driver during a suspend/resume
877          * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
878          */
879         if (rc)
880                 return rc;
881
882         do {
883                 rc = __tpm_pcr_read(chip, 0, digest);
884                 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
885                         dev_info(chip->dev,
886                                  "TPM is disabled/deactivated (0x%X)\n", rc);
887                         /* TPM is disabled and/or deactivated; driver can
888                          * proceed and TPM does handle commands for
889                          * suspend/resume correctly
890                          */
891                         return 0;
892                 }
893                 if (rc != TPM_WARN_DOING_SELFTEST)
894                         return rc;
895                 msleep(delay_msec);
896         } while (--loops > 0);
897
898         return rc;
899 }
900 EXPORT_SYMBOL_GPL(tpm_do_selftest);
901
902 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
903 {
904         struct tpm_chip *chip;
905         int rc;
906
907         chip = tpm_chip_find_get(chip_num);
908         if (chip == NULL)
909                 return -ENODEV;
910
911         rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
912
913         tpm_chip_put(chip);
914         return rc;
915 }
916 EXPORT_SYMBOL_GPL(tpm_send);
917
918 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
919                       char *buf)
920 {
921         cap_t cap;
922         u8 digest[TPM_DIGEST_SIZE];
923         ssize_t rc;
924         int i, j, num_pcrs;
925         char *str = buf;
926         struct tpm_chip *chip = dev_get_drvdata(dev);
927
928         rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
929                         "attempting to determine the number of PCRS");
930         if (rc)
931                 return 0;
932
933         num_pcrs = be32_to_cpu(cap.num_pcrs);
934         for (i = 0; i < num_pcrs; i++) {
935                 rc = __tpm_pcr_read(chip, i, digest);
936                 if (rc)
937                         break;
938                 str += sprintf(str, "PCR-%02d: ", i);
939                 for (j = 0; j < TPM_DIGEST_SIZE; j++)
940                         str += sprintf(str, "%02X ", digest[j]);
941                 str += sprintf(str, "\n");
942         }
943         return str - buf;
944 }
945 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
946
947 #define  READ_PUBEK_RESULT_SIZE 314
948 #define TPM_ORD_READPUBEK cpu_to_be32(124)
949 struct tpm_input_header tpm_readpubek_header = {
950         .tag = TPM_TAG_RQU_COMMAND,
951         .length = cpu_to_be32(30),
952         .ordinal = TPM_ORD_READPUBEK
953 };
954
955 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
956                        char *buf)
957 {
958         u8 *data;
959         struct tpm_cmd_t tpm_cmd;
960         ssize_t err;
961         int i, rc;
962         char *str = buf;
963
964         struct tpm_chip *chip = dev_get_drvdata(dev);
965
966         tpm_cmd.header.in = tpm_readpubek_header;
967         err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
968                         "attempting to read the PUBEK");
969         if (err)
970                 goto out;
971
972         /* 
973            ignore header 10 bytes
974            algorithm 32 bits (1 == RSA )
975            encscheme 16 bits
976            sigscheme 16 bits
977            parameters (RSA 12->bytes: keybit, #primes, expbit)  
978            keylenbytes 32 bits
979            256 byte modulus
980            ignore checksum 20 bytes
981          */
982         data = tpm_cmd.params.readpubek_out_buffer;
983         str +=
984             sprintf(str,
985                     "Algorithm: %02X %02X %02X %02X\n"
986                     "Encscheme: %02X %02X\n"
987                     "Sigscheme: %02X %02X\n"
988                     "Parameters: %02X %02X %02X %02X "
989                     "%02X %02X %02X %02X "
990                     "%02X %02X %02X %02X\n"
991                     "Modulus length: %d\n"
992                     "Modulus:\n",
993                     data[0], data[1], data[2], data[3],
994                     data[4], data[5],
995                     data[6], data[7],
996                     data[12], data[13], data[14], data[15],
997                     data[16], data[17], data[18], data[19],
998                     data[20], data[21], data[22], data[23],
999                     be32_to_cpu(*((__be32 *) (data + 24))));
1000
1001         for (i = 0; i < 256; i++) {
1002                 str += sprintf(str, "%02X ", data[i + 28]);
1003                 if ((i + 1) % 16 == 0)
1004                         str += sprintf(str, "\n");
1005         }
1006 out:
1007         rc = str - buf;
1008         return rc;
1009 }
1010 EXPORT_SYMBOL_GPL(tpm_show_pubek);
1011
1012
1013 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
1014                       char *buf)
1015 {
1016         cap_t cap;
1017         ssize_t rc;
1018         char *str = buf;
1019
1020         rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1021                         "attempting to determine the manufacturer");
1022         if (rc)
1023                 return 0;
1024         str += sprintf(str, "Manufacturer: 0x%x\n",
1025                        be32_to_cpu(cap.manufacturer_id));
1026
1027         rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1028                         "attempting to determine the 1.1 version");
1029         if (rc)
1030                 return 0;
1031         str += sprintf(str,
1032                        "TCG version: %d.%d\nFirmware version: %d.%d\n",
1033                        cap.tpm_version.Major, cap.tpm_version.Minor,
1034                        cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1035         return str - buf;
1036 }
1037 EXPORT_SYMBOL_GPL(tpm_show_caps);
1038
1039 ssize_t tpm_show_caps_1_2(struct device * dev,
1040                           struct device_attribute * attr, char *buf)
1041 {
1042         cap_t cap;
1043         ssize_t rc;
1044         char *str = buf;
1045
1046         rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1047                         "attempting to determine the manufacturer");
1048         if (rc)
1049                 return 0;
1050         str += sprintf(str, "Manufacturer: 0x%x\n",
1051                        be32_to_cpu(cap.manufacturer_id));
1052         rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1053                          "attempting to determine the 1.2 version");
1054         if (rc)
1055                 return 0;
1056         str += sprintf(str,
1057                        "TCG version: %d.%d\nFirmware version: %d.%d\n",
1058                        cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1059                        cap.tpm_version_1_2.revMajor,
1060                        cap.tpm_version_1_2.revMinor);
1061         return str - buf;
1062 }
1063 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1064
1065 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1066                           char *buf)
1067 {
1068         struct tpm_chip *chip = dev_get_drvdata(dev);
1069
1070         if (chip->vendor.duration[TPM_LONG] == 0)
1071                 return 0;
1072
1073         return sprintf(buf, "%d %d %d [%s]\n",
1074                        jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1075                        jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1076                        jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1077                        chip->vendor.duration_adjusted
1078                        ? "adjusted" : "original");
1079 }
1080 EXPORT_SYMBOL_GPL(tpm_show_durations);
1081
1082 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1083                           char *buf)
1084 {
1085         struct tpm_chip *chip = dev_get_drvdata(dev);
1086
1087         return sprintf(buf, "%d %d %d %d [%s]\n",
1088                        jiffies_to_usecs(chip->vendor.timeout_a),
1089                        jiffies_to_usecs(chip->vendor.timeout_b),
1090                        jiffies_to_usecs(chip->vendor.timeout_c),
1091                        jiffies_to_usecs(chip->vendor.timeout_d),
1092                        chip->vendor.timeout_adjusted
1093                        ? "adjusted" : "original");
1094 }
1095 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1096
1097 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1098                         const char *buf, size_t count)
1099 {
1100         struct tpm_chip *chip = dev_get_drvdata(dev);
1101         if (chip == NULL)
1102                 return 0;
1103
1104         chip->vendor.cancel(chip);
1105         return count;
1106 }
1107 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1108
1109 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1110                          wait_queue_head_t *queue)
1111 {
1112         unsigned long stop;
1113         long rc;
1114         u8 status;
1115
1116         /* check current status */
1117         status = chip->vendor.status(chip);
1118         if ((status & mask) == mask)
1119                 return 0;
1120
1121         stop = jiffies + timeout;
1122
1123         if (chip->vendor.irq) {
1124 again:
1125                 timeout = stop - jiffies;
1126                 if ((long)timeout <= 0)
1127                         return -ETIME;
1128                 rc = wait_event_interruptible_timeout(*queue,
1129                                                       ((chip->vendor.status(chip)
1130                                                       & mask) == mask),
1131                                                       timeout);
1132                 if (rc > 0)
1133                         return 0;
1134                 if (rc == -ERESTARTSYS && freezing(current)) {
1135                         clear_thread_flag(TIF_SIGPENDING);
1136                         goto again;
1137                 }
1138         } else {
1139                 do {
1140                         msleep(TPM_TIMEOUT);
1141                         status = chip->vendor.status(chip);
1142                         if ((status & mask) == mask)
1143                                 return 0;
1144                 } while (time_before(jiffies, stop));
1145         }
1146         return -ETIME;
1147 }
1148 EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1149 /*
1150  * Device file system interface to the TPM
1151  *
1152  * It's assured that the chip will be opened just once,
1153  * by the check of is_open variable, which is protected
1154  * by driver_lock.
1155  */
1156 int tpm_open(struct inode *inode, struct file *file)
1157 {
1158         int minor = iminor(inode);
1159         struct tpm_chip *chip = NULL, *pos;
1160
1161         rcu_read_lock();
1162         list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1163                 if (pos->vendor.miscdev.minor == minor) {
1164                         chip = pos;
1165                         get_device(chip->dev);
1166                         break;
1167                 }
1168         }
1169         rcu_read_unlock();
1170
1171         if (!chip)
1172                 return -ENODEV;
1173
1174         if (test_and_set_bit(0, &chip->is_open)) {
1175                 dev_dbg(chip->dev, "Another process owns this TPM\n");
1176                 put_device(chip->dev);
1177                 return -EBUSY;
1178         }
1179
1180         chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1181         if (chip->data_buffer == NULL) {
1182                 clear_bit(0, &chip->is_open);
1183                 put_device(chip->dev);
1184                 return -ENOMEM;
1185         }
1186
1187         atomic_set(&chip->data_pending, 0);
1188
1189         file->private_data = chip;
1190         return 0;
1191 }
1192 EXPORT_SYMBOL_GPL(tpm_open);
1193
1194 /*
1195  * Called on file close
1196  */
1197 int tpm_release(struct inode *inode, struct file *file)
1198 {
1199         struct tpm_chip *chip = file->private_data;
1200
1201         del_singleshot_timer_sync(&chip->user_read_timer);
1202         flush_work_sync(&chip->work);
1203         file->private_data = NULL;
1204         atomic_set(&chip->data_pending, 0);
1205         kfree(chip->data_buffer);
1206         clear_bit(0, &chip->is_open);
1207         put_device(chip->dev);
1208         return 0;
1209 }
1210 EXPORT_SYMBOL_GPL(tpm_release);
1211
1212 ssize_t tpm_write(struct file *file, const char __user *buf,
1213                   size_t size, loff_t *off)
1214 {
1215         struct tpm_chip *chip = file->private_data;
1216         size_t in_size = size, out_size;
1217
1218         /* cannot perform a write until the read has cleared
1219            either via tpm_read or a user_read_timer timeout */
1220         while (atomic_read(&chip->data_pending) != 0)
1221                 msleep(TPM_TIMEOUT);
1222
1223         resume_if_needed(chip);
1224
1225         mutex_lock(&chip->buffer_mutex);
1226
1227         if (in_size > TPM_BUFSIZE)
1228                 in_size = TPM_BUFSIZE;
1229
1230         if (copy_from_user
1231             (chip->data_buffer, (void __user *) buf, in_size)) {
1232                 mutex_unlock(&chip->buffer_mutex);
1233                 return -EFAULT;
1234         }
1235
1236         /* atomic tpm command send and result receive */
1237         out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1238
1239         atomic_set(&chip->data_pending, out_size);
1240         mutex_unlock(&chip->buffer_mutex);
1241
1242         /* Set a timeout by which the reader must come claim the result */
1243         mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1244
1245         return in_size;
1246 }
1247 EXPORT_SYMBOL_GPL(tpm_write);
1248
1249 ssize_t tpm_read(struct file *file, char __user *buf,
1250                  size_t size, loff_t *off)
1251 {
1252         struct tpm_chip *chip = file->private_data;
1253         ssize_t ret_size;
1254         int rc;
1255
1256         del_singleshot_timer_sync(&chip->user_read_timer);
1257         flush_work_sync(&chip->work);
1258         ret_size = atomic_read(&chip->data_pending);
1259         /* TODO(wad): atomic_set should come AFTER the buffer is copied. */
1260         atomic_set(&chip->data_pending, 0);
1261         if (ret_size > 0) {     /* relay data */
1262                 ssize_t orig_ret_size = ret_size;
1263                 if (size < ret_size)
1264                         ret_size = size;
1265
1266                 mutex_lock(&chip->buffer_mutex);
1267                 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1268                 memset(chip->data_buffer, 0, orig_ret_size);
1269                 if (rc)
1270                         ret_size = -EFAULT;
1271
1272                 mutex_unlock(&chip->buffer_mutex);
1273         }
1274
1275         return ret_size;
1276 }
1277 EXPORT_SYMBOL_GPL(tpm_read);
1278
1279 void tpm_remove_hardware(struct device *dev)
1280 {
1281         struct tpm_chip *chip = dev_get_drvdata(dev);
1282
1283         if (chip == NULL) {
1284                 dev_err(dev, "No device data found\n");
1285                 return;
1286         }
1287
1288         spin_lock(&driver_lock);
1289         list_del_rcu(&chip->list);
1290         spin_unlock(&driver_lock);
1291         synchronize_rcu();
1292
1293         misc_deregister(&chip->vendor.miscdev);
1294         sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1295         tpm_bios_log_teardown(chip->bios_dir);
1296
1297         /* write it this way to be explicit (chip->dev == dev) */
1298         put_device(chip->dev);
1299 }
1300 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1301
1302 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1303 #define SAVESTATE_RESULT_SIZE 10
1304
1305 static struct tpm_input_header savestate_header = {
1306         .tag = TPM_TAG_RQU_COMMAND,
1307         .length = cpu_to_be32(10),
1308         .ordinal = TPM_ORD_SAVESTATE
1309 };
1310
1311 /*
1312  * We are about to suspend. Save the TPM state
1313  * so that it can be restored.
1314  */
1315 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1316 {
1317         struct tpm_chip *chip = dev_get_drvdata(dev);
1318         struct tpm_cmd_t cmd;
1319         int rc;
1320
1321         u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1322
1323         if (chip == NULL)
1324                 return -ENODEV;
1325
1326         /* for buggy tpm, flush pcrs with extend to selected dummy */
1327         if (tpm_suspend_pcr) {
1328                 cmd.header.in = pcrextend_header;
1329                 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1330                 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1331                        TPM_DIGEST_SIZE);
1332                 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1333                                   "extending dummy pcr before suspend");
1334         }
1335
1336         /* now do the actual savestate */
1337         cmd.header.in = savestate_header;
1338         rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1339                           "sending savestate before suspend");
1340         return rc;
1341 }
1342 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1343
1344 /*
1345  * Resume from a power safe. The BIOS already restored
1346  * the TPM state.
1347  */
1348 int tpm_pm_resume(struct device *dev)
1349 {
1350         struct tpm_chip *chip = dev_get_drvdata(dev);
1351
1352         if (chip == NULL)
1353                 return -ENODEV;
1354
1355         needs_resume(chip);
1356         return 0;
1357 }
1358 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1359
1360 /* In case vendor provided release function, call it too.*/
1361
1362 void tpm_dev_vendor_release(struct tpm_chip *chip)
1363 {
1364         if (chip->vendor.release)
1365                 chip->vendor.release(chip->dev);
1366
1367         clear_bit(chip->dev_num, dev_mask);
1368         kfree(chip->vendor.miscdev.name);
1369 }
1370 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1371
1372
1373 /*
1374  * Once all references to platform device are down to 0,
1375  * release all allocated structures.
1376  */
1377 void tpm_dev_release(struct device *dev)
1378 {
1379         struct tpm_chip *chip = dev_get_drvdata(dev);
1380
1381         tpm_dev_vendor_release(chip);
1382
1383         chip->release(dev);
1384         kfree(chip);
1385 }
1386 EXPORT_SYMBOL_GPL(tpm_dev_release);
1387
1388 /*
1389  * Called from tpm_<specific>.c probe function only for devices 
1390  * the driver has determined it should claim.  Prior to calling
1391  * this function the specific probe function has called pci_enable_device
1392  * upon errant exit from this function specific probe function should call
1393  * pci_disable_device
1394  */
1395 struct tpm_chip *tpm_register_hardware(struct device *dev,
1396                                         const struct tpm_vendor_specific *entry)
1397 {
1398 #define DEVNAME_SIZE 7
1399
1400         char *devname;
1401         struct tpm_chip *chip;
1402
1403         /* Driver specific per-device data */
1404         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1405         devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1406
1407         if (chip == NULL || devname == NULL)
1408                 goto out_free;
1409
1410         mutex_init(&chip->buffer_mutex);
1411         mutex_init(&chip->tpm_mutex);
1412         mutex_init(&chip->resume_mutex);
1413         INIT_LIST_HEAD(&chip->list);
1414
1415         INIT_WORK(&chip->work, timeout_work);
1416
1417         setup_timer(&chip->user_read_timer, user_reader_timeout,
1418                         (unsigned long)chip);
1419
1420         memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1421
1422         chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1423
1424         if (chip->dev_num >= TPM_NUM_DEVICES) {
1425                 dev_err(dev, "No available tpm device numbers\n");
1426                 goto out_free;
1427         } else if (chip->dev_num == 0)
1428                 chip->vendor.miscdev.minor = TPM_MINOR;
1429         else
1430                 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1431
1432         set_bit(chip->dev_num, dev_mask);
1433
1434         scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1435         chip->vendor.miscdev.name = devname;
1436
1437         chip->vendor.miscdev.parent = dev;
1438         chip->dev = get_device(dev);
1439         chip->release = dev->release;
1440         dev->release = tpm_dev_release;
1441         dev_set_drvdata(dev, chip);
1442
1443         if (misc_register(&chip->vendor.miscdev)) {
1444                 dev_err(chip->dev,
1445                         "unable to misc_register %s, minor %d\n",
1446                         chip->vendor.miscdev.name,
1447                         chip->vendor.miscdev.minor);
1448                 put_device(chip->dev);
1449                 return NULL;
1450         }
1451
1452         if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1453                 misc_deregister(&chip->vendor.miscdev);
1454                 put_device(chip->dev);
1455
1456                 return NULL;
1457         }
1458
1459         chip->bios_dir = tpm_bios_log_setup(devname);
1460
1461         /* Make chip available */
1462         spin_lock(&driver_lock);
1463         list_add_rcu(&chip->list, &tpm_chip_list);
1464         spin_unlock(&driver_lock);
1465
1466         return chip;
1467
1468 out_free:
1469         kfree(chip);
1470         kfree(devname);
1471         return NULL;
1472 }
1473 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1474
1475 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1476 MODULE_DESCRIPTION("TPM Driver");
1477 MODULE_VERSION("2.0");
1478 MODULE_LICENSE("GPL");