summaryrefslogtreecommitdiff
path: root/io-module/mtac.c
blob: 1c88b40114b000e309cea76c8b273f8e130e882b (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
static struct kobj_attribute* create_attribute(const char* _name, umode_t _mode) {
	char* attr_name;
	struct kobj_attribute* _attr;
	
	_attr = kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL);
	if (! _attr) {
		log_error("kzalloc of attribute %s failed", _name);
		return NULL;
	}

	sysfs_attr_init(_attr);
	attr_name = kstrdup(_name, GFP_KERNEL);
	if (! attr_name) {
		log_error("GFP_KERNEL dup failed for attribute [%s]", _name);
		return NULL;
	}

	_attr->attr.name = attr_name;
	_attr->attr.mode = _mode;

	return _attr;
}

static ssize_t ap_show_product_info(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
	ssize_t value;
	int port;

	if (sscanf(attr->attr.name, "vendor-id:%d", &port) > 0) {
		value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].vendor_id);
	} else if (sscanf(attr->attr.name, "product-id:%d", &port) > 0) {
		value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].product_id);
	} else if (sscanf(attr->attr.name, "device-id:%d", &port) > 0) {
		value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].device_id);
	} else if (sscanf(attr->attr.name, "hw-version:%d", &port) > 0) {
		value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].hw_version);
	} else if (sscanf(attr->attr.name, "mac-eth:%d", &port) > 0) {
		value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n",
			ap_eeprom[port - 1].mac_addr[0],
			ap_eeprom[port - 1].mac_addr[1],
			ap_eeprom[port - 1].mac_addr[2],
			ap_eeprom[port - 1].mac_addr[3],
			ap_eeprom[port - 1].mac_addr[4],
			ap_eeprom[port - 1].mac_addr[5]);
	} else {
		log_error("attribute '%s' not found", attr->attr.name);
		value = -1;
	}

	return value;
}

static bool ap_add_product_info_attributes(int port, int type) {
	struct kobj_attribute *attr;
	char buf[32];

	switch (type) {
		case MTAC_ETH_0_0:
			sprintf(buf, "mac-eth:%d", port);
			attr = create_attribute(buf, MTS_ATTR_MODE_RO);
			if (! attr) {
				log_error("failed to create attribute[%s]", buf);
				return false;
			}
			attr->show = ap_show_product_info;
			device_attrs[device_attrs_size++] = &attr->attr;
			break;

		case MTAC_GPIOB_0_0:
		case MTAC_MFSER_0_0:
			break;
		default:
			log_error("invalid accessory card type");
			return false;
	}

	sprintf(buf, "vendor-id:%d", port);
	attr = create_attribute(buf, MTS_ATTR_MODE_RO);
	if (! attr) {
		log_error("failed to create attribute[%s]", buf);
		return false;
	}
	attr->show = ap_show_product_info;
	device_attrs[device_attrs_size++] = &attr->attr;

	sprintf(buf, "product-id:%d", port);
	attr = create_attribute(buf, MTS_ATTR_MODE_RO);
	if (! attr) {
		log_error("failed to create attribute[%s]", buf);
		return false;
	}
	attr->show = ap_show_product_info;
	device_attrs[device_attrs_size++] = &attr->attr;

	sprintf(buf, "device-id:%d", port);
	attr = create_attribute(buf, MTS_ATTR_MODE_RO);
	if (! attr) {
		log_error("failed to create attribute[%s]", buf);
		return false;
	}
	attr->show = ap_show_product_info;
	device_attrs[device_attrs_size++] = &attr->attr;

	sprintf(buf, "hw-version:%d", port);
	attr = create_attribute(buf, MTS_ATTR_MODE_RO);
	if (! attr) {
		log_error("failed to create attribute[%s]", buf);
		return false;
	}
	attr->show = ap_show_product_info;
	device_attrs[device_attrs_size++] = &attr->attr;

	return true;
}

struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name) {
	struct gpio_pin *pin;
	char *pin_attr_name;
	long port;
	int port_index;
	char *colon;

	colon = strstr(name, ":");
	if (colon && ++colon) {
		if (kstrtol(colon, 10, &port)) {
			log_error("kstrtol failed on [%s]", colon);
			return NULL;
		}
	} else {
		log_error("could not read port from attr name [%s]", name);
		return NULL;
	}
	port_index = port - 1;

	pin_attr_name = port_info[port_index]->gpio_pin_name_by_attr_name(name);

	for (pin = gpio_pins; *pin->name; pin++) {
		if (!strcmp(pin->pin.label, pin_attr_name)) {
			return pin;
		}
	}

	log_error("pin with attr name [%s] not found", name);
	return NULL;
}

static ssize_t mts_attr_show_ap_gpio_pin(struct kobject *kobj,
			struct kobj_attribute *attr,
			char *buf)
{
	int value;
	struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name);

	if (!pin) {
		return -ENODEV;
	}

	mutex_lock(&mts_io_mutex);

	value = gpio_get_value(pin->pin.gpio);

	mutex_unlock(&mts_io_mutex);

	if (value < 0) {
		return value;
	}

	if (pin->active_low) {
		value = !value;
	}

	return sprintf(buf, "%d\n", value);
}

static ssize_t mts_attr_store_ap_gpio_pin(struct kobject *kobj,
		struct kobj_attribute *attr, const char *buf, size_t count)
{
	int value;
	struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name);

	if (!pin) {
		return -ENODEV;
	}

	if (sscanf(buf, "%i", &value) != 1) {
		return -EINVAL;
	}

	if (pin->active_low) {
		value = !value;
	}

	mutex_lock(&mts_io_mutex);

	gpio_set_value(pin->pin.gpio, value);

	mutex_unlock(&mts_io_mutex);

	return count;
}