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