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