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