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