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;
67 static struct sr_dev_plugin *plugins_list[] = {
71 #ifdef HAVE_LA_SALEAE_LOGIC
72 &saleae_logic_plugin_info,
77 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
78 &zeroplus_logic_cube_plugin_info,
80 #ifdef HAVE_LA_ASIX_SIGMA
81 &asix_sigma_plugin_info,
83 #ifdef HAVE_LA_CHRONOVU_LA8
84 &chronovu_la8_plugin_info,
86 #ifdef HAVE_LA_LINK_MSO19
87 &link_mso19_plugin_info,
96 * Return the list of loaded hardware plugins.
98 * The list of plugins is initialized from sr_init(), and can only be reset
99 * by calling sr_exit().
101 * @return Pointer to the NULL-terminated list of hardware plugin pointers.
103 SR_API struct sr_dev_plugin **sr_hw_list(void)
109 * Initialize a hardware plugin.
111 * The specified plugin is initialized, and all devices discovered by the
112 * plugin are instantiated.
114 * @param plugin The plugin to initialize.
116 * @return The number of devices found and instantiated by the plugin.
118 SR_API int sr_hw_init(struct sr_dev_plugin *plugin)
120 int num_devs, num_probes, i, j;
121 int num_initialized_devs = 0;
125 sr_dbg("initializing %s plugin", plugin->name);
126 num_devs = plugin->init(NULL);
127 for (i = 0; i < num_devs; i++) {
128 num_probes = GPOINTER_TO_INT(
129 plugin->dev_info_get(i, SR_DI_NUM_PROBES));
130 probe_names = (char **)plugin->dev_info_get(i,
134 sr_warn("hwplugin: %s: plugin %s does not return a "
135 "list of probe names", __func__, plugin->name);
139 dev = sr_dev_new(plugin, i);
140 for (j = 0; j < num_probes; j++)
141 sr_dev_probe_add(dev, probe_names[j]);
142 num_initialized_devs++;
145 return num_initialized_devs;
148 SR_PRIV void sr_hw_cleanup_all(void)
151 struct sr_dev_plugin **plugins;
153 plugins = sr_hw_list();
154 for (i = 0; plugins[i]; i++) {
155 if (plugins[i]->cleanup)
156 plugins[i]->cleanup();
160 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
161 const char *vendor, const char *model, const char *version)
163 struct sr_dev_inst *sdi;
165 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
166 sr_err("hwplugin: %s: sdi malloc failed", __func__);
171 sdi->status = status;
173 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
174 sdi->model = model ? g_strdup(model) : NULL;
175 sdi->version = version ? g_strdup(version) : NULL;
181 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
183 struct sr_dev_inst *sdi;
186 for (l = dev_insts; l; l = l->next) {
187 sdi = (struct sr_dev_inst *)(l->data);
188 if (sdi->index == dev_index)
191 sr_warn("could not find device index %d instance", dev_index);
196 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
201 g_free(sdi->version);
205 #ifdef HAVE_LIBUSB_1_0
207 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
208 uint8_t address, struct libusb_device_handle *hdl)
210 struct sr_usb_dev_inst *udi;
212 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
213 sr_err("hwplugin: %s: udi malloc failed", __func__);
218 udi->address = address;
219 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
224 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
226 /* Avoid compiler warnings. */
229 /* Nothing to do for this device instance type. */
234 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
237 struct sr_serial_dev_inst *serial;
239 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
240 sr_err("hwplugin: %s: serial malloc failed", __func__);
244 serial->port = g_strdup(port);
250 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
252 g_free(serial->port);
256 * Find out if a hardware plugin has a specific capability.
258 * @param plugin The hardware plugin in which to search for the capability.
259 * @param hwcap The capability to find in the list.
261 * @return TRUE if found, FALSE otherwise.
263 SR_API gboolean sr_hw_has_hwcap(struct sr_dev_plugin *plugin, int hwcap)
267 hwcaps = plugin->hwcap_get_all();
268 for (i = 0; hwcaps[i]; i++) {
269 if (hwcaps[i] == hwcap)
277 * Get a hardware plugin capability option.
279 * @param hwcap The capability to get.
281 * @return A pointer to a struct with information about the parameter, or NULL
282 * if the capability was not found.
284 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
288 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
289 if (sr_hwcap_options[i].hwcap == hwcap)
290 return &sr_hwcap_options[i];
296 /* unnecessary level of indirection follows. */
298 SR_PRIV void sr_source_remove(int fd)
300 sr_session_source_remove(fd);
303 SR_PRIV void sr_source_add(int fd, int events, int timeout,
304 sr_receive_data_callback rcv_cb, void *user_data)
306 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);