summaryrefslogtreecommitdiff
path: root/mtac.c
blob: 165d8e010490ae3de8d024855380c6aad0392070 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#define DRIVER_VERSION  "v4.0.5"
#define DRIVER_AUTHOR   "John Klug <john.klug@multitech.com>"
#define DRIVER_DESC "MTS driver to supervise MTAC slots"
#define DRIVER_NAME "mtac-slots"
#define DEBUG 0

#include <linux/types.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/firmware.h>
#include <linux/kmod.h>
#include <linux/bitops.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/mts_io.h>

#include "mtac.h"

/* accessory card EEPROMs, read outside of driver */
uint8_t mts_ap_eeprom[NUM_AP][MTS_AP_EEPROM_SIZE];
EXPORT_SYMBOL(mts_ap_eeprom);
              
static const char* eeprom_file_name[NUM_AP] = { 
#if NUM_AP > 0
  "1-0050/eeprom", "1-0052/eeprom"
#endif
};

/* info for accessory port (contains function pointers for setup and teardown,
 * gpio pin list for the device inserted into the port. */
struct ap_info* mtac_port_info[NUM_AP];
EXPORT_SYMBOL(mtac_port_info); /* Formerly port_info */

/* Protect the mtac port info structure */
DEFINE_MUTEX(mtac_mutex);
EXPORT_SYMBOL(mtac_mutex);

void mtac_clear_port_pins(int port_index)
{
    char buf[32];
    struct gpio_pin *pin;
    struct gpio_pin *pins;
    
    snprintf(buf,sizeof buf,"AP%d_",port_index+1);
    
    /* Find all the GPIO pins for this port and
    * free them all.
    */
    log_debug("mtac_clear_port_pins: State of mtac mutex is %s",
        mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
    mutex_lock(&mtac_mutex);
    pins = mtac_port_info[port_index]->gpio_pins;
    for (pin = pins; *pin->name; pin++) {
        if (!memcmp(pin->name,buf,strlen(buf))) {
            gpio_free(pin->pin.gpio);
        }
    }
    mtac_port_info[port_index]->gpio_pins = NULL;
    log_debug("Unlock mtac_mutex");
    mutex_unlock(&mtac_mutex);
}
EXPORT_SYMBOL(mtac_clear_port_pins);

void mtac_set_port_pins(int port_index, struct gpio_pin *pins, struct kobject *subdir)
{
        log_debug("mtac_set_port_pins: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mtac_port_info[port_index]->gpio_pins = pins;
        mtac_port_info[port_index]->subdirs = subdir;
}
EXPORT_SYMBOL(mtac_set_port_pins);

struct kobj_attribute* mtac_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;
}
EXPORT_SYMBOL(mtac_create_attribute);

int mtac_port_from_kobject(struct kobject *kobj) {
        int port;
        const char *name;

        name = kobj->name;
        if (! name) {
                log_error("kobject->name is NULL");
                return -1;
        }

        if (sscanf(name, "ap%d", &port) < 1) {
                log_error("failed to scan port from kobject->name [%s]", name);
                return -1;
        }

        if (port < 1 || port > NUM_AP) {
                log_error("port number %d is invalid", port);
                return -1;
        }

        return port;
}
EXPORT_SYMBOL(mtac_port_from_kobject);


/*
* This function is not normally used directly by mtac modules.
*/
ssize_t mtac_show_product_info(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
        ssize_t value;
        int port;
        int port_index;
        struct mts_ap_eeprom_layout *app;

        port = mtac_port_from_kobject(kobj);
        if (port < 1) {
                log_error("mtac_port_from_kobject returned %d", port);
                return -1;
        }
        port_index = port - 1;

        app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index];
        if (! strcmp(attr->attr.name, "vendor-id")) {
                value = snprintf(buf, 32, "%s\n", app->vendor_id);
        } else if (! strcmp(attr->attr.name, "product-id")) {
                value = snprintf(buf, 32, "%s\n", app->product_id);
        } else if (! strcmp(attr->attr.name, "device-id")) {
                value = snprintf(buf, 32, "%s\n", app->device_id);
        } else if (! strcmp(attr->attr.name, "hw-version")) {
                value = snprintf(buf, 32, "%s\n", app->hw_version);
        } else {
                log_error("attribute [%s] not found", attr->attr.name);
                value = -1;
        }
        return value;
}
EXPORT_SYMBOL(mtac_show_product_info);

bool mtac_add_product_info_attributes(int port, struct attribute** attrs, int* index) {
        char buf[32];
        struct kobj_attribute* kobj_attr;

        sprintf(buf, "vendor-id");
        kobj_attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RO);
        if (! kobj_attr) {
                log_error("failed to create attribute [%s] in port %d", buf, port);
                return false;
        }
        kobj_attr->show = mtac_show_product_info;
        attrs[(*index)++] = &kobj_attr->attr;

        sprintf(buf, "product-id");
        kobj_attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RO);
        if (! kobj_attr) {
                log_error("failed to create attribute [%s] in port %d", buf, port);
                return false;
        }
        kobj_attr->show = mtac_show_product_info;
        attrs[(*index)++] = &kobj_attr->attr;

        sprintf(buf, "device-id");
        kobj_attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RO);
        if (! kobj_attr) {
                log_error("failed to create attribute [%s] in port %d", buf, port);
                return false;
        }
        kobj_attr->show = mtac_show_product_info;
        attrs[(*index)++] = &kobj_attr->attr;

        sprintf(buf, "hw-version");
        kobj_attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RO);
        if (! kobj_attr) {
                log_error("failed to create attribute [%s] in port %d", buf, port);
                return false;
        }
        kobj_attr->show = mtac_show_product_info;
        attrs[(*index)++] = &kobj_attr->attr;

        return true;
}
EXPORT_SYMBOL(mtac_add_product_info_attributes);


/*
* This function is not normally used directly by mtac modules.
*/
struct gpio_pin *mtac_gpio_pin_by_attr_name(const char *name, int port) {
        struct gpio_pin *pin;
        char *pin_attr_name;
        int port_index = port - 1;
        struct gpio_pin *port_gpio_pins;

        log_debug("mtac_gpio_pin_by_attr_name: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);
        pin_attr_name = mtac_port_info[port_index]->gpio_pin_name_by_attr_name(name, port);
        port_gpio_pins = mtac_port_info[port_index]->gpio_pins;

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

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

ssize_t mtac_attr_show_ap_gpio_pin(struct kobject *kobj,
                        struct kobj_attribute *attr,
                        char *buf)
{
    int value;
    int port;
    struct gpio_pin *pin;
        
    port = mtac_port_from_kobject(kobj);
    if (port < 1) {
            log_error("mtac_port_from_kobject returned %d", port);
            return -EINVAL;
    }

    pin	= mtac_gpio_pin_by_attr_name(attr->attr.name, port);
    if (!pin) {
            return -ENODEV;
    }

    log_debug("mtac_attr_show_ap_gpio_pin: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
    mutex_lock(&mtac_mutex);

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

    mutex_unlock(&mtac_mutex);

    if (value < 0) {
            return value;
    }

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

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


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

        port = mtac_port_from_kobject(kobj);
        if (port < 1) {
                log_error("mtac_port_from_kobject returned %d", port);
                return -EINVAL;
        }
        
        pin	= mtac_gpio_pin_by_attr_name(attr->attr.name, port);
        if (!pin) {
                return -ENODEV;
        }

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

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

        log_debug("mtac_attr_store_ap_gpio_pin: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);

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

        mutex_unlock(&mtac_mutex);

        return count;
}
EXPORT_SYMBOL(mtac_attr_store_ap_gpio_pin);

static void display_port(int port_index) {
        int port = port_index + 1;
        struct mts_ap_eeprom_layout *app;
        

        /* Our caller has locked the mtac_mutex */
        if(!mutex_is_locked(&mtac_mutex))
                log_error("display_port: Must always hold the mtac_mutex here, but mutex was not locked");

        app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index];

        log_info("accessory card %d vendor-id: %.32s", port, app->vendor_id);
        log_info("accessory card %d product-id: %.32s", port,  app->product_id);
        log_info("accessory card %d device-id: %.32s", port,  app->device_id);
        log_info("accessory card %d hw-version: %.32s", port,  app->hw_version);
}

static void acquire_gpio(struct gpio_pin *pins, int port_index)
{
        char buf[32];
        struct gpio_pin *pin;
        int ret;

        snprintf(buf,sizeof buf,"AP%d_",port_index+1);
        for (pin = pins; *pin->name; pin++) {
                if (!memcmp(pin->name,buf,strlen(buf))) {
                        log_debug("Request name:%s label: %s pin: %d", pin->name, pin->pin.label, pin->pin.gpio);
                        ret = gpio_request_one(pin->pin.gpio, pin->pin.flags, pin->pin.label);
                        if (ret)
                                log_debug("could not request pin %s (%d) but it could have already been requested under a different pin name", pin->name, ret);
                }
        }
}

#include <linux/delay.h>

struct gpio_pin *mtac_gpio_pin_by_name(const char *name, int port_index) {
        struct gpio_pin *pin;

        log_debug("mtac_gpio_pin_by_name: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);
        for (pin = mtac_port_info[port_index]->gpio_pins; *pin->name; pin++) {
                if (!strcmp(pin->name, name)) {
                        mutex_unlock(&mtac_mutex);
                        return pin;
                }
        }
        mutex_unlock(&mtac_mutex);

        log_error("pin named %s not found", name);

        return NULL;
}
EXPORT_SYMBOL(mtac_gpio_pin_by_name);


/* static gpio_pins */
// A GPIO pin number must only occur once.
struct gpio_pin *mtac_gpio_pin_by_num(unsigned num, int port_index) {
        int ipin = 0;
        struct gpio_pin *port_gpio_pins;

        log_debug("mtac_gpio_pin_by_num: State of mtac mutex is %s",
            mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);
        port_gpio_pins = mtac_port_info[port_index]->gpio_pins;
        while(*(port_gpio_pins[ipin].name)) {
                if (port_gpio_pins[ipin].pin.gpio == num) {
                        mutex_unlock(&mtac_mutex);
                        return &(port_gpio_pins[ipin]);
                }
                ipin++;
        }
        mutex_unlock(&mtac_mutex);
        log_error("pin numbered %u not found", num);

        return NULL;
}
EXPORT_SYMBOL(mtac_gpio_pin_by_num);

/*
* Depends on static gpio_pins
*/
int mtac_find(void(*set_info)(struct ap_info* info), const char *target_product_id)
{
    int port_index;
    struct mts_ap_eeprom_layout *app;
    int slot_count = 0;

    log_debug("mtac_find enter");
    for (port_index = 0; port_index < NUM_AP; port_index++) {
        log_debug("mtac_find: port_index: %d State of mtac mutex is %s",
                  port_index,
                  mutex_is_locked(&mtac_mutex) ? "locked" : "unlocked");
        mutex_lock(&mtac_mutex);
        if (mtac_port_info[port_index] != NULL) {
            log_debug("Accessory Port %d of %d is in use",port_index+1,NUM_AP);
            mutex_unlock(&mtac_mutex);
            continue;
        }
        app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index];
        log_debug("target_product_id=%s, eeprom=%s",target_product_id,app->product_id);
        if (mts_ap_eeprom[port_index][0] == 0xFF) {
            log_info("uninitialized eeprom on accessory card %d", port_index);
            mutex_unlock(&mtac_mutex);
            continue;
        } else if (mts_ap_eeprom[port_index][0] == 0x0) {
            log_debug("no accessory card inserted in port_index %d", port_index);
            mutex_unlock(&mtac_mutex);
            continue;
        } else if (strstr(app->product_id, target_product_id)) {
            log_debug("strstr matches");
            mtac_port_info[port_index] = kzalloc(sizeof(struct ap_info), GFP_KERNEL);
            if (! mtac_port_info[port_index]) {
                log_error("alloc of port info failed");
                mutex_unlock(&mtac_mutex);
                return -ENOSPC;
            }
            set_info(mtac_port_info[port_index]);
            if (! mtac_port_info[port_index]->setup(port_index+1)) {
                log_error("accessory port %d setup failed", port_index+1);
                mtac_port_info[port_index]->teardown(port_index+1);
                kfree(mtac_port_info[port_index]);
                mtac_port_info[port_index] = NULL;
                mutex_unlock(&mtac_mutex);
                return -ENOSPC;
            } else {
                acquire_gpio(mtac_port_info[port_index]->gpio_pins,port_index);
                slot_count++;
                log_debug("slot_count=%d",slot_count);
            }
        } else
            log_debug("len prod_id: %d eeprom pr id: %d",strlen(target_product_id),strlen(app->product_id));
        mutex_unlock(&mtac_mutex);
    }
    return slot_count;
}
EXPORT_SYMBOL(mtac_find);

void mtac_free(const char *product_id, bool(* setup)(enum ap port), const char *link)
{
    int port_index, count, port;
    char buf[32];
    struct mts_ap_eeprom_layout *app;
    log_debug("mtac_free enter");
    count = 0;
    for (port_index = 0; port_index < NUM_AP; port_index++) {
        port = port_index + 1;
        app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index];
        if (app && strstr(app->product_id, product_id)) {
            count++;
            log_debug("Free port %d product_id: %s mtac_port_info: %p",
                      port_index + 1, product_id,mtac_port_info[port_index]);
            if(mtac_port_info[port_index])
                log_debug("setup: %p setup ptr: %p",mtac_port_info[port_index]->setup,setup);
            if (mtac_port_info[port_index] && (mtac_port_info[port_index]->setup == setup)) {
                log_debug("port_index %d is occupied by us, teardown next",port_index);
                           
                if (count > 1) {
                    sprintf(buf, "%s-%d",link,port);
                } else {
                    sprintf(buf, "%s",link);
                }
                sysfs_remove_link(mtac_port_info[port_index]->subdirs->parent, buf);

                mtac_port_info[port_index]->teardown(port_index+1);
                kfree(mtac_port_info[port_index]);
                mtac_port_info[port_index] = NULL;  // Slot available
            } // MTAC Slot array is in use by our module
        } // Slot EEPROM says it is occupied by our module
    } // Loop through the ports
}
EXPORT_SYMBOL(mtac_free);

/*
* Display the EEPROM for all the slots.
*/
static int __init mtac_init(void)
{
    int port_index;
    const struct firmware* fw = NULL;
    int ret;

    log_debug("init: " DRIVER_VERSION);
    mutex_lock(&mtac_mutex);

    for (port_index = 0; port_index < NUM_AP; port_index++) {
        mts_ap_eeprom[port_index][0] = 0x00; /* initialize to not present */
        if((ret = request_firmware_direct(&fw, eeprom_file_name[port_index], &mts_io_platform_device->dev)) == 0) {
          if(fw->size == sizeof(mts_ap_eeprom[0])) {
              memcpy(mts_ap_eeprom[port_index], fw->data, sizeof(mts_ap_eeprom[0]));
              log_info("EEPROM contents loaded (%s)", eeprom_file_name[port_index]);
          } else {
              log_error("EEPROM invalid size (%s:%d)", eeprom_file_name[port_index], fw->size);
          }
          release_firmware(fw);
        } else {
            log_error("EEPROM unable to read (%s:%d)", eeprom_file_name[port_index], ret);
        }

        if (mts_ap_eeprom[port_index][0] == 0xFF)
            log_alert("uninitialized eeprom on accessory card %d", port_index);
        else if (mts_ap_eeprom[port_index][0] == 0x0)
            log_info("no accessory card inserted in port %d", port_index+1);
        else
            display_port(port_index);
    }
    mutex_unlock(&mtac_mutex);

    return 0;
}

static void __exit mtac_exit(void)
{
    log_info("exiting");
}
module_init(mtac_init);
module_exit(mtac_exit);

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