summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/sonynwz/nvp-nwz.c
blob: f7511d61181795c20f3f07c99f9a8009f587854c (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "nvp-nwz.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>

static unsigned long find_model_id(void)
{
    /* try with the environment variable */
    const char *mid = getenv("ICX_MODEL_ID");
    if(mid == NULL)
        return 0;
    char *end;
    unsigned long v = strtoul(mid, &end, 0);
    if(*end)
        return 0;
    else
        return v;
}

unsigned long nwz_get_model_id(void)
{
    static unsigned long model_id = 0xffffffff;
    if(model_id == 0xffffffff)
        model_id = find_model_id();
    return model_id;
}

const char *nwz_get_model_name(void)
{
    for(int i = 0; i < NWZ_MODEL_COUNT; i++)
        if(nwz_model[i].mid == nwz_get_model_id())
            return nwz_model[i].name;
    return NULL;
}

static int find_series(void)
{
    for(int i = 0; i < NWZ_SERIES_COUNT; i++)
        for(int j = 0; j < nwz_series[i].mid_count; j++)
            if(nwz_series[i].mid[j] == nwz_get_model_id())
                return i;
    return -1;
}

int nwz_get_series(void)
{
    static int series = -2;
    if(series == -2)
        series = find_series();
    return series;
}

static nwz_nvp_index_t *get_nvp_index(void)
{
    static nwz_nvp_index_t *index = 0;
    if(index == 0)
    {
        int series = nwz_get_series();
        index = series < 0 ? 0 : nwz_series[series].nvp_index;
    }
    return index;
}

int nwz_nvp_read(enum nwz_nvp_node_t node, void *data)
{
    int size = nwz_nvp[node].size;
    if(data == 0)
        return size;
    nwz_nvp_index_t *index = get_nvp_index();
    if(index == 0 || (*index)[node] == NWZ_NVP_INVALID)
        return -1;
    char nvp_path[32];
    snprintf(nvp_path, sizeof(nvp_path), "/dev/icx_nvp/%03d", (*index)[node]);
    int fd = open(nvp_path, O_RDONLY);
    if(fd < 0)
        return -1;
    int cnt = read(fd, data, size);
    close(fd);
    return cnt == size ? size : -1;
}

int nwz_nvp_write(enum nwz_nvp_node_t node, void *data)
{
    int size = nwz_nvp[node].size;
    nwz_nvp_index_t *index = get_nvp_index();
    if(index == 0 || (*index)[node] == NWZ_NVP_INVALID)
        return -1;
    char nvp_path[32];
    snprintf(nvp_path, sizeof(nvp_path), "/dev/icx_nvp/%03d", (*index)[node]);
    int fd = open(nvp_path, O_WRONLY);
    if(fd < 0)
        return -1;
    int cnt = write(fd, data, size);
    close(fd);
    return cnt == size ? 0 : -1;
}