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