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