]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
initial version of alsa plugin.
[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 <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 "config.h"
28
29 source_callback_add source_cb_add = NULL;
30 source_callback_remove source_cb_remove = NULL;
31
32 /* The list of loaded plugins lives here. */
33 GSList *plugins;
34
35 /*
36  * This enumerates which plugin capabilities correspond to user-settable
37  * options.
38  */
39 struct hwcap_option hwcap_options[] = {
40         {HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate"},
41         {HWCAP_CAPTURE_RATIO, T_UINT64, "Pre-trigger capture ratio", "captureratio"},
42         {HWCAP_PATTERN_MODE, T_CHAR, "Pattern generator mode", "patternmode"},
43         {0, 0, NULL, NULL},
44 };
45
46 #ifdef HAVE_LA_DEMO
47 extern struct device_plugin demo_plugin_info;
48 #endif
49 #ifdef HAVE_LA_SALEAE_LOGIC
50 extern struct device_plugin saleae_logic_plugin_info;
51 #endif
52 #ifdef HAVE_LA_OLS
53 extern struct device_plugin ols_plugin_info;
54 #endif
55 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
56 extern struct device_plugin zeroplus_logic_cube_plugin_info;
57 #endif
58 #ifdef HAVE_LA_ASIX_SIGMA
59 extern struct device_plugin asix_sigma_plugin_info;
60 #endif
61 #ifdef HAVE_LA_LINK_MSO19
62 extern struct device_plugin link_mso19_plugin_info;
63 #endif
64 #ifdef HAVE_LA_ALSA
65 extern struct device_plugin alsa_plugin_info;
66 #endif
67
68
69 /* TODO: No linked list needed, this can be a simple array. */
70 int load_hwplugins(void)
71 {
72 #ifdef HAVE_LA_DEMO
73         plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
74 #endif
75 #ifdef HAVE_LA_SALEAE_LOGIC
76         plugins =
77             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
78 #endif
79 #ifdef HAVE_LA_OLS
80         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
81 #endif
82 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
83         plugins = g_slist_append(plugins,
84                            (gpointer *)&zeroplus_logic_cube_plugin_info);
85 #endif
86 #ifdef HAVE_LA_ASIX_SIGMA
87         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
88 #endif
89 #ifdef HAVE_LA_LINK_MSO19
90         plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
91 #endif
92 #ifdef HAVE_LA_ALSA
93         plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
94 #endif
95
96
97         return SIGROK_OK;
98 }
99
100 GSList *list_hwplugins(void)
101 {
102         return plugins;
103 }
104
105 struct sigrok_device_instance *sigrok_device_instance_new(int index, int status,
106                 const char *vendor, const char *model, const char *version)
107 {
108         struct sigrok_device_instance *sdi;
109
110         if (!(sdi = malloc(sizeof(struct sigrok_device_instance))))
111                 return NULL;
112
113         sdi->index = index;
114         sdi->status = status;
115         sdi->instance_type = -1;
116         sdi->vendor = vendor ? strdup(vendor) : strdup("(unknown)");
117         sdi->model = model ? strdup(model) : NULL;
118         sdi->version = version ? strdup(version) : NULL;
119         sdi->priv = NULL;
120         sdi->usb = NULL;
121
122         return sdi;
123 }
124
125 struct sigrok_device_instance *get_sigrok_device_instance(
126                                 GSList *device_instances, int device_index)
127 {
128         struct sigrok_device_instance *sdi;
129         GSList *l;
130
131         for (l = device_instances; l; l = l->next) {
132                 sdi = (struct sigrok_device_instance *)(l->data);
133                 if (sdi->index == device_index)
134                         return sdi;
135         }
136         g_warning("could not find device index %d instance", device_index);
137
138         return NULL;
139 }
140
141 void sigrok_device_instance_free(struct sigrok_device_instance *sdi)
142 {
143         switch (sdi->instance_type) {
144         case USB_INSTANCE:
145                 usb_device_instance_free(sdi->usb);
146                 break;
147         case SERIAL_INSTANCE:
148                 serial_device_instance_free(sdi->serial);
149                 break;
150         default:
151                 /* No specific type, nothing extra to free. */
152                 break;
153         }
154
155         free(sdi->vendor);
156         free(sdi->model);
157         free(sdi->version);
158         free(sdi);
159 }
160
161 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
162                         uint8_t address, struct libusb_device_handle *hdl)
163 {
164         struct usb_device_instance *udi;
165
166         if (!(udi = malloc(sizeof(struct usb_device_instance))))
167                 return NULL;
168
169         udi->bus = bus;
170         udi->address = address;
171         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
172
173         return udi;
174 }
175
176 void usb_device_instance_free(struct usb_device_instance *usb)
177 {
178         /* Avoid compiler warnings. */
179         usb = usb;
180
181         /* Nothing to do for this device instance type. */
182 }
183
184 struct serial_device_instance *serial_device_instance_new(
185                                                 const char *port, int fd)
186 {
187         struct serial_device_instance *serial;
188
189         if (!(serial = malloc(sizeof(struct serial_device_instance))))
190                 return NULL;
191
192         serial->port = strdup(port);
193         serial->fd = fd;
194
195         return serial;
196 }
197
198 void serial_device_instance_free(struct serial_device_instance *serial)
199 {
200         free(serial->port);
201 }
202
203 int find_hwcap(int *capabilities, int hwcap)
204 {
205         int i;
206
207         for (i = 0; capabilities[i]; i++) {
208                 if (capabilities[i] == hwcap)
209                         return TRUE;
210         }
211
212         return FALSE;
213 }
214
215 struct hwcap_option *find_hwcap_option(int hwcap)
216 {
217         int i;
218
219         for (i = 0; hwcap_options[i].capability; i++) {
220                 if (hwcap_options[i].capability == hwcap)
221                         return &hwcap_options[i];
222         }
223
224         return NULL;
225 }
226
227 void source_remove(int fd)
228 {
229         if (source_cb_remove)
230                 source_cb_remove(fd);
231 }
232
233 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
234                 void *user_data)
235 {
236         if (source_cb_add)
237                 source_cb_add(fd, events, timeout, rcv_cb, user_data);
238 }