]> sigrok.org Git - libsigrok.git/blame - device.c
LA8: free() sample buffers in hw_closedev().
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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 <stdio.h>
21#include <glib.h>
1b452b85 22#include <sigrok.h>
b08024a8 23#include <sigrok-internal.h>
a1bb33af 24
a00ba012 25extern struct sr_global *global;
a1bb33af
UH
26
27GSList *devices = NULL;
28
2bf4aca6 29void sr_device_scan(void)
a1bb33af
UH
30{
31 GSList *plugins, *l;
5c2d46d1 32 struct sr_device_plugin *plugin;
a1bb33af 33
ee4b6342 34 plugins = sr_list_hwplugins();
a1bb33af 35
1b452b85
UH
36 /*
37 * Initialize all plugins first. Since the init() call may involve
a1bb33af
UH
38 * a firmware upload and associated delay, we may as well get all
39 * of these out of the way first.
40 */
1b452b85 41 for (l = plugins; l; l = l->next) {
a1bb33af 42 plugin = l->data;
2bf4aca6 43 sr_device_plugin_init(plugin);
a1bb33af 44 }
e54bcdc5
BV
45
46}
47
2bf4aca6 48int sr_device_plugin_init(struct sr_device_plugin *plugin)
e54bcdc5
BV
49{
50 int num_devices, num_probes, i;
51
b08024a8 52 sr_info("initializing %s plugin", plugin->name);
e54bcdc5
BV
53 num_devices = plugin->init(NULL);
54 for (i = 0; i < num_devices; i++) {
5a2326a7 55 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
2bf4aca6 56 sr_device_new(plugin, i, num_probes);
e54bcdc5
BV
57 }
58
59 return num_devices;
a1bb33af
UH
60}
61
2bf4aca6 62void sr_device_close_all(void)
a1bb33af 63{
5c2d46d1 64 struct sr_device *device;
a1bb33af 65
1b452b85 66 while (devices) {
a1bb33af 67 device = devices->data;
b8c2f85f 68 if (device->plugin && device->plugin->close)
873080cc 69 device->plugin->close(device->plugin_index);
2bf4aca6 70 sr_device_destroy(device);
a1bb33af 71 }
a1bb33af
UH
72}
73
2bf4aca6 74GSList *sr_device_list(void)
a1bb33af 75{
e54bcdc5
BV
76
77 if (!devices)
2bf4aca6 78 sr_device_scan();
e54bcdc5 79
a1bb33af
UH
80 return devices;
81}
82
2bf4aca6 83struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
5c2d46d1 84 int num_probes)
a1bb33af 85{
5c2d46d1 86 struct sr_device *device;
873080cc 87 int i;
a1bb33af 88
5c2d46d1 89 device = g_malloc0(sizeof(struct sr_device));
a1bb33af
UH
90 device->plugin = plugin;
91 device->plugin_index = plugin_index;
92 devices = g_slist_append(devices, device);
93
7d658874 94 for (i = 0; i < num_probes; i++)
2bf4aca6 95 sr_device_probe_add(device, NULL);
a1bb33af
UH
96
97 return device;
98}
99
2bf4aca6 100void sr_device_clear(struct sr_device *device)
a1bb33af 101{
1b452b85 102 unsigned int pnum;
a1bb33af 103
1b452b85 104 /* TODO: Plugin-specific clear call? */
a1bb33af 105
1b452b85
UH
106 if (!device->probes)
107 return;
a1bb33af 108
1b452b85 109 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 110 sr_device_probe_clear(device, pnum);
a1bb33af
UH
111}
112
2bf4aca6 113void sr_device_destroy(struct sr_device *device)
a1bb33af 114{
1b452b85 115 unsigned int pnum;
a1bb33af 116
1b452b85
UH
117 /*
118 * TODO: Plugin-specific destroy call, need to decrease refcount
119 * in plugin.
120 */
a1bb33af
UH
121
122 devices = g_slist_remove(devices, device);
1b452b85
UH
123 if (device->probes) {
124 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 125 sr_device_probe_clear(device, pnum);
a1bb33af
UH
126 g_slist_free(device->probes);
127 }
128 g_free(device);
a1bb33af
UH
129}
130
2bf4aca6 131void sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 132{
1afe8989 133 struct sr_probe *p;
a1bb33af 134
03dbc020 135 p = sr_device_probe_find(device, probenum);
1b452b85 136 if (!p)
a1bb33af
UH
137 return;
138
1b452b85 139 if (p->name) {
a1bb33af
UH
140 g_free(p->name);
141 p->name = NULL;
142 }
143
1b452b85 144 if (p->trigger) {
a1bb33af
UH
145 g_free(p->trigger);
146 p->trigger = NULL;
147 }
a1bb33af
UH
148}
149
8225e921 150void sr_device_probe_add(struct sr_device *device, const char *name)
a1bb33af 151{
1afe8989 152 struct sr_probe *p;
7d658874
BV
153 char probename[16];
154 int probenum;
a1bb33af 155
7d658874 156 probenum = g_slist_length(device->probes) + 1;
1afe8989 157 p = g_malloc0(sizeof(struct sr_probe));
7d658874 158 p->index = probenum;
a1bb33af 159 p->enabled = TRUE;
7d658874
BV
160 if (name) {
161 p->name = g_strdup(name);
162 } else {
163 snprintf(probename, 16, "%d", probenum);
164 p->name = g_strdup(probename);
165 }
a1bb33af
UH
166 p->trigger = NULL;
167 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
168}
169
03dbc020 170struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
a1bb33af
UH
171{
172 GSList *l;
1afe8989 173 struct sr_probe *p, *found_probe;
a1bb33af
UH
174
175 found_probe = NULL;
1b452b85 176 for (l = device->probes; l; l = l->next) {
a1bb33af 177 p = l->data;
1b452b85 178 if (p->index == probenum) {
a1bb33af
UH
179 found_probe = p;
180 break;
181 }
182 }
183
184 return found_probe;
185}
186
7d658874 187/* TODO: return SIGROK_ERR if probenum not found */
8225e921
UH
188void sr_device_probe_name(struct sr_device *device, int probenum,
189 const char *name)
a1bb33af 190{
1afe8989 191 struct sr_probe *p;
a1bb33af 192
03dbc020 193 p = sr_device_probe_find(device, probenum);
1b452b85 194 if (!p)
a1bb33af
UH
195 return;
196
1b452b85 197 if (p->name)
a1bb33af
UH
198 g_free(p->name);
199 p->name = g_strdup(name);
a1bb33af
UH
200}
201
7d658874 202/* TODO: return SIGROK_ERR if probenum not found */
2bf4aca6 203void sr_device_trigger_clear(struct sr_device *device)
a1bb33af 204{
1afe8989 205 struct sr_probe *p;
1b452b85 206 unsigned int pnum;
a1bb33af 207
1b452b85
UH
208 if (!device->probes)
209 return;
a1bb33af 210
1b452b85 211 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 212 p = sr_device_probe_find(device, pnum);
1b452b85
UH
213 if (p && p->trigger) {
214 g_free(p->trigger);
215 p->trigger = NULL;
216 }
217 }
218}
a1bb33af 219
7d658874 220/* TODO: return SIGROK_ERR if probenum not found */
8225e921
UH
221void sr_device_trigger_set(struct sr_device *device, int probenum,
222 const char *trigger)
a1bb33af 223{
1afe8989 224 struct sr_probe *p;
a1bb33af 225
03dbc020 226 p = sr_device_probe_find(device, probenum);
1b452b85 227 if (!p)
a1bb33af
UH
228 return;
229
1b452b85 230 if (p->trigger)
a1bb33af
UH
231 g_free(p->trigger);
232
233 p->trigger = g_strdup(trigger);
7d658874
BV
234
235}
236
2bf4aca6 237gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
7d658874
BV
238{
239 int *capabilities, i;
240
218557b8 241 if (!device || !device->plugin)
f437ea3f 242 return FALSE;
218557b8 243
7d658874
BV
244 if ((capabilities = device->plugin->get_capabilities()))
245 for (i = 0; capabilities[i]; i++)
246 if (capabilities[i] == hwcap)
247 return TRUE;
248
249 return FALSE;
a1bb33af 250}