]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
sr: Random cosmetics, fix/amend Doxygen comments.
[libsigrok.git] / hwplugin.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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>
26 #include "sigrok.h"
27 #include "sigrok-internal.h"
28
29 /* The list of loaded plugins lives here. */
30 static GSList *plugins;
31
32 /*
33  * This enumerates which plugin capabilities correspond to user-settable
34  * options.
35  */
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"},
42         {0, 0, NULL, NULL},
43 };
44
45 #ifdef HAVE_LA_DEMO
46 extern struct sr_device_plugin demo_plugin_info;
47 #endif
48 #ifdef HAVE_LA_SALEAE_LOGIC
49 extern struct sr_device_plugin saleae_logic_plugin_info;
50 #endif
51 #ifdef HAVE_LA_OLS
52 extern struct sr_device_plugin ols_plugin_info;
53 #endif
54 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
55 extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
56 #endif
57 #ifdef HAVE_LA_ASIX_SIGMA
58 extern struct sr_device_plugin asix_sigma_plugin_info;
59 #endif
60 #ifdef HAVE_LA_CHRONOVU_LA8
61 extern SR_PRIV struct device_plugin chronovu_la8_plugin_info;
62 #endif
63 #ifdef HAVE_LA_LINK_MSO19
64 extern struct sr_device_plugin link_mso19_plugin_info;
65 #endif
66 #ifdef HAVE_LA_ALSA
67 extern struct sr_device_plugin alsa_plugin_info;
68 #endif
69
70 /* TODO: No linked list needed, this can be a simple array. */
71 SR_PRIV int sr_hw_load_all(void)
72 {
73 #ifdef HAVE_LA_DEMO
74         plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
75 #endif
76 #ifdef HAVE_LA_SALEAE_LOGIC
77         plugins =
78             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
79 #endif
80 #ifdef HAVE_LA_OLS
81         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
82 #endif
83 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
84         plugins = g_slist_append(plugins,
85                            (gpointer *)&zeroplus_logic_cube_plugin_info);
86 #endif
87 #ifdef HAVE_LA_ASIX_SIGMA
88         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
89 #endif
90 #ifdef HAVE_LA_CHRONOVU_LA8
91         plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
92 #endif
93 #ifdef HAVE_LA_LINK_MSO19
94         plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
95 #endif
96 #ifdef HAVE_LA_ALSA
97         plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
98 #endif
99
100         return SR_OK;
101 }
102
103 /**
104  * Return 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  */
111 SR_API GSList *sr_hw_list(void)
112 {
113         return plugins;
114 }
115
116 /**
117  * Initialize a hardware plugin.
118  *
119  * The specified plugin is initialized, and all devices discovered by the
120  * plugin are instantiated.
121  *
122  * @param plugin The plugin to initialize.
123  *
124  * @return The number of devices found and instantiated by the plugin.
125  */
126 SR_API int sr_hw_init(struct sr_device_plugin *plugin)
127 {
128         int num_devices, num_probes, i, j;
129         int num_initialized_devices = 0;
130         struct sr_device *device;
131         char **probe_names;
132
133         sr_dbg("initializing %s plugin", plugin->name);
134         num_devices = plugin->init(NULL);
135         for (i = 0; i < num_devices; i++) {
136                 num_probes = GPOINTER_TO_INT(
137                                 plugin->get_device_info(i, SR_DI_NUM_PROBES));
138                 probe_names = (char **)plugin->get_device_info(i,
139                                                         SR_DI_PROBE_NAMES);
140
141                 if (!probe_names) {
142                         sr_warn("hwplugin: %s: plugin %s does not return a "
143                                 "list of probe names", __func__, plugin->name);
144                         continue;
145                 }
146
147                 device = sr_dev_new(plugin, i);
148                 for (j = 0; j < num_probes; j++)
149                         sr_dev_probe_add(device, probe_names[j]);
150                 num_initialized_devices++;
151         }
152
153         return num_initialized_devices;
154 }
155
156 SR_PRIV void sr_hw_cleanup_all(void)
157 {
158         struct sr_device_plugin *plugin;
159         GSList *l;
160
161         for (l = plugins; l; l = l->next) {
162                 plugin = l->data;
163                 if (plugin->cleanup)
164                         plugin->cleanup();
165         }
166 }
167
168 SR_PRIV struct sr_device_instance *sr_dev_inst_new(int index, int status,
169                 const char *vendor, const char *model, const char *version)
170 {
171         struct sr_device_instance *sdi;
172
173         if (!(sdi = g_try_malloc(sizeof(struct sr_device_instance)))) {
174                 sr_err("hwplugin: %s: sdi malloc failed", __func__);
175                 return NULL;
176         }
177
178         sdi->index = index;
179         sdi->status = status;
180         sdi->instance_type = -1;
181         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
182         sdi->model = model ? g_strdup(model) : NULL;
183         sdi->version = version ? g_strdup(version) : NULL;
184         sdi->priv = NULL;
185
186         return sdi;
187 }
188
189 SR_PRIV struct sr_device_instance *sr_dev_inst_get(
190                 GSList *device_instances, int device_index)
191 {
192         struct sr_device_instance *sdi;
193         GSList *l;
194
195         for (l = device_instances; l; l = l->next) {
196                 sdi = (struct sr_device_instance *)(l->data);
197                 if (sdi->index == device_index)
198                         return sdi;
199         }
200         sr_warn("could not find device index %d instance", device_index);
201
202         return NULL;
203 }
204
205 SR_PRIV void sr_dev_inst_free(struct sr_device_instance *sdi)
206 {
207         g_free(sdi->priv);
208         g_free(sdi->vendor);
209         g_free(sdi->model);
210         g_free(sdi->version);
211         g_free(sdi);
212 }
213
214 #ifdef HAVE_LIBUSB_1_0
215
216 SR_PRIV struct sr_usb_device_instance *sr_usb_dev_inst_new(uint8_t bus,
217                         uint8_t address, struct libusb_device_handle *hdl)
218 {
219         struct sr_usb_device_instance *udi;
220
221         if (!(udi = g_try_malloc(sizeof(struct sr_usb_device_instance)))) {
222                 sr_err("hwplugin: %s: udi malloc failed", __func__);
223                 return NULL;
224         }
225
226         udi->bus = bus;
227         udi->address = address;
228         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
229
230         return udi;
231 }
232
233 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_device_instance *usb)
234 {
235         /* Avoid compiler warnings. */
236         (void)usb;
237
238         /* Nothing to do for this device instance type. */
239 }
240
241 #endif
242
243 SR_PRIV struct sr_serial_device_instance *sr_serial_dev_inst_new(
244                                                 const char *port, int fd)
245 {
246         struct sr_serial_device_instance *serial;
247
248         if (!(serial = g_try_malloc(sizeof(struct sr_serial_device_instance)))) {
249                 sr_err("hwplugin: %s: serial malloc failed", __func__);
250                 return NULL;
251         }
252
253         serial->port = g_strdup(port);
254         serial->fd = fd;
255
256         return serial;
257 }
258
259 SR_PRIV void sr_serial_dev_inst_free(
260                 struct sr_serial_device_instance *serial)
261 {
262         g_free(serial->port);
263 }
264
265 /**
266  * Find out if a hardware plugin has a specific capability.
267  *
268  * @param plugin The hardware plugin in which to search for the capability.
269  * @param hwcap The capability to find in the list.
270  *
271  * @return TRUE if found, FALSE otherwise.
272  */
273 SR_API gboolean sr_hw_has_hwcap(struct sr_device_plugin *plugin, int hwcap)
274 {
275         int *capabilities, i;
276
277         capabilities = plugin->get_capabilities();
278         for (i = 0; capabilities[i]; i++) {
279                 if (capabilities[i] == hwcap)
280                         return TRUE;
281         }
282
283         return FALSE;
284 }
285
286 /**
287  * Get a hardware plugin capability option.
288  *
289  * @param hwcap The capability to get.
290  *
291  * @return A pointer to a struct with information about the parameter, or NULL
292  *         if the capability was not found.
293  */
294 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
295 {
296         int i;
297
298         for (i = 0; sr_hwcap_options[i].capability; i++) {
299                 if (sr_hwcap_options[i].capability == hwcap)
300                         return &sr_hwcap_options[i];
301         }
302
303         return NULL;
304 }
305
306 /* unnecessary level of indirection follows. */
307
308 SR_PRIV void sr_source_remove(int fd)
309 {
310         sr_session_source_remove(fd);
311 }
312
313 SR_PRIV void sr_source_add(int fd, int events, int timeout,
314                    sr_receive_data_callback rcv_cb, void *user_data)
315 {
316         sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
317 }