summaryrefslogtreecommitdiff
path: root/io-module/mtac_mfser.c
blob: 14eddd1190d541a1c23a2bff8a18a135bdbba3c4 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
struct gpio_pin *ap_mfser_pin_by_attr_name(const char *name) {
	struct gpio_pin *pin;
	char *pin_attr_name;

	if (!strcmp(name, "rs4xx-term-res:0")) {
		pin_attr_name = "ap1-gpio3";
	} else if (!strcmp(name, "rts-override:0")) {
		pin_attr_name = "ap1-gpio4";
	} else if (!strcmp(name, "rs4xx-term-res:1")) {
		pin_attr_name = "ap2-gpio3";
	} else if (!strcmp(name, "rts-override:1")) {
		pin_attr_name = "ap2-gpio4";
	} else {
		log_error("accessory card attribute %s not available", name);
		return NULL;
	}

	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_mfser_pin(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	int value;
	struct gpio_pin *pin = ap_mfser_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_mfser_pin(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int value;
	struct gpio_pin *pin = ap_mfser_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;
}

static ssize_t mts_attr_show_mfser_mode(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	int ret;
	int modesel0;
	int modesel1;

	struct gpio_pin *pin_modesel0;
	struct gpio_pin *pin_modesel1;

	if (strstr(attr->attr.name, ":0")) {
		pin_modesel0 = gpio_pin_by_name("AP1_GPIO1");
		pin_modesel1 = gpio_pin_by_name("AP1_GPIO2");
	}
	else if (strstr(attr->attr.name, ":1")) {
		pin_modesel0 = gpio_pin_by_name("AP2_GPIO1");
		pin_modesel1 = gpio_pin_by_name("AP2_GPIO2");
	}
	else {
		log_error("unknown serial-mode attr %s", attr->attr.name);
		return -ENODEV;
	}

	if (!pin_modesel0 || !pin_modesel1)
		return -ENODEV;

	mutex_lock(&mts_io_mutex);

	modesel0 = gpio_get_value(pin_modesel0->pin.gpio);
	modesel1 = gpio_get_value(pin_modesel1->pin.gpio);

	if (modesel1 == 0 && modesel0 == 0)
		ret = sprintf(buf, "loopback\n");
	else if (modesel1 == 0 && modesel0 == 1)
		ret = sprintf(buf, "rs232\n");
	else if (modesel1 == 1 && modesel0 == 0)
		ret = sprintf(buf, "rs485-half\n");
	else if (modesel1 == 1 && modesel0 == 1)
		ret = sprintf(buf, "rs422-485-full\n");
	else
		ret = sprintf(buf, "error\n");

	mutex_unlock(&mts_io_mutex);

	return ret;
}

static ssize_t mts_attr_store_mfser_mode(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	int modesel0;
	int modesel1;
	struct gpio_pin *pin_modesel0;
	struct gpio_pin *pin_modesel1;

	if (strstr(attr->attr.name, ":0")) {
		pin_modesel0 = gpio_pin_by_name("AP1_GPIO1");
		pin_modesel1 = gpio_pin_by_name("AP1_GPIO2");
	}
	else if (strstr(attr->attr.name, ":1")) {
		pin_modesel0 = gpio_pin_by_name("AP2_GPIO1");
		pin_modesel1 = gpio_pin_by_name("AP2_GPIO2");
	}
	else {
		log_error("unknown serial-mode attr %s", attr->attr.name);
		return -ENODEV;
	}

	if (!pin_modesel0 || !pin_modesel1)
		return -ENODEV;

	if (!strcasecmp(buf, "loopback")) {
		modesel1 = 0;
		modesel0 = 0;
	}
	else if (!strcasecmp(buf, "rs232")) {
		modesel1 = 0;
		modesel0 = 1;
	}
	else if (!strcasecmp(buf, "rs485-half")) {
		modesel1 = 1;
		modesel0 = 0;
	}
	else if (!strcasecmp(buf, "rs422-485-full")) {
		modesel1 = 1;
		modesel0 = 1;
	}
	else  {
		return -EINVAL;
	}

	mutex_lock(&mts_io_mutex);

	gpio_set_value(pin_modesel0->pin.gpio, modesel0);
	gpio_set_value(pin_modesel1->pin.gpio, modesel1);

	mutex_unlock(&mts_io_mutex);

	return count;
}

/* accessory port 1 serial attributes */
static DEVICE_ATTR_MTS(dev_attr_ap1_serial_mode, "serial-mode:0", mts_attr_show_mfser_mode, mts_attr_store_mfser_mode);
static DEVICE_ATTR_MTS(dev_attr_ap1_rs4xx_term_res, "rs4xx-term-res:0", mts_attr_show_ap_mfser_pin, mts_attr_store_ap_mfser_pin);
static DEVICE_ATTR_MTS(dev_attr_ap1_rts_override, "rts-override:0", mts_attr_show_ap_mfser_pin, mts_attr_store_ap_mfser_pin);

static int ap1_mfser_attributes_size = 3;		// not including NULL at end

static struct attribute *ap1_mfser_attributes[] = {
	&dev_attr_ap1_serial_mode.attr,
	&dev_attr_ap1_rs4xx_term_res.attr,		// gpio3
	&dev_attr_ap1_rts_override.attr,		// gpio4
	NULL,
};

/* accessory port 2 serial attributes */
static DEVICE_ATTR_MTS(dev_attr_ap2_serial_mode, "serial-mode:1", mts_attr_show_mfser_mode, mts_attr_store_mfser_mode);
static DEVICE_ATTR_MTS(dev_attr_ap2_rs4xx_term_res, "rs4xx-term-res:1", mts_attr_show_ap_mfser_pin, mts_attr_store_ap_mfser_pin);
static DEVICE_ATTR_MTS(dev_attr_ap2_rts_override, "rts-override:1", mts_attr_show_ap_mfser_pin, mts_attr_store_ap_mfser_pin);

static int ap2_mfser_attributes_size = 3;		// not including NULL at end

static struct attribute *ap2_mfser_attributes[] = {
	&dev_attr_ap2_serial_mode.attr,
	&dev_attr_ap2_rs4xx_term_res.attr,		// gpio3
	&dev_attr_ap2_rts_override.attr,		// gpio4
	NULL,
};