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