tpm.c 14.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2004 IBM Corporation
 *
 * Authors:
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
 *
 * Device driver for TCG/TCPA TPM (trusted platform module).
 * Specifications at www.trustedcomputinggroup.org	 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 * 
 * Note, the TPM chip is not interrupt driven (only polling)
 * and can have very long timeouts (minutes!). Hence the unusual
22
 * calls to msleep.
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30
 *
 */

#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include "tpm.h"

31 32 33 34 35 36
enum tpm_const {
	TPM_MINOR = 224,	/* officially assigned */
	TPM_BUFSIZE = 2048,
	TPM_NUM_DEVICES = 256,
	TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
};
L
Linus Torvalds 已提交
37 38 39

static LIST_HEAD(tpm_chip_list);
static DEFINE_SPINLOCK(driver_lock);
40
static int dev_mask[TPM_NUM_MASK_ENTRIES];
L
Linus Torvalds 已提交
41 42 43 44 45

static void user_reader_timeout(unsigned long ptr)
{
	struct tpm_chip *chip = (struct tpm_chip *) ptr;

K
Kylene Jo Hall 已提交
46 47 48 49 50 51 52
	schedule_work(&chip->work);
}

static void timeout_work(void * ptr)
{
	struct tpm_chip *chip = ptr;

L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64
	down(&chip->buffer_mutex);
	atomic_set(&chip->data_pending, 0);
	memset(chip->data_buffer, 0, TPM_BUFSIZE);
	up(&chip->buffer_mutex);
}

/*
 * Internal kernel interface to transmit TPM commands
 */
static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
			    size_t bufsiz)
{
K
Kylene Hall 已提交
65
	ssize_t rc;
L
Linus Torvalds 已提交
66
	u32 count;
67
	unsigned long stop;
L
Linus Torvalds 已提交
68

69
	count = be32_to_cpu(*((__be32 *) (buf + 2)));
L
Linus Torvalds 已提交
70 71 72 73

	if (count == 0)
		return -ENODATA;
	if (count > bufsiz) {
74
		dev_err(chip->dev,
A
Al Viro 已提交
75
			"invalid count value %x %zx \n", count, bufsiz);
L
Linus Torvalds 已提交
76 77 78 79 80
		return -E2BIG;
	}

	down(&chip->tpm_mutex);

K
Kylene Hall 已提交
81
	if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
82
		dev_err(chip->dev,
K
Kylene Hall 已提交
83 84
			"tpm_transmit: tpm_send: error %zd\n", rc);
		goto out;
L
Linus Torvalds 已提交
85 86
	}

87
	stop = jiffies + 2 * 60 * HZ;
L
Linus Torvalds 已提交
88
	do {
89
		u8 status = chip->vendor->status(chip);
L
Linus Torvalds 已提交
90 91 92 93
		if ((status & chip->vendor->req_complete_mask) ==
		    chip->vendor->req_complete_val) {
			goto out_recv;
		}
K
Kylene Hall 已提交
94 95

		if ((status == chip->vendor->req_canceled)) {
96
			dev_err(chip->dev, "Operation Canceled\n");
K
Kylene Hall 已提交
97 98 99 100 101
			rc = -ECANCELED;
			goto out;
		}

		msleep(TPM_TIMEOUT);	/* CHECK */
L
Linus Torvalds 已提交
102
		rmb();
103
	} while (time_before(jiffies, stop));
L
Linus Torvalds 已提交
104 105 106


	chip->vendor->cancel(chip);
107
	dev_err(chip->dev, "Operation Timed out\n");
K
Kylene Hall 已提交
108 109
	rc = -ETIME;
	goto out;
L
Linus Torvalds 已提交
110 111

out_recv:
K
Kylene Hall 已提交
112 113
	rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
	if (rc < 0)
114
		dev_err(chip->dev,
K
Kylene Hall 已提交
115 116
			"tpm_transmit: tpm_recv: error %zd\n", rc);
out:
L
Linus Torvalds 已提交
117
	up(&chip->tpm_mutex);
K
Kylene Hall 已提交
118
	return rc;
L
Linus Torvalds 已提交
119 120 121 122
}

#define TPM_DIGEST_SIZE 20
#define CAP_PCR_RESULT_SIZE 18
123
static const u8 cap_pcr[] = {
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 22,		/* length */
	0, 0, 0, 101,		/* TPM_ORD_GetCapability */
	0, 0, 0, 5,
	0, 0, 0, 4,
	0, 0, 1, 1
};

#define READ_PCR_RESULT_SIZE 30
133
static const u8 pcrread[] = {
L
Linus Torvalds 已提交
134 135 136 137 138 139
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 14,		/* length */
	0, 0, 0, 21,		/* TPM_ORD_PcrRead */
	0, 0, 0, 0		/* PCR index */
};

140 141
ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
		      char *buf)
L
Linus Torvalds 已提交
142 143 144
{
	u8 data[READ_PCR_RESULT_SIZE];
	ssize_t len;
145 146
	int i, j, num_pcrs;
	__be32 index;
L
Linus Torvalds 已提交
147 148
	char *str = buf;

149
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
150 151 152 153 154
	if (chip == NULL)
		return -ENODEV;

	memcpy(data, cap_pcr, sizeof(cap_pcr));
	if ((len = tpm_transmit(chip, data, sizeof(data)))
K
Kylene Hall 已提交
155
	    < CAP_PCR_RESULT_SIZE) {
156
		dev_dbg(chip->dev, "A TPM error (%d) occurred "
K
Kylene Hall 已提交
157 158 159 160
				"attempting to determine the number of PCRS\n",
			be32_to_cpu(*((__be32 *) (data + 6))));
		return 0;
	}
L
Linus Torvalds 已提交
161

162
	num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
L
Linus Torvalds 已提交
163 164 165 166 167 168

	for (i = 0; i < num_pcrs; i++) {
		memcpy(data, pcrread, sizeof(pcrread));
		index = cpu_to_be32(i);
		memcpy(data + 10, &index, 4);
		if ((len = tpm_transmit(chip, data, sizeof(data)))
K
Kylene Hall 已提交
169
		    < READ_PCR_RESULT_SIZE){
170
			dev_dbg(chip->dev, "A TPM error (%d) occurred"
K
Kylene Hall 已提交
171
				" attempting to read PCR %d of %d\n",
A
Andrew Morton 已提交
172 173
				be32_to_cpu(*((__be32 *) (data + 6))),
				i, num_pcrs);
K
Kylene Hall 已提交
174 175
			goto out;
		}
L
Linus Torvalds 已提交
176 177 178 179 180
		str += sprintf(str, "PCR-%02d: ", i);
		for (j = 0; j < TPM_DIGEST_SIZE; j++)
			str += sprintf(str, "%02X ", *(data + 10 + j));
		str += sprintf(str, "\n");
	}
K
Kylene Hall 已提交
181
out:
L
Linus Torvalds 已提交
182 183
	return str - buf;
}
184
EXPORT_SYMBOL_GPL(tpm_show_pcrs);
L
Linus Torvalds 已提交
185 186

#define  READ_PUBEK_RESULT_SIZE 314
187
static const u8 readpubek[] = {
L
Linus Torvalds 已提交
188 189 190 191 192
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 30,		/* length */
	0, 0, 0, 124,		/* TPM_ORD_ReadPubek */
};

193 194
ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
		       char *buf)
L
Linus Torvalds 已提交
195
{
K
Kylene Hall 已提交
196
	u8 *data;
L
Linus Torvalds 已提交
197
	ssize_t len;
198
	int i, rc;
L
Linus Torvalds 已提交
199 200
	char *str = buf;

201
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
202 203 204
	if (chip == NULL)
		return -ENODEV;

A
Andrew Morton 已提交
205
	data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
K
Kylene Hall 已提交
206 207 208
	if (!data)
		return -ENOMEM;

L
Linus Torvalds 已提交
209 210
	memcpy(data, readpubek, sizeof(readpubek));

K
Kylene Hall 已提交
211 212
	if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
	    READ_PUBEK_RESULT_SIZE) {
213
		dev_dbg(chip->dev, "A TPM error (%d) occurred "
K
Kylene Hall 已提交
214 215
				"attempting to read the PUBEK\n",
			    be32_to_cpu(*((__be32 *) (data + 6))));
216 217
		rc = 0;
		goto out;
K
Kylene Hall 已提交
218
	}
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	/* 
	   ignore header 10 bytes
	   algorithm 32 bits (1 == RSA )
	   encscheme 16 bits
	   sigscheme 16 bits
	   parameters (RSA 12->bytes: keybit, #primes, expbit)  
	   keylenbytes 32 bits
	   256 byte modulus
	   ignore checksum 20 bytes
	 */

	str +=
	    sprintf(str,
		    "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
		    "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
		    " %02X %02X %02X %02X %02X %02X %02X %02X\n"
		    "Modulus length: %d\nModulus: \n",
		    data[10], data[11], data[12], data[13], data[14],
		    data[15], data[16], data[17], data[22], data[23],
		    data[24], data[25], data[26], data[27], data[28],
		    data[29], data[30], data[31], data[32], data[33],
K
Kylene Jo Hall 已提交
241
		    be32_to_cpu(*((__be32 *) (data + 34))));
L
Linus Torvalds 已提交
242 243

	for (i = 0; i < 256; i++) {
K
Kylene Jo Hall 已提交
244
		str += sprintf(str, "%02X ", data[i + 38]);
L
Linus Torvalds 已提交
245 246 247
		if ((i + 1) % 16 == 0)
			str += sprintf(str, "\n");
	}
K
Kylene Hall 已提交
248
	rc = str - buf;
249
out:
K
Kylene Hall 已提交
250 251
	kfree(data);
	return rc;
L
Linus Torvalds 已提交
252
}
253
EXPORT_SYMBOL_GPL(tpm_show_pubek);
L
Linus Torvalds 已提交
254 255

#define CAP_VER_RESULT_SIZE 18
256
static const u8 cap_version[] = {
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 18,		/* length */
	0, 0, 0, 101,		/* TPM_ORD_GetCapability */
	0, 0, 0, 6,
	0, 0, 0, 0
};

#define CAP_MANUFACTURER_RESULT_SIZE 18
265
static const u8 cap_manufacturer[] = {
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 22,		/* length */
	0, 0, 0, 101,		/* TPM_ORD_GetCapability */
	0, 0, 0, 5,
	0, 0, 0, 4,
	0, 0, 1, 3
};

274 275
ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
		      char *buf)
L
Linus Torvalds 已提交
276
{
K
Kylene Hall 已提交
277
	u8 data[sizeof(cap_manufacturer)];
L
Linus Torvalds 已提交
278 279 280
	ssize_t len;
	char *str = buf;

281
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291
	if (chip == NULL)
		return -ENODEV;

	memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));

	if ((len = tpm_transmit(chip, data, sizeof(data))) <
	    CAP_MANUFACTURER_RESULT_SIZE)
		return len;

	str += sprintf(str, "Manufacturer: 0x%x\n",
292
		       be32_to_cpu(*((__be32 *) (data + 14))));
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306

	memcpy(data, cap_version, sizeof(cap_version));

	if ((len = tpm_transmit(chip, data, sizeof(data))) <
	    CAP_VER_RESULT_SIZE)
		return len;

	str +=
	    sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
		    (int) data[14], (int) data[15], (int) data[16],
		    (int) data[17]);

	return str - buf;
}
307 308 309 310 311 312 313 314 315 316 317 318 319
EXPORT_SYMBOL_GPL(tpm_show_caps);

ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct tpm_chip *chip = dev_get_drvdata(dev);
	if (chip == NULL)
		return 0;

	chip->vendor->cancel(chip);
	return count;
}
EXPORT_SYMBOL_GPL(tpm_store_cancel);
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

/*
 * Device file system interface to the TPM
 */
int tpm_open(struct inode *inode, struct file *file)
{
	int rc = 0, minor = iminor(inode);
	struct tpm_chip *chip = NULL, *pos;

	spin_lock(&driver_lock);

	list_for_each_entry(pos, &tpm_chip_list, list) {
		if (pos->vendor->miscdev.minor == minor) {
			chip = pos;
			break;
		}
	}

	if (chip == NULL) {
		rc = -ENODEV;
		goto err_out;
	}

	if (chip->num_opens) {
A
Andrew Morton 已提交
344
		dev_dbg(chip->dev, "Another process owns this TPM\n");
L
Linus Torvalds 已提交
345 346 347 348 349
		rc = -EBUSY;
		goto err_out;
	}

	chip->num_opens++;
350
	get_device(chip->dev);
L
Linus Torvalds 已提交
351 352 353 354 355 356

	spin_unlock(&driver_lock);

	chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
	if (chip->data_buffer == NULL) {
		chip->num_opens--;
357
		put_device(chip->dev);
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		return -ENOMEM;
	}

	atomic_set(&chip->data_pending, 0);

	file->private_data = chip;
	return 0;

err_out:
	spin_unlock(&driver_lock);
	return rc;
}
EXPORT_SYMBOL_GPL(tpm_open);

int tpm_release(struct inode *inode, struct file *file)
{
	struct tpm_chip *chip = file->private_data;

	spin_lock(&driver_lock);
K
Kylene Hall 已提交
377
	file->private_data = NULL;
L
Linus Torvalds 已提交
378
	chip->num_opens--;
379
	del_singleshot_timer_sync(&chip->user_read_timer);
380
	flush_scheduled_work();
L
Linus Torvalds 已提交
381
	atomic_set(&chip->data_pending, 0);
382
	put_device(chip->dev);
K
Kylene Hall 已提交
383 384
	kfree(chip->data_buffer);
	spin_unlock(&driver_lock);
L
Linus Torvalds 已提交
385 386 387 388
	return 0;
}
EXPORT_SYMBOL_GPL(tpm_release);

A
Andrew Morton 已提交
389
ssize_t tpm_write(struct file *file, const char __user *buf,
L
Linus Torvalds 已提交
390 391 392 393 394 395 396
		  size_t size, loff_t * off)
{
	struct tpm_chip *chip = file->private_data;
	int in_size = size, out_size;

	/* cannot perform a write until the read has cleared
	   either via tpm_read or a user_read_timer timeout */
397 398
	while (atomic_read(&chip->data_pending) != 0)
		msleep(TPM_TIMEOUT);
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	down(&chip->buffer_mutex);

	if (in_size > TPM_BUFSIZE)
		in_size = TPM_BUFSIZE;

	if (copy_from_user
	    (chip->data_buffer, (void __user *) buf, in_size)) {
		up(&chip->buffer_mutex);
		return -EFAULT;
	}

	/* atomic tpm command send and result receive */
	out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);

	atomic_set(&chip->data_pending, out_size);
	up(&chip->buffer_mutex);

	/* Set a timeout by which the reader must come claim the result */
418
	mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
L
Linus Torvalds 已提交
419 420 421 422 423 424

	return in_size;
}

EXPORT_SYMBOL_GPL(tpm_write);

A
Andrew Morton 已提交
425
ssize_t tpm_read(struct file * file, char __user *buf,
L
Linus Torvalds 已提交
426 427 428
		 size_t size, loff_t * off)
{
	struct tpm_chip *chip = file->private_data;
429
	int ret_size;
L
Linus Torvalds 已提交
430

431
	del_singleshot_timer_sync(&chip->user_read_timer);
432
	flush_scheduled_work();
433 434 435 436 437
	ret_size = atomic_read(&chip->data_pending);
	atomic_set(&chip->data_pending, 0);
	if (ret_size > 0) {	/* relay data */
		if (size < ret_size)
			ret_size = size;
L
Linus Torvalds 已提交
438 439

		down(&chip->buffer_mutex);
440
		if (copy_to_user(buf, chip->data_buffer, ret_size))
441
			ret_size = -EFAULT;
L
Linus Torvalds 已提交
442 443 444 445 446 447 448
		up(&chip->buffer_mutex);
	}

	return ret_size;
}
EXPORT_SYMBOL_GPL(tpm_read);

449
void tpm_remove_hardware(struct device *dev)
L
Linus Torvalds 已提交
450
{
451
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
452 453

	if (chip == NULL) {
454
		dev_err(dev, "No device data found\n");
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462 463
		return;
	}

	spin_lock(&driver_lock);

	list_del(&chip->list);

	spin_unlock(&driver_lock);

464
	dev_set_drvdata(dev, NULL);
L
Linus Torvalds 已提交
465
	misc_deregister(&chip->vendor->miscdev);
466
	kfree(chip->vendor->miscdev.name);
L
Linus Torvalds 已提交
467

468
	sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
469
	tpm_bios_log_teardown(chip->bios_dir);
L
Linus Torvalds 已提交
470

A
Andrew Morton 已提交
471
	dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &=
472
		~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
L
Linus Torvalds 已提交
473 474 475

	kfree(chip);

476
	put_device(dev);
L
Linus Torvalds 已提交
477
}
478
EXPORT_SYMBOL_GPL(tpm_remove_hardware);
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489

static u8 savestate[] = {
	0, 193,			/* TPM_TAG_RQU_COMMAND */
	0, 0, 0, 10,		/* blob length (in bytes) */
	0, 0, 0, 152		/* TPM_ORD_SaveState */
};

/*
 * We are about to suspend. Save the TPM state
 * so that it can be restored.
 */
490
int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
L
Linus Torvalds 已提交
491
{
492
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502 503 504
	if (chip == NULL)
		return -ENODEV;

	tpm_transmit(chip, savestate, sizeof(savestate));
	return 0;
}
EXPORT_SYMBOL_GPL(tpm_pm_suspend);

/*
 * Resume from a power safe. The BIOS already restored
 * the TPM state.
 */
505
int tpm_pm_resume(struct device *dev)
L
Linus Torvalds 已提交
506
{
507
	struct tpm_chip *chip = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	if (chip == NULL)
		return -ENODEV;

	return 0;
}
EXPORT_SYMBOL_GPL(tpm_pm_resume);

/*
 * Called from tpm_<specific>.c probe function only for devices 
 * the driver has determined it should claim.  Prior to calling
 * this function the specific probe function has called pci_enable_device
 * upon errant exit from this function specific probe function should call
 * pci_disable_device
 */
A
Andrew Morton 已提交
523
int tpm_register_hardware(struct device *dev, struct tpm_vendor_specific *entry)
L
Linus Torvalds 已提交
524
{
525 526 527
#define DEVNAME_SIZE 7

	char *devname;
L
Linus Torvalds 已提交
528 529 530 531
	struct tpm_chip *chip;
	int i, j;

	/* Driver specific per-device data */
A
Andrew Morton 已提交
532
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
533 534 535 536 537 538 539
	if (chip == NULL)
		return -ENOMEM;

	init_MUTEX(&chip->buffer_mutex);
	init_MUTEX(&chip->tpm_mutex);
	INIT_LIST_HEAD(&chip->list);

K
Kylene Jo Hall 已提交
540 541
	INIT_WORK(&chip->work, timeout_work, chip);

542 543 544 545
	init_timer(&chip->user_read_timer);
	chip->user_read_timer.function = user_reader_timeout;
	chip->user_read_timer.data = (unsigned long) chip;

L
Linus Torvalds 已提交
546 547 548 549
	chip->vendor = entry;

	chip->dev_num = -1;

550 551
	for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
		for (j = 0; j < 8 * sizeof(int); j++)
L
Linus Torvalds 已提交
552
			if ((dev_mask[i] & (1 << j)) == 0) {
553 554
				chip->dev_num =
				    i * TPM_NUM_MASK_ENTRIES + j;
L
Linus Torvalds 已提交
555 556 557 558 559 560
				dev_mask[i] |= 1 << j;
				goto dev_num_search_complete;
			}

dev_num_search_complete:
	if (chip->dev_num < 0) {
A
Andrew Morton 已提交
561
		dev_err(dev, "No available tpm device numbers\n");
L
Linus Torvalds 已提交
562 563 564 565 566 567 568
		kfree(chip);
		return -ENODEV;
	} else if (chip->dev_num == 0)
		chip->vendor->miscdev.minor = TPM_MINOR;
	else
		chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;

569 570
	devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
	scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
L
Linus Torvalds 已提交
571 572
	chip->vendor->miscdev.name = devname;

573 574
	chip->vendor->miscdev.dev = dev;
	chip->dev = get_device(dev);
L
Linus Torvalds 已提交
575 576

	if (misc_register(&chip->vendor->miscdev)) {
577
		dev_err(chip->dev,
L
Linus Torvalds 已提交
578 579 580
			"unable to misc_register %s, minor %d\n",
			chip->vendor->miscdev.name,
			chip->vendor->miscdev.minor);
581
		put_device(dev);
L
Linus Torvalds 已提交
582 583 584 585 586
		kfree(chip);
		dev_mask[i] &= !(1 << j);
		return -ENODEV;
	}

K
Kylene Hall 已提交
587 588
	spin_lock(&driver_lock);

589
	dev_set_drvdata(dev, chip);
L
Linus Torvalds 已提交
590 591 592

	list_add(&chip->list, &tpm_chip_list);

K
Kylene Hall 已提交
593 594
	spin_unlock(&driver_lock);

595
	sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
L
Linus Torvalds 已提交
596

597 598
	chip->bios_dir = tpm_bios_log_setup(devname);

L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606
	return 0;
}
EXPORT_SYMBOL_GPL(tpm_register_hardware);

MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
MODULE_DESCRIPTION("TPM Driver");
MODULE_VERSION("2.0");
MODULE_LICENSE("GPL");