blob: b6c4a320cfde4c39618d489606237200943e7df0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/spinlock.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "tpm.h"
32
Kylene Hall3122a882005-06-23 22:01:48 -070033enum tpm_const {
34 TPM_MINOR = 224, /* officially assigned */
Rajiv Andrade7f366782009-09-24 16:27:46 -030035 TPM_BUFSIZE = 4096,
Kylene Hall3122a882005-06-23 22:01:48 -070036 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070037};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070039enum tpm_duration {
40 TPM_SHORT = 0,
41 TPM_MEDIUM = 1,
42 TPM_LONG = 2,
43 TPM_UNDEFINED,
44};
45
46#define TPM_MAX_ORDINAL 243
47#define TPM_MAX_PROTECTED_ORDINAL 12
48#define TPM_PROTECTED_ORDINAL_MASK 0xFF
49
Dmitry Torokhov9b3056c2010-10-01 14:16:39 -070050/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static LIST_HEAD(tpm_chip_list);
61static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070062static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070064/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void user_reader_timeout(unsigned long ptr)
334{
335 struct tpm_chip *chip = (struct tpm_chip *) ptr;
336
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800337 schedule_work(&chip->work);
338}
339
David Howellsc4028952006-11-22 14:57:56 +0000340static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800341{
David Howellsc4028952006-11-22 14:57:56 +0000342 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800343
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700344 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 atomic_set(&chip->data_pending, 0);
346 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700347 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700351 * 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
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800367 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700368 duration = chip->vendor.duration[duration_idx];
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800369 if (duration <= 0)
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700370 return 2 * 60 * HZ;
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800371 else
372 return duration;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700373}
374EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
375
376/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * 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{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700382 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700383 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700384 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Kylene Hall81179bb2005-06-23 22:01:59 -0700386 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700387 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (count == 0)
389 return -ENODATA;
390 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800391 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700392 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -E2BIG;
394 }
395
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700396 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700398 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800399 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700400 "tpm_transmit: tpm_send: error %zd\n", rc);
401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700404 if (chip->vendor.irq)
405 goto out_recv;
406
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700407 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700409 u8 status = chip->vendor.status(chip);
410 if ((status & chip->vendor.req_complete_mask) ==
411 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700413
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700414 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800415 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700416 rc = -ECANCELED;
417 goto out;
418 }
419
420 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700422 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700424 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800425 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700426 rc = -ETIME;
427 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700430 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700431 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800432 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700433 "tpm_transmit: tpm_recv: error %zd\n", rc);
434out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700435 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700436 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700440#define TPM_ERROR_SIZE 10
441#define TPM_RET_CODE_IDX 6
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700442
443enum tpm_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200444 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)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700448};
449
450enum tpm_sub_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200451 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),
458
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700459};
460
Rajiv Andrade08837432009-02-02 15:23:43 -0200461static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
462 int len, const char *desc)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700463{
464 int err;
465
Rajiv Andrade08837432009-02-02 15:23:43 -0200466 len = tpm_transmit(chip,(u8 *) cmd, len);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700467 if (len < 0)
468 return len;
469 if (len == TPM_ERROR_SIZE) {
Rajiv Andrade08837432009-02-02 15:23:43 -0200470 err = be32_to_cpu(cmd->header.out.return_code);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700471 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
472 return err;
473 }
474 return 0;
475}
476
Rajiv Andrade08837432009-02-02 15:23:43 -0200477#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
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700515void tpm_gen_interrupt(struct tpm_chip *chip)
516{
Rajiv Andrade08837432009-02-02 15:23:43 -0200517 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700518 ssize_t rc;
519
Rajiv Andrade08837432009-02-02 15:23:43 -0200520 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;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700524
Rajiv Andrade08837432009-02-02 15:23:43 -0200525 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700526 "attempting to determine the timeouts");
527}
528EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
529
Stefan Berger2b30a902011-11-11 12:57:02 -0500530int tpm_get_timeouts(struct tpm_chip *chip)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700531{
Rajiv Andrade08837432009-02-02 15:23:43 -0200532 struct tpm_cmd_t tpm_cmd;
533 struct timeout_t *timeout_cap;
534 struct duration_t *duration_cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700535 ssize_t rc;
536 u32 timeout;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400537 unsigned int scale = 1;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700538
Rajiv Andrade08837432009-02-02 15:23:43 -0200539 tpm_cmd.header.in = tpm_getcap_header;
540 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
541 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
542 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700543
Rajiv Andrade08837432009-02-02 15:23:43 -0200544 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700545 "attempting to determine the timeouts");
546 if (rc)
547 goto duration;
548
Stefan Berger829bf062011-03-30 12:13:26 -0400549 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
550 be32_to_cpu(tpm_cmd.header.out.length)
551 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500552 return -EINVAL;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700553
Rajiv Andrade08837432009-02-02 15:23:43 -0200554 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700555 /* Don't overwrite default if value is 0 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200556 timeout = be32_to_cpu(timeout_cap->a);
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400557 if (timeout && timeout < 1000) {
558 /* timeouts in msec rather usec */
559 scale = 1000;
Stefan Berger62592102011-03-30 12:13:28 -0400560 chip->vendor.timeout_adjusted = true;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400561 }
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700562 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400563 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200564 timeout = be32_to_cpu(timeout_cap->b);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700565 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400566 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200567 timeout = be32_to_cpu(timeout_cap->c);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700568 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400569 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200570 timeout = be32_to_cpu(timeout_cap->d);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700571 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400572 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700573
574duration:
Rajiv Andrade08837432009-02-02 15:23:43 -0200575 tpm_cmd.header.in = tpm_getcap_header;
576 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
577 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
578 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700579
Rajiv Andrade08837432009-02-02 15:23:43 -0200580 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700581 "attempting to determine the durations");
582 if (rc)
Stefan Berger2b30a902011-11-11 12:57:02 -0500583 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700584
Stefan Berger979b1402011-03-30 12:13:23 -0400585 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
586 be32_to_cpu(tpm_cmd.header.out.length)
587 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500588 return -EINVAL;
Stefan Berger979b1402011-03-30 12:13:23 -0400589
Rajiv Andrade08837432009-02-02 15:23:43 -0200590 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700591 chip->vendor.duration[TPM_SHORT] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200592 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700593 chip->vendor.duration[TPM_MEDIUM] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200594 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700595 chip->vendor.duration[TPM_LONG] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200596 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
Stefan Bergere934acc2011-03-30 12:13:24 -0400597
598 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
599 * value wrong and apparently reports msecs rather than usecs. So we
600 * fix up the resulting too-small TPM_SHORT value to make things work.
601 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
602 */
603 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
604 chip->vendor.duration[TPM_SHORT] = HZ;
605 chip->vendor.duration[TPM_MEDIUM] *= 1000;
606 chip->vendor.duration[TPM_LONG] *= 1000;
Stefan Berger04ab2292011-03-30 12:13:25 -0400607 chip->vendor.duration_adjusted = true;
Stefan Bergere934acc2011-03-30 12:13:24 -0400608 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
609 }
Stefan Berger2b30a902011-11-11 12:57:02 -0500610 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700611}
612EXPORT_SYMBOL_GPL(tpm_get_timeouts);
613
614void tpm_continue_selftest(struct tpm_chip *chip)
615{
616 u8 data[] = {
617 0, 193, /* TPM_TAG_RQU_COMMAND */
618 0, 0, 0, 10, /* length */
Stefan Bergerc9206692011-03-30 12:13:34 -0400619 0, 0, 0, 83, /* TPM_ORD_ContinueSelfTest */
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700620 };
621
622 tpm_transmit(chip, data, sizeof(data));
623}
624EXPORT_SYMBOL_GPL(tpm_continue_selftest);
625
626ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
627 char *buf)
628{
Rajiv Andrade08837432009-02-02 15:23:43 -0200629 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700630 ssize_t rc;
631
Rajiv Andrade08837432009-02-02 15:23:43 -0200632 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
633 "attempting to determine the permanent enabled state");
634 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700635 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700636
Rajiv Andrade08837432009-02-02 15:23:43 -0200637 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700638 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700639}
640EXPORT_SYMBOL_GPL(tpm_show_enabled);
641
642ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
643 char *buf)
644{
Rajiv Andrade08837432009-02-02 15:23:43 -0200645 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700646 ssize_t rc;
647
Rajiv Andrade08837432009-02-02 15:23:43 -0200648 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
649 "attempting to determine the permanent active state");
650 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700651 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700652
Rajiv Andrade08837432009-02-02 15:23:43 -0200653 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700654 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700655}
656EXPORT_SYMBOL_GPL(tpm_show_active);
657
658ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
659 char *buf)
660{
Rajiv Andrade08837432009-02-02 15:23:43 -0200661 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700662 ssize_t rc;
663
Rajiv Andrade08837432009-02-02 15:23:43 -0200664 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
665 "attempting to determine the owner state");
666 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700667 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700668
Rajiv Andrade08837432009-02-02 15:23:43 -0200669 rc = sprintf(buf, "%d\n", cap.owned);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700670 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700671}
672EXPORT_SYMBOL_GPL(tpm_show_owned);
673
674ssize_t tpm_show_temp_deactivated(struct device * dev,
675 struct device_attribute * attr, char *buf)
676{
Rajiv Andrade08837432009-02-02 15:23:43 -0200677 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700678 ssize_t rc;
679
Rajiv Andrade08837432009-02-02 15:23:43 -0200680 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
681 "attempting to determine the temporary state");
682 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700683 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700684
Rajiv Andrade08837432009-02-02 15:23:43 -0200685 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700686 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700687}
688EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
689
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200690/*
691 * tpm_chip_find_get - return tpm_chip for given chip number
692 */
693static struct tpm_chip *tpm_chip_find_get(int chip_num)
694{
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200695 struct tpm_chip *pos, *chip = NULL;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200696
697 rcu_read_lock();
698 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
699 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
700 continue;
701
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200702 if (try_module_get(pos->dev->driver->owner)) {
703 chip = pos;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200704 break;
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200705 }
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200706 }
707 rcu_read_unlock();
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200708 return chip;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200709}
710
711#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
712#define READ_PCR_RESULT_SIZE 30
713static struct tpm_input_header pcrread_header = {
714 .tag = TPM_TAG_RQU_COMMAND,
715 .length = cpu_to_be32(14),
716 .ordinal = TPM_ORDINAL_PCRREAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717};
718
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200719int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
720{
721 int rc;
722 struct tpm_cmd_t cmd;
723
724 cmd.header.in = pcrread_header;
725 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
Rajiv Andrade23acb982009-09-30 12:26:55 -0300726 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200727 "attempting to read a pcr value");
728
729 if (rc == 0)
730 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
731 TPM_DIGEST_SIZE);
732 return rc;
733}
734
735/**
736 * tpm_pcr_read - read a pcr value
737 * @chip_num: tpm idx # or ANY
738 * @pcr_idx: pcr idx to retrieve
739 * @res_buf: TPM_PCR value
740 * size of res_buf is 20 bytes (or NULL if you don't care)
741 *
742 * The TPM driver should be built-in, but for whatever reason it
743 * isn't, protect against the chip disappearing, by incrementing
744 * the module usage count.
745 */
746int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
747{
748 struct tpm_chip *chip;
749 int rc;
750
751 chip = tpm_chip_find_get(chip_num);
752 if (chip == NULL)
753 return -ENODEV;
754 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
Mimi Zohara0e39342010-11-23 17:50:32 -0500755 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200756 return rc;
757}
758EXPORT_SYMBOL_GPL(tpm_pcr_read);
759
760/**
761 * tpm_pcr_extend - extend pcr value with hash
762 * @chip_num: tpm idx # or AN&
763 * @pcr_idx: pcr idx to extend
764 * @hash: hash value used to extend pcr value
765 *
766 * The TPM driver should be built-in, but for whatever reason it
767 * isn't, protect against the chip disappearing, by incrementing
768 * the module usage count.
769 */
770#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700771#define EXTEND_PCR_RESULT_SIZE 34
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200772static struct tpm_input_header pcrextend_header = {
773 .tag = TPM_TAG_RQU_COMMAND,
774 .length = cpu_to_be32(34),
775 .ordinal = TPM_ORD_PCR_EXTEND
776};
777
778int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
779{
780 struct tpm_cmd_t cmd;
781 int rc;
782 struct tpm_chip *chip;
783
784 chip = tpm_chip_find_get(chip_num);
785 if (chip == NULL)
786 return -ENODEV;
787
788 cmd.header.in = pcrextend_header;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200789 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
790 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700791 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200792 "attempting extend a PCR value");
793
Mimi Zohara0e39342010-11-23 17:50:32 -0500794 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200795 return rc;
796}
797EXPORT_SYMBOL_GPL(tpm_pcr_extend);
798
Mimi Zoharc749ba92010-11-23 18:54:16 -0500799int tpm_send(u32 chip_num, void *cmd, size_t buflen)
800{
801 struct tpm_chip *chip;
802 int rc;
803
804 chip = tpm_chip_find_get(chip_num);
805 if (chip == NULL)
806 return -ENODEV;
807
808 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
809
810 tpm_chip_put(chip);
811 return rc;
812}
813EXPORT_SYMBOL_GPL(tpm_send);
814
Kylene Hall6659ca22005-06-23 22:02:00 -0700815ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
816 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Rajiv Andrade08837432009-02-02 15:23:43 -0200818 cap_t cap;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200819 u8 digest[TPM_DIGEST_SIZE];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700820 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700821 int i, j, num_pcrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 char *str = buf;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800823 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Rajiv Andrade08837432009-02-02 15:23:43 -0200825 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700826 "attempting to determine the number of PCRS");
Rajiv Andrade08837432009-02-02 15:23:43 -0200827 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Rajiv Andrade08837432009-02-02 15:23:43 -0200830 num_pcrs = be32_to_cpu(cap.num_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 for (i = 0; i < num_pcrs; i++) {
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200832 rc = __tpm_pcr_read(chip, i, digest);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700833 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 str += sprintf(str, "PCR-%02d: ", i);
836 for (j = 0; j < TPM_DIGEST_SIZE; j++)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200837 str += sprintf(str, "%02X ", digest[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 str += sprintf(str, "\n");
839 }
840 return str - buf;
841}
Kylene Hall6659ca22005-06-23 22:02:00 -0700842EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844#define READ_PUBEK_RESULT_SIZE 314
Rajiv Andrade08837432009-02-02 15:23:43 -0200845#define TPM_ORD_READPUBEK cpu_to_be32(124)
846struct tpm_input_header tpm_readpubek_header = {
847 .tag = TPM_TAG_RQU_COMMAND,
848 .length = cpu_to_be32(30),
849 .ordinal = TPM_ORD_READPUBEK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850};
851
Kylene Hall6659ca22005-06-23 22:02:00 -0700852ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
853 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Kylene Hall2df71112005-06-23 22:01:54 -0700855 u8 *data;
Rajiv Andrade08837432009-02-02 15:23:43 -0200856 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700857 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700858 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 char *str = buf;
860
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800861 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Rajiv Andrade08837432009-02-02 15:23:43 -0200863 tpm_cmd.header.in = tpm_readpubek_header;
864 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700865 "attempting to read the PUBEK");
866 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /*
870 ignore header 10 bytes
871 algorithm 32 bits (1 == RSA )
872 encscheme 16 bits
873 sigscheme 16 bits
874 parameters (RSA 12->bytes: keybit, #primes, expbit)
875 keylenbytes 32 bits
876 256 byte modulus
877 ignore checksum 20 bytes
878 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200879 data = tpm_cmd.params.readpubek_out_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 str +=
881 sprintf(str,
Stefan Berger5a794442011-03-30 12:13:29 -0400882 "Algorithm: %02X %02X %02X %02X\n"
883 "Encscheme: %02X %02X\n"
884 "Sigscheme: %02X %02X\n"
885 "Parameters: %02X %02X %02X %02X "
886 "%02X %02X %02X %02X "
887 "%02X %02X %02X %02X\n"
888 "Modulus length: %d\n"
889 "Modulus:\n",
890 data[0], data[1], data[2], data[3],
891 data[4], data[5],
892 data[6], data[7],
893 data[12], data[13], data[14], data[15],
894 data[16], data[17], data[18], data[19],
895 data[20], data[21], data[22], data[23],
896 be32_to_cpu(*((__be32 *) (data + 24))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 for (i = 0; i < 256; i++) {
Stefan Berger5a794442011-03-30 12:13:29 -0400899 str += sprintf(str, "%02X ", data[i + 28]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if ((i + 1) % 16 == 0)
901 str += sprintf(str, "\n");
902 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700903out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700904 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700905 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
Kylene Hall6659ca22005-06-23 22:02:00 -0700907EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Kylene Hall6659ca22005-06-23 22:02:00 -0700910ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
911 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Rajiv Andrade08837432009-02-02 15:23:43 -0200913 cap_t cap;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700914 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 char *str = buf;
916
Rajiv Andrade08837432009-02-02 15:23:43 -0200917 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700918 "attempting to determine the manufacturer");
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700919 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200920 return 0;
921 str += sprintf(str, "Manufacturer: 0x%x\n",
922 be32_to_cpu(cap.manufacturer_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Rajiv Andrade08837432009-02-02 15:23:43 -0200924 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
925 "attempting to determine the 1.1 version");
926 if (rc)
927 return 0;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700928 str += sprintf(str,
929 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200930 cap.tpm_version.Major, cap.tpm_version.Minor,
931 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return str - buf;
933}
Kylene Hall6659ca22005-06-23 22:02:00 -0700934EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700936ssize_t tpm_show_caps_1_2(struct device * dev,
937 struct device_attribute * attr, char *buf)
938{
Rajiv Andrade08837432009-02-02 15:23:43 -0200939 cap_t cap;
940 ssize_t rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700941 char *str = buf;
942
Rajiv Andrade08837432009-02-02 15:23:43 -0200943 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
944 "attempting to determine the manufacturer");
945 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700946 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700947 str += sprintf(str, "Manufacturer: 0x%x\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200948 be32_to_cpu(cap.manufacturer_id));
949 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
950 "attempting to determine the 1.2 version");
951 if (rc)
952 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700953 str += sprintf(str,
954 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200955 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
956 cap.tpm_version_1_2.revMajor,
957 cap.tpm_version_1_2.revMinor);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700958 return str - buf;
959}
960EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
961
Stefan Berger04ab2292011-03-30 12:13:25 -0400962ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
963 char *buf)
964{
965 struct tpm_chip *chip = dev_get_drvdata(dev);
966
Stefan Berger403d1d02011-08-23 08:52:10 -0400967 if (chip->vendor.duration[TPM_LONG] == 0)
968 return 0;
969
Stefan Berger04ab2292011-03-30 12:13:25 -0400970 return sprintf(buf, "%d %d %d [%s]\n",
971 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
972 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
973 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
974 chip->vendor.duration_adjusted
975 ? "adjusted" : "original");
976}
977EXPORT_SYMBOL_GPL(tpm_show_durations);
978
Stefan Berger62592102011-03-30 12:13:28 -0400979ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
980 char *buf)
981{
982 struct tpm_chip *chip = dev_get_drvdata(dev);
983
984 return sprintf(buf, "%d %d %d %d [%s]\n",
985 jiffies_to_usecs(chip->vendor.timeout_a),
986 jiffies_to_usecs(chip->vendor.timeout_b),
987 jiffies_to_usecs(chip->vendor.timeout_c),
988 jiffies_to_usecs(chip->vendor.timeout_d),
989 chip->vendor.timeout_adjusted
990 ? "adjusted" : "original");
991}
992EXPORT_SYMBOL_GPL(tpm_show_timeouts);
993
Kylene Hall6659ca22005-06-23 22:02:00 -0700994ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
995 const char *buf, size_t count)
996{
997 struct tpm_chip *chip = dev_get_drvdata(dev);
998 if (chip == NULL)
999 return 0;
1000
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001001 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -07001002 return count;
1003}
1004EXPORT_SYMBOL_GPL(tpm_store_cancel);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * Device file system interface to the TPM
Rajiv Andradef89c5ed2008-10-11 09:03:39 +11001008 *
1009 * It's assured that the chip will be opened just once,
1010 * by the check of is_open variable, which is protected
1011 * by driver_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
1013int tpm_open(struct inode *inode, struct file *file)
1014{
Rajiv Andradef02a9362008-10-11 09:04:23 +11001015 int minor = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct tpm_chip *chip = NULL, *pos;
1017
Rajiv Andradef02a9362008-10-11 09:04:23 +11001018 rcu_read_lock();
1019 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001020 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 chip = pos;
Rajiv Andradef02a9362008-10-11 09:04:23 +11001022 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024 }
1025 }
Rajiv Andradef02a9362008-10-11 09:04:23 +11001026 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Rajiv Andradef02a9362008-10-11 09:04:23 +11001028 if (!chip)
1029 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Rajiv Andradedc36d322008-10-11 09:04:02 +11001031 if (test_and_set_bit(0, &chip->is_open)) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001032 dev_dbg(chip->dev, "Another process owns this TPM\n");
Rajiv Andradef02a9362008-10-11 09:04:23 +11001033 put_device(chip->dev);
1034 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Peter Huewe1309d7a2011-03-29 13:31:25 +02001037 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (chip->data_buffer == NULL) {
Rajiv Andradedc36d322008-10-11 09:04:02 +11001039 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001040 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -ENOMEM;
1042 }
1043
1044 atomic_set(&chip->data_pending, 0);
1045
1046 file->private_data = chip;
1047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049EXPORT_SYMBOL_GPL(tpm_open);
1050
Rajiv Andradef02a9362008-10-11 09:04:23 +11001051/*
1052 * Called on file close
1053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054int tpm_release(struct inode *inode, struct file *file)
1055{
1056 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Rajiv Andrade4bdec112008-10-11 09:05:20 +11001058 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo2e5c44c2010-12-24 15:59:07 +01001059 flush_work_sync(&chip->work);
Kylene Hall5e976d52005-06-23 22:02:03 -07001060 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 atomic_set(&chip->data_pending, 0);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001062 kfree(chip->data_buffer);
Rajiv Andradedc36d322008-10-11 09:04:02 +11001063 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001064 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067EXPORT_SYMBOL_GPL(tpm_release);
1068
Andrew Mortonb888c872005-10-30 15:03:28 -08001069ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001070 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001073 size_t in_size = size, out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /* cannot perform a write until the read has cleared
1076 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -07001077 while (atomic_read(&chip->data_pending) != 0)
1078 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001080 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (in_size > TPM_BUFSIZE)
1083 in_size = TPM_BUFSIZE;
1084
1085 if (copy_from_user
1086 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001087 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EFAULT;
1089 }
1090
1091 /* atomic tpm command send and result receive */
1092 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1093
1094 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001095 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001098 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return in_size;
1101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102EXPORT_SYMBOL_GPL(tpm_write);
1103
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001104ssize_t tpm_read(struct file *file, char __user *buf,
1105 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001108 ssize_t ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Kylene Hall5b44bd52005-06-23 22:01:53 -07001110 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo2e5c44c2010-12-24 15:59:07 +01001111 flush_work_sync(&chip->work);
Kylene Hall5b44bd52005-06-23 22:01:53 -07001112 ret_size = atomic_read(&chip->data_pending);
1113 atomic_set(&chip->data_pending, 0);
1114 if (ret_size > 0) { /* relay data */
1115 if (size < ret_size)
1116 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001118 mutex_lock(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001119 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001120 ret_size = -EFAULT;
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001121 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124 return ret_size;
1125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126EXPORT_SYMBOL_GPL(tpm_read);
1127
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001128void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001130 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001133 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
1135 }
1136
1137 spin_lock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001138 list_del_rcu(&chip->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 spin_unlock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001140 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001142 misc_deregister(&chip->vendor.miscdev);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001143 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001144 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001146 /* write it this way to be explicit (chip->dev == dev) */
1147 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001149EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001151#define TPM_ORD_SAVESTATE cpu_to_be32(152)
1152#define SAVESTATE_RESULT_SIZE 10
1153
1154static struct tpm_input_header savestate_header = {
1155 .tag = TPM_TAG_RQU_COMMAND,
1156 .length = cpu_to_be32(10),
1157 .ordinal = TPM_ORD_SAVESTATE
1158};
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
1161 * We are about to suspend. Save the TPM state
1162 * so that it can be restored.
1163 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001164int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001166 struct tpm_chip *chip = dev_get_drvdata(dev);
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001167 struct tpm_cmd_t cmd;
1168 int rc;
1169
1170 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
David Smith2490c682008-01-14 00:55:12 -08001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (chip == NULL)
1173 return -ENODEV;
1174
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001175 /* for buggy tpm, flush pcrs with extend to selected dummy */
1176 if (tpm_suspend_pcr) {
1177 cmd.header.in = pcrextend_header;
1178 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1179 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1180 TPM_DIGEST_SIZE);
1181 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1182 "extending dummy pcr before suspend");
1183 }
1184
1185 /* now do the actual savestate */
1186 cmd.header.in = savestate_header;
1187 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1188 "sending savestate before suspend");
1189 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1192
1193/*
1194 * Resume from a power safe. The BIOS already restored
1195 * the TPM state.
1196 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001197int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001199 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (chip == NULL)
1202 return -ENODEV;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206EXPORT_SYMBOL_GPL(tpm_pm_resume);
1207
Rajiv Andrade253115b2008-10-11 09:04:39 +11001208/* In case vendor provided release function, call it too.*/
1209
1210void tpm_dev_vendor_release(struct tpm_chip *chip)
1211{
1212 if (chip->vendor.release)
1213 chip->vendor.release(chip->dev);
1214
1215 clear_bit(chip->dev_num, dev_mask);
1216 kfree(chip->vendor.miscdev.name);
1217}
1218EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1219
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001222 * Once all references to platform device are down to 0,
1223 * release all allocated structures.
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001224 */
Stephen Rothwellcbb2ed42008-10-18 20:25:46 -07001225void tpm_dev_release(struct device *dev)
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001226{
1227 struct tpm_chip *chip = dev_get_drvdata(dev);
1228
Rajiv Andrade253115b2008-10-11 09:04:39 +11001229 tpm_dev_vendor_release(chip);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001230
Rajiv Andrade253115b2008-10-11 09:04:39 +11001231 chip->release(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001232 kfree(chip);
1233}
Rajiv Andrade253115b2008-10-11 09:04:39 +11001234EXPORT_SYMBOL_GPL(tpm_dev_release);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001235
1236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * Called from tpm_<specific>.c probe function only for devices
1238 * the driver has determined it should claim. Prior to calling
1239 * this function the specific probe function has called pci_enable_device
1240 * upon errant exit from this function specific probe function should call
1241 * pci_disable_device
1242 */
Rajiv Andradef02a9362008-10-11 09:04:23 +11001243struct tpm_chip *tpm_register_hardware(struct device *dev,
1244 const struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001246#define DEVNAME_SIZE 7
1247
1248 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001252 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001253 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1254
Andrew Mortondd78c942008-10-15 22:04:37 -07001255 if (chip == NULL || devname == NULL)
1256 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001258 mutex_init(&chip->buffer_mutex);
1259 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 INIT_LIST_HEAD(&chip->list);
1261
David Howellsc4028952006-11-22 14:57:56 +00001262 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001263
Jiri Slaby40565f12007-02-12 00:52:31 -08001264 setup_timer(&chip->user_read_timer, user_reader_timeout,
1265 (unsigned long)chip);
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001266
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001267 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001269 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001271 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001272 dev_err(dev, "No available tpm device numbers\n");
Andrew Mortondd78c942008-10-15 22:04:37 -07001273 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001275 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001277 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001279 set_bit(chip->dev_num, dev_mask);
1280
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001281 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001282 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001284 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001285 chip->dev = get_device(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001286 chip->release = dev->release;
1287 dev->release = tpm_dev_release;
1288 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001290 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001291 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001293 chip->vendor.miscdev.name,
1294 chip->vendor.miscdev.minor);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001295 put_device(chip->dev);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001299 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
Neil Horman5d469ec02006-12-06 20:37:08 -08001300 misc_deregister(&chip->vendor.miscdev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001301 put_device(chip->dev);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001302
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001303 return NULL;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001306 chip->bios_dir = tpm_bios_log_setup(devname);
1307
Rajiv Andradef02a9362008-10-11 09:04:23 +11001308 /* Make chip available */
1309 spin_lock(&driver_lock);
1310 list_add_rcu(&chip->list, &tpm_chip_list);
1311 spin_unlock(&driver_lock);
1312
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001313 return chip;
Andrew Mortondd78c942008-10-15 22:04:37 -07001314
1315out_free:
1316 kfree(chip);
1317 kfree(devname);
1318 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320EXPORT_SYMBOL_GPL(tpm_register_hardware);
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1323MODULE_DESCRIPTION("TPM Driver");
1324MODULE_VERSION("2.0");
1325MODULE_LICENSE("GPL");