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"
30 * This enumerates which plugin capabilities correspond to user-settable
33 /* TODO: This shouldn't be a global. */
34 SR_API struct sr_hwcap_option sr_hwcap_options[] = {
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"},
38 {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
43 extern SR_PRIV struct sr_dev_plugin demo_plugin_info;
45 #ifdef HAVE_LA_SALEAE_LOGIC
46 extern SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info;
49 extern SR_PRIV struct sr_dev_plugin ols_plugin_info;
51 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
52 extern SR_PRIV struct sr_dev_plugin zeroplus_logic_cube_plugin_info;
54 #ifdef HAVE_LA_ASIX_SIGMA
55 extern SR_PRIV struct sr_dev_plugin asix_sigma_plugin_info;
57 #ifdef HAVE_LA_CHRONOVU_LA8
58 extern SR_PRIV struct sr_dev_plugin chronovu_la8_plugin_info;
60 #ifdef HAVE_LA_LINK_MSO19
61 extern SR_PRIV struct sr_dev_plugin link_mso19_plugin_info;
64 extern SR_PRIV struct sr_dev_plugin alsa_plugin_info;
66 #ifdef HAVE_LA_FX2LAFW
67 extern SR_PRIV struct sr_dev_plugin fx2lafw_plugin_info;
70 static struct sr_dev_plugin *plugins_list[] = {
74 #ifdef HAVE_LA_SALEAE_LOGIC
75 &saleae_logic_plugin_info,
80 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
81 &zeroplus_logic_cube_plugin_info,
83 #ifdef HAVE_LA_ASIX_SIGMA
84 &asix_sigma_plugin_info,
86 #ifdef HAVE_LA_CHRONOVU_LA8
87 &chronovu_la8_plugin_info,
89 #ifdef HAVE_LA_LINK_MSO19
90 &link_mso19_plugin_info,
95 #ifdef HAVE_LA_FX2LAFW
102 * Return the list of loaded hardware plugins.
104 * The list of plugins is initialized from sr_init(), and can only be reset
105 * by calling sr_exit().
107 * @return Pointer to the NULL-terminated list of hardware plugin pointers.
109 SR_API struct sr_dev_plugin **sr_hw_list(void)
115 * Initialize a hardware plugin.
117 * The specified plugin is initialized, and all devices discovered by the
118 * plugin are instantiated.
120 * @param plugin The plugin to initialize.
122 * @return The number of devices found and instantiated by the plugin.
124 SR_API int sr_hw_init(struct sr_dev_plugin *plugin)
126 int num_devs, num_probes, i, j;
127 int num_initialized_devs = 0;
131 sr_dbg("initializing %s plugin", plugin->name);
132 num_devs = plugin->init(NULL);
133 for (i = 0; i < num_devs; i++) {
134 num_probes = GPOINTER_TO_INT(
135 plugin->dev_info_get(i, SR_DI_NUM_PROBES));
136 probe_names = (char **)plugin->dev_info_get(i,
140 sr_warn("hwplugin: %s: plugin %s does not return a "
141 "list of probe names", __func__, plugin->name);
145 dev = sr_dev_new(plugin, i);
146 for (j = 0; j < num_probes; j++)
147 sr_dev_probe_add(dev, probe_names[j]);
148 num_initialized_devs++;
151 return num_initialized_devs;
154 SR_PRIV void sr_hw_cleanup_all(void)
157 struct sr_dev_plugin **plugins;
159 plugins = sr_hw_list();
160 for (i = 0; plugins[i]; i++) {
161 if (plugins[i]->cleanup)
162 plugins[i]->cleanup();
166 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
167 const char *vendor, const char *model, const char *version)
169 struct sr_dev_inst *sdi;
171 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
172 sr_err("hwplugin: %s: sdi malloc failed", __func__);
177 sdi->status = status;
179 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
180 sdi->model = model ? g_strdup(model) : NULL;
181 sdi->version = version ? g_strdup(version) : NULL;
187 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
189 struct sr_dev_inst *sdi;
192 for (l = dev_insts; l; l = l->next) {
193 sdi = (struct sr_dev_inst *)(l->data);
194 if (sdi->index == dev_index)
197 sr_warn("could not find device index %d instance", dev_index);
202 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
207 g_free(sdi->version);
211 #ifdef HAVE_LIBUSB_1_0
213 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
214 uint8_t address, struct libusb_device_handle *hdl)
216 struct sr_usb_dev_inst *udi;
218 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
219 sr_err("hwplugin: %s: udi malloc failed", __func__);
224 udi->address = address;
225 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
230 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
232 /* Avoid compiler warnings. */
235 /* Nothing to do for this device instance type. */
240 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
243 struct sr_serial_dev_inst *serial;
245 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
246 sr_err("hwplugin: %s: serial malloc failed", __func__);
250 serial->port = g_strdup(port);
256 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
258 g_free(serial->port);
262 * Find out if a hardware plugin has a specific capability.
264 * @param plugin The hardware plugin in which to search for the capability.
265 * @param hwcap The capability to find in the list.
267 * @return TRUE if found, FALSE otherwise.
269 SR_API gboolean sr_hw_has_hwcap(struct sr_dev_plugin *plugin, int hwcap)
273 hwcaps = plugin->hwcap_get_all();
274 for (i = 0; hwcaps[i]; i++) {
275 if (hwcaps[i] == hwcap)
283 * Get a hardware plugin capability option.
285 * @param hwcap The capability to get.
287 * @return A pointer to a struct with information about the parameter, or NULL
288 * if the capability was not found.
290 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
294 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
295 if (sr_hwcap_options[i].hwcap == hwcap)
296 return &sr_hwcap_options[i];
302 /* unnecessary level of indirection follows. */
304 SR_PRIV void sr_source_remove(int fd)
306 sr_session_source_remove(fd);
309 SR_PRIV void sr_source_add(int fd, int events, int timeout,
310 sr_receive_data_callback rcv_cb, void *user_data)
312 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);