summaryrefslogtreecommitdiff
path: root/mtac_xdot.c
blob: af0645bbdbc20011bca0b8712961dd4db439fd67 (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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#define DRIVER_VERSION  "v2.0.0"
#define DRIVER_AUTHOR   "Multi-Tech"
#define DRIVER_DESC "MTS XDOT (LoRa) Accessory Card"
#define DRIVER_NAME "mtac-xdot"

#include <linux/types.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>
#include <linux/gpio/consumer.h>

static struct gpio_pin gpio_pins_mtcdt_mtac_xdot[] = {
	// gpio pins for Accessory Card 1
	{
		.name = "AP1_NRESET",
		.pin = {
			.gpio = ~0U,
			.flags = GPIOD_OUT_HIGH,  // Only when not flashing
			.label = "ap1-reset",
		},
		.do_gpio_desc = 1,
	},


	// gpio pins for Accessory Card 2
	{
		.name = "AP2_NRESET",
		.pin = {
			.gpio = ~0U,
			.flags = GPIOD_OUT_HIGH,
			.label = "ap2-reset",
		},
		.do_gpio_desc = 1,
	},
	{ },
};

static int reset_state[NUM_AP];

static ssize_t xdot_show_reset(struct kobject *kobj,
                        struct kobj_attribute *attr,
                        char *buf)
{
    int value;
    int port;
    struct gpio_pin *pin;
        
    dev_dbg(&mts_io_platform_device->dev,"xdot_show_reset: enter\n");
    port = mtac_port_from_kobject(kobj);
    dev_dbg(&mts_io_platform_device->dev,"xdot_show_reset: %d\n",reset_state[port - 1]);
    if (port < 1) {
            dev_err(&mts_io_platform_device->dev,"mtac_port_from_kobject returned %d\n", port);
            return -EINVAL;
    }
    
    pin	= mtac_gpio_pin_by_attr_name(attr->attr.name, port);
    if (!pin) {
        return -ENODEV;
    }

    dev_dbg(&mts_io_platform_device->dev,"mtac_attr_show_ap_gpio_pin: State of mtac mutex is %s\n",
        mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
    mutex_lock(&mtac_mutex);

    value = gpiod_get_value(pin->desc);
    if (value < 0) {
        mutex_unlock(&mtac_mutex);
        return -ENODEV;
    }
    if(reset_state[port - 1] < 0) {
        mutex_unlock(&mtac_mutex);
        return sprintf(buf, "Input state: %d\n", value);
    }

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

static ssize_t xdot_store_reset(struct kobject *kobj,
                struct kobj_attribute *attr, const char *buf, size_t count)
{
        int value, ret;
        int port, port_index;
        struct gpio_pin *pin;

        dev_dbg(&mts_io_platform_device->dev,"xdot_store_reset: store %s\n",buf);
        port = mtac_port_from_kobject(kobj);
        if (port < 1) {
                dev_err(&mts_io_platform_device->dev,"mtac_port_from_kobject returned %d\n", port);
                return -EINVAL;
        }
        port_index = port - 1;
        
        pin	= mtac_gpio_pin_by_attr_name(attr->attr.name, port);
        if (!pin) {
                return -ENODEV;
        }

        if (sscanf(buf, "%i", &value) != 1) {
                return -EINVAL;
        }
        if(value < -1 || value > 1)
                return EINVAL;
        
        dev_dbg(&mts_io_platform_device->dev,"xdot_store_reset: old %d new %d\n",reset_state[port_index],value);

        
        dev_dbg(&mts_io_platform_device->dev,"mtac_attr_store_ap_gpio_pin: State of mtac mutex is %s\n",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);

        if (value == reset_state[port_index]) {
            mutex_unlock(&mtac_mutex);
            return count;  // No change
        }
        
        // reset_state does not match value
        
        if (value == -1) {
            // Wrong GPIO flags, so change to input
            gpiod_put(pin->desc);
	    pin->desc = devm_gpiod_get_optional(&mts_io_platform_device->dev, pin->name, GPIOD_IN);
            if (IS_ERR(pin->desc)) {
                dev_err(&mts_io_platform_device->dev,"Could not request pin %s (%d)\n", pin->name, ret);
                reset_state[port_index] = -2;  // Invalid state
                mutex_unlock(&mtac_mutex);
                return PTR_ERR(pin->desc);
            }
            reset_state[port_index] = -1;
            mutex_unlock(&mtac_mutex);
            return count;  // Pin is now an input
        }
        
        if (reset_state[port_index] == -1) {
            // Wrong GPIO flags, so change to output pin type
            gpiod_put(pin->desc);
            pin->desc = devm_gpiod_get_optional(&mts_io_platform_device->dev, pin->name, GPIOD_IN);
            if (IS_ERR(pin->desc)) {
                dev_err(&mts_io_platform_device->dev,"Could not request pin %s (%d)\n", pin->name, ret);
                reset_state[port_index] = -2;  // Invalid state
                mutex_unlock(&mtac_mutex);
                return PTR_ERR(pin->desc);
            }
        }
        
        if (value >= 0) {
            // Change the value
            gpiod_set_value(pin->desc, value);
            reset_state[port_index] = value;
        }

        mutex_unlock(&mtac_mutex);

        return count;
}


ssize_t xdot_show_eui(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	int retval = -1;
	int port;
	int port_index;
	struct mts_ap_eeprom_layout *app;


	port = mtac_port_from_kobject(kobj);
	if (port < 1) {
		dev_err(&mts_io_platform_device->dev,"mtac_port_from_kobject returned %d\n", port);
		return -1;
	}
	port_index = port - 1;

	app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index];
	if (! strcmp(attr->attr.name, "eui")) {
		retval = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
			app->eui[0],
			app->eui[1],
			app->eui[2],
			app->eui[3],
			app->eui[4],
			app->eui[5],
			app->eui[6],
			app->eui[7]);
	}
	return retval;
}

static char* xdot_gpio_pin_name_by_attr_name(const char* name, int port) {
	switch (port) {
		case port_1:
			if (! strcmp(name, "reset")) {
				return "ap1-reset";
			} else {
				dev_err(&mts_io_platform_device->dev,"attribute name [%s] is invalid for XDOT in port %d\n", name, port);
				return "";
			}

		case port_2:
			if (! strcmp(name, "reset")) {
				return "ap2-reset";
			} else {
				dev_err(&mts_io_platform_device->dev,"attribute name [%s] is invalid for XDOT in port %d\n", name, port);
				return "";
			}
	}
	return "";
}

// 1 eui
// 1 reset
// 1 vendor-id
// 1 product-id
// 1 device-id
// 1 hw-version
// NULL
static size_t ap_xdot_attrs_size = 7;

static bool xdot_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;
	struct kobject *subdir;

	dev_info(&mts_io_platform_device->dev,"loading XDOT accessory card in port %d\n", port);

	sprintf(buf, "ap%d", port);
	subdir = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj);
	if (! subdir) {
		dev_err(&mts_io_platform_device->dev,"kobject_create_and_add for XDOT in port %d failed\n", port);
		return false;
	}

	mtac_set_port_pins(port_index,gpio_pins_mtcdt_mtac_xdot,subdir);

	// create the link to the apX directory this card is in
	// if we're in the first slot, we get plain "XDOT"
	// if we're in a different slot, we might need to use "XDOT-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_XDOT)) {
					count++;
				}
			}
		}
	}
	if (count > 0) {
		sprintf(buf, "xdot-%d", count);
	} else {
		sprintf(buf, "xdot");
	}
	ret = sysfs_create_link(mtac_port_info[port_index]->subdirs->parent, mtac_port_info[port_index]->subdirs, buf);
	if (ret) {
		dev_err(&mts_io_platform_device->dev,"Failed to link [%s] to [%s], %d\n", buf, mtac_port_info[port_index]->subdirs->name, ret);
	}

	attrs = kzalloc(sizeof(struct attribute*) * ap_xdot_attrs_size, GFP_KERNEL);
	if (! attrs) {
		dev_err(&mts_io_platform_device->dev,"Failed to allocate attribute space for port %d\n", port);
		return false;
	}
	
	sprintf(buf,"eui");
	attr = mtac_create_attribute("eui", MTS_ATTR_MODE_RO);
	if (! attr) {
		dev_err(&mts_io_platform_device->dev,"Failed to create attribute [%s] for XDOT in port %d\n", buf, port);
		attrs[index] = NULL;
		mtac_port_info[port_index]->attr_group.attrs = attrs;
		return false;
	}
	attr->show = xdot_show_eui;
	attrs[index++] = &attr->attr;

	sprintf(buf, "reset");
	attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW);
	if (! attr) {
		dev_err(&mts_io_platform_device->dev,"Failed to create attribute [%s] for ETH in port %d\n", buf, port);
		kfree(attrs);
		return false;
	}
	mtac_port_info[port_index]->attr_group.attrs = attrs;
	attr->show = xdot_show_reset;
	attr->store = xdot_store_reset;
	attrs[index++] = &attr->attr;

	// add attributes for eeprom contents
	if (! mtac_add_product_info_attributes(port, attrs, &index)) {
		dev_err(&mts_io_platform_device->dev,"Failed to add product info attributes for XDOT in port %d\n", port);
		return false;
	}

	attrs[index] = NULL;

	mtac_port_info[port_index]->attr_group.attrs = attrs;
	if (sysfs_create_group(mtac_port_info[port_index]->subdirs, &mtac_port_info[port_index]->attr_group)) {
		dev_err(&mts_io_platform_device->dev,"sysfs_create_group failed for XDOT in port %d\n", port);
		return false;
	}

	return true;
}

static bool xdot_teardown(enum ap port) {
	int i;
	int port_index = port - 1;
	struct attribute **attrs = mtac_port_info[port_index]->attr_group.attrs;

	dev_info(&mts_io_platform_device->dev,"unloading XDOT accessory card in port %d\n", port);

	// clean up allocated memory for attributes
	for (i = 0; i < ap_xdot_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_xdot_info(struct ap_info* info) {
	snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_XDOT);
	info->setup = &xdot_setup;
	info->teardown = &xdot_teardown;
	info->gpio_pin_name_by_attr_name = &xdot_gpio_pin_name_by_attr_name;
}

/*
 * Loop through all the slots, and set up the
 * mtac-xdot driver for all slots.
 */
static int __init mtac_xdot_init(void)
{
	int slot_count = 0;

	slot_count = mtac_find(set_xdot_info,PRODUCT_ID_MTAC_XDOT);

	if (slot_count < 1) {
		dev_dbg(&mts_io_platform_device->dev,"No MTAC XDOT found\n");
		if (slot_count < 0)
			return slot_count;
		else
			return -ENXIO;

	}

	return 0;
}
/* We can only tear down our own device */
static void __exit mtac_xdot_exit(void)
{
	mtac_free(PRODUCT_ID_MTAC_XDOT,xdot_setup,"xdot");
	dev_info(&mts_io_platform_device->dev,"exiting\n");
}

module_init(mtac_xdot_init);
module_exit(mtac_xdot_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");