blob: 1a53da99b58fc8af06640dc9fe2a9333dae9c689 [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
46 down(&chip->buffer_mutex);
47 atomic_set(&chip->data_pending, 0);
48 memset(chip->data_buffer, 0, TPM_BUFSIZE);
49 up(&chip->buffer_mutex);
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Internal kernel interface to transmit TPM commands
54 */
55static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
56 size_t bufsiz)
57{
Kylene Halld9e5b6b2005-06-23 22:02:02 -070058 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 u32 count;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070060 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Kylene Hall81179bb2005-06-23 22:01:59 -070062 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (count == 0)
65 return -ENODATA;
66 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080067 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -070068 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -E2BIG;
70 }
71
72 down(&chip->tpm_mutex);
73
Kylene Halld9e5b6b2005-06-23 22:02:02 -070074 if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080075 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -070076 "tpm_transmit: tpm_send: error %zd\n", rc);
77 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070080 stop = jiffies + 2 * 60 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 do {
Kylene Jo Hallb4ed3e32005-10-30 15:03:23 -080082 u8 status = chip->vendor->status(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if ((status & chip->vendor->req_complete_mask) ==
84 chip->vendor->req_complete_val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto out_recv;
86 }
Kylene Halld9e5b6b2005-06-23 22:02:02 -070087
88 if ((status == chip->vendor->req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -080089 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -070090 rc = -ECANCELED;
91 goto out;
92 }
93
94 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -070096 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98
99 chip->vendor->cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800100 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700101 rc = -ETIME;
102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104out_recv:
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700105 rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
106 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800107 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700108 "tpm_transmit: tpm_recv: error %zd\n", rc);
109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 up(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700111 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114#define TPM_DIGEST_SIZE 20
115#define CAP_PCR_RESULT_SIZE 18
Kylene Halldff37e42005-06-23 22:01:50 -0700116static const u8 cap_pcr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 0, 193, /* TPM_TAG_RQU_COMMAND */
118 0, 0, 0, 22, /* length */
119 0, 0, 0, 101, /* TPM_ORD_GetCapability */
120 0, 0, 0, 5,
121 0, 0, 0, 4,
122 0, 0, 1, 1
123};
124
125#define READ_PCR_RESULT_SIZE 30
Kylene Halldff37e42005-06-23 22:01:50 -0700126static const u8 pcrread[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 0, 193, /* TPM_TAG_RQU_COMMAND */
128 0, 0, 0, 14, /* length */
129 0, 0, 0, 21, /* TPM_ORD_PcrRead */
130 0, 0, 0, 0 /* PCR index */
131};
132
Kylene Hall6659ca22005-06-23 22:02:00 -0700133ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
134 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 u8 data[READ_PCR_RESULT_SIZE];
137 ssize_t len;
Kylene Hall81179bb2005-06-23 22:01:59 -0700138 int i, j, num_pcrs;
139 __be32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 char *str = buf;
141
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800142 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (chip == NULL)
144 return -ENODEV;
145
146 memcpy(data, cap_pcr, sizeof(cap_pcr));
147 if ((len = tpm_transmit(chip, data, sizeof(data)))
Kylene Halle234bc92005-06-23 22:02:08 -0700148 < CAP_PCR_RESULT_SIZE) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800149 dev_dbg(chip->dev, "A TPM error (%d) occurred "
Kylene Halle234bc92005-06-23 22:02:08 -0700150 "attempting to determine the number of PCRS\n",
151 be32_to_cpu(*((__be32 *) (data + 6))));
152 return 0;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Kylene Hall81179bb2005-06-23 22:01:59 -0700155 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 for (i = 0; i < num_pcrs; i++) {
158 memcpy(data, pcrread, sizeof(pcrread));
159 index = cpu_to_be32(i);
160 memcpy(data + 10, &index, 4);
161 if ((len = tpm_transmit(chip, data, sizeof(data)))
Kylene Halle234bc92005-06-23 22:02:08 -0700162 < READ_PCR_RESULT_SIZE){
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800163 dev_dbg(chip->dev, "A TPM error (%d) occurred"
Kylene Halle234bc92005-06-23 22:02:08 -0700164 " attempting to read PCR %d of %d\n",
Andrew Mortonb888c872005-10-30 15:03:28 -0800165 be32_to_cpu(*((__be32 *) (data + 6))),
166 i, num_pcrs);
Kylene Halle234bc92005-06-23 22:02:08 -0700167 goto out;
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 str += sprintf(str, "PCR-%02d: ", i);
170 for (j = 0; j < TPM_DIGEST_SIZE; j++)
171 str += sprintf(str, "%02X ", *(data + 10 + j));
172 str += sprintf(str, "\n");
173 }
Kylene Halle234bc92005-06-23 22:02:08 -0700174out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return str - buf;
176}
Kylene Hall6659ca22005-06-23 22:02:00 -0700177EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#define READ_PUBEK_RESULT_SIZE 314
Kylene Halldff37e42005-06-23 22:01:50 -0700180static const u8 readpubek[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 0, 193, /* TPM_TAG_RQU_COMMAND */
182 0, 0, 0, 30, /* length */
183 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
184};
185
Kylene Hall6659ca22005-06-23 22:02:00 -0700186ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
187 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Kylene Hall2df71112005-06-23 22:01:54 -0700189 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ssize_t len;
Kylene Hall81179bb2005-06-23 22:01:59 -0700191 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 char *str = buf;
193
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800194 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (chip == NULL)
196 return -ENODEV;
197
Andrew Mortonb888c872005-10-30 15:03:28 -0800198 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
Kylene Hall2df71112005-06-23 22:01:54 -0700199 if (!data)
200 return -ENOMEM;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 memcpy(data, readpubek, sizeof(readpubek));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Kylene Hall2df71112005-06-23 22:01:54 -0700204 if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
205 READ_PUBEK_RESULT_SIZE) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800206 dev_dbg(chip->dev, "A TPM error (%d) occurred "
Kylene Halle234bc92005-06-23 22:02:08 -0700207 "attempting to read the PUBEK\n",
208 be32_to_cpu(*((__be32 *) (data + 6))));
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700209 rc = 0;
210 goto out;
Kylene Hall2df71112005-06-23 22:01:54 -0700211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /*
214 ignore header 10 bytes
215 algorithm 32 bits (1 == RSA )
216 encscheme 16 bits
217 sigscheme 16 bits
218 parameters (RSA 12->bytes: keybit, #primes, expbit)
219 keylenbytes 32 bits
220 256 byte modulus
221 ignore checksum 20 bytes
222 */
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 str +=
225 sprintf(str,
226 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
227 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
228 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
229 "Modulus length: %d\nModulus: \n",
230 data[10], data[11], data[12], data[13], data[14],
231 data[15], data[16], data[17], data[22], data[23],
232 data[24], data[25], data[26], data[27], data[28],
233 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700234 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700237 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if ((i + 1) % 16 == 0)
239 str += sprintf(str, "\n");
240 }
Kylene Hall2df71112005-06-23 22:01:54 -0700241 rc = str - buf;
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700242out:
Kylene Hall2df71112005-06-23 22:01:54 -0700243 kfree(data);
244 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Kylene Hall6659ca22005-06-23 22:02:00 -0700246EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248#define CAP_VER_RESULT_SIZE 18
Kylene Halldff37e42005-06-23 22:01:50 -0700249static const u8 cap_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 0, 193, /* TPM_TAG_RQU_COMMAND */
251 0, 0, 0, 18, /* length */
252 0, 0, 0, 101, /* TPM_ORD_GetCapability */
253 0, 0, 0, 6,
254 0, 0, 0, 0
255};
256
257#define CAP_MANUFACTURER_RESULT_SIZE 18
Kylene Halldff37e42005-06-23 22:01:50 -0700258static const u8 cap_manufacturer[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 0, 193, /* TPM_TAG_RQU_COMMAND */
260 0, 0, 0, 22, /* length */
261 0, 0, 0, 101, /* TPM_ORD_GetCapability */
262 0, 0, 0, 5,
263 0, 0, 0, 4,
264 0, 0, 1, 3
265};
266
Kylene Hall6659ca22005-06-23 22:02:00 -0700267ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
268 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Kylene Hall2df71112005-06-23 22:01:54 -0700270 u8 data[sizeof(cap_manufacturer)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ssize_t len;
272 char *str = buf;
273
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800274 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (chip == NULL)
276 return -ENODEV;
277
278 memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
279
280 if ((len = tpm_transmit(chip, data, sizeof(data))) <
281 CAP_MANUFACTURER_RESULT_SIZE)
282 return len;
283
284 str += sprintf(str, "Manufacturer: 0x%x\n",
Kylene Hall81179bb2005-06-23 22:01:59 -0700285 be32_to_cpu(*((__be32 *) (data + 14))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 memcpy(data, cap_version, sizeof(cap_version));
288
289 if ((len = tpm_transmit(chip, data, sizeof(data))) <
290 CAP_VER_RESULT_SIZE)
291 return len;
292
293 str +=
294 sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
295 (int) data[14], (int) data[15], (int) data[16],
296 (int) data[17]);
297
298 return str - buf;
299}
Kylene Hall6659ca22005-06-23 22:02:00 -0700300EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Kylene Hall6659ca22005-06-23 22:02:00 -0700302ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 struct tpm_chip *chip = dev_get_drvdata(dev);
306 if (chip == NULL)
307 return 0;
308
309 chip->vendor->cancel(chip);
310 return count;
311}
312EXPORT_SYMBOL_GPL(tpm_store_cancel);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*
315 * Device file system interface to the TPM
316 */
317int tpm_open(struct inode *inode, struct file *file)
318{
319 int rc = 0, minor = iminor(inode);
320 struct tpm_chip *chip = NULL, *pos;
321
322 spin_lock(&driver_lock);
323
324 list_for_each_entry(pos, &tpm_chip_list, list) {
325 if (pos->vendor->miscdev.minor == minor) {
326 chip = pos;
327 break;
328 }
329 }
330
331 if (chip == NULL) {
332 rc = -ENODEV;
333 goto err_out;
334 }
335
336 if (chip->num_opens) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800337 dev_dbg(chip->dev, "Another process owns this TPM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 rc = -EBUSY;
339 goto err_out;
340 }
341
342 chip->num_opens++;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800343 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 spin_unlock(&driver_lock);
346
347 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
348 if (chip->data_buffer == NULL) {
349 chip->num_opens--;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800350 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ENOMEM;
352 }
353
354 atomic_set(&chip->data_pending, 0);
355
356 file->private_data = chip;
357 return 0;
358
359err_out:
360 spin_unlock(&driver_lock);
361 return rc;
362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363EXPORT_SYMBOL_GPL(tpm_open);
364
365int tpm_release(struct inode *inode, struct file *file)
366{
367 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 spin_lock(&driver_lock);
Kylene Hall5e976d52005-06-23 22:02:03 -0700370 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 chip->num_opens--;
Kylene Hallfe3fd482005-06-23 22:01:56 -0700372 del_singleshot_timer_sync(&chip->user_read_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 atomic_set(&chip->data_pending, 0);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800374 put_device(chip->dev);
Kylene Hall5e976d52005-06-23 22:02:03 -0700375 kfree(chip->data_buffer);
376 spin_unlock(&driver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379EXPORT_SYMBOL_GPL(tpm_release);
380
Andrew Mortonb888c872005-10-30 15:03:28 -0800381ssize_t tpm_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 size_t size, loff_t * off)
383{
384 struct tpm_chip *chip = file->private_data;
385 int in_size = size, out_size;
386
387 /* cannot perform a write until the read has cleared
388 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700389 while (atomic_read(&chip->data_pending) != 0)
390 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 down(&chip->buffer_mutex);
393
394 if (in_size > TPM_BUFSIZE)
395 in_size = TPM_BUFSIZE;
396
397 if (copy_from_user
398 (chip->data_buffer, (void __user *) buf, in_size)) {
399 up(&chip->buffer_mutex);
400 return -EFAULT;
401 }
402
403 /* atomic tpm command send and result receive */
404 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
405
406 atomic_set(&chip->data_pending, out_size);
407 up(&chip->buffer_mutex);
408
409 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd482005-06-23 22:01:56 -0700410 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return in_size;
413}
414
415EXPORT_SYMBOL_GPL(tpm_write);
416
Andrew Mortonb888c872005-10-30 15:03:28 -0800417ssize_t tpm_read(struct file * file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 size_t size, loff_t * off)
419{
420 struct tpm_chip *chip = file->private_data;
Kylene Hall5b44bd52005-06-23 22:01:53 -0700421 int ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Kylene Hall5b44bd52005-06-23 22:01:53 -0700423 del_singleshot_timer_sync(&chip->user_read_timer);
424 ret_size = atomic_read(&chip->data_pending);
425 atomic_set(&chip->data_pending, 0);
426 if (ret_size > 0) { /* relay data */
427 if (size < ret_size)
428 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 down(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -0800431 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -0700432 ret_size = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 up(&chip->buffer_mutex);
434 }
435
436 return ret_size;
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438EXPORT_SYMBOL_GPL(tpm_read);
439
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800440void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800442 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800445 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return;
447 }
448
449 spin_lock(&driver_lock);
450
451 list_del(&chip->list);
452
453 spin_unlock(&driver_lock);
454
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800455 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 misc_deregister(&chip->vendor->miscdev);
Kylene Jo Hall9a936eb2005-06-30 02:58:50 -0700457 kfree(chip->vendor->miscdev.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800459 sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Andrew Mortonb888c872005-10-30 15:03:28 -0800461 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
Kylene Jo Hallf6a23822005-11-13 16:07:42 -0800462 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 kfree(chip);
465
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800466 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800468EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470static u8 savestate[] = {
471 0, 193, /* TPM_TAG_RQU_COMMAND */
472 0, 0, 0, 10, /* blob length (in bytes) */
473 0, 0, 0, 152 /* TPM_ORD_SaveState */
474};
475
476/*
477 * We are about to suspend. Save the TPM state
478 * so that it can be restored.
479 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800480int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800482 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (chip == NULL)
484 return -ENODEV;
485
486 tpm_transmit(chip, savestate, sizeof(savestate));
487 return 0;
488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489EXPORT_SYMBOL_GPL(tpm_pm_suspend);
490
491/*
492 * Resume from a power safe. The BIOS already restored
493 * the TPM state.
494 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800495int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -0800497 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (chip == NULL)
500 return -ENODEV;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504EXPORT_SYMBOL_GPL(tpm_pm_resume);
505
506/*
507 * Called from tpm_<specific>.c probe function only for devices
508 * the driver has determined it should claim. Prior to calling
509 * this function the specific probe function has called pci_enable_device
510 * upon errant exit from this function specific probe function should call
511 * pci_disable_device
512 */
Andrew Mortonb888c872005-10-30 15:03:28 -0800513int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700515#define DEVNAME_SIZE 7
516
517 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct tpm_chip *chip;
519 int i, j;
520
521 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -0800522 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (chip == NULL)
524 return -ENOMEM;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 init_MUTEX(&chip->buffer_mutex);
527 init_MUTEX(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 INIT_LIST_HEAD(&chip->list);
529
Kylene Hallfe3fd482005-06-23 22:01:56 -0700530 init_timer(&chip->user_read_timer);
531 chip->user_read_timer.function = user_reader_timeout;
532 chip->user_read_timer.data = (unsigned long) chip;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 chip->vendor = entry;
535
536 chip->dev_num = -1;
537
Kylene Hall3122a882005-06-23 22:01:48 -0700538 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
539 for (j = 0; j < 8 * sizeof(int); j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if ((dev_mask[i] & (1 << j)) == 0) {
Kylene Hall3122a882005-06-23 22:01:48 -0700541 chip->dev_num =
542 i * TPM_NUM_MASK_ENTRIES + j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dev_mask[i] |= 1 << j;
544 goto dev_num_search_complete;
545 }
546
547dev_num_search_complete:
548 if (chip->dev_num < 0) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800549 dev_err(dev, "No available tpm device numbers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 kfree(chip);
551 return -ENODEV;
552 } else if (chip->dev_num == 0)
553 chip->vendor->miscdev.minor = TPM_MINOR;
554 else
555 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
556
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -0700557 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
558 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 chip->vendor->miscdev.name = devname;
560
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800561 chip->vendor->miscdev.dev = dev;
562 chip->dev = get_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (misc_register(&chip->vendor->miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800565 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 "unable to misc_register %s, minor %d\n",
567 chip->vendor->miscdev.name,
568 chip->vendor->miscdev.minor);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800569 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 kfree(chip);
571 dev_mask[i] &= !(1 << j);
572 return -ENODEV;
573 }
574
Kylene Hall5e976d52005-06-23 22:02:03 -0700575 spin_lock(&driver_lock);
576
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800577 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 list_add(&chip->list, &tpm_chip_list);
580
Kylene Hall5e976d52005-06-23 22:02:03 -0700581 spin_unlock(&driver_lock);
582
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800583 sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return 0;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587EXPORT_SYMBOL_GPL(tpm_register_hardware);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
590MODULE_DESCRIPTION("TPM Driver");
591MODULE_VERSION("2.0");
592MODULE_LICENSE("GPL");