]> sigrok.org Git - libsigrok.git/blob - hardware/nexus-osciprime/api.c
nexus-osciprime: suppress warnings
[libsigrok.git] / hardware / nexus-osciprime / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 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 <string.h>
21 #include <glib.h>
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
24 #include "protocol.h"
25
26 #define OSCI_VENDOR "Nexus Computing"
27 #define OSCI_MODEL "OsciPrime"
28 #define OSCI_VERSION "1.0"
29 #define OSCI_FIRMWARE FIRMWARE_DIR "/nexus-osciprime.fw"
30 #define OSCI_VIDPID "04b4.1004"
31
32 static const int hwopts[] = {
33         SR_HWOPT_CONN,
34         SR_HWOPT_SERIALCOMM,
35         0,
36 };
37
38 static const int hwcaps[] = {
39         SR_HWCAP_OSCILLOSCOPE,
40         SR_HWCAP_LIMIT_SAMPLES,
41         SR_HWCAP_CONTINUOUS,
42         SR_HWCAP_TIMEBASE,
43         SR_HWCAP_VDIV,
44         0,
45 };
46
47 static const struct sr_rational timebases[] = {
48         /* 24 MHz */
49         { 42, 1e9 },
50         /* 12 MHz */
51         { 83, 1e9 },
52         /* 6 MHz */
53         { 167, 1e9 },
54         /* 3 MHz */
55         { 333, 1e9 },
56         /* 1.5 MHz */
57         { 667, 1e9 },
58         /* 750 kHz */
59         { 1333, 1e9 },
60         /* 375 kHz */
61         { 2667, 1e9 },
62         /* 187.5 kHz */
63         { 5333, 1e9 },
64         /* 93.25 kHz */
65         { 10724, 1e9 },
66         /* 46.875 kHz */
67         { 21333, 1e9 },
68         /* 23.4375 kHz */
69         { 42666, 1e9 },
70         /* 11.718 kHz */
71         { 85339, 1e9 },
72         /* 5.859 kHz */
73         { 170678, 1e9 },
74         /* 2.929 kHz */
75         { 341413, 1e9 },
76         /* 1.465 kHz */
77         { 682594, 1e9 },
78         /* 732 Hz */
79         { 1366, 1e6 },
80         /* 366 Hz */
81         { 2732, 1e6 },
82         /* 183 Hz */
83         { 5464, 1e6 },
84         /* 91 Hz */
85         { 10989, 1e6 },
86         /* 46 Hz */
87         { 21739, 1e6 },
88         /* 23 Hz */
89         { 43478, 1e6 },
90         /* 12 Hz */
91         { 83333, 1e6 },
92 };
93
94 static const char *probe_names[] = {
95         "CHA",
96         "CHB",
97         NULL,
98 };
99
100 static const struct sr_rational vdivs[] = {
101         { 1, 1 },
102         { 2, 1 },
103         { 5, 2 },
104         { 5, 1 },
105         { 10, 1 },
106 };
107
108
109 SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info;
110 static struct sr_dev_driver *di = &nexus_osciprime_driver_info;
111 static int hw_dev_close(struct sr_dev_inst *sdi);
112
113 /* Properly close and free all devices. */
114 static int clear_instances(void)
115 {
116         struct sr_dev_inst *sdi;
117         struct drv_context *drvc;
118         struct dev_context *devc;
119         GSList *l;
120
121         if (!(drvc = di->priv))
122                 return SR_OK;
123
124         for (l = drvc->instances; l; l = l->next) {
125                 if (!(sdi = l->data))
126                         continue;
127                 if (!(devc = sdi->priv))
128                         continue;
129
130                 hw_dev_close(sdi);
131                 sr_usb_dev_inst_free(devc->usb);
132                 sr_dev_inst_free(sdi);
133         }
134
135         g_slist_free(drvc->instances);
136         drvc->instances = NULL;
137
138         return SR_OK;
139 }
140
141 static int hw_init(struct sr_context *sr_ctx)
142 {
143         struct drv_context *drvc;
144
145         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
146                 sr_err("Driver context malloc failed.");
147                 return SR_ERR_MALLOC;
148         }
149
150         drvc->sr_ctx = sr_ctx;
151         di->priv = drvc;
152
153         return SR_OK;
154 }
155
156 static GSList *hw_scan(GSList *options)
157 {
158         struct drv_context *drvc;
159         struct dev_context *devc;
160         struct sr_dev_inst *sdi;
161         struct sr_usb_dev_inst *usb;
162         struct sr_hwopt *opt;
163         struct sr_probe *probe;
164         libusb_device *dev;
165         GSList *usb_devices, *devices, *l;
166         int i;
167         const char *conn;
168
169         (void)options;
170
171         if (!(drvc = di->priv)) {
172                 sr_err("Driver was not initialized.");
173                 return NULL;
174         }
175
176         /* USB scan is always authoritative. */
177         clear_instances();
178
179         conn = NULL;
180         for (l = options; l; l = l->next) {
181                 opt = l->data;
182                 switch (opt->hwopt) {
183                 case SR_HWOPT_CONN:
184                         conn = opt->value;
185                         break;
186                 }
187         }
188         if (!conn)
189                 conn = OSCI_VIDPID;
190
191         devices = NULL;
192         if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
193                 for (l = usb_devices; l; l = l->next) {
194                         usb = l->data;
195                         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
196                                         OSCI_VENDOR, OSCI_MODEL, OSCI_VERSION)))
197                                 return NULL;
198                         sdi->driver = di;
199                         for (i = 0; probe_names[i]; i++) {
200                                 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
201                                                 probe_names[i])))
202                                         return NULL;
203                                 sdi->probes = g_slist_append(sdi->probes, probe);
204                         }
205
206                         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
207                                 return NULL;
208                         sdi->priv = devc;
209                         devc->usb = usb;
210
211                         if (strcmp(conn, OSCI_VIDPID)) {
212                                 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
213                                         break;
214                                 dev = libusb_get_device(usb->devhdl);
215                                 if (ezusb_upload_firmware(dev, 0, OSCI_FIRMWARE) == SR_OK)
216                                         /* Remember when the firmware on this device was updated */
217                                         devc->fw_updated = g_get_monotonic_time();
218                                 else
219                                         sr_err("Firmware upload failed for device "
220                                                         "at bus %d address %d.", usb->bus, usb->address);
221                         }
222
223                         drvc->instances = g_slist_append(drvc->instances, sdi);
224                         devices = g_slist_append(devices, sdi);
225                 }
226                 g_slist_free(usb_devices);
227         } else
228                 g_slist_free_full(usb_devices, g_free);
229
230         return devices;
231 }
232
233 static GSList *hw_dev_list(void)
234 {
235         struct drv_context *drvc;
236
237         drvc = di->priv;
238
239         return drvc->instances;
240 }
241
242 static int hw_dev_open(struct sr_dev_inst *sdi)
243 {
244
245         /* TODO */
246         (void)sdi;
247
248         return SR_OK;
249 }
250
251 static int hw_dev_close(struct sr_dev_inst *sdi)
252 {
253
254         /* TODO */
255         (void)sdi;
256
257         return SR_OK;
258 }
259
260 static int hw_cleanup(void)
261 {
262         clear_instances();
263
264         /* TODO */
265
266         return SR_OK;
267 }
268
269 static int hw_info_get(int info_id, const void **data,
270                        const struct sr_dev_inst *sdi)
271 {
272
273         /* TODO */
274         (void)data;
275         (void)sdi;
276
277         switch (info_id) {
278         default:
279                 sr_err("Unknown info_id: %d.", info_id);
280                 return SR_ERR_ARG;
281         }
282
283         return SR_OK;
284 }
285
286 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
287                              const void *value)
288 {
289         int ret;
290
291         /* TODO */
292         (void)value;
293
294         if (sdi->status != SR_ST_ACTIVE) {
295                 sr_err("Device inactive, can't set config options.");
296                 return SR_ERR;
297         }
298
299         ret = SR_OK;
300         switch (hwcap) {
301
302         default:
303                 sr_err("Unknown hardware capability: %d.", hwcap);
304                 ret = SR_ERR_ARG;
305         }
306
307         return ret;
308 }
309
310 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
311                                     void *cb_data)
312 {
313         /* TODO */
314         (void)sdi;
315         (void)cb_data;
316
317         return SR_OK;
318 }
319
320 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
321 {
322         (void)cb_data;
323
324         if (sdi->status != SR_ST_ACTIVE) {
325                 sr_err("Device inactive, can't stop acquisition.");
326                 return SR_ERR;
327         }
328
329         /* TODO */
330
331         return SR_OK;
332 }
333
334 SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info = {
335         .name = "nexus-osciprime",
336         .longname = "Nexus OsciPrime",
337         .api_version = 1,
338         .init = hw_init,
339         .cleanup = hw_cleanup,
340         .scan = hw_scan,
341         .dev_list = hw_dev_list,
342         .dev_clear = clear_instances,
343         .dev_open = hw_dev_open,
344         .dev_close = hw_dev_close,
345         .info_get = hw_info_get,
346         .dev_config_set = hw_dev_config_set,
347         .dev_acquisition_start = hw_dev_acquisition_start,
348         .dev_acquisition_stop = hw_dev_acquisition_stop,
349         .priv = NULL,
350 };