blob: 5e58296f2832204994b752ab39911bf1a7c03152 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 IBM Corporation
3 *
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 *
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
11 *
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
18 * License.
19 *
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070022 * calls to msleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 */
25
26#include <linux/sched.h>
27#include <linux/poll.h>
28#include <linux/spinlock.h>
29#include "tpm.h"
30
Kylene Hall3122a882005-06-23 22:01:48 -070031enum tpm_const {
32 TPM_MINOR = 224, /* officially assigned */
33 TPM_BUFSIZE = 2048,
34 TPM_NUM_DEVICES = 256,
35 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
36};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static LIST_HEAD(tpm_chip_list);
39static DEFINE_SPINLOCK(driver_lock);
Kylene Hall3122a882005-06-23 22:01:48 -070040static int dev_mask[TPM_NUM_MASK_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static void user_reader_timeout(unsigned long ptr)
43{
44 struct tpm_chip *chip = (struct tpm_chip *) ptr;
45
Kylene Jo Hall09e12f92005-11-13 16:07:43 -080046 schedule_work(&chip->work);
47}
48
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -070049static void timeout_work(void *ptr)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -080050{
51 struct tpm_chip *chip = ptr;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 down(&chip->buffer_mutex);
54 atomic_set(&chip->data_pending, 0);
55 memset(chip->data_buffer, 0, TPM_BUFSIZE);
56 up(&chip->buffer_mutex);
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Internal kernel interface to transmit TPM commands
61 */
62static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
63 size_t bufsiz)
64{
Kylene Halld9e5b6b2005-06-23 22:02:02 -070065 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 u32 count;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070067 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Kylene Hall81179bb2005-06-23 22:01:59 -070069 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (count == 0)
72 return -ENODATA;
73 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080074 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -070075 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -E2BIG;
77 }
78
79 down(&chip->tpm_mutex);
80
Kylene Jo Hall90dda522006-04-22 02:37:15 -070081 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080082 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -070083 "tpm_transmit: tpm_send: error %zd\n", rc);
84 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070087 stop = jiffies + 2 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -070089 u8 status = chip->vendor.status(chip);
90 if ((status & chip->vendor.req_complete_mask) ==
91 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -070093
Kylene Jo Hall90dda522006-04-22 02:37:15 -070094 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080095 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -070096 rc = -ECANCELED;
97 goto out;
98 }
99
100 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700102 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700104 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800105 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700106 rc = -ETIME;
107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700110 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700111 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800112 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700113 "tpm_transmit: tpm_recv: error %zd\n", rc);
114out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 up(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700116 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700120#define TPM_ERROR_SIZE 10
121#define TPM_RET_CODE_IDX 6
122#define TPM_GET_CAP_RET_SIZE_IDX 10
123#define TPM_GET_CAP_RET_UINT32_1_IDX 14
124#define TPM_GET_CAP_RET_UINT32_2_IDX 18
125#define TPM_GET_CAP_RET_UINT32_3_IDX 22
126#define TPM_GET_CAP_RET_UINT32_4_IDX 26
127
128#define TPM_CAP_IDX 13
129#define TPM_CAP_SUBCAP_IDX 21
130
131enum tpm_capabilities {
132 TPM_CAP_PROP = 5,
133};
134
135enum tpm_sub_capabilities {
136 TPM_CAP_PROP_PCR = 0x1,
137 TPM_CAP_PROP_MANUFACTURER = 0x3,
138};
139
140/*
141 * This is a semi generic GetCapability command for use
142 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
143 * and their associated sub_capabilities.
144 */
145
146static const u8 tpm_cap[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 0, 193, /* TPM_TAG_RQU_COMMAND */
148 0, 0, 0, 22, /* length */
149 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700150 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
151 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
152 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700155static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
156 char *desc)
157{
158 int err;
159
160 len = tpm_transmit(chip, data, len);
161 if (len < 0)
162 return len;
163 if (len == TPM_ERROR_SIZE) {
164 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
165 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
166 return err;
167 }
168 return 0;
169}
170
Kylene Halldff37e42005-06-23 22:01:50 -0700171static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 0, 193, /* TPM_TAG_RQU_COMMAND */
173 0, 0, 0, 14, /* length */
174 0, 0, 0, 21, /* TPM_ORD_PcrRead */
175 0, 0, 0, 0 /* PCR index */
176};
177
Kylene Hall6659ca22005-06-23 22:02:00 -0700178ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
179 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700181 u8 data[30];
182 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700183 int i, j, num_pcrs;
184 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 char *str = buf;
186
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800187 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (chip == NULL)
189 return -ENODEV;
190
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700191 memcpy(data, tpm_cap, sizeof(tpm_cap));
192 data[TPM_CAP_IDX] = TPM_CAP_PROP;
193 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
194
195 rc = transmit_cmd(chip, data, sizeof(data),
196 "attempting to determine the number of PCRS");
197 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Kylene Hall81179bb2005-06-23 22:01:59 -0700200 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 for (i = 0; i < num_pcrs; i++) {
202 memcpy(data, pcrread, sizeof(pcrread));
203 index = cpu_to_be32(i);
204 memcpy(data + 10, &index, 4);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700205 rc = transmit_cmd(chip, data, sizeof(data),
206 "attempting to read a PCR");
207 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 str += sprintf(str, "PCR-%02d: ", i);
210 for (j = 0; j < TPM_DIGEST_SIZE; j++)
211 str += sprintf(str, "%02X ", *(data + 10 + j));
212 str += sprintf(str, "\n");
213 }
Kylene Halle234bc92005-06-23 22:02:08 -0700214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return str - buf;
216}
Kylene Hall6659ca22005-06-23 22:02:00 -0700217EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700220static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 0, 193, /* TPM_TAG_RQU_COMMAND */
222 0, 0, 0, 30, /* length */
223 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
224};
225
Kylene Hall6659ca22005-06-23 22:02:00 -0700226ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
227 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Kylene Hall2df71112005-06-23 22:01:54 -0700229 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700230 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700231 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 char *str = buf;
233
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800234 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (chip == NULL)
236 return -ENODEV;
237
Andrew Mortonb888c872005-10-30 15:03:28 -0800238 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700239 if (!data)
240 return -ENOMEM;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700244 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
245 "attempting to read the PUBEK");
246 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /*
250 ignore header 10 bytes
251 algorithm 32 bits (1 == RSA )
252 encscheme 16 bits
253 sigscheme 16 bits
254 parameters (RSA 12->bytes: keybit, #primes, expbit)
255 keylenbytes 32 bits
256 256 byte modulus
257 ignore checksum 20 bytes
258 */
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 str +=
261 sprintf(str,
262 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
263 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
264 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
265 "Modulus length: %d\nModulus: \n",
266 data[10], data[11], data[12], data[13], data[14],
267 data[15], data[16], data[17], data[22], data[23],
268 data[24], data[25], data[26], data[27], data[28],
269 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700270 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700273 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if ((i + 1) % 16 == 0)
275 str += sprintf(str, "\n");
276 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700277out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700278 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700279 kfree(data);
280 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Kylene Hall6659ca22005-06-23 22:02:00 -0700282EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700284#define CAP_VERSION_1_1 6
285#define CAP_VERSION_IDX 13
Kylene Halldff37e42005-06-23 22:01:50 -0700286static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 0, 193, /* TPM_TAG_RQU_COMMAND */
288 0, 0, 0, 18, /* length */
289 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700290 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 0, 0, 0, 0
292};
293
Kylene Hall6659ca22005-06-23 22:02:00 -0700294ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
295 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700297 u8 data[30];
298 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 char *str = buf;
300
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800301 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (chip == NULL)
303 return -ENODEV;
304
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700305 memcpy(data, tpm_cap, sizeof(tpm_cap));
306 data[TPM_CAP_IDX] = TPM_CAP_PROP;
307 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700309 rc = transmit_cmd(chip, data, sizeof(data),
310 "attempting to determine the manufacturer");
311 if (rc)
312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700315 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 memcpy(data, cap_version, sizeof(cap_version));
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700318 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
319 rc = transmit_cmd(chip, data, sizeof(data),
320 "attempting to determine the 1.1 version");
321 if (rc)
322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700324 str += sprintf(str,
325 "TCG version: %d.%d\nFirmware version: %d.%d\n",
326 (int) data[14], (int) data[15], (int) data[16],
327 (int) data[17]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700329out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return str - buf;
331}
Kylene Hall6659ca22005-06-23 22:02:00 -0700332EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Kylene Hall6659ca22005-06-23 22:02:00 -0700334ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 struct tpm_chip *chip = dev_get_drvdata(dev);
338 if (chip == NULL)
339 return 0;
340
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700341 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -0700342 return count;
343}
344EXPORT_SYMBOL_GPL(tpm_store_cancel);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * Device file system interface to the TPM
348 */
349int tpm_open(struct inode *inode, struct file *file)
350{
351 int rc = 0, minor = iminor(inode);
352 struct tpm_chip *chip = NULL, *pos;
353
354 spin_lock(&driver_lock);
355
356 list_for_each_entry(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700357 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 chip = pos;
359 break;
360 }
361 }
362
363 if (chip == NULL) {
364 rc = -ENODEV;
365 goto err_out;
366 }
367
368 if (chip->num_opens) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800369 dev_dbg(chip->dev, "Another process owns this TPM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 rc = -EBUSY;
371 goto err_out;
372 }
373
374 chip->num_opens++;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800375 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 spin_unlock(&driver_lock);
378
379 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
380 if (chip->data_buffer == NULL) {
381 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800382 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -ENOMEM;
384 }
385
386 atomic_set(&chip->data_pending, 0);
387
388 file->private_data = chip;
389 return 0;
390
391err_out:
392 spin_unlock(&driver_lock);
393 return rc;
394}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395EXPORT_SYMBOL_GPL(tpm_open);
396
397int tpm_release(struct inode *inode, struct file *file)
398{
399 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 spin_lock(&driver_lock);
Kylene Hall5e976d52005-06-23 22:02:03 -0700402 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 chip->num_opens--;
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700404 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -0800405 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 atomic_set(&chip->data_pending, 0);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800407 put_device(chip->dev);
Kylene Hall5e976d52005-06-23 22:02:03 -0700408 kfree(chip->data_buffer);
409 spin_unlock(&driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412EXPORT_SYMBOL_GPL(tpm_release);
413
Andrew Mortonb888c872005-10-30 15:03:28 -0800414ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700415 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct tpm_chip *chip = file->private_data;
418 int in_size = size, out_size;
419
420 /* cannot perform a write until the read has cleared
421 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700422 while (atomic_read(&chip->data_pending) != 0)
423 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 down(&chip->buffer_mutex);
426
427 if (in_size > TPM_BUFSIZE)
428 in_size = TPM_BUFSIZE;
429
430 if (copy_from_user
431 (chip->data_buffer, (void __user *) buf, in_size)) {
432 up(&chip->buffer_mutex);
433 return -EFAULT;
434 }
435
436 /* atomic tpm command send and result receive */
437 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
438
439 atomic_set(&chip->data_pending, out_size);
440 up(&chip->buffer_mutex);
441
442 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700443 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return in_size;
446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447EXPORT_SYMBOL_GPL(tpm_write);
448
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700449ssize_t tpm_read(struct file *file, char __user *buf,
450 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct tpm_chip *chip = file->private_data;
Kylene Hall5b44bd52005-06-23 22:01:53 -0700453 int ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Kylene Hall5b44bd52005-06-23 22:01:53 -0700455 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -0800456 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -0700457 ret_size = atomic_read(&chip->data_pending);
458 atomic_set(&chip->data_pending, 0);
459 if (ret_size > 0) { /* relay data */
460 if (size < ret_size)
461 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 down(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -0800464 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -0700465 ret_size = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 up(&chip->buffer_mutex);
467 }
468
469 return ret_size;
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471EXPORT_SYMBOL_GPL(tpm_read);
472
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800473void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800475 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800478 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return;
480 }
481
482 spin_lock(&driver_lock);
483
484 list_del(&chip->list);
485
486 spin_unlock(&driver_lock);
487
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800488 dev_set_drvdata(dev, NULL);
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700489 misc_deregister(&chip->vendor.miscdev);
490 kfree(chip->vendor.miscdev.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700492 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800493 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700495 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES] &=
496 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 kfree(chip);
499
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800500 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800502EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504static u8 savestate[] = {
505 0, 193, /* TPM_TAG_RQU_COMMAND */
506 0, 0, 0, 10, /* blob length (in bytes) */
507 0, 0, 0, 152 /* TPM_ORD_SaveState */
508};
509
510/*
511 * We are about to suspend. Save the TPM state
512 * so that it can be restored.
513 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800514int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800516 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (chip == NULL)
518 return -ENODEV;
519
520 tpm_transmit(chip, savestate, sizeof(savestate));
521 return 0;
522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523EXPORT_SYMBOL_GPL(tpm_pm_suspend);
524
525/*
526 * Resume from a power safe. The BIOS already restored
527 * the TPM state.
528 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800529int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800531 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (chip == NULL)
534 return -ENODEV;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538EXPORT_SYMBOL_GPL(tpm_pm_resume);
539
540/*
541 * Called from tpm_<specific>.c probe function only for devices
542 * the driver has determined it should claim. Prior to calling
543 * this function the specific probe function has called pci_enable_device
544 * upon errant exit from this function specific probe function should call
545 * pci_disable_device
546 */
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700547struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
548 *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700550#define DEVNAME_SIZE 7
551
552 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct tpm_chip *chip;
554 int i, j;
555
556 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -0800557 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (chip == NULL)
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 init_MUTEX(&chip->buffer_mutex);
562 init_MUTEX(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 INIT_LIST_HEAD(&chip->list);
564
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800565 INIT_WORK(&chip->work, timeout_work, chip);
566
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700567 init_timer(&chip->user_read_timer);
568 chip->user_read_timer.function = user_reader_timeout;
569 chip->user_read_timer.data = (unsigned long) chip;
570
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700571 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 chip->dev_num = -1;
574
Kylene Hall3122a882005-06-23 22:01:48 -0700575 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
576 for (j = 0; j < 8 * sizeof(int); j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if ((dev_mask[i] & (1 << j)) == 0) {
Kylene Hall3122a882005-06-23 22:01:48 -0700578 chip->dev_num =
579 i * TPM_NUM_MASK_ENTRIES + j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 dev_mask[i] |= 1 << j;
581 goto dev_num_search_complete;
582 }
583
584dev_num_search_complete:
585 if (chip->dev_num < 0) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800586 dev_err(dev, "No available tpm device numbers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 kfree(chip);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700590 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700592 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700594 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
595 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700596 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700598 chip->vendor.miscdev.dev = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800599 chip->dev = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700601 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800602 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700604 chip->vendor.miscdev.name,
605 chip->vendor.miscdev.minor);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800606 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 kfree(chip);
608 dev_mask[i] &= !(1 << j);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700609 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Kylene Hall5e976d52005-06-23 22:02:03 -0700612 spin_lock(&driver_lock);
613
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800614 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 list_add(&chip->list, &tpm_chip_list);
617
Kylene Hall5e976d52005-06-23 22:02:03 -0700618 spin_unlock(&driver_lock);
619
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700620 sysfs_create_group(&dev->kobj, chip->vendor.attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800622 chip->bios_dir = tpm_bios_log_setup(devname);
623
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700624 return chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626EXPORT_SYMBOL_GPL(tpm_register_hardware);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
629MODULE_DESCRIPTION("TPM Driver");
630MODULE_VERSION("2.0");
631MODULE_LICENSE("GPL");