Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 6 | * Dave Safford <safford@watson.ibm.com> |
| 7 | * Reiner Sailer <sailer@watson.ibm.com> |
| 8 | * Kylene Hall <kjhall@us.ibm.com> |
| 9 | * |
| 10 | * Maintained by: <tpmdd_devel@lists.sourceforge.net> |
| 11 | * |
| 12 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 13 | * Specifications at www.trustedcomputinggroup.org |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License as |
| 17 | * published by the Free Software Foundation, version 2 of the |
| 18 | * License. |
| 19 | * |
| 20 | * Note, the TPM chip is not interrupt driven (only polling) |
| 21 | * and can have very long timeouts (minutes!). Hence the unusual |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 22 | * calls to msleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/poll.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include "tpm.h" |
| 30 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 31 | enum tpm_const { |
| 32 | TPM_MINOR = 224, /* officially assigned */ |
| 33 | TPM_BUFSIZE = 2048, |
| 34 | TPM_NUM_DEVICES = 256, |
| 35 | TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int)) |
| 36 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Kylene 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 Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 51 | static int dev_mask[TPM_NUM_MASK_ENTRIES]; |
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 | |
Kylene Jo Hall | 3c2f606 | 2006-04-22 02:36:56 -0700 | [diff] [blame] | 329 | static void timeout_work(void *ptr) |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 330 | { |
| 331 | struct tpm_chip *chip = ptr; |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | down(&chip->buffer_mutex); |
| 334 | atomic_set(&chip->data_pending, 0); |
| 335 | memset(chip->data_buffer, 0, TPM_BUFSIZE); |
| 336 | up(&chip->buffer_mutex); |
| 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) |
| 357 | duration = chip->vendor.duration[duration_idx] * HZ / 1000; |
| 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 | |
| 385 | down(&chip->tpm_mutex); |
| 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: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | up(&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 |
| 431 | #define TPM_GET_CAP_RET_SIZE_IDX 10 |
| 432 | #define TPM_GET_CAP_RET_UINT32_1_IDX 14 |
| 433 | #define TPM_GET_CAP_RET_UINT32_2_IDX 18 |
| 434 | #define TPM_GET_CAP_RET_UINT32_3_IDX 22 |
| 435 | #define TPM_GET_CAP_RET_UINT32_4_IDX 26 |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 436 | #define TPM_GET_CAP_PERM_DISABLE_IDX 16 |
| 437 | #define TPM_GET_CAP_PERM_INACTIVE_IDX 18 |
| 438 | #define TPM_GET_CAP_RET_BOOL_1_IDX 14 |
| 439 | #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16 |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 440 | |
| 441 | #define TPM_CAP_IDX 13 |
| 442 | #define TPM_CAP_SUBCAP_IDX 21 |
| 443 | |
| 444 | enum tpm_capabilities { |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 445 | TPM_CAP_FLAG = 4, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 446 | TPM_CAP_PROP = 5, |
| 447 | }; |
| 448 | |
| 449 | enum tpm_sub_capabilities { |
| 450 | TPM_CAP_PROP_PCR = 0x1, |
| 451 | TPM_CAP_PROP_MANUFACTURER = 0x3, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 452 | TPM_CAP_FLAG_PERM = 0x8, |
| 453 | TPM_CAP_FLAG_VOL = 0x9, |
| 454 | TPM_CAP_PROP_OWNER = 0x11, |
| 455 | TPM_CAP_PROP_TIS_TIMEOUT = 0x15, |
| 456 | TPM_CAP_PROP_TIS_DURATION = 0x20, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | /* |
| 460 | * This is a semi generic GetCapability command for use |
| 461 | * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG |
| 462 | * and their associated sub_capabilities. |
| 463 | */ |
| 464 | |
| 465 | static const u8 tpm_cap[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 467 | 0, 0, 0, 22, /* length */ |
| 468 | 0, 0, 0, 101, /* TPM_ORD_GetCapability */ |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 469 | 0, 0, 0, 0, /* TPM_CAP_<TYPE> */ |
| 470 | 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */ |
| 471 | 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 474 | static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len, |
| 475 | char *desc) |
| 476 | { |
| 477 | int err; |
| 478 | |
| 479 | len = tpm_transmit(chip, data, len); |
| 480 | if (len < 0) |
| 481 | return len; |
| 482 | if (len == TPM_ERROR_SIZE) { |
| 483 | err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))); |
| 484 | dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); |
| 485 | return err; |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 490 | void tpm_gen_interrupt(struct tpm_chip *chip) |
| 491 | { |
| 492 | u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)]; |
| 493 | ssize_t rc; |
| 494 | |
| 495 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 496 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 497 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT; |
| 498 | |
| 499 | rc = transmit_cmd(chip, data, sizeof(data), |
| 500 | "attempting to determine the timeouts"); |
| 501 | } |
| 502 | EXPORT_SYMBOL_GPL(tpm_gen_interrupt); |
| 503 | |
| 504 | void tpm_get_timeouts(struct tpm_chip *chip) |
| 505 | { |
| 506 | u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)]; |
| 507 | ssize_t rc; |
| 508 | u32 timeout; |
| 509 | |
| 510 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 511 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 512 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT; |
| 513 | |
| 514 | rc = transmit_cmd(chip, data, sizeof(data), |
| 515 | "attempting to determine the timeouts"); |
| 516 | if (rc) |
| 517 | goto duration; |
| 518 | |
| 519 | if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX))) |
| 520 | != 4 * sizeof(u32)) |
| 521 | goto duration; |
| 522 | |
| 523 | /* Don't overwrite default if value is 0 */ |
| 524 | timeout = |
| 525 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))); |
| 526 | if (timeout) |
| 527 | chip->vendor.timeout_a = timeout; |
| 528 | timeout = |
| 529 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX))); |
| 530 | if (timeout) |
| 531 | chip->vendor.timeout_b = timeout; |
| 532 | timeout = |
| 533 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX))); |
| 534 | if (timeout) |
| 535 | chip->vendor.timeout_c = timeout; |
| 536 | timeout = |
| 537 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX))); |
| 538 | if (timeout) |
| 539 | chip->vendor.timeout_d = timeout; |
| 540 | |
| 541 | duration: |
| 542 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 543 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 544 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION; |
| 545 | |
| 546 | rc = transmit_cmd(chip, data, sizeof(data), |
| 547 | "attempting to determine the durations"); |
| 548 | if (rc) |
| 549 | return; |
| 550 | |
| 551 | if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX))) |
| 552 | != 3 * sizeof(u32)) |
| 553 | return; |
| 554 | |
| 555 | chip->vendor.duration[TPM_SHORT] = |
| 556 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))); |
| 557 | chip->vendor.duration[TPM_MEDIUM] = |
| 558 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX))); |
| 559 | chip->vendor.duration[TPM_LONG] = |
| 560 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX))); |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(tpm_get_timeouts); |
| 563 | |
| 564 | void tpm_continue_selftest(struct tpm_chip *chip) |
| 565 | { |
| 566 | u8 data[] = { |
| 567 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 568 | 0, 0, 0, 10, /* length */ |
| 569 | 0, 0, 0, 83, /* TPM_ORD_GetCapability */ |
| 570 | }; |
| 571 | |
| 572 | tpm_transmit(chip, data, sizeof(data)); |
| 573 | } |
| 574 | EXPORT_SYMBOL_GPL(tpm_continue_selftest); |
| 575 | |
| 576 | ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr, |
| 577 | char *buf) |
| 578 | { |
| 579 | u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)]; |
| 580 | ssize_t rc; |
| 581 | |
| 582 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 583 | if (chip == NULL) |
| 584 | return -ENODEV; |
| 585 | |
| 586 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 587 | data[TPM_CAP_IDX] = TPM_CAP_FLAG; |
| 588 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM; |
| 589 | |
| 590 | rc = transmit_cmd(chip, data, sizeof(data), |
| 591 | "attemtping to determine the permanent state"); |
| 592 | if (rc) |
| 593 | return 0; |
| 594 | return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]); |
| 595 | } |
| 596 | EXPORT_SYMBOL_GPL(tpm_show_enabled); |
| 597 | |
| 598 | ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr, |
| 599 | char *buf) |
| 600 | { |
| 601 | u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)]; |
| 602 | ssize_t rc; |
| 603 | |
| 604 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 605 | if (chip == NULL) |
| 606 | return -ENODEV; |
| 607 | |
| 608 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 609 | data[TPM_CAP_IDX] = TPM_CAP_FLAG; |
| 610 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM; |
| 611 | |
| 612 | rc = transmit_cmd(chip, data, sizeof(data), |
| 613 | "attemtping to determine the permanent state"); |
| 614 | if (rc) |
| 615 | return 0; |
| 616 | return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]); |
| 617 | } |
| 618 | EXPORT_SYMBOL_GPL(tpm_show_active); |
| 619 | |
| 620 | ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr, |
| 621 | char *buf) |
| 622 | { |
| 623 | u8 data[sizeof(tpm_cap)]; |
| 624 | ssize_t rc; |
| 625 | |
| 626 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 627 | if (chip == NULL) |
| 628 | return -ENODEV; |
| 629 | |
| 630 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 631 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 632 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER; |
| 633 | |
| 634 | rc = transmit_cmd(chip, data, sizeof(data), |
| 635 | "attempting to determine the owner state"); |
| 636 | if (rc) |
| 637 | return 0; |
| 638 | return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]); |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(tpm_show_owned); |
| 641 | |
| 642 | ssize_t tpm_show_temp_deactivated(struct device * dev, |
| 643 | struct device_attribute * attr, char *buf) |
| 644 | { |
| 645 | u8 data[sizeof(tpm_cap)]; |
| 646 | ssize_t rc; |
| 647 | |
| 648 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 649 | if (chip == NULL) |
| 650 | return -ENODEV; |
| 651 | |
| 652 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 653 | data[TPM_CAP_IDX] = TPM_CAP_FLAG; |
| 654 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL; |
| 655 | |
| 656 | rc = transmit_cmd(chip, data, sizeof(data), |
| 657 | "attempting to determine the temporary state"); |
| 658 | if (rc) |
| 659 | return 0; |
| 660 | return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]); |
| 661 | } |
| 662 | EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated); |
| 663 | |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 664 | static const u8 pcrread[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 666 | 0, 0, 0, 14, /* length */ |
| 667 | 0, 0, 0, 21, /* TPM_ORD_PcrRead */ |
| 668 | 0, 0, 0, 0 /* PCR index */ |
| 669 | }; |
| 670 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 671 | ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, |
| 672 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 674 | u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)]; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 675 | ssize_t rc; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 676 | int i, j, num_pcrs; |
| 677 | __be32 index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | char *str = buf; |
| 679 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 680 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (chip == NULL) |
| 682 | return -ENODEV; |
| 683 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 684 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 685 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 686 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR; |
| 687 | |
| 688 | rc = transmit_cmd(chip, data, sizeof(data), |
| 689 | "attempting to determine the number of PCRS"); |
| 690 | if (rc) |
Kylene Hall | e234bc9 | 2005-06-23 22:02:08 -0700 | [diff] [blame] | 691 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 693 | num_pcrs = be32_to_cpu(*((__be32 *) (data + 14))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | for (i = 0; i < num_pcrs; i++) { |
| 695 | memcpy(data, pcrread, sizeof(pcrread)); |
| 696 | index = cpu_to_be32(i); |
| 697 | memcpy(data + 10, &index, 4); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 698 | rc = transmit_cmd(chip, data, sizeof(data), |
| 699 | "attempting to read a PCR"); |
| 700 | if (rc) |
Kylene Hall | e234bc9 | 2005-06-23 22:02:08 -0700 | [diff] [blame] | 701 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | str += sprintf(str, "PCR-%02d: ", i); |
| 703 | for (j = 0; j < TPM_DIGEST_SIZE; j++) |
| 704 | str += sprintf(str, "%02X ", *(data + 10 + j)); |
| 705 | str += sprintf(str, "\n"); |
| 706 | } |
Kylene Hall | e234bc9 | 2005-06-23 22:02:08 -0700 | [diff] [blame] | 707 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return str - buf; |
| 709 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 710 | EXPORT_SYMBOL_GPL(tpm_show_pcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | #define READ_PUBEK_RESULT_SIZE 314 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 713 | static const u8 readpubek[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 715 | 0, 0, 0, 30, /* length */ |
| 716 | 0, 0, 0, 124, /* TPM_ORD_ReadPubek */ |
| 717 | }; |
| 718 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 719 | ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr, |
| 720 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 722 | u8 *data; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 723 | ssize_t err; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 724 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | char *str = buf; |
| 726 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 727 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | if (chip == NULL) |
| 729 | return -ENODEV; |
| 730 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 731 | data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL); |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 732 | if (!data) |
| 733 | return -ENOMEM; |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | memcpy(data, readpubek, sizeof(readpubek)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 737 | err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE, |
| 738 | "attempting to read the PUBEK"); |
| 739 | if (err) |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 740 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | /* |
| 743 | ignore header 10 bytes |
| 744 | algorithm 32 bits (1 == RSA ) |
| 745 | encscheme 16 bits |
| 746 | sigscheme 16 bits |
| 747 | parameters (RSA 12->bytes: keybit, #primes, expbit) |
| 748 | keylenbytes 32 bits |
| 749 | 256 byte modulus |
| 750 | ignore checksum 20 bytes |
| 751 | */ |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | str += |
| 754 | sprintf(str, |
| 755 | "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n" |
| 756 | "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X" |
| 757 | " %02X %02X %02X %02X %02X %02X %02X %02X\n" |
| 758 | "Modulus length: %d\nModulus: \n", |
| 759 | data[10], data[11], data[12], data[13], data[14], |
| 760 | data[15], data[16], data[17], data[22], data[23], |
| 761 | data[24], data[25], data[26], data[27], data[28], |
| 762 | data[29], data[30], data[31], data[32], data[33], |
Kylene Jo Hall | 1dda8ab | 2005-06-25 14:55:40 -0700 | [diff] [blame] | 763 | be32_to_cpu(*((__be32 *) (data + 34)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | for (i = 0; i < 256; i++) { |
Kylene Jo Hall | 1dda8ab | 2005-06-25 14:55:40 -0700 | [diff] [blame] | 766 | str += sprintf(str, "%02X ", data[i + 38]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | if ((i + 1) % 16 == 0) |
| 768 | str += sprintf(str, "\n"); |
| 769 | } |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 770 | out: |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 771 | rc = str - buf; |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 772 | kfree(data); |
| 773 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 775 | EXPORT_SYMBOL_GPL(tpm_show_pubek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 777 | #define CAP_VERSION_1_1 6 |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 778 | #define CAP_VERSION_1_2 0x1A |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 779 | #define CAP_VERSION_IDX 13 |
Kylene Hall | dff37e4 | 2005-06-23 22:01:50 -0700 | [diff] [blame] | 780 | static const u8 cap_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 782 | 0, 0, 0, 18, /* length */ |
| 783 | 0, 0, 0, 101, /* TPM_ORD_GetCapability */ |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 784 | 0, 0, 0, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | 0, 0, 0, 0 |
| 786 | }; |
| 787 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 788 | ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr, |
| 789 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 791 | u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)]; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 792 | ssize_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | char *str = buf; |
| 794 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 795 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | if (chip == NULL) |
| 797 | return -ENODEV; |
| 798 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 799 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 800 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 801 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 803 | rc = transmit_cmd(chip, data, sizeof(data), |
| 804 | "attempting to determine the manufacturer"); |
| 805 | if (rc) |
| 806 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | str += sprintf(str, "Manufacturer: 0x%x\n", |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 809 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | memcpy(data, cap_version, sizeof(cap_version)); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 812 | data[CAP_VERSION_IDX] = CAP_VERSION_1_1; |
| 813 | rc = transmit_cmd(chip, data, sizeof(data), |
| 814 | "attempting to determine the 1.1 version"); |
| 815 | if (rc) |
| 816 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 818 | str += sprintf(str, |
| 819 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 820 | (int) data[14], (int) data[15], (int) data[16], |
| 821 | (int) data[17]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 823 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | return str - buf; |
| 825 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 826 | EXPORT_SYMBOL_GPL(tpm_show_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 828 | ssize_t tpm_show_caps_1_2(struct device * dev, |
| 829 | struct device_attribute * attr, char *buf) |
| 830 | { |
| 831 | u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)]; |
| 832 | ssize_t len; |
| 833 | char *str = buf; |
| 834 | |
| 835 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 836 | if (chip == NULL) |
| 837 | return -ENODEV; |
| 838 | |
| 839 | memcpy(data, tpm_cap, sizeof(tpm_cap)); |
| 840 | data[TPM_CAP_IDX] = TPM_CAP_PROP; |
| 841 | data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER; |
| 842 | |
| 843 | if ((len = tpm_transmit(chip, data, sizeof(data))) <= |
| 844 | TPM_ERROR_SIZE) { |
| 845 | dev_dbg(chip->dev, "A TPM error (%d) occurred " |
| 846 | "attempting to determine the manufacturer\n", |
| 847 | be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)))); |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | str += sprintf(str, "Manufacturer: 0x%x\n", |
| 852 | be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)))); |
| 853 | |
| 854 | memcpy(data, cap_version, sizeof(cap_version)); |
| 855 | data[CAP_VERSION_IDX] = CAP_VERSION_1_2; |
| 856 | |
| 857 | if ((len = tpm_transmit(chip, data, sizeof(data))) <= |
| 858 | TPM_ERROR_SIZE) { |
| 859 | dev_err(chip->dev, "A TPM error (%d) occurred " |
| 860 | "attempting to determine the 1.2 version\n", |
| 861 | be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)))); |
| 862 | goto out; |
| 863 | } |
| 864 | str += sprintf(str, |
| 865 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 866 | (int) data[16], (int) data[17], (int) data[18], |
| 867 | (int) data[19]); |
| 868 | |
| 869 | out: |
| 870 | return str - buf; |
| 871 | } |
| 872 | EXPORT_SYMBOL_GPL(tpm_show_caps_1_2); |
| 873 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 874 | ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr, |
| 875 | const char *buf, size_t count) |
| 876 | { |
| 877 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 878 | if (chip == NULL) |
| 879 | return 0; |
| 880 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 881 | chip->vendor.cancel(chip); |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 882 | return count; |
| 883 | } |
| 884 | EXPORT_SYMBOL_GPL(tpm_store_cancel); |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Device file system interface to the TPM |
| 888 | */ |
| 889 | int tpm_open(struct inode *inode, struct file *file) |
| 890 | { |
| 891 | int rc = 0, minor = iminor(inode); |
| 892 | struct tpm_chip *chip = NULL, *pos; |
| 893 | |
| 894 | spin_lock(&driver_lock); |
| 895 | |
| 896 | list_for_each_entry(pos, &tpm_chip_list, list) { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 897 | if (pos->vendor.miscdev.minor == minor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | chip = pos; |
| 899 | break; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | if (chip == NULL) { |
| 904 | rc = -ENODEV; |
| 905 | goto err_out; |
| 906 | } |
| 907 | |
| 908 | if (chip->num_opens) { |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 909 | dev_dbg(chip->dev, "Another process owns this TPM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | rc = -EBUSY; |
| 911 | goto err_out; |
| 912 | } |
| 913 | |
| 914 | chip->num_opens++; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 915 | get_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | spin_unlock(&driver_lock); |
| 918 | |
| 919 | chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); |
| 920 | if (chip->data_buffer == NULL) { |
| 921 | chip->num_opens--; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 922 | put_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | return -ENOMEM; |
| 924 | } |
| 925 | |
| 926 | atomic_set(&chip->data_pending, 0); |
| 927 | |
| 928 | file->private_data = chip; |
| 929 | return 0; |
| 930 | |
| 931 | err_out: |
| 932 | spin_unlock(&driver_lock); |
| 933 | return rc; |
| 934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | EXPORT_SYMBOL_GPL(tpm_open); |
| 936 | |
| 937 | int tpm_release(struct inode *inode, struct file *file) |
| 938 | { |
| 939 | struct tpm_chip *chip = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | spin_lock(&driver_lock); |
Kylene Hall | 5e976d5 | 2005-06-23 22:02:03 -0700 | [diff] [blame] | 942 | file->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | chip->num_opens--; |
Kylene Hall | fe3fd483 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 944 | del_singleshot_timer_sync(&chip->user_read_timer); |
Kylene Jo Hall | ba39611 | 2005-11-18 01:10:57 -0800 | [diff] [blame] | 945 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | atomic_set(&chip->data_pending, 0); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 947 | put_device(chip->dev); |
Kylene Hall | 5e976d5 | 2005-06-23 22:02:03 -0700 | [diff] [blame] | 948 | kfree(chip->data_buffer); |
| 949 | spin_unlock(&driver_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return 0; |
| 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | EXPORT_SYMBOL_GPL(tpm_release); |
| 953 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 954 | ssize_t tpm_write(struct file *file, const char __user *buf, |
Kylene Jo Hall | 3c2f606 | 2006-04-22 02:36:56 -0700 | [diff] [blame] | 955 | size_t size, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
| 957 | struct tpm_chip *chip = file->private_data; |
| 958 | int in_size = size, out_size; |
| 959 | |
| 960 | /* cannot perform a write until the read has cleared |
| 961 | either via tpm_read or a user_read_timer timeout */ |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 962 | while (atomic_read(&chip->data_pending) != 0) |
| 963 | msleep(TPM_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | down(&chip->buffer_mutex); |
| 966 | |
| 967 | if (in_size > TPM_BUFSIZE) |
| 968 | in_size = TPM_BUFSIZE; |
| 969 | |
| 970 | if (copy_from_user |
| 971 | (chip->data_buffer, (void __user *) buf, in_size)) { |
| 972 | up(&chip->buffer_mutex); |
| 973 | return -EFAULT; |
| 974 | } |
| 975 | |
| 976 | /* atomic tpm command send and result receive */ |
| 977 | out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); |
| 978 | |
| 979 | atomic_set(&chip->data_pending, out_size); |
| 980 | up(&chip->buffer_mutex); |
| 981 | |
| 982 | /* Set a timeout by which the reader must come claim the result */ |
Kylene Hall | fe3fd483 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 983 | mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | return in_size; |
| 986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | EXPORT_SYMBOL_GPL(tpm_write); |
| 988 | |
Kylene Jo Hall | 3c2f606 | 2006-04-22 02:36:56 -0700 | [diff] [blame] | 989 | ssize_t tpm_read(struct file *file, char __user *buf, |
| 990 | size_t size, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
| 992 | struct tpm_chip *chip = file->private_data; |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 993 | int ret_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 995 | del_singleshot_timer_sync(&chip->user_read_timer); |
Kylene Jo Hall | ba39611 | 2005-11-18 01:10:57 -0800 | [diff] [blame] | 996 | flush_scheduled_work(); |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 997 | ret_size = atomic_read(&chip->data_pending); |
| 998 | atomic_set(&chip->data_pending, 0); |
| 999 | if (ret_size > 0) { /* relay data */ |
| 1000 | if (size < ret_size) |
| 1001 | ret_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | down(&chip->buffer_mutex); |
Kylene Jo Hall | f6a2382 | 2005-11-13 16:07:42 -0800 | [diff] [blame] | 1004 | if (copy_to_user(buf, chip->data_buffer, ret_size)) |
Kylene Hall | 5b44bd5 | 2005-06-23 22:01:53 -0700 | [diff] [blame] | 1005 | ret_size = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | up(&chip->buffer_mutex); |
| 1007 | } |
| 1008 | |
| 1009 | return ret_size; |
| 1010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | EXPORT_SYMBOL_GPL(tpm_read); |
| 1012 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1013 | void tpm_remove_hardware(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1015 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (chip == NULL) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1018 | dev_err(dev, "No device data found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | spin_lock(&driver_lock); |
| 1023 | |
| 1024 | list_del(&chip->list); |
| 1025 | |
| 1026 | spin_unlock(&driver_lock); |
| 1027 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1028 | dev_set_drvdata(dev, NULL); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1029 | misc_deregister(&chip->vendor.miscdev); |
| 1030 | kfree(chip->vendor.miscdev.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1032 | sysfs_remove_group(&dev->kobj, chip->vendor.attr_group); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1033 | tpm_bios_log_teardown(chip->bios_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1035 | dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES] &= |
| 1036 | ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
| 1038 | kfree(chip); |
| 1039 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1040 | put_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1042 | EXPORT_SYMBOL_GPL(tpm_remove_hardware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | static u8 savestate[] = { |
| 1045 | 0, 193, /* TPM_TAG_RQU_COMMAND */ |
| 1046 | 0, 0, 0, 10, /* blob length (in bytes) */ |
| 1047 | 0, 0, 0, 152 /* TPM_ORD_SaveState */ |
| 1048 | }; |
| 1049 | |
| 1050 | /* |
| 1051 | * We are about to suspend. Save the TPM state |
| 1052 | * so that it can be restored. |
| 1053 | */ |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1054 | int tpm_pm_suspend(struct device *dev, pm_message_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1056 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | if (chip == NULL) |
| 1058 | return -ENODEV; |
| 1059 | |
| 1060 | tpm_transmit(chip, savestate, sizeof(savestate)); |
| 1061 | return 0; |
| 1062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
| 1064 | |
| 1065 | /* |
| 1066 | * Resume from a power safe. The BIOS already restored |
| 1067 | * the TPM state. |
| 1068 | */ |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1069 | int tpm_pm_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1071 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (chip == NULL) |
| 1074 | return -ENODEV; |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
| 1079 | |
| 1080 | /* |
| 1081 | * Called from tpm_<specific>.c probe function only for devices |
| 1082 | * the driver has determined it should claim. Prior to calling |
| 1083 | * this function the specific probe function has called pci_enable_device |
| 1084 | * upon errant exit from this function specific probe function should call |
| 1085 | * pci_disable_device |
| 1086 | */ |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1087 | struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific |
| 1088 | *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
Kylene Jo Hall | 6f9becc | 2005-06-25 14:55:41 -0700 | [diff] [blame] | 1090 | #define DEVNAME_SIZE 7 |
| 1091 | |
| 1092 | char *devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | struct tpm_chip *chip; |
| 1094 | int i, j; |
| 1095 | |
| 1096 | /* Driver specific per-device data */ |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1097 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | if (chip == NULL) |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1099 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | init_MUTEX(&chip->buffer_mutex); |
| 1102 | init_MUTEX(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | INIT_LIST_HEAD(&chip->list); |
| 1104 | |
Kylene Jo Hall | 09e12f9 | 2005-11-13 16:07:43 -0800 | [diff] [blame] | 1105 | INIT_WORK(&chip->work, timeout_work, chip); |
| 1106 | |
Kylene Hall | fe3fd483 | 2005-06-23 22:01:56 -0700 | [diff] [blame] | 1107 | init_timer(&chip->user_read_timer); |
| 1108 | chip->user_read_timer.function = user_reader_timeout; |
| 1109 | chip->user_read_timer.data = (unsigned long) chip; |
| 1110 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1111 | memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
| 1113 | chip->dev_num = -1; |
| 1114 | |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 1115 | for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++) |
| 1116 | for (j = 0; j < 8 * sizeof(int); j++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | if ((dev_mask[i] & (1 << j)) == 0) { |
Kylene Hall | 3122a88 | 2005-06-23 22:01:48 -0700 | [diff] [blame] | 1118 | chip->dev_num = |
| 1119 | i * TPM_NUM_MASK_ENTRIES + j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | dev_mask[i] |= 1 << j; |
| 1121 | goto dev_num_search_complete; |
| 1122 | } |
| 1123 | |
| 1124 | dev_num_search_complete: |
| 1125 | if (chip->dev_num < 0) { |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1126 | dev_err(dev, "No available tpm device numbers\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | kfree(chip); |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1128 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } else if (chip->dev_num == 0) |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1130 | chip->vendor.miscdev.minor = TPM_MINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | else |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1132 | chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
Kylene Jo Hall | 6f9becc | 2005-06-25 14:55:41 -0700 | [diff] [blame] | 1134 | devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL); |
| 1135 | scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1136 | chip->vendor.miscdev.name = devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1138 | chip->vendor.miscdev.dev = dev; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1139 | chip->dev = get_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1141 | if (misc_register(&chip->vendor.miscdev)) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1142 | dev_err(chip->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | "unable to misc_register %s, minor %d\n", |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1144 | chip->vendor.miscdev.name, |
| 1145 | chip->vendor.miscdev.minor); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1146 | put_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | kfree(chip); |
| 1148 | dev_mask[i] &= !(1 << j); |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1149 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Kylene Hall | 5e976d5 | 2005-06-23 22:02:03 -0700 | [diff] [blame] | 1152 | spin_lock(&driver_lock); |
| 1153 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1154 | dev_set_drvdata(dev, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | list_add(&chip->list, &tpm_chip_list); |
| 1157 | |
Kylene Hall | 5e976d5 | 2005-06-23 22:02:03 -0700 | [diff] [blame] | 1158 | spin_unlock(&driver_lock); |
| 1159 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1160 | sysfs_create_group(&dev->kobj, chip->vendor.attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1162 | chip->bios_dir = tpm_bios_log_setup(devname); |
| 1163 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1164 | return chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | EXPORT_SYMBOL_GPL(tpm_register_hardware); |
| 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 1169 | MODULE_DESCRIPTION("TPM Driver"); |
| 1170 | MODULE_VERSION("2.0"); |
| 1171 | MODULE_LICENSE("GPL"); |