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