1 /*****************************************************************************
2  * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
3  *
4  * This copy is licensed under the Apache License, Version 2.0 (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and limitations under the License.
11 
12  ****************************************************************************/
13 
14 /**
15  * @file
16  * $Author$
17  * $Revision$
18  * $Date$
19  *
20  * Low level interface to SPI/eSE driver.
21  *
22  */
23 
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <linux/se_gemalto.h>
31 
32 #include "libse-gto-private.h"
33 #include "spi.h"
34 
35 #define USE_OPEN_RETRY
36 #define MAX_RETRY_CNT 10
37 
38 int
spi_setup(struct se_gto_ctx * ctx)39 spi_setup(struct se_gto_ctx *ctx)
40 {
41 #ifdef USE_OPEN_RETRY
42     int retryCnt = 0;
43 
44 retry:
45     ctx->t1.spi_fd = open(ctx->gtodev, O_RDWR);
46     if (ctx->t1.spi_fd < 0) {
47         err("cannot use %s for spi device, errno = 0x%x\n", ctx->gtodev, errno);
48         if(errno == -EBUSY || errno == EBUSY) {
49             retryCnt++;
50             err("Retry open driver - retry cnt : %d\n", retryCnt);
51             if(retryCnt < MAX_RETRY_CNT) {
52                 sleep(1);
53                 goto retry;
54             }
55         }
56         return -1;
57       }
58 #else
59      ctx->t1.spi_fd = open(ctx->gtodev, O_RDWR);
60      if (ctx->t1.spi_fd < 0) {
61          err("cannot use %s for spi device\n", ctx->gtodev);
62          return -1;
63      }
64 #endif
65      return ctx->t1.spi_fd < 0;
66 }
67 
68 int
spi_teardown(struct se_gto_ctx * ctx)69 spi_teardown(struct se_gto_ctx *ctx)
70 {
71     if (ctx->t1.spi_fd >= 0)
72         if (close(ctx->t1.spi_fd) < 0)
73             warn("failed to close fd to %s, %s.\n", ctx->gtodev, strerror(errno));
74     ctx->t1.spi_fd = -1;
75     return 0;
76 }
77 
78 int
spi_write(int fd,const void * buf,size_t count)79 spi_write(int fd, const void *buf, size_t count)
80 {
81     ssize_t n;
82 
83     n = write(fd, buf, count);
84     if ((int)n != n)
85         return -EFAULT;
86 
87     return (int)n;
88 }
89 
90 int
spi_read(int fd,void * buf,size_t count)91 spi_read(int fd, void *buf, size_t count)
92 {
93     ssize_t n;
94 
95     n = read(fd, buf, count);
96     if ((int)n != n)
97         return -EFAULT;
98 
99     return (int)n;
100 }
101