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 schedule_work(&chip->work);
49 static void timeout_work(void *ptr)
51 struct tpm_chip *chip = ptr;
53 down(&chip->buffer_mutex);
54 atomic_set(&chip->data_pending, 0);
55 memset(chip->data_buffer, 0, TPM_BUFSIZE);
56 up(&chip->buffer_mutex);
60 * Internal kernel interface to transmit TPM commands
62 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
69 count = be32_to_cpu(*((__be32 *) (buf + 2)));
75 "invalid count value %x %zx \n", count, bufsiz);
79 down(&chip->tpm_mutex);
81 if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
83 "tpm_transmit: tpm_send: error %zd\n", rc);
87 stop = jiffies + 2 * 60 * HZ;
89 u8 status = chip->vendor->status(chip);
90 if ((status & chip->vendor->req_complete_mask) ==
91 chip->vendor->req_complete_val) {
95 if ((status == chip->vendor->req_canceled)) {
96 dev_err(chip->dev, "Operation Canceled\n");
101 msleep(TPM_TIMEOUT); /* CHECK */
103 } while (time_before(jiffies, stop));
106 chip->vendor->cancel(chip);
107 dev_err(chip->dev, "Operation Timed out\n");
112 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
115 "tpm_transmit: tpm_recv: error %zd\n", rc);
117 up(&chip->tpm_mutex);
121 #define TPM_DIGEST_SIZE 20
122 #define CAP_PCR_RESULT_SIZE 18
123 static const u8 cap_pcr[] = {
124 0, 193, /* TPM_TAG_RQU_COMMAND */
125 0, 0, 0, 22, /* length */
126 0, 0, 0, 101, /* TPM_ORD_GetCapability */
132 #define READ_PCR_RESULT_SIZE 30
133 static const u8 pcrread[] = {
134 0, 193, /* TPM_TAG_RQU_COMMAND */
135 0, 0, 0, 14, /* length */
136 0, 0, 0, 21, /* TPM_ORD_PcrRead */
137 0, 0, 0, 0 /* PCR index */
140 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
143 u8 data[READ_PCR_RESULT_SIZE];
149 struct tpm_chip *chip = dev_get_drvdata(dev);
153 memcpy(data, cap_pcr, sizeof(cap_pcr));
154 if ((len = tpm_transmit(chip, data, sizeof(data)))
155 < CAP_PCR_RESULT_SIZE) {
156 dev_dbg(chip->dev, "A TPM error (%d) occurred "
157 "attempting to determine the number of PCRS\n",
158 be32_to_cpu(*((__be32 *) (data + 6))));
162 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
164 for (i = 0; i < num_pcrs; i++) {
165 memcpy(data, pcrread, sizeof(pcrread));
166 index = cpu_to_be32(i);
167 memcpy(data + 10, &index, 4);
168 if ((len = tpm_transmit(chip, data, sizeof(data)))
169 < READ_PCR_RESULT_SIZE){
170 dev_dbg(chip->dev, "A TPM error (%d) occurred"
171 " attempting to read PCR %d of %d\n",
172 be32_to_cpu(*((__be32 *) (data + 6))),
176 str += sprintf(str, "PCR-%02d: ", i);
177 for (j = 0; j < TPM_DIGEST_SIZE; j++)
178 str += sprintf(str, "%02X ", *(data + 10 + j));
179 str += sprintf(str, "\n");
184 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
186 #define READ_PUBEK_RESULT_SIZE 314
187 static const u8 readpubek[] = {
188 0, 193, /* TPM_TAG_RQU_COMMAND */
189 0, 0, 0, 30, /* length */
190 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
193 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
201 struct tpm_chip *chip = dev_get_drvdata(dev);
205 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
209 memcpy(data, readpubek, sizeof(readpubek));
211 if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
212 READ_PUBEK_RESULT_SIZE) {
213 dev_dbg(chip->dev, "A TPM error (%d) occurred "
214 "attempting to read the PUBEK\n",
215 be32_to_cpu(*((__be32 *) (data + 6))));
221 ignore header 10 bytes
222 algorithm 32 bits (1 == RSA )
225 parameters (RSA 12->bytes: keybit, #primes, expbit)
228 ignore checksum 20 bytes
233 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
234 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
235 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
236 "Modulus length: %d\nModulus: \n",
237 data[10], data[11], data[12], data[13], data[14],
238 data[15], data[16], data[17], data[22], data[23],
239 data[24], data[25], data[26], data[27], data[28],
240 data[29], data[30], data[31], data[32], data[33],
241 be32_to_cpu(*((__be32 *) (data + 34))));
243 for (i = 0; i < 256; i++) {
244 str += sprintf(str, "%02X ", data[i + 38]);
245 if ((i + 1) % 16 == 0)
246 str += sprintf(str, "\n");
253 EXPORT_SYMBOL_GPL(tpm_show_pubek);
255 #define CAP_VER_RESULT_SIZE 18
256 static const u8 cap_version[] = {
257 0, 193, /* TPM_TAG_RQU_COMMAND */
258 0, 0, 0, 18, /* length */
259 0, 0, 0, 101, /* TPM_ORD_GetCapability */
264 #define CAP_MANUFACTURER_RESULT_SIZE 18
265 static const u8 cap_manufacturer[] = {
266 0, 193, /* TPM_TAG_RQU_COMMAND */
267 0, 0, 0, 22, /* length */
268 0, 0, 0, 101, /* TPM_ORD_GetCapability */
274 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
277 u8 data[sizeof(cap_manufacturer)];
281 struct tpm_chip *chip = dev_get_drvdata(dev);
285 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
287 if ((len = tpm_transmit(chip, data, sizeof(data))) <
288 CAP_MANUFACTURER_RESULT_SIZE)
291 str += sprintf(str, "Manufacturer: 0x%x\n",
292 be32_to_cpu(*((__be32 *) (data + 14))));
294 memcpy(data, cap_version, sizeof(cap_version));
296 if ((len = tpm_transmit(chip, data, sizeof(data))) <
301 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
302 (int) data[14], (int) data[15], (int) data[16],
307 EXPORT_SYMBOL_GPL(tpm_show_caps);
309 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
312 struct tpm_chip *chip = dev_get_drvdata(dev);
316 chip->vendor->cancel(chip);
319 EXPORT_SYMBOL_GPL(tpm_store_cancel);
322 * Device file system interface to the TPM
324 int tpm_open(struct inode *inode, struct file *file)
326 int rc = 0, minor = iminor(inode);
327 struct tpm_chip *chip = NULL, *pos;
329 spin_lock(&driver_lock);
331 list_for_each_entry(pos, &tpm_chip_list, list) {
332 if (pos->vendor->miscdev.minor == minor) {
343 if (chip->num_opens) {
344 dev_dbg(chip->dev, "Another process owns this TPM\n");
350 get_device(chip->dev);
352 spin_unlock(&driver_lock);
354 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
355 if (chip->data_buffer == NULL) {
357 put_device(chip->dev);
361 atomic_set(&chip->data_pending, 0);
363 file->private_data = chip;
367 spin_unlock(&driver_lock);
370 EXPORT_SYMBOL_GPL(tpm_open);
372 int tpm_release(struct inode *inode, struct file *file)
374 struct tpm_chip *chip = file->private_data;
376 spin_lock(&driver_lock);
377 file->private_data = NULL;
379 del_singleshot_timer_sync(&chip->user_read_timer);
380 flush_scheduled_work();
381 atomic_set(&chip->data_pending, 0);
382 put_device(chip->dev);
383 kfree(chip->data_buffer);
384 spin_unlock(&driver_lock);
387 EXPORT_SYMBOL_GPL(tpm_release);
389 ssize_t tpm_write(struct file *file, const char __user *buf,
390 size_t size, loff_t *off)
392 struct tpm_chip *chip = file->private_data;
393 int in_size = size, out_size;
395 /* cannot perform a write until the read has cleared
396 either via tpm_read or a user_read_timer timeout */
397 while (atomic_read(&chip->data_pending) != 0)
400 down(&chip->buffer_mutex);
402 if (in_size > TPM_BUFSIZE)
403 in_size = TPM_BUFSIZE;
406 (chip->data_buffer, (void __user *) buf, in_size)) {
407 up(&chip->buffer_mutex);
411 /* atomic tpm command send and result receive */
412 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
414 atomic_set(&chip->data_pending, out_size);
415 up(&chip->buffer_mutex);
417 /* Set a timeout by which the reader must come claim the result */
418 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
422 EXPORT_SYMBOL_GPL(tpm_write);
424 ssize_t tpm_read(struct file *file, char __user *buf,
425 size_t size, loff_t *off)
427 struct tpm_chip *chip = file->private_data;
430 del_singleshot_timer_sync(&chip->user_read_timer);
431 flush_scheduled_work();
432 ret_size = atomic_read(&chip->data_pending);
433 atomic_set(&chip->data_pending, 0);
434 if (ret_size > 0) { /* relay data */
438 down(&chip->buffer_mutex);
439 if (copy_to_user(buf, chip->data_buffer, ret_size))
441 up(&chip->buffer_mutex);
446 EXPORT_SYMBOL_GPL(tpm_read);
448 void tpm_remove_hardware(struct device *dev)
450 struct tpm_chip *chip = dev_get_drvdata(dev);
453 dev_err(dev, "No device data found\n");
457 spin_lock(&driver_lock);
459 list_del(&chip->list);
461 spin_unlock(&driver_lock);
463 dev_set_drvdata(dev, NULL);
464 misc_deregister(&chip->vendor->miscdev);
465 kfree(chip->vendor->miscdev.name);
467 sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
468 tpm_bios_log_teardown(chip->bios_dir);
470 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
471 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
477 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
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 device *dev, pm_message_t pm_state)
491 struct tpm_chip *chip = dev_get_drvdata(dev);
495 tpm_transmit(chip, savestate, sizeof(savestate));
498 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
501 * Resume from a power safe. The BIOS already restored
504 int tpm_pm_resume(struct device *dev)
506 struct tpm_chip *chip = dev_get_drvdata(dev);
513 EXPORT_SYMBOL_GPL(tpm_pm_resume);
516 * Called from tpm_<specific>.c probe function only for devices
517 * the driver has determined it should claim. Prior to calling
518 * this function the specific probe function has called pci_enable_device
519 * upon errant exit from this function specific probe function should call
522 int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
524 #define DEVNAME_SIZE 7
527 struct tpm_chip *chip;
530 /* Driver specific per-device data */
531 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
535 init_MUTEX(&chip->buffer_mutex);
536 init_MUTEX(&chip->tpm_mutex);
537 INIT_LIST_HEAD(&chip->list);
539 INIT_WORK(&chip->work, timeout_work, chip);
541 init_timer(&chip->user_read_timer);
542 chip->user_read_timer.function = user_reader_timeout;
543 chip->user_read_timer.data = (unsigned long) chip;
545 chip->vendor = entry;
549 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
550 for (j = 0; j < 8 * sizeof(int); j++)
551 if ((dev_mask[i] & (1 << j)) == 0) {
553 i * TPM_NUM_MASK_ENTRIES + j;
554 dev_mask[i] |= 1 << j;
555 goto dev_num_search_complete;
558 dev_num_search_complete:
559 if (chip->dev_num < 0) {
560 dev_err(dev, "No available tpm device numbers\n");
563 } else if (chip->dev_num == 0)
564 chip->vendor->miscdev.minor = TPM_MINOR;
566 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
568 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
569 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
570 chip->vendor->miscdev.name = devname;
572 chip->vendor->miscdev.dev = dev;
573 chip->dev = get_device(dev);
575 if (misc_register(&chip->vendor->miscdev)) {
577 "unable to misc_register %s, minor %d\n",
578 chip->vendor->miscdev.name,
579 chip->vendor->miscdev.minor);
582 dev_mask[i] &= !(1 << j);
586 spin_lock(&driver_lock);
588 dev_set_drvdata(dev, chip);
590 list_add(&chip->list, &tpm_chip_list);
592 spin_unlock(&driver_lock);
594 sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
596 chip->bios_dir = tpm_bios_log_setup(devname);
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");