summaryrefslogtreecommitdiff
path: root/io-module/mts_capab.c
blob: f22c70b729d8c24cc32f5b8929bd45f71be2eba4 (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
106
107
108
109
/*
 * capab.c
 *
 *  Created on: Dec 1, 2016
 *      Author: jklug
 *
 *  capabilities from EE-PROM, enumerated in /sys/devices/platform/mts-io/capability
 *  an associative array, written the hard way in C.
 */

/* This array is not in any order. */
struct capab_map_s {
	unsigned int bitvalue;
	const char *name;
};

/*
 * Should match mts_eeprom.h capabilities
 */
static struct capab_map_s capabilities_map[] = {
		{ CAPA_GPS,       "gps"},
		{ CAPA_DIN,       "din"},
		{ CAPA_DOUT,      "dout"},
		{ CAPA_ADC,       "adc"},
		{ CAPA_BLUETOOTH, "bluetooth"},
		{ CAPA_WIFI,      "wifi"},
		{ CAPA_LORA,      "lora"},
		{ CAPA_BATTERY,   "battery"},
		{ CAPA_SUPERCAP,  "supercap"},
};

static ssize_t capab_show_value(struct device *dev, struct device_attribute *at, char *buf) {
	int i;
	const int count = sizeof(capabilities_map)/sizeof(capabilities_map[0]);
        struct kobj_attribute *attr = (struct kobj_attribute *)at;

	for (i = 0; i < count; i++) {
		if (strcmp(capabilities_map[i].name, attr->attr.name) == 0) {
			return snprintf(buf, 32, "%u\n", DEVICE_CAPA(id_eeprom.capa, capabilities_map[i].bitvalue) != 0);
		}
	}

	return (ssize_t)-1;
}

static DEVICE_ATTR_RO_MTS(capa_attr_gps,  "gps",       capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_din,  "din",       capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_dout, "dout",      capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_adc,  "adc",       capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_bt,   "bluetooth", capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_wifi, "wifi",      capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_lora, "lora",      capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_battery, "battery",      capab_show_value);
static DEVICE_ATTR_RO_MTS(capa_attr_supercap, "supercap",    capab_show_value);

static struct attribute *mts_capa_attributes[] = {
		&capa_attr_gps.attr,
		&capa_attr_din.attr,
		&capa_attr_dout.attr,
		&capa_attr_adc.attr,
		&capa_attr_bt.attr,
		&capa_attr_wifi.attr,
		&capa_attr_lora.attr,
		&capa_attr_battery.attr,
		&capa_attr_supercap.attr,
		NULL,
};

static struct attribute_group mts_capa_attr_group = {
	.attrs = mts_capa_attributes
};

static struct kobject *mts_capa_kobject = NULL;
EXPORT_SYMBOL(mts_capa_kobject);

static int mts_capab_dir_create(uint8_t hw_version)
{
    int i,j;
    
    if (hw_version == MTCPM_0_0) {
        for (i=0; mts_capa_attributes[i]; i++) {
            /* Remove capa_lora for MTCPM mts-io driver */
            if (mts_capa_attributes[i] == &capa_attr_lora.attr) {
                for(j=i; mts_capa_attributes[j]; j++)
                    mts_capa_attributes[j] = mts_capa_attributes[j+1];
                break;
            }
        }
    }
    mts_capa_kobject = kobject_create_and_add("capability", &mts_io_platform_device->dev.kobj);
    if (!mts_capa_kobject) {
            log_error("kobject_create_and_add for capability directory failed");
            return -ENOMEM;
    }

    if (sysfs_create_group(mts_capa_kobject, &mts_capa_attr_group)) {
            log_error("sysfs_create_group failed to create capability group");
            return -ENOMEM;
    }

    return 0;
}

static void mts_capab_dir_delete(void)
{
	if (mts_capa_kobject) {
		kobject_put(mts_capa_kobject);
	}
}