]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
Move the probe naming to the creator of the device, and let each driver name its...
[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>
b7f09cf8
UH
27#include "sigrok.h"
28#include "sigrok-internal.h"
a1bb33af 29
62c82025 30/* The list of loaded plugins lives here. */
a0ecd83b 31static GSList *plugins;
a1bb33af 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
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
e46b8fb1 101 return SR_OK;
a1bb33af
UH
102}
103
ee4b6342 104GSList *sr_list_hwplugins(void)
a1bb33af 105{
a1bb33af
UH
106 return plugins;
107}
108
8722c31e
BV
109int sr_init_hwplugins(struct sr_device_plugin *plugin)
110{
464d12c7
KS
111 int num_devices, num_probes, i, j;
112 int num_initialized_devices = 0;
113 struct sr_device *device;
114 char **probe_names;
8722c31e
BV
115
116 g_message("initializing %s plugin", plugin->name);
117 num_devices = plugin->init(NULL);
118 for (i = 0; i < num_devices; i++) {
4d436e71
GM
119 num_probes = GPOINTER_TO_INT(
120 plugin->get_device_info(i, SR_DI_NUM_PROBES));
464d12c7
KS
121 probe_names = (char**)plugin->get_device_info(i, SR_DI_PROBE_NAMES);
122
123 if (!probe_names) {
124 sr_warn("Plugin %s does not return a list of probe names.", plugin->name);
125 continue;
126 }
127
128 device = sr_device_new(plugin, i);
129 for (j = 0; j < num_probes; j++) {
130 sr_device_probe_add(device, probe_names[j]);
131 }
132 num_initialized_devices++;
8722c31e
BV
133 }
134
464d12c7 135 return num_initialized_devices;
8722c31e
BV
136}
137
138void sr_cleanup_hwplugins(void)
139{
140 struct sr_device_plugin *plugin;
141 GSList *l;
142
143 for (l = plugins; l; l = l->next) {
144 plugin = l->data;
145 if (plugin->cleanup)
146 plugin->cleanup();
147 }
8722c31e
BV
148}
149
a00ba012 150struct sr_device_instance *sr_device_instance_new(int index, int status,
49d0ce50 151 const char *vendor, const char *model, const char *version)
a1bb33af 152{
a00ba012 153 struct sr_device_instance *sdi;
a1bb33af 154
8722c31e 155 if (!(sdi = g_malloc(sizeof(struct sr_device_instance))))
a1bb33af
UH
156 return NULL;
157
158 sdi->index = index;
159 sdi->status = status;
160 sdi->instance_type = -1;
8722c31e
BV
161 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
162 sdi->model = model ? g_strdup(model) : NULL;
163 sdi->version = version ? g_strdup(version) : NULL;
01cf8814 164 sdi->priv = NULL;
a1bb33af
UH
165 sdi->usb = NULL;
166
167 return sdi;
168}
169
d32d961d
UH
170struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
171 int device_index)
a1bb33af 172{
a00ba012 173 struct sr_device_instance *sdi;
a1bb33af
UH
174 GSList *l;
175
62c82025 176 for (l = device_instances; l; l = l->next) {
a00ba012 177 sdi = (struct sr_device_instance *)(l->data);
62c82025 178 if (sdi->index == device_index)
a1bb33af
UH
179 return sdi;
180 }
b08024a8 181 sr_warn("could not find device index %d instance", device_index);
a1bb33af
UH
182
183 return NULL;
184}
185
a00ba012 186void sr_device_instance_free(struct sr_device_instance *sdi)
a1bb33af 187{
62c82025 188 switch (sdi->instance_type) {
22b02383 189#ifdef HAVE_LIBUSB_1_0
5a2326a7 190 case SR_USB_INSTANCE:
6c290072 191 sr_usb_device_instance_free(sdi->usb);
a1bb33af 192 break;
22b02383 193#endif
5a2326a7 194 case SR_SERIAL_INSTANCE:
6c290072 195 sr_serial_device_instance_free(sdi->serial);
a1bb33af 196 break;
49d0ce50 197 default:
62c82025 198 /* No specific type, nothing extra to free. */
49d0ce50 199 break;
a1bb33af
UH
200 }
201
8722c31e
BV
202 if (sdi->priv)
203 g_free(sdi->priv);
204
205 g_free(sdi->vendor);
206 g_free(sdi->model);
207 g_free(sdi->version);
208 g_free(sdi);
a1bb33af
UH
209}
210
22b02383
UH
211#ifdef HAVE_LIBUSB_1_0
212
6c290072 213struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
62c82025 214 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af 215{
6c290072 216 struct sr_usb_device_instance *udi;
a1bb33af 217
6c290072 218 if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
a1bb33af
UH
219 return NULL;
220
221 udi->bus = bus;
222 udi->address = address;
49d0ce50 223 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
a1bb33af
UH
224
225 return udi;
226}
227
6c290072 228void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
a1bb33af 229{
17e1afcb 230 /* Avoid compiler warnings. */
cb93f8a9 231 (void)usb;
a1bb33af 232
62c82025 233 /* Nothing to do for this device instance type. */
a1bb33af
UH
234}
235
22b02383
UH
236#endif
237
6c290072 238struct sr_serial_device_instance *sr_serial_device_instance_new(
49d0ce50 239 const char *port, int fd)
a1bb33af 240{
6c290072 241 struct sr_serial_device_instance *serial;
a1bb33af 242
6c290072 243 if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
a1bb33af
UH
244 return NULL;
245
246 serial->port = strdup(port);
247 serial->fd = fd;
248
249 return serial;
250}
251
6c290072 252void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
a1bb33af 253{
a1bb33af 254 free(serial->port);
a1bb33af
UH
255}
256
a65de030 257int sr_find_hwcap(int *capabilities, int hwcap)
a1bb33af
UH
258{
259 int i;
260
49d0ce50 261 for (i = 0; capabilities[i]; i++) {
62c82025 262 if (capabilities[i] == hwcap)
a1bb33af 263 return TRUE;
49d0ce50 264 }
a1bb33af
UH
265
266 return FALSE;
267}
268
a65de030 269struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
a1bb33af 270{
a1bb33af
UH
271 int i;
272
a65de030
UH
273 for (i = 0; sr_hwcap_options[i].capability; i++) {
274 if (sr_hwcap_options[i].capability == hwcap)
275 return &sr_hwcap_options[i];
a1bb33af
UH
276 }
277
49d0ce50 278 return NULL;
a1bb33af
UH
279}
280
544a4582
BV
281/* unnecessary level of indirection follows. */
282
6f1be0a2 283void sr_source_remove(int fd)
a1bb33af 284{
8a2efef2 285 sr_session_source_remove(fd);
a1bb33af
UH
286}
287
6f1be0a2 288void sr_source_add(int fd, int events, int timeout,
a887e3da 289 sr_receive_data_callback rcv_cb, void *user_data)
a1bb33af 290{
8a2efef2 291 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
a1bb33af 292}