]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
sigrok.h: Add SIGROK_{MAJOR,MINOR,MICRO,}_VERSION #defines.
[libsigrok.git] / hwplugin.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 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 "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <sigrok.h>
28 #include <sigrok-internal.h>
29
30 /* The list of loaded plugins lives here. */
31 GSList *plugins;
32
33 /*
34  * This enumerates which plugin capabilities correspond to user-settable
35  * options.
36  */
37 /* TODO: This shouldn't be a global. */
38 struct sr_hwcap_option sr_hwcap_options[] = {
39         {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
40         {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
41         {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "patternmode"},
42         {SR_HWCAP_RLE, SR_T_CHAR, "Run Length Encoding", "rle"},
43         {0, 0, NULL, NULL},
44 };
45
46 #ifdef HAVE_LA_DEMO
47 extern struct sr_device_plugin demo_plugin_info;
48 #endif
49 #ifdef HAVE_LA_SALEAE_LOGIC
50 extern struct sr_device_plugin saleae_logic_plugin_info;
51 #endif
52 #ifdef HAVE_LA_OLS
53 extern struct sr_device_plugin ols_plugin_info;
54 #endif
55 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
56 extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
57 #endif
58 #ifdef HAVE_LA_ASIX_SIGMA
59 extern struct sr_device_plugin asix_sigma_plugin_info;
60 #endif
61 #ifdef HAVE_LA_CHRONOVU_LA8
62 extern struct device_plugin chronovu_la8_plugin_info;
63 #endif
64 #ifdef HAVE_LA_LINK_MSO19
65 extern struct sr_device_plugin link_mso19_plugin_info;
66 #endif
67 #ifdef HAVE_LA_ALSA
68 extern struct sr_device_plugin alsa_plugin_info;
69 #endif
70
71
72 /* TODO: No linked list needed, this can be a simple array. */
73 int load_hwplugins(void)
74 {
75 #ifdef HAVE_LA_DEMO
76         plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
77 #endif
78 #ifdef HAVE_LA_SALEAE_LOGIC
79         plugins =
80             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
81 #endif
82 #ifdef HAVE_LA_OLS
83         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
84 #endif
85 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
86         plugins = g_slist_append(plugins,
87                            (gpointer *)&zeroplus_logic_cube_plugin_info);
88 #endif
89 #ifdef HAVE_LA_ASIX_SIGMA
90         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
91 #endif
92 #ifdef HAVE_LA_CHRONOVU_LA8
93         plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
94 #endif
95 #ifdef HAVE_LA_LINK_MSO19
96         plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
97 #endif
98 #ifdef HAVE_LA_ALSA
99         plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
100 #endif
101
102
103         return SR_OK;
104 }
105
106 GSList *sr_list_hwplugins(void)
107 {
108         return plugins;
109 }
110
111 int sr_init_hwplugins(struct sr_device_plugin *plugin)
112 {
113         int num_devices, num_probes, i;
114
115         g_message("initializing %s plugin", plugin->name);
116         num_devices = plugin->init(NULL);
117         for (i = 0; i < num_devices; i++) {
118                 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
119                 sr_device_new(plugin, i, num_probes);
120         }
121
122         return num_devices;
123 }
124
125 void sr_cleanup_hwplugins(void)
126 {
127         struct sr_device_plugin *plugin;
128         GSList *l;
129
130         for (l = plugins; l; l = l->next) {
131                 plugin = l->data;
132                 if (plugin->cleanup)
133                         plugin->cleanup();
134         }
135
136 }
137
138 struct sr_device_instance *sr_device_instance_new(int index, int status,
139                 const char *vendor, const char *model, const char *version)
140 {
141         struct sr_device_instance *sdi;
142
143         if (!(sdi = g_malloc(sizeof(struct sr_device_instance))))
144                 return NULL;
145
146         sdi->index = index;
147         sdi->status = status;
148         sdi->instance_type = -1;
149         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
150         sdi->model = model ? g_strdup(model) : NULL;
151         sdi->version = version ? g_strdup(version) : NULL;
152         sdi->priv = NULL;
153         sdi->usb = NULL;
154
155         return sdi;
156 }
157
158 struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
159                                                   int device_index)
160 {
161         struct sr_device_instance *sdi;
162         GSList *l;
163
164         for (l = device_instances; l; l = l->next) {
165                 sdi = (struct sr_device_instance *)(l->data);
166                 if (sdi->index == device_index)
167                         return sdi;
168         }
169         sr_warn("could not find device index %d instance", device_index);
170
171         return NULL;
172 }
173
174 void sr_device_instance_free(struct sr_device_instance *sdi)
175 {
176         switch (sdi->instance_type) {
177 #ifdef HAVE_LIBUSB_1_0
178         case SR_USB_INSTANCE:
179                 sr_usb_device_instance_free(sdi->usb);
180                 break;
181 #endif
182         case SR_SERIAL_INSTANCE:
183                 sr_serial_device_instance_free(sdi->serial);
184                 break;
185         default:
186                 /* No specific type, nothing extra to free. */
187                 break;
188         }
189
190         if (sdi->priv)
191                 g_free(sdi->priv);
192
193         g_free(sdi->vendor);
194         g_free(sdi->model);
195         g_free(sdi->version);
196         g_free(sdi);
197
198 }
199
200 #ifdef HAVE_LIBUSB_1_0
201
202 struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
203                         uint8_t address, struct libusb_device_handle *hdl)
204 {
205         struct sr_usb_device_instance *udi;
206
207         if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
208                 return NULL;
209
210         udi->bus = bus;
211         udi->address = address;
212         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
213
214         return udi;
215 }
216
217 void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
218 {
219         /* Avoid compiler warnings. */
220         usb = usb;
221
222         /* Nothing to do for this device instance type. */
223 }
224
225 #endif
226
227 struct sr_serial_device_instance *sr_serial_device_instance_new(
228                                                 const char *port, int fd)
229 {
230         struct sr_serial_device_instance *serial;
231
232         if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
233                 return NULL;
234
235         serial->port = strdup(port);
236         serial->fd = fd;
237
238         return serial;
239 }
240
241 void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
242 {
243         free(serial->port);
244 }
245
246 int sr_find_hwcap(int *capabilities, int hwcap)
247 {
248         int i;
249
250         for (i = 0; capabilities[i]; i++) {
251                 if (capabilities[i] == hwcap)
252                         return TRUE;
253         }
254
255         return FALSE;
256 }
257
258 struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
259 {
260         int i;
261
262         for (i = 0; sr_hwcap_options[i].capability; i++) {
263                 if (sr_hwcap_options[i].capability == hwcap)
264                         return &sr_hwcap_options[i];
265         }
266
267         return NULL;
268 }
269
270 /* unnecessary level of indirection follows. */
271
272 void sr_source_remove(int fd)
273 {
274         sr_session_source_remove(fd);
275 }
276
277 void sr_source_add(int fd, int events, int timeout,
278                    sr_receive_data_callback rcv_cb, void *user_data)
279 {
280         sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
281 }