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