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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
static char* mfser_gpio_pin_name_by_attr_name(const char* name, int port) {
switch (port) {
case port_1:
if (! strcmp(name, "rs4xx-term-res")) {
return "ap1-gpio3";
} else if (! strcmp(name, "rts-override")) {
return "ap1-gpio4";
} else {
log_error("attirbute name [%s] is invalid for MFSER in port %d", name, port);
return "";
}
case port_2:
if (! strcmp(name, "rs4xx-term-res")) {
return "ap2-gpio3";
} else if (! strcmp(name, "rts-override")) {
return "ap2-gpio4";
} else {
log_error("attirbute name [%s] is invalid for MFSER in port %d", name, port);
return "";
}
}
return "";
}
static ssize_t mts_attr_show_mfser_mode(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
int ret;
int port;
int modesel0;
int modesel1;
struct gpio_pin *pin_modesel0;
struct gpio_pin *pin_modesel1;
port = port_from_kobject(kobj);
if (port < 0) {
log_error("port_from_kobject returned %d", port);
return -EINVAL;
}
switch (port) {
case port_1:
pin_modesel0 = gpio_pin_by_name("AP1_GPIO1");
pin_modesel1 = gpio_pin_by_name("AP1_GPIO2");
break;
case port_2:
pin_modesel0 = gpio_pin_by_name("AP2_GPIO1");
pin_modesel1 = gpio_pin_by_name("AP2_GPIO2");
break;
default:
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 kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t count)
{
int port;
int modesel0;
int modesel1;
struct gpio_pin *pin_modesel0;
struct gpio_pin *pin_modesel1;
port = port_from_kobject(kobj);
if (port < 0) {
log_error("port_from_kobject returned %d", port);
return -EINVAL;
}
switch (port) {
case port_1:
pin_modesel0 = gpio_pin_by_name("AP1_GPIO1");
pin_modesel1 = gpio_pin_by_name("AP1_GPIO2");
break;
case port_2:
pin_modesel0 = gpio_pin_by_name("AP2_GPIO1");
pin_modesel1 = gpio_pin_by_name("AP2_GPIO2");
break;
default:
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;
}
// 1 serial mode
// 1 rs4xx term resistor
// 1 rts override
// 1 vendor-id
// 1 product-id
// 1 device-id
// 1 hw-version
// NULL
static size_t ap_mfser_attrs_size = 8;
static bool mfser_setup(enum ap port) {
int i;
int port_index = port - 1;
int index = 0;
int count = 0;
int ret;
char buf[32];
struct kobj_attribute* attr;
struct attribute **attrs;
log_info("loading MFSER accessory card in port %d", port);
sprintf(buf, "ap%d", port);
ap_subdirs[port_index] = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj);
if (! ap_subdirs[port_index]) {
log_error("kobject_create_and_add for MFSER in port %d failed", port);
return false;
}
// create the link to the apX directory this card is in
// if we're in the first slot, we get plain "mfser"
// if we're in a different slot, we might need to use "mfser-2" to differentiate
if (port > 1) {
for (i = 1; i < port; i++) {
if (port_info[i - 1]) {
if (strstr(port_info[i - 1]->product_id, PRODUCT_ID_MTAC_MFSER)) {
count++;
}
}
}
}
if (count > 0) {
sprintf(buf, "mfser-%d", count + 1);
} else {
sprintf(buf, "mfser");
}
ret = sysfs_create_link(ap_subdirs[port_index]->parent, ap_subdirs[port_index], buf);
if (ret) {
log_error("failed to link [%s] to [%s], %d", buf, ap_subdirs[port_index]->name, ret);
}
attrs = kzalloc(sizeof(struct attribute*) * ap_mfser_attrs_size, GFP_KERNEL);
if (! attrs) {
log_error("failed to allocate attribute space for port %d", port);
return false;
}
sprintf(buf, "serial-mode");
attr = create_attribute(buf, MTS_ATTR_MODE_RW);
if (! attr) {
log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
return false;
}
attr->show = mts_attr_show_mfser_mode;
attr->store = mts_attr_store_mfser_mode;
attrs[index++] = &attr->attr;
sprintf(buf, "rs4xx-term-res");
attr = create_attribute(buf, MTS_ATTR_MODE_RW);
if (! attr) {
log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
return false;
}
attr->show = mts_attr_show_ap_gpio_pin;
attr->store = mts_attr_store_ap_gpio_pin;
attrs[index++] = &attr->attr;
sprintf(buf, "rts-override");
attr = create_attribute(buf, MTS_ATTR_MODE_RW);
if (! attr) {
log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
return false;
}
attr->show = mts_attr_show_ap_gpio_pin;
attr->store = mts_attr_store_ap_gpio_pin;
attrs[index++] = &attr->attr;
// add attributes for eeprom contents
if (! ap_add_product_info_attributes(port, MTAC_MFSER_0_0, attrs, &index)) {
log_error("failed to add product info attributes for MFSER in port %d", port);
return false;
}
attrs[index] = NULL;
ap_attr_groups[port_index].attrs = attrs;
if (sysfs_create_group(ap_subdirs[port_index], &ap_attr_groups[port_index])) {
log_error("sysfs_create_group failed for MFSER in port %d", port);
return false;
}
return true;
}
static bool mfser_teardown(enum ap port) {
int i;
int port_index = port - 1;
struct attribute **attrs = ap_attr_groups[port_index].attrs;
log_info("unloading MFSER accessory card in port %d", port);
// clean up allocated memory for attributes
for (i = 0; i < ap_mfser_attrs_size; i++) {
if (attrs[i]) {
if (attrs[i]->name)
kfree(attrs[i]->name);
kfree(attrs[i]);
}
}
kfree(attrs);
// clean up our "apX/" kobject if it exists
if (ap_subdirs[port_index]) {
kobject_put(ap_subdirs[port_index]);
}
return true;
}
bool set_mfser_info(struct ap_info* info) {
snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_MFSER);
info->setup = &mfser_setup;
info->teardown = &mfser_teardown;
info->gpio_pin_name_by_attr_name = &mfser_gpio_pin_name_by_attr_name;
return true;
}
|