2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 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/>.
22 #define USB_CONN "1041.8101"
23 #define VENDOR "Kecheng"
24 #define USB_INTERFACE 0
25 #define EP_IN 0x80 | 1
28 static const int32_t hwcaps[] = {
29 SR_CONF_SOUNDLEVELMETER,
30 SR_CONF_LIMIT_SAMPLES,
34 SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info;
35 static struct sr_dev_driver *di = &kecheng_kc_330b_driver_info;
38 static int init(struct sr_context *sr_ctx)
40 return std_init(sr_ctx, di, LOG_PREFIX);
43 static int scan_kecheng(struct sr_usb_dev_inst *usb, char **model)
45 struct drv_context *drvc;
47 unsigned char cmd, buf[32];
50 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
54 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5);
56 libusb_close(usb->devhdl);
57 sr_dbg("Failed to send Identify command: %s", libusb_error_name(ret));
61 ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 32, &len, 10);
63 libusb_close(usb->devhdl);
64 sr_dbg("Failed to receive response: %s", libusb_error_name(ret));
68 libusb_close(usb->devhdl);
71 if (len < 2 || buf[0] != (CMD_IDENTIFY | 0x80) || buf[1] > 30) {
72 sr_dbg("Invalid response to Identify command");
76 buf[buf[1] + 2] = '\x0';
77 *model = g_strndup((const gchar *)buf + 2, 30);
83 static GSList *scan(GSList *options)
85 struct drv_context *drvc;
86 struct dev_context *devc;
87 struct sr_dev_inst *sdi;
88 struct sr_probe *probe;
89 GSList *usb_devices, *devices, *l;
95 drvc->instances = NULL;
98 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_CONN))) {
99 /* We have a list of sr_usb_dev_inst matching the connection
100 * string. Wrap them in sr_dev_inst and we're done. */
101 for (l = usb_devices; l; l = l->next) {
102 if (scan_kecheng(l->data, &model) != SR_OK)
104 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR,
109 sdi->inst_type = SR_INST_USB;
111 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
113 sdi->probes = g_slist_append(sdi->probes, probe);
115 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
116 sr_dbg("Device context malloc failed.");
120 devc->limit_samples = 0;
122 drvc->instances = g_slist_append(drvc->instances, sdi);
123 devices = g_slist_append(devices, sdi);
125 g_slist_free(usb_devices);
127 g_slist_free_full(usb_devices, g_free);
132 static GSList *dev_list(void)
134 struct drv_context *drvc;
138 return drvc->instances;
141 static int dev_clear(void)
143 return std_dev_clear(di, NULL);
146 static int dev_open(struct sr_dev_inst *sdi)
148 struct drv_context *drvc;
149 struct sr_usb_dev_inst *usb;
152 if (!(drvc = di->priv)) {
153 sr_err("Driver was not initialized.");
159 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
162 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
163 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
166 sdi->status = SR_ST_ACTIVE;
171 static int dev_close(struct sr_dev_inst *sdi)
173 struct sr_usb_dev_inst *usb;
176 sr_err("Driver was not initialized.");
186 libusb_release_interface(usb->devhdl, USB_INTERFACE);
187 libusb_close(usb->devhdl);
189 sdi->status = SR_ST_INACTIVE;
194 static int cleanup(void)
197 struct drv_context *drvc;
199 if (!(drvc = di->priv))
200 /* Can get called on an unused driver, doesn't matter. */
210 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
212 struct dev_context *devc;
216 case SR_CONF_LIMIT_SAMPLES:
217 *data = g_variant_new_uint64(devc->limit_samples);
226 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
228 struct dev_context *devc;
231 if (sdi->status != SR_ST_ACTIVE)
232 return SR_ERR_DEV_CLOSED;
235 sr_err("Driver was not initialized.");
242 case SR_CONF_LIMIT_SAMPLES:
243 devc->limit_samples = g_variant_get_uint64(data);
244 sr_dbg("Setting sample limit to %" PRIu64 ".",
245 devc->limit_samples);
254 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
260 case SR_CONF_DEVICE_OPTIONS:
261 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
262 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
271 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
277 if (sdi->status != SR_ST_ACTIVE)
278 return SR_ERR_DEV_CLOSED;
280 /* TODO: configure hardware, reset acquisition state, set up
281 * callbacks and send header packet. */
286 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
290 if (sdi->status != SR_ST_ACTIVE)
291 return SR_ERR_DEV_CLOSED;
293 /* TODO: stop acquisition. */
298 SR_PRIV struct sr_dev_driver kecheng_kc_330b_driver_info = {
299 .name = "kecheng-kc-330b",
300 .longname = "Kecheng KC-330B",
305 .dev_list = dev_list,
306 .dev_clear = dev_clear,
307 .config_get = config_get,
308 .config_set = config_set,
309 .config_list = config_list,
310 .dev_open = dev_open,
311 .dev_close = dev_close,
312 .dev_acquisition_start = dev_acquisition_start,
313 .dev_acquisition_stop = dev_acquisition_stop,