Home | History | Annotate | Line # | Download | only in ic
tpm.c revision 1.17
      1 /*	$NetBSD: tpm.c,v 1.17 2021/01/04 18:22:19 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2008, 2009 Michael Shalayeff
     34  * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
     35  * All rights reserved.
     36  *
     37  * Permission to use, copy, modify, and distribute this software for any
     38  * purpose with or without fee is hereby granted, provided that the above
     39  * copyright notice and this permission notice appear in all copies.
     40  *
     41  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     42  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     43  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     44  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     45  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
     46  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     47  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.17 2021/01/04 18:22:19 riastradh Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/types.h>
     55 
     56 #include <sys/bus.h>
     57 #include <sys/conf.h>
     58 #include <sys/device.h>
     59 #include <sys/kernel.h>
     60 #include <sys/malloc.h>
     61 #include <sys/pmf.h>
     62 #include <sys/proc.h>
     63 #include <sys/systm.h>
     64 
     65 #include <dev/ic/tpmreg.h>
     66 #include <dev/ic/tpmvar.h>
     67 
     68 #include "ioconf.h"
     69 
     70 CTASSERT(sizeof(struct tpm_header) == 10);
     71 
     72 #define TPM_BUFSIZ	1024
     73 
     74 #define TPM_PARAM_SIZE	0x0001	/* that's a flag */
     75 
     76 /* Timeouts. */
     77 #define TPM_ACCESS_TMO	2000	/* 2sec */
     78 #define TPM_READY_TMO	2000	/* 2sec */
     79 #define TPM_READ_TMO	2000	/* 2sec */
     80 #define TPM_BURST_TMO	2000	/* 2sec */
     81 
     82 #define TPM_CAPS_REQUIRED \
     83 	(TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
     84 	 TPM_INTF_INT_LEVEL_LOW)
     85 
     86 static inline int
     87 tpm_tmotohz(int tmo)
     88 {
     89 	struct timeval tv;
     90 
     91 	tv.tv_sec = tmo / 1000;
     92 	tv.tv_usec = 1000 * (tmo % 1000);
     93 
     94 	return tvtohz(&tv);
     95 }
     96 
     97 static int
     98 tpm_getburst(struct tpm_softc *sc)
     99 {
    100 	int burst, to, rv;
    101 
    102 	to = tpm_tmotohz(TPM_BURST_TMO);
    103 
    104 	while (to--) {
    105 		/*
    106 		 * Burst count is in bits 23:8, so read the two higher bytes.
    107 		 */
    108 		burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
    109 		burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
    110 		    << 8;
    111 
    112 		if (burst)
    113 			return burst;
    114 
    115 		rv = tsleep(sc, PCATCH, "tpm_getburst", 1);
    116 		if (rv && rv != EWOULDBLOCK) {
    117 			return 0;
    118 		}
    119 	}
    120 
    121 	return 0;
    122 }
    123 
    124 static inline uint8_t
    125 tpm_status(struct tpm_softc *sc)
    126 {
    127 	return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
    128 	    TPM_STS_STATUS_BITS;
    129 }
    130 
    131 /* -------------------------------------------------------------------------- */
    132 
    133 static bool
    134 tpm12_suspend(struct tpm_softc *sc)
    135 {
    136 	static const uint8_t command[10] = {
    137 		0x00, 0xC1,		/* TPM_TAG_RQU_COMMAND */
    138 		0x00, 0x00, 0x00, 10,	/* Length in bytes */
    139 		0x00, 0x00, 0x00, 0x98	/* TPM_ORD_SaveState */
    140 	};
    141 	struct tpm_header response;
    142 
    143 	if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
    144 		return false;
    145 	if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
    146 		return false;
    147 	if (TPM_BE32(response.code) != 0)
    148 		return false;
    149 
    150 	return true;
    151 }
    152 
    153 static bool
    154 tpm20_suspend(struct tpm_softc *sc)
    155 {
    156 	static const uint8_t command[12] = {
    157 		0x80, 0x01,		/* TPM_ST_NO_SESSIONS */
    158 		0x00, 0x00, 0x00, 12,	/* Length in bytes */
    159 		0x00, 0x00, 0x01, 0x45,	/* TPM_CC_Shutdown */
    160 		0x00, 0x01		/* TPM_SU_STATE */
    161 	};
    162 	struct tpm_header response;
    163 
    164 	if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
    165 		return false;
    166 	if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
    167 		return false;
    168 	if (TPM_BE32(response.code) != 0)
    169 		return false;
    170 
    171 	return true;
    172 }
    173 
    174 bool
    175 tpm_suspend(device_t dev, const pmf_qual_t *qual)
    176 {
    177 	struct tpm_softc *sc = device_private(dev);
    178 
    179 	switch (sc->sc_ver) {
    180 	case TPM_1_2:
    181 		return tpm12_suspend(sc);
    182 	case TPM_2_0:
    183 		return tpm20_suspend(sc);
    184 	default:
    185 		panic("%s: impossible", __func__);
    186 	}
    187 }
    188 
    189 bool
    190 tpm_resume(device_t dev, const pmf_qual_t *qual)
    191 {
    192 	/*
    193 	 * Don't do anything, the BIOS is supposed to restore the previously
    194 	 * saved state.
    195 	 */
    196 	return true;
    197 }
    198 
    199 /* -------------------------------------------------------------------------- */
    200 
    201 static int
    202 tpm_poll(struct tpm_softc *sc, uint8_t mask, int to, wchan_t chan)
    203 {
    204 	int rv;
    205 
    206 	while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
    207 		rv = tsleep(chan, PCATCH, "tpm_poll", 1);
    208 		if (rv && rv != EWOULDBLOCK) {
    209 			return rv;
    210 		}
    211 	}
    212 
    213 	return 0;
    214 }
    215 
    216 static int
    217 tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
    218 {
    219 	int retry, to, rv;
    220 	uint8_t todo;
    221 
    222 	to = tpm_tmotohz(tmo);
    223 	retry = 3;
    224 
    225 restart:
    226 	todo = bits;
    227 
    228 	/*
    229 	 * TPM_STS_VALID has priority over the others.
    230 	 */
    231 	if (todo & TPM_STS_VALID) {
    232 		if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
    233 			return rv;
    234 		todo &= ~TPM_STS_VALID;
    235 	}
    236 
    237 	if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
    238 		return rv;
    239 
    240 	if ((todo & sc->sc_status) != todo) {
    241 		if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
    242 			bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    243 			    TPM_STS_RESP_RETRY);
    244 			goto restart;
    245 		}
    246 		return EIO;
    247 	}
    248 
    249 	return 0;
    250 }
    251 
    252 /* -------------------------------------------------------------------------- */
    253 
    254 /*
    255  * TPM using the TIS 1.2 interface.
    256  */
    257 
    258 static int
    259 tpm12_request_locality(struct tpm_softc *sc, int l)
    260 {
    261 	uint32_t r;
    262 	int to, rv;
    263 
    264 	if (l != 0)
    265 		return EINVAL;
    266 
    267 	if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
    268 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
    269 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
    270 		return 0;
    271 
    272 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
    273 	    TPM_ACCESS_REQUEST_USE);
    274 
    275 	to = tpm_tmotohz(TPM_ACCESS_TMO);
    276 
    277 	while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
    278 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    279 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
    280 		rv = tsleep(sc->sc_intf->init, PCATCH, "tpm_locality", 1);
    281 		if (rv && rv != EWOULDBLOCK) {
    282 			return rv;
    283 		}
    284 	}
    285 
    286 	if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    287 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
    288 		return EBUSY;
    289 	}
    290 
    291 	return 0;
    292 }
    293 
    294 static int
    295 tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
    296 {
    297 	uint32_t cap;
    298 	uint8_t reg;
    299 	int tmo;
    300 
    301 	cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
    302 	if (cap == 0xffffffff)
    303 		return EINVAL;
    304 	if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
    305 		return ENOTSUP;
    306 
    307 	/* Request locality 0. */
    308 	bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
    309 
    310 	/* Wait for it to become active. */
    311 	tmo = TPM_ACCESS_TMO; /* Milliseconds. */
    312 	while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
    313 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    314 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
    315 		DELAY(1000); /* 1 millisecond. */
    316 	}
    317 	if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    318 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
    319 		return ETIMEDOUT;
    320 	}
    321 
    322 	if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
    323 		return EINVAL;
    324 
    325 	return 0;
    326 }
    327 
    328 static int
    329 tpm_tis12_init(struct tpm_softc *sc)
    330 {
    331 	int rv;
    332 
    333 	sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
    334 	    TPM_INTF_CAPABILITY);
    335 	sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
    336 	sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
    337 
    338 	aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
    339 	    sc->sc_devid, sc->sc_rev);
    340 
    341 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    342 		return rv;
    343 
    344 	/* Abort whatever it thought it was doing. */
    345 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
    346 
    347 	return 0;
    348 }
    349 
    350 static int
    351 tpm_tis12_start(struct tpm_softc *sc, int rw)
    352 {
    353 	int rv;
    354 
    355 	if (rw == UIO_READ) {
    356 		rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
    357 		    TPM_READ_TMO, sc->sc_intf->read);
    358 		return rv;
    359 	}
    360 
    361 	/* Request the 0th locality. */
    362 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    363 		return rv;
    364 
    365 	sc->sc_status = tpm_status(sc);
    366 	if (sc->sc_status & TPM_STS_CMD_READY)
    367 		return 0;
    368 
    369 	/* Abort previous and restart. */
    370 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
    371 	rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
    372 	if (rv)
    373 		return rv;
    374 
    375 	return 0;
    376 }
    377 
    378 static int
    379 tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
    380     int flags)
    381 {
    382 	uint8_t *p = buf;
    383 	size_t cnt;
    384 	int rv, n;
    385 
    386 	cnt = 0;
    387 	while (len > 0) {
    388 		rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
    389 		    TPM_READ_TMO, sc->sc_intf->read);
    390 		if (rv)
    391 			return rv;
    392 
    393 		n = MIN(len, tpm_getburst(sc));
    394 		while (n > 0) {
    395 			*p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
    396 			cnt++;
    397 			len--;
    398 			n--;
    399 		}
    400 
    401 		if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
    402 			break;
    403 	}
    404 
    405 	if (count)
    406 		*count = cnt;
    407 
    408 	return 0;
    409 }
    410 
    411 static int
    412 tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
    413 {
    414 	const uint8_t *p = buf;
    415 	size_t cnt;
    416 	int rv, r;
    417 
    418 	if (len == 0)
    419 		return 0;
    420 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    421 		return rv;
    422 
    423 	cnt = 0;
    424 	while (cnt < len - 1) {
    425 		for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
    426 			bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
    427 			cnt++;
    428 		}
    429 		if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
    430 			return rv;
    431 		}
    432 		sc->sc_status = tpm_status(sc);
    433 		if (!(sc->sc_status & TPM_STS_DATA_EXPECT)) {
    434 			return EIO;
    435 		}
    436 	}
    437 
    438 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
    439 	cnt++;
    440 
    441 	if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
    442 		return rv;
    443 	}
    444 	if ((sc->sc_status & TPM_STS_DATA_EXPECT) != 0) {
    445 		return EIO;
    446 	}
    447 
    448 	return 0;
    449 }
    450 
    451 static int
    452 tpm_tis12_end(struct tpm_softc *sc, int rw, int err)
    453 {
    454 	int rv = 0;
    455 
    456 	if (rw == UIO_READ) {
    457 		rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_intf->read);
    458 		if (rv)
    459 			return rv;
    460 
    461 		/* Still more data? */
    462 		sc->sc_status = tpm_status(sc);
    463 		if (!err && (sc->sc_status & TPM_STS_DATA_AVAIL)) {
    464 			rv = EIO;
    465 		}
    466 
    467 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    468 		    TPM_STS_CMD_READY);
    469 
    470 		/* Release the 0th locality. */
    471 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
    472 		    TPM_ACCESS_ACTIVE_LOCALITY);
    473 	} else {
    474 		/* Hungry for more? */
    475 		sc->sc_status = tpm_status(sc);
    476 		if (!err && (sc->sc_status & TPM_STS_DATA_EXPECT)) {
    477 			rv = EIO;
    478 		}
    479 
    480 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    481 		    err ? TPM_STS_CMD_READY : TPM_STS_GO);
    482 	}
    483 
    484 	return rv;
    485 }
    486 
    487 const struct tpm_intf tpm_intf_tis12 = {
    488 	.version = TIS_1_2,
    489 	.probe = tpm_tis12_probe,
    490 	.init = tpm_tis12_init,
    491 	.start = tpm_tis12_start,
    492 	.read = tpm_tis12_read,
    493 	.write = tpm_tis12_write,
    494 	.end = tpm_tis12_end
    495 };
    496 
    497 /* -------------------------------------------------------------------------- */
    498 
    499 static dev_type_open(tpmopen);
    500 static dev_type_close(tpmclose);
    501 static dev_type_read(tpmread);
    502 static dev_type_write(tpmwrite);
    503 static dev_type_ioctl(tpmioctl);
    504 
    505 const struct cdevsw tpm_cdevsw = {
    506 	.d_open = tpmopen,
    507 	.d_close = tpmclose,
    508 	.d_read = tpmread,
    509 	.d_write = tpmwrite,
    510 	.d_ioctl = tpmioctl,
    511 	.d_stop = nostop,
    512 	.d_tty = notty,
    513 	.d_poll = nopoll,
    514 	.d_mmap = nommap,
    515 	.d_kqfilter = nokqfilter,
    516 	.d_discard = nodiscard,
    517 	.d_flag = D_OTHER | D_MPSAFE,
    518 };
    519 
    520 static int
    521 tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
    522 {
    523 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
    524 	int ret = 0;
    525 
    526 	if (sc == NULL)
    527 		return ENXIO;
    528 
    529 	mutex_enter(&sc->sc_lock);
    530 	if (sc->sc_busy) {
    531 		ret = EBUSY;
    532 	} else {
    533 		sc->sc_busy = true;
    534 	}
    535 	mutex_exit(&sc->sc_lock);
    536 
    537 	return ret;
    538 }
    539 
    540 static int
    541 tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
    542 {
    543 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
    544 	int ret = 0;
    545 
    546 	if (sc == NULL)
    547 		return ENXIO;
    548 
    549 	mutex_enter(&sc->sc_lock);
    550 	if (!sc->sc_busy) {
    551 		ret = EINVAL;
    552 	} else {
    553 		sc->sc_busy = false;
    554 	}
    555 	mutex_exit(&sc->sc_lock);
    556 
    557 	return ret;
    558 }
    559 
    560 static int
    561 tpmread(dev_t dev, struct uio *uio, int flags)
    562 {
    563 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
    564 	struct tpm_header hdr;
    565 	uint8_t buf[TPM_BUFSIZ];
    566 	size_t cnt, len, n;
    567 	int rv;
    568 
    569 	if (sc == NULL)
    570 		return ENXIO;
    571 
    572 	if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)))
    573 		return rv;
    574 
    575 	/* Get the header. */
    576 	if ((rv = (*sc->sc_intf->read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
    577 		goto out;
    578 	}
    579 	len = TPM_BE32(hdr.length);
    580 	if (len > uio->uio_resid || len < cnt) {
    581 		rv = EIO;
    582 		goto out;
    583 	}
    584 
    585 	/* Copy out the header. */
    586 	if ((rv = uiomove(&hdr, cnt, uio))) {
    587 		goto out;
    588 	}
    589 
    590 	/* Process the rest. */
    591 	len -= cnt;
    592 	while (len > 0) {
    593 		n = MIN(sizeof(buf), len);
    594 		if ((rv = (*sc->sc_intf->read)(sc, buf, n, NULL, TPM_PARAM_SIZE))) {
    595 			goto out;
    596 		}
    597 		if ((rv = uiomove(buf, n, uio))) {
    598 			goto out;
    599 		}
    600 		len -= n;
    601 	}
    602 
    603 out:
    604 	rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
    605 	return rv;
    606 }
    607 
    608 static int
    609 tpmwrite(dev_t dev, struct uio *uio, int flags)
    610 {
    611 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
    612 	uint8_t buf[TPM_BUFSIZ];
    613 	int n, rv;
    614 
    615 	if (sc == NULL)
    616 		return ENXIO;
    617 
    618 	n = MIN(sizeof(buf), uio->uio_resid);
    619 	if ((rv = uiomove(buf, n, uio))) {
    620 		goto out;
    621 	}
    622 	if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE))) {
    623 		goto out;
    624 	}
    625 	if ((rv = (*sc->sc_intf->write)(sc, buf, n))) {
    626 		goto out;
    627 	}
    628 
    629 	rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
    630 out:
    631 	return rv;
    632 }
    633 
    634 static int
    635 tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    636 {
    637 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
    638 	struct tpm_ioc_getinfo *info;
    639 
    640 	if (sc == NULL)
    641 		return ENXIO;
    642 
    643 	switch (cmd) {
    644 	case TPM_IOC_GETINFO:
    645 		info = addr;
    646 		info->api_version = TPM_API_VERSION;
    647 		info->tpm_version = sc->sc_ver;
    648 		info->itf_version = sc->sc_intf->version;
    649 		info->device_id = sc->sc_devid;
    650 		info->device_rev = sc->sc_rev;
    651 		info->device_caps = sc->sc_caps;
    652 		return 0;
    653 	default:
    654 		break;
    655 	}
    656 
    657 	return ENOTTY;
    658 }
    659