Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 22 | * calls to msleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/poll.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include "tpm.h" |
| 30 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 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 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 38 | /* PCI configuration addresses */ |
| 39 | enum tpm_pci_config_addr { |
| 40 | PCI_GEN_PMCON_1 = 0xA0, |
| 41 | PCI_GEN1_DEC = 0xE4, |
| 42 | PCI_LPC_EN = 0xE6, |
| 43 | PCI_GEN2_DEC = 0xEC |
| 44 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 46 | enum tpm_config { |
| 47 | TPM_LOCK_REG = 0x0D, |
| 48 | TPM_INTERUPT_REG = 0x0A, |
| 49 | TPM_BASE_ADDR_LO = 0x08, |
| 50 | TPM_BASE_ADDR_HI = 0x09, |
| 51 | TPM_UNLOCK_VALUE = 0x55, |
| 52 | TPM_LOCK_VALUE = 0xAA, |
| 53 | TPM_DISABLE_INTERUPT_VALUE = 0x00 |
| 54 | }; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | static LIST_HEAD(tpm_chip_list); |
| 58 | static DEFINE_SPINLOCK(driver_lock); |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 59 | static int dev_mask[TPM_NUM_MASK_ENTRIES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static void user_reader_timeout(unsigned long ptr) |
| 62 | { |
| 63 | struct tpm_chip *chip = (struct tpm_chip *) ptr; |
| 64 | |
| 65 | down(&chip->buffer_mutex); |
| 66 | atomic_set(&chip->data_pending, 0); |
| 67 | memset(chip->data_buffer, 0, TPM_BUFSIZE); |
| 68 | up(&chip->buffer_mutex); |
| 69 | } |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Initialize the LPC bus and enable the TPM ports |
| 73 | */ |
| 74 | int tpm_lpc_bus_init(struct pci_dev *pci_dev, u16 base) |
| 75 | { |
| 76 | u32 lpcenable, tmp; |
| 77 | int is_lpcm = 0; |
| 78 | |
| 79 | switch (pci_dev->vendor) { |
| 80 | case PCI_VENDOR_ID_INTEL: |
| 81 | switch (pci_dev->device) { |
| 82 | case PCI_DEVICE_ID_INTEL_82801CA_12: |
| 83 | case PCI_DEVICE_ID_INTEL_82801DB_12: |
| 84 | is_lpcm = 1; |
| 85 | break; |
| 86 | } |
| 87 | /* init ICH (enable LPC) */ |
| 88 | pci_read_config_dword(pci_dev, PCI_GEN1_DEC, &lpcenable); |
| 89 | lpcenable |= 0x20000000; |
| 90 | pci_write_config_dword(pci_dev, PCI_GEN1_DEC, lpcenable); |
| 91 | |
| 92 | if (is_lpcm) { |
| 93 | pci_read_config_dword(pci_dev, PCI_GEN1_DEC, |
| 94 | &lpcenable); |
| 95 | if ((lpcenable & 0x20000000) == 0) { |
| 96 | dev_err(&pci_dev->dev, |
| 97 | "cannot enable LPC\n"); |
| 98 | return -ENODEV; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* initialize TPM registers */ |
| 103 | pci_read_config_dword(pci_dev, PCI_GEN2_DEC, &tmp); |
| 104 | |
| 105 | if (!is_lpcm) |
| 106 | tmp = (tmp & 0xFFFF0000) | (base & 0xFFF0); |
| 107 | else |
| 108 | tmp = |
| 109 | (tmp & 0xFFFF0000) | (base & 0xFFF0) | |
| 110 | 0x00000001; |
| 111 | |
| 112 | pci_write_config_dword(pci_dev, PCI_GEN2_DEC, tmp); |
| 113 | |
| 114 | if (is_lpcm) { |
| 115 | pci_read_config_dword(pci_dev, PCI_GEN_PMCON_1, |
| 116 | &tmp); |
| 117 | tmp |= 0x00000004; /* enable CLKRUN */ |
| 118 | pci_write_config_dword(pci_dev, PCI_GEN_PMCON_1, |
| 119 | tmp); |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | break; |
| 122 | case PCI_VENDOR_ID_AMD: |
| 123 | /* nothing yet */ |
| 124 | break; |
| 125 | } |
| 126 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 127 | tpm_write_index(TPM_LOCK_REG, TPM_UNLOCK_VALUE); |
| 128 | tpm_write_index(TPM_INTERUPT_REG, TPM_DISABLE_INTERUPT_VALUE); |
| 129 | tpm_write_index(TPM_BASE_ADDR_LO, base); |
| 130 | tpm_write_index(TPM_BASE_ADDR_HI, (base & 0xFF00) >> 8); |
| 131 | tpm_write_index(TPM_LOCK_REG, TPM_LOCK_VALUE); |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL_GPL(tpm_lpc_bus_init); |
| 137 | |
| 138 | /* |
| 139 | * Internal kernel interface to transmit TPM commands |
| 140 | */ |
| 141 | static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, |
| 142 | size_t bufsiz) |
| 143 | { |
| 144 | ssize_t len; |
| 145 | u32 count; |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 146 | unsigned long stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 148 | count = be32_to_cpu(*((__be32 *) (buf + 2))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | if (count == 0) |
| 151 | return -ENODATA; |
| 152 | if (count > bufsiz) { |
| 153 | dev_err(&chip->pci_dev->dev, |
Al Viro | b76be68 | 2005-04-26 07:43:41 -0700 | [diff] [blame] | 154 | "invalid count value %x %zx \n", count, bufsiz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return -E2BIG; |
| 156 | } |
| 157 | |
| 158 | down(&chip->tpm_mutex); |
| 159 | |
| 160 | if ((len = chip->vendor->send(chip, (u8 *) buf, count)) < 0) { |
| 161 | dev_err(&chip->pci_dev->dev, |
Al Viro | b76be68 | 2005-04-26 07:43:41 -0700 | [diff] [blame] | 162 | "tpm_transmit: tpm_send: error %zd\n", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return len; |
| 164 | } |
| 165 | |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 166 | stop = jiffies + 2 * 60 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | do { |
| 168 | u8 status = inb(chip->vendor->base + 1); |
| 169 | if ((status & chip->vendor->req_complete_mask) == |
| 170 | chip->vendor->req_complete_val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | goto out_recv; |
| 172 | } |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 173 | msleep(TPM_TIMEOUT); /* CHECK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | rmb(); |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 175 | } while (time_before(jiffies, stop)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | |
| 178 | chip->vendor->cancel(chip); |
| 179 | dev_err(&chip->pci_dev->dev, "Time expired\n"); |
| 180 | up(&chip->tpm_mutex); |
| 181 | return -EIO; |
| 182 | |
| 183 | out_recv: |
| 184 | len = chip->vendor->recv(chip, (u8 *) buf, bufsiz); |
| 185 | if (len < 0) |
| 186 | dev_err(&chip->pci_dev->dev, |
Al Viro | b76be68 | 2005-04-26 07:43:41 -0700 | [diff] [blame] | 187 | "tpm_transmit: tpm_recv: error %zd\n", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | up(&chip->tpm_mutex); |
| 189 | return len; |
| 190 | } |
| 191 | |
| 192 | #define TPM_DIGEST_SIZE 20 |
| 193 | #define CAP_PCR_RESULT_SIZE 18 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 194 | static const u8 cap_pcr[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 196 | 0, 0, 0, 22, /* length */ |
| 197 | 0, 0, 0, 101, /* TPM_ORD_GetCapability */ |
| 198 | 0, 0, 0, 5, |
| 199 | 0, 0, 0, 4, |
| 200 | 0, 0, 1, 1 |
| 201 | }; |
| 202 | |
| 203 | #define READ_PCR_RESULT_SIZE 30 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 204 | static const u8 pcrread[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 206 | 0, 0, 0, 14, /* length */ |
| 207 | 0, 0, 0, 21, /* TPM_ORD_PcrRead */ |
| 208 | 0, 0, 0, 0 /* PCR index */ |
| 209 | }; |
| 210 | |
Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 211 | static ssize_t show_pcrs(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | u8 data[READ_PCR_RESULT_SIZE]; |
| 214 | ssize_t len; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 215 | int i, j, num_pcrs; |
| 216 | __be32 index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | char *str = buf; |
| 218 | |
| 219 | struct tpm_chip *chip = |
Kylene Hall | e2fe906 | 2005-06-23 22:01:57 -0700 | [diff] [blame] | 220 | pci_get_drvdata(to_pci_dev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (chip == NULL) |
| 222 | return -ENODEV; |
| 223 | |
| 224 | memcpy(data, cap_pcr, sizeof(cap_pcr)); |
| 225 | if ((len = tpm_transmit(chip, data, sizeof(data))) |
| 226 | < CAP_PCR_RESULT_SIZE) |
| 227 | return len; |
| 228 | |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 229 | num_pcrs = be32_to_cpu(*((__be32 *) (data + 14))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | for (i = 0; i < num_pcrs; i++) { |
| 232 | memcpy(data, pcrread, sizeof(pcrread)); |
| 233 | index = cpu_to_be32(i); |
| 234 | memcpy(data + 10, &index, 4); |
| 235 | if ((len = tpm_transmit(chip, data, sizeof(data))) |
| 236 | < READ_PCR_RESULT_SIZE) |
| 237 | return len; |
| 238 | str += sprintf(str, "PCR-%02d: ", i); |
| 239 | for (j = 0; j < TPM_DIGEST_SIZE; j++) |
| 240 | str += sprintf(str, "%02X ", *(data + 10 + j)); |
| 241 | str += sprintf(str, "\n"); |
| 242 | } |
| 243 | return str - buf; |
| 244 | } |
| 245 | |
| 246 | static DEVICE_ATTR(pcrs, S_IRUGO, show_pcrs, NULL); |
| 247 | |
| 248 | #define READ_PUBEK_RESULT_SIZE 314 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 249 | static const u8 readpubek[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 251 | 0, 0, 0, 30, /* length */ |
| 252 | 0, 0, 0, 124, /* TPM_ORD_ReadPubek */ |
| 253 | }; |
| 254 | |
Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 255 | static ssize_t show_pubek(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 257 | u8 *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | ssize_t len; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 259 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | char *str = buf; |
| 261 | |
| 262 | struct tpm_chip *chip = |
Kylene Hall | e2fe906 | 2005-06-23 22:01:57 -0700 | [diff] [blame] | 263 | pci_get_drvdata(to_pci_dev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (chip == NULL) |
| 265 | return -ENODEV; |
| 266 | |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 267 | data = kmalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL); |
| 268 | if (!data) |
| 269 | return -ENOMEM; |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | memcpy(data, readpubek, sizeof(readpubek)); |
| 272 | memset(data + sizeof(readpubek), 0, 20); /* zero nonce */ |
| 273 | |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 274 | if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) < |
| 275 | READ_PUBEK_RESULT_SIZE) { |
| 276 | rc = len; |
| 277 | goto out; |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | ignore header 10 bytes |
| 282 | algorithm 32 bits (1 == RSA ) |
| 283 | encscheme 16 bits |
| 284 | sigscheme 16 bits |
| 285 | parameters (RSA 12->bytes: keybit, #primes, expbit) |
| 286 | keylenbytes 32 bits |
| 287 | 256 byte modulus |
| 288 | ignore checksum 20 bytes |
| 289 | */ |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | str += |
| 292 | sprintf(str, |
| 293 | "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n" |
| 294 | "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X" |
| 295 | " %02X %02X %02X %02X %02X %02X %02X %02X\n" |
| 296 | "Modulus length: %d\nModulus: \n", |
| 297 | data[10], data[11], data[12], data[13], data[14], |
| 298 | data[15], data[16], data[17], data[22], data[23], |
| 299 | data[24], data[25], data[26], data[27], data[28], |
| 300 | data[29], data[30], data[31], data[32], data[33], |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 301 | be32_to_cpu(*((__be32 *) (data + 32)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | for (i = 0; i < 256; i++) { |
| 304 | str += sprintf(str, "%02X ", data[i + 39]); |
| 305 | if ((i + 1) % 16 == 0) |
| 306 | str += sprintf(str, "\n"); |
| 307 | } |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 308 | rc = str - buf; |
| 309 | out: |
| 310 | kfree(data); |
| 311 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static DEVICE_ATTR(pubek, S_IRUGO, show_pubek, NULL); |
| 315 | |
| 316 | #define CAP_VER_RESULT_SIZE 18 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 317 | static const u8 cap_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 319 | 0, 0, 0, 18, /* length */ |
| 320 | 0, 0, 0, 101, /* TPM_ORD_GetCapability */ |
| 321 | 0, 0, 0, 6, |
| 322 | 0, 0, 0, 0 |
| 323 | }; |
| 324 | |
| 325 | #define CAP_MANUFACTURER_RESULT_SIZE 18 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 326 | static const u8 cap_manufacturer[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 328 | 0, 0, 0, 22, /* length */ |
| 329 | 0, 0, 0, 101, /* TPM_ORD_GetCapability */ |
| 330 | 0, 0, 0, 5, |
| 331 | 0, 0, 0, 4, |
| 332 | 0, 0, 1, 3 |
| 333 | }; |
| 334 | |
Yani Ioannou | 74880c0 | 2005-05-17 06:41:12 -0400 | [diff] [blame] | 335 | static ssize_t show_caps(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 337 | u8 data[sizeof(cap_manufacturer)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | ssize_t len; |
| 339 | char *str = buf; |
| 340 | |
| 341 | struct tpm_chip *chip = |
Kylene Hall | e2fe906 | 2005-06-23 22:01:57 -0700 | [diff] [blame] | 342 | pci_get_drvdata(to_pci_dev(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (chip == NULL) |
| 344 | return -ENODEV; |
| 345 | |
| 346 | memcpy(data, cap_manufacturer, sizeof(cap_manufacturer)); |
| 347 | |
| 348 | if ((len = tpm_transmit(chip, data, sizeof(data))) < |
| 349 | CAP_MANUFACTURER_RESULT_SIZE) |
| 350 | return len; |
| 351 | |
| 352 | str += sprintf(str, "Manufacturer: 0x%x\n", |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame^] | 353 | be32_to_cpu(*((__be32 *) (data + 14)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | memcpy(data, cap_version, sizeof(cap_version)); |
| 356 | |
| 357 | if ((len = tpm_transmit(chip, data, sizeof(data))) < |
| 358 | CAP_VER_RESULT_SIZE) |
| 359 | return len; |
| 360 | |
| 361 | str += |
| 362 | sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 363 | (int) data[14], (int) data[15], (int) data[16], |
| 364 | (int) data[17]); |
| 365 | |
| 366 | return str - buf; |
| 367 | } |
| 368 | |
| 369 | static DEVICE_ATTR(caps, S_IRUGO, show_caps, NULL); |
| 370 | |
| 371 | /* |
| 372 | * Device file system interface to the TPM |
| 373 | */ |
| 374 | int tpm_open(struct inode *inode, struct file *file) |
| 375 | { |
| 376 | int rc = 0, minor = iminor(inode); |
| 377 | struct tpm_chip *chip = NULL, *pos; |
| 378 | |
| 379 | spin_lock(&driver_lock); |
| 380 | |
| 381 | list_for_each_entry(pos, &tpm_chip_list, list) { |
| 382 | if (pos->vendor->miscdev.minor == minor) { |
| 383 | chip = pos; |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (chip == NULL) { |
| 389 | rc = -ENODEV; |
| 390 | goto err_out; |
| 391 | } |
| 392 | |
| 393 | if (chip->num_opens) { |
| 394 | dev_dbg(&chip->pci_dev->dev, |
| 395 | "Another process owns this TPM\n"); |
| 396 | rc = -EBUSY; |
| 397 | goto err_out; |
| 398 | } |
| 399 | |
| 400 | chip->num_opens++; |
| 401 | pci_dev_get(chip->pci_dev); |
| 402 | |
| 403 | spin_unlock(&driver_lock); |
| 404 | |
| 405 | chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); |
| 406 | if (chip->data_buffer == NULL) { |
| 407 | chip->num_opens--; |
| 408 | pci_dev_put(chip->pci_dev); |
| 409 | return -ENOMEM; |
| 410 | } |
| 411 | |
| 412 | atomic_set(&chip->data_pending, 0); |
| 413 | |
| 414 | file->private_data = chip; |
| 415 | return 0; |
| 416 | |
| 417 | err_out: |
| 418 | spin_unlock(&driver_lock); |
| 419 | return rc; |
| 420 | } |
| 421 | |
| 422 | EXPORT_SYMBOL_GPL(tpm_open); |
| 423 | |
| 424 | int tpm_release(struct inode *inode, struct file *file) |
| 425 | { |
| 426 | struct tpm_chip *chip = file->private_data; |
| 427 | |
| 428 | file->private_data = NULL; |
| 429 | |
| 430 | spin_lock(&driver_lock); |
| 431 | chip->num_opens--; |
Kylene Hall | fe3fd48 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 432 | del_singleshot_timer_sync(&chip->user_read_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | atomic_set(&chip->data_pending, 0); |
| 434 | |
| 435 | pci_dev_put(chip->pci_dev); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | EXPORT_SYMBOL_GPL(tpm_release); |
| 440 | |
| 441 | ssize_t tpm_write(struct file * file, const char __user * buf, |
| 442 | size_t size, loff_t * off) |
| 443 | { |
| 444 | struct tpm_chip *chip = file->private_data; |
| 445 | int in_size = size, out_size; |
| 446 | |
| 447 | /* cannot perform a write until the read has cleared |
| 448 | either via tpm_read or a user_read_timer timeout */ |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 449 | while (atomic_read(&chip->data_pending) != 0) |
| 450 | msleep(TPM_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | down(&chip->buffer_mutex); |
| 453 | |
| 454 | if (in_size > TPM_BUFSIZE) |
| 455 | in_size = TPM_BUFSIZE; |
| 456 | |
| 457 | if (copy_from_user |
| 458 | (chip->data_buffer, (void __user *) buf, in_size)) { |
| 459 | up(&chip->buffer_mutex); |
| 460 | return -EFAULT; |
| 461 | } |
| 462 | |
| 463 | /* atomic tpm command send and result receive */ |
| 464 | out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); |
| 465 | |
| 466 | atomic_set(&chip->data_pending, out_size); |
| 467 | up(&chip->buffer_mutex); |
| 468 | |
| 469 | /* Set a timeout by which the reader must come claim the result */ |
Kylene Hall | fe3fd48 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 470 | mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
| 472 | return in_size; |
| 473 | } |
| 474 | |
| 475 | EXPORT_SYMBOL_GPL(tpm_write); |
| 476 | |
| 477 | ssize_t tpm_read(struct file * file, char __user * buf, |
| 478 | size_t size, loff_t * off) |
| 479 | { |
| 480 | struct tpm_chip *chip = file->private_data; |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 481 | int ret_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 483 | del_singleshot_timer_sync(&chip->user_read_timer); |
| 484 | ret_size = atomic_read(&chip->data_pending); |
| 485 | atomic_set(&chip->data_pending, 0); |
| 486 | if (ret_size > 0) { /* relay data */ |
| 487 | if (size < ret_size) |
| 488 | ret_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | down(&chip->buffer_mutex); |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 491 | if (copy_to_user |
| 492 | ((void __user *) buf, chip->data_buffer, ret_size)) |
| 493 | ret_size = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | up(&chip->buffer_mutex); |
| 495 | } |
| 496 | |
| 497 | return ret_size; |
| 498 | } |
| 499 | |
| 500 | EXPORT_SYMBOL_GPL(tpm_read); |
| 501 | |
| 502 | void __devexit tpm_remove(struct pci_dev *pci_dev) |
| 503 | { |
| 504 | struct tpm_chip *chip = pci_get_drvdata(pci_dev); |
| 505 | |
| 506 | if (chip == NULL) { |
| 507 | dev_err(&pci_dev->dev, "No device data found\n"); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | spin_lock(&driver_lock); |
| 512 | |
| 513 | list_del(&chip->list); |
| 514 | |
| 515 | spin_unlock(&driver_lock); |
| 516 | |
| 517 | pci_set_drvdata(pci_dev, NULL); |
| 518 | misc_deregister(&chip->vendor->miscdev); |
| 519 | |
| 520 | device_remove_file(&pci_dev->dev, &dev_attr_pubek); |
| 521 | device_remove_file(&pci_dev->dev, &dev_attr_pcrs); |
| 522 | device_remove_file(&pci_dev->dev, &dev_attr_caps); |
| 523 | |
| 524 | pci_disable_device(pci_dev); |
| 525 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 526 | dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
| 528 | kfree(chip); |
| 529 | |
| 530 | pci_dev_put(pci_dev); |
| 531 | } |
| 532 | |
| 533 | EXPORT_SYMBOL_GPL(tpm_remove); |
| 534 | |
| 535 | static u8 savestate[] = { |
| 536 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 537 | 0, 0, 0, 10, /* blob length (in bytes) */ |
| 538 | 0, 0, 0, 152 /* TPM_ORD_SaveState */ |
| 539 | }; |
| 540 | |
| 541 | /* |
| 542 | * We are about to suspend. Save the TPM state |
| 543 | * so that it can be restored. |
| 544 | */ |
Pavel Machek | 4fd416c | 2005-04-16 15:25:24 -0700 | [diff] [blame] | 545 | int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | struct tpm_chip *chip = pci_get_drvdata(pci_dev); |
| 548 | if (chip == NULL) |
| 549 | return -ENODEV; |
| 550 | |
| 551 | tpm_transmit(chip, savestate, sizeof(savestate)); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
| 556 | |
| 557 | /* |
| 558 | * Resume from a power safe. The BIOS already restored |
| 559 | * the TPM state. |
| 560 | */ |
| 561 | int tpm_pm_resume(struct pci_dev *pci_dev) |
| 562 | { |
| 563 | struct tpm_chip *chip = pci_get_drvdata(pci_dev); |
| 564 | |
| 565 | if (chip == NULL) |
| 566 | return -ENODEV; |
| 567 | |
| 568 | spin_lock(&driver_lock); |
| 569 | tpm_lpc_bus_init(pci_dev, chip->vendor->base); |
| 570 | spin_unlock(&driver_lock); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
| 576 | |
| 577 | /* |
| 578 | * Called from tpm_<specific>.c probe function only for devices |
| 579 | * the driver has determined it should claim. Prior to calling |
| 580 | * this function the specific probe function has called pci_enable_device |
| 581 | * upon errant exit from this function specific probe function should call |
| 582 | * pci_disable_device |
| 583 | */ |
| 584 | int tpm_register_hardware(struct pci_dev *pci_dev, |
| 585 | struct tpm_vendor_specific *entry) |
| 586 | { |
| 587 | char devname[7]; |
| 588 | struct tpm_chip *chip; |
| 589 | int i, j; |
| 590 | |
| 591 | /* Driver specific per-device data */ |
| 592 | chip = kmalloc(sizeof(*chip), GFP_KERNEL); |
| 593 | if (chip == NULL) |
| 594 | return -ENOMEM; |
| 595 | |
| 596 | memset(chip, 0, sizeof(struct tpm_chip)); |
| 597 | |
| 598 | init_MUTEX(&chip->buffer_mutex); |
| 599 | init_MUTEX(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | INIT_LIST_HEAD(&chip->list); |
| 601 | |
Kylene Hall | fe3fd48 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 602 | init_timer(&chip->user_read_timer); |
| 603 | chip->user_read_timer.function = user_reader_timeout; |
| 604 | chip->user_read_timer.data = (unsigned long) chip; |
| 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | chip->vendor = entry; |
| 607 | |
| 608 | chip->dev_num = -1; |
| 609 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 610 | for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++) |
| 611 | for (j = 0; j < 8 * sizeof(int); j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if ((dev_mask[i] & (1 << j)) == 0) { |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 613 | chip->dev_num = |
| 614 | i * TPM_NUM_MASK_ENTRIES + j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | dev_mask[i] |= 1 << j; |
| 616 | goto dev_num_search_complete; |
| 617 | } |
| 618 | |
| 619 | dev_num_search_complete: |
| 620 | if (chip->dev_num < 0) { |
| 621 | dev_err(&pci_dev->dev, |
| 622 | "No available tpm device numbers\n"); |
| 623 | kfree(chip); |
| 624 | return -ENODEV; |
| 625 | } else if (chip->dev_num == 0) |
| 626 | chip->vendor->miscdev.minor = TPM_MINOR; |
| 627 | else |
| 628 | chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 629 | |
| 630 | snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num); |
| 631 | chip->vendor->miscdev.name = devname; |
| 632 | |
| 633 | chip->vendor->miscdev.dev = &(pci_dev->dev); |
| 634 | chip->pci_dev = pci_dev_get(pci_dev); |
| 635 | |
| 636 | if (misc_register(&chip->vendor->miscdev)) { |
| 637 | dev_err(&chip->pci_dev->dev, |
| 638 | "unable to misc_register %s, minor %d\n", |
| 639 | chip->vendor->miscdev.name, |
| 640 | chip->vendor->miscdev.minor); |
| 641 | pci_dev_put(pci_dev); |
| 642 | kfree(chip); |
| 643 | dev_mask[i] &= !(1 << j); |
| 644 | return -ENODEV; |
| 645 | } |
| 646 | |
| 647 | pci_set_drvdata(pci_dev, chip); |
| 648 | |
| 649 | list_add(&chip->list, &tpm_chip_list); |
| 650 | |
| 651 | device_create_file(&pci_dev->dev, &dev_attr_pubek); |
| 652 | device_create_file(&pci_dev->dev, &dev_attr_pcrs); |
| 653 | device_create_file(&pci_dev->dev, &dev_attr_caps); |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | EXPORT_SYMBOL_GPL(tpm_register_hardware); |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 661 | MODULE_DESCRIPTION("TPM Driver"); |
| 662 | MODULE_VERSION("2.0"); |
| 663 | MODULE_LICENSE("GPL"); |