blob: 9c47dc48c9fd4526ac8e8972fb80c6b21ba2cae8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Yoder8e81cc12007-08-22 14:01:04 -070010 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 Aravamudan700d8bd2005-06-23 22:01:47 -070022 * calls to msleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/poll.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/spinlock.h>
Arnd Bergmann25368ca2008-05-20 19:16:51 +020029#include <linux/smp_lock.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "tpm.h"
32
Kylene Hall3122a882005-06-23 22:01:48 -070033enum tpm_const {
34 TPM_MINOR = 224, /* officially assigned */
35 TPM_BUFSIZE = 2048,
36 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070037};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070039enum tpm_duration {
40 TPM_SHORT = 0,
41 TPM_MEDIUM = 1,
42 TPM_LONG = 2,
43 TPM_UNDEFINED,
44};
45
46#define TPM_MAX_ORDINAL 243
47#define TPM_MAX_PROTECTED_ORDINAL 12
48#define TPM_PROTECTED_ORDINAL_MASK 0xFF
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static LIST_HEAD(tpm_chip_list);
51static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070052static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070054/*
55 * Array with one entry per ordinal defining the maximum amount
56 * of time the chip could take to return the result. The ordinal
57 * designation of short, medium or long is defined in a table in
58 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
59 * values of the SHORT, MEDIUM, and LONG durations are retrieved
60 * from the chip during initialization with a call to tpm_get_timeouts.
61 */
62static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
63 TPM_UNDEFINED, /* 0 */
64 TPM_UNDEFINED,
65 TPM_UNDEFINED,
66 TPM_UNDEFINED,
67 TPM_UNDEFINED,
68 TPM_UNDEFINED, /* 5 */
69 TPM_UNDEFINED,
70 TPM_UNDEFINED,
71 TPM_UNDEFINED,
72 TPM_UNDEFINED,
73 TPM_SHORT, /* 10 */
74 TPM_SHORT,
75};
76
77static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
78 TPM_UNDEFINED, /* 0 */
79 TPM_UNDEFINED,
80 TPM_UNDEFINED,
81 TPM_UNDEFINED,
82 TPM_UNDEFINED,
83 TPM_UNDEFINED, /* 5 */
84 TPM_UNDEFINED,
85 TPM_UNDEFINED,
86 TPM_UNDEFINED,
87 TPM_UNDEFINED,
88 TPM_SHORT, /* 10 */
89 TPM_SHORT,
90 TPM_MEDIUM,
91 TPM_LONG,
92 TPM_LONG,
93 TPM_MEDIUM, /* 15 */
94 TPM_SHORT,
95 TPM_SHORT,
96 TPM_MEDIUM,
97 TPM_LONG,
98 TPM_SHORT, /* 20 */
99 TPM_SHORT,
100 TPM_MEDIUM,
101 TPM_MEDIUM,
102 TPM_MEDIUM,
103 TPM_SHORT, /* 25 */
104 TPM_SHORT,
105 TPM_MEDIUM,
106 TPM_SHORT,
107 TPM_SHORT,
108 TPM_MEDIUM, /* 30 */
109 TPM_LONG,
110 TPM_MEDIUM,
111 TPM_SHORT,
112 TPM_SHORT,
113 TPM_SHORT, /* 35 */
114 TPM_MEDIUM,
115 TPM_MEDIUM,
116 TPM_UNDEFINED,
117 TPM_UNDEFINED,
118 TPM_MEDIUM, /* 40 */
119 TPM_LONG,
120 TPM_MEDIUM,
121 TPM_SHORT,
122 TPM_SHORT,
123 TPM_SHORT, /* 45 */
124 TPM_SHORT,
125 TPM_SHORT,
126 TPM_SHORT,
127 TPM_LONG,
128 TPM_MEDIUM, /* 50 */
129 TPM_MEDIUM,
130 TPM_UNDEFINED,
131 TPM_UNDEFINED,
132 TPM_UNDEFINED,
133 TPM_UNDEFINED, /* 55 */
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_UNDEFINED,
137 TPM_UNDEFINED,
138 TPM_MEDIUM, /* 60 */
139 TPM_MEDIUM,
140 TPM_MEDIUM,
141 TPM_SHORT,
142 TPM_SHORT,
143 TPM_MEDIUM, /* 65 */
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_SHORT, /* 70 */
149 TPM_SHORT,
150 TPM_UNDEFINED,
151 TPM_UNDEFINED,
152 TPM_UNDEFINED,
153 TPM_UNDEFINED, /* 75 */
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_LONG, /* 80 */
159 TPM_UNDEFINED,
160 TPM_MEDIUM,
161 TPM_LONG,
162 TPM_SHORT,
163 TPM_UNDEFINED, /* 85 */
164 TPM_UNDEFINED,
165 TPM_UNDEFINED,
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_SHORT, /* 90 */
169 TPM_SHORT,
170 TPM_SHORT,
171 TPM_SHORT,
172 TPM_SHORT,
173 TPM_UNDEFINED, /* 95 */
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_UNDEFINED,
177 TPM_UNDEFINED,
178 TPM_MEDIUM, /* 100 */
179 TPM_SHORT,
180 TPM_SHORT,
181 TPM_UNDEFINED,
182 TPM_UNDEFINED,
183 TPM_UNDEFINED, /* 105 */
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_UNDEFINED,
187 TPM_UNDEFINED,
188 TPM_SHORT, /* 110 */
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_SHORT,
192 TPM_SHORT,
193 TPM_SHORT, /* 115 */
194 TPM_SHORT,
195 TPM_SHORT,
196 TPM_UNDEFINED,
197 TPM_UNDEFINED,
198 TPM_LONG, /* 120 */
199 TPM_LONG,
200 TPM_MEDIUM,
201 TPM_UNDEFINED,
202 TPM_SHORT,
203 TPM_SHORT, /* 125 */
204 TPM_SHORT,
205 TPM_LONG,
206 TPM_SHORT,
207 TPM_SHORT,
208 TPM_SHORT, /* 130 */
209 TPM_MEDIUM,
210 TPM_UNDEFINED,
211 TPM_SHORT,
212 TPM_MEDIUM,
213 TPM_UNDEFINED, /* 135 */
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_UNDEFINED,
217 TPM_UNDEFINED,
218 TPM_SHORT, /* 140 */
219 TPM_SHORT,
220 TPM_UNDEFINED,
221 TPM_UNDEFINED,
222 TPM_UNDEFINED,
223 TPM_UNDEFINED, /* 145 */
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_SHORT, /* 150 */
229 TPM_MEDIUM,
230 TPM_MEDIUM,
231 TPM_SHORT,
232 TPM_SHORT,
233 TPM_UNDEFINED, /* 155 */
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_SHORT, /* 160 */
239 TPM_SHORT,
240 TPM_SHORT,
241 TPM_SHORT,
242 TPM_UNDEFINED,
243 TPM_UNDEFINED, /* 165 */
244 TPM_UNDEFINED,
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_LONG, /* 170 */
249 TPM_UNDEFINED,
250 TPM_UNDEFINED,
251 TPM_UNDEFINED,
252 TPM_UNDEFINED,
253 TPM_UNDEFINED, /* 175 */
254 TPM_UNDEFINED,
255 TPM_UNDEFINED,
256 TPM_UNDEFINED,
257 TPM_UNDEFINED,
258 TPM_MEDIUM, /* 180 */
259 TPM_SHORT,
260 TPM_MEDIUM,
261 TPM_MEDIUM,
262 TPM_MEDIUM,
263 TPM_MEDIUM, /* 185 */
264 TPM_SHORT,
265 TPM_UNDEFINED,
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_UNDEFINED, /* 190 */
269 TPM_UNDEFINED,
270 TPM_UNDEFINED,
271 TPM_UNDEFINED,
272 TPM_UNDEFINED,
273 TPM_UNDEFINED, /* 195 */
274 TPM_UNDEFINED,
275 TPM_UNDEFINED,
276 TPM_UNDEFINED,
277 TPM_UNDEFINED,
278 TPM_SHORT, /* 200 */
279 TPM_UNDEFINED,
280 TPM_UNDEFINED,
281 TPM_UNDEFINED,
282 TPM_SHORT,
283 TPM_SHORT, /* 205 */
284 TPM_SHORT,
285 TPM_SHORT,
286 TPM_SHORT,
287 TPM_SHORT,
288 TPM_MEDIUM, /* 210 */
289 TPM_UNDEFINED,
290 TPM_MEDIUM,
291 TPM_MEDIUM,
292 TPM_MEDIUM,
293 TPM_UNDEFINED, /* 215 */
294 TPM_MEDIUM,
295 TPM_UNDEFINED,
296 TPM_UNDEFINED,
297 TPM_SHORT,
298 TPM_SHORT, /* 220 */
299 TPM_SHORT,
300 TPM_SHORT,
301 TPM_SHORT,
302 TPM_SHORT,
303 TPM_UNDEFINED, /* 225 */
304 TPM_UNDEFINED,
305 TPM_UNDEFINED,
306 TPM_UNDEFINED,
307 TPM_UNDEFINED,
308 TPM_SHORT, /* 230 */
309 TPM_LONG,
310 TPM_MEDIUM,
311 TPM_UNDEFINED,
312 TPM_UNDEFINED,
313 TPM_UNDEFINED, /* 235 */
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_UNDEFINED,
318 TPM_SHORT, /* 240 */
319 TPM_UNDEFINED,
320 TPM_MEDIUM,
321};
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static void user_reader_timeout(unsigned long ptr)
324{
325 struct tpm_chip *chip = (struct tpm_chip *) ptr;
326
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800327 schedule_work(&chip->work);
328}
329
David Howellsc4028952006-11-22 14:57:56 +0000330static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800331{
David Howellsc4028952006-11-22 14:57:56 +0000332 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800333
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700334 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 atomic_set(&chip->data_pending, 0);
336 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700337 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700341 * Returns max number of jiffies to wait
342 */
343unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
344 u32 ordinal)
345{
346 int duration_idx = TPM_UNDEFINED;
347 int duration = 0;
348
349 if (ordinal < TPM_MAX_ORDINAL)
350 duration_idx = tpm_ordinal_duration[ordinal];
351 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
352 TPM_MAX_PROTECTED_ORDINAL)
353 duration_idx =
354 tpm_protected_ordinal_duration[ordinal &
355 TPM_PROTECTED_ORDINAL_MASK];
356
357 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700358 duration = chip->vendor.duration[duration_idx];
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700359 if (duration <= 0)
360 return 2 * 60 * HZ;
361 else
362 return duration;
363}
364EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
365
366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Internal kernel interface to transmit TPM commands
368 */
369static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
370 size_t bufsiz)
371{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700372 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700373 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700374 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Kylene Hall81179bb2005-06-23 22:01:59 -0700376 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700377 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (count == 0)
379 return -ENODATA;
380 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800381 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700382 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -E2BIG;
384 }
385
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700386 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700388 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800389 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700390 "tpm_transmit: tpm_send: error %zd\n", rc);
391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700394 if (chip->vendor.irq)
395 goto out_recv;
396
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700397 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700399 u8 status = chip->vendor.status(chip);
400 if ((status & chip->vendor.req_complete_mask) ==
401 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700403
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700404 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800405 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700406 rc = -ECANCELED;
407 goto out;
408 }
409
410 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700412 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700414 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800415 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700416 rc = -ETIME;
417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700420 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700421 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800422 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700423 "tpm_transmit: tpm_recv: error %zd\n", rc);
424out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700425 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700426 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700430#define TPM_ERROR_SIZE 10
431#define TPM_RET_CODE_IDX 6
432#define TPM_GET_CAP_RET_SIZE_IDX 10
433#define TPM_GET_CAP_RET_UINT32_1_IDX 14
434#define TPM_GET_CAP_RET_UINT32_2_IDX 18
435#define TPM_GET_CAP_RET_UINT32_3_IDX 22
436#define TPM_GET_CAP_RET_UINT32_4_IDX 26
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700437#define TPM_GET_CAP_PERM_DISABLE_IDX 16
438#define TPM_GET_CAP_PERM_INACTIVE_IDX 18
439#define TPM_GET_CAP_RET_BOOL_1_IDX 14
440#define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700441
442#define TPM_CAP_IDX 13
443#define TPM_CAP_SUBCAP_IDX 21
444
445enum tpm_capabilities {
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700446 TPM_CAP_FLAG = 4,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700447 TPM_CAP_PROP = 5,
448};
449
450enum tpm_sub_capabilities {
451 TPM_CAP_PROP_PCR = 0x1,
452 TPM_CAP_PROP_MANUFACTURER = 0x3,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700453 TPM_CAP_FLAG_PERM = 0x8,
454 TPM_CAP_FLAG_VOL = 0x9,
455 TPM_CAP_PROP_OWNER = 0x11,
456 TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
457 TPM_CAP_PROP_TIS_DURATION = 0x20,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700458};
459
460/*
461 * This is a semi generic GetCapability command for use
462 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
463 * and their associated sub_capabilities.
464 */
465
466static const u8 tpm_cap[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 0, 193, /* TPM_TAG_RQU_COMMAND */
468 0, 0, 0, 22, /* length */
469 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700470 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
471 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
472 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700475static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
476 char *desc)
477{
478 int err;
479
480 len = tpm_transmit(chip, data, len);
481 if (len < 0)
482 return len;
483 if (len == TPM_ERROR_SIZE) {
484 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
485 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
486 return err;
487 }
488 return 0;
489}
490
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700491void tpm_gen_interrupt(struct tpm_chip *chip)
492{
493 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
494 ssize_t rc;
495
496 memcpy(data, tpm_cap, sizeof(tpm_cap));
497 data[TPM_CAP_IDX] = TPM_CAP_PROP;
498 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
499
500 rc = transmit_cmd(chip, data, sizeof(data),
501 "attempting to determine the timeouts");
502}
503EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
504
505void tpm_get_timeouts(struct tpm_chip *chip)
506{
507 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
508 ssize_t rc;
509 u32 timeout;
510
511 memcpy(data, tpm_cap, sizeof(tpm_cap));
512 data[TPM_CAP_IDX] = TPM_CAP_PROP;
513 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
514
515 rc = transmit_cmd(chip, data, sizeof(data),
516 "attempting to determine the timeouts");
517 if (rc)
518 goto duration;
519
520 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
521 != 4 * sizeof(u32))
522 goto duration;
523
524 /* Don't overwrite default if value is 0 */
525 timeout =
526 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
527 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700528 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700529 timeout =
530 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
531 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700532 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700533 timeout =
534 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
535 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700536 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700537 timeout =
538 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
539 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700540 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700541
542duration:
543 memcpy(data, tpm_cap, sizeof(tpm_cap));
544 data[TPM_CAP_IDX] = TPM_CAP_PROP;
545 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
546
547 rc = transmit_cmd(chip, data, sizeof(data),
548 "attempting to determine the durations");
549 if (rc)
550 return;
551
552 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
553 != 3 * sizeof(u32))
554 return;
555
556 chip->vendor.duration[TPM_SHORT] =
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700557 usecs_to_jiffies(be32_to_cpu
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700558 (*((__be32 *) (data +
559 TPM_GET_CAP_RET_UINT32_1_IDX))));
Valdis Kletnieks292cf4a2008-10-15 22:04:35 -0700560 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
561 * value wrong and apparently reports msecs rather than usecs. So we
562 * fix up the resulting too-small TPM_SHORT value to make things work.
563 */
564 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
565 chip->vendor.duration[TPM_SHORT] = HZ;
566
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700567 chip->vendor.duration[TPM_MEDIUM] =
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700568 usecs_to_jiffies(be32_to_cpu
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700569 (*((__be32 *) (data +
570 TPM_GET_CAP_RET_UINT32_2_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700571 chip->vendor.duration[TPM_LONG] =
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700572 usecs_to_jiffies(be32_to_cpu
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700573 (*((__be32 *) (data +
574 TPM_GET_CAP_RET_UINT32_3_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700575}
576EXPORT_SYMBOL_GPL(tpm_get_timeouts);
577
578void tpm_continue_selftest(struct tpm_chip *chip)
579{
580 u8 data[] = {
581 0, 193, /* TPM_TAG_RQU_COMMAND */
582 0, 0, 0, 10, /* length */
583 0, 0, 0, 83, /* TPM_ORD_GetCapability */
584 };
585
586 tpm_transmit(chip, data, sizeof(data));
587}
588EXPORT_SYMBOL_GPL(tpm_continue_selftest);
589
Marcin Obaraec288bd2008-07-25 19:44:59 -0700590#define TPM_INTERNAL_RESULT_SIZE 200
591
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700592ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
593 char *buf)
594{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700595 u8 *data;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700596 ssize_t rc;
597
598 struct tpm_chip *chip = dev_get_drvdata(dev);
599 if (chip == NULL)
600 return -ENODEV;
601
Marcin Obaraec288bd2008-07-25 19:44:59 -0700602 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
603 if (!data)
604 return -ENOMEM;
605
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700606 memcpy(data, tpm_cap, sizeof(tpm_cap));
607 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
608 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
609
Marcin Obaraec288bd2008-07-25 19:44:59 -0700610 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
611 "attemtping to determine the permanent enabled state");
612 if (rc) {
613 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700614 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700615 }
616
617 rc = sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
618
619 kfree(data);
620 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700621}
622EXPORT_SYMBOL_GPL(tpm_show_enabled);
623
624ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
625 char *buf)
626{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700627 u8 *data;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700628 ssize_t rc;
629
630 struct tpm_chip *chip = dev_get_drvdata(dev);
631 if (chip == NULL)
632 return -ENODEV;
633
Marcin Obaraec288bd2008-07-25 19:44:59 -0700634 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
635 if (!data)
636 return -ENOMEM;
637
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700638 memcpy(data, tpm_cap, sizeof(tpm_cap));
639 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
640 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
641
Marcin Obaraec288bd2008-07-25 19:44:59 -0700642 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
643 "attemtping to determine the permanent active state");
644 if (rc) {
645 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700646 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700647 }
648
649 rc = sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
650
651 kfree(data);
652 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700653}
654EXPORT_SYMBOL_GPL(tpm_show_active);
655
656ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
657 char *buf)
658{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700659 u8 *data;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700660 ssize_t rc;
661
662 struct tpm_chip *chip = dev_get_drvdata(dev);
663 if (chip == NULL)
664 return -ENODEV;
665
Marcin Obaraec288bd2008-07-25 19:44:59 -0700666 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
667 if (!data)
668 return -ENOMEM;
669
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700670 memcpy(data, tpm_cap, sizeof(tpm_cap));
671 data[TPM_CAP_IDX] = TPM_CAP_PROP;
672 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
673
Marcin Obaraec288bd2008-07-25 19:44:59 -0700674 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700675 "attempting to determine the owner state");
Marcin Obaraec288bd2008-07-25 19:44:59 -0700676 if (rc) {
677 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700678 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700679 }
680
681 rc = sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
682
683 kfree(data);
684 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700685}
686EXPORT_SYMBOL_GPL(tpm_show_owned);
687
688ssize_t tpm_show_temp_deactivated(struct device * dev,
689 struct device_attribute * attr, char *buf)
690{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700691 u8 *data;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700692 ssize_t rc;
693
694 struct tpm_chip *chip = dev_get_drvdata(dev);
695 if (chip == NULL)
696 return -ENODEV;
697
Marcin Obaraec288bd2008-07-25 19:44:59 -0700698 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
699 if (!data)
700 return -ENOMEM;
701
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700702 memcpy(data, tpm_cap, sizeof(tpm_cap));
703 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
704 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
705
Marcin Obaraec288bd2008-07-25 19:44:59 -0700706 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700707 "attempting to determine the temporary state");
Marcin Obaraec288bd2008-07-25 19:44:59 -0700708 if (rc) {
709 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700710 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700711 }
712
713 rc = sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
714
715 kfree(data);
716 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700717}
718EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
719
Kylene Halldff37e42005-06-23 22:01:50 -0700720static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 0, 193, /* TPM_TAG_RQU_COMMAND */
722 0, 0, 0, 14, /* length */
723 0, 0, 0, 21, /* TPM_ORD_PcrRead */
724 0, 0, 0, 0 /* PCR index */
725};
726
Kylene Hall6659ca22005-06-23 22:02:00 -0700727ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
728 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700730 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700731 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700732 int i, j, num_pcrs;
733 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 char *str = buf;
735
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800736 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (chip == NULL)
738 return -ENODEV;
739
Marcin Obaraec288bd2008-07-25 19:44:59 -0700740 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
741 if (!data)
742 return -ENOMEM;
743
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700744 memcpy(data, tpm_cap, sizeof(tpm_cap));
745 data[TPM_CAP_IDX] = TPM_CAP_PROP;
746 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
747
Marcin Obaraec288bd2008-07-25 19:44:59 -0700748 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700749 "attempting to determine the number of PCRS");
Marcin Obaraec288bd2008-07-25 19:44:59 -0700750 if (rc) {
751 kfree(data);
Kylene Halle234bc92005-06-23 22:02:08 -0700752 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Kylene Hall81179bb2005-06-23 22:01:59 -0700755 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 for (i = 0; i < num_pcrs; i++) {
757 memcpy(data, pcrread, sizeof(pcrread));
758 index = cpu_to_be32(i);
759 memcpy(data + 10, &index, 4);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700760 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700761 "attempting to read a PCR");
762 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 str += sprintf(str, "PCR-%02d: ", i);
765 for (j = 0; j < TPM_DIGEST_SIZE; j++)
766 str += sprintf(str, "%02X ", *(data + 10 + j));
767 str += sprintf(str, "\n");
768 }
Kylene Halle234bc92005-06-23 22:02:08 -0700769out:
Marcin Obaraec288bd2008-07-25 19:44:59 -0700770 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return str - buf;
772}
Kylene Hall6659ca22005-06-23 22:02:00 -0700773EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700776static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 0, 193, /* TPM_TAG_RQU_COMMAND */
778 0, 0, 0, 30, /* length */
779 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
780};
781
Kylene Hall6659ca22005-06-23 22:02:00 -0700782ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
783 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Kylene Hall2df71112005-06-23 22:01:54 -0700785 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700786 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700787 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 char *str = buf;
789
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800790 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (chip == NULL)
792 return -ENODEV;
793
Andrew Mortonb888c872005-10-30 15:03:28 -0800794 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700795 if (!data)
796 return -ENOMEM;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700800 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
801 "attempting to read the PUBEK");
802 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 ignore header 10 bytes
807 algorithm 32 bits (1 == RSA )
808 encscheme 16 bits
809 sigscheme 16 bits
810 parameters (RSA 12->bytes: keybit, #primes, expbit)
811 keylenbytes 32 bits
812 256 byte modulus
813 ignore checksum 20 bytes
814 */
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 str +=
817 sprintf(str,
818 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
819 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
820 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
821 "Modulus length: %d\nModulus: \n",
822 data[10], data[11], data[12], data[13], data[14],
823 data[15], data[16], data[17], data[22], data[23],
824 data[24], data[25], data[26], data[27], data[28],
825 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700826 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700829 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if ((i + 1) % 16 == 0)
831 str += sprintf(str, "\n");
832 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700833out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700834 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700835 kfree(data);
836 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
Kylene Hall6659ca22005-06-23 22:02:00 -0700838EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700840#define CAP_VERSION_1_1 6
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700841#define CAP_VERSION_1_2 0x1A
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700842#define CAP_VERSION_IDX 13
Kylene Halldff37e42005-06-23 22:01:50 -0700843static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 0, 193, /* TPM_TAG_RQU_COMMAND */
845 0, 0, 0, 18, /* length */
846 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700847 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 0, 0, 0, 0
849};
850
Kylene Hall6659ca22005-06-23 22:02:00 -0700851ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
852 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700854 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700855 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 char *str = buf;
857
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800858 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (chip == NULL)
860 return -ENODEV;
861
Marcin Obaraec288bd2008-07-25 19:44:59 -0700862 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
863 if (!data)
864 return -ENOMEM;
865
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700866 memcpy(data, tpm_cap, sizeof(tpm_cap));
867 data[TPM_CAP_IDX] = TPM_CAP_PROP;
868 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Marcin Obaraec288bd2008-07-25 19:44:59 -0700870 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700871 "attempting to determine the manufacturer");
Marcin Obaraec288bd2008-07-25 19:44:59 -0700872 if (rc) {
873 kfree(data);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700874 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700878 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 memcpy(data, cap_version, sizeof(cap_version));
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700881 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700882 rc = transmit_cmd(chip, data, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700883 "attempting to determine the 1.1 version");
884 if (rc)
885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700887 str += sprintf(str,
888 "TCG version: %d.%d\nFirmware version: %d.%d\n",
889 (int) data[14], (int) data[15], (int) data[16],
890 (int) data[17]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700892out:
Marcin Obaraec288bd2008-07-25 19:44:59 -0700893 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return str - buf;
895}
Kylene Hall6659ca22005-06-23 22:02:00 -0700896EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700898ssize_t tpm_show_caps_1_2(struct device * dev,
899 struct device_attribute * attr, char *buf)
900{
Marcin Obaraec288bd2008-07-25 19:44:59 -0700901 u8 *data;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700902 ssize_t len;
903 char *str = buf;
904
905 struct tpm_chip *chip = dev_get_drvdata(dev);
906 if (chip == NULL)
907 return -ENODEV;
908
Marcin Obaraec288bd2008-07-25 19:44:59 -0700909 data = kzalloc(TPM_INTERNAL_RESULT_SIZE, GFP_KERNEL);
910 if (!data)
911 return -ENOMEM;
912
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700913 memcpy(data, tpm_cap, sizeof(tpm_cap));
914 data[TPM_CAP_IDX] = TPM_CAP_PROP;
915 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
916
Marcin Obaraec288bd2008-07-25 19:44:59 -0700917 len = tpm_transmit(chip, data, TPM_INTERNAL_RESULT_SIZE);
918 if (len <= TPM_ERROR_SIZE) {
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700919 dev_dbg(chip->dev, "A TPM error (%d) occurred "
920 "attempting to determine the manufacturer\n",
921 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
Marcin Obaraec288bd2008-07-25 19:44:59 -0700922 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700923 return 0;
924 }
925
926 str += sprintf(str, "Manufacturer: 0x%x\n",
927 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
928
929 memcpy(data, cap_version, sizeof(cap_version));
930 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
931
Marcin Obaraec288bd2008-07-25 19:44:59 -0700932 len = tpm_transmit(chip, data, TPM_INTERNAL_RESULT_SIZE);
933 if (len <= TPM_ERROR_SIZE) {
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700934 dev_err(chip->dev, "A TPM error (%d) occurred "
935 "attempting to determine the 1.2 version\n",
936 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
937 goto out;
938 }
939 str += sprintf(str,
940 "TCG version: %d.%d\nFirmware version: %d.%d\n",
941 (int) data[16], (int) data[17], (int) data[18],
942 (int) data[19]);
943
944out:
Marcin Obaraec288bd2008-07-25 19:44:59 -0700945 kfree(data);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700946 return str - buf;
947}
948EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
949
Kylene Hall6659ca22005-06-23 22:02:00 -0700950ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
951 const char *buf, size_t count)
952{
953 struct tpm_chip *chip = dev_get_drvdata(dev);
954 if (chip == NULL)
955 return 0;
956
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700957 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -0700958 return count;
959}
960EXPORT_SYMBOL_GPL(tpm_store_cancel);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Device file system interface to the TPM
Rajiv Andradef89c5ed2008-10-11 09:03:39 +1100964 *
965 * It's assured that the chip will be opened just once,
966 * by the check of is_open variable, which is protected
967 * by driver_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
969int tpm_open(struct inode *inode, struct file *file)
970{
Rajiv Andradef02a9362008-10-11 09:04:23 +1100971 int minor = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct tpm_chip *chip = NULL, *pos;
973
Rajiv Andradef02a9362008-10-11 09:04:23 +1100974 rcu_read_lock();
975 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700976 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 chip = pos;
Rajiv Andradef02a9362008-10-11 09:04:23 +1100978 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 }
981 }
Rajiv Andradef02a9362008-10-11 09:04:23 +1100982 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Rajiv Andradef02a9362008-10-11 09:04:23 +1100984 if (!chip)
985 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Rajiv Andradedc36d322008-10-11 09:04:02 +1100987 if (test_and_set_bit(0, &chip->is_open)) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800988 dev_dbg(chip->dev, "Another process owns this TPM\n");
Rajiv Andradef02a9362008-10-11 09:04:23 +1100989 put_device(chip->dev);
990 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
994 if (chip->data_buffer == NULL) {
Rajiv Andradedc36d322008-10-11 09:04:02 +1100995 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800996 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -ENOMEM;
998 }
999
1000 atomic_set(&chip->data_pending, 0);
1001
1002 file->private_data = chip;
1003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005EXPORT_SYMBOL_GPL(tpm_open);
1006
Rajiv Andradef02a9362008-10-11 09:04:23 +11001007/*
1008 * Called on file close
1009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010int tpm_release(struct inode *inode, struct file *file)
1011{
1012 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Rajiv Andrade4bdec112008-10-11 09:05:20 +11001014 del_singleshot_timer_sync(&chip->user_read_timer);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001015 flush_scheduled_work();
Kylene Hall5e976d52005-06-23 22:02:03 -07001016 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 atomic_set(&chip->data_pending, 0);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001018 kfree(chip->data_buffer);
Rajiv Andradedc36d322008-10-11 09:04:02 +11001019 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001020 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023EXPORT_SYMBOL_GPL(tpm_release);
1024
Andrew Mortonb888c872005-10-30 15:03:28 -08001025ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001026 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001029 size_t in_size = size, out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* cannot perform a write until the read has cleared
1032 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -07001033 while (atomic_read(&chip->data_pending) != 0)
1034 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001036 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (in_size > TPM_BUFSIZE)
1039 in_size = TPM_BUFSIZE;
1040
1041 if (copy_from_user
1042 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001043 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -EFAULT;
1045 }
1046
1047 /* atomic tpm command send and result receive */
1048 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1049
1050 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001051 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd482005-06-23 22:01:56 -07001054 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 return in_size;
1057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058EXPORT_SYMBOL_GPL(tpm_write);
1059
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001060ssize_t tpm_read(struct file *file, char __user *buf,
1061 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001064 ssize_t ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Kylene Hall5b44bd52005-06-23 22:01:53 -07001066 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -08001067 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -07001068 ret_size = atomic_read(&chip->data_pending);
1069 atomic_set(&chip->data_pending, 0);
1070 if (ret_size > 0) { /* relay data */
1071 if (size < ret_size)
1072 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001074 mutex_lock(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001075 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001076 ret_size = -EFAULT;
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001077 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
1080 return ret_size;
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082EXPORT_SYMBOL_GPL(tpm_read);
1083
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001084void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001086 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001089 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return;
1091 }
1092
1093 spin_lock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001094 list_del_rcu(&chip->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 spin_unlock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001096 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001098 misc_deregister(&chip->vendor.miscdev);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001099 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001100 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001102 /* write it this way to be explicit (chip->dev == dev) */
1103 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001105EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
1108 * We are about to suspend. Save the TPM state
1109 * so that it can be restored.
1110 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001111int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001113 struct tpm_chip *chip = dev_get_drvdata(dev);
David Smith2490c682008-01-14 00:55:12 -08001114 u8 savestate[] = {
1115 0, 193, /* TPM_TAG_RQU_COMMAND */
1116 0, 0, 0, 10, /* blob length (in bytes) */
1117 0, 0, 0, 152 /* TPM_ORD_SaveState */
1118 };
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (chip == NULL)
1121 return -ENODEV;
1122
1123 tpm_transmit(chip, savestate, sizeof(savestate));
1124 return 0;
1125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1127
1128/*
1129 * Resume from a power safe. The BIOS already restored
1130 * the TPM state.
1131 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001132int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001134 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (chip == NULL)
1137 return -ENODEV;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141EXPORT_SYMBOL_GPL(tpm_pm_resume);
1142
Rajiv Andrade253115b2008-10-11 09:04:39 +11001143/* In case vendor provided release function, call it too.*/
1144
1145void tpm_dev_vendor_release(struct tpm_chip *chip)
1146{
1147 if (chip->vendor.release)
1148 chip->vendor.release(chip->dev);
1149
1150 clear_bit(chip->dev_num, dev_mask);
1151 kfree(chip->vendor.miscdev.name);
1152}
1153EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1154
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001157 * Once all references to platform device are down to 0,
1158 * release all allocated structures.
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001159 */
Stephen Rothwellcbb2ed42008-10-18 20:25:46 -07001160void tpm_dev_release(struct device *dev)
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001161{
1162 struct tpm_chip *chip = dev_get_drvdata(dev);
1163
Rajiv Andrade253115b2008-10-11 09:04:39 +11001164 tpm_dev_vendor_release(chip);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001165
Rajiv Andrade253115b2008-10-11 09:04:39 +11001166 chip->release(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001167 kfree(chip);
1168}
Rajiv Andrade253115b2008-10-11 09:04:39 +11001169EXPORT_SYMBOL_GPL(tpm_dev_release);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001170
1171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * Called from tpm_<specific>.c probe function only for devices
1173 * the driver has determined it should claim. Prior to calling
1174 * this function the specific probe function has called pci_enable_device
1175 * upon errant exit from this function specific probe function should call
1176 * pci_disable_device
1177 */
Rajiv Andradef02a9362008-10-11 09:04:23 +11001178struct tpm_chip *tpm_register_hardware(struct device *dev,
1179 const struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001181#define DEVNAME_SIZE 7
1182
1183 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001187 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001188 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1189
Andrew Mortondd78c942008-10-15 22:04:37 -07001190 if (chip == NULL || devname == NULL)
1191 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001193 mutex_init(&chip->buffer_mutex);
1194 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 INIT_LIST_HEAD(&chip->list);
1196
David Howellsc4028952006-11-22 14:57:56 +00001197 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001198
Jiri Slaby40565f12007-02-12 00:52:31 -08001199 setup_timer(&chip->user_read_timer, user_reader_timeout,
1200 (unsigned long)chip);
Kylene Hallfe3fd482005-06-23 22:01:56 -07001201
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001202 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001204 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001206 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001207 dev_err(dev, "No available tpm device numbers\n");
Andrew Mortondd78c942008-10-15 22:04:37 -07001208 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001210 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001212 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001214 set_bit(chip->dev_num, dev_mask);
1215
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001216 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001217 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001219 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001220 chip->dev = get_device(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001221 chip->release = dev->release;
1222 dev->release = tpm_dev_release;
1223 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001225 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001226 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001228 chip->vendor.miscdev.name,
1229 chip->vendor.miscdev.minor);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001230 put_device(chip->dev);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001234 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
Neil Horman5d469ec2006-12-06 20:37:08 -08001235 misc_deregister(&chip->vendor.miscdev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001236 put_device(chip->dev);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001237
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001238 return NULL;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001241 chip->bios_dir = tpm_bios_log_setup(devname);
1242
Rajiv Andradef02a9362008-10-11 09:04:23 +11001243 /* Make chip available */
1244 spin_lock(&driver_lock);
1245 list_add_rcu(&chip->list, &tpm_chip_list);
1246 spin_unlock(&driver_lock);
1247
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001248 return chip;
Andrew Mortondd78c942008-10-15 22:04:37 -07001249
1250out_free:
1251 kfree(chip);
1252 kfree(devname);
1253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255EXPORT_SYMBOL_GPL(tpm_register_hardware);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1258MODULE_DESCRIPTION("TPM Driver");
1259MODULE_VERSION("2.0");
1260MODULE_LICENSE("GPL");