blob: c88424a0c89b60d28d3805895e6ee973710b7d8c [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>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "tpm.h"
31
Kylene Hall3122a882005-06-23 22:01:48 -070032enum tpm_const {
33 TPM_MINOR = 224, /* officially assigned */
34 TPM_BUFSIZE = 2048,
35 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070036};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070038enum 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 Torvalds1da177e2005-04-16 15:20:36 -070049static LIST_HEAD(tpm_chip_list);
50static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070051static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070053/*
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 */
61static 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
76static 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 Torvalds1da177e2005-04-16 15:20:36 -0700322static void user_reader_timeout(unsigned long ptr)
323{
324 struct tpm_chip *chip = (struct tpm_chip *) ptr;
325
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800326 schedule_work(&chip->work);
327}
328
David Howellsc4028952006-11-22 14:57:56 +0000329static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800330{
David Howellsc4028952006-11-22 14:57:56 +0000331 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800332
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700333 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 atomic_set(&chip->data_pending, 0);
335 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700336 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700340 * Returns max number of jiffies to wait
341 */
342unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
343 u32 ordinal)
344{
345 int duration_idx = TPM_UNDEFINED;
346 int duration = 0;
347
348 if (ordinal < TPM_MAX_ORDINAL)
349 duration_idx = tpm_ordinal_duration[ordinal];
350 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
351 TPM_MAX_PROTECTED_ORDINAL)
352 duration_idx =
353 tpm_protected_ordinal_duration[ordinal &
354 TPM_PROTECTED_ORDINAL_MASK];
355
356 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700357 duration = chip->vendor.duration[duration_idx];
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700358 if (duration <= 0)
359 return 2 * 60 * HZ;
360 else
361 return duration;
362}
363EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
364
365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Internal kernel interface to transmit TPM commands
367 */
368static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
369 size_t bufsiz)
370{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700371 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700372 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700373 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Kylene Hall81179bb2005-06-23 22:01:59 -0700375 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700376 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (count == 0)
378 return -ENODATA;
379 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800380 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700381 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -E2BIG;
383 }
384
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700385 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700387 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800388 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700389 "tpm_transmit: tpm_send: error %zd\n", rc);
390 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700393 if (chip->vendor.irq)
394 goto out_recv;
395
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700396 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700398 u8 status = chip->vendor.status(chip);
399 if ((status & chip->vendor.req_complete_mask) ==
400 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700402
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700403 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800404 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700405 rc = -ECANCELED;
406 goto out;
407 }
408
409 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700411 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700413 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800414 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700415 rc = -ETIME;
416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700419 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700420 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800421 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700422 "tpm_transmit: tpm_recv: error %zd\n", rc);
423out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700424 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700425 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700429#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 Hall08e96e42006-04-22 02:37:50 -0700436#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 Hallbeed53a2006-04-22 02:37:05 -0700440
441#define TPM_CAP_IDX 13
442#define TPM_CAP_SUBCAP_IDX 21
443
444enum tpm_capabilities {
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700445 TPM_CAP_FLAG = 4,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700446 TPM_CAP_PROP = 5,
447};
448
449enum tpm_sub_capabilities {
450 TPM_CAP_PROP_PCR = 0x1,
451 TPM_CAP_PROP_MANUFACTURER = 0x3,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700452 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 Hallbeed53a2006-04-22 02:37:05 -0700457};
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
465static const u8 tpm_cap[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 0, 193, /* TPM_TAG_RQU_COMMAND */
467 0, 0, 0, 22, /* length */
468 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700469 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 Torvalds1da177e2005-04-16 15:20:36 -0700472};
473
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700474static 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 Hall08e96e42006-04-22 02:37:50 -0700490void 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}
502EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
503
504void 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)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700527 chip->vendor.timeout_a = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700528 timeout =
529 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
530 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700531 chip->vendor.timeout_b = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700532 timeout =
533 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
534 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700535 chip->vendor.timeout_c = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700536 timeout =
537 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
538 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700539 chip->vendor.timeout_d = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700540
541duration:
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] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700556 msecs_to_jiffies(be32_to_cpu
557 (*((__be32 *) (data +
558 TPM_GET_CAP_RET_UINT32_1_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700559 chip->vendor.duration[TPM_MEDIUM] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700560 msecs_to_jiffies(be32_to_cpu
561 (*((__be32 *) (data +
562 TPM_GET_CAP_RET_UINT32_2_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700563 chip->vendor.duration[TPM_LONG] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700564 msecs_to_jiffies(be32_to_cpu
565 (*((__be32 *) (data +
566 TPM_GET_CAP_RET_UINT32_3_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700567}
568EXPORT_SYMBOL_GPL(tpm_get_timeouts);
569
570void tpm_continue_selftest(struct tpm_chip *chip)
571{
572 u8 data[] = {
573 0, 193, /* TPM_TAG_RQU_COMMAND */
574 0, 0, 0, 10, /* length */
575 0, 0, 0, 83, /* TPM_ORD_GetCapability */
576 };
577
578 tpm_transmit(chip, data, sizeof(data));
579}
580EXPORT_SYMBOL_GPL(tpm_continue_selftest);
581
582ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
583 char *buf)
584{
585 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
586 ssize_t rc;
587
588 struct tpm_chip *chip = dev_get_drvdata(dev);
589 if (chip == NULL)
590 return -ENODEV;
591
592 memcpy(data, tpm_cap, sizeof(tpm_cap));
593 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
594 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
595
596 rc = transmit_cmd(chip, data, sizeof(data),
597 "attemtping to determine the permanent state");
598 if (rc)
599 return 0;
600 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
601}
602EXPORT_SYMBOL_GPL(tpm_show_enabled);
603
604ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
605 char *buf)
606{
607 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
608 ssize_t rc;
609
610 struct tpm_chip *chip = dev_get_drvdata(dev);
611 if (chip == NULL)
612 return -ENODEV;
613
614 memcpy(data, tpm_cap, sizeof(tpm_cap));
615 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
616 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
617
618 rc = transmit_cmd(chip, data, sizeof(data),
619 "attemtping to determine the permanent state");
620 if (rc)
621 return 0;
622 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
623}
624EXPORT_SYMBOL_GPL(tpm_show_active);
625
626ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
627 char *buf)
628{
629 u8 data[sizeof(tpm_cap)];
630 ssize_t rc;
631
632 struct tpm_chip *chip = dev_get_drvdata(dev);
633 if (chip == NULL)
634 return -ENODEV;
635
636 memcpy(data, tpm_cap, sizeof(tpm_cap));
637 data[TPM_CAP_IDX] = TPM_CAP_PROP;
638 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
639
640 rc = transmit_cmd(chip, data, sizeof(data),
641 "attempting to determine the owner state");
642 if (rc)
643 return 0;
644 return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
645}
646EXPORT_SYMBOL_GPL(tpm_show_owned);
647
648ssize_t tpm_show_temp_deactivated(struct device * dev,
649 struct device_attribute * attr, char *buf)
650{
651 u8 data[sizeof(tpm_cap)];
652 ssize_t rc;
653
654 struct tpm_chip *chip = dev_get_drvdata(dev);
655 if (chip == NULL)
656 return -ENODEV;
657
658 memcpy(data, tpm_cap, sizeof(tpm_cap));
659 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
660 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
661
662 rc = transmit_cmd(chip, data, sizeof(data),
663 "attempting to determine the temporary state");
664 if (rc)
665 return 0;
666 return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
667}
668EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
669
Kylene Halldff37e42005-06-23 22:01:50 -0700670static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 0, 193, /* TPM_TAG_RQU_COMMAND */
672 0, 0, 0, 14, /* length */
673 0, 0, 0, 21, /* TPM_ORD_PcrRead */
674 0, 0, 0, 0 /* PCR index */
675};
676
Kylene Hall6659ca22005-06-23 22:02:00 -0700677ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
678 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700680 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700681 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700682 int i, j, num_pcrs;
683 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 char *str = buf;
685
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800686 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (chip == NULL)
688 return -ENODEV;
689
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700690 memcpy(data, tpm_cap, sizeof(tpm_cap));
691 data[TPM_CAP_IDX] = TPM_CAP_PROP;
692 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
693
694 rc = transmit_cmd(chip, data, sizeof(data),
695 "attempting to determine the number of PCRS");
696 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Kylene Hall81179bb2005-06-23 22:01:59 -0700699 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 for (i = 0; i < num_pcrs; i++) {
701 memcpy(data, pcrread, sizeof(pcrread));
702 index = cpu_to_be32(i);
703 memcpy(data + 10, &index, 4);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700704 rc = transmit_cmd(chip, data, sizeof(data),
705 "attempting to read a PCR");
706 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 str += sprintf(str, "PCR-%02d: ", i);
709 for (j = 0; j < TPM_DIGEST_SIZE; j++)
710 str += sprintf(str, "%02X ", *(data + 10 + j));
711 str += sprintf(str, "\n");
712 }
Kylene Halle234bc92005-06-23 22:02:08 -0700713out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return str - buf;
715}
Kylene Hall6659ca22005-06-23 22:02:00 -0700716EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700719static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 0, 193, /* TPM_TAG_RQU_COMMAND */
721 0, 0, 0, 30, /* length */
722 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
723};
724
Kylene Hall6659ca22005-06-23 22:02:00 -0700725ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
726 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Kylene Hall2df71112005-06-23 22:01:54 -0700728 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700729 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700730 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 char *str = buf;
732
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800733 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (chip == NULL)
735 return -ENODEV;
736
Andrew Mortonb888c872005-10-30 15:03:28 -0800737 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700738 if (!data)
739 return -ENOMEM;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700743 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
744 "attempting to read the PUBEK");
745 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
749 ignore header 10 bytes
750 algorithm 32 bits (1 == RSA )
751 encscheme 16 bits
752 sigscheme 16 bits
753 parameters (RSA 12->bytes: keybit, #primes, expbit)
754 keylenbytes 32 bits
755 256 byte modulus
756 ignore checksum 20 bytes
757 */
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 str +=
760 sprintf(str,
761 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
762 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
763 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
764 "Modulus length: %d\nModulus: \n",
765 data[10], data[11], data[12], data[13], data[14],
766 data[15], data[16], data[17], data[22], data[23],
767 data[24], data[25], data[26], data[27], data[28],
768 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700769 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700772 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if ((i + 1) % 16 == 0)
774 str += sprintf(str, "\n");
775 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700776out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700777 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700778 kfree(data);
779 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Kylene Hall6659ca22005-06-23 22:02:00 -0700781EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700783#define CAP_VERSION_1_1 6
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700784#define CAP_VERSION_1_2 0x1A
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700785#define CAP_VERSION_IDX 13
Kylene Halldff37e42005-06-23 22:01:50 -0700786static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 0, 193, /* TPM_TAG_RQU_COMMAND */
788 0, 0, 0, 18, /* length */
789 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700790 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 0, 0, 0, 0
792};
793
Kylene Hall6659ca22005-06-23 22:02:00 -0700794ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
795 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700797 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700798 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 char *str = buf;
800
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800801 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (chip == NULL)
803 return -ENODEV;
804
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700805 memcpy(data, tpm_cap, sizeof(tpm_cap));
806 data[TPM_CAP_IDX] = TPM_CAP_PROP;
807 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700809 rc = transmit_cmd(chip, data, sizeof(data),
810 "attempting to determine the manufacturer");
811 if (rc)
812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700815 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 memcpy(data, cap_version, sizeof(cap_version));
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700818 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
819 rc = transmit_cmd(chip, data, sizeof(data),
820 "attempting to determine the 1.1 version");
821 if (rc)
822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700824 str += sprintf(str,
825 "TCG version: %d.%d\nFirmware version: %d.%d\n",
826 (int) data[14], (int) data[15], (int) data[16],
827 (int) data[17]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700829out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return str - buf;
831}
Kylene Hall6659ca22005-06-23 22:02:00 -0700832EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700834ssize_t tpm_show_caps_1_2(struct device * dev,
835 struct device_attribute * attr, char *buf)
836{
837 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
838 ssize_t len;
839 char *str = buf;
840
841 struct tpm_chip *chip = dev_get_drvdata(dev);
842 if (chip == NULL)
843 return -ENODEV;
844
845 memcpy(data, tpm_cap, sizeof(tpm_cap));
846 data[TPM_CAP_IDX] = TPM_CAP_PROP;
847 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
848
849 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
850 TPM_ERROR_SIZE) {
851 dev_dbg(chip->dev, "A TPM error (%d) occurred "
852 "attempting to determine the manufacturer\n",
853 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
854 return 0;
855 }
856
857 str += sprintf(str, "Manufacturer: 0x%x\n",
858 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
859
860 memcpy(data, cap_version, sizeof(cap_version));
861 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
862
863 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
864 TPM_ERROR_SIZE) {
865 dev_err(chip->dev, "A TPM error (%d) occurred "
866 "attempting to determine the 1.2 version\n",
867 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
868 goto out;
869 }
870 str += sprintf(str,
871 "TCG version: %d.%d\nFirmware version: %d.%d\n",
872 (int) data[16], (int) data[17], (int) data[18],
873 (int) data[19]);
874
875out:
876 return str - buf;
877}
878EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
879
Kylene Hall6659ca22005-06-23 22:02:00 -0700880ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
881 const char *buf, size_t count)
882{
883 struct tpm_chip *chip = dev_get_drvdata(dev);
884 if (chip == NULL)
885 return 0;
886
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700887 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -0700888 return count;
889}
890EXPORT_SYMBOL_GPL(tpm_store_cancel);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Device file system interface to the TPM
894 */
895int tpm_open(struct inode *inode, struct file *file)
896{
897 int rc = 0, minor = iminor(inode);
898 struct tpm_chip *chip = NULL, *pos;
899
900 spin_lock(&driver_lock);
901
902 list_for_each_entry(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700903 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 chip = pos;
905 break;
906 }
907 }
908
909 if (chip == NULL) {
910 rc = -ENODEV;
911 goto err_out;
912 }
913
914 if (chip->num_opens) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800915 dev_dbg(chip->dev, "Another process owns this TPM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 rc = -EBUSY;
917 goto err_out;
918 }
919
920 chip->num_opens++;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800921 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 spin_unlock(&driver_lock);
924
925 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
926 if (chip->data_buffer == NULL) {
927 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800928 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -ENOMEM;
930 }
931
932 atomic_set(&chip->data_pending, 0);
933
934 file->private_data = chip;
935 return 0;
936
937err_out:
938 spin_unlock(&driver_lock);
939 return rc;
940}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941EXPORT_SYMBOL_GPL(tpm_open);
942
943int tpm_release(struct inode *inode, struct file *file)
944{
945 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Parag Warudkar8e39c932007-05-08 00:31:09 -0700947 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 spin_lock(&driver_lock);
Kylene Hall5e976d52005-06-23 22:02:03 -0700949 file->private_data = NULL;
Kylene Hallfe3fd482005-06-23 22:01:56 -0700950 del_singleshot_timer_sync(&chip->user_read_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 atomic_set(&chip->data_pending, 0);
Parag Warudkar8e39c932007-05-08 00:31:09 -0700952 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800953 put_device(chip->dev);
Kylene Hall5e976d52005-06-23 22:02:03 -0700954 kfree(chip->data_buffer);
955 spin_unlock(&driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958EXPORT_SYMBOL_GPL(tpm_release);
959
Andrew Mortonb888c872005-10-30 15:03:28 -0800960ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700961 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 struct tpm_chip *chip = file->private_data;
964 int in_size = size, out_size;
965
966 /* cannot perform a write until the read has cleared
967 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700968 while (atomic_read(&chip->data_pending) != 0)
969 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700971 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (in_size > TPM_BUFSIZE)
974 in_size = TPM_BUFSIZE;
975
976 if (copy_from_user
977 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700978 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EFAULT;
980 }
981
982 /* atomic tpm command send and result receive */
983 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
984
985 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700986 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd482005-06-23 22:01:56 -0700989 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return in_size;
992}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993EXPORT_SYMBOL_GPL(tpm_write);
994
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700995ssize_t tpm_read(struct file *file, char __user *buf,
996 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct tpm_chip *chip = file->private_data;
Kylene Hall5b44bd52005-06-23 22:01:53 -0700999 int ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Kylene Hall5b44bd52005-06-23 22:01:53 -07001001 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -08001002 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -07001003 ret_size = atomic_read(&chip->data_pending);
1004 atomic_set(&chip->data_pending, 0);
1005 if (ret_size > 0) { /* relay data */
1006 if (size < ret_size)
1007 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001009 mutex_lock(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001010 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001011 ret_size = -EFAULT;
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001012 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 return ret_size;
1016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017EXPORT_SYMBOL_GPL(tpm_read);
1018
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001019void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001021 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001024 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return;
1026 }
1027
1028 spin_lock(&driver_lock);
1029
1030 list_del(&chip->list);
1031
1032 spin_unlock(&driver_lock);
1033
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001034 dev_set_drvdata(dev, NULL);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001035 misc_deregister(&chip->vendor.miscdev);
1036 kfree(chip->vendor.miscdev.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001038 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001039 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001041 clear_bit(chip->dev_num, dev_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 kfree(chip);
1044
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001045 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001047EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
1050 * We are about to suspend. Save the TPM state
1051 * so that it can be restored.
1052 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001053int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001055 struct tpm_chip *chip = dev_get_drvdata(dev);
David Smith2490c682008-01-14 00:55:12 -08001056 u8 savestate[] = {
1057 0, 193, /* TPM_TAG_RQU_COMMAND */
1058 0, 0, 0, 10, /* blob length (in bytes) */
1059 0, 0, 0, 152 /* TPM_ORD_SaveState */
1060 };
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (chip == NULL)
1063 return -ENODEV;
1064
1065 tpm_transmit(chip, savestate, sizeof(savestate));
1066 return 0;
1067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1069
1070/*
1071 * Resume from a power safe. The BIOS already restored
1072 * the TPM state.
1073 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001074int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001076 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (chip == NULL)
1079 return -ENODEV;
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083EXPORT_SYMBOL_GPL(tpm_pm_resume);
1084
1085/*
1086 * Called from tpm_<specific>.c probe function only for devices
1087 * the driver has determined it should claim. Prior to calling
1088 * this function the specific probe function has called pci_enable_device
1089 * upon errant exit from this function specific probe function should call
1090 * pci_disable_device
1091 */
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001092struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
1093 *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001095#define DEVNAME_SIZE 7
1096
1097 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001101 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001102 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1103
1104 if (chip == NULL || devname == NULL) {
1105 kfree(chip);
1106 kfree(devname);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001107 return NULL;
Parag Warudkar8e39c932007-05-08 00:31:09 -07001108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001110 mutex_init(&chip->buffer_mutex);
1111 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 INIT_LIST_HEAD(&chip->list);
1113
David Howellsc4028952006-11-22 14:57:56 +00001114 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001115
Jiri Slaby40565f12007-02-12 00:52:31 -08001116 setup_timer(&chip->user_read_timer, user_reader_timeout,
1117 (unsigned long)chip);
Kylene Hallfe3fd482005-06-23 22:01:56 -07001118
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001119 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001121 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001123 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001124 dev_err(dev, "No available tpm device numbers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 kfree(chip);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001126 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001128 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001130 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001132 set_bit(chip->dev_num, dev_mask);
1133
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001134 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001135 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001137 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001138 chip->dev = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001140 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001141 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001143 chip->vendor.miscdev.name,
1144 chip->vendor.miscdev.minor);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001145 put_device(dev);
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001146 clear_bit(chip->dev_num, dev_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 kfree(chip);
Randy Dunlap2e826362006-07-14 00:24:28 -07001148 kfree(devname);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001149 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Kylene Hall5e976d52005-06-23 22:02:03 -07001152 spin_lock(&driver_lock);
1153
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001154 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 list_add(&chip->list, &tpm_chip_list);
1157
Kylene Hall5e976d52005-06-23 22:02:03 -07001158 spin_unlock(&driver_lock);
1159
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001160 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1161 list_del(&chip->list);
Neil Horman5d469ec2006-12-06 20:37:08 -08001162 misc_deregister(&chip->vendor.miscdev);
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001163 put_device(dev);
1164 clear_bit(chip->dev_num, dev_mask);
1165 kfree(chip);
1166 kfree(devname);
1167 return NULL;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001170 chip->bios_dir = tpm_bios_log_setup(devname);
1171
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001172 return chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174EXPORT_SYMBOL_GPL(tpm_register_hardware);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1177MODULE_DESCRIPTION("TPM Driver");
1178MODULE_VERSION("2.0");
1179MODULE_LICENSE("GPL");