2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #define SERIALCOMM "115200/8n1"
25 SR_PRIV struct sr_dev_driver testo_driver_info;
26 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
28 static const uint32_t scanopts[] = {
32 static const uint32_t devopts[] = {
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
39 unsigned char TESTO_x35_REQUEST[] = { 0x12, 0, 0, 0, 1, 1, 0x55, 0xd1, 0xb7 };
40 struct testo_model models[] = {
41 { "435", 9, TESTO_x35_REQUEST },
44 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
46 return std_init(sr_ctx, di, LOG_PREFIX);
49 static GSList *scan(struct sr_dev_driver *di, GSList *options)
51 struct drv_context *drvc;
52 struct dev_context *devc;
53 struct sr_config *src;
54 struct sr_dev_inst *sdi;
55 struct sr_usb_dev_inst *usb;
56 struct libusb_device_descriptor des;
57 libusb_device **devlist;
58 struct libusb_device_handle *hdl;
59 GSList *conn_devices, *devices, *l;
62 char manufacturer[64], product[64], connection_id[64];
66 drvc->instances = NULL;
69 for (l = options; l; l = l->next) {
71 if (src->key != SR_CONF_CONN)
73 str = g_variant_get_string(src->data, NULL);
74 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, str);
77 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
78 for (i = 0; devlist[i]; i++) {
81 for (l = conn_devices; l; l = l->next) {
83 if (usb->bus == libusb_get_bus_number(devlist[i])
84 && usb->address == libusb_get_device_address(devlist[i]))
88 /* This device matched none of the ones that
89 * matched the conn specification. */
93 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
94 sr_warn("Failed to get device descriptor: %s.",
95 libusb_error_name(ret));
99 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
102 manufacturer[0] = product[0] = '\0';
103 if (des.iManufacturer && (ret = libusb_get_string_descriptor_ascii(
104 hdl, des.iManufacturer, (unsigned char *) manufacturer,
105 sizeof(manufacturer))) < 0) {
106 sr_warn("Failed to get manufacturer string descriptor: %s.",
107 libusb_error_name(ret));
109 if (des.iProduct && (ret = libusb_get_string_descriptor_ascii(
110 hdl, des.iProduct, (unsigned char *) product,
111 sizeof(product))) < 0) {
112 sr_warn("Failed to get product string descriptor: %s.",
113 libusb_error_name(ret));
117 if (strncmp(manufacturer, "testo", 5))
120 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
122 /* Hardcode the 435 for now.*/
123 if (strcmp(product, "testo 435/635/735"))
126 sdi = g_malloc0(sizeof(struct sr_dev_inst));
127 sdi->status = SR_ST_INACTIVE;
128 sdi->vendor = g_strdup("Testo");
129 sdi->model = g_strdup("435/635/735");
131 sdi->inst_type = SR_INST_USB;
132 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
133 libusb_get_device_address(devlist[i]), NULL);
134 sdi->connection_id = g_strdup(connection_id);
135 devc = g_malloc(sizeof(struct dev_context));
136 devc->model = &models[0];
137 devc->limit_msec = 0;
138 devc->limit_samples = 0;
140 if (testo_probe_channels(sdi) != SR_OK)
142 drvc->instances = g_slist_append(drvc->instances, sdi);
143 devices = g_slist_append(devices, sdi);
145 libusb_free_device_list(devlist, 1);
146 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
151 static GSList *dev_list(const struct sr_dev_driver *di)
153 return ((struct drv_context *)(di->priv))->instances;
156 static int dev_clear(const struct sr_dev_driver *di)
158 return std_dev_clear(di, NULL);
161 static int dev_open(struct sr_dev_inst *sdi)
163 struct sr_dev_driver *di = sdi->driver;
164 struct drv_context *drvc = di->priv;
165 struct sr_usb_dev_inst *usb;
166 libusb_device **devlist;
168 char connection_id[64];
171 sr_err("Driver was not initialized.");
176 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
177 for (i = 0; devlist[i]; i++) {
178 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
179 if (strcmp(sdi->connection_id, connection_id))
181 if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
182 sr_err("Failed to open device: %s.", libusb_error_name(ret));
187 libusb_free_device_list(devlist, 1);
189 sr_err("Device not found.");
193 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
194 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
195 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
196 sr_err("Failed to detach kernel driver: %s.",
197 libusb_error_name(ret));
203 if ((ret = libusb_claim_interface(usb->devhdl, 0))) {
204 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
207 sdi->status = SR_ST_ACTIVE;
212 static int dev_close(struct sr_dev_inst *sdi)
214 struct sr_dev_driver *di = sdi->driver;
215 struct sr_usb_dev_inst *usb;
218 sr_err("Driver was not initialized.");
227 libusb_release_interface(usb->devhdl, 0);
228 libusb_close(usb->devhdl);
230 sdi->status = SR_ST_INACTIVE;
235 static int cleanup(const struct sr_dev_driver *di)
238 struct drv_context *drvc;
240 if (!(drvc = di->priv))
249 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
250 const struct sr_channel_group *cg)
252 struct sr_usb_dev_inst *usb;
259 if (!sdi || !sdi->conn)
262 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
263 *data = g_variant_new_string(str);
272 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
273 const struct sr_channel_group *cg)
275 struct sr_dev_driver *di = sdi->driver;
276 struct dev_context *devc;
282 if (sdi->status != SR_ST_ACTIVE)
283 return SR_ERR_DEV_CLOSED;
286 sr_err("Driver was not initialized.");
293 case SR_CONF_LIMIT_MSEC:
294 devc->limit_msec = g_variant_get_uint64(data);
295 now = g_get_monotonic_time() / 1000;
296 devc->end_time = now + devc->limit_msec;
297 sr_dbg("Setting time limit to %" PRIu64 "ms.",
300 case SR_CONF_LIMIT_SAMPLES:
301 devc->limit_samples = g_variant_get_uint64(data);
302 sr_dbg("Setting sample limit to %" PRIu64 ".",
303 devc->limit_samples);
312 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
313 const struct sr_channel_group *cg)
319 case SR_CONF_SCAN_OPTIONS:
320 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
321 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
323 case SR_CONF_DEVICE_OPTIONS:
324 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
325 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
334 static void receive_data(struct sr_dev_inst *sdi, unsigned char *data, int len)
336 struct dev_context *devc;
342 if (devc->reply_size + len > MAX_REPLY_SIZE) {
343 /* Something went very wrong. */
344 sr_dbg("Receive buffer overrun.");
345 devc->reply_size = 0;
349 memcpy(devc->reply + devc->reply_size, data, len);
350 devc->reply_size += len;
351 /* Sixth byte contains the length of the packet. */
352 if (devc->reply_size < 7)
355 packet_size = 7 + devc->reply[6] * 7 + 2;
356 if (devc->reply_size < packet_size)
359 if (!testo_check_packet_prefix(devc->reply, devc->reply_size))
362 crc = crc16_mcrf4xx(0xffff, devc->reply, devc->reply_size - 2);
363 if (crc == RL16(&devc->reply[devc->reply_size - 2])) {
364 testo_receive_packet(sdi);
367 sr_dbg("Packet has invalid CRC.");
370 devc->reply_size = 0;
371 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
372 dev_acquisition_stop(sdi, devc->cb_data);
374 testo_request_packet(sdi);
378 SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
380 struct dev_context *devc;
381 struct sr_dev_inst *sdi;
384 sdi = transfer->user_data;
386 if (transfer == devc->out_transfer)
387 /* Just the command acknowledgement. */
390 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
391 /* USB device was unplugged. */
392 dev_acquisition_stop(sdi, devc->cb_data);
393 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
394 /* First two bytes in any transfer are FTDI status bytes. */
395 if (transfer->actual_length > 2)
396 receive_data(sdi, transfer->buffer + 2, transfer->actual_length - 2);
398 /* Anything else is either an error or a timeout, which is fine:
399 * we were just going to send another transfer request anyway. */
401 if (sdi->status == SR_ST_ACTIVE) {
402 if ((ret = libusb_submit_transfer(transfer) != 0)) {
403 sr_err("Unable to resubmit transfer: %s.",
404 libusb_error_name(ret));
405 g_free(transfer->buffer);
406 libusb_free_transfer(transfer);
407 dev_acquisition_stop(sdi, devc->cb_data);
410 /* This was the last transfer we're going to receive, so
412 g_free(transfer->buffer);
413 libusb_free_transfer(transfer);
417 static int handle_events(int fd, int revents, void *cb_data)
419 struct sr_dev_driver *di;
420 struct dev_context *devc;
421 struct drv_context *drvc;
422 struct sr_datafeed_packet packet;
423 struct sr_dev_inst *sdi;
435 if (devc->limit_msec) {
436 now = g_get_monotonic_time() / 1000;
437 if (now > devc->end_time)
438 dev_acquisition_stop(sdi, devc->cb_data);
441 if (sdi->status == SR_ST_STOPPING) {
442 usb_source_remove(sdi->session, drvc->sr_ctx);
446 packet.type = SR_DF_END;
447 sr_session_send(sdi, &packet);
450 memset(&tv, 0, sizeof(struct timeval));
451 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
457 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
459 struct sr_dev_driver *di = sdi->driver;
460 struct drv_context *drvc;
461 struct dev_context *devc;
462 struct sr_usb_dev_inst *usb;
463 struct libusb_transfer *transfer;
468 if (sdi->status != SR_ST_ACTIVE)
469 return SR_ERR_DEV_CLOSED;
472 sr_err("Driver was not initialized.");
478 devc->cb_data = cb_data;
480 devc->num_samples = 0;
481 devc->reply_size = 0;
483 /* Send header packet to the session bus. */
484 std_session_send_df_header(cb_data, LOG_PREFIX);
486 usb_source_add(sdi->session, drvc->sr_ctx, 100,
487 handle_events, (void *)sdi);
489 if (testo_set_serial_params(usb) != SR_OK)
492 devc->out_transfer = libusb_alloc_transfer(0);
493 if (testo_request_packet(sdi) != SR_OK)
496 buf = g_try_malloc(MAX_REPLY_SIZE);
497 transfer = libusb_alloc_transfer(0);
498 libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf,
499 MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100);
500 if ((ret = libusb_submit_transfer(transfer) != 0)) {
501 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
502 libusb_free_transfer(transfer);
506 devc->reply_size = 0;
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 struct sr_dev_driver *di = sdi->driver;
517 sr_err("Driver was not initialized.");
521 if (sdi->status != SR_ST_ACTIVE)
522 return SR_ERR_DEV_CLOSED;
524 sdi->status = SR_ST_STOPPING;
529 SR_PRIV struct sr_dev_driver testo_driver_info = {
536 .dev_list = dev_list,
537 .dev_clear = dev_clear,
538 .config_get = config_get,
539 .config_set = config_set,
540 .config_list = config_list,
541 .dev_open = dev_open,
542 .dev_close = dev_close,
543 .dev_acquisition_start = dev_acquisition_start,
544 .dev_acquisition_stop = dev_acquisition_stop,