]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
better cleanup of device/plugin resources
[libsigrok.git] / hwplugin.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
22b02383 20#include "config.h"
a1bb33af
UH
21#include <stdlib.h>
22#include <stdio.h>
23#include <sys/types.h>
24#include <dirent.h>
25#include <string.h>
26#include <glib.h>
62c82025 27#include <sigrok.h>
b08024a8 28#include <sigrok-internal.h>
a1bb33af 29
62c82025 30/* The list of loaded plugins lives here. */
a1bb33af
UH
31GSList *plugins;
32
62c82025
UH
33/*
34 * This enumerates which plugin capabilities correspond to user-settable
35 * options.
36 */
a65de030
UH
37/* TODO: This shouldn't be a global. */
38struct sr_hwcap_option sr_hwcap_options[] = {
5a2326a7
UH
39 {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
40 {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
41 {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "patternmode"},
49d0ce50 42 {0, 0, NULL, NULL},
a1bb33af
UH
43};
44
a61b0e6a 45#ifdef HAVE_LA_DEMO
5c2d46d1 46extern struct sr_device_plugin demo_plugin_info;
a61b0e6a 47#endif
960a75e4 48#ifdef HAVE_LA_SALEAE_LOGIC
5c2d46d1 49extern struct sr_device_plugin saleae_logic_plugin_info;
960a75e4
UH
50#endif
51#ifdef HAVE_LA_OLS
5c2d46d1 52extern struct sr_device_plugin ols_plugin_info;
960a75e4
UH
53#endif
54#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
5c2d46d1 55extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
960a75e4 56#endif
5b907f9b 57#ifdef HAVE_LA_ASIX_SIGMA
5c2d46d1 58extern struct sr_device_plugin asix_sigma_plugin_info;
5b907f9b 59#endif
f4314d7e
UH
60#ifdef HAVE_LA_CHRONOVU_LA8
61extern struct device_plugin chronovu_la8_plugin_info;
62#endif
bffed4fc 63#ifdef HAVE_LA_LINK_MSO19
5c2d46d1 64extern struct sr_device_plugin link_mso19_plugin_info;
bffed4fc 65#endif
6ed4f044 66#ifdef HAVE_LA_ALSA
5c2d46d1 67extern struct sr_device_plugin alsa_plugin_info;
6ed4f044
DR
68#endif
69
01cf8814 70
49d0ce50 71/* TODO: No linked list needed, this can be a simple array. */
a1bb33af
UH
72int load_hwplugins(void)
73{
a61b0e6a 74#ifdef HAVE_LA_DEMO
6239c175 75 plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
a61b0e6a 76#endif
960a75e4 77#ifdef HAVE_LA_SALEAE_LOGIC
62c82025 78 plugins =
49d0ce50 79 g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
960a75e4
UH
80#endif
81#ifdef HAVE_LA_OLS
49d0ce50 82 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
960a75e4
UH
83#endif
84#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
62c82025 85 plugins = g_slist_append(plugins,
49d0ce50 86 (gpointer *)&zeroplus_logic_cube_plugin_info);
960a75e4 87#endif
5b907f9b 88#ifdef HAVE_LA_ASIX_SIGMA
28a35d8a 89 plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
5b907f9b 90#endif
f4314d7e
UH
91#ifdef HAVE_LA_CHRONOVU_LA8
92 plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
93#endif
bffed4fc
UH
94#ifdef HAVE_LA_LINK_MSO19
95 plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
96#endif
6ed4f044
DR
97#ifdef HAVE_LA_ALSA
98 plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
99#endif
100
a1bb33af 101
e46b8fb1 102 return SR_OK;
a1bb33af
UH
103}
104
ee4b6342 105GSList *sr_list_hwplugins(void)
a1bb33af 106{
a1bb33af
UH
107 return plugins;
108}
109
8722c31e
BV
110int sr_init_hwplugins(struct sr_device_plugin *plugin)
111{
112 int num_devices, num_probes, i;
113
114 g_message("initializing %s plugin", plugin->name);
115 num_devices = plugin->init(NULL);
116 for (i = 0; i < num_devices; i++) {
117 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
118 sr_device_new(plugin, i, num_probes);
119 }
120
121 return num_devices;
122}
123
124void sr_cleanup_hwplugins(void)
125{
126 struct sr_device_plugin *plugin;
127 GSList *l;
128
129 for (l = plugins; l; l = l->next) {
130 plugin = l->data;
131 if (plugin->cleanup)
132 plugin->cleanup();
133 }
134
135}
136
a00ba012 137struct sr_device_instance *sr_device_instance_new(int index, int status,
49d0ce50 138 const char *vendor, const char *model, const char *version)
a1bb33af 139{
a00ba012 140 struct sr_device_instance *sdi;
a1bb33af 141
8722c31e 142 if (!(sdi = g_malloc(sizeof(struct sr_device_instance))))
a1bb33af
UH
143 return NULL;
144
145 sdi->index = index;
146 sdi->status = status;
147 sdi->instance_type = -1;
8722c31e
BV
148 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
149 sdi->model = model ? g_strdup(model) : NULL;
150 sdi->version = version ? g_strdup(version) : NULL;
01cf8814 151 sdi->priv = NULL;
a1bb33af
UH
152 sdi->usb = NULL;
153
154 return sdi;
155}
156
d32d961d
UH
157struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
158 int device_index)
a1bb33af 159{
a00ba012 160 struct sr_device_instance *sdi;
a1bb33af
UH
161 GSList *l;
162
62c82025 163 for (l = device_instances; l; l = l->next) {
a00ba012 164 sdi = (struct sr_device_instance *)(l->data);
62c82025 165 if (sdi->index == device_index)
a1bb33af
UH
166 return sdi;
167 }
b08024a8 168 sr_warn("could not find device index %d instance", device_index);
a1bb33af
UH
169
170 return NULL;
171}
172
a00ba012 173void sr_device_instance_free(struct sr_device_instance *sdi)
a1bb33af 174{
62c82025 175 switch (sdi->instance_type) {
22b02383 176#ifdef HAVE_LIBUSB_1_0
5a2326a7 177 case SR_USB_INSTANCE:
6c290072 178 sr_usb_device_instance_free(sdi->usb);
a1bb33af 179 break;
22b02383 180#endif
5a2326a7 181 case SR_SERIAL_INSTANCE:
6c290072 182 sr_serial_device_instance_free(sdi->serial);
a1bb33af 183 break;
49d0ce50 184 default:
62c82025 185 /* No specific type, nothing extra to free. */
49d0ce50 186 break;
a1bb33af
UH
187 }
188
8722c31e
BV
189 if (sdi->priv)
190 g_free(sdi->priv);
191
192 g_free(sdi->vendor);
193 g_free(sdi->model);
194 g_free(sdi->version);
195 g_free(sdi);
196
a1bb33af
UH
197}
198
22b02383
UH
199#ifdef HAVE_LIBUSB_1_0
200
6c290072 201struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
62c82025 202 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af 203{
6c290072 204 struct sr_usb_device_instance *udi;
a1bb33af 205
6c290072 206 if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
a1bb33af
UH
207 return NULL;
208
209 udi->bus = bus;
210 udi->address = address;
49d0ce50 211 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
a1bb33af
UH
212
213 return udi;
214}
215
6c290072 216void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
a1bb33af 217{
17e1afcb 218 /* Avoid compiler warnings. */
afc8e4de 219 usb = usb;
a1bb33af 220
62c82025 221 /* Nothing to do for this device instance type. */
a1bb33af
UH
222}
223
22b02383
UH
224#endif
225
6c290072 226struct sr_serial_device_instance *sr_serial_device_instance_new(
49d0ce50 227 const char *port, int fd)
a1bb33af 228{
6c290072 229 struct sr_serial_device_instance *serial;
a1bb33af 230
6c290072 231 if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
a1bb33af
UH
232 return NULL;
233
234 serial->port = strdup(port);
235 serial->fd = fd;
236
237 return serial;
238}
239
6c290072 240void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
a1bb33af 241{
a1bb33af 242 free(serial->port);
a1bb33af
UH
243}
244
a65de030 245int sr_find_hwcap(int *capabilities, int hwcap)
a1bb33af
UH
246{
247 int i;
248
49d0ce50 249 for (i = 0; capabilities[i]; i++) {
62c82025 250 if (capabilities[i] == hwcap)
a1bb33af 251 return TRUE;
49d0ce50 252 }
a1bb33af
UH
253
254 return FALSE;
255}
256
a65de030 257struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
a1bb33af 258{
a1bb33af
UH
259 int i;
260
a65de030
UH
261 for (i = 0; sr_hwcap_options[i].capability; i++) {
262 if (sr_hwcap_options[i].capability == hwcap)
263 return &sr_hwcap_options[i];
a1bb33af
UH
264 }
265
49d0ce50 266 return NULL;
a1bb33af
UH
267}
268
544a4582
BV
269/* unnecessary level of indirection follows. */
270
6f1be0a2 271void sr_source_remove(int fd)
a1bb33af 272{
8a2efef2 273 sr_session_source_remove(fd);
a1bb33af
UH
274}
275
6f1be0a2 276void sr_source_add(int fd, int events, int timeout,
a887e3da 277 sr_receive_data_callback rcv_cb, void *user_data)
a1bb33af 278{
8a2efef2 279 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
a1bb33af 280}