2 * Copyright (C) 2004 IBM Corporation
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>
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
32 TPM_MINOR = 224, /* officially assigned */
34 TPM_NUM_DEVICES = 256,
35 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
38 static LIST_HEAD(tpm_chip_list);
39 static DEFINE_SPINLOCK(driver_lock);
40 static int dev_mask[TPM_NUM_MASK_ENTRIES];
42 static void user_reader_timeout(unsigned long ptr)
44 struct tpm_chip *chip = (struct tpm_chip *) ptr;
46 down(&chip->buffer_mutex);
47 atomic_set(&chip->data_pending, 0);
48 memset(chip->data_buffer, 0, TPM_BUFSIZE);
49 up(&chip->buffer_mutex);
53 * Internal kernel interface to transmit TPM commands
55 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
62 count = be32_to_cpu(*((__be32 *) (buf + 2)));
67 dev_err(&chip->pci_dev->dev,
68 "invalid count value %x %zx \n", count, bufsiz);
72 down(&chip->tpm_mutex);
74 if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
75 dev_err(&chip->pci_dev->dev,
76 "tpm_transmit: tpm_send: error %zd\n", rc);
80 stop = jiffies + 2 * 60 * HZ;
82 u8 status = inb(chip->vendor->base + 1);
83 if ((status & chip->vendor->req_complete_mask) ==
84 chip->vendor->req_complete_val) {
88 if ((status == chip->vendor->req_canceled)) {
89 dev_err(&chip->pci_dev->dev, "Operation Canceled\n");
94 msleep(TPM_TIMEOUT); /* CHECK */
96 } while (time_before(jiffies, stop));
99 chip->vendor->cancel(chip);
100 dev_err(&chip->pci_dev->dev, "Operation Timed out\n");
105 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
107 dev_err(&chip->pci_dev->dev,
108 "tpm_transmit: tpm_recv: error %zd\n", rc);
110 up(&chip->tpm_mutex);
114 #define TPM_DIGEST_SIZE 20
115 #define CAP_PCR_RESULT_SIZE 18
116 static const u8 cap_pcr[] = {
117 0, 193, /* TPM_TAG_RQU_COMMAND */
118 0, 0, 0, 22, /* length */
119 0, 0, 0, 101, /* TPM_ORD_GetCapability */
125 #define READ_PCR_RESULT_SIZE 30
126 static const u8 pcrread[] = {
127 0, 193, /* TPM_TAG_RQU_COMMAND */
128 0, 0, 0, 14, /* length */
129 0, 0, 0, 21, /* TPM_ORD_PcrRead */
130 0, 0, 0, 0 /* PCR index */
133 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
136 u8 data[READ_PCR_RESULT_SIZE];
142 struct tpm_chip *chip =
143 pci_get_drvdata(to_pci_dev(dev));
147 memcpy(data, cap_pcr, sizeof(cap_pcr));
148 if ((len = tpm_transmit(chip, data, sizeof(data)))
149 < CAP_PCR_RESULT_SIZE) {
150 dev_err(&chip->pci_dev->dev, "A TPM error (%d) occurred "
151 "attempting to determine the number of PCRS\n",
152 be32_to_cpu(*((__be32 *) (data + 6))));
156 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
158 for (i = 0; i < num_pcrs; i++) {
159 memcpy(data, pcrread, sizeof(pcrread));
160 index = cpu_to_be32(i);
161 memcpy(data + 10, &index, 4);
162 if ((len = tpm_transmit(chip, data, sizeof(data)))
163 < READ_PCR_RESULT_SIZE){
164 dev_err(&chip->pci_dev->dev, "A TPM error (%d) occurred"
165 " attempting to read PCR %d of %d\n",
166 be32_to_cpu(*((__be32 *) (data + 6))), i, num_pcrs);
169 str += sprintf(str, "PCR-%02d: ", i);
170 for (j = 0; j < TPM_DIGEST_SIZE; j++)
171 str += sprintf(str, "%02X ", *(data + 10 + j));
172 str += sprintf(str, "\n");
177 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
179 #define READ_PUBEK_RESULT_SIZE 314
180 static const u8 readpubek[] = {
181 0, 193, /* TPM_TAG_RQU_COMMAND */
182 0, 0, 0, 30, /* length */
183 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
186 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
194 struct tpm_chip *chip =
195 pci_get_drvdata(to_pci_dev(dev));
199 data = kmalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
203 memcpy(data, readpubek, sizeof(readpubek));
204 memset(data + sizeof(readpubek), 0, 20); /* zero nonce */
206 if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
207 READ_PUBEK_RESULT_SIZE) {
208 dev_err(&chip->pci_dev->dev, "A TPM error (%d) occurred "
209 "attempting to read the PUBEK\n",
210 be32_to_cpu(*((__be32 *) (data + 6))));
215 ignore header 10 bytes
216 algorithm 32 bits (1 == RSA )
219 parameters (RSA 12->bytes: keybit, #primes, expbit)
222 ignore checksum 20 bytes
227 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
228 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
229 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
230 "Modulus length: %d\nModulus: \n",
231 data[10], data[11], data[12], data[13], data[14],
232 data[15], data[16], data[17], data[22], data[23],
233 data[24], data[25], data[26], data[27], data[28],
234 data[29], data[30], data[31], data[32], data[33],
235 be32_to_cpu(*((__be32 *) (data + 32))));
237 for (i = 0; i < 256; i++) {
238 str += sprintf(str, "%02X ", data[i + 39]);
239 if ((i + 1) % 16 == 0)
240 str += sprintf(str, "\n");
247 EXPORT_SYMBOL_GPL(tpm_show_pubek);
249 #define CAP_VER_RESULT_SIZE 18
250 static const u8 cap_version[] = {
251 0, 193, /* TPM_TAG_RQU_COMMAND */
252 0, 0, 0, 18, /* length */
253 0, 0, 0, 101, /* TPM_ORD_GetCapability */
258 #define CAP_MANUFACTURER_RESULT_SIZE 18
259 static const u8 cap_manufacturer[] = {
260 0, 193, /* TPM_TAG_RQU_COMMAND */
261 0, 0, 0, 22, /* length */
262 0, 0, 0, 101, /* TPM_ORD_GetCapability */
268 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
271 u8 data[sizeof(cap_manufacturer)];
275 struct tpm_chip *chip =
276 pci_get_drvdata(to_pci_dev(dev));
280 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
282 if ((len = tpm_transmit(chip, data, sizeof(data))) <
283 CAP_MANUFACTURER_RESULT_SIZE)
286 str += sprintf(str, "Manufacturer: 0x%x\n",
287 be32_to_cpu(*((__be32 *) (data + 14))));
289 memcpy(data, cap_version, sizeof(cap_version));
291 if ((len = tpm_transmit(chip, data, sizeof(data))) <
296 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
297 (int) data[14], (int) data[15], (int) data[16],
302 EXPORT_SYMBOL_GPL(tpm_show_caps);
304 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
307 struct tpm_chip *chip = dev_get_drvdata(dev);
311 chip->vendor->cancel(chip);
314 EXPORT_SYMBOL_GPL(tpm_store_cancel);
318 * Device file system interface to the TPM
320 int tpm_open(struct inode *inode, struct file *file)
322 int rc = 0, minor = iminor(inode);
323 struct tpm_chip *chip = NULL, *pos;
325 spin_lock(&driver_lock);
327 list_for_each_entry(pos, &tpm_chip_list, list) {
328 if (pos->vendor->miscdev.minor == minor) {
339 if (chip->num_opens) {
340 dev_dbg(&chip->pci_dev->dev,
341 "Another process owns this TPM\n");
347 pci_dev_get(chip->pci_dev);
349 spin_unlock(&driver_lock);
351 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
352 if (chip->data_buffer == NULL) {
354 pci_dev_put(chip->pci_dev);
358 atomic_set(&chip->data_pending, 0);
360 file->private_data = chip;
364 spin_unlock(&driver_lock);
368 EXPORT_SYMBOL_GPL(tpm_open);
370 int tpm_release(struct inode *inode, struct file *file)
372 struct tpm_chip *chip = file->private_data;
374 spin_lock(&driver_lock);
375 file->private_data = NULL;
377 del_singleshot_timer_sync(&chip->user_read_timer);
378 atomic_set(&chip->data_pending, 0);
379 pci_dev_put(chip->pci_dev);
380 kfree(chip->data_buffer);
381 spin_unlock(&driver_lock);
385 EXPORT_SYMBOL_GPL(tpm_release);
387 ssize_t tpm_write(struct file * file, const char __user * buf,
388 size_t size, loff_t * off)
390 struct tpm_chip *chip = file->private_data;
391 int in_size = size, out_size;
393 /* cannot perform a write until the read has cleared
394 either via tpm_read or a user_read_timer timeout */
395 while (atomic_read(&chip->data_pending) != 0)
398 down(&chip->buffer_mutex);
400 if (in_size > TPM_BUFSIZE)
401 in_size = TPM_BUFSIZE;
404 (chip->data_buffer, (void __user *) buf, in_size)) {
405 up(&chip->buffer_mutex);
409 /* atomic tpm command send and result receive */
410 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
412 atomic_set(&chip->data_pending, out_size);
413 up(&chip->buffer_mutex);
415 /* Set a timeout by which the reader must come claim the result */
416 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
421 EXPORT_SYMBOL_GPL(tpm_write);
423 ssize_t tpm_read(struct file * file, char __user * buf,
424 size_t size, loff_t * off)
426 struct tpm_chip *chip = file->private_data;
429 del_singleshot_timer_sync(&chip->user_read_timer);
430 ret_size = atomic_read(&chip->data_pending);
431 atomic_set(&chip->data_pending, 0);
432 if (ret_size > 0) { /* relay data */
436 down(&chip->buffer_mutex);
438 ((void __user *) buf, chip->data_buffer, ret_size))
440 up(&chip->buffer_mutex);
446 EXPORT_SYMBOL_GPL(tpm_read);
448 void __devexit tpm_remove(struct pci_dev *pci_dev)
450 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
453 dev_err(&pci_dev->dev, "No device data found\n");
457 spin_lock(&driver_lock);
459 list_del(&chip->list);
461 spin_unlock(&driver_lock);
463 pci_set_drvdata(pci_dev, NULL);
464 misc_deregister(&chip->vendor->miscdev);
466 sysfs_remove_group(&pci_dev->dev.kobj, chip->vendor->attr_group);
468 pci_disable_device(pci_dev);
470 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
474 pci_dev_put(pci_dev);
477 EXPORT_SYMBOL_GPL(tpm_remove);
479 static u8 savestate[] = {
480 0, 193, /* TPM_TAG_RQU_COMMAND */
481 0, 0, 0, 10, /* blob length (in bytes) */
482 0, 0, 0, 152 /* TPM_ORD_SaveState */
486 * We are about to suspend. Save the TPM state
487 * so that it can be restored.
489 int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
491 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
495 tpm_transmit(chip, savestate, sizeof(savestate));
499 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
502 * Resume from a power safe. The BIOS already restored
505 int tpm_pm_resume(struct pci_dev *pci_dev)
507 struct tpm_chip *chip = pci_get_drvdata(pci_dev);
515 EXPORT_SYMBOL_GPL(tpm_pm_resume);
518 * Called from tpm_<specific>.c probe function only for devices
519 * the driver has determined it should claim. Prior to calling
520 * this function the specific probe function has called pci_enable_device
521 * upon errant exit from this function specific probe function should call
524 int tpm_register_hardware(struct pci_dev *pci_dev,
525 struct tpm_vendor_specific *entry)
528 struct tpm_chip *chip;
531 /* Driver specific per-device data */
532 chip = kmalloc(sizeof(*chip), GFP_KERNEL);
536 memset(chip, 0, sizeof(struct tpm_chip));
538 init_MUTEX(&chip->buffer_mutex);
539 init_MUTEX(&chip->tpm_mutex);
540 INIT_LIST_HEAD(&chip->list);
542 init_timer(&chip->user_read_timer);
543 chip->user_read_timer.function = user_reader_timeout;
544 chip->user_read_timer.data = (unsigned long) chip;
546 chip->vendor = entry;
550 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
551 for (j = 0; j < 8 * sizeof(int); j++)
552 if ((dev_mask[i] & (1 << j)) == 0) {
554 i * TPM_NUM_MASK_ENTRIES + j;
555 dev_mask[i] |= 1 << j;
556 goto dev_num_search_complete;
559 dev_num_search_complete:
560 if (chip->dev_num < 0) {
561 dev_err(&pci_dev->dev,
562 "No available tpm device numbers\n");
565 } else if (chip->dev_num == 0)
566 chip->vendor->miscdev.minor = TPM_MINOR;
568 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
570 snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num);
571 chip->vendor->miscdev.name = devname;
573 chip->vendor->miscdev.dev = &(pci_dev->dev);
574 chip->pci_dev = pci_dev_get(pci_dev);
576 if (misc_register(&chip->vendor->miscdev)) {
577 dev_err(&chip->pci_dev->dev,
578 "unable to misc_register %s, minor %d\n",
579 chip->vendor->miscdev.name,
580 chip->vendor->miscdev.minor);
581 pci_dev_put(pci_dev);
583 dev_mask[i] &= !(1 << j);
587 spin_lock(&driver_lock);
589 pci_set_drvdata(pci_dev, chip);
591 list_add(&chip->list, &tpm_chip_list);
593 spin_unlock(&driver_lock);
595 sysfs_create_group(&pci_dev->dev.kobj, chip->vendor->attr_group);
600 EXPORT_SYMBOL_GPL(tpm_register_hardware);
602 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
603 MODULE_DESCRIPTION("TPM Driver");
604 MODULE_VERSION("2.0");
605 MODULE_LICENSE("GPL");