]> sigrok.org Git - libsigrok.git/blob - src/hardware/hantek-6xxx/protocol.c
a43f72bbb546247abcd8ce68cfce68c19478d501
[libsigrok.git] / src / hardware / hantek-6xxx / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Christer Ekholm <christerekholm@gmail.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 <config.h>
21 #include "protocol.h"
22
23 SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi)
24 {
25         struct dev_context *devc;
26         struct drv_context *drvc = sdi->driver->context;
27         struct sr_usb_dev_inst *usb;
28         struct libusb_device_descriptor des;
29         libusb_device **devlist;
30         int err, i;
31         char connection_id[64];
32
33         devc = sdi->priv;
34         usb = sdi->conn;
35
36         if (sdi->status == SR_ST_ACTIVE)
37                 /* Already in use. */
38                 return SR_ERR;
39
40         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
41         for (i = 0; devlist[i]; i++) {
42                 libusb_get_device_descriptor(devlist[i], &des);
43
44                 if (des.idVendor != devc->profile->fw_vid
45                     || des.idProduct != devc->profile->fw_pid)
46                         continue;
47
48                 if ((sdi->status == SR_ST_INITIALIZING) ||
49                                 (sdi->status == SR_ST_INACTIVE)) {
50                         /*
51                          * Check device by its physical USB bus/port address.
52                          */
53                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
54                         if (strcmp(sdi->connection_id, connection_id))
55                                 /* This is not the one. */
56                                 continue;
57                 }
58
59                 if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
60                         if (usb->address == 0xff) {
61                                 /*
62                                  * First time we touch this device after firmware upload,
63                                  * so we don't know the address yet.
64                                  */
65                                 usb->address = libusb_get_device_address(devlist[i]);
66                         }
67
68                         sdi->status = SR_ST_ACTIVE;
69                         sr_info("Opened device on %d.%d (logical) / "
70                                         "%s (physical) interface %d.",
71                                 usb->bus, usb->address,
72                                 sdi->connection_id, USB_INTERFACE);
73                 } else {
74                         sr_err("Failed to open device: %s.",
75                                libusb_error_name(err));
76                 }
77
78                 /* If we made it here, we handled the device (somehow). */
79                 break;
80         }
81
82         libusb_free_device_list(devlist, 1);
83
84         if (sdi->status != SR_ST_ACTIVE)
85                 return SR_ERR;
86
87         return SR_OK;
88 }
89
90 SR_PRIV void hantek_6xxx_close(struct sr_dev_inst *sdi)
91 {
92         struct sr_usb_dev_inst *usb;
93
94         usb = sdi->conn;
95
96         if (!usb->devhdl)
97                 return;
98
99         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
100                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
101         libusb_release_interface(usb->devhdl, USB_INTERFACE);
102         libusb_close(usb->devhdl);
103         usb->devhdl = NULL;
104         sdi->status = SR_ST_INACTIVE;
105 }
106
107 SR_PRIV int hantek_6xxx_get_channeldata(const struct sr_dev_inst *sdi,
108                 libusb_transfer_cb_fn cb, uint32_t data_amount)
109 {
110         struct sr_usb_dev_inst *usb;
111         struct libusb_transfer *transfer;
112         int ret;
113         unsigned char *buf;
114
115         sr_dbg("Request channel data.");
116
117         usb = sdi->conn;
118
119         if (!(buf = g_try_malloc(data_amount))) {
120                 sr_err("Failed to malloc USB endpoint buffer.");
121                 return SR_ERR_MALLOC;
122         }
123         transfer = libusb_alloc_transfer(0);
124         libusb_fill_bulk_transfer(transfer, usb->devhdl, HANTEK_EP_IN, buf,
125                         data_amount, cb, (void *)sdi, 4000);
126         if ((ret = libusb_submit_transfer(transfer)) < 0) {
127                 sr_err("Failed to submit transfer: %s.",
128                         libusb_error_name(ret));
129                 /* TODO: Free them all. */
130                 libusb_free_transfer(transfer);
131                 g_free(buf);
132                 return SR_ERR;
133         }
134
135         return SR_OK;
136 }
137
138 static uint8_t samplerate_to_reg(uint64_t samplerate)
139 {
140         const uint64_t samplerate_values[] = {SAMPLERATE_VALUES};
141         const uint8_t samplerate_regs[] = {SAMPLERATE_REGS};
142         uint32_t i;
143
144         for (i = 0; i < ARRAY_SIZE(samplerate_values); i++) {
145                 if (samplerate_values[i] == samplerate)
146                         return samplerate_regs[i];
147         }
148
149         sr_err("Failed to convert samplerate: %" PRIu64 ".", samplerate);
150
151         return samplerate_regs[ARRAY_SIZE(samplerate_values) - 1];
152 }
153
154 static uint8_t voltage_to_reg(uint8_t state)
155 {
156         const uint8_t vdiv_reg[] = {VDIV_REG};
157
158         if (state < ARRAY_SIZE(vdiv_reg)) {
159                 return vdiv_reg[state];
160         } else {
161                 sr_err("Failed to convert vdiv: %d.", state);
162                 return vdiv_reg[ARRAY_SIZE(vdiv_reg) - 1];
163         }
164 }
165
166 static int write_control(const struct sr_dev_inst *sdi,
167                 enum control_requests reg, uint8_t value)
168 {
169         struct sr_usb_dev_inst *usb = sdi->conn;
170         int ret;
171
172         sr_spew("hantek_6xxx_write_control: 0x%x 0x%x", reg, value);
173
174         if ((ret = libusb_control_transfer(usb->devhdl,
175                         LIBUSB_REQUEST_TYPE_VENDOR, (uint8_t)reg,
176                         0, 0, &value, 1, 100)) <= 0) {
177                 sr_err("Failed to control transfer: 0x%x: %s.", reg,
178                         libusb_error_name(ret));
179                 return ret;
180         }
181
182         return 0;
183 }
184
185 SR_PRIV int hantek_6xxx_start_data_collecting(const struct sr_dev_inst *sdi)
186 {
187         sr_dbg("trigger");
188         return write_control(sdi, TRIGGER_REG, 1);
189 }
190
191 SR_PRIV int hantek_6xxx_stop_data_collecting(const struct sr_dev_inst *sdi)
192 {
193         return write_control(sdi, TRIGGER_REG, 0);
194 }
195
196 SR_PRIV int hantek_6xxx_update_samplerate(const struct sr_dev_inst *sdi)
197 {
198         struct dev_context *devc = sdi->priv;
199         sr_dbg("update samplerate %d", samplerate_to_reg(devc->samplerate));
200
201         return write_control(sdi, SAMPLERATE_REG, samplerate_to_reg(devc->samplerate));
202 }
203
204 SR_PRIV int hantek_6xxx_update_vdiv(const struct sr_dev_inst *sdi)
205 {
206         struct dev_context *devc = sdi->priv;
207         int ret1, ret2;
208
209         sr_dbg("update vdiv %d %d", voltage_to_reg(devc->voltage[0]),
210                 voltage_to_reg(devc->voltage[1]));
211
212         ret1 = write_control(sdi, VDIV_CH1_REG, voltage_to_reg(devc->voltage[0]));
213         ret2 = write_control(sdi, VDIV_CH2_REG, voltage_to_reg(devc->voltage[1]));
214
215         return MIN(ret1, ret2);
216 }
217
218 SR_PRIV int hantek_6xxx_update_coupling(const struct sr_dev_inst *sdi)
219 {
220         struct dev_context *devc = sdi->priv;
221         uint8_t coupling = 0xFF & ((devc->coupling[1] << 4) | devc->coupling[0]);
222
223         if (devc->has_coupling) {
224                 sr_dbg("update coupling 0x%x", coupling);
225                 return write_control(sdi, COUPLING_REG, coupling);
226         } else {
227                 sr_dbg("coupling not supported");
228                 return SR_OK;
229         }
230 }
231
232 SR_PRIV int hantek_6xxx_update_channels(const struct sr_dev_inst *sdi)
233 {
234         struct dev_context *devc = sdi->priv;
235         uint8_t chan = devc->ch_enabled[1] ? 2 : 1;
236         sr_dbg("update channels amount %d", chan);
237
238         return write_control(sdi, CHANNELS_REG, chan);
239 }
240
241 SR_PRIV int hantek_6xxx_init(const struct sr_dev_inst *sdi)
242 {
243         sr_dbg("Initializing");
244
245         hantek_6xxx_update_samplerate(sdi);
246         hantek_6xxx_update_vdiv(sdi);
247         hantek_6xxx_update_coupling(sdi);
248         // hantek_6xxx_update_channels(sdi); /* Only 2 channel mode supported. */
249
250         return SR_OK;
251 }