]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
sr: No need for dynamic hardware driver registration.
[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 /*
30  * This enumerates which plugin capabilities correspond to user-settable
31  * options.
32  */
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"},
39         {0, 0, NULL, NULL},
40 };
41
42 #ifdef HAVE_LA_DEMO
43 extern SR_PRIV struct sr_dev_plugin demo_plugin_info;
44 #endif
45 #ifdef HAVE_LA_SALEAE_LOGIC
46 extern SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info;
47 #endif
48 #ifdef HAVE_LA_OLS
49 extern SR_PRIV struct sr_dev_plugin ols_plugin_info;
50 #endif
51 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
52 extern SR_PRIV struct sr_dev_plugin zeroplus_logic_cube_plugin_info;
53 #endif
54 #ifdef HAVE_LA_ASIX_SIGMA
55 extern SR_PRIV struct sr_dev_plugin asix_sigma_plugin_info;
56 #endif
57 #ifdef HAVE_LA_CHRONOVU_LA8
58 extern SR_PRIV struct sr_dev_plugin chronovu_la8_plugin_info;
59 #endif
60 #ifdef HAVE_LA_LINK_MSO19
61 extern SR_PRIV struct sr_dev_plugin link_mso19_plugin_info;
62 #endif
63 #ifdef HAVE_LA_ALSA
64 extern SR_PRIV struct sr_dev_plugin alsa_plugin_info;
65 #endif
66
67 static struct sr_dev_plugin *plugins_list[] = {
68 #ifdef HAVE_LA_DEMO
69         &demo_plugin_info,
70 #endif
71 #ifdef HAVE_LA_SALEAE_LOGIC
72         &saleae_logic_plugin_info,
73 #endif
74 #ifdef HAVE_LA_OLS
75         &ols_plugin_info,
76 #endif
77 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
78         &zeroplus_logic_cube_plugin_info,
79 #endif
80 #ifdef HAVE_LA_ASIX_SIGMA
81         &asix_sigma_plugin_info,
82 #endif
83 #ifdef HAVE_LA_CHRONOVU_LA8
84         &chronovu_la8_plugin_info,
85 #endif
86 #ifdef HAVE_LA_LINK_MSO19
87         &link_mso19_plugin_info,
88 #endif
89 #ifdef HAVE_LA_ALSA
90         &alsa_plugin_info,
91 #endif
92         NULL,
93 };
94
95 /**
96  * Return the list of loaded hardware plugins.
97  *
98  * The list of plugins is initialized from sr_init(), and can only be reset
99  * by calling sr_exit().
100  *
101  * @return Pointer to the NULL-terminated list of hardware plugin pointers.
102  */
103 SR_API struct sr_dev_plugin **sr_hw_list(void)
104 {
105         return plugins_list;
106 }
107
108 /**
109  * Initialize a hardware plugin.
110  *
111  * The specified plugin is initialized, and all devices discovered by the
112  * plugin are instantiated.
113  *
114  * @param plugin The plugin to initialize.
115  *
116  * @return The number of devices found and instantiated by the plugin.
117  */
118 SR_API int sr_hw_init(struct sr_dev_plugin *plugin)
119 {
120         int num_devs, num_probes, i, j;
121         int num_initialized_devs = 0;
122         struct sr_dev *dev;
123         char **probe_names;
124
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,
131                                                         SR_DI_PROBE_NAMES);
132
133                 if (!probe_names) {
134                         sr_warn("hwplugin: %s: plugin %s does not return a "
135                                 "list of probe names", __func__, plugin->name);
136                         continue;
137                 }
138
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++;
143         }
144
145         return num_initialized_devs;
146 }
147
148 SR_PRIV void sr_hw_cleanup_all(void)
149 {
150         int i;
151         struct sr_dev_plugin **plugins;
152
153         plugins = sr_hw_list();
154         for (i = 0; plugins[i]; i++) {
155                 if (plugins[i]->cleanup)
156                         plugins[i]->cleanup();
157         }
158 }
159
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)
162 {
163         struct sr_dev_inst *sdi;
164
165         if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
166                 sr_err("hwplugin: %s: sdi malloc failed", __func__);
167                 return NULL;
168         }
169
170         sdi->index = index;
171         sdi->status = status;
172         sdi->inst_type = -1;
173         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
174         sdi->model = model ? g_strdup(model) : NULL;
175         sdi->version = version ? g_strdup(version) : NULL;
176         sdi->priv = NULL;
177
178         return sdi;
179 }
180
181 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
182 {
183         struct sr_dev_inst *sdi;
184         GSList *l;
185
186         for (l = dev_insts; l; l = l->next) {
187                 sdi = (struct sr_dev_inst *)(l->data);
188                 if (sdi->index == dev_index)
189                         return sdi;
190         }
191         sr_warn("could not find device index %d instance", dev_index);
192
193         return NULL;
194 }
195
196 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
197 {
198         g_free(sdi->priv);
199         g_free(sdi->vendor);
200         g_free(sdi->model);
201         g_free(sdi->version);
202         g_free(sdi);
203 }
204
205 #ifdef HAVE_LIBUSB_1_0
206
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)
209 {
210         struct sr_usb_dev_inst *udi;
211
212         if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
213                 sr_err("hwplugin: %s: udi malloc failed", __func__);
214                 return NULL;
215         }
216
217         udi->bus = bus;
218         udi->address = address;
219         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
220
221         return udi;
222 }
223
224 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
225 {
226         /* Avoid compiler warnings. */
227         (void)usb;
228
229         /* Nothing to do for this device instance type. */
230 }
231
232 #endif
233
234 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
235                                                           int fd)
236 {
237         struct sr_serial_dev_inst *serial;
238
239         if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
240                 sr_err("hwplugin: %s: serial malloc failed", __func__);
241                 return NULL;
242         }
243
244         serial->port = g_strdup(port);
245         serial->fd = fd;
246
247         return serial;
248 }
249
250 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
251 {
252         g_free(serial->port);
253 }
254
255 /**
256  * Find out if a hardware plugin has a specific capability.
257  *
258  * @param plugin The hardware plugin in which to search for the capability.
259  * @param hwcap The capability to find in the list.
260  *
261  * @return TRUE if found, FALSE otherwise.
262  */
263 SR_API gboolean sr_hw_has_hwcap(struct sr_dev_plugin *plugin, int hwcap)
264 {
265         int *hwcaps, i;
266
267         hwcaps = plugin->hwcap_get_all();
268         for (i = 0; hwcaps[i]; i++) {
269                 if (hwcaps[i] == hwcap)
270                         return TRUE;
271         }
272
273         return FALSE;
274 }
275
276 /**
277  * Get a hardware plugin capability option.
278  *
279  * @param hwcap The capability to get.
280  *
281  * @return A pointer to a struct with information about the parameter, or NULL
282  *         if the capability was not found.
283  */
284 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
285 {
286         int i;
287
288         for (i = 0; sr_hwcap_options[i].hwcap; i++) {
289                 if (sr_hwcap_options[i].hwcap == hwcap)
290                         return &sr_hwcap_options[i];
291         }
292
293         return NULL;
294 }
295
296 /* unnecessary level of indirection follows. */
297
298 SR_PRIV void sr_source_remove(int fd)
299 {
300         sr_session_source_remove(fd);
301 }
302
303 SR_PRIV void sr_source_add(int fd, int events, int timeout,
304                    sr_receive_data_callback rcv_cb, void *user_data)
305 {
306         sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
307 }