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