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