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