]> sigrok.org Git - libsigrok.git/blame - hardware/uni-t-ut32x/api.c
probe_groups: API changes required to implement probe groups.
[libsigrok.git] / hardware / uni-t-ut32x / api.c
CommitLineData
3877dde4
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 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 "protocol.h"
21
6513f97f
BV
22#include <string.h>
23
6513f97f
BV
24static const int32_t hwcaps[] = {
25 SR_CONF_THERMOMETER,
26 SR_CONF_LIMIT_SAMPLES,
27 SR_CONF_CONTINUOUS,
28 SR_CONF_DATA_SOURCE,
29};
30
31static char *probes[] = {
32 "T1",
33 "T2",
34 "T1-T2",
35};
36
37static const char *data_sources[] = {
38 "Live",
39 "Memory",
40};
41
3877dde4
BV
42SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info;
43static struct sr_dev_driver *di = &uni_t_ut32x_driver_info;
44
45
46static int init(struct sr_context *sr_ctx)
47{
48 return std_init(sr_ctx, di, LOG_PREFIX);
49}
50
51static GSList *scan(GSList *options)
52{
53 struct drv_context *drvc;
6513f97f
BV
54 struct dev_context *devc;
55 struct sr_dev_inst *sdi;
56 struct sr_probe *probe;
57 struct sr_config *src;
58 GSList *usb_devices, *devices, *l;
59 int i;
60 const char *conn;
3877dde4 61
3877dde4
BV
62 drvc = di->priv;
63 drvc->instances = NULL;
64
6513f97f
BV
65 conn = NULL;
66 for (l = options; l; l = l->next) {
67 src = l->data;
68 switch (src->key) {
69 case SR_CONF_CONN:
70 conn = g_variant_get_string(src->data, NULL);
71 break;
72 }
73 }
74 if (!conn)
75 return NULL;
76
77 devices = NULL;
78 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_CONN))) {
79 /* We have a list of sr_usb_dev_inst matching the connection
80 * string. Wrap them in sr_dev_inst and we're done. */
81 for (l = usb_devices; l; l = l->next) {
82 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR,
83 MODEL, NULL)))
84 return NULL;
85 sdi->driver = di;
86 sdi->inst_type = SR_INST_USB;
87 sdi->conn = l->data;
88 for (i = 0; i < 3; i++) {
89 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
90 probes[i]))) {
91 sr_dbg("Probe malloc failed.");
92 return NULL;
93 }
94 sdi->probes = g_slist_append(sdi->probes, probe);
95 }
96
97 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
98 sr_dbg("Device context malloc failed.");
99 return NULL;
100 }
101 sdi->priv = devc;
102 devc->limit_samples = 0;
103 devc->data_source = DEFAULT_DATA_SOURCE;
104 drvc->instances = g_slist_append(drvc->instances, sdi);
105 devices = g_slist_append(devices, sdi);
106 }
107 g_slist_free(usb_devices);
108 } else
109 g_slist_free_full(usb_devices, g_free);
3877dde4
BV
110
111 return devices;
112}
113
114static GSList *dev_list(void)
115{
45c0841b 116 return ((struct drv_context *)(di->priv))->instances;
3877dde4
BV
117}
118
119static int dev_clear(void)
120{
121 return std_dev_clear(di, NULL);
122}
123
124static int dev_open(struct sr_dev_inst *sdi)
125{
6513f97f
BV
126 struct drv_context *drvc;
127 struct sr_usb_dev_inst *usb;
128 int ret;
129
130 if (!(drvc = di->priv)) {
131 sr_err("Driver was not initialized.");
132 return SR_ERR;
133 }
134
135 usb = sdi->conn;
136
137 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
138 return SR_ERR;
139
140/*
141 * The libusbx 1.0.9 darwin backend is broken: it can report a kernel
142 * driver being active, but detaching it always returns an error.
143 */
144#if !defined(__APPLE__)
d6ff054a
BV
145 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
146 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
6513f97f
BV
147 sr_err("failed to detach kernel driver: %s",
148 libusb_error_name(ret));
149 return SR_ERR;
150 }
151 }
152#endif
3877dde4 153
d6ff054a 154 if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) {
6513f97f
BV
155 sr_err("Failed to set configuration: %s.", libusb_error_name(ret));
156 return SR_ERR;
157 }
3877dde4 158
6513f97f
BV
159 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
160 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
161 return SR_ERR;
162 }
3877dde4
BV
163 sdi->status = SR_ST_ACTIVE;
164
6513f97f 165 return ret;
3877dde4
BV
166}
167
168static int dev_close(struct sr_dev_inst *sdi)
169{
6513f97f 170 struct sr_usb_dev_inst *usb;
3877dde4 171
6513f97f
BV
172 if (!di->priv) {
173 sr_err("Driver was not initialized.");
174 return SR_ERR;
175 }
176
177 usb = sdi->conn;
178 if (!usb->devhdl)
179 /* Nothing to do. */
180 return SR_OK;
3877dde4 181
6513f97f
BV
182 libusb_release_interface(usb->devhdl, USB_INTERFACE);
183 libusb_close(usb->devhdl);
184 usb->devhdl = NULL;
3877dde4
BV
185 sdi->status = SR_ST_INACTIVE;
186
187 return SR_OK;
188}
189
190static int cleanup(void)
191{
6513f97f
BV
192 int ret;
193 struct drv_context *drvc;
3877dde4 194
6513f97f
BV
195 if (!(drvc = di->priv))
196 /* Can get called on an unused driver, doesn't matter. */
197 return SR_OK;
3877dde4 198
6513f97f
BV
199 ret = dev_clear();
200 g_free(drvc);
201 di->priv = NULL;
202
203 return ret;
3877dde4
BV
204}
205
d3c74a6f
BV
206static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
207 const struct sr_probe_group *probe_group)
3877dde4 208{
6513f97f 209 struct dev_context *devc;
3877dde4 210
d3c74a6f
BV
211 (void)probe_group;
212
6513f97f 213 devc = sdi->priv;
3877dde4 214 switch (key) {
6513f97f
BV
215 case SR_CONF_LIMIT_SAMPLES:
216 *data = g_variant_new_uint64(devc->limit_samples);
217 break;
218 case SR_CONF_DATA_SOURCE:
219 if (devc->data_source == DATA_SOURCE_LIVE)
220 *data = g_variant_new_string("Live");
221 else
222 *data = g_variant_new_string("Memory");
223 break;
3877dde4
BV
224 default:
225 return SR_ERR_NA;
226 }
227
6513f97f 228 return SR_OK;
3877dde4
BV
229}
230
d3c74a6f
BV
231static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
232 const struct sr_probe_group *probe_group)
3877dde4 233{
6513f97f 234 struct dev_context *devc;
3877dde4 235 int ret;
6513f97f 236 const char *tmp_str;
3877dde4 237
d3c74a6f
BV
238 (void)probe_group;
239
3877dde4
BV
240 if (sdi->status != SR_ST_ACTIVE)
241 return SR_ERR_DEV_CLOSED;
242
6513f97f
BV
243 if (!di->priv) {
244 sr_err("Driver was not initialized.");
245 return SR_ERR;
246 }
247
248 devc = sdi->priv;
3877dde4
BV
249 ret = SR_OK;
250 switch (key) {
6513f97f
BV
251 case SR_CONF_LIMIT_SAMPLES:
252 devc->limit_samples = g_variant_get_uint64(data);
253 sr_dbg("Setting sample limit to %" PRIu64 ".",
254 devc->limit_samples);
255 break;
256 case SR_CONF_DATA_SOURCE:
257 tmp_str = g_variant_get_string(data, NULL);
258 if (!strcmp(tmp_str, "Live"))
259 devc->data_source = DATA_SOURCE_LIVE;
260 else if (!strcmp(tmp_str, "Memory"))
261 devc->data_source = DATA_SOURCE_MEMORY;
262 else
263 return SR_ERR;
264 break;
3877dde4
BV
265 default:
266 ret = SR_ERR_NA;
267 }
268
269 return ret;
270}
271
d3c74a6f
BV
272static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
273 const struct sr_probe_group *probe_group)
3877dde4 274{
3877dde4
BV
275
276 (void)sdi;
d3c74a6f 277 (void)probe_group;
3877dde4 278
3877dde4 279 switch (key) {
6513f97f
BV
280 case SR_CONF_DEVICE_OPTIONS:
281 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
282 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
283 break;
284 case SR_CONF_DATA_SOURCE:
285 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
286 break;
3877dde4
BV
287 default:
288 return SR_ERR_NA;
289 }
290
6513f97f 291 return SR_OK;
3877dde4
BV
292}
293
294static int dev_acquisition_start(const struct sr_dev_inst *sdi,
295 void *cb_data)
296{
6513f97f
BV
297 struct drv_context *drvc;
298 struct dev_context *devc;
299 struct sr_usb_dev_inst *usb;
300 const struct libusb_pollfd **pfd;
301 int len, ret, i;
302 unsigned char cmd[2];
3877dde4
BV
303
304 if (sdi->status != SR_ST_ACTIVE)
305 return SR_ERR_DEV_CLOSED;
306
6513f97f
BV
307 drvc = di->priv;
308 devc = sdi->priv;
309 usb = sdi->conn;
310
311 devc->cb_data = cb_data;
312 devc->num_samples = 0;
d6ff054a
BV
313 devc->packet_len = 0;
314
315 /* Configure serial port parameters on USB-UART interface
316 * chip inside the device (just baudrate 2400 actually). */
317 cmd[0] = 0x09;
318 cmd[1] = 0x60;
319 ret = libusb_control_transfer(usb->devhdl, 0x21, 0x09, 0x0300, 0x00,
320 cmd, 2, 5);
321 if (ret != 2) {
322 sr_dbg("Failed to configure CH9325: %s", libusb_error_name(ret));
323 return SR_ERR;
324 }
6513f97f
BV
325
326 /* Send header packet to the session bus. */
327 std_session_send_df_header(cb_data, LOG_PREFIX);
328
329 if (!(devc->xfer = libusb_alloc_transfer(0)))
330 return SR_ERR;
331
6513f97f
BV
332 /* Length of payload to follow. */
333 cmd[0] = 0x01;
334 if (devc->data_source == DATA_SOURCE_LIVE)
335 cmd[1] = CMD_GET_LIVE;
336 else
337 cmd[1] = CMD_GET_STORED;
338
339 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
340 if (ret != 0 || len != 2) {
341 sr_dbg("Failed to start acquisition: %s", libusb_error_name(ret));
342 libusb_free_transfer(devc->xfer);
343 return SR_ERR;
344 }
345
346 libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf,
d6ff054a 347 8, uni_t_ut32x_receive_transfer, (void *)sdi, 15);
6513f97f
BV
348 if (libusb_submit_transfer(devc->xfer) != 0) {
349 libusb_free_transfer(devc->xfer);
350 return SR_ERR;
351 }
3877dde4 352
d6ff054a
BV
353 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
354 for (i = 0; pfd[i]; i++) {
355 /* Handle USB events every 10ms. */
356 sr_source_add(pfd[i]->fd, pfd[i]->events, 10,
357 uni_t_ut32x_handle_events, (void *)sdi);
358 /* We'll need to remove this fd later. */
359 devc->usbfd[i] = pfd[i]->fd;
360 }
361 devc->usbfd[i] = -1;
362
3877dde4
BV
363 return SR_OK;
364}
365
366static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
367{
d6ff054a 368
3877dde4
BV
369 (void)cb_data;
370
371 if (sdi->status != SR_ST_ACTIVE)
372 return SR_ERR_DEV_CLOSED;
373
d6ff054a
BV
374 /* Signal USB transfer handler to clean up and stop. */
375 sdi->status = SR_ST_STOPPING;
3877dde4
BV
376
377 return SR_OK;
378}
379
380SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info = {
381 .name = "uni-t-ut32x",
382 .longname = "UNI-T UT32x",
383 .api_version = 1,
384 .init = init,
385 .cleanup = cleanup,
386 .scan = scan,
387 .dev_list = dev_list,
388 .dev_clear = dev_clear,
389 .config_get = config_get,
390 .config_set = config_set,
391 .config_list = config_list,
392 .dev_open = dev_open,
393 .dev_close = dev_close,
394 .dev_acquisition_start = dev_acquisition_start,
395 .dev_acquisition_stop = dev_acquisition_stop,
396 .priv = NULL,
397};