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 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 10 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/poll.h> |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/spinlock.h> |
Arnd Bergmann | 25368ca | 2008-05-20 19:16:51 +0200 | [diff] [blame] | 29 | #include <linux/smp_lock.h> |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "tpm.h" |
| 32 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 33 | enum tpm_const { |
| 34 | TPM_MINOR = 224, /* officially assigned */ |
| 35 | TPM_BUFSIZE = 2048, |
| 36 | TPM_NUM_DEVICES = 256, |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 37 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 39 | enum tpm_duration { |
| 40 | TPM_SHORT = 0, |
| 41 | TPM_MEDIUM = 1, |
| 42 | TPM_LONG = 2, |
| 43 | TPM_UNDEFINED, |
| 44 | }; |
| 45 | |
| 46 | #define TPM_MAX_ORDINAL 243 |
| 47 | #define TPM_MAX_PROTECTED_ORDINAL 12 |
| 48 | #define TPM_PROTECTED_ORDINAL_MASK 0xFF |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static LIST_HEAD(tpm_chip_list); |
| 51 | static DEFINE_SPINLOCK(driver_lock); |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 52 | static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 54 | /* |
| 55 | * Array with one entry per ordinal defining the maximum amount |
| 56 | * of time the chip could take to return the result. The ordinal |
| 57 | * designation of short, medium or long is defined in a table in |
| 58 | * TCG Specification TPM Main Part 2 TPM Structures Section 17. The |
| 59 | * values of the SHORT, MEDIUM, and LONG durations are retrieved |
| 60 | * from the chip during initialization with a call to tpm_get_timeouts. |
| 61 | */ |
| 62 | static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = { |
| 63 | TPM_UNDEFINED, /* 0 */ |
| 64 | TPM_UNDEFINED, |
| 65 | TPM_UNDEFINED, |
| 66 | TPM_UNDEFINED, |
| 67 | TPM_UNDEFINED, |
| 68 | TPM_UNDEFINED, /* 5 */ |
| 69 | TPM_UNDEFINED, |
| 70 | TPM_UNDEFINED, |
| 71 | TPM_UNDEFINED, |
| 72 | TPM_UNDEFINED, |
| 73 | TPM_SHORT, /* 10 */ |
| 74 | TPM_SHORT, |
| 75 | }; |
| 76 | |
| 77 | static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { |
| 78 | TPM_UNDEFINED, /* 0 */ |
| 79 | TPM_UNDEFINED, |
| 80 | TPM_UNDEFINED, |
| 81 | TPM_UNDEFINED, |
| 82 | TPM_UNDEFINED, |
| 83 | TPM_UNDEFINED, /* 5 */ |
| 84 | TPM_UNDEFINED, |
| 85 | TPM_UNDEFINED, |
| 86 | TPM_UNDEFINED, |
| 87 | TPM_UNDEFINED, |
| 88 | TPM_SHORT, /* 10 */ |
| 89 | TPM_SHORT, |
| 90 | TPM_MEDIUM, |
| 91 | TPM_LONG, |
| 92 | TPM_LONG, |
| 93 | TPM_MEDIUM, /* 15 */ |
| 94 | TPM_SHORT, |
| 95 | TPM_SHORT, |
| 96 | TPM_MEDIUM, |
| 97 | TPM_LONG, |
| 98 | TPM_SHORT, /* 20 */ |
| 99 | TPM_SHORT, |
| 100 | TPM_MEDIUM, |
| 101 | TPM_MEDIUM, |
| 102 | TPM_MEDIUM, |
| 103 | TPM_SHORT, /* 25 */ |
| 104 | TPM_SHORT, |
| 105 | TPM_MEDIUM, |
| 106 | TPM_SHORT, |
| 107 | TPM_SHORT, |
| 108 | TPM_MEDIUM, /* 30 */ |
| 109 | TPM_LONG, |
| 110 | TPM_MEDIUM, |
| 111 | TPM_SHORT, |
| 112 | TPM_SHORT, |
| 113 | TPM_SHORT, /* 35 */ |
| 114 | TPM_MEDIUM, |
| 115 | TPM_MEDIUM, |
| 116 | TPM_UNDEFINED, |
| 117 | TPM_UNDEFINED, |
| 118 | TPM_MEDIUM, /* 40 */ |
| 119 | TPM_LONG, |
| 120 | TPM_MEDIUM, |
| 121 | TPM_SHORT, |
| 122 | TPM_SHORT, |
| 123 | TPM_SHORT, /* 45 */ |
| 124 | TPM_SHORT, |
| 125 | TPM_SHORT, |
| 126 | TPM_SHORT, |
| 127 | TPM_LONG, |
| 128 | TPM_MEDIUM, /* 50 */ |
| 129 | TPM_MEDIUM, |
| 130 | TPM_UNDEFINED, |
| 131 | TPM_UNDEFINED, |
| 132 | TPM_UNDEFINED, |
| 133 | TPM_UNDEFINED, /* 55 */ |
| 134 | TPM_UNDEFINED, |
| 135 | TPM_UNDEFINED, |
| 136 | TPM_UNDEFINED, |
| 137 | TPM_UNDEFINED, |
| 138 | TPM_MEDIUM, /* 60 */ |
| 139 | TPM_MEDIUM, |
| 140 | TPM_MEDIUM, |
| 141 | TPM_SHORT, |
| 142 | TPM_SHORT, |
| 143 | TPM_MEDIUM, /* 65 */ |
| 144 | TPM_UNDEFINED, |
| 145 | TPM_UNDEFINED, |
| 146 | TPM_UNDEFINED, |
| 147 | TPM_UNDEFINED, |
| 148 | TPM_SHORT, /* 70 */ |
| 149 | TPM_SHORT, |
| 150 | TPM_UNDEFINED, |
| 151 | TPM_UNDEFINED, |
| 152 | TPM_UNDEFINED, |
| 153 | TPM_UNDEFINED, /* 75 */ |
| 154 | TPM_UNDEFINED, |
| 155 | TPM_UNDEFINED, |
| 156 | TPM_UNDEFINED, |
| 157 | TPM_UNDEFINED, |
| 158 | TPM_LONG, /* 80 */ |
| 159 | TPM_UNDEFINED, |
| 160 | TPM_MEDIUM, |
| 161 | TPM_LONG, |
| 162 | TPM_SHORT, |
| 163 | TPM_UNDEFINED, /* 85 */ |
| 164 | TPM_UNDEFINED, |
| 165 | TPM_UNDEFINED, |
| 166 | TPM_UNDEFINED, |
| 167 | TPM_UNDEFINED, |
| 168 | TPM_SHORT, /* 90 */ |
| 169 | TPM_SHORT, |
| 170 | TPM_SHORT, |
| 171 | TPM_SHORT, |
| 172 | TPM_SHORT, |
| 173 | TPM_UNDEFINED, /* 95 */ |
| 174 | TPM_UNDEFINED, |
| 175 | TPM_UNDEFINED, |
| 176 | TPM_UNDEFINED, |
| 177 | TPM_UNDEFINED, |
| 178 | TPM_MEDIUM, /* 100 */ |
| 179 | TPM_SHORT, |
| 180 | TPM_SHORT, |
| 181 | TPM_UNDEFINED, |
| 182 | TPM_UNDEFINED, |
| 183 | TPM_UNDEFINED, /* 105 */ |
| 184 | TPM_UNDEFINED, |
| 185 | TPM_UNDEFINED, |
| 186 | TPM_UNDEFINED, |
| 187 | TPM_UNDEFINED, |
| 188 | TPM_SHORT, /* 110 */ |
| 189 | TPM_SHORT, |
| 190 | TPM_SHORT, |
| 191 | TPM_SHORT, |
| 192 | TPM_SHORT, |
| 193 | TPM_SHORT, /* 115 */ |
| 194 | TPM_SHORT, |
| 195 | TPM_SHORT, |
| 196 | TPM_UNDEFINED, |
| 197 | TPM_UNDEFINED, |
| 198 | TPM_LONG, /* 120 */ |
| 199 | TPM_LONG, |
| 200 | TPM_MEDIUM, |
| 201 | TPM_UNDEFINED, |
| 202 | TPM_SHORT, |
| 203 | TPM_SHORT, /* 125 */ |
| 204 | TPM_SHORT, |
| 205 | TPM_LONG, |
| 206 | TPM_SHORT, |
| 207 | TPM_SHORT, |
| 208 | TPM_SHORT, /* 130 */ |
| 209 | TPM_MEDIUM, |
| 210 | TPM_UNDEFINED, |
| 211 | TPM_SHORT, |
| 212 | TPM_MEDIUM, |
| 213 | TPM_UNDEFINED, /* 135 */ |
| 214 | TPM_UNDEFINED, |
| 215 | TPM_UNDEFINED, |
| 216 | TPM_UNDEFINED, |
| 217 | TPM_UNDEFINED, |
| 218 | TPM_SHORT, /* 140 */ |
| 219 | TPM_SHORT, |
| 220 | TPM_UNDEFINED, |
| 221 | TPM_UNDEFINED, |
| 222 | TPM_UNDEFINED, |
| 223 | TPM_UNDEFINED, /* 145 */ |
| 224 | TPM_UNDEFINED, |
| 225 | TPM_UNDEFINED, |
| 226 | TPM_UNDEFINED, |
| 227 | TPM_UNDEFINED, |
| 228 | TPM_SHORT, /* 150 */ |
| 229 | TPM_MEDIUM, |
| 230 | TPM_MEDIUM, |
| 231 | TPM_SHORT, |
| 232 | TPM_SHORT, |
| 233 | TPM_UNDEFINED, /* 155 */ |
| 234 | TPM_UNDEFINED, |
| 235 | TPM_UNDEFINED, |
| 236 | TPM_UNDEFINED, |
| 237 | TPM_UNDEFINED, |
| 238 | TPM_SHORT, /* 160 */ |
| 239 | TPM_SHORT, |
| 240 | TPM_SHORT, |
| 241 | TPM_SHORT, |
| 242 | TPM_UNDEFINED, |
| 243 | TPM_UNDEFINED, /* 165 */ |
| 244 | TPM_UNDEFINED, |
| 245 | TPM_UNDEFINED, |
| 246 | TPM_UNDEFINED, |
| 247 | TPM_UNDEFINED, |
| 248 | TPM_LONG, /* 170 */ |
| 249 | TPM_UNDEFINED, |
| 250 | TPM_UNDEFINED, |
| 251 | TPM_UNDEFINED, |
| 252 | TPM_UNDEFINED, |
| 253 | TPM_UNDEFINED, /* 175 */ |
| 254 | TPM_UNDEFINED, |
| 255 | TPM_UNDEFINED, |
| 256 | TPM_UNDEFINED, |
| 257 | TPM_UNDEFINED, |
| 258 | TPM_MEDIUM, /* 180 */ |
| 259 | TPM_SHORT, |
| 260 | TPM_MEDIUM, |
| 261 | TPM_MEDIUM, |
| 262 | TPM_MEDIUM, |
| 263 | TPM_MEDIUM, /* 185 */ |
| 264 | TPM_SHORT, |
| 265 | TPM_UNDEFINED, |
| 266 | TPM_UNDEFINED, |
| 267 | TPM_UNDEFINED, |
| 268 | TPM_UNDEFINED, /* 190 */ |
| 269 | TPM_UNDEFINED, |
| 270 | TPM_UNDEFINED, |
| 271 | TPM_UNDEFINED, |
| 272 | TPM_UNDEFINED, |
| 273 | TPM_UNDEFINED, /* 195 */ |
| 274 | TPM_UNDEFINED, |
| 275 | TPM_UNDEFINED, |
| 276 | TPM_UNDEFINED, |
| 277 | TPM_UNDEFINED, |
| 278 | TPM_SHORT, /* 200 */ |
| 279 | TPM_UNDEFINED, |
| 280 | TPM_UNDEFINED, |
| 281 | TPM_UNDEFINED, |
| 282 | TPM_SHORT, |
| 283 | TPM_SHORT, /* 205 */ |
| 284 | TPM_SHORT, |
| 285 | TPM_SHORT, |
| 286 | TPM_SHORT, |
| 287 | TPM_SHORT, |
| 288 | TPM_MEDIUM, /* 210 */ |
| 289 | TPM_UNDEFINED, |
| 290 | TPM_MEDIUM, |
| 291 | TPM_MEDIUM, |
| 292 | TPM_MEDIUM, |
| 293 | TPM_UNDEFINED, /* 215 */ |
| 294 | TPM_MEDIUM, |
| 295 | TPM_UNDEFINED, |
| 296 | TPM_UNDEFINED, |
| 297 | TPM_SHORT, |
| 298 | TPM_SHORT, /* 220 */ |
| 299 | TPM_SHORT, |
| 300 | TPM_SHORT, |
| 301 | TPM_SHORT, |
| 302 | TPM_SHORT, |
| 303 | TPM_UNDEFINED, /* 225 */ |
| 304 | TPM_UNDEFINED, |
| 305 | TPM_UNDEFINED, |
| 306 | TPM_UNDEFINED, |
| 307 | TPM_UNDEFINED, |
| 308 | TPM_SHORT, /* 230 */ |
| 309 | TPM_LONG, |
| 310 | TPM_MEDIUM, |
| 311 | TPM_UNDEFINED, |
| 312 | TPM_UNDEFINED, |
| 313 | TPM_UNDEFINED, /* 235 */ |
| 314 | TPM_UNDEFINED, |
| 315 | TPM_UNDEFINED, |
| 316 | TPM_UNDEFINED, |
| 317 | TPM_UNDEFINED, |
| 318 | TPM_SHORT, /* 240 */ |
| 319 | TPM_UNDEFINED, |
| 320 | TPM_MEDIUM, |
| 321 | }; |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | static void user_reader_timeout(unsigned long ptr) |
| 324 | { |
| 325 | struct tpm_chip *chip = (struct tpm_chip *) ptr; |
| 326 | |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 327 | schedule_work(&chip->work); |
| 328 | } |
| 329 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 330 | static void timeout_work(struct work_struct *work) |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 331 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 332 | struct tpm_chip *chip = container_of(work, struct tpm_chip, work); |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 333 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 334 | mutex_lock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | atomic_set(&chip->data_pending, 0); |
| 336 | memset(chip->data_buffer, 0, TPM_BUFSIZE); |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 337 | mutex_unlock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | /* |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 341 | * Returns max number of jiffies to wait |
| 342 | */ |
| 343 | unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, |
| 344 | u32 ordinal) |
| 345 | { |
| 346 | int duration_idx = TPM_UNDEFINED; |
| 347 | int duration = 0; |
| 348 | |
| 349 | if (ordinal < TPM_MAX_ORDINAL) |
| 350 | duration_idx = tpm_ordinal_duration[ordinal]; |
| 351 | else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
| 352 | TPM_MAX_PROTECTED_ORDINAL) |
| 353 | duration_idx = |
| 354 | tpm_protected_ordinal_duration[ordinal & |
| 355 | TPM_PROTECTED_ORDINAL_MASK]; |
| 356 | |
| 357 | if (duration_idx != TPM_UNDEFINED) |
Kylene Jo Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 358 | duration = chip->vendor.duration[duration_idx]; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 359 | if (duration <= 0) |
| 360 | return 2 * 60 * HZ; |
| 361 | else |
| 362 | return duration; |
| 363 | } |
| 364 | EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); |
| 365 | |
| 366 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | * Internal kernel interface to transmit TPM commands |
| 368 | */ |
| 369 | static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, |
| 370 | size_t bufsiz) |
| 371 | { |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 372 | ssize_t rc; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 373 | u32 count, ordinal; |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 374 | unsigned long stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 376 | count = be32_to_cpu(*((__be32 *) (buf + 2))); |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 377 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | if (count == 0) |
| 379 | return -ENODATA; |
| 380 | if (count > bufsiz) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 381 | dev_err(chip->dev, |
Al Viro | b76be68 | 2005-04-26 07:43:41 -0700 | [diff] [blame] | 382 | "invalid count value %x %zx \n", count, bufsiz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return -E2BIG; |
| 384 | } |
| 385 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 386 | mutex_lock(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 388 | if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 389 | dev_err(chip->dev, |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 390 | "tpm_transmit: tpm_send: error %zd\n", rc); |
| 391 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 394 | if (chip->vendor.irq) |
| 395 | goto out_recv; |
| 396 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 397 | stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | do { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 399 | u8 status = chip->vendor.status(chip); |
| 400 | if ((status & chip->vendor.req_complete_mask) == |
| 401 | chip->vendor.req_complete_val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | goto out_recv; |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 403 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 404 | if ((status == chip->vendor.req_canceled)) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 405 | dev_err(chip->dev, "Operation Canceled\n"); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 406 | rc = -ECANCELED; |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | msleep(TPM_TIMEOUT); /* CHECK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | rmb(); |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 412 | } while (time_before(jiffies, stop)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 414 | chip->vendor.cancel(chip); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 415 | dev_err(chip->dev, "Operation Timed out\n"); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 416 | rc = -ETIME; |
| 417 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | out_recv: |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 420 | rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 421 | if (rc < 0) |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 422 | dev_err(chip->dev, |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 423 | "tpm_transmit: tpm_recv: error %zd\n", rc); |
| 424 | out: |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 425 | mutex_unlock(&chip->tpm_mutex); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 426 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | #define TPM_DIGEST_SIZE 20 |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 430 | #define TPM_ERROR_SIZE 10 |
| 431 | #define TPM_RET_CODE_IDX 6 |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 432 | |
| 433 | enum tpm_capabilities { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 434 | TPM_CAP_FLAG = cpu_to_be32(4), |
| 435 | TPM_CAP_PROP = cpu_to_be32(5), |
| 436 | CAP_VERSION_1_1 = cpu_to_be32(0x06), |
| 437 | CAP_VERSION_1_2 = cpu_to_be32(0x1A) |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | enum tpm_sub_capabilities { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 441 | TPM_CAP_PROP_PCR = cpu_to_be32(0x101), |
| 442 | TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103), |
| 443 | TPM_CAP_FLAG_PERM = cpu_to_be32(0x108), |
| 444 | TPM_CAP_FLAG_VOL = cpu_to_be32(0x109), |
| 445 | TPM_CAP_PROP_OWNER = cpu_to_be32(0x111), |
| 446 | TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115), |
| 447 | TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120), |
| 448 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 449 | }; |
| 450 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 451 | static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, |
| 452 | int len, const char *desc) |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 453 | { |
| 454 | int err; |
| 455 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 456 | len = tpm_transmit(chip,(u8 *) cmd, len); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 457 | if (len < 0) |
| 458 | return len; |
| 459 | if (len == TPM_ERROR_SIZE) { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 460 | err = be32_to_cpu(cmd->header.out.return_code); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 461 | dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); |
| 462 | return err; |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 467 | #define TPM_INTERNAL_RESULT_SIZE 200 |
| 468 | #define TPM_TAG_RQU_COMMAND cpu_to_be16(193) |
| 469 | #define TPM_ORD_GET_CAP cpu_to_be32(101) |
| 470 | |
| 471 | static const struct tpm_input_header tpm_getcap_header = { |
| 472 | .tag = TPM_TAG_RQU_COMMAND, |
| 473 | .length = cpu_to_be32(22), |
| 474 | .ordinal = TPM_ORD_GET_CAP |
| 475 | }; |
| 476 | |
| 477 | ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, |
| 478 | const char *desc) |
| 479 | { |
| 480 | struct tpm_cmd_t tpm_cmd; |
| 481 | int rc; |
| 482 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 483 | |
| 484 | tpm_cmd.header.in = tpm_getcap_header; |
| 485 | if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { |
| 486 | tpm_cmd.params.getcap_in.cap = subcap_id; |
| 487 | /*subcap field not necessary */ |
| 488 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); |
| 489 | tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); |
| 490 | } else { |
| 491 | if (subcap_id == TPM_CAP_FLAG_PERM || |
| 492 | subcap_id == TPM_CAP_FLAG_VOL) |
| 493 | tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG; |
| 494 | else |
| 495 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 496 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 497 | tpm_cmd.params.getcap_in.subcap = subcap_id; |
| 498 | } |
| 499 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); |
| 500 | if (!rc) |
| 501 | *cap = tpm_cmd.params.getcap_out.cap; |
| 502 | return rc; |
| 503 | } |
| 504 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 505 | void tpm_gen_interrupt(struct tpm_chip *chip) |
| 506 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 507 | struct tpm_cmd_t tpm_cmd; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 508 | ssize_t rc; |
| 509 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 510 | tpm_cmd.header.in = tpm_getcap_header; |
| 511 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 512 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 513 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 514 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 515 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 516 | "attempting to determine the timeouts"); |
| 517 | } |
| 518 | EXPORT_SYMBOL_GPL(tpm_gen_interrupt); |
| 519 | |
| 520 | void tpm_get_timeouts(struct tpm_chip *chip) |
| 521 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 522 | struct tpm_cmd_t tpm_cmd; |
| 523 | struct timeout_t *timeout_cap; |
| 524 | struct duration_t *duration_cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 525 | ssize_t rc; |
| 526 | u32 timeout; |
| 527 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 528 | tpm_cmd.header.in = tpm_getcap_header; |
| 529 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 530 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 531 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 532 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 533 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 534 | "attempting to determine the timeouts"); |
| 535 | if (rc) |
| 536 | goto duration; |
| 537 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 538 | if (be32_to_cpu(tpm_cmd.header.out.length) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 539 | != 4 * sizeof(u32)) |
| 540 | goto duration; |
| 541 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 542 | timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 543 | /* Don't overwrite default if value is 0 */ |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 544 | timeout = be32_to_cpu(timeout_cap->a); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 545 | if (timeout) |
Marcin Obara | 9e5b1b1 | 2008-10-15 22:04:34 -0700 | [diff] [blame] | 546 | chip->vendor.timeout_a = usecs_to_jiffies(timeout); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 547 | timeout = be32_to_cpu(timeout_cap->b); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 548 | if (timeout) |
Marcin Obara | 9e5b1b1 | 2008-10-15 22:04:34 -0700 | [diff] [blame] | 549 | chip->vendor.timeout_b = usecs_to_jiffies(timeout); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 550 | timeout = be32_to_cpu(timeout_cap->c); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 551 | if (timeout) |
Marcin Obara | 9e5b1b1 | 2008-10-15 22:04:34 -0700 | [diff] [blame] | 552 | chip->vendor.timeout_c = usecs_to_jiffies(timeout); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 553 | timeout = be32_to_cpu(timeout_cap->d); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 554 | if (timeout) |
Marcin Obara | 9e5b1b1 | 2008-10-15 22:04:34 -0700 | [diff] [blame] | 555 | chip->vendor.timeout_d = usecs_to_jiffies(timeout); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 556 | |
| 557 | duration: |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 558 | tpm_cmd.header.in = tpm_getcap_header; |
| 559 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 560 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 561 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 562 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 563 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 564 | "attempting to determine the durations"); |
| 565 | if (rc) |
| 566 | return; |
| 567 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 568 | if (be32_to_cpu(tpm_cmd.header.out.return_code) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 569 | != 3 * sizeof(u32)) |
| 570 | return; |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 571 | duration_cap = &tpm_cmd.params.getcap_out.cap.duration; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 572 | chip->vendor.duration[TPM_SHORT] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 573 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short)); |
Valdis Kletnieks | 292cf4a | 2008-10-15 22:04:35 -0700 | [diff] [blame] | 574 | /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above |
| 575 | * value wrong and apparently reports msecs rather than usecs. So we |
| 576 | * fix up the resulting too-small TPM_SHORT value to make things work. |
| 577 | */ |
| 578 | if (chip->vendor.duration[TPM_SHORT] < (HZ/100)) |
| 579 | chip->vendor.duration[TPM_SHORT] = HZ; |
| 580 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 581 | chip->vendor.duration[TPM_MEDIUM] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 582 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium)); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 583 | chip->vendor.duration[TPM_LONG] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 584 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long)); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 585 | } |
| 586 | EXPORT_SYMBOL_GPL(tpm_get_timeouts); |
| 587 | |
| 588 | void tpm_continue_selftest(struct tpm_chip *chip) |
| 589 | { |
| 590 | u8 data[] = { |
| 591 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 592 | 0, 0, 0, 10, /* length */ |
| 593 | 0, 0, 0, 83, /* TPM_ORD_GetCapability */ |
| 594 | }; |
| 595 | |
| 596 | tpm_transmit(chip, data, sizeof(data)); |
| 597 | } |
| 598 | EXPORT_SYMBOL_GPL(tpm_continue_selftest); |
| 599 | |
| 600 | ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr, |
| 601 | char *buf) |
| 602 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 603 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 604 | ssize_t rc; |
| 605 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 606 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 607 | "attempting to determine the permanent enabled state"); |
| 608 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 609 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 610 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 611 | rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 612 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 613 | } |
| 614 | EXPORT_SYMBOL_GPL(tpm_show_enabled); |
| 615 | |
| 616 | ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr, |
| 617 | char *buf) |
| 618 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 619 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 620 | ssize_t rc; |
| 621 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 622 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 623 | "attempting to determine the permanent active state"); |
| 624 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 625 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 626 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 627 | rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 628 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 629 | } |
| 630 | EXPORT_SYMBOL_GPL(tpm_show_active); |
| 631 | |
| 632 | ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr, |
| 633 | char *buf) |
| 634 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 635 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 636 | ssize_t rc; |
| 637 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 638 | rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, |
| 639 | "attempting to determine the owner state"); |
| 640 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 641 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 642 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 643 | rc = sprintf(buf, "%d\n", cap.owned); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 644 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 645 | } |
| 646 | EXPORT_SYMBOL_GPL(tpm_show_owned); |
| 647 | |
| 648 | ssize_t tpm_show_temp_deactivated(struct device * dev, |
| 649 | struct device_attribute * attr, char *buf) |
| 650 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 651 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 652 | ssize_t rc; |
| 653 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 654 | rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, |
| 655 | "attempting to determine the temporary state"); |
| 656 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 657 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 658 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 659 | rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 660 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 661 | } |
| 662 | EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated); |
| 663 | |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 664 | static const u8 pcrread[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 666 | 0, 0, 0, 14, /* length */ |
| 667 | 0, 0, 0, 21, /* TPM_ORD_PcrRead */ |
| 668 | 0, 0, 0, 0 /* PCR index */ |
| 669 | }; |
| 670 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 671 | ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, |
| 672 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 674 | cap_t cap; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 675 | u8 *data; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 676 | ssize_t rc; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 677 | int i, j, num_pcrs; |
| 678 | __be32 index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | char *str = buf; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 680 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 682 | data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL); |
| 683 | if (!data) |
| 684 | return -ENOMEM; |
| 685 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 686 | rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 687 | "attempting to determine the number of PCRS"); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 688 | if (rc) |
Kylene Hall | e234bc9 | 2005-06-23 22:02:08 -0700 | [diff] [blame] | 689 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 691 | num_pcrs = be32_to_cpu(cap.num_pcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | for (i = 0; i < num_pcrs; i++) { |
| 693 | memcpy(data, pcrread, sizeof(pcrread)); |
| 694 | index = cpu_to_be32(i); |
| 695 | memcpy(data + 10, &index, 4); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 696 | rc = transmit_cmd(chip, (struct tpm_cmd_t *)data, |
| 697 | TPM_INTERNAL_RESULT_SIZE, |
| 698 | "attempting to read a PCR"); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 699 | if (rc) |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 700 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | str += sprintf(str, "PCR-%02d: ", i); |
| 702 | for (j = 0; j < TPM_DIGEST_SIZE; j++) |
| 703 | str += sprintf(str, "%02X ", *(data + 10 + j)); |
| 704 | str += sprintf(str, "\n"); |
| 705 | } |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 706 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return str - buf; |
| 708 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 709 | EXPORT_SYMBOL_GPL(tpm_show_pcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | #define READ_PUBEK_RESULT_SIZE 314 |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 712 | #define TPM_ORD_READPUBEK cpu_to_be32(124) |
| 713 | struct tpm_input_header tpm_readpubek_header = { |
| 714 | .tag = TPM_TAG_RQU_COMMAND, |
| 715 | .length = cpu_to_be32(30), |
| 716 | .ordinal = TPM_ORD_READPUBEK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | }; |
| 718 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 719 | ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr, |
| 720 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 722 | u8 *data; |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 723 | struct tpm_cmd_t tpm_cmd; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 724 | ssize_t err; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 725 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | char *str = buf; |
| 727 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 728 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 730 | tpm_cmd.header.in = tpm_readpubek_header; |
| 731 | err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 732 | "attempting to read the PUBEK"); |
| 733 | if (err) |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 734 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | ignore header 10 bytes |
| 738 | algorithm 32 bits (1 == RSA ) |
| 739 | encscheme 16 bits |
| 740 | sigscheme 16 bits |
| 741 | parameters (RSA 12->bytes: keybit, #primes, expbit) |
| 742 | keylenbytes 32 bits |
| 743 | 256 byte modulus |
| 744 | ignore checksum 20 bytes |
| 745 | */ |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 746 | data = tpm_cmd.params.readpubek_out_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | str += |
| 748 | sprintf(str, |
| 749 | "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n" |
| 750 | "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X" |
| 751 | " %02X %02X %02X %02X %02X %02X %02X %02X\n" |
| 752 | "Modulus length: %d\nModulus: \n", |
| 753 | data[10], data[11], data[12], data[13], data[14], |
| 754 | data[15], data[16], data[17], data[22], data[23], |
| 755 | data[24], data[25], data[26], data[27], data[28], |
| 756 | data[29], data[30], data[31], data[32], data[33], |
Kylene Jo Hall | 1dda8ab | 2005-06-25 14:55:40 -0700 | [diff] [blame] | 757 | be32_to_cpu(*((__be32 *) (data + 34)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | for (i = 0; i < 256; i++) { |
Kylene Jo Hall | 1dda8ab | 2005-06-25 14:55:40 -0700 | [diff] [blame] | 760 | str += sprintf(str, "%02X ", data[i + 38]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if ((i + 1) % 16 == 0) |
| 762 | str += sprintf(str, "\n"); |
| 763 | } |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 764 | out: |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 765 | rc = str - buf; |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 766 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 768 | EXPORT_SYMBOL_GPL(tpm_show_pubek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 771 | ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr, |
| 772 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 774 | cap_t cap; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 775 | ssize_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | char *str = buf; |
| 777 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 778 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 779 | "attempting to determine the manufacturer"); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 780 | if (rc) |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 781 | return 0; |
| 782 | str += sprintf(str, "Manufacturer: 0x%x\n", |
| 783 | be32_to_cpu(cap.manufacturer_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 785 | rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, |
| 786 | "attempting to determine the 1.1 version"); |
| 787 | if (rc) |
| 788 | return 0; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 789 | str += sprintf(str, |
| 790 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 791 | cap.tpm_version.Major, cap.tpm_version.Minor, |
| 792 | cap.tpm_version.revMajor, cap.tpm_version.revMinor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return str - buf; |
| 794 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 795 | EXPORT_SYMBOL_GPL(tpm_show_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 797 | ssize_t tpm_show_caps_1_2(struct device * dev, |
| 798 | struct device_attribute * attr, char *buf) |
| 799 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 800 | cap_t cap; |
| 801 | ssize_t rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 802 | char *str = buf; |
| 803 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 804 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
| 805 | "attempting to determine the manufacturer"); |
| 806 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 807 | return 0; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 808 | str += sprintf(str, "Manufacturer: 0x%x\n", |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 809 | be32_to_cpu(cap.manufacturer_id)); |
| 810 | rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, |
| 811 | "attempting to determine the 1.2 version"); |
| 812 | if (rc) |
| 813 | return 0; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 814 | str += sprintf(str, |
| 815 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame^] | 816 | cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor, |
| 817 | cap.tpm_version_1_2.revMajor, |
| 818 | cap.tpm_version_1_2.revMinor); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 819 | return str - buf; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(tpm_show_caps_1_2); |
| 822 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 823 | ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr, |
| 824 | const char *buf, size_t count) |
| 825 | { |
| 826 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 827 | if (chip == NULL) |
| 828 | return 0; |
| 829 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 830 | chip->vendor.cancel(chip); |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 831 | return count; |
| 832 | } |
| 833 | EXPORT_SYMBOL_GPL(tpm_store_cancel); |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* |
| 836 | * Device file system interface to the TPM |
Rajiv Andrade | f89c5ed | 2008-10-11 09:03:39 +1100 | [diff] [blame] | 837 | * |
| 838 | * It's assured that the chip will be opened just once, |
| 839 | * by the check of is_open variable, which is protected |
| 840 | * by driver_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | */ |
| 842 | int tpm_open(struct inode *inode, struct file *file) |
| 843 | { |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 844 | int minor = iminor(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | struct tpm_chip *chip = NULL, *pos; |
| 846 | |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 847 | rcu_read_lock(); |
| 848 | list_for_each_entry_rcu(pos, &tpm_chip_list, list) { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 849 | if (pos->vendor.miscdev.minor == minor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | chip = pos; |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 851 | get_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | } |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 855 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 857 | if (!chip) |
| 858 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
Rajiv Andrade | dc36d32 | 2008-10-11 09:04:02 +1100 | [diff] [blame] | 860 | if (test_and_set_bit(0, &chip->is_open)) { |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 861 | dev_dbg(chip->dev, "Another process owns this TPM\n"); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 862 | put_device(chip->dev); |
| 863 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); |
| 867 | if (chip->data_buffer == NULL) { |
Rajiv Andrade | dc36d32 | 2008-10-11 09:04:02 +1100 | [diff] [blame] | 868 | clear_bit(0, &chip->is_open); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 869 | put_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return -ENOMEM; |
| 871 | } |
| 872 | |
| 873 | atomic_set(&chip->data_pending, 0); |
| 874 | |
| 875 | file->private_data = chip; |
| 876 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | EXPORT_SYMBOL_GPL(tpm_open); |
| 879 | |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 880 | /* |
| 881 | * Called on file close |
| 882 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | int tpm_release(struct inode *inode, struct file *file) |
| 884 | { |
| 885 | struct tpm_chip *chip = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Rajiv Andrade | 4bdec11 | 2008-10-11 09:05:20 +1100 | [diff] [blame] | 887 | del_singleshot_timer_sync(&chip->user_read_timer); |
Parag Warudkar | 8e39c93 | 2007-05-08 00:31:09 -0700 | [diff] [blame] | 888 | flush_scheduled_work(); |
Kylene Hall | 5e976d5 | 2005-06-23 22:02:03 -0700 | [diff] [blame] | 889 | file->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | atomic_set(&chip->data_pending, 0); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 891 | kfree(chip->data_buffer); |
Rajiv Andrade | dc36d32 | 2008-10-11 09:04:02 +1100 | [diff] [blame] | 892 | clear_bit(0, &chip->is_open); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 893 | put_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return 0; |
| 895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | EXPORT_SYMBOL_GPL(tpm_release); |
| 897 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 898 | ssize_t tpm_write(struct file *file, const char __user *buf, |
Kylene Jo Hall | 3c2f606 | 2006-04-22 02:36:56 -0700 | [diff] [blame] | 899 | size_t size, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
| 901 | struct tpm_chip *chip = file->private_data; |
Michael Halcrow | 0147600 | 2008-07-25 19:45:00 -0700 | [diff] [blame] | 902 | size_t in_size = size, out_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* cannot perform a write until the read has cleared |
| 905 | either via tpm_read or a user_read_timer timeout */ |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 906 | while (atomic_read(&chip->data_pending) != 0) |
| 907 | msleep(TPM_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 909 | mutex_lock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | if (in_size > TPM_BUFSIZE) |
| 912 | in_size = TPM_BUFSIZE; |
| 913 | |
| 914 | if (copy_from_user |
| 915 | (chip->data_buffer, (void __user *) buf, in_size)) { |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 916 | mutex_unlock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return -EFAULT; |
| 918 | } |
| 919 | |
| 920 | /* atomic tpm command send and result receive */ |
| 921 | out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); |
| 922 | |
| 923 | atomic_set(&chip->data_pending, out_size); |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 924 | mutex_unlock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
| 926 | /* Set a timeout by which the reader must come claim the result */ |
Kylene Hall | fe3fd483 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 927 | mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | return in_size; |
| 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | EXPORT_SYMBOL_GPL(tpm_write); |
| 932 | |
Kylene Jo Hall | 3c2f606 | 2006-04-22 02:36:56 -0700 | [diff] [blame] | 933 | ssize_t tpm_read(struct file *file, char __user *buf, |
| 934 | size_t size, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
| 936 | struct tpm_chip *chip = file->private_data; |
Michael Halcrow | 0147600 | 2008-07-25 19:45:00 -0700 | [diff] [blame] | 937 | ssize_t ret_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 939 | del_singleshot_timer_sync(&chip->user_read_timer); |
Kylene Jo Hall | ba39611 | 2005-11-18 01:10:57 -0800 | [diff] [blame] | 940 | flush_scheduled_work(); |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 941 | ret_size = atomic_read(&chip->data_pending); |
| 942 | atomic_set(&chip->data_pending, 0); |
| 943 | if (ret_size > 0) { /* relay data */ |
| 944 | if (size < ret_size) |
| 945 | ret_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 947 | mutex_lock(&chip->buffer_mutex); |
Kylene Jo Hall | f6a2382 | 2005-11-13 16:07:42 -0800 | [diff] [blame] | 948 | if (copy_to_user(buf, chip->data_buffer, ret_size)) |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 949 | ret_size = -EFAULT; |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 950 | mutex_unlock(&chip->buffer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | return ret_size; |
| 954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | EXPORT_SYMBOL_GPL(tpm_read); |
| 956 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 957 | void tpm_remove_hardware(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 959 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | if (chip == NULL) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 962 | dev_err(dev, "No device data found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | |
| 966 | spin_lock(&driver_lock); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 967 | list_del_rcu(&chip->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | spin_unlock(&driver_lock); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 969 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 971 | misc_deregister(&chip->vendor.miscdev); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 972 | sysfs_remove_group(&dev->kobj, chip->vendor.attr_group); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 973 | tpm_bios_log_teardown(chip->bios_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 975 | /* write it this way to be explicit (chip->dev == dev) */ |
| 976 | put_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 978 | EXPORT_SYMBOL_GPL(tpm_remove_hardware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | /* |
| 981 | * We are about to suspend. Save the TPM state |
| 982 | * so that it can be restored. |
| 983 | */ |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 984 | int tpm_pm_suspend(struct device *dev, pm_message_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 986 | struct tpm_chip *chip = dev_get_drvdata(dev); |
David Smith | 2490c68 | 2008-01-14 00:55:12 -0800 | [diff] [blame] | 987 | u8 savestate[] = { |
| 988 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 989 | 0, 0, 0, 10, /* blob length (in bytes) */ |
| 990 | 0, 0, 0, 152 /* TPM_ORD_SaveState */ |
| 991 | }; |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (chip == NULL) |
| 994 | return -ENODEV; |
| 995 | |
| 996 | tpm_transmit(chip, savestate, sizeof(savestate)); |
| 997 | return 0; |
| 998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
| 1000 | |
| 1001 | /* |
| 1002 | * Resume from a power safe. The BIOS already restored |
| 1003 | * the TPM state. |
| 1004 | */ |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1005 | int tpm_pm_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1007 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (chip == NULL) |
| 1010 | return -ENODEV; |
| 1011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | return 0; |
| 1013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
| 1015 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1016 | /* In case vendor provided release function, call it too.*/ |
| 1017 | |
| 1018 | void tpm_dev_vendor_release(struct tpm_chip *chip) |
| 1019 | { |
| 1020 | if (chip->vendor.release) |
| 1021 | chip->vendor.release(chip->dev); |
| 1022 | |
| 1023 | clear_bit(chip->dev_num, dev_mask); |
| 1024 | kfree(chip->vendor.miscdev.name); |
| 1025 | } |
| 1026 | EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); |
| 1027 | |
| 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | /* |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1030 | * Once all references to platform device are down to 0, |
| 1031 | * release all allocated structures. |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1032 | */ |
Stephen Rothwell | cbb2ed4 | 2008-10-18 20:25:46 -0700 | [diff] [blame] | 1033 | void tpm_dev_release(struct device *dev) |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1034 | { |
| 1035 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 1036 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1037 | tpm_dev_vendor_release(chip); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1038 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1039 | chip->release(dev); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1040 | kfree(chip); |
| 1041 | } |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1042 | EXPORT_SYMBOL_GPL(tpm_dev_release); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1043 | |
| 1044 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | * Called from tpm_<specific>.c probe function only for devices |
| 1046 | * the driver has determined it should claim. Prior to calling |
| 1047 | * this function the specific probe function has called pci_enable_device |
| 1048 | * upon errant exit from this function specific probe function should call |
| 1049 | * pci_disable_device |
| 1050 | */ |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1051 | struct tpm_chip *tpm_register_hardware(struct device *dev, |
| 1052 | const struct tpm_vendor_specific *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
Kylene Jo Hall | 6f9becc | 2005-06-25 14:55:41 -0700 | [diff] [blame] | 1054 | #define DEVNAME_SIZE 7 |
| 1055 | |
| 1056 | char *devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | struct tpm_chip *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | /* Driver specific per-device data */ |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1060 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Parag Warudkar | 8e39c93 | 2007-05-08 00:31:09 -0700 | [diff] [blame] | 1061 | devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL); |
| 1062 | |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1063 | if (chip == NULL || devname == NULL) |
| 1064 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 1066 | mutex_init(&chip->buffer_mutex); |
| 1067 | mutex_init(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | INIT_LIST_HEAD(&chip->list); |
| 1069 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1070 | INIT_WORK(&chip->work, timeout_work); |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 1071 | |
Jiri Slaby | 40565f1 | 2007-02-12 00:52:31 -0800 | [diff] [blame] | 1072 | setup_timer(&chip->user_read_timer, user_reader_timeout, |
| 1073 | (unsigned long)chip); |
Kylene Hall | fe3fd483 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 1074 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1075 | memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1077 | chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1079 | if (chip->dev_num >= TPM_NUM_DEVICES) { |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1080 | dev_err(dev, "No available tpm device numbers\n"); |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1081 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } else if (chip->dev_num == 0) |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1083 | chip->vendor.miscdev.minor = TPM_MINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | else |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1085 | chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1087 | set_bit(chip->dev_num, dev_mask); |
| 1088 | |
Kylene Jo Hall | 6f9becc | 2005-06-25 14:55:41 -0700 | [diff] [blame] | 1089 | scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1090 | chip->vendor.miscdev.name = devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 1092 | chip->vendor.miscdev.parent = dev; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1093 | chip->dev = get_device(dev); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1094 | chip->release = dev->release; |
| 1095 | dev->release = tpm_dev_release; |
| 1096 | dev_set_drvdata(dev, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1098 | if (misc_register(&chip->vendor.miscdev)) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1099 | dev_err(chip->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | "unable to misc_register %s, minor %d\n", |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1101 | chip->vendor.miscdev.name, |
| 1102 | chip->vendor.miscdev.minor); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1103 | put_device(chip->dev); |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1104 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Jeff Garzik | f33d9bd | 2006-10-11 01:21:51 -0700 | [diff] [blame] | 1107 | if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) { |
Neil Horman | 5d469ec0 | 2006-12-06 20:37:08 -0800 | [diff] [blame] | 1108 | misc_deregister(&chip->vendor.miscdev); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1109 | put_device(chip->dev); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1110 | |
Jeff Garzik | f33d9bd | 2006-10-11 01:21:51 -0700 | [diff] [blame] | 1111 | return NULL; |
| 1112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1114 | chip->bios_dir = tpm_bios_log_setup(devname); |
| 1115 | |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1116 | /* Make chip available */ |
| 1117 | spin_lock(&driver_lock); |
| 1118 | list_add_rcu(&chip->list, &tpm_chip_list); |
| 1119 | spin_unlock(&driver_lock); |
| 1120 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1121 | return chip; |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1122 | |
| 1123 | out_free: |
| 1124 | kfree(chip); |
| 1125 | kfree(devname); |
| 1126 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | EXPORT_SYMBOL_GPL(tpm_register_hardware); |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 1131 | MODULE_DESCRIPTION("TPM Driver"); |
| 1132 | MODULE_VERSION("2.0"); |
| 1133 | MODULE_LICENSE("GPL"); |