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