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