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