]>
Commit | Line | Data |
---|---|---|
8d9c8554 AJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
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 | ||
6ec6c43b | 20 | #include <config.h> |
8d9c8554 AJ |
21 | #include "protocol.h" |
22 | ||
ecaa89af AJ |
23 | #define BRYMEN_BC86X "0820.0001" |
24 | ||
a0e0bb41 | 25 | static const uint32_t scanopts[] = { |
ecaa89af AJ |
26 | SR_CONF_CONN, |
27 | }; | |
28 | ||
05199c0a | 29 | static const uint32_t drvopts[] = { |
ecaa89af | 30 | SR_CONF_MULTIMETER, |
05199c0a UH |
31 | }; |
32 | ||
33 | static const uint32_t devopts[] = { | |
ecaa89af | 34 | SR_CONF_CONTINUOUS, |
5827f61b BV |
35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
36 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
ecaa89af AJ |
37 | }; |
38 | ||
4f840ce9 | 39 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
8d9c8554 | 40 | { |
ecaa89af | 41 | GSList *usb_devices, *devices, *l; |
8d9c8554 | 42 | struct drv_context *drvc; |
ecaa89af AJ |
43 | struct dev_context *devc; |
44 | struct sr_dev_inst *sdi; | |
45 | struct sr_usb_dev_inst *usb; | |
46 | struct sr_config *src; | |
ecaa89af | 47 | const char *conn; |
8d9c8554 | 48 | |
41812aca | 49 | drvc = di->context; |
8d9c8554 | 50 | |
ecaa89af AJ |
51 | conn = BRYMEN_BC86X; |
52 | for (l = options; l; l = l->next) { | |
53 | src = l->data; | |
54 | switch (src->key) { | |
55 | case SR_CONF_CONN: | |
56 | conn = g_variant_get_string(src->data, NULL); | |
57 | break; | |
58 | } | |
59 | } | |
60 | ||
61 | devices = NULL; | |
62 | if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { | |
63 | g_slist_free_full(usb_devices, g_free); | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | for (l = usb_devices; l; l = l->next) { | |
68 | usb = l->data; | |
69 | ||
aac29cc1 | 70 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
71 | sdi->status = SR_ST_INACTIVE; |
72 | sdi->vendor = g_strdup("Brymen"); | |
73 | sdi->model = g_strdup("BM869"); | |
f57d8ffe | 74 | devc = g_malloc0(sizeof(struct dev_context)); |
ecaa89af | 75 | sdi->priv = devc; |
5e23fcab ML |
76 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
77 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
ecaa89af AJ |
78 | |
79 | sdi->inst_type = SR_INST_USB; | |
80 | sdi->conn = usb; | |
81 | ||
ab939ebb LPC |
82 | sr_sw_limits_init(&devc->sw_limits); |
83 | ||
ecaa89af AJ |
84 | devices = g_slist_append(devices, sdi); |
85 | } | |
8d9c8554 | 86 | |
15a5bfe4 | 87 | return std_scan_complete(di, devices); |
8d9c8554 AJ |
88 | } |
89 | ||
8d9c8554 AJ |
90 | static int dev_open(struct sr_dev_inst *sdi) |
91 | { | |
4f840ce9 | 92 | struct sr_dev_driver *di = sdi->driver; |
41812aca | 93 | struct drv_context *drvc = di->context; |
ecaa89af AJ |
94 | struct sr_usb_dev_inst *usb; |
95 | struct dev_context *devc; | |
96 | int ret; | |
8d9c8554 | 97 | |
ecaa89af AJ |
98 | usb = sdi->conn; |
99 | devc = sdi->priv; | |
100 | ||
7e463623 | 101 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) < 0) |
6d930b41 | 102 | return SR_ERR; |
ecaa89af | 103 | |
ecaa89af AJ |
104 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { |
105 | ret = libusb_detach_kernel_driver(usb->devhdl, 0); | |
106 | if (ret < 0) { | |
107 | sr_err("Failed to detach kernel driver: %s.", | |
108 | libusb_error_name(ret)); | |
109 | return SR_ERR; | |
110 | } | |
111 | devc->detached_kernel_driver = 1; | |
ecaa89af | 112 | } |
8d9c8554 | 113 | |
ecaa89af AJ |
114 | if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) { |
115 | sr_err("Failed to claim interface 0: %s.", | |
116 | libusb_error_name(ret)); | |
117 | return SR_ERR; | |
118 | } | |
8d9c8554 | 119 | |
7e463623 | 120 | return SR_OK; |
8d9c8554 AJ |
121 | } |
122 | ||
123 | static int dev_close(struct sr_dev_inst *sdi) | |
124 | { | |
ecaa89af AJ |
125 | struct sr_usb_dev_inst *usb; |
126 | struct dev_context *devc; | |
127 | int ret; | |
128 | ||
129 | usb = sdi->conn; | |
130 | devc = sdi->priv; | |
131 | ||
132 | if ((ret = libusb_release_interface(usb->devhdl, 0))) | |
133 | sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret)); | |
ecaa89af AJ |
134 | |
135 | if (!ret && devc->detached_kernel_driver) { | |
f1ba6b4b | 136 | if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) |
ecaa89af AJ |
137 | sr_err("Failed to attach kernel driver: %s.\n", |
138 | libusb_error_name(ret)); | |
f1ba6b4b | 139 | else |
ecaa89af | 140 | devc->detached_kernel_driver = 0; |
ecaa89af | 141 | } |
8d9c8554 | 142 | |
ecaa89af | 143 | libusb_close(usb->devhdl); |
8d9c8554 | 144 | |
f1ba6b4b | 145 | return (ret == 0) ? SR_OK : SR_ERR; |
8d9c8554 AJ |
146 | } |
147 | ||
dd7a72ea UH |
148 | static int config_get(uint32_t key, GVariant **data, |
149 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
8d9c8554 | 150 | { |
ecaa89af | 151 | struct dev_context *devc = sdi->priv; |
8d9c8554 | 152 | |
53b4680f | 153 | (void)cg; |
8d9c8554 | 154 | |
ab939ebb | 155 | return sr_sw_limits_config_get(&devc->sw_limits, key, data); |
8d9c8554 AJ |
156 | } |
157 | ||
dd7a72ea UH |
158 | static int config_set(uint32_t key, GVariant *data, |
159 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
8d9c8554 | 160 | { |
ecaa89af | 161 | struct dev_context *devc; |
8d9c8554 | 162 | |
53b4680f | 163 | (void)cg; |
8d9c8554 | 164 | |
b0baddef | 165 | devc = sdi->priv; |
ecaa89af | 166 | |
ab939ebb | 167 | return sr_sw_limits_config_set(&devc->sw_limits, key, data); |
8d9c8554 AJ |
168 | } |
169 | ||
dd7a72ea UH |
170 | static int config_list(uint32_t key, GVariant **data, |
171 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
8d9c8554 | 172 | { |
05199c0a | 173 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
8d9c8554 AJ |
174 | } |
175 | ||
695dc859 | 176 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
8d9c8554 | 177 | { |
ecaa89af | 178 | struct dev_context *devc; |
8d9c8554 | 179 | |
ecaa89af | 180 | devc = sdi->priv; |
ab939ebb LPC |
181 | |
182 | sr_sw_limits_acquisition_start(&devc->sw_limits); | |
ecaa89af | 183 | |
bee2b016 | 184 | std_session_send_df_header(sdi); |
ecaa89af | 185 | |
c650d3ec | 186 | sr_session_source_add(sdi->session, -1, 0, 10, |
102f1239 | 187 | brymen_bm86x_receive_data, (void *)sdi); |
8d9c8554 AJ |
188 | |
189 | return SR_OK; | |
190 | } | |
191 | ||
695dc859 | 192 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
8d9c8554 | 193 | { |
bee2b016 | 194 | std_session_send_df_end(sdi); |
ecaa89af | 195 | |
dd7a4a71 | 196 | sr_session_source_remove(sdi->session, -1); |
8d9c8554 AJ |
197 | |
198 | return SR_OK; | |
199 | } | |
200 | ||
dd5c48a6 | 201 | static struct sr_dev_driver brymen_bm86x_driver_info = { |
8d9c8554 AJ |
202 | .name = "brymen-bm86x", |
203 | .longname = "Brymen BM86X", | |
204 | .api_version = 1, | |
c2fdcc25 | 205 | .init = std_init, |
700d6b64 | 206 | .cleanup = std_cleanup, |
8d9c8554 | 207 | .scan = scan, |
c01bf34c | 208 | .dev_list = std_dev_list, |
f778bf02 | 209 | .dev_clear = std_dev_clear, |
8d9c8554 AJ |
210 | .config_get = config_get, |
211 | .config_set = config_set, | |
212 | .config_list = config_list, | |
213 | .dev_open = dev_open, | |
214 | .dev_close = dev_close, | |
215 | .dev_acquisition_start = dev_acquisition_start, | |
216 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 217 | .context = NULL, |
8d9c8554 | 218 | }; |
dd5c48a6 | 219 | SR_REGISTER_DEV_DRIVER(brymen_bm86x_driver_info); |