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)));
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) {
76 "tpm_transmit: tpm_send: error %zd\n", rc);
80 stop = jiffies + 2 * 60 * HZ;
82 u8 status = chip->vendor->status(chip);
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->dev, "Operation Canceled\n");
94 msleep(TPM_TIMEOUT); /* CHECK */
96 } while (time_before(jiffies, stop));
99 chip->vendor->cancel(chip);
100 dev_err(chip->dev, "Operation Timed out\n");
105 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
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 = dev_get_drvdata(dev);
146 memcpy(data, cap_pcr, sizeof(cap_pcr));
147 if ((len = tpm_transmit(chip, data, sizeof(data)))
148 < CAP_PCR_RESULT_SIZE) {
149 dev_dbg(chip->dev, "A TPM error (%d) occurred "
150 "attempting to determine the number of PCRS\n",
151 be32_to_cpu(*((__be32 *) (data + 6))));
155 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
157 for (i = 0; i < num_pcrs; i++) {
158 memcpy(data, pcrread, sizeof(pcrread));
159 index = cpu_to_be32(i);
160 memcpy(data + 10, &index, 4);
161 if ((len = tpm_transmit(chip, data, sizeof(data)))
162 < READ_PCR_RESULT_SIZE){
163 dev_dbg(chip->dev, "A TPM error (%d) occurred"
164 " attempting to read PCR %d of %d\n",
165 be32_to_cpu(*((__be32 *) (data + 6))),
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 = dev_get_drvdata(dev);
198 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
202 memcpy(data, readpubek, sizeof(readpubek));
204 if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
205 READ_PUBEK_RESULT_SIZE) {
206 dev_dbg(chip->dev, "A TPM error (%d) occurred "
207 "attempting to read the PUBEK\n",
208 be32_to_cpu(*((__be32 *) (data + 6))));
214 ignore header 10 bytes
215 algorithm 32 bits (1 == RSA )
218 parameters (RSA 12->bytes: keybit, #primes, expbit)
221 ignore checksum 20 bytes
226 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
227 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
228 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
229 "Modulus length: %d\nModulus: \n",
230 data[10], data[11], data[12], data[13], data[14],
231 data[15], data[16], data[17], data[22], data[23],
232 data[24], data[25], data[26], data[27], data[28],
233 data[29], data[30], data[31], data[32], data[33],
234 be32_to_cpu(*((__be32 *) (data + 34))));
236 for (i = 0; i < 256; i++) {
237 str += sprintf(str, "%02X ", data[i + 38]);
238 if ((i + 1) % 16 == 0)
239 str += sprintf(str, "\n");
246 EXPORT_SYMBOL_GPL(tpm_show_pubek);
248 #define CAP_VER_RESULT_SIZE 18
249 static const u8 cap_version[] = {
250 0, 193, /* TPM_TAG_RQU_COMMAND */
251 0, 0, 0, 18, /* length */
252 0, 0, 0, 101, /* TPM_ORD_GetCapability */
257 #define CAP_MANUFACTURER_RESULT_SIZE 18
258 static const u8 cap_manufacturer[] = {
259 0, 193, /* TPM_TAG_RQU_COMMAND */
260 0, 0, 0, 22, /* length */
261 0, 0, 0, 101, /* TPM_ORD_GetCapability */
267 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
270 u8 data[sizeof(cap_manufacturer)];
274 struct tpm_chip *chip = dev_get_drvdata(dev);
278 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
280 if ((len = tpm_transmit(chip, data, sizeof(data))) <
281 CAP_MANUFACTURER_RESULT_SIZE)
284 str += sprintf(str, "Manufacturer: 0x%x\n",
285 be32_to_cpu(*((__be32 *) (data + 14))));
287 memcpy(data, cap_version, sizeof(cap_version));
289 if ((len = tpm_transmit(chip, data, sizeof(data))) <
294 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
295 (int) data[14], (int) data[15], (int) data[16],
300 EXPORT_SYMBOL_GPL(tpm_show_caps);
302 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
303 const char *buf, size_t count)
305 struct tpm_chip *chip = dev_get_drvdata(dev);
309 chip->vendor->cancel(chip);
312 EXPORT_SYMBOL_GPL(tpm_store_cancel);
315 * Device file system interface to the TPM
317 int tpm_open(struct inode *inode, struct file *file)
319 int rc = 0, minor = iminor(inode);
320 struct tpm_chip *chip = NULL, *pos;
322 spin_lock(&driver_lock);
324 list_for_each_entry(pos, &tpm_chip_list, list) {
325 if (pos->vendor->miscdev.minor == minor) {
336 if (chip->num_opens) {
337 dev_dbg(chip->dev, "Another process owns this TPM\n");
343 get_device(chip->dev);
345 spin_unlock(&driver_lock);
347 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
348 if (chip->data_buffer == NULL) {
350 put_device(chip->dev);
354 atomic_set(&chip->data_pending, 0);
356 file->private_data = chip;
360 spin_unlock(&driver_lock);
363 EXPORT_SYMBOL_GPL(tpm_open);
365 int tpm_release(struct inode *inode, struct file *file)
367 struct tpm_chip *chip = file->private_data;
369 spin_lock(&driver_lock);
370 file->private_data = NULL;
372 del_singleshot_timer_sync(&chip->user_read_timer);
373 atomic_set(&chip->data_pending, 0);
374 put_device(chip->dev);
375 kfree(chip->data_buffer);
376 spin_unlock(&driver_lock);
379 EXPORT_SYMBOL_GPL(tpm_release);
381 ssize_t tpm_write(struct file *file, const char __user *buf,
382 size_t size, loff_t * off)
384 struct tpm_chip *chip = file->private_data;
385 int in_size = size, out_size;
387 /* cannot perform a write until the read has cleared
388 either via tpm_read or a user_read_timer timeout */
389 while (atomic_read(&chip->data_pending) != 0)
392 down(&chip->buffer_mutex);
394 if (in_size > TPM_BUFSIZE)
395 in_size = TPM_BUFSIZE;
398 (chip->data_buffer, (void __user *) buf, in_size)) {
399 up(&chip->buffer_mutex);
403 /* atomic tpm command send and result receive */
404 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
406 atomic_set(&chip->data_pending, out_size);
407 up(&chip->buffer_mutex);
409 /* Set a timeout by which the reader must come claim the result */
410 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
415 EXPORT_SYMBOL_GPL(tpm_write);
417 ssize_t tpm_read(struct file * file, char __user *buf,
418 size_t size, loff_t * off)
420 struct tpm_chip *chip = file->private_data;
423 del_singleshot_timer_sync(&chip->user_read_timer);
424 ret_size = atomic_read(&chip->data_pending);
425 atomic_set(&chip->data_pending, 0);
426 if (ret_size > 0) { /* relay data */
430 down(&chip->buffer_mutex);
432 ((void __user *) buf, chip->data_buffer, ret_size))
434 up(&chip->buffer_mutex);
439 EXPORT_SYMBOL_GPL(tpm_read);
441 void tpm_remove_hardware(struct device *dev)
443 struct tpm_chip *chip = dev_get_drvdata(dev);
446 dev_err(dev, "No device data found\n");
450 spin_lock(&driver_lock);
452 list_del(&chip->list);
454 spin_unlock(&driver_lock);
456 dev_set_drvdata(dev, NULL);
457 misc_deregister(&chip->vendor->miscdev);
458 kfree(chip->vendor->miscdev.name);
460 sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
462 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
463 !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
469 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
471 static u8 savestate[] = {
472 0, 193, /* TPM_TAG_RQU_COMMAND */
473 0, 0, 0, 10, /* blob length (in bytes) */
474 0, 0, 0, 152 /* TPM_ORD_SaveState */
478 * We are about to suspend. Save the TPM state
479 * so that it can be restored.
481 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
483 struct tpm_chip *chip = dev_get_drvdata(dev);
487 tpm_transmit(chip, savestate, sizeof(savestate));
490 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
493 * Resume from a power safe. The BIOS already restored
496 int tpm_pm_resume(struct device *dev)
498 struct tpm_chip *chip = dev_get_drvdata(dev);
505 EXPORT_SYMBOL_GPL(tpm_pm_resume);
508 * Called from tpm_<specific>.c probe function only for devices
509 * the driver has determined it should claim. Prior to calling
510 * this function the specific probe function has called pci_enable_device
511 * upon errant exit from this function specific probe function should call
514 int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
516 #define DEVNAME_SIZE 7
519 struct tpm_chip *chip;
522 /* Driver specific per-device data */
523 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
527 init_MUTEX(&chip->buffer_mutex);
528 init_MUTEX(&chip->tpm_mutex);
529 INIT_LIST_HEAD(&chip->list);
531 init_timer(&chip->user_read_timer);
532 chip->user_read_timer.function = user_reader_timeout;
533 chip->user_read_timer.data = (unsigned long) chip;
535 chip->vendor = entry;
539 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
540 for (j = 0; j < 8 * sizeof(int); j++)
541 if ((dev_mask[i] & (1 << j)) == 0) {
543 i * TPM_NUM_MASK_ENTRIES + j;
544 dev_mask[i] |= 1 << j;
545 goto dev_num_search_complete;
548 dev_num_search_complete:
549 if (chip->dev_num < 0) {
550 dev_err(dev, "No available tpm device numbers\n");
553 } else if (chip->dev_num == 0)
554 chip->vendor->miscdev.minor = TPM_MINOR;
556 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
558 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
559 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
560 chip->vendor->miscdev.name = devname;
562 chip->vendor->miscdev.dev = dev;
563 chip->dev = get_device(dev);
565 if (misc_register(&chip->vendor->miscdev)) {
567 "unable to misc_register %s, minor %d\n",
568 chip->vendor->miscdev.name,
569 chip->vendor->miscdev.minor);
572 dev_mask[i] &= !(1 << j);
576 spin_lock(&driver_lock);
578 dev_set_drvdata(dev, chip);
580 list_add(&chip->list, &tpm_chip_list);
582 spin_unlock(&driver_lock);
584 sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
588 EXPORT_SYMBOL_GPL(tpm_register_hardware);
590 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
591 MODULE_DESCRIPTION("TPM Driver");
592 MODULE_VERSION("2.0");
593 MODULE_LICENSE("GPL");