blob: 187bcdaf7bf646b625809ab44819abc20bed9a57 [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 Halld9e5b6b2005-06-23 22:02:02 -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 Hallb4ed3e32005-10-30 15:03:23 -080089 u8 status = chip->vendor->status(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if ((status & chip->vendor->req_complete_mask) ==
91 chip->vendor->req_complete_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 goto out_recv;
93 }
Kylene Halld9e5b6b2005-06-23 22:02:02 -070094
95 if ((status == chip->vendor->req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080096 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -070097 rc = -ECANCELED;
98 goto out;
99 }
100
101 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700103 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105
106 chip->vendor->cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800107 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700108 rc = -ETIME;
109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111out_recv:
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700112 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
113 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800114 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700115 "tpm_transmit: tpm_recv: error %zd\n", rc);
116out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 up(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700118 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700122#define TPM_ERROR_SIZE 10
123#define TPM_RET_CODE_IDX 6
124#define TPM_GET_CAP_RET_SIZE_IDX 10
125#define TPM_GET_CAP_RET_UINT32_1_IDX 14
126#define TPM_GET_CAP_RET_UINT32_2_IDX 18
127#define TPM_GET_CAP_RET_UINT32_3_IDX 22
128#define TPM_GET_CAP_RET_UINT32_4_IDX 26
129
130#define TPM_CAP_IDX 13
131#define TPM_CAP_SUBCAP_IDX 21
132
133enum tpm_capabilities {
134 TPM_CAP_PROP = 5,
135};
136
137enum tpm_sub_capabilities {
138 TPM_CAP_PROP_PCR = 0x1,
139 TPM_CAP_PROP_MANUFACTURER = 0x3,
140};
141
142/*
143 * This is a semi generic GetCapability command for use
144 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
145 * and their associated sub_capabilities.
146 */
147
148static const u8 tpm_cap[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 0, 193, /* TPM_TAG_RQU_COMMAND */
150 0, 0, 0, 22, /* length */
151 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700152 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
153 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
154 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700157static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
158 char *desc)
159{
160 int err;
161
162 len = tpm_transmit(chip, data, len);
163 if (len < 0)
164 return len;
165 if (len == TPM_ERROR_SIZE) {
166 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
167 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
168 return err;
169 }
170 return 0;
171}
172
Kylene Halldff37e42005-06-23 22:01:50 -0700173static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 0, 193, /* TPM_TAG_RQU_COMMAND */
175 0, 0, 0, 14, /* length */
176 0, 0, 0, 21, /* TPM_ORD_PcrRead */
177 0, 0, 0, 0 /* PCR index */
178};
179
Kylene Hall6659ca22005-06-23 22:02:00 -0700180ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
181 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700183 u8 data[30];
184 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700185 int i, j, num_pcrs;
186 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 char *str = buf;
188
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800189 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (chip == NULL)
191 return -ENODEV;
192
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700193 memcpy(data, tpm_cap, sizeof(tpm_cap));
194 data[TPM_CAP_IDX] = TPM_CAP_PROP;
195 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
196
197 rc = transmit_cmd(chip, data, sizeof(data),
198 "attempting to determine the number of PCRS");
199 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Kylene Hall81179bb2005-06-23 22:01:59 -0700202 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (i = 0; i < num_pcrs; i++) {
204 memcpy(data, pcrread, sizeof(pcrread));
205 index = cpu_to_be32(i);
206 memcpy(data + 10, &index, 4);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700207 rc = transmit_cmd(chip, data, sizeof(data),
208 "attempting to read a PCR");
209 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700210 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 str += sprintf(str, "PCR-%02d: ", i);
212 for (j = 0; j < TPM_DIGEST_SIZE; j++)
213 str += sprintf(str, "%02X ", *(data + 10 + j));
214 str += sprintf(str, "\n");
215 }
Kylene Halle234bc92005-06-23 22:02:08 -0700216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return str - buf;
218}
Kylene Hall6659ca22005-06-23 22:02:00 -0700219EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700222static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 0, 193, /* TPM_TAG_RQU_COMMAND */
224 0, 0, 0, 30, /* length */
225 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
226};
227
Kylene Hall6659ca22005-06-23 22:02:00 -0700228ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
229 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Kylene Hall2df71112005-06-23 22:01:54 -0700231 u8 *data;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700232 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700233 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 char *str = buf;
235
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800236 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (chip == NULL)
238 return -ENODEV;
239
Andrew Mortonb888c872005-10-30 15:03:28 -0800240 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700241 if (!data)
242 return -ENOMEM;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700246 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
247 "attempting to read the PUBEK");
248 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /*
252 ignore header 10 bytes
253 algorithm 32 bits (1 == RSA )
254 encscheme 16 bits
255 sigscheme 16 bits
256 parameters (RSA 12->bytes: keybit, #primes, expbit)
257 keylenbytes 32 bits
258 256 byte modulus
259 ignore checksum 20 bytes
260 */
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 str +=
263 sprintf(str,
264 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
265 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
266 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
267 "Modulus length: %d\nModulus: \n",
268 data[10], data[11], data[12], data[13], data[14],
269 data[15], data[16], data[17], data[22], data[23],
270 data[24], data[25], data[26], data[27], data[28],
271 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700272 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700275 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if ((i + 1) % 16 == 0)
277 str += sprintf(str, "\n");
278 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700279out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700280 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700281 kfree(data);
282 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Kylene Hall6659ca22005-06-23 22:02:00 -0700284EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700286#define CAP_VERSION_1_1 6
287#define CAP_VERSION_IDX 13
Kylene Halldff37e42005-06-23 22:01:50 -0700288static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 0, 193, /* TPM_TAG_RQU_COMMAND */
290 0, 0, 0, 18, /* length */
291 0, 0, 0, 101, /* TPM_ORD_GetCapability */
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700292 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 0, 0, 0, 0
294};
295
Kylene Hall6659ca22005-06-23 22:02:00 -0700296ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
297 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700299 u8 data[30];
300 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 char *str = buf;
302
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800303 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (chip == NULL)
305 return -ENODEV;
306
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700307 memcpy(data, tpm_cap, sizeof(tpm_cap));
308 data[TPM_CAP_IDX] = TPM_CAP_PROP;
309 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700311 rc = transmit_cmd(chip, data, sizeof(data),
312 "attempting to determine the manufacturer");
313 if (rc)
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700317 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 memcpy(data, cap_version, sizeof(cap_version));
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700320 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
321 rc = transmit_cmd(chip, data, sizeof(data),
322 "attempting to determine the 1.1 version");
323 if (rc)
324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700326 str += sprintf(str,
327 "TCG version: %d.%d\nFirmware version: %d.%d\n",
328 (int) data[14], (int) data[15], (int) data[16],
329 (int) data[17]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700331out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return str - buf;
333}
Kylene Hall6659ca22005-06-23 22:02:00 -0700334EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Kylene Hall6659ca22005-06-23 22:02:00 -0700336ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
338{
339 struct tpm_chip *chip = dev_get_drvdata(dev);
340 if (chip == NULL)
341 return 0;
342
343 chip->vendor->cancel(chip);
344 return count;
345}
346EXPORT_SYMBOL_GPL(tpm_store_cancel);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/*
349 * Device file system interface to the TPM
350 */
351int tpm_open(struct inode *inode, struct file *file)
352{
353 int rc = 0, minor = iminor(inode);
354 struct tpm_chip *chip = NULL, *pos;
355
356 spin_lock(&driver_lock);
357
358 list_for_each_entry(pos, &tpm_chip_list, list) {
359 if (pos->vendor->miscdev.minor == minor) {
360 chip = pos;
361 break;
362 }
363 }
364
365 if (chip == NULL) {
366 rc = -ENODEV;
367 goto err_out;
368 }
369
370 if (chip->num_opens) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800371 dev_dbg(chip->dev, "Another process owns this TPM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 rc = -EBUSY;
373 goto err_out;
374 }
375
376 chip->num_opens++;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800377 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 spin_unlock(&driver_lock);
380
381 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
382 if (chip->data_buffer == NULL) {
383 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800384 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -ENOMEM;
386 }
387
388 atomic_set(&chip->data_pending, 0);
389
390 file->private_data = chip;
391 return 0;
392
393err_out:
394 spin_unlock(&driver_lock);
395 return rc;
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397EXPORT_SYMBOL_GPL(tpm_open);
398
399int tpm_release(struct inode *inode, struct file *file)
400{
401 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 spin_lock(&driver_lock);
Kylene Hall5e976d52005-06-23 22:02:03 -0700404 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 chip->num_opens--;
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700406 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -0800407 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 atomic_set(&chip->data_pending, 0);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800409 put_device(chip->dev);
Kylene Hall5e976d52005-06-23 22:02:03 -0700410 kfree(chip->data_buffer);
411 spin_unlock(&driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414EXPORT_SYMBOL_GPL(tpm_release);
415
Andrew Mortonb888c872005-10-30 15:03:28 -0800416ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700417 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct tpm_chip *chip = file->private_data;
420 int in_size = size, out_size;
421
422 /* cannot perform a write until the read has cleared
423 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700424 while (atomic_read(&chip->data_pending) != 0)
425 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 down(&chip->buffer_mutex);
428
429 if (in_size > TPM_BUFSIZE)
430 in_size = TPM_BUFSIZE;
431
432 if (copy_from_user
433 (chip->data_buffer, (void __user *) buf, in_size)) {
434 up(&chip->buffer_mutex);
435 return -EFAULT;
436 }
437
438 /* atomic tpm command send and result receive */
439 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
440
441 atomic_set(&chip->data_pending, out_size);
442 up(&chip->buffer_mutex);
443
444 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700445 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return in_size;
448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449EXPORT_SYMBOL_GPL(tpm_write);
450
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700451ssize_t tpm_read(struct file *file, char __user *buf,
452 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct tpm_chip *chip = file->private_data;
Kylene Hall5b44bd52005-06-23 22:01:53 -0700455 int ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Kylene Hall5b44bd52005-06-23 22:01:53 -0700457 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -0800458 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -0700459 ret_size = atomic_read(&chip->data_pending);
460 atomic_set(&chip->data_pending, 0);
461 if (ret_size > 0) { /* relay data */
462 if (size < ret_size)
463 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 down(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -0800466 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -0700467 ret_size = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 up(&chip->buffer_mutex);
469 }
470
471 return ret_size;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473EXPORT_SYMBOL_GPL(tpm_read);
474
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800475void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800477 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800480 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return;
482 }
483
484 spin_lock(&driver_lock);
485
486 list_del(&chip->list);
487
488 spin_unlock(&driver_lock);
489
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800490 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 misc_deregister(&chip->vendor->miscdev);
Kylene Jo Hall9a936eb2005-06-30 02:58:50 -0700492 kfree(chip->vendor->miscdev.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800494 sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800495 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Andrew Mortonb888c872005-10-30 15:03:28 -0800497 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
Kylene Jo Hallf6a23822005-11-13 16:07:42 -0800498 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 kfree(chip);
501
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800502 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800504EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506static u8 savestate[] = {
507 0, 193, /* TPM_TAG_RQU_COMMAND */
508 0, 0, 0, 10, /* blob length (in bytes) */
509 0, 0, 0, 152 /* TPM_ORD_SaveState */
510};
511
512/*
513 * We are about to suspend. Save the TPM state
514 * so that it can be restored.
515 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800516int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800518 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (chip == NULL)
520 return -ENODEV;
521
522 tpm_transmit(chip, savestate, sizeof(savestate));
523 return 0;
524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525EXPORT_SYMBOL_GPL(tpm_pm_suspend);
526
527/*
528 * Resume from a power safe. The BIOS already restored
529 * the TPM state.
530 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800531int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800533 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (chip == NULL)
536 return -ENODEV;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540EXPORT_SYMBOL_GPL(tpm_pm_resume);
541
542/*
543 * Called from tpm_<specific>.c probe function only for devices
544 * the driver has determined it should claim. Prior to calling
545 * this function the specific probe function has called pci_enable_device
546 * upon errant exit from this function specific probe function should call
547 * pci_disable_device
548 */
Andrew Mortonb888c872005-10-30 15:03:28 -0800549int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700551#define DEVNAME_SIZE 7
552
553 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct tpm_chip *chip;
555 int i, j;
556
557 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -0800558 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (chip == NULL)
560 return -ENOMEM;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 init_MUTEX(&chip->buffer_mutex);
563 init_MUTEX(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 INIT_LIST_HEAD(&chip->list);
565
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800566 INIT_WORK(&chip->work, timeout_work, chip);
567
Kylene Hallfe3fd4832005-06-23 22:01:56 -0700568 init_timer(&chip->user_read_timer);
569 chip->user_read_timer.function = user_reader_timeout;
570 chip->user_read_timer.data = (unsigned long) chip;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 chip->vendor = entry;
573
574 chip->dev_num = -1;
575
Kylene Hall3122a882005-06-23 22:01:48 -0700576 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
577 for (j = 0; j < 8 * sizeof(int); j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if ((dev_mask[i] & (1 << j)) == 0) {
Kylene Hall3122a882005-06-23 22:01:48 -0700579 chip->dev_num =
580 i * TPM_NUM_MASK_ENTRIES + j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dev_mask[i] |= 1 << j;
582 goto dev_num_search_complete;
583 }
584
585dev_num_search_complete:
586 if (chip->dev_num < 0) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800587 dev_err(dev, "No available tpm device numbers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 kfree(chip);
589 return -ENODEV;
590 } else if (chip->dev_num == 0)
591 chip->vendor->miscdev.minor = TPM_MINOR;
592 else
593 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
594
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700595 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
596 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 chip->vendor->miscdev.name = devname;
598
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800599 chip->vendor->miscdev.dev = dev;
600 chip->dev = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (misc_register(&chip->vendor->miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800603 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 "unable to misc_register %s, minor %d\n",
605 chip->vendor->miscdev.name,
606 chip->vendor->miscdev.minor);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800607 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 kfree(chip);
609 dev_mask[i] &= !(1 << j);
610 return -ENODEV;
611 }
612
Kylene Hall5e976d52005-06-23 22:02:03 -0700613 spin_lock(&driver_lock);
614
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800615 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 list_add(&chip->list, &tpm_chip_list);
618
Kylene Hall5e976d52005-06-23 22:02:03 -0700619 spin_unlock(&driver_lock);
620
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800621 sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -0800623 chip->bios_dir = tpm_bios_log_setup(devname);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627EXPORT_SYMBOL_GPL(tpm_register_hardware);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
630MODULE_DESCRIPTION("TPM Driver");
631MODULE_VERSION("2.0");
632MODULE_LICENSE("GPL");