]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
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 <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <sys/types.h> | |
23 | #include <dirent.h> | |
24 | #include <string.h> | |
25 | #include <glib.h> | |
b7f09cf8 UH |
26 | #include "sigrok.h" |
27 | #include "sigrok-internal.h" | |
a1bb33af | 28 | |
62c82025 | 29 | /* The list of loaded plugins lives here. */ |
a0ecd83b | 30 | static GSList *plugins; |
a1bb33af | 31 | |
62c82025 UH |
32 | /* |
33 | * This enumerates which plugin capabilities correspond to user-settable | |
34 | * options. | |
35 | */ | |
a65de030 | 36 | /* TODO: This shouldn't be a global. */ |
1a081ca6 | 37 | SR_API struct sr_hwcap_option sr_hwcap_options[] = { |
5a2326a7 UH |
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"}, | |
4d436e71 | 41 | {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"}, |
49d0ce50 | 42 | {0, 0, NULL, NULL}, |
a1bb33af UH |
43 | }; |
44 | ||
a61b0e6a | 45 | #ifdef HAVE_LA_DEMO |
5c2d46d1 | 46 | extern struct sr_device_plugin demo_plugin_info; |
a61b0e6a | 47 | #endif |
960a75e4 | 48 | #ifdef HAVE_LA_SALEAE_LOGIC |
5c2d46d1 | 49 | extern struct sr_device_plugin saleae_logic_plugin_info; |
960a75e4 UH |
50 | #endif |
51 | #ifdef HAVE_LA_OLS | |
5c2d46d1 | 52 | extern struct sr_device_plugin ols_plugin_info; |
960a75e4 UH |
53 | #endif |
54 | #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE | |
5c2d46d1 | 55 | extern struct sr_device_plugin zeroplus_logic_cube_plugin_info; |
960a75e4 | 56 | #endif |
5b907f9b | 57 | #ifdef HAVE_LA_ASIX_SIGMA |
5c2d46d1 | 58 | extern struct sr_device_plugin asix_sigma_plugin_info; |
5b907f9b | 59 | #endif |
f4314d7e | 60 | #ifdef HAVE_LA_CHRONOVU_LA8 |
ca070ed9 | 61 | extern SR_PRIV struct device_plugin chronovu_la8_plugin_info; |
f4314d7e | 62 | #endif |
bffed4fc | 63 | #ifdef HAVE_LA_LINK_MSO19 |
5c2d46d1 | 64 | extern struct sr_device_plugin link_mso19_plugin_info; |
bffed4fc | 65 | #endif |
6ed4f044 | 66 | #ifdef HAVE_LA_ALSA |
5c2d46d1 | 67 | extern struct sr_device_plugin alsa_plugin_info; |
6ed4f044 DR |
68 | #endif |
69 | ||
49d0ce50 | 70 | /* TODO: No linked list needed, this can be a simple array. */ |
1a081ca6 | 71 | SR_PRIV int load_hwplugins(void) |
a1bb33af | 72 | { |
a61b0e6a | 73 | #ifdef HAVE_LA_DEMO |
6239c175 | 74 | plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info); |
a61b0e6a | 75 | #endif |
960a75e4 | 76 | #ifdef HAVE_LA_SALEAE_LOGIC |
62c82025 | 77 | plugins = |
49d0ce50 | 78 | g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info); |
960a75e4 UH |
79 | #endif |
80 | #ifdef HAVE_LA_OLS | |
49d0ce50 | 81 | plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info); |
960a75e4 UH |
82 | #endif |
83 | #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE | |
62c82025 | 84 | plugins = g_slist_append(plugins, |
49d0ce50 | 85 | (gpointer *)&zeroplus_logic_cube_plugin_info); |
960a75e4 | 86 | #endif |
5b907f9b | 87 | #ifdef HAVE_LA_ASIX_SIGMA |
28a35d8a | 88 | plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info); |
5b907f9b | 89 | #endif |
f4314d7e UH |
90 | #ifdef HAVE_LA_CHRONOVU_LA8 |
91 | plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info); | |
92 | #endif | |
bffed4fc UH |
93 | #ifdef HAVE_LA_LINK_MSO19 |
94 | plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info); | |
95 | #endif | |
6ed4f044 DR |
96 | #ifdef HAVE_LA_ALSA |
97 | plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info); | |
98 | #endif | |
99 | ||
e46b8fb1 | 100 | return SR_OK; |
a1bb33af UH |
101 | } |
102 | ||
1a081ca6 | 103 | SR_API GSList *sr_list_hwplugins(void) |
a1bb33af | 104 | { |
a1bb33af UH |
105 | return plugins; |
106 | } | |
107 | ||
1a081ca6 | 108 | SR_API int sr_init_hwplugins(struct sr_device_plugin *plugin) |
8722c31e | 109 | { |
464d12c7 KS |
110 | int num_devices, num_probes, i, j; |
111 | int num_initialized_devices = 0; | |
112 | struct sr_device *device; | |
113 | char **probe_names; | |
8722c31e | 114 | |
8f81fe87 | 115 | sr_dbg("initializing %s plugin", plugin->name); |
8722c31e BV |
116 | num_devices = plugin->init(NULL); |
117 | for (i = 0; i < num_devices; i++) { | |
4d436e71 GM |
118 | num_probes = GPOINTER_TO_INT( |
119 | plugin->get_device_info(i, SR_DI_NUM_PROBES)); | |
c37d2b1b UH |
120 | probe_names = (char **)plugin->get_device_info(i, |
121 | SR_DI_PROBE_NAMES); | |
464d12c7 KS |
122 | |
123 | if (!probe_names) { | |
c37d2b1b UH |
124 | sr_warn("hwplugin: %s: plugin %s does not return a " |
125 | "list of probe names", __func__, plugin->name); | |
464d12c7 KS |
126 | continue; |
127 | } | |
128 | ||
129 | device = sr_device_new(plugin, i); | |
c37d2b1b | 130 | for (j = 0; j < num_probes; j++) |
464d12c7 | 131 | sr_device_probe_add(device, probe_names[j]); |
464d12c7 | 132 | num_initialized_devices++; |
8722c31e BV |
133 | } |
134 | ||
464d12c7 | 135 | return num_initialized_devices; |
8722c31e BV |
136 | } |
137 | ||
1a081ca6 | 138 | SR_API void sr_cleanup_hwplugins(void) |
8722c31e BV |
139 | { |
140 | struct sr_device_plugin *plugin; | |
141 | GSList *l; | |
142 | ||
143 | for (l = plugins; l; l = l->next) { | |
144 | plugin = l->data; | |
145 | if (plugin->cleanup) | |
146 | plugin->cleanup(); | |
147 | } | |
8722c31e BV |
148 | } |
149 | ||
1a081ca6 | 150 | SR_API struct sr_device_instance *sr_device_instance_new(int index, int status, |
49d0ce50 | 151 | const char *vendor, const char *model, const char *version) |
a1bb33af | 152 | { |
a00ba012 | 153 | struct sr_device_instance *sdi; |
a1bb33af | 154 | |
8722c31e | 155 | if (!(sdi = g_malloc(sizeof(struct sr_device_instance)))) |
a1bb33af UH |
156 | return NULL; |
157 | ||
158 | sdi->index = index; | |
159 | sdi->status = status; | |
160 | sdi->instance_type = -1; | |
8722c31e BV |
161 | sdi->vendor = vendor ? g_strdup(vendor) : NULL; |
162 | sdi->model = model ? g_strdup(model) : NULL; | |
163 | sdi->version = version ? g_strdup(version) : NULL; | |
01cf8814 | 164 | sdi->priv = NULL; |
a1bb33af UH |
165 | |
166 | return sdi; | |
167 | } | |
168 | ||
1a081ca6 UH |
169 | SR_API struct sr_device_instance *sr_get_device_instance( |
170 | GSList *device_instances, int device_index) | |
a1bb33af | 171 | { |
a00ba012 | 172 | struct sr_device_instance *sdi; |
a1bb33af UH |
173 | GSList *l; |
174 | ||
62c82025 | 175 | for (l = device_instances; l; l = l->next) { |
a00ba012 | 176 | sdi = (struct sr_device_instance *)(l->data); |
62c82025 | 177 | if (sdi->index == device_index) |
a1bb33af UH |
178 | return sdi; |
179 | } | |
b08024a8 | 180 | sr_warn("could not find device index %d instance", device_index); |
a1bb33af UH |
181 | |
182 | return NULL; | |
183 | } | |
184 | ||
1a081ca6 | 185 | SR_API void sr_device_instance_free(struct sr_device_instance *sdi) |
a1bb33af | 186 | { |
66410a86 | 187 | g_free(sdi->priv); |
8722c31e BV |
188 | g_free(sdi->vendor); |
189 | g_free(sdi->model); | |
190 | g_free(sdi->version); | |
191 | g_free(sdi); | |
a1bb33af UH |
192 | } |
193 | ||
22b02383 UH |
194 | #ifdef HAVE_LIBUSB_1_0 |
195 | ||
1a081ca6 | 196 | SR_PRIV struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus, |
62c82025 | 197 | uint8_t address, struct libusb_device_handle *hdl) |
a1bb33af | 198 | { |
6c290072 | 199 | struct sr_usb_device_instance *udi; |
a1bb33af | 200 | |
6c290072 | 201 | if (!(udi = malloc(sizeof(struct sr_usb_device_instance)))) |
a1bb33af UH |
202 | return NULL; |
203 | ||
204 | udi->bus = bus; | |
205 | udi->address = address; | |
49d0ce50 | 206 | udi->devhdl = hdl; /* TODO: Check if this is NULL? */ |
a1bb33af UH |
207 | |
208 | return udi; | |
209 | } | |
210 | ||
1a081ca6 | 211 | SR_PRIV void sr_usb_device_instance_free(struct sr_usb_device_instance *usb) |
a1bb33af | 212 | { |
17e1afcb | 213 | /* Avoid compiler warnings. */ |
cb93f8a9 | 214 | (void)usb; |
a1bb33af | 215 | |
62c82025 | 216 | /* Nothing to do for this device instance type. */ |
a1bb33af UH |
217 | } |
218 | ||
22b02383 UH |
219 | #endif |
220 | ||
1a081ca6 | 221 | SR_PRIV struct sr_serial_device_instance *sr_serial_device_instance_new( |
49d0ce50 | 222 | const char *port, int fd) |
a1bb33af | 223 | { |
6c290072 | 224 | struct sr_serial_device_instance *serial; |
a1bb33af | 225 | |
6c290072 | 226 | if (!(serial = malloc(sizeof(struct sr_serial_device_instance)))) |
a1bb33af UH |
227 | return NULL; |
228 | ||
229 | serial->port = strdup(port); | |
230 | serial->fd = fd; | |
231 | ||
232 | return serial; | |
233 | } | |
234 | ||
1a081ca6 UH |
235 | SR_PRIV void sr_serial_device_instance_free( |
236 | struct sr_serial_device_instance *serial) | |
a1bb33af | 237 | { |
a1bb33af | 238 | free(serial->port); |
a1bb33af UH |
239 | } |
240 | ||
1a081ca6 | 241 | SR_API int sr_find_hwcap(int *capabilities, int hwcap) |
a1bb33af UH |
242 | { |
243 | int i; | |
244 | ||
49d0ce50 | 245 | for (i = 0; capabilities[i]; i++) { |
62c82025 | 246 | if (capabilities[i] == hwcap) |
a1bb33af | 247 | return TRUE; |
49d0ce50 | 248 | } |
a1bb33af UH |
249 | |
250 | return FALSE; | |
251 | } | |
252 | ||
1a081ca6 | 253 | SR_API struct sr_hwcap_option *sr_find_hwcap_option(int hwcap) |
a1bb33af | 254 | { |
a1bb33af UH |
255 | int i; |
256 | ||
a65de030 UH |
257 | for (i = 0; sr_hwcap_options[i].capability; i++) { |
258 | if (sr_hwcap_options[i].capability == hwcap) | |
259 | return &sr_hwcap_options[i]; | |
a1bb33af UH |
260 | } |
261 | ||
49d0ce50 | 262 | return NULL; |
a1bb33af UH |
263 | } |
264 | ||
544a4582 BV |
265 | /* unnecessary level of indirection follows. */ |
266 | ||
6f1be0a2 | 267 | void sr_source_remove(int fd) |
a1bb33af | 268 | { |
8a2efef2 | 269 | sr_session_source_remove(fd); |
a1bb33af UH |
270 | } |
271 | ||
6f1be0a2 | 272 | void sr_source_add(int fd, int events, int timeout, |
a887e3da | 273 | sr_receive_data_callback rcv_cb, void *user_data) |
a1bb33af | 274 | { |
8a2efef2 | 275 | sr_session_source_add(fd, events, timeout, rcv_cb, user_data); |
a1bb33af | 276 | } |