aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Gaignard <benjamin.gaignard@linaro.org>2015-07-10 13:26:53 +0200
committerBenjamin Gaignard <benjamin.gaignard@linaro.org>2015-07-10 13:26:53 +0200
commit66f990435773a6b09581d415842ac6f6a73c6d13 (patch)
tree30119941de7446319330b7d0899e59cc7c77c5d6
parent5e8098201f9aa60d5d4239e47e5f9fdb3f3d4a83 (diff)
adapt lib to SMAF driver version 3 proposal
Signed-off-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
-rw-r--r--lib/libsmaf.c42
-rw-r--r--lib/libsmaf.h3
-rw-r--r--lib/smaf.h19
-rw-r--r--tests/test_smaf.c95
4 files changed, 48 insertions, 111 deletions
diff --git a/lib/libsmaf.c b/lib/libsmaf.c
index 2025086..3584e54 100644
--- a/lib/libsmaf.c
+++ b/lib/libsmaf.c
@@ -41,30 +41,31 @@ static int smaf_fd = -1;
int smaf_open(void)
{
if (open_count)
- goto add;
+ return 0;
smaf_fd = open(SMAF_DEV, O_RDWR, 0);
if (smaf_fd == -1)
return -1;
-add:
open_count++;
return 0;
}
void smaf_close(void)
{
- open_count--;
-
if (open_count)
- return;
+ open_count--;
- close(smaf_fd);
- smaf_fd = -1;
+ if (!open_count) {
+ if (smaf_fd != -1)
+ close(smaf_fd);
+
+ smaf_fd = -1;
+ }
}
-int smaf_create_buffer(unsigned int length, unsigned int flags, int *fd)
+int smaf_create_buffer(unsigned int length, unsigned int flags, char *name, int *fd)
{
struct smaf_create_data create;
int ret;
@@ -72,10 +73,12 @@ int smaf_create_buffer(unsigned int length, unsigned int flags, int *fd)
if (smaf_fd == -1)
return -1;
- memset (&create, 0, sizeof (create));
+ memset(&create, 0, sizeof (create));
create.length = length;
create.flags = flags;
+ if (name)
+ strncpy(&create.name, name, sizeof(create.name));
ret = ioctl(smaf_fd, SMAF_IOC_CREATE, &create);
if (ret) {
@@ -95,7 +98,7 @@ int smaf_set_secure(int fd, int secure)
if (smaf_fd == -1)
return -1;
- memset (&flag, 0, sizeof(flag));
+ memset(&flag, 0, sizeof(flag));
flag.fd = fd;
flag.secure = secure;
@@ -112,27 +115,10 @@ int smaf_get_secure(int fd)
if (smaf_fd == -1)
return 0;
- memset (&flag, 0, sizeof(flag));
+ memset(&flag, 0, sizeof(flag));
flag.fd = fd;
ret = ioctl(smaf_fd, SMAF_IOC_GET_SECURE_FLAG, &flag);
return flag.secure;
}
-
-int smaf_select_allocator(int fd, char *name)
-{
- struct smaf_select_by_name select;
- int ret;
-
- if (smaf_fd == -1)
- return -1;
-
- memset(&select, 0, sizeof(select));
- select.fd = fd;
- strncpy(select.name, name, sizeof(select.name));
-
- ret = ioctl(smaf_fd, SMAF_IOC_SELECT_BY_NAME, &select);
-
- return ret;
-}
diff --git a/lib/libsmaf.h b/lib/libsmaf.h
index efb4d07..c3247f7 100644
--- a/lib/libsmaf.h
+++ b/lib/libsmaf.h
@@ -31,9 +31,8 @@
int smaf_open();
void smaf_close();
-int smaf_create_buffer(unsigned int length, unsigned int flags, int *fd);
+int smaf_create_buffer(unsigned int length, unsigned int flags, char *name, int *fd);
int smaf_set_secure(int fd, int secure);
int smaf_get_secure(int fd);
-int smaf_select_allocator(int fd, char *name);
#endif
diff --git a/lib/smaf.h b/lib/smaf.h
index b8a0194..428168e 100644
--- a/lib/smaf.h
+++ b/lib/smaf.h
@@ -12,15 +12,19 @@
#include <linux/ioctl.h>
#include <linux/types.h>
+#define ALLOCATOR_NAME_LENGTH 64
+
/**
* struct smaf_create_data - allocation parameters
* @length: size of the allocation
* @flags: flags passed to allocator
+ * @name: name of the allocator to be selected, could be NULL
* @fd: returned file descriptor
*/
struct smaf_create_data {
size_t length;
unsigned int flags;
+ char name[ALLOCATOR_NAME_LENGTH];
int fd;
};
@@ -34,20 +38,10 @@ struct smaf_secure_flag {
int secure;
};
-/**
- * struct smaf_select_by_name - select an allocator
- * @fd: file descriptor
- * @name: name of the allocator to be selected
- */
-struct smaf_select_by_name {
- int fd;
- char name[64];
-};
-
#define SMAF_IOC_MAGIC 'S'
#define SMAF_IOC_CREATE _IOWR(SMAF_IOC_MAGIC, 0, \
- struct smaf_create_data)
+ struct smaf_create_data)
#define SMAF_IOC_GET_SECURE_FLAG _IOWR(SMAF_IOC_MAGIC, 1, \
struct smaf_secure_flag)
@@ -55,7 +49,4 @@ struct smaf_select_by_name {
#define SMAF_IOC_SET_SECURE_FLAG _IOWR(SMAF_IOC_MAGIC, 2, \
struct smaf_secure_flag)
-#define SMAF_IOC_SELECT_BY_NAME _IOWR(SMAF_IOC_MAGIC, 3, \
- struct smaf_select_by_name)
-
#endif
diff --git a/tests/test_smaf.c b/tests/test_smaf.c
index c8e2082..202ea63 100644
--- a/tests/test_smaf.c
+++ b/tests/test_smaf.c
@@ -24,133 +24,94 @@
#define LENGTH 1024*16
-static void test_select_invalid(void)
+static void test_create_named_invalid(void)
{
int ret;
int fd;
- ret = smaf_open();
- if (ret) {
- printf("test_select_invalid smaf_open() failed %d\n", ret);
- return;
- }
-
- ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, &fd);
-
- if (ret || (fd == -1)) {
- printf("test_select_invalid smaf_create_buffer() failed %d\n", ret);
- smaf_close();
- return;
- }
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "deadbeef", &fd);
- ret = smaf_select_allocator(fd, "deadbeef");
if (!ret) {
- printf("test_select_invalid smaf_select_allocator failed %d\n", ret);
- goto end;
+ printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
+ return;
}
- printf("test_select_invalid successed\n");
-end:
- close(fd);
- smaf_close();
+ printf("%s successed\n", __func__);
}
-static void test_select(void)
+static void test_create_named(void)
{
int ret;
int fd;
- ret = smaf_open();
- if (ret) {
- printf("test_select smaf_open() failed %d\n", ret);
- return;
- }
-
- ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, &fd);
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, "smaf-cma", &fd);
if (ret || (fd == -1)) {
- printf("test_select smaf_create_buffer() failed %d\n", ret);
- smaf_close();
+ printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
- ret = smaf_select_allocator(fd, "smaf-cma");
- if (ret) {
- printf("test_select smaf_select_allocator failed %d\n", ret);
- goto end;
- }
+ printf("%s successed\n", __func__);
- printf("test_select successed\n");
-end:
close(fd);
- smaf_close();
}
static void test_secure(void)
{
int ret;
int fd;
- ret = smaf_open();
- if (ret) {
- printf("test_secure smaf_open() failed %d\n", ret);
- return;
- }
-
- ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, &fd);
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);
if (ret || (fd == -1)) {
- printf("test_secure smaf_create_buffer() failed %d\n", ret);
- smaf_close();
+ printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
ret = smaf_set_secure(fd, 1);
if (ret) {
- printf("test_secure smaf_set_secure() failed %d\n", ret);
+ printf("%s smaf_set_secure() failed %d\n", __func__, ret);
goto end;
}
ret = smaf_get_secure(fd);
if (!ret) {
- printf("test_secure smaf_get_secure() failed %d\n", ret);
+ printf("%s smaf_get_secure() failed %d\n", __func__, ret);
goto end;
}
- printf("test_secure successed\n");
+ printf("%s successed\n", __func__);
end:
close(fd);
- smaf_close();
}
-static void test_create(void)
+static void test_create_unnamed(void)
{
int ret;
int fd;
- ret = smaf_open();
-
- if (ret) {
- printf("test_create smaf_open() failed %d\n", ret);
- return;
- }
- ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, &fd);
+ ret = smaf_create_buffer(LENGTH, O_CLOEXEC | O_RDWR, NULL, &fd);
if (ret || (fd == -1)) {
- printf("test_create smaf_create_buffer() failed %d\n", ret);
- smaf_close();
+ printf("%s smaf_create_buffer() failed %d\n", __func__, ret);
return;
}
close(fd);
- smaf_close();
- printf("test_create successed\n");
+ printf("%s successed\n", __func__);
}
void main (void)
{
- test_create();
+ if (smaf_open()) {
+ printf("Can't open /dev/smaf\n");
+ return;
+ }
+
+ test_create_unnamed();
+ test_create_named();
+ test_create_named_invalid();
test_secure();
- test_select();
- test_select_invalid();
+
+ smaf_close();
}