]>
Commit | Line | Data |
---|---|---|
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 *channels[] = { | |
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_channel *ch; | |
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, 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 (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, | |
90 | channels[i]))) { | |
91 | sr_dbg("Channel malloc failed."); | |
92 | return NULL; | |
93 | } | |
94 | sdi->channels = g_slist_append(sdi->channels, ch); | |
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 | return ((struct drv_context *)(di->priv))->instances; | |
117 | } | |
118 | ||
119 | static int dev_open(struct sr_dev_inst *sdi) | |
120 | { | |
121 | struct drv_context *drvc; | |
122 | struct sr_usb_dev_inst *usb; | |
123 | int ret; | |
124 | ||
125 | if (!(drvc = di->priv)) { | |
126 | sr_err("Driver was not initialized."); | |
127 | return SR_ERR; | |
128 | } | |
129 | ||
130 | usb = sdi->conn; | |
131 | ||
132 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) | |
133 | return SR_ERR; | |
134 | ||
135 | /* | |
136 | * The libusbx 1.0.9 darwin backend is broken: it can report a kernel | |
137 | * driver being active, but detaching it always returns an error. | |
138 | */ | |
139 | #if !defined(__APPLE__) | |
140 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { | |
141 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { | |
142 | sr_err("failed to detach kernel driver: %s", | |
143 | libusb_error_name(ret)); | |
144 | return SR_ERR; | |
145 | } | |
146 | } | |
147 | #endif | |
148 | ||
149 | if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) { | |
150 | sr_err("Failed to set configuration: %s.", libusb_error_name(ret)); | |
151 | return SR_ERR; | |
152 | } | |
153 | ||
154 | if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) { | |
155 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); | |
156 | return SR_ERR; | |
157 | } | |
158 | sdi->status = SR_ST_ACTIVE; | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | static int dev_close(struct sr_dev_inst *sdi) | |
164 | { | |
165 | struct sr_usb_dev_inst *usb; | |
166 | ||
167 | if (!di->priv) { | |
168 | sr_err("Driver was not initialized."); | |
169 | return SR_ERR; | |
170 | } | |
171 | ||
172 | usb = sdi->conn; | |
173 | if (!usb->devhdl) | |
174 | /* Nothing to do. */ | |
175 | return SR_OK; | |
176 | ||
177 | libusb_release_interface(usb->devhdl, USB_INTERFACE); | |
178 | libusb_close(usb->devhdl); | |
179 | usb->devhdl = NULL; | |
180 | sdi->status = SR_ST_INACTIVE; | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int cleanup(void) | |
186 | { | |
187 | int ret; | |
188 | struct drv_context *drvc; | |
189 | ||
190 | if (!(drvc = di->priv)) | |
191 | /* Can get called on an unused driver, doesn't matter. */ | |
192 | return SR_OK; | |
193 | ||
194 | ||
195 | ret = std_dev_clear(di, NULL); | |
196 | g_free(drvc); | |
197 | di->priv = NULL; | |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
203 | const struct sr_channel_group *cg) | |
204 | { | |
205 | struct dev_context *devc; | |
206 | ||
207 | (void)cg; | |
208 | ||
209 | devc = sdi->priv; | |
210 | switch (key) { | |
211 | case SR_CONF_LIMIT_SAMPLES: | |
212 | *data = g_variant_new_uint64(devc->limit_samples); | |
213 | break; | |
214 | case SR_CONF_DATA_SOURCE: | |
215 | if (devc->data_source == DATA_SOURCE_LIVE) | |
216 | *data = g_variant_new_string("Live"); | |
217 | else | |
218 | *data = g_variant_new_string("Memory"); | |
219 | break; | |
220 | default: | |
221 | return SR_ERR_NA; | |
222 | } | |
223 | ||
224 | return SR_OK; | |
225 | } | |
226 | ||
227 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
228 | const struct sr_channel_group *cg) | |
229 | { | |
230 | struct dev_context *devc; | |
231 | int ret; | |
232 | const char *tmp_str; | |
233 | ||
234 | (void)cg; | |
235 | ||
236 | if (sdi->status != SR_ST_ACTIVE) | |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
239 | if (!di->priv) { | |
240 | sr_err("Driver was not initialized."); | |
241 | return SR_ERR; | |
242 | } | |
243 | ||
244 | devc = sdi->priv; | |
245 | ret = SR_OK; | |
246 | switch (key) { | |
247 | case SR_CONF_LIMIT_SAMPLES: | |
248 | devc->limit_samples = g_variant_get_uint64(data); | |
249 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
250 | devc->limit_samples); | |
251 | break; | |
252 | case SR_CONF_DATA_SOURCE: | |
253 | tmp_str = g_variant_get_string(data, NULL); | |
254 | if (!strcmp(tmp_str, "Live")) | |
255 | devc->data_source = DATA_SOURCE_LIVE; | |
256 | else if (!strcmp(tmp_str, "Memory")) | |
257 | devc->data_source = DATA_SOURCE_MEMORY; | |
258 | else | |
259 | return SR_ERR; | |
260 | break; | |
261 | default: | |
262 | ret = SR_ERR_NA; | |
263 | } | |
264 | ||
265 | return ret; | |
266 | } | |
267 | ||
268 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
269 | const struct sr_channel_group *cg) | |
270 | { | |
271 | ||
272 | (void)sdi; | |
273 | (void)cg; | |
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 | int len, ret; | |
297 | unsigned char cmd[2]; | |
298 | ||
299 | if (sdi->status != SR_ST_ACTIVE) | |
300 | return SR_ERR_DEV_CLOSED; | |
301 | ||
302 | drvc = di->priv; | |
303 | devc = sdi->priv; | |
304 | usb = sdi->conn; | |
305 | ||
306 | devc->cb_data = cb_data; | |
307 | devc->num_samples = 0; | |
308 | devc->packet_len = 0; | |
309 | ||
310 | /* Configure serial port parameters on USB-UART interface | |
311 | * chip inside the device (just baudrate 2400 actually). */ | |
312 | cmd[0] = 0x09; | |
313 | cmd[1] = 0x60; | |
314 | ret = libusb_control_transfer(usb->devhdl, 0x21, 0x09, 0x0300, 0x00, | |
315 | cmd, 2, 5); | |
316 | if (ret != 2) { | |
317 | sr_dbg("Failed to configure CH9325: %s", libusb_error_name(ret)); | |
318 | return SR_ERR; | |
319 | } | |
320 | ||
321 | /* Send header packet to the session bus. */ | |
322 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
323 | ||
324 | if (!(devc->xfer = libusb_alloc_transfer(0))) | |
325 | return SR_ERR; | |
326 | ||
327 | /* Length of payload to follow. */ | |
328 | cmd[0] = 0x01; | |
329 | if (devc->data_source == DATA_SOURCE_LIVE) | |
330 | cmd[1] = CMD_GET_LIVE; | |
331 | else | |
332 | cmd[1] = CMD_GET_STORED; | |
333 | ||
334 | ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5); | |
335 | if (ret != 0 || len != 2) { | |
336 | sr_dbg("Failed to start acquisition: %s", libusb_error_name(ret)); | |
337 | libusb_free_transfer(devc->xfer); | |
338 | return SR_ERR; | |
339 | } | |
340 | ||
341 | libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf, | |
342 | 8, uni_t_ut32x_receive_transfer, (void *)sdi, 15); | |
343 | if (libusb_submit_transfer(devc->xfer) != 0) { | |
344 | libusb_free_transfer(devc->xfer); | |
345 | return SR_ERR; | |
346 | } | |
347 | ||
348 | usb_source_add(sdi->session, drvc->sr_ctx, 10, | |
349 | uni_t_ut32x_handle_events, (void *)sdi); | |
350 | ||
351 | return SR_OK; | |
352 | } | |
353 | ||
354 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
355 | { | |
356 | ||
357 | (void)cb_data; | |
358 | ||
359 | if (sdi->status != SR_ST_ACTIVE) | |
360 | return SR_ERR_DEV_CLOSED; | |
361 | ||
362 | /* Signal USB transfer handler to clean up and stop. */ | |
363 | sdi->status = SR_ST_STOPPING; | |
364 | ||
365 | return SR_OK; | |
366 | } | |
367 | ||
368 | SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info = { | |
369 | .name = "uni-t-ut32x", | |
370 | .longname = "UNI-T UT32x", | |
371 | .api_version = 1, | |
372 | .init = init, | |
373 | .cleanup = cleanup, | |
374 | .scan = scan, | |
375 | .dev_list = dev_list, | |
376 | .dev_clear = NULL, | |
377 | .config_get = config_get, | |
378 | .config_set = config_set, | |
379 | .config_list = config_list, | |
380 | .dev_open = dev_open, | |
381 | .dev_close = dev_close, | |
382 | .dev_acquisition_start = dev_acquisition_start, | |
383 | .dev_acquisition_stop = dev_acquisition_stop, | |
384 | .priv = NULL, | |
385 | }; |