]> sigrok.org Git - libsigrok.git/blame - src/hardware/ipdbg-la/api.c
drivers: Make per-driver sr_dev_driver structs static.
[libsigrok.git] / src / hardware / ipdbg-la / api.c
CommitLineData
a4210e18
EK
1/*
2 * This file is part of the libsigrok project.
3 *
932ef10f 4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
a4210e18
EK
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include "protocol.h"
22
1f15efc1 23static const uint32_t drvopts[] = {
9d2e5483 24 SR_CONF_LOGIC_ANALYZER,
a4210e18
EK
25};
26
1f15efc1 27static const uint32_t scanopts[] = {
9d2e5483 28 SR_CONF_CONN,
a4210e18
EK
29};
30
1f15efc1 31static const uint32_t devopts[] = {
1f9652a8 32 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
9d2e5483 33 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
1f9652a8 34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
a4210e18
EK
35};
36
1f15efc1 37static const int32_t trigger_matches[] = {
9d2e5483
SA
38 SR_TRIGGER_ZERO,
39 SR_TRIGGER_ONE,
40 SR_TRIGGER_RISING,
41 SR_TRIGGER_FALLING,
42 SR_TRIGGER_EDGE,
a4210e18
EK
43};
44
8a9788e2 45static void ipdbg_la_split_addr_port(const char *conn, char **addr,
9d2e5483 46 char **port)
a4210e18 47{
9d2e5483 48 char **strs = g_strsplit(conn, "/", 3);
a4210e18 49
9d2e5483
SA
50 *addr = g_strdup(strs[1]);
51 *port = g_strdup(strs[2]);
a4210e18 52
9d2e5483 53 g_strfreev(strs);
a4210e18
EK
54}
55
56static GSList *scan(struct sr_dev_driver *di, GSList *options)
57{
9d2e5483
SA
58 struct drv_context *drvc;
59 GSList *devices;
a4210e18 60
9d2e5483
SA
61 devices = NULL;
62 drvc = di->context;
63 drvc->instances = NULL;
64 const char *conn;
65 struct sr_config *src;
66 GSList *l;
a4210e18 67
9d2e5483
SA
68 conn = NULL;
69 for (l = options; l; l = l->next) {
70 src = l->data;
71 switch (src->key) {
72 case SR_CONF_CONN:
73 conn = g_variant_get_string(src->data, NULL);
74 break;
75 }
76 }
a4210e18 77
9d2e5483
SA
78 if (!conn)
79 return NULL;
a4210e18 80
8a9788e2 81 struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new();
a4210e18 82
8a9788e2 83 ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port);
a4210e18 84
9d2e5483
SA
85 if (!tcp->address)
86 return NULL;
a4210e18 87
8a9788e2 88 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 89 return NULL;
a4210e18 90
8a9788e2
UH
91 ipdbg_la_send_reset(tcp);
92 ipdbg_la_send_reset(tcp);
ed186484 93
8a9788e2 94 if (ipdbg_la_request_id(tcp) != SR_OK)
ed186484 95 return NULL;
a4210e18 96
9d2e5483 97 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
9d2e5483
SA
98 sdi->status = SR_ST_INACTIVE;
99 sdi->vendor = g_strdup("ipdbg.org");
8a9788e2 100 sdi->model = g_strdup("IPDBG LA");
9d2e5483
SA
101 sdi->version = g_strdup("v1.0");
102 sdi->driver = di;
a4210e18 103
1f15efc1 104 struct dev_context *devc = ipdbg_la_dev_new();
9d2e5483 105 sdi->priv = devc;
a4210e18 106
8a9788e2 107 ipdbg_la_get_addrwidth_and_datawidth(tcp, devc);
a4210e18 108
ac3625be
UH
109 sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width,
110 devc->data_width);
9d2e5483 111 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
7f4c9a04 112
ac3625be 113 for (uint32_t i = 0; i < devc->data_width; i++) {
8bc8e909
UH
114 char *name = g_strdup_printf("CH%d", i);
115 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
116 g_free(name);
9d2e5483 117 }
a4210e18 118
9d2e5483
SA
119 sdi->inst_type = SR_INST_USER;
120 sdi->conn = tcp;
a4210e18 121
8a9788e2 122 ipdbg_la_tcp_close(tcp);
a4210e18 123
9d2e5483 124 devices = g_slist_append(devices, sdi);
750303aa 125
9d2e5483 126 return std_scan_complete(di, devices);
a4210e18
EK
127}
128
129static int dev_clear(const struct sr_dev_driver *di)
130{
9d2e5483
SA
131 struct drv_context *drvc = di->context;
132 struct sr_dev_inst *sdi;
133 GSList *l;
134
135 if (drvc) {
136 for (l = drvc->instances; l; l = l->next) {
137 sdi = l->data;
8a9788e2 138 struct ipdbg_la_tcp *tcp = sdi->conn;
9d2e5483 139 if (tcp) {
8a9788e2
UH
140 ipdbg_la_tcp_close(tcp);
141 ipdbg_la_tcp_free(tcp);
9d2e5483
SA
142 g_free(tcp);
143 }
144 sdi->conn = NULL;
145 }
146 }
147
148 return std_dev_clear(di);
a4210e18
EK
149}
150
151static int dev_open(struct sr_dev_inst *sdi)
152{
8a9788e2 153 struct ipdbg_la_tcp *tcp = sdi->conn;
a4210e18 154
9d2e5483
SA
155 if (!tcp)
156 return SR_ERR;
a4210e18 157
8a9788e2 158 if (ipdbg_la_tcp_open(tcp) != SR_OK)
9d2e5483 159 return SR_ERR;
a4210e18 160
9d2e5483 161 return SR_OK;
a4210e18
EK
162}
163
164static int dev_close(struct sr_dev_inst *sdi)
165{
13ac501a 166 /* Should be called before a new call to scan(). */
8a9788e2 167 struct ipdbg_la_tcp *tcp = sdi->conn;
38e7493d 168
9d2e5483 169 if (tcp)
8a9788e2 170 ipdbg_la_tcp_close(tcp);
a4210e18 171
9d2e5483 172 sdi->conn = NULL;
a4210e18 173
9d2e5483 174 return SR_OK;
a4210e18
EK
175}
176
177static int config_get(uint32_t key, GVariant **data,
9d2e5483 178 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 179{
77b6b98d 180 struct dev_context *devc = sdi->priv;
a4210e18 181
9d2e5483 182 (void)cg;
a4210e18 183
9d2e5483
SA
184 switch (key) {
185 case SR_CONF_CAPTURE_RATIO:
186 *data = g_variant_new_uint64(devc->capture_ratio);
187 break;
188 case SR_CONF_LIMIT_SAMPLES:
189 *data = g_variant_new_uint64(devc->limit_samples);
190 break;
191 default:
77b6b98d 192 return SR_ERR_NA;
9d2e5483 193 }
a4210e18 194
77b6b98d 195 return SR_OK;
a4210e18
EK
196}
197
198static int config_set(uint32_t key, GVariant *data,
9d2e5483 199 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 200{
77b6b98d 201 struct dev_context *devc = sdi->priv;
9d2e5483
SA
202
203 (void)cg;
204
9d2e5483
SA
205 switch (key) {
206 case SR_CONF_CAPTURE_RATIO:
77b6b98d 207 devc->capture_ratio = g_variant_get_uint64(data);
9d2e5483
SA
208 break;
209 case SR_CONF_LIMIT_SAMPLES:
77b6b98d 210 devc->limit_samples = g_variant_get_uint64(data);
9d2e5483
SA
211 break;
212 default:
77b6b98d 213 return SR_ERR_NA;
9d2e5483
SA
214 }
215
77b6b98d 216 return SR_OK;
a4210e18
EK
217}
218
219static int config_list(uint32_t key, GVariant **data,
9d2e5483 220 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a4210e18 221{
9d2e5483
SA
222 switch (key) {
223 case SR_CONF_SCAN_OPTIONS:
9d2e5483 224 case SR_CONF_DEVICE_OPTIONS:
77b6b98d 225 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
9d2e5483 226 case SR_CONF_TRIGGER_MATCH:
77b6b98d 227 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
9d2e5483
SA
228 break;
229 default:
230 return SR_ERR_NA;
231 }
232
233 return SR_OK;
a4210e18
EK
234}
235
a4210e18
EK
236static int dev_acquisition_start(const struct sr_dev_inst *sdi)
237{
8a9788e2 238 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 239 struct dev_context *devc = sdi->priv;
a4210e18 240
8a9788e2
UH
241 ipdbg_la_convert_trigger(sdi);
242 ipdbg_la_send_trigger(devc, tcp);
243 ipdbg_la_send_delay(devc, tcp);
a4210e18 244
9d2e5483
SA
245 /* If the device stops sending for longer than it takes to send a byte,
246 * that means it's finished. But wait at least 100 ms to be safe.
247 */
248 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
8a9788e2 249 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
a4210e18 250
8a9788e2 251 ipdbg_la_send_start(tcp);
a4210e18 252
9d2e5483 253 return SR_OK;
a4210e18
EK
254}
255
256static int dev_acquisition_stop(struct sr_dev_inst *sdi)
257{
8a9788e2 258 struct ipdbg_la_tcp *tcp = sdi->conn;
1f15efc1 259 struct dev_context *devc = sdi->priv;
a4210e18 260
9d2e5483 261 uint8_t byte;
b51288e3 262
9d2e5483
SA
263 if (devc->num_transfers > 0) {
264 while (devc->num_transfers <
ac3625be 265 (devc->limit_samples_max * devc->data_width_bytes)) {
8a9788e2 266 ipdbg_la_tcp_receive(tcp, &byte);
9d2e5483
SA
267 devc->num_transfers++;
268 }
269 }
a4210e18 270
8a9788e2
UH
271 ipdbg_la_send_reset(tcp);
272 ipdbg_la_abort_acquisition(sdi);
a4210e18 273
9d2e5483 274 return SR_OK;
a4210e18
EK
275}
276
3f34a402 277static struct sr_dev_driver ipdbg_la_driver_info = {
8a9788e2
UH
278 .name = "ipdbg-la",
279 .longname = "IPDBG LA",
9d2e5483 280 .api_version = 1,
3831eaf9 281 .init = std_init,
9d2e5483
SA
282 .cleanup = std_cleanup,
283 .scan = scan,
3831eaf9 284 .dev_list = std_dev_list,
9d2e5483
SA
285 .dev_clear = dev_clear,
286 .config_get = config_get,
287 .config_set = config_set,
288 .config_list = config_list,
289 .dev_open = dev_open,
290 .dev_close = dev_close,
291 .dev_acquisition_start = dev_acquisition_start,
292 .dev_acquisition_stop = dev_acquisition_stop,
293 .context = NULL,
a4210e18 294};
a4210e18 295SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);