Linux Audio

Check our new training course

Open-source upstreaming

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