]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
sr: adjust copyright year
[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
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 60#ifdef HAVE_LA_CHRONOVU_LA8
ca070ed9 61extern SR_PRIV struct device_plugin chronovu_la8_plugin_info;
f4314d7e 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
49d0ce50 70/* TODO: No linked list needed, this can be a simple array. */
1a081ca6 71SR_PRIV int load_hwplugins(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
BV
103/**
104 * Returns the list of loaded hardware plugins.
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 */
1a081ca6 111SR_API GSList *sr_list_hwplugins(void)
a1bb33af 112{
a1645fcd 113
a1bb33af
UH
114 return plugins;
115}
116
a1645fcd
BV
117/**
118 * Initialize a hardware plugin.
119 *
120 * The specified plugin is initialized, and all devices discovered by the
121 * plugin are instantiated.
122 *
123 * @param plugin The plugin to initialize.
124 *
125 * @return The number of devices found and instantiated by the plugin.
126 */
127SR_API int sr_init_hwplugin(struct sr_device_plugin *plugin)
8722c31e 128{
464d12c7
KS
129 int num_devices, num_probes, i, j;
130 int num_initialized_devices = 0;
131 struct sr_device *device;
132 char **probe_names;
8722c31e 133
8f81fe87 134 sr_dbg("initializing %s plugin", plugin->name);
8722c31e
BV
135 num_devices = plugin->init(NULL);
136 for (i = 0; i < num_devices; i++) {
4d436e71
GM
137 num_probes = GPOINTER_TO_INT(
138 plugin->get_device_info(i, SR_DI_NUM_PROBES));
c37d2b1b
UH
139 probe_names = (char **)plugin->get_device_info(i,
140 SR_DI_PROBE_NAMES);
464d12c7
KS
141
142 if (!probe_names) {
c37d2b1b
UH
143 sr_warn("hwplugin: %s: plugin %s does not return a "
144 "list of probe names", __func__, plugin->name);
464d12c7
KS
145 continue;
146 }
147
03168500 148 device = sr_dev_new(plugin, i);
c37d2b1b 149 for (j = 0; j < num_probes; j++)
03168500 150 sr_dev_probe_add(device, probe_names[j]);
464d12c7 151 num_initialized_devices++;
8722c31e
BV
152 }
153
464d12c7 154 return num_initialized_devices;
8722c31e
BV
155}
156
bb08ee2e 157SR_PRIV void sr_cleanup_hwplugins(void)
8722c31e
BV
158{
159 struct sr_device_plugin *plugin;
160 GSList *l;
161
162 for (l = plugins; l; l = l->next) {
163 plugin = l->data;
164 if (plugin->cleanup)
165 plugin->cleanup();
166 }
8722c31e
BV
167}
168
bb08ee2e 169SR_PRIV struct sr_device_instance *sr_device_instance_new(int index, int status,
49d0ce50 170 const char *vendor, const char *model, const char *version)
a1bb33af 171{
a00ba012 172 struct sr_device_instance *sdi;
a1bb33af 173
133a37bf
UH
174 if (!(sdi = g_try_malloc(sizeof(struct sr_device_instance)))) {
175 sr_err("hwplugin: %s: sdi malloc failed", __func__);
a1bb33af 176 return NULL;
133a37bf 177 }
a1bb33af
UH
178
179 sdi->index = index;
180 sdi->status = status;
181 sdi->instance_type = -1;
8722c31e
BV
182 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
183 sdi->model = model ? g_strdup(model) : NULL;
184 sdi->version = version ? g_strdup(version) : NULL;
01cf8814 185 sdi->priv = NULL;
a1bb33af
UH
186
187 return sdi;
188}
189
bb08ee2e 190SR_PRIV struct sr_device_instance *sr_get_device_instance(
1a081ca6 191 GSList *device_instances, int device_index)
a1bb33af 192{
a00ba012 193 struct sr_device_instance *sdi;
a1bb33af
UH
194 GSList *l;
195
62c82025 196 for (l = device_instances; l; l = l->next) {
a00ba012 197 sdi = (struct sr_device_instance *)(l->data);
62c82025 198 if (sdi->index == device_index)
a1bb33af
UH
199 return sdi;
200 }
b08024a8 201 sr_warn("could not find device index %d instance", device_index);
a1bb33af
UH
202
203 return NULL;
204}
205
bb08ee2e 206SR_PRIV void sr_device_instance_free(struct sr_device_instance *sdi)
a1bb33af 207{
66410a86 208 g_free(sdi->priv);
8722c31e
BV
209 g_free(sdi->vendor);
210 g_free(sdi->model);
211 g_free(sdi->version);
212 g_free(sdi);
a1bb33af
UH
213}
214
22b02383
UH
215#ifdef HAVE_LIBUSB_1_0
216
1a081ca6 217SR_PRIV struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
62c82025 218 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af 219{
6c290072 220 struct sr_usb_device_instance *udi;
a1bb33af 221
133a37bf
UH
222 if (!(udi = g_try_malloc(sizeof(struct sr_usb_device_instance)))) {
223 sr_err("hwplugin: %s: udi malloc failed", __func__);
a1bb33af 224 return NULL;
133a37bf 225 }
a1bb33af
UH
226
227 udi->bus = bus;
228 udi->address = address;
49d0ce50 229 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
a1bb33af
UH
230
231 return udi;
232}
233
1a081ca6 234SR_PRIV void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
a1bb33af 235{
17e1afcb 236 /* Avoid compiler warnings. */
cb93f8a9 237 (void)usb;
a1bb33af 238
62c82025 239 /* Nothing to do for this device instance type. */
a1bb33af
UH
240}
241
22b02383
UH
242#endif
243
1a081ca6 244SR_PRIV struct sr_serial_device_instance *sr_serial_device_instance_new(
49d0ce50 245 const char *port, int fd)
a1bb33af 246{
6c290072 247 struct sr_serial_device_instance *serial;
a1bb33af 248
133a37bf
UH
249 if (!(serial = g_try_malloc(sizeof(struct sr_serial_device_instance)))) {
250 sr_err("hwplugin: %s: serial malloc failed", __func__);
a1bb33af 251 return NULL;
133a37bf 252 }
a1bb33af 253
133a37bf 254 serial->port = g_strdup(port);
a1bb33af
UH
255 serial->fd = fd;
256
257 return serial;
258}
259
1a081ca6
UH
260SR_PRIV void sr_serial_device_instance_free(
261 struct sr_serial_device_instance *serial)
a1bb33af 262{
133a37bf 263 g_free(serial->port);
a1bb33af
UH
264}
265
a1645fcd
BV
266/**
267 * Find out if a list of hardware plugin capabilities has a specific cap.
268 *
269 * @param capabilities A NULL-terminated integer array of capabilities, as
270 * returned by a plugin's get_capabilities() function.
271 * @param hwcap The capability to find in the list.
272 *
273 * @return Returns TRUE if found, FALSE otherwise.
274 */
275SR_API gboolean sr_has_hwcap(int *capabilities, int hwcap)
a1bb33af
UH
276{
277 int i;
278
49d0ce50 279 for (i = 0; capabilities[i]; i++) {
62c82025 280 if (capabilities[i] == hwcap)
a1bb33af 281 return TRUE;
49d0ce50 282 }
a1bb33af
UH
283
284 return FALSE;
285}
286
a1645fcd
BV
287/**
288 * Find a hardware plugin capability option parameter structure.
289 *
290 * @param hwcap The capability to find
291 *
292 * @return Returns a struct with information about the parameter, or NULL
293 * if not found.
294 */
1a081ca6 295SR_API struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
a1bb33af 296{
a1bb33af
UH
297 int i;
298
a65de030
UH
299 for (i = 0; sr_hwcap_options[i].capability; i++) {
300 if (sr_hwcap_options[i].capability == hwcap)
301 return &sr_hwcap_options[i];
a1bb33af
UH
302 }
303
49d0ce50 304 return NULL;
a1bb33af
UH
305}
306
544a4582
BV
307/* unnecessary level of indirection follows. */
308
bb08ee2e 309SR_PRIV void sr_source_remove(int fd)
a1bb33af 310{
8a2efef2 311 sr_session_source_remove(fd);
a1bb33af
UH
312}
313
bb08ee2e 314SR_PRIV void sr_source_add(int fd, int events, int timeout,
a887e3da 315 sr_receive_data_callback rcv_cb, void *user_data)
a1bb33af 316{
8a2efef2 317 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
a1bb33af 318}