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