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