]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/tpm/tpm_tis_i2c.c
tpm: tpm_tis_i2c: Use a consistent tpm_tis_i2c_ prefix
[karo-tx-uboot.git] / drivers / tpm / tpm_tis_i2c.c
1 /*
2  * Copyright (C) 2011 Infineon Technologies
3  *
4  * Authors:
5  * Peter Huewe <huewe.external@infineon.com>
6  *
7  * Description:
8  * Device driver for TCG/TCPA TPM (trusted platform module).
9  * Specifications at www.trustedcomputinggroup.org
10  *
11  * This device driver implements the TPM interface as defined in
12  * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
13  * Infineon I2C Protocol Stack Specification v0.20.
14  *
15  * It is based on the Linux kernel driver tpm.c from Leendert van
16  * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
17  *
18  * Version: 2.1.1
19  *
20  * SPDX-License-Identifier:     GPL-2.0
21  */
22
23 #include <common.h>
24 #include <dm.h>
25 #include <fdtdec.h>
26 #include <linux/compiler.h>
27 #include <i2c.h>
28 #include <tpm.h>
29 #include <asm-generic/errno.h>
30 #include <linux/types.h>
31 #include <linux/unaligned/be_byteshift.h>
32
33 #include "tpm_tis_i2c.h"
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 static const char * const chip_name[] = {
38         [SLB9635] = "slb9635tt",
39         [SLB9645] = "slb9645tt",
40         [UNKNOWN] = "unknown/fallback to slb9635",
41 };
42
43 static struct tpm_chip g_chip;
44
45 /*
46  * tpm_tis_i2c_read() - read from TPM register
47  * @addr: register address to read from
48  * @buffer: provided by caller
49  * @len: number of bytes to read
50  *
51  * Read len bytes from TPM register and put them into
52  * buffer (little-endian format, i.e. first byte is put into buffer[0]).
53  *
54  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
55  * values have to be swapped.
56  *
57  * Return -EIO on error, 0 on success.
58  */
59 static int tpm_tis_i2c_read(u8 addr, u8 *buffer, size_t len)
60 {
61         int rc;
62         int count;
63         uint32_t addrbuf = addr;
64
65         if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
66                 /* slb9635 protocol should work in both cases */
67                 for (count = 0; count < MAX_COUNT; count++) {
68                         rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
69                         if (rc == 0)
70                                 break;  /* Success, break to skip sleep */
71                         udelay(SLEEP_DURATION);
72                 }
73                 if (rc)
74                         return -rc;
75
76                 /* After the TPM has successfully received the register address
77                  * it needs some time, thus we're sleeping here again, before
78                  * retrieving the data
79                  */
80                 for (count = 0; count < MAX_COUNT; count++) {
81                         udelay(SLEEP_DURATION);
82                         rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
83                         if (rc == 0)
84                                 break;  /* success, break to skip sleep */
85                 }
86         } else {
87                 /*
88                  * Use a combined read for newer chips.
89                  * Unfortunately the smbus functions are not suitable due to
90                  * the 32 byte limit of the smbus.
91                  * Retries should usually not be needed, but are kept just to
92                  * be safe on the safe side.
93                  */
94                 for (count = 0; count < MAX_COUNT; count++) {
95                         rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
96                         if (rc == 0)
97                                 break;  /* break here to skip sleep */
98                         udelay(SLEEP_DURATION);
99                 }
100         }
101
102         /* Take care of 'guard time' */
103         udelay(SLEEP_DURATION);
104         if (rc)
105                 return -rc;
106
107         return 0;
108 }
109
110 static int tpm_tis_i2c_write_generic(u8 addr, u8 *buffer, size_t len,
111                                      unsigned int sleep_time, u8 max_count)
112 {
113         int rc = 0;
114         int count;
115
116         for (count = 0; count < max_count; count++) {
117                 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
118                 if (rc == 0)
119                         break;  /* Success, break to skip sleep */
120                 udelay(sleep_time);
121         }
122
123         /* take care of 'guard time' */
124         udelay(sleep_time);
125         if (rc)
126                 return -rc;
127
128         return 0;
129 }
130
131 /*
132  * tpm_tis_i2c_write() - write to TPM register
133  * @addr: register address to write to
134  * @buffer: containing data to be written
135  * @len: number of bytes to write
136  *
137  * Write len bytes from provided buffer to TPM register (little
138  * endian format, i.e. buffer[0] is written as first byte).
139  *
140  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
141  * values have to be swapped.
142  *
143  * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
144  *
145  * Return -EIO on error, 0 on success
146  */
147 static int tpm_tis_i2c_write(u8 addr, u8 *buffer, size_t len)
148 {
149         return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION,
150                         MAX_COUNT);
151 }
152
153 /*
154  * This function is needed especially for the cleanup situation after
155  * sending TPM_READY
156  */
157 static int tpm_tis_i2c_write_long(u8 addr, u8 *buffer, size_t len)
158 {
159         return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
160                         MAX_COUNT_LONG);
161 }
162
163 static int tpm_tis_i2c_check_locality(struct tpm_chip *chip, int loc)
164 {
165         const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
166         u8 buf;
167         int rc;
168
169         rc = tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1);
170         if (rc < 0)
171                 return rc;
172
173         if ((buf & mask) == mask) {
174                 chip->locality = loc;
175                 return loc;
176         }
177
178         return -1;
179 }
180
181 static void tpm_tis_i2c_release_locality(struct tpm_chip *chip, int loc,
182                                          int force)
183 {
184         const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
185         u8 buf;
186
187         if (tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1) < 0)
188                 return;
189
190         if (force || (buf & mask) == mask) {
191                 buf = TPM_ACCESS_ACTIVE_LOCALITY;
192                 tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
193         }
194 }
195
196 static int tpm_tis_i2c_request_locality(struct tpm_chip *chip, int loc)
197 {
198         unsigned long start, stop;
199         u8 buf = TPM_ACCESS_REQUEST_USE;
200         int rc;
201
202         if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
203                 return loc;  /* We already have the locality */
204
205         rc = tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
206         if (rc)
207                 return rc;
208
209         /* Wait for burstcount */
210         start = get_timer(0);
211         stop = chip->timeout_a;
212         do {
213                 if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
214                         return loc;
215                 udelay(TPM_TIMEOUT * 1000);
216         } while (get_timer(start) < stop);
217
218         return -1;
219 }
220
221 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
222 {
223         /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
224         u8 buf;
225
226         if (tpm_tis_i2c_read(TPM_STS(chip->locality), &buf, 1) < 0)
227                 return 0;
228         else
229                 return buf;
230 }
231
232 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
233 {
234         int rc;
235
236         /* This causes the current command to be aborted */
237         u8 buf = TPM_STS_COMMAND_READY;
238
239         debug("%s\n", __func__);
240         rc = tpm_tis_i2c_write_long(TPM_STS(chip->locality), &buf, 1);
241         if (rc)
242                 debug("%s: rc=%d\n", __func__, rc);
243 }
244
245 static ssize_t tpm_tis_i2c_get_burstcount(struct tpm_chip *chip)
246 {
247         unsigned long start, stop;
248         ssize_t burstcnt;
249         u8 addr, buf[3];
250
251         /* Wait for burstcount */
252         /* XXX: Which timeout value? Spec has 2 answers (c & d) */
253         start = get_timer(0);
254         stop = chip->timeout_d;
255         do {
256                 /* Note: STS is little endian */
257                 addr = TPM_STS(chip->locality) + 1;
258                 if (tpm_tis_i2c_read(addr, buf, 3) < 0)
259                         burstcnt = 0;
260                 else
261                         burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
262
263                 if (burstcnt)
264                         return burstcnt;
265                 udelay(TPM_TIMEOUT * 1000);
266         } while (get_timer(start) < stop);
267
268         return -EBUSY;
269 }
270
271 static int tpm_tis_i2c_wait_for_stat(struct tpm_chip *chip, u8 mask,
272                                      unsigned long timeout, int *status)
273 {
274         unsigned long start, stop;
275
276         /* Check current status */
277         *status = tpm_tis_i2c_status(chip);
278         if ((*status & mask) == mask)
279                 return 0;
280
281         start = get_timer(0);
282         stop = timeout;
283         do {
284                 udelay(TPM_TIMEOUT * 1000);
285                 *status = tpm_tis_i2c_status(chip);
286                 if ((*status & mask) == mask)
287                         return 0;
288         } while (get_timer(start) < stop);
289
290         return -ETIME;
291 }
292
293 static int tpm_tis_i2c_recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
294 {
295         size_t size = 0;
296         ssize_t burstcnt;
297         int rc;
298
299         while (size < count) {
300                 burstcnt = tpm_tis_i2c_get_burstcount(chip);
301
302                 /* burstcount < 0 -> tpm is busy */
303                 if (burstcnt < 0)
304                         return burstcnt;
305
306                 /* Limit received data to max left */
307                 if (burstcnt > (count - size))
308                         burstcnt = count - size;
309
310                 rc = tpm_tis_i2c_read(TPM_DATA_FIFO(chip->locality),
311                                 &(buf[size]), burstcnt);
312                 if (rc == 0)
313                         size += burstcnt;
314         }
315
316         return size;
317 }
318
319 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
320 {
321         int size = 0;
322         int expected, status;
323
324         if (count < TPM_HEADER_SIZE) {
325                 size = -EIO;
326                 goto out;
327         }
328
329         /* Read first 10 bytes, including tag, paramsize, and result */
330         size = tpm_tis_i2c_recv_data(chip, buf, TPM_HEADER_SIZE);
331         if (size < TPM_HEADER_SIZE) {
332                 error("Unable to read header\n");
333                 goto out;
334         }
335
336         expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
337         if ((size_t)expected > count) {
338                 error("Error size=%x, expected=%x, count=%x\n", size, expected,
339                       count);
340                 size = -EIO;
341                 goto out;
342         }
343
344         size += tpm_tis_i2c_recv_data(chip, &buf[TPM_HEADER_SIZE],
345                                       expected - TPM_HEADER_SIZE);
346         if (size < expected) {
347                 error("Unable to read remainder of result\n");
348                 size = -ETIME;
349                 goto out;
350         }
351
352         tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c,
353                                   &status);
354         if (status & TPM_STS_DATA_AVAIL) {  /* Retry? */
355                 error("Error left over data\n");
356                 size = -EIO;
357                 goto out;
358         }
359
360 out:
361         tpm_tis_i2c_ready(chip);
362         /*
363          * The TPM needs some time to clean up here,
364          * so we sleep rather than keeping the bus busy
365          */
366         udelay(2000);
367         tpm_tis_i2c_release_locality(chip, chip->locality, 0);
368
369         return size;
370 }
371
372 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
373 {
374         int rc, status;
375         size_t burstcnt;
376         size_t count = 0;
377         int retry = 0;
378         u8 sts = TPM_STS_GO;
379
380         debug("%s: len=%d\n", __func__, len);
381         if (len > TPM_DEV_BUFSIZE)
382                 return -E2BIG;  /* Command is too long for our tpm, sorry */
383
384         if (tpm_tis_i2c_request_locality(chip, 0) < 0)
385                 return -EBUSY;
386
387         status = tpm_tis_i2c_status(chip);
388         if ((status & TPM_STS_COMMAND_READY) == 0) {
389                 tpm_tis_i2c_ready(chip);
390                 if (tpm_tis_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
391                                               chip->timeout_b, &status) < 0) {
392                         rc = -ETIME;
393                         goto out_err;
394                 }
395         }
396
397         burstcnt = tpm_tis_i2c_get_burstcount(chip);
398
399         /* burstcount < 0 -> tpm is busy */
400         if (burstcnt < 0)
401                 return burstcnt;
402
403         while (count < len) {
404                 udelay(300);
405                 if (burstcnt > len - count)
406                         burstcnt = len - count;
407
408 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
409                 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
410                         burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
411 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
412
413                 rc = tpm_tis_i2c_write(TPM_DATA_FIFO(chip->locality),
414                                 &(buf[count]), burstcnt);
415                 if (rc == 0)
416                         count += burstcnt;
417                 else {
418                         debug("%s: error\n", __func__);
419                         if (retry++ > 10) {
420                                 rc = -EIO;
421                                 goto out_err;
422                         }
423                         rc = tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID,
424                                                        chip->timeout_c,
425                                                        &status);
426                         if (rc)
427                                 goto out_err;
428
429                         if ((status & TPM_STS_DATA_EXPECT) == 0) {
430                                 rc = -EIO;
431                                 goto out_err;
432                         }
433                 }
434         }
435
436         /* Go and do it */
437         tpm_tis_i2c_write(TPM_STS(chip->locality), &sts, 1);
438         debug("done\n");
439
440         return len;
441
442 out_err:
443         debug("%s: out_err\n", __func__);
444         tpm_tis_i2c_ready(chip);
445         /*
446          * The TPM needs some time to clean up here,
447          * so we sleep rather than keeping the bus busy
448          */
449         udelay(2000);
450         tpm_tis_i2c_release_locality(chip, chip->locality, 0);
451
452         return rc;
453 }
454
455 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
456 {
457 #if CONFIG_IS_ENABLED(OF_CONTROL)
458         const void *blob = gd->fdt_blob;
459
460         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
461                 return SLB9645;
462
463         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
464                 return SLB9635;
465 #endif
466         return UNKNOWN;
467 }
468
469 static int tpm_tis_i2c_init(struct udevice *dev)
470 {
471         struct tpm_chip *chip = &g_chip;
472         u32 vendor;
473         u32 expected_did_vid;
474
475         g_chip.dev = dev;
476         g_chip.chip_type = tpm_tis_i2c_chip_type();
477         chip->is_open = 1;
478
479         /* Disable interrupts (not supported) */
480         chip->irq = 0;
481
482         /* Default timeouts - these could move to the device tree */
483         chip->timeout_a = TIS_SHORT_TIMEOUT;
484         chip->timeout_b = TIS_LONG_TIMEOUT;
485         chip->timeout_c = TIS_SHORT_TIMEOUT;
486         chip->timeout_d = TIS_SHORT_TIMEOUT;
487         chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
488         chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
489         chip->req_canceled = TPM_STS_COMMAND_READY;
490
491         if (tpm_tis_i2c_request_locality(chip, 0) < 0)
492                 return  -ENODEV;
493
494         /* Read four bytes from DID_VID register */
495         if (tpm_tis_i2c_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
496                 tpm_tis_i2c_release_locality(chip, 0, 1);
497                 return -EIO;
498         }
499
500         if (g_chip.chip_type == SLB9635) {
501                 vendor = be32_to_cpu(vendor);
502                 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
503         } else {
504                 /* device id and byte order has changed for newer i2c tpms */
505                 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
506         }
507
508         if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
509                 error("Vendor id did not match! ID was %08x\n", vendor);
510                 return -ENODEV;
511         }
512
513         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
514               chip_name[g_chip.chip_type], vendor >> 16);
515
516         /*
517          * A timeout query to TPM can be placed here.
518          * Standard timeout values are used so far
519          */
520
521         return 0;
522 }
523
524 /* Returns max number of milliseconds to wait */
525 static unsigned long tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip *chip,
526                                                        u32 ordinal)
527 {
528         int duration_idx = TPM_UNDEFINED;
529         int duration = 0;
530
531         if (ordinal < TPM_MAX_ORDINAL) {
532                 duration_idx = tpm_ordinal_duration[ordinal];
533         } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
534                         TPM_MAX_PROTECTED_ORDINAL) {
535                 duration_idx = tpm_protected_ordinal_duration[
536                                 ordinal & TPM_PROTECTED_ORDINAL_MASK];
537         }
538
539         if (duration_idx != TPM_UNDEFINED)
540                 duration = chip->duration[duration_idx];
541
542         if (duration <= 0)
543                 return 2 * 60 * HZ; /* Two minutes timeout */
544         else
545                 return duration;
546 }
547
548 static ssize_t tpm_tis_i2c_transmit(const unsigned char *buf, size_t bufsiz)
549 {
550         int rc;
551         u32 count, ordinal;
552         unsigned long start, stop;
553
554         struct tpm_chip *chip = &g_chip;
555
556         /* switch endianess: big->little */
557         count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
558         ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
559
560         if (count == 0) {
561                 error("no data\n");
562                 return -ENODATA;
563         }
564         if (count > bufsiz) {
565                 error("invalid count value %x %zx\n", count, bufsiz);
566                 return -E2BIG;
567         }
568
569         debug("Calling send\n");
570         rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
571         debug("   ... done calling send\n");
572         if (rc < 0) {
573                 error("tpm_transmit: tpm_send: error %d\n", rc);
574                 goto out;
575         }
576
577         if (chip->irq)
578                 goto out_recv;
579
580         start = get_timer(0);
581         stop = tpm_tis_i2c_calc_ordinal_duration(chip, ordinal);
582         do {
583                 debug("waiting for status... %ld %ld\n", start, stop);
584                 u8 status = tpm_tis_i2c_status(chip);
585                 if ((status & chip->req_complete_mask) ==
586                     chip->req_complete_val) {
587                         debug("...got it;\n");
588                         goto out_recv;
589                 }
590
591                 if (status == chip->req_canceled) {
592                         error("Operation Canceled\n");
593                         rc = -ECANCELED;
594                         goto out;
595                 }
596                 udelay(TPM_TIMEOUT * 1000);
597         } while (get_timer(start) < stop);
598
599         tpm_tis_i2c_ready(chip);
600         error("Operation Timed out\n");
601         rc = -ETIME;
602         goto out;
603
604 out_recv:
605         debug("out_recv: reading response...\n");
606         rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
607         if (rc < 0)
608                 error("tpm_transmit: tpm_recv: error %d\n", rc);
609
610 out:
611         return rc;
612 }
613
614 /**
615  * Decode TPM configuration.
616  *
617  * @param dev   Returns a configuration of TPM device
618  * @return 0 if ok, -1 on error
619  */
620 static int tpm_tis_i2c_decode_config(struct tpm_chip *chip)
621 {
622         const void *blob = gd->fdt_blob;
623         struct udevice *bus;
624         int chip_addr;
625         int parent;
626         int node;
627         int ret;
628
629         node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
630         if (node < 0) {
631                 node = fdtdec_next_compatible(blob, 0,
632                                 COMPAT_INFINEON_SLB9645_TPM);
633         }
634         if (node < 0) {
635                 debug("%s: Node not found\n", __func__);
636                 return -1;
637         }
638         parent = fdt_parent_offset(blob, node);
639         if (parent < 0) {
640                 debug("%s: Cannot find node parent\n", __func__);
641                 return -1;
642         }
643
644         /*
645          * TODO(sjg@chromium.org): Remove this when driver model supports
646          * TPMs
647          */
648         ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
649         if (ret) {
650                 debug("Cannot find bus for node '%s: ret=%d'\n",
651                       fdt_get_name(blob, parent, NULL), ret);
652                 return ret;
653         }
654
655         chip_addr = fdtdec_get_int(blob, node, "reg", -1);
656         if (chip_addr == -1) {
657                 debug("Cannot find reg property for node '%s: ret=%d'\n",
658                       fdt_get_name(blob, node, NULL), ret);
659                 return ret;
660         }
661         /*
662          * TODO(sjg@chromium.org): Older TPMs will need to use the older method
663          * in tpm_tis_i2c_read() so the offset length needs to be 0 here.
664          */
665         ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
666         if (ret) {
667                 debug("Cannot find device for node '%s: ret=%d'\n",
668                       fdt_get_name(blob, node, NULL), ret);
669                 return ret;
670         }
671
672         return 0;
673 }
674
675 int tis_init(void)
676 {
677         if (g_chip.inited)
678                 return 0;
679
680         if (tpm_tis_i2c_decode_config(&g_chip))
681                 return -1;
682
683         debug("%s: done\n", __func__);
684
685         g_chip.inited = 1;
686
687         return 0;
688 }
689
690 int tis_open(void)
691 {
692         int rc;
693
694         if (!g_chip.inited)
695                 return -1;
696
697         debug("%s: start\n", __func__);
698         if (g_chip.is_open)
699                 return -EBUSY;
700         rc = tpm_tis_i2c_init(g_chip.dev);
701         if (rc < 0)
702                 g_chip.is_open = 0;
703
704         return rc;
705 }
706
707 int tis_close(void)
708 {
709         if (!g_chip.inited)
710                 return -1;
711
712         if (g_chip.is_open) {
713                 tpm_tis_i2c_release_locality(&g_chip, g_chip.locality, 1);
714                 g_chip.is_open = 0;
715         }
716
717         return 0;
718 }
719
720 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
721                 uint8_t *recvbuf, size_t *rbuf_len)
722 {
723         int len;
724         uint8_t buf[4096];
725
726         if (!g_chip.inited)
727                 return -1;
728
729         if (sizeof(buf) < sbuf_size)
730                 return -1;
731
732         memcpy(buf, sendbuf, sbuf_size);
733
734         len = tpm_tis_i2c_transmit(buf, sbuf_size);
735
736         if (len < 10) {
737                 *rbuf_len = 0;
738                 return -1;
739         }
740
741         memcpy(recvbuf, buf, len);
742         *rbuf_len = len;
743
744         return 0;
745 }