-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtac_enf.c
220 lines (187 loc) · 5.21 KB
/
mtac_enf.c
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
#define DRIVER_VERSION "v0.1.0"
#define DRIVER_AUTHOR "David R. Bild <[email protected]>"
#define DRIVER_DESC "Xaptum ENF Access Accessory Card"
#define DRIVER_NAME "mtac-enf"
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/kmod.h>
#include <linux/bitops.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mtac.h>
#include <linux/mts_io.h>
#define PRODUCT_ID_MTAC_ENF "XAP-EA-004"
static struct gpio_pin gpio_pins_mtcdt_mtac_enf[] = {
// gpio pins for Accessory Card 1
{
.name = "AP1_RESET",
.pin = {
.gpio = AT91_PIN_PB12,
.flags = GPIOF_OUT_INIT_HIGH,
.label = "ap1-reset",
}
},
// gpio pins for Accessory Card 2
{
.name = "AP2_RESET",
.pin = {
.gpio = AT91_PIN_PB13,
.flags = GPIOF_OUT_INIT_HIGH,
.label = "ap2-reset",
}
},
{ },
};
static char* enf_gpio_pin_name_by_attr_name(const char* name, int port) {
switch (port) {
case port_1:
if (! strcmp(name, "reset")) {
return "ap1-reset";
} else {
log_error("attribute name [%s] is invalid for ENF in port %d", name, port);
return "";
}
case port_2:
if (! strcmp(name, "reset")) {
return "ap2-reset";
} else {
log_error("attribute name [%s] is invalid for ENF in port %d", name, port);
return "";
}
}
/* NOTREACHED */
return "";
}
// 1 vendor-id
// 1 product-id
// 1 device-id
// 1 hw-version
// 1 reset
// NULL
static size_t ap_enf_attrs_size = 6;
static bool enf_setup(enum ap port) {
int i;
int port_index = port - 1;
int index = 0;
int count = 0;
int ret;
char buf[32];
struct attribute **attrs;
struct kobj_attribute* attr;
struct kobject *subdir;
log_info("loading ENF accessory card in port %d", port);
sprintf(buf, "ap%d", port);
subdir = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj);
if (! subdir) {
log_error("kobject_create_and_add for ENF in port %d failed", port);
return false;
}
mtac_set_port_pins(port_index,gpio_pins_mtcdt_mtac_enf,subdir);
// create the link to the apX directory this card is in
// if we're in the first slot, we get plain "enf"
// if we're in a different slot, we might need to use "enf-2" to differentiate
if (port > 1) {
for (i = 1; i < port; i++) {
if (mtac_port_info[i - 1]) {
if (strstr(mtac_port_info[i - 1]->product_id, PRODUCT_ID_MTAC_ENF)) {
count++;
}
}
}
}
if (count > 0) {
sprintf(buf, "enf-%d", count + 1);
} else {
sprintf(buf, "enf");
}
ret = sysfs_create_link(mtac_port_info[port_index]->subdirs->parent, mtac_port_info[port_index]->subdirs, buf);
if (ret) {
log_error("failed to link [%s] to [%s], %d", buf, mtac_port_info[port_index]->subdirs->name, ret);
}
attrs = kzalloc(sizeof(struct attribute*) * ap_enf_attrs_size, GFP_KERNEL);
if (! attrs) {
log_error("failed to allocate attribute space for port %d", port);
return false;
}
sprintf(buf, "reset");
attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW);
if (! attr) {
log_error("failed to create attribute [%s] for ENF in port %d", buf, port);
kfree(attrs);
return false;
}
mtac_port_info[port_index]->attr_group.attrs = attrs;
attr->show = mtac_attr_show_ap_gpio_pin;
attr->store = mtac_attr_store_ap_gpio_pin;
attrs[index++] = &attr->attr;
// add attributes for eeprom contents
if (! mtac_add_product_info_attributes(port, attrs, &index)) {
log_error("failed to add product info attributes for ENF in port %d", port);
return false;
}
attrs[index] = NULL;
if (sysfs_create_group(mtac_port_info[port_index]->subdirs, &mtac_port_info[port_index]->attr_group)) {
log_error("sysfs_create_group failed for ENF in port %d", port);
return false;
}
return true;
}
static bool enf_teardown(enum ap port) {
int i;
int port_index = port - 1;
struct attribute **attrs = mtac_port_info[port_index]->attr_group.attrs;
mtac_port_info[port_index]->attr_group.attrs = NULL;
log_info("unloading ENF accessory card in port %d", port);
// clean up allocated memory for attributes
for (i = 0; i < ap_enf_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 (mtac_port_info[port_index]->subdirs) {
kobject_put(mtac_port_info[port_index]->subdirs);
}
mtac_clear_port_pins(port_index);
return true;
}
void set_enf_info(struct ap_info* info) {
snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_ENF);
info->setup = &enf_setup;
info->teardown = &enf_teardown;
info->gpio_pin_name_by_attr_name = &enf_gpio_pin_name_by_attr_name;
}
/*
* Loop through all the slots, and set up the
* mtac-enf driver for all slots.
*/
static int __init mtac_enf_init(void)
{
int slot_count = 0;
slot_count = mtac_find(set_enf_info,PRODUCT_ID_MTAC_ENF);
if (slot_count < 1) {
log_debug("No MTAC ENF found");
if (slot_count < 0)
return slot_count;
else
return -ENXIO;
}
return 0;
}
/* We can only tear down our own device */
static void __exit mtac_enf_exit(void)
{
mtac_free(PRODUCT_ID_MTAC_ENF,enf_setup,"enf");
log_info("exiting");
}
module_init(mtac_enf_init);
module_exit(mtac_enf_exit);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");