]> sigrok.org Git - libsigrok.git/blame - hardware/nexus-osciprime/api.c
sr_driver_init(): Improve checks.
[libsigrok.git] / hardware / nexus-osciprime / api.c
CommitLineData
35a078bc
BV
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
523dfc24 20#include <string.h>
35a078bc
BV
21#include <glib.h>
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
24#include "protocol.h"
25
523dfc24
BV
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
32static const int hwopts[] = {
1953564a
BV
33 SR_CONF_CONN,
34 SR_CONF_SERIALCOMM,
523dfc24
BV
35 0,
36};
37
38static const int hwcaps[] = {
1953564a
BV
39 SR_CONF_OSCILLOSCOPE,
40 SR_CONF_LIMIT_SAMPLES,
41 SR_CONF_CONTINUOUS,
42 SR_CONF_TIMEBASE,
43 SR_CONF_VDIV,
523dfc24
BV
44 0,
45};
46
47static 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
94static const char *probe_names[] = {
78693401 95 "CHA", "CHB",
523dfc24
BV
96 NULL,
97};
98
99static const struct sr_rational vdivs[] = {
100 { 1, 1 },
101 { 2, 1 },
102 { 5, 2 },
103 { 5, 1 },
104 { 10, 1 },
105};
106
107
35a078bc
BV
108SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info;
109static struct sr_dev_driver *di = &nexus_osciprime_driver_info;
523dfc24 110static int hw_dev_close(struct sr_dev_inst *sdi);
35a078bc
BV
111
112/* Properly close and free all devices. */
113static int clear_instances(void)
114{
115 struct sr_dev_inst *sdi;
116 struct drv_context *drvc;
117 struct dev_context *devc;
118 GSList *l;
119
120 if (!(drvc = di->priv))
121 return SR_OK;
122
123 for (l = drvc->instances; l; l = l->next) {
124 if (!(sdi = l->data))
125 continue;
126 if (!(devc = sdi->priv))
127 continue;
128
523dfc24
BV
129 hw_dev_close(sdi);
130 sr_usb_dev_inst_free(devc->usb);
35a078bc
BV
131 sr_dev_inst_free(sdi);
132 }
133
134 g_slist_free(drvc->instances);
135 drvc->instances = NULL;
136
137 return SR_OK;
138}
139
523dfc24 140static int hw_init(struct sr_context *sr_ctx)
35a078bc
BV
141{
142 struct drv_context *drvc;
143
144 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
145 sr_err("Driver context malloc failed.");
146 return SR_ERR_MALLOC;
147 }
148
523dfc24 149 drvc->sr_ctx = sr_ctx;
35a078bc
BV
150 di->priv = drvc;
151
152 return SR_OK;
153}
154
155static GSList *hw_scan(GSList *options)
156{
157 struct drv_context *drvc;
523dfc24
BV
158 struct dev_context *devc;
159 struct sr_dev_inst *sdi;
160 struct sr_usb_dev_inst *usb;
1987b8d6 161 struct sr_config *src;
523dfc24
BV
162 struct sr_probe *probe;
163 libusb_device *dev;
164 GSList *usb_devices, *devices, *l;
165 int i;
166 const char *conn;
35a078bc
BV
167
168 (void)options;
169
523dfc24
BV
170 if (!(drvc = di->priv)) {
171 sr_err("Driver was not initialized.");
172 return NULL;
173 }
35a078bc 174
523dfc24
BV
175 /* USB scan is always authoritative. */
176 clear_instances();
177
178 conn = NULL;
179 for (l = options; l; l = l->next) {
1987b8d6
BV
180 src = l->data;
181 switch (src->key) {
1953564a 182 case SR_CONF_CONN:
1987b8d6 183 conn = src->value;
523dfc24
BV
184 break;
185 }
186 }
187 if (!conn)
188 conn = OSCI_VIDPID;
189
190 devices = NULL;
191 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
192 for (l = usb_devices; l; l = l->next) {
193 usb = l->data;
194 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
195 OSCI_VENDOR, OSCI_MODEL, OSCI_VERSION)))
196 return NULL;
197 sdi->driver = di;
198 for (i = 0; probe_names[i]; i++) {
199 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
200 probe_names[i])))
201 return NULL;
202 sdi->probes = g_slist_append(sdi->probes, probe);
203 }
204
205 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
206 return NULL;
207 sdi->priv = devc;
208 devc->usb = usb;
209
210 if (strcmp(conn, OSCI_VIDPID)) {
211 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
212 break;
213 dev = libusb_get_device(usb->devhdl);
214 if (ezusb_upload_firmware(dev, 0, OSCI_FIRMWARE) == SR_OK)
215 /* Remember when the firmware on this device was updated */
216 devc->fw_updated = g_get_monotonic_time();
217 else
218 sr_err("Firmware upload failed for device "
219 "at bus %d address %d.", usb->bus, usb->address);
220 }
221
222 drvc->instances = g_slist_append(drvc->instances, sdi);
223 devices = g_slist_append(devices, sdi);
224 }
225 g_slist_free(usb_devices);
226 } else
227 g_slist_free_full(usb_devices, g_free);
35a078bc
BV
228
229 return devices;
230}
231
232static GSList *hw_dev_list(void)
233{
234 struct drv_context *drvc;
235
236 drvc = di->priv;
237
238 return drvc->instances;
239}
240
241static int hw_dev_open(struct sr_dev_inst *sdi)
242{
df36acb3 243
35a078bc 244 /* TODO */
df36acb3 245 (void)sdi;
35a078bc
BV
246
247 return SR_OK;
248}
249
250static int hw_dev_close(struct sr_dev_inst *sdi)
251{
df36acb3 252
35a078bc 253 /* TODO */
df36acb3 254 (void)sdi;
35a078bc
BV
255
256 return SR_OK;
257}
258
259static int hw_cleanup(void)
260{
261 clear_instances();
262
263 /* TODO */
264
265 return SR_OK;
266}
267
035a1078 268static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
35a078bc
BV
269{
270 int ret;
271
df36acb3
BV
272 /* TODO */
273 (void)value;
274
35a078bc
BV
275 if (sdi->status != SR_ST_ACTIVE) {
276 sr_err("Device inactive, can't set config options.");
277 return SR_ERR;
278 }
279
280 ret = SR_OK;
035a1078 281 switch (id) {
df36acb3 282
35a078bc 283 default:
035a1078 284 sr_err("Unknown hardware capability: %d.", id);
35a078bc
BV
285 ret = SR_ERR_ARG;
286 }
287
288 return ret;
289}
290
a1c743fc
BV
291static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
292{
293
294 (void)sdi;
295
296 switch (key) {
297 default:
298 return SR_ERR_ARG;
299 }
300
301 return SR_OK;
302}
303
35a078bc
BV
304static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
305 void *cb_data)
306{
307 /* TODO */
df36acb3
BV
308 (void)sdi;
309 (void)cb_data;
35a078bc
BV
310
311 return SR_OK;
312}
313
314static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
315{
316 (void)cb_data;
317
318 if (sdi->status != SR_ST_ACTIVE) {
319 sr_err("Device inactive, can't stop acquisition.");
320 return SR_ERR;
321 }
322
323 /* TODO */
324
325 return SR_OK;
326}
327
328SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info = {
329 .name = "nexus-osciprime",
523dfc24 330 .longname = "Nexus OsciPrime",
35a078bc
BV
331 .api_version = 1,
332 .init = hw_init,
333 .cleanup = hw_cleanup,
334 .scan = hw_scan,
335 .dev_list = hw_dev_list,
336 .dev_clear = clear_instances,
035a1078 337 .config_set = config_set,
a1c743fc 338 .config_list = config_list,
35a078bc
BV
339 .dev_open = hw_dev_open,
340 .dev_close = hw_dev_close,
35a078bc
BV
341 .dev_acquisition_start = hw_dev_acquisition_start,
342 .dev_acquisition_stop = hw_dev_acquisition_stop,
343 .priv = NULL,
344};