blob: 33e1f66e39cb62550a762724b8dcbfd4d7d5326b [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 *
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
11 *
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
18 * License.
19 *
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070022 * calls to msleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 */
25
26#include <linux/sched.h>
27#include <linux/poll.h>
28#include <linux/spinlock.h>
29#include "tpm.h"
30
Kylene Hall3122a882005-06-23 22:01:48 -070031enum tpm_const {
32 TPM_MINOR = 224, /* officially assigned */
33 TPM_BUFSIZE = 2048,
34 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070035};
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070037enum tpm_duration {
38 TPM_SHORT = 0,
39 TPM_MEDIUM = 1,
40 TPM_LONG = 2,
41 TPM_UNDEFINED,
42};
43
44#define TPM_MAX_ORDINAL 243
45#define TPM_MAX_PROTECTED_ORDINAL 12
46#define TPM_PROTECTED_ORDINAL_MASK 0xFF
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static LIST_HEAD(tpm_chip_list);
49static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070050static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070052/*
53 * Array with one entry per ordinal defining the maximum amount
54 * of time the chip could take to return the result. The ordinal
55 * designation of short, medium or long is defined in a table in
56 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
57 * values of the SHORT, MEDIUM, and LONG durations are retrieved
58 * from the chip during initialization with a call to tpm_get_timeouts.
59 */
60static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
61 TPM_UNDEFINED, /* 0 */
62 TPM_UNDEFINED,
63 TPM_UNDEFINED,
64 TPM_UNDEFINED,
65 TPM_UNDEFINED,
66 TPM_UNDEFINED, /* 5 */
67 TPM_UNDEFINED,
68 TPM_UNDEFINED,
69 TPM_UNDEFINED,
70 TPM_UNDEFINED,
71 TPM_SHORT, /* 10 */
72 TPM_SHORT,
73};
74
75static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
76 TPM_UNDEFINED, /* 0 */
77 TPM_UNDEFINED,
78 TPM_UNDEFINED,
79 TPM_UNDEFINED,
80 TPM_UNDEFINED,
81 TPM_UNDEFINED, /* 5 */
82 TPM_UNDEFINED,
83 TPM_UNDEFINED,
84 TPM_UNDEFINED,
85 TPM_UNDEFINED,
86 TPM_SHORT, /* 10 */
87 TPM_SHORT,
88 TPM_MEDIUM,
89 TPM_LONG,
90 TPM_LONG,
91 TPM_MEDIUM, /* 15 */
92 TPM_SHORT,
93 TPM_SHORT,
94 TPM_MEDIUM,
95 TPM_LONG,
96 TPM_SHORT, /* 20 */
97 TPM_SHORT,
98 TPM_MEDIUM,
99 TPM_MEDIUM,
100 TPM_MEDIUM,
101 TPM_SHORT, /* 25 */
102 TPM_SHORT,
103 TPM_MEDIUM,
104 TPM_SHORT,
105 TPM_SHORT,
106 TPM_MEDIUM, /* 30 */
107 TPM_LONG,
108 TPM_MEDIUM,
109 TPM_SHORT,
110 TPM_SHORT,
111 TPM_SHORT, /* 35 */
112 TPM_MEDIUM,
113 TPM_MEDIUM,
114 TPM_UNDEFINED,
115 TPM_UNDEFINED,
116 TPM_MEDIUM, /* 40 */
117 TPM_LONG,
118 TPM_MEDIUM,
119 TPM_SHORT,
120 TPM_SHORT,
121 TPM_SHORT, /* 45 */
122 TPM_SHORT,
123 TPM_SHORT,
124 TPM_SHORT,
125 TPM_LONG,
126 TPM_MEDIUM, /* 50 */
127 TPM_MEDIUM,
128 TPM_UNDEFINED,
129 TPM_UNDEFINED,
130 TPM_UNDEFINED,
131 TPM_UNDEFINED, /* 55 */
132 TPM_UNDEFINED,
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_MEDIUM, /* 60 */
137 TPM_MEDIUM,
138 TPM_MEDIUM,
139 TPM_SHORT,
140 TPM_SHORT,
141 TPM_MEDIUM, /* 65 */
142 TPM_UNDEFINED,
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_SHORT, /* 70 */
147 TPM_SHORT,
148 TPM_UNDEFINED,
149 TPM_UNDEFINED,
150 TPM_UNDEFINED,
151 TPM_UNDEFINED, /* 75 */
152 TPM_UNDEFINED,
153 TPM_UNDEFINED,
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_LONG, /* 80 */
157 TPM_UNDEFINED,
158 TPM_MEDIUM,
159 TPM_LONG,
160 TPM_SHORT,
161 TPM_UNDEFINED, /* 85 */
162 TPM_UNDEFINED,
163 TPM_UNDEFINED,
164 TPM_UNDEFINED,
165 TPM_UNDEFINED,
166 TPM_SHORT, /* 90 */
167 TPM_SHORT,
168 TPM_SHORT,
169 TPM_SHORT,
170 TPM_SHORT,
171 TPM_UNDEFINED, /* 95 */
172 TPM_UNDEFINED,
173 TPM_UNDEFINED,
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_MEDIUM, /* 100 */
177 TPM_SHORT,
178 TPM_SHORT,
179 TPM_UNDEFINED,
180 TPM_UNDEFINED,
181 TPM_UNDEFINED, /* 105 */
182 TPM_UNDEFINED,
183 TPM_UNDEFINED,
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_SHORT, /* 110 */
187 TPM_SHORT,
188 TPM_SHORT,
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_SHORT, /* 115 */
192 TPM_SHORT,
193 TPM_SHORT,
194 TPM_UNDEFINED,
195 TPM_UNDEFINED,
196 TPM_LONG, /* 120 */
197 TPM_LONG,
198 TPM_MEDIUM,
199 TPM_UNDEFINED,
200 TPM_SHORT,
201 TPM_SHORT, /* 125 */
202 TPM_SHORT,
203 TPM_LONG,
204 TPM_SHORT,
205 TPM_SHORT,
206 TPM_SHORT, /* 130 */
207 TPM_MEDIUM,
208 TPM_UNDEFINED,
209 TPM_SHORT,
210 TPM_MEDIUM,
211 TPM_UNDEFINED, /* 135 */
212 TPM_UNDEFINED,
213 TPM_UNDEFINED,
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_SHORT, /* 140 */
217 TPM_SHORT,
218 TPM_UNDEFINED,
219 TPM_UNDEFINED,
220 TPM_UNDEFINED,
221 TPM_UNDEFINED, /* 145 */
222 TPM_UNDEFINED,
223 TPM_UNDEFINED,
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_SHORT, /* 150 */
227 TPM_MEDIUM,
228 TPM_MEDIUM,
229 TPM_SHORT,
230 TPM_SHORT,
231 TPM_UNDEFINED, /* 155 */
232 TPM_UNDEFINED,
233 TPM_UNDEFINED,
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_SHORT, /* 160 */
237 TPM_SHORT,
238 TPM_SHORT,
239 TPM_SHORT,
240 TPM_UNDEFINED,
241 TPM_UNDEFINED, /* 165 */
242 TPM_UNDEFINED,
243 TPM_UNDEFINED,
244 TPM_UNDEFINED,
245 TPM_UNDEFINED,
246 TPM_LONG, /* 170 */
247 TPM_UNDEFINED,
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_UNDEFINED,
251 TPM_UNDEFINED, /* 175 */
252 TPM_UNDEFINED,
253 TPM_UNDEFINED,
254 TPM_UNDEFINED,
255 TPM_UNDEFINED,
256 TPM_MEDIUM, /* 180 */
257 TPM_SHORT,
258 TPM_MEDIUM,
259 TPM_MEDIUM,
260 TPM_MEDIUM,
261 TPM_MEDIUM, /* 185 */
262 TPM_SHORT,
263 TPM_UNDEFINED,
264 TPM_UNDEFINED,
265 TPM_UNDEFINED,
266 TPM_UNDEFINED, /* 190 */
267 TPM_UNDEFINED,
268 TPM_UNDEFINED,
269 TPM_UNDEFINED,
270 TPM_UNDEFINED,
271 TPM_UNDEFINED, /* 195 */
272 TPM_UNDEFINED,
273 TPM_UNDEFINED,
274 TPM_UNDEFINED,
275 TPM_UNDEFINED,
276 TPM_SHORT, /* 200 */
277 TPM_UNDEFINED,
278 TPM_UNDEFINED,
279 TPM_UNDEFINED,
280 TPM_SHORT,
281 TPM_SHORT, /* 205 */
282 TPM_SHORT,
283 TPM_SHORT,
284 TPM_SHORT,
285 TPM_SHORT,
286 TPM_MEDIUM, /* 210 */
287 TPM_UNDEFINED,
288 TPM_MEDIUM,
289 TPM_MEDIUM,
290 TPM_MEDIUM,
291 TPM_UNDEFINED, /* 215 */
292 TPM_MEDIUM,
293 TPM_UNDEFINED,
294 TPM_UNDEFINED,
295 TPM_SHORT,
296 TPM_SHORT, /* 220 */
297 TPM_SHORT,
298 TPM_SHORT,
299 TPM_SHORT,
300 TPM_SHORT,
301 TPM_UNDEFINED, /* 225 */
302 TPM_UNDEFINED,
303 TPM_UNDEFINED,
304 TPM_UNDEFINED,
305 TPM_UNDEFINED,
306 TPM_SHORT, /* 230 */
307 TPM_LONG,
308 TPM_MEDIUM,
309 TPM_UNDEFINED,
310 TPM_UNDEFINED,
311 TPM_UNDEFINED, /* 235 */
312 TPM_UNDEFINED,
313 TPM_UNDEFINED,
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_SHORT, /* 240 */
317 TPM_UNDEFINED,
318 TPM_MEDIUM,
319};
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void user_reader_timeout(unsigned long ptr)
322{
323 struct tpm_chip *chip = (struct tpm_chip *) ptr;
324
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800325 schedule_work(&chip->work);
326}
327
David Howellsc4028952006-11-22 14:57:56 +0000328static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800329{
David Howellsc4028952006-11-22 14:57:56 +0000330 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 down(&chip->buffer_mutex);
333 atomic_set(&chip->data_pending, 0);
334 memset(chip->data_buffer, 0, TPM_BUFSIZE);
335 up(&chip->buffer_mutex);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700339 * Returns max number of jiffies to wait
340 */
341unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
342 u32 ordinal)
343{
344 int duration_idx = TPM_UNDEFINED;
345 int duration = 0;
346
347 if (ordinal < TPM_MAX_ORDINAL)
348 duration_idx = tpm_ordinal_duration[ordinal];
349 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
350 TPM_MAX_PROTECTED_ORDINAL)
351 duration_idx =
352 tpm_protected_ordinal_duration[ordinal &
353 TPM_PROTECTED_ORDINAL_MASK];
354
355 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700356 duration = chip->vendor.duration[duration_idx];
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700357 if (duration <= 0)
358 return 2 * 60 * HZ;
359 else
360 return duration;
361}
362EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
363
364/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * Internal kernel interface to transmit TPM commands
366 */
367static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
368 size_t bufsiz)
369{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700370 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700371 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700372 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Kylene Hall81179bb2005-06-23 22:01:59 -0700374 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700375 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (count == 0)
377 return -ENODATA;
378 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800379 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700380 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -E2BIG;
382 }
383
384 down(&chip->tpm_mutex);
385
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700386 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800387 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700388 "tpm_transmit: tpm_send: error %zd\n", rc);
389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700392 if (chip->vendor.irq)
393 goto out_recv;
394
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700395 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700397 u8 status = chip->vendor.status(chip);
398 if ((status & chip->vendor.req_complete_mask) ==
399 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700401
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700402 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800403 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700404 rc = -ECANCELED;
405 goto out;
406 }
407
408 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700410 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700412 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800413 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700414 rc = -ETIME;
415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700418 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700419 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800420 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700421 "tpm_transmit: tpm_recv: error %zd\n", rc);
422out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 up(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700424 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700428#define TPM_ERROR_SIZE 10
429#define TPM_RET_CODE_IDX 6
430#define TPM_GET_CAP_RET_SIZE_IDX 10
431#define TPM_GET_CAP_RET_UINT32_1_IDX 14
432#define TPM_GET_CAP_RET_UINT32_2_IDX 18
433#define TPM_GET_CAP_RET_UINT32_3_IDX 22
434#define TPM_GET_CAP_RET_UINT32_4_IDX 26
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700435#define TPM_GET_CAP_PERM_DISABLE_IDX 16
436#define TPM_GET_CAP_PERM_INACTIVE_IDX 18
437#define TPM_GET_CAP_RET_BOOL_1_IDX 14
438#define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700439
440#define TPM_CAP_IDX 13
441#define TPM_CAP_SUBCAP_IDX 21
442
443enum tpm_capabilities {
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700444 TPM_CAP_FLAG = 4,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700445 TPM_CAP_PROP = 5,
446};
447
448enum tpm_sub_capabilities {
449 TPM_CAP_PROP_PCR = 0x1,
450 TPM_CAP_PROP_MANUFACTURER = 0x3,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700451 TPM_CAP_FLAG_PERM = 0x8,
452 TPM_CAP_FLAG_VOL = 0x9,
453 TPM_CAP_PROP_OWNER = 0x11,
454 TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
455 TPM_CAP_PROP_TIS_DURATION = 0x20,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700456};
457
458/*
459 * This is a semi generic GetCapability command for use
460 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
461 * and their associated sub_capabilities.
462 */
463
464static const u8 tpm_cap[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 0, 193, /* TPM_TAG_RQU_COMMAND */
466 0, 0, 0, 22, /* length */
467 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700468 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
469 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
470 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700473static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
474 char *desc)
475{
476 int err;
477
478 len = tpm_transmit(chip, data, len);
479 if (len < 0)
480 return len;
481 if (len == TPM_ERROR_SIZE) {
482 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
483 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
484 return err;
485 }
486 return 0;
487}
488
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700489void tpm_gen_interrupt(struct tpm_chip *chip)
490{
491 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
492 ssize_t rc;
493
494 memcpy(data, tpm_cap, sizeof(tpm_cap));
495 data[TPM_CAP_IDX] = TPM_CAP_PROP;
496 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
497
498 rc = transmit_cmd(chip, data, sizeof(data),
499 "attempting to determine the timeouts");
500}
501EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
502
503void tpm_get_timeouts(struct tpm_chip *chip)
504{
505 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
506 ssize_t rc;
507 u32 timeout;
508
509 memcpy(data, tpm_cap, sizeof(tpm_cap));
510 data[TPM_CAP_IDX] = TPM_CAP_PROP;
511 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
512
513 rc = transmit_cmd(chip, data, sizeof(data),
514 "attempting to determine the timeouts");
515 if (rc)
516 goto duration;
517
518 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
519 != 4 * sizeof(u32))
520 goto duration;
521
522 /* Don't overwrite default if value is 0 */
523 timeout =
524 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
525 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700526 chip->vendor.timeout_a = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700527 timeout =
528 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
529 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700530 chip->vendor.timeout_b = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700531 timeout =
532 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
533 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700534 chip->vendor.timeout_c = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700535 timeout =
536 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
537 if (timeout)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700538 chip->vendor.timeout_d = msecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700539
540duration:
541 memcpy(data, tpm_cap, sizeof(tpm_cap));
542 data[TPM_CAP_IDX] = TPM_CAP_PROP;
543 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
544
545 rc = transmit_cmd(chip, data, sizeof(data),
546 "attempting to determine the durations");
547 if (rc)
548 return;
549
550 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
551 != 3 * sizeof(u32))
552 return;
553
554 chip->vendor.duration[TPM_SHORT] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700555 msecs_to_jiffies(be32_to_cpu
556 (*((__be32 *) (data +
557 TPM_GET_CAP_RET_UINT32_1_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700558 chip->vendor.duration[TPM_MEDIUM] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700559 msecs_to_jiffies(be32_to_cpu
560 (*((__be32 *) (data +
561 TPM_GET_CAP_RET_UINT32_2_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700562 chip->vendor.duration[TPM_LONG] =
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700563 msecs_to_jiffies(be32_to_cpu
564 (*((__be32 *) (data +
565 TPM_GET_CAP_RET_UINT32_3_IDX))));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700566}
567EXPORT_SYMBOL_GPL(tpm_get_timeouts);
568
569void tpm_continue_selftest(struct tpm_chip *chip)
570{
571 u8 data[] = {
572 0, 193, /* TPM_TAG_RQU_COMMAND */
573 0, 0, 0, 10, /* length */
574 0, 0, 0, 83, /* TPM_ORD_GetCapability */
575 };
576
577 tpm_transmit(chip, data, sizeof(data));
578}
579EXPORT_SYMBOL_GPL(tpm_continue_selftest);
580
581ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
582 char *buf)
583{
584 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
585 ssize_t rc;
586
587 struct tpm_chip *chip = dev_get_drvdata(dev);
588 if (chip == NULL)
589 return -ENODEV;
590
591 memcpy(data, tpm_cap, sizeof(tpm_cap));
592 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
593 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
594
595 rc = transmit_cmd(chip, data, sizeof(data),
596 "attemtping to determine the permanent state");
597 if (rc)
598 return 0;
599 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
600}
601EXPORT_SYMBOL_GPL(tpm_show_enabled);
602
603ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
604 char *buf)
605{
606 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
607 ssize_t rc;
608
609 struct tpm_chip *chip = dev_get_drvdata(dev);
610 if (chip == NULL)
611 return -ENODEV;
612
613 memcpy(data, tpm_cap, sizeof(tpm_cap));
614 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
615 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
616
617 rc = transmit_cmd(chip, data, sizeof(data),
618 "attemtping to determine the permanent state");
619 if (rc)
620 return 0;
621 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
622}
623EXPORT_SYMBOL_GPL(tpm_show_active);
624
625ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
626 char *buf)
627{
628 u8 data[sizeof(tpm_cap)];
629 ssize_t rc;
630
631 struct tpm_chip *chip = dev_get_drvdata(dev);
632 if (chip == NULL)
633 return -ENODEV;
634
635 memcpy(data, tpm_cap, sizeof(tpm_cap));
636 data[TPM_CAP_IDX] = TPM_CAP_PROP;
637 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
638
639 rc = transmit_cmd(chip, data, sizeof(data),
640 "attempting to determine the owner state");
641 if (rc)
642 return 0;
643 return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
644}
645EXPORT_SYMBOL_GPL(tpm_show_owned);
646
647ssize_t tpm_show_temp_deactivated(struct device * dev,
648 struct device_attribute * attr, char *buf)
649{
650 u8 data[sizeof(tpm_cap)];
651 ssize_t rc;
652
653 struct tpm_chip *chip = dev_get_drvdata(dev);
654 if (chip == NULL)
655 return -ENODEV;
656
657 memcpy(data, tpm_cap, sizeof(tpm_cap));
658 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
659 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
660
661 rc = transmit_cmd(chip, data, sizeof(data),
662 "attempting to determine the temporary state");
663 if (rc)
664 return 0;
665 return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
666}
667EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
668
Kylene Halldff37e42005-06-23 22:01:50 -0700669static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 0, 193, /* TPM_TAG_RQU_COMMAND */
671 0, 0, 0, 14, /* length */
672 0, 0, 0, 21, /* TPM_ORD_PcrRead */
673 0, 0, 0, 0 /* PCR index */
674};
675
Kylene Hall6659ca22005-06-23 22:02:00 -0700676ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
677 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700679 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700680 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700681 int i, j, num_pcrs;
682 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 char *str = buf;
684
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800685 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (chip == NULL)
687 return -ENODEV;
688
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700689 memcpy(data, tpm_cap, sizeof(tpm_cap));
690 data[TPM_CAP_IDX] = TPM_CAP_PROP;
691 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
692
693 rc = transmit_cmd(chip, data, sizeof(data),
694 "attempting to determine the number of PCRS");
695 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Kylene Hall81179bb2005-06-23 22:01:59 -0700698 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 for (i = 0; i < num_pcrs; i++) {
700 memcpy(data, pcrread, sizeof(pcrread));
701 index = cpu_to_be32(i);
702 memcpy(data + 10, &index, 4);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700703 rc = transmit_cmd(chip, data, sizeof(data),
704 "attempting to read a PCR");
705 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700706 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 str += sprintf(str, "PCR-%02d: ", i);
708 for (j = 0; j < TPM_DIGEST_SIZE; j++)
709 str += sprintf(str, "%02X ", *(data + 10 + j));
710 str += sprintf(str, "\n");
711 }
Kylene Halle234bc92005-06-23 22:02:08 -0700712out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return str - buf;
714}
Kylene Hall6659ca22005-06-23 22:02:00 -0700715EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700718static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 0, 193, /* TPM_TAG_RQU_COMMAND */
720 0, 0, 0, 30, /* length */
721 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
722};
723
Kylene Hall6659ca22005-06-23 22:02:00 -0700724ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
725 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Kylene Hall2df71112005-06-23 22:01:54 -0700727 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700728 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700729 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 char *str = buf;
731
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800732 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (chip == NULL)
734 return -ENODEV;
735
Andrew Mortonb888c872005-10-30 15:03:28 -0800736 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700737 if (!data)
738 return -ENOMEM;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700742 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
743 "attempting to read the PUBEK");
744 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
748 ignore header 10 bytes
749 algorithm 32 bits (1 == RSA )
750 encscheme 16 bits
751 sigscheme 16 bits
752 parameters (RSA 12->bytes: keybit, #primes, expbit)
753 keylenbytes 32 bits
754 256 byte modulus
755 ignore checksum 20 bytes
756 */
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 str +=
759 sprintf(str,
760 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
761 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
762 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
763 "Modulus length: %d\nModulus: \n",
764 data[10], data[11], data[12], data[13], data[14],
765 data[15], data[16], data[17], data[22], data[23],
766 data[24], data[25], data[26], data[27], data[28],
767 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700768 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700771 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if ((i + 1) % 16 == 0)
773 str += sprintf(str, "\n");
774 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700775out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700776 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700777 kfree(data);
778 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
Kylene Hall6659ca22005-06-23 22:02:00 -0700780EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700782#define CAP_VERSION_1_1 6
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700783#define CAP_VERSION_1_2 0x1A
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700784#define CAP_VERSION_IDX 13
Kylene Halldff37e42005-06-23 22:01:50 -0700785static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 0, 193, /* TPM_TAG_RQU_COMMAND */
787 0, 0, 0, 18, /* length */
788 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700789 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 0, 0, 0, 0
791};
792
Kylene Hall6659ca22005-06-23 22:02:00 -0700793ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
794 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700796 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700797 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 char *str = buf;
799
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800800 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (chip == NULL)
802 return -ENODEV;
803
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700804 memcpy(data, tpm_cap, sizeof(tpm_cap));
805 data[TPM_CAP_IDX] = TPM_CAP_PROP;
806 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700808 rc = transmit_cmd(chip, data, sizeof(data),
809 "attempting to determine the manufacturer");
810 if (rc)
811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700814 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 memcpy(data, cap_version, sizeof(cap_version));
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700817 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
818 rc = transmit_cmd(chip, data, sizeof(data),
819 "attempting to determine the 1.1 version");
820 if (rc)
821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700823 str += sprintf(str,
824 "TCG version: %d.%d\nFirmware version: %d.%d\n",
825 (int) data[14], (int) data[15], (int) data[16],
826 (int) data[17]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700828out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return str - buf;
830}
Kylene Hall6659ca22005-06-23 22:02:00 -0700831EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700833ssize_t tpm_show_caps_1_2(struct device * dev,
834 struct device_attribute * attr, char *buf)
835{
836 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
837 ssize_t len;
838 char *str = buf;
839
840 struct tpm_chip *chip = dev_get_drvdata(dev);
841 if (chip == NULL)
842 return -ENODEV;
843
844 memcpy(data, tpm_cap, sizeof(tpm_cap));
845 data[TPM_CAP_IDX] = TPM_CAP_PROP;
846 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
847
848 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
849 TPM_ERROR_SIZE) {
850 dev_dbg(chip->dev, "A TPM error (%d) occurred "
851 "attempting to determine the manufacturer\n",
852 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
853 return 0;
854 }
855
856 str += sprintf(str, "Manufacturer: 0x%x\n",
857 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
858
859 memcpy(data, cap_version, sizeof(cap_version));
860 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
861
862 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
863 TPM_ERROR_SIZE) {
864 dev_err(chip->dev, "A TPM error (%d) occurred "
865 "attempting to determine the 1.2 version\n",
866 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
867 goto out;
868 }
869 str += sprintf(str,
870 "TCG version: %d.%d\nFirmware version: %d.%d\n",
871 (int) data[16], (int) data[17], (int) data[18],
872 (int) data[19]);
873
874out:
875 return str - buf;
876}
877EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
878
Kylene Hall6659ca22005-06-23 22:02:00 -0700879ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
880 const char *buf, size_t count)
881{
882 struct tpm_chip *chip = dev_get_drvdata(dev);
883 if (chip == NULL)
884 return 0;
885
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700886 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -0700887 return count;
888}
889EXPORT_SYMBOL_GPL(tpm_store_cancel);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/*
892 * Device file system interface to the TPM
893 */
894int tpm_open(struct inode *inode, struct file *file)
895{
896 int rc = 0, minor = iminor(inode);
897 struct tpm_chip *chip = NULL, *pos;
898
899 spin_lock(&driver_lock);
900
901 list_for_each_entry(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700902 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 chip = pos;
904 break;
905 }
906 }
907
908 if (chip == NULL) {
909 rc = -ENODEV;
910 goto err_out;
911 }
912
913 if (chip->num_opens) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800914 dev_dbg(chip->dev, "Another process owns this TPM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rc = -EBUSY;
916 goto err_out;
917 }
918
919 chip->num_opens++;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800920 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 spin_unlock(&driver_lock);
923
924 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
925 if (chip->data_buffer == NULL) {
926 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800927 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ENOMEM;
929 }
930
931 atomic_set(&chip->data_pending, 0);
932
933 file->private_data = chip;
934 return 0;
935
936err_out:
937 spin_unlock(&driver_lock);
938 return rc;
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940EXPORT_SYMBOL_GPL(tpm_open);
941
942int tpm_release(struct inode *inode, struct file *file)
943{
944 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 spin_lock(&driver_lock);
Kylene Hall5e976d52005-06-23 22:02:03 -0700947 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 chip->num_opens--;
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700949 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -0800950 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 atomic_set(&chip->data_pending, 0);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800952 put_device(chip->dev);
Kylene Hall5e976d52005-06-23 22:02:03 -0700953 kfree(chip->data_buffer);
954 spin_unlock(&driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957EXPORT_SYMBOL_GPL(tpm_release);
958
Andrew Mortonb888c872005-10-30 15:03:28 -0800959ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700960 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct tpm_chip *chip = file->private_data;
963 int in_size = size, out_size;
964
965 /* cannot perform a write until the read has cleared
966 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700967 while (atomic_read(&chip->data_pending) != 0)
968 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 down(&chip->buffer_mutex);
971
972 if (in_size > TPM_BUFSIZE)
973 in_size = TPM_BUFSIZE;
974
975 if (copy_from_user
976 (chip->data_buffer, (void __user *) buf, in_size)) {
977 up(&chip->buffer_mutex);
978 return -EFAULT;
979 }
980
981 /* atomic tpm command send and result receive */
982 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
983
984 atomic_set(&chip->data_pending, out_size);
985 up(&chip->buffer_mutex);
986
987 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700988 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 return in_size;
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992EXPORT_SYMBOL_GPL(tpm_write);
993
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700994ssize_t tpm_read(struct file *file, char __user *buf,
995 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct tpm_chip *chip = file->private_data;
Kylene Hall5b44bd52005-06-23 22:01:53 -0700998 int ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Kylene Hall5b44bd52005-06-23 22:01:53 -07001000 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -08001001 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -07001002 ret_size = atomic_read(&chip->data_pending);
1003 atomic_set(&chip->data_pending, 0);
1004 if (ret_size > 0) { /* relay data */
1005 if (size < ret_size)
1006 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 down(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001009 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001010 ret_size = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 up(&chip->buffer_mutex);
1012 }
1013
1014 return ret_size;
1015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016EXPORT_SYMBOL_GPL(tpm_read);
1017
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001018void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001020 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001023 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026
1027 spin_lock(&driver_lock);
1028
1029 list_del(&chip->list);
1030
1031 spin_unlock(&driver_lock);
1032
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001033 dev_set_drvdata(dev, NULL);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001034 misc_deregister(&chip->vendor.miscdev);
1035 kfree(chip->vendor.miscdev.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001037 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001038 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001040 clear_bit(chip->dev_num, dev_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 kfree(chip);
1043
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001044 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001046EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048static u8 savestate[] = {
1049 0, 193, /* TPM_TAG_RQU_COMMAND */
1050 0, 0, 0, 10, /* blob length (in bytes) */
1051 0, 0, 0, 152 /* TPM_ORD_SaveState */
1052};
1053
1054/*
1055 * We are about to suspend. Save the TPM state
1056 * so that it can be restored.
1057 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001058int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001060 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (chip == NULL)
1062 return -ENODEV;
1063
1064 tpm_transmit(chip, savestate, sizeof(savestate));
1065 return 0;
1066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1068
1069/*
1070 * Resume from a power safe. The BIOS already restored
1071 * the TPM state.
1072 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001073int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001075 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (chip == NULL)
1078 return -ENODEV;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082EXPORT_SYMBOL_GPL(tpm_pm_resume);
1083
1084/*
1085 * Called from tpm_<specific>.c probe function only for devices
1086 * the driver has determined it should claim. Prior to calling
1087 * this function the specific probe function has called pci_enable_device
1088 * upon errant exit from this function specific probe function should call
1089 * pci_disable_device
1090 */
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001091struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
1092 *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001094#define DEVNAME_SIZE 7
1095
1096 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001100 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (chip == NULL)
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001102 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 init_MUTEX(&chip->buffer_mutex);
1105 init_MUTEX(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 INIT_LIST_HEAD(&chip->list);
1107
David Howellsc4028952006-11-22 14:57:56 +00001108 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001109
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001110 init_timer(&chip->user_read_timer);
1111 chip->user_read_timer.function = user_reader_timeout;
1112 chip->user_read_timer.data = (unsigned long) chip;
1113
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001114 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001116 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001118 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001119 dev_err(dev, "No available tpm device numbers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree(chip);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001121 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001123 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001125 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001127 set_bit(chip->dev_num, dev_mask);
1128
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001129 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1130 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001131 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001133 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001134 chip->dev = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001136 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001137 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001139 chip->vendor.miscdev.name,
1140 chip->vendor.miscdev.minor);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001141 put_device(dev);
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001142 clear_bit(chip->dev_num, dev_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 kfree(chip);
Randy Dunlap2e826362006-07-14 00:24:28 -07001144 kfree(devname);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001145 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Kylene Hall5e976d52005-06-23 22:02:03 -07001148 spin_lock(&driver_lock);
1149
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001150 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 list_add(&chip->list, &tpm_chip_list);
1153
Kylene Hall5e976d52005-06-23 22:02:03 -07001154 spin_unlock(&driver_lock);
1155
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001156 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1157 list_del(&chip->list);
Neil Horman5d469ec2006-12-06 20:37:08 -08001158 misc_deregister(&chip->vendor.miscdev);
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001159 put_device(dev);
1160 clear_bit(chip->dev_num, dev_mask);
1161 kfree(chip);
1162 kfree(devname);
1163 return NULL;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001166 chip->bios_dir = tpm_bios_log_setup(devname);
1167
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001168 return chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170EXPORT_SYMBOL_GPL(tpm_register_hardware);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1173MODULE_DESCRIPTION("TPM Driver");
1174MODULE_VERSION("2.0");
1175MODULE_LICENSE("GPL");