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