-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw_sensors.c
154 lines (136 loc) · 5.51 KB
/
hw_sensors.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
/* Copyright Statement:
*
* This software/firmware and related documentation ("Fishsemi Software") are
* protected under relevant copyright laws. The information contained herein is
* confidential and proprietary to Fishsemi Inc. and/or its licensors. Without
* the prior written permission of Fishsemi inc. and/or its licensors, any
* reproduction, modification, use or disclosure of Fishsemi Software, and
* information contained herein, in whole or in part, shall be strictly
* prohibited.
*
* Fishsemi Inc. (C) 2019. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("PINECONE SOFTWARE")
* RECEIVED FROM PINECONE AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
* ON AN "AS-IS" BASIS ONLY. PINECONE EXPRESSLY DISCLAIMS ANY AND ALL
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NONINFRINGEMENT. NEITHER DOES PINECONE PROVIDE ANY WARRANTY WHATSOEVER WITH
* RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
* INCORPORATED IN, OR SUPPLIED WITH THE PINECONE SOFTWARE, AND RECEIVER AGREES
* TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
* RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
* OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN PINECONE
* SOFTWARE. PINECONE SHALL ALSO NOT BE RESPONSIBLE FOR ANY PINECONE SOFTWARE
* RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND PINECONE'S
* ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE PINECONE SOFTWARE
* RELEASED HEREUNDER WILL BE, AT PINECONE'S OPTION, TO REVISE OR REPLACE THE
* PINECONE SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
* CHARGE PAID BY RECEIVER TO PINECONE FOR SUCH PINECONE SOFTWARE AT ISSUE.
*
* The following software/firmware and/or related documentation ("Fishsemi
* Software") have been modified by Fishsemi Inc. All revisions are subject to
* any receiver's applicable license agreements with Fishsemi Inc.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sensor_driver.h"
#include "hw_sensors.h"
#include "utils.h"
#if 0
static struct list_node hw_sensors = LIST_INITIAL_VALUE(hw_sensors);
#else
/* here we just store the pointers of the sensor_dev, so a static array will be more efficent */
#define MAX_HW_SENSORS 10
static FAR struct sensor_dev *hw_sensors[MAX_HW_SENSORS];
static int hw_sensor_num = 0;
#endif
extern FAR const struct sensor_driver *g_sensor_drv[];
extern FAR const struct sensor_platform_data *g_sensor_pdata[];
static FAR const struct sensor_driver *hw_sensors_get_match_driver(FAR const struct sensor_platform_data *pdata,
FAR const struct sensor_matching_data **matching_data)
{
FAR const struct sensor_matching_data **mdata;
int i, j;
for (i = 0; g_sensor_drv[i] != NULL; i++) {
mdata = g_sensor_drv[i]->mdata;
for (j = 0; mdata[j] != NULL; j++)
if (strcmp(mdata[j]->name, pdata->name) == 0) {
*matching_data = mdata[j];
return g_sensor_drv[i];
}
}
return NULL;
}
int hw_sensors_init(void)
{
/* 1. get the version of the hardware, e.g. board_id
* 2. iterate all the sensor boards to get the one matches the board_id
* 3. iterate all the sensor drivers in the section ".sensor_driver_section",
* and get the matched driver
* 4. call the probe of the driver with platform data
* 5. if the reture value is 0, then put the dev into the hw_sensors[]
* */
FAR const struct sensor_driver *drv;
FAR const struct sensor_matching_data *mdata = NULL;
FAR struct sensor_dev *sdev;
int i, j;
uint32_t num;
int ret = -ENODEV;
/*XXX: maybe we can remove this limitation, a succeeded init call needed? */
if (hw_sensor_num) {
snshuberr("hw_sensors_init is a oneshot routine!!!\n");
return -EINVAL;
}
if (!g_sensor_pdata[0] || !g_sensor_drv[0]) {
snshuberr("hw_sensors_init failed to get right platform or driver data\n");
return -ENOENT;
}
for (i = 0; g_sensor_pdata[i] != NULL; i++) {
snshubinfo("start to init for %s\n", g_sensor_pdata[i]->name);
drv = hw_sensors_get_match_driver(g_sensor_pdata[i], &mdata);
if (!drv) {
snshuberr("no drivers for %s\n", g_sensor_pdata[i]->name);
continue;
}
ret = drv->probe(g_sensor_pdata[i], mdata, &sdev, &num);
if (ret) {
snshuberr("probe for %s failed:%d\n", g_sensor_pdata[i]->name, ret);
continue;
}
for (j = 0; j < num; j++) {
if (j + hw_sensor_num >= MAX_HW_SENSORS) {
/*XXX*/
snshuberr("too many sensor devices\n");
break;
}
hw_sensors[j + hw_sensor_num] = sdev + j;
}
hw_sensor_num += j;
}
snshubinfo("hw_sensors: %d hw sensors probed:\n", hw_sensor_num);
for (i = 0; i < hw_sensor_num; i++) {
snshubinfo("\tname=%s, type=%d\n", hw_sensors[i]->name, hw_sensors[i]->type);
}
return 0;
}
int hw_sensors_deinit(void)
{
/*TODO:
* call the remove functions of all the sensor devices, in which the
* sensor_dev structure will be freed and the devices will be shutdown
* */
memset(hw_sensors, 0, sizeof(struct sensor_dev *) * MAX_HW_SENSORS);
hw_sensor_num = 0;
return 0;
}
/* get all of the hardware sensors */
int hw_sensors_get_list(FAR const struct sensor_dev ***sensor_list)
{
*sensor_list = (const struct sensor_dev **)&hw_sensors[0];
return hw_sensor_num;
}