summaryrefstree
path: root/kernel/drivers/nfc/snvm/hal/ese_hal.c
blob: d5ef8817b22ed4c5dcf01c98eef8e7be64b8b159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright (C) 2020 Samsung Electronics. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program;
 *
 */
#include <linux/kernel.h>
#include <linux/slab.h>
#include "ese_i2c.h"
#include "ese_spi.h"
#include "ese_hal.h"

#undef ENABLE_HAL_LOG

struct ese_hal_s {
	void *client;
	int (*send)(void *ctx, unsigned char *buf, unsigned int size);
	int (*receive)(void *ctx, unsigned char *buf, unsigned int size);
};

int ese_hal_send(void *ctx, unsigned char *buf, unsigned int size)
{
	struct ese_hal_s *hal = (struct ese_hal_s *)ctx;

	if (hal == NULL || hal->client == NULL || hal->send == NULL
			|| buf == NULL || size == 0) {
		return -1;
	}
#ifdef ENABLE_HAL_LOG
	print_hex_dump(KERN_DEBUG, "[star-hal] send : ", DUMP_PREFIX_NONE, 16, 1, buf, size, 0);
#endif
	if (hal->send(hal->client, buf, size) < 0) {
		return -1;
	}

	return (int)size;
}

int ese_hal_receive(void *ctx, unsigned char *buf, unsigned int size)
{
	struct ese_hal_s *hal = (struct ese_hal_s *)ctx;

	if (hal == NULL || hal->client == NULL || hal->receive == NULL
			|| buf == NULL || size == 0) {
		return -1;
	}

	if (hal->receive(hal->client, buf, size) < 0) {
		return -1;
	}
#ifdef ENABLE_HAL_LOG
	print_hex_dump(KERN_DEBUG, "[star-hal] recv : ", DUMP_PREFIX_NONE, 16, 1, buf, size, 0);
#endif
	return (int)size;
}

void *ese_hal_init(enum hal_type_e type, void *client)
{
	struct ese_hal_s *hal = NULL;

	if (client == NULL) {
		return NULL;
	}

	hal = kzalloc(sizeof(struct ese_hal_s), GFP_KERNEL);
	if (hal == NULL) {
		return NULL;
	}

	switch(type) {
	case ESE_HAL_I2C:
		hal->client = client;
		hal->send = ese_i2c_send;
		hal->receive = ese_i2c_receive;
		break;
	case ESE_HAL_SPI:
		hal->client = client;
		hal->send = ese_spi_send;
		hal->receive = ese_spi_receive;
		break;
	default:
		kfree(hal);
		return NULL;
	}

	return hal;
}

void ese_hal_release(void *ctx)
{
	if (ctx != NULL) {
		kfree(ctx);
	}
}