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