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