2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include <sys/types.h>
27 #include "sigrok-internal.h"
29 /* The list of loaded plugins lives here. */
30 static GSList *plugins;
33 * This enumerates which plugin capabilities correspond to user-settable
36 /* TODO: This shouldn't be a global. */
37 SR_API struct sr_hwcap_option sr_hwcap_options[] = {
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"},
41 {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
46 extern struct sr_dev_plugin demo_plugin_info;
48 #ifdef HAVE_LA_SALEAE_LOGIC
49 extern struct sr_dev_plugin saleae_logic_plugin_info;
52 extern struct sr_dev_plugin ols_plugin_info;
54 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
55 extern struct sr_dev_plugin zeroplus_logic_cube_plugin_info;
57 #ifdef HAVE_LA_ASIX_SIGMA
58 extern struct sr_dev_plugin asix_sigma_plugin_info;
60 #ifdef HAVE_LA_CHRONOVU_LA8
61 extern SR_PRIV struct dev_plugin chronovu_la8_plugin_info;
63 #ifdef HAVE_LA_LINK_MSO19
64 extern struct sr_dev_plugin link_mso19_plugin_info;
67 extern struct sr_dev_plugin alsa_plugin_info;
70 /* TODO: No linked list needed, this can be a simple array. */
71 SR_PRIV int sr_hw_load_all(void)
74 plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
76 #ifdef HAVE_LA_SALEAE_LOGIC
78 g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
81 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
83 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
84 plugins = g_slist_append(plugins,
85 (gpointer *)&zeroplus_logic_cube_plugin_info);
87 #ifdef HAVE_LA_ASIX_SIGMA
88 plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
90 #ifdef HAVE_LA_CHRONOVU_LA8
91 plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
93 #ifdef HAVE_LA_LINK_MSO19
94 plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
97 plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
104 * Return the list of loaded hardware plugins.
106 * The list of plugins is initialized from sr_init(), and can only be reset
107 * by calling sr_exit().
109 * @return A GSList of pointers to loaded plugins.
111 SR_API GSList *sr_hw_list(void)
117 * Initialize a hardware plugin.
119 * The specified plugin is initialized, and all devices discovered by the
120 * plugin are instantiated.
122 * @param plugin The plugin to initialize.
124 * @return The number of devices found and instantiated by the plugin.
126 SR_API int sr_hw_init(struct sr_dev_plugin *plugin)
128 int num_devs, num_probes, i, j;
129 int num_initialized_devs = 0;
133 sr_dbg("initializing %s plugin", plugin->name);
134 num_devs = plugin->init(NULL);
135 for (i = 0; i < num_devs; i++) {
136 num_probes = GPOINTER_TO_INT(
137 plugin->get_dev_info(i, SR_DI_NUM_PROBES));
138 probe_names = (char **)plugin->get_dev_info(i,
142 sr_warn("hwplugin: %s: plugin %s does not return a "
143 "list of probe names", __func__, plugin->name);
147 dev = sr_dev_new(plugin, i);
148 for (j = 0; j < num_probes; j++)
149 sr_dev_probe_add(dev, probe_names[j]);
150 num_initialized_devs++;
153 return num_initialized_devs;
156 SR_PRIV void sr_hw_cleanup_all(void)
158 struct sr_dev_plugin *plugin;
161 for (l = plugins; l; l = l->next) {
168 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
169 const char *vendor, const char *model, const char *version)
171 struct sr_dev_inst *sdi;
173 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
174 sr_err("hwplugin: %s: sdi malloc failed", __func__);
179 sdi->status = status;
181 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
182 sdi->model = model ? g_strdup(model) : NULL;
183 sdi->version = version ? g_strdup(version) : NULL;
189 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
191 struct sr_dev_inst *sdi;
194 for (l = dev_insts; l; l = l->next) {
195 sdi = (struct sr_dev_inst *)(l->data);
196 if (sdi->index == dev_index)
199 sr_warn("could not find device index %d instance", dev_index);
204 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
209 g_free(sdi->version);
213 #ifdef HAVE_LIBUSB_1_0
215 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
216 uint8_t address, struct libusb_device_handle *hdl)
218 struct sr_usb_dev_inst *udi;
220 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
221 sr_err("hwplugin: %s: udi malloc failed", __func__);
226 udi->address = address;
227 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
232 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
234 /* Avoid compiler warnings. */
237 /* Nothing to do for this device instance type. */
242 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
245 struct sr_serial_dev_inst *serial;
247 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
248 sr_err("hwplugin: %s: serial malloc failed", __func__);
252 serial->port = g_strdup(port);
258 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
260 g_free(serial->port);
264 * Find out if a hardware plugin has a specific capability.
266 * @param plugin The hardware plugin in which to search for the capability.
267 * @param hwcap The capability to find in the list.
269 * @return TRUE if found, FALSE otherwise.
271 SR_API gboolean sr_hw_has_hwcap(struct sr_dev_plugin *plugin, int hwcap)
275 hwcaps = plugin->hwcap_get_all();
276 for (i = 0; hwcaps[i]; i++) {
277 if (hwcaps[i] == hwcap)
285 * Get a hardware plugin capability option.
287 * @param hwcap The capability to get.
289 * @return A pointer to a struct with information about the parameter, or NULL
290 * if the capability was not found.
292 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
296 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
297 if (sr_hwcap_options[i].hwcap == hwcap)
298 return &sr_hwcap_options[i];
304 /* unnecessary level of indirection follows. */
306 SR_PRIV void sr_source_remove(int fd)
308 sr_session_source_remove(fd);
311 SR_PRIV void sr_source_add(int fd, int events, int timeout,
312 sr_receive_data_callback rcv_cb, void *user_data)
314 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);