]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
sr/cli/gtk: s/capability/hwcap/.
[libsigrok.git] / hwplugin.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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 <stdlib.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <string.h>
25#include <glib.h>
b7f09cf8
UH
26#include "sigrok.h"
27#include "sigrok-internal.h"
a1bb33af 28
62c82025 29/* The list of loaded plugins lives here. */
a0ecd83b 30static GSList *plugins;
a1bb33af 31
62c82025
UH
32/*
33 * This enumerates which plugin capabilities correspond to user-settable
34 * options.
35 */
a65de030 36/* TODO: This shouldn't be a global. */
1a081ca6 37SR_API struct sr_hwcap_option sr_hwcap_options[] = {
5a2326a7
UH
38 {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
39 {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
40 {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "patternmode"},
4d436e71 41 {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
49d0ce50 42 {0, 0, NULL, NULL},
a1bb33af
UH
43};
44
a61b0e6a 45#ifdef HAVE_LA_DEMO
bb7ef793 46extern struct sr_dev_plugin demo_plugin_info;
a61b0e6a 47#endif
960a75e4 48#ifdef HAVE_LA_SALEAE_LOGIC
bb7ef793 49extern struct sr_dev_plugin saleae_logic_plugin_info;
960a75e4
UH
50#endif
51#ifdef HAVE_LA_OLS
bb7ef793 52extern struct sr_dev_plugin ols_plugin_info;
960a75e4
UH
53#endif
54#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
bb7ef793 55extern struct sr_dev_plugin zeroplus_logic_cube_plugin_info;
960a75e4 56#endif
5b907f9b 57#ifdef HAVE_LA_ASIX_SIGMA
bb7ef793 58extern struct sr_dev_plugin asix_sigma_plugin_info;
5b907f9b 59#endif
f4314d7e 60#ifdef HAVE_LA_CHRONOVU_LA8
bb7ef793 61extern SR_PRIV struct dev_plugin chronovu_la8_plugin_info;
f4314d7e 62#endif
bffed4fc 63#ifdef HAVE_LA_LINK_MSO19
bb7ef793 64extern struct sr_dev_plugin link_mso19_plugin_info;
bffed4fc 65#endif
6ed4f044 66#ifdef HAVE_LA_ALSA
bb7ef793 67extern struct sr_dev_plugin alsa_plugin_info;
6ed4f044
DR
68#endif
69
49d0ce50 70/* TODO: No linked list needed, this can be a simple array. */
93a04e3b 71SR_PRIV int sr_hw_load_all(void)
a1bb33af 72{
a61b0e6a 73#ifdef HAVE_LA_DEMO
6239c175 74 plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
a61b0e6a 75#endif
960a75e4 76#ifdef HAVE_LA_SALEAE_LOGIC
62c82025 77 plugins =
49d0ce50 78 g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
960a75e4
UH
79#endif
80#ifdef HAVE_LA_OLS
49d0ce50 81 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
960a75e4
UH
82#endif
83#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
62c82025 84 plugins = g_slist_append(plugins,
49d0ce50 85 (gpointer *)&zeroplus_logic_cube_plugin_info);
960a75e4 86#endif
5b907f9b 87#ifdef HAVE_LA_ASIX_SIGMA
28a35d8a 88 plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
5b907f9b 89#endif
f4314d7e
UH
90#ifdef HAVE_LA_CHRONOVU_LA8
91 plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
92#endif
bffed4fc
UH
93#ifdef HAVE_LA_LINK_MSO19
94 plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
95#endif
6ed4f044
DR
96#ifdef HAVE_LA_ALSA
97 plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
98#endif
99
e46b8fb1 100 return SR_OK;
a1bb33af
UH
101}
102
a1645fcd 103/**
44dae539 104 * Return the list of loaded hardware plugins.
a1645fcd
BV
105 *
106 * The list of plugins is initialized from sr_init(), and can only be reset
107 * by calling sr_exit().
108 *
109 * @return A GSList of pointers to loaded plugins.
110 */
93a04e3b 111SR_API GSList *sr_hw_list(void)
a1bb33af 112{
a1bb33af
UH
113 return plugins;
114}
115
a1645fcd
BV
116/**
117 * Initialize a hardware plugin.
118 *
119 * The specified plugin is initialized, and all devices discovered by the
120 * plugin are instantiated.
121 *
122 * @param plugin The plugin to initialize.
123 *
124 * @return The number of devices found and instantiated by the plugin.
125 */
bb7ef793 126SR_API int sr_hw_init(struct sr_dev_plugin *plugin)
8722c31e 127{
bb7ef793
UH
128 int num_devs, num_probes, i, j;
129 int num_initialized_devs = 0;
130 struct sr_dev *dev;
464d12c7 131 char **probe_names;
8722c31e 132
8f81fe87 133 sr_dbg("initializing %s plugin", plugin->name);
bb7ef793
UH
134 num_devs = plugin->init(NULL);
135 for (i = 0; i < num_devs; i++) {
4d436e71 136 num_probes = GPOINTER_TO_INT(
bb7ef793
UH
137 plugin->get_dev_info(i, SR_DI_NUM_PROBES));
138 probe_names = (char **)plugin->get_dev_info(i,
c37d2b1b 139 SR_DI_PROBE_NAMES);
464d12c7
KS
140
141 if (!probe_names) {
c37d2b1b
UH
142 sr_warn("hwplugin: %s: plugin %s does not return a "
143 "list of probe names", __func__, plugin->name);
464d12c7
KS
144 continue;
145 }
146
bb7ef793 147 dev = sr_dev_new(plugin, i);
c37d2b1b 148 for (j = 0; j < num_probes; j++)
bb7ef793
UH
149 sr_dev_probe_add(dev, probe_names[j]);
150 num_initialized_devs++;
8722c31e
BV
151 }
152
bb7ef793 153 return num_initialized_devs;
8722c31e
BV
154}
155
93a04e3b 156SR_PRIV void sr_hw_cleanup_all(void)
8722c31e 157{
bb7ef793 158 struct sr_dev_plugin *plugin;
8722c31e
BV
159 GSList *l;
160
161 for (l = plugins; l; l = l->next) {
162 plugin = l->data;
163 if (plugin->cleanup)
164 plugin->cleanup();
165 }
8722c31e
BV
166}
167
d68e2d1a 168SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
49d0ce50 169 const char *vendor, const char *model, const char *version)
a1bb33af 170{
d68e2d1a 171 struct sr_dev_inst *sdi;
a1bb33af 172
d68e2d1a 173 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
133a37bf 174 sr_err("hwplugin: %s: sdi malloc failed", __func__);
a1bb33af 175 return NULL;
133a37bf 176 }
a1bb33af
UH
177
178 sdi->index = index;
179 sdi->status = status;
1d9a8a5f 180 sdi->inst_type = -1;
8722c31e
BV
181 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
182 sdi->model = model ? g_strdup(model) : NULL;
183 sdi->version = version ? g_strdup(version) : NULL;
01cf8814 184 sdi->priv = NULL;
a1bb33af
UH
185
186 return sdi;
187}
188
bb7ef793 189SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
a1bb33af 190{
d68e2d1a 191 struct sr_dev_inst *sdi;
a1bb33af
UH
192 GSList *l;
193
d68e2d1a
UH
194 for (l = dev_insts; l; l = l->next) {
195 sdi = (struct sr_dev_inst *)(l->data);
bb7ef793 196 if (sdi->index == dev_index)
a1bb33af
UH
197 return sdi;
198 }
bb7ef793 199 sr_warn("could not find device index %d instance", dev_index);
a1bb33af
UH
200
201 return NULL;
202}
203
d68e2d1a 204SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
a1bb33af 205{
66410a86 206 g_free(sdi->priv);
8722c31e
BV
207 g_free(sdi->vendor);
208 g_free(sdi->model);
209 g_free(sdi->version);
210 g_free(sdi);
a1bb33af
UH
211}
212
22b02383
UH
213#ifdef HAVE_LIBUSB_1_0
214
d68e2d1a 215SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
62c82025 216 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af 217{
d68e2d1a 218 struct sr_usb_dev_inst *udi;
a1bb33af 219
d68e2d1a 220 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
133a37bf 221 sr_err("hwplugin: %s: udi malloc failed", __func__);
a1bb33af 222 return NULL;
133a37bf 223 }
a1bb33af
UH
224
225 udi->bus = bus;
226 udi->address = address;
49d0ce50 227 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
a1bb33af
UH
228
229 return udi;
230}
231
d68e2d1a 232SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
a1bb33af 233{
17e1afcb 234 /* Avoid compiler warnings. */
cb93f8a9 235 (void)usb;
a1bb33af 236
62c82025 237 /* Nothing to do for this device instance type. */
a1bb33af
UH
238}
239
22b02383
UH
240#endif
241
d68e2d1a
UH
242SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
243 int fd)
a1bb33af 244{
d68e2d1a 245 struct sr_serial_dev_inst *serial;
a1bb33af 246
d68e2d1a 247 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
133a37bf 248 sr_err("hwplugin: %s: serial malloc failed", __func__);
a1bb33af 249 return NULL;
133a37bf 250 }
a1bb33af 251
133a37bf 252 serial->port = g_strdup(port);
a1bb33af
UH
253 serial->fd = fd;
254
255 return serial;
256}
257
d68e2d1a 258SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
a1bb33af 259{
133a37bf 260 g_free(serial->port);
a1bb33af
UH
261}
262
a1645fcd 263/**
da1466d6 264 * Find out if a hardware plugin has a specific capability.
a1645fcd 265 *
44dae539 266 * @param plugin The hardware plugin in which to search for the capability.
a1645fcd
BV
267 * @param hwcap The capability to find in the list.
268 *
d3683c42 269 * @return TRUE if found, FALSE otherwise.
a1645fcd 270 */
bb7ef793 271SR_API gboolean sr_hw_has_hwcap(struct sr_dev_plugin *plugin, int hwcap)
a1bb33af 272{
ffedd0bf 273 int *hwcaps, i;
a1bb33af 274
ffedd0bf
UH
275 hwcaps = plugin->hwcap_get_all();
276 for (i = 0; hwcaps[i]; i++) {
277 if (hwcaps[i] == hwcap)
a1bb33af 278 return TRUE;
49d0ce50 279 }
a1bb33af
UH
280
281 return FALSE;
282}
283
a1645fcd 284/**
93a04e3b 285 * Get a hardware plugin capability option.
a1645fcd 286 *
93a04e3b 287 * @param hwcap The capability to get.
a1645fcd 288 *
44dae539
UH
289 * @return A pointer to a struct with information about the parameter, or NULL
290 * if the capability was not found.
a1645fcd 291 */
93a04e3b 292SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
a1bb33af 293{
a1bb33af
UH
294 int i;
295
ffedd0bf
UH
296 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
297 if (sr_hwcap_options[i].hwcap == hwcap)
a65de030 298 return &sr_hwcap_options[i];
a1bb33af
UH
299 }
300
49d0ce50 301 return NULL;
a1bb33af
UH
302}
303
544a4582
BV
304/* unnecessary level of indirection follows. */
305
bb08ee2e 306SR_PRIV void sr_source_remove(int fd)
a1bb33af 307{
8a2efef2 308 sr_session_source_remove(fd);
a1bb33af
UH
309}
310
bb08ee2e 311SR_PRIV void sr_source_add(int fd, int events, int timeout,
a887e3da 312 sr_receive_data_callback rcv_cb, void *user_data)
a1bb33af 313{
8a2efef2 314 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
a1bb33af 315}