]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | #define BRYMEN_BC86X "0820.0001" | |
24 | ||
25 | static const uint32_t scanopts[] = { | |
26 | SR_CONF_CONN, | |
27 | }; | |
28 | ||
29 | static const uint32_t devopts[] = { | |
30 | SR_CONF_MULTIMETER, | |
31 | SR_CONF_CONTINUOUS, | |
32 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
33 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
34 | }; | |
35 | ||
36 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
37 | { | |
38 | GSList *usb_devices, *devices, *l; | |
39 | struct drv_context *drvc; | |
40 | struct dev_context *devc; | |
41 | struct sr_dev_inst *sdi; | |
42 | struct sr_usb_dev_inst *usb; | |
43 | struct sr_config *src; | |
44 | const char *conn; | |
45 | ||
46 | drvc = di->context; | |
47 | ||
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 | ||
67 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
68 | sdi->status = SR_ST_INACTIVE; | |
69 | sdi->vendor = g_strdup("Brymen"); | |
70 | sdi->model = g_strdup("BM869"); | |
71 | devc = g_malloc0(sizeof(struct dev_context)); | |
72 | sdi->priv = devc; | |
73 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
74 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
75 | ||
76 | sdi->inst_type = SR_INST_USB; | |
77 | sdi->conn = usb; | |
78 | ||
79 | sr_sw_limits_init(&devc->sw_limits); | |
80 | ||
81 | devices = g_slist_append(devices, sdi); | |
82 | } | |
83 | ||
84 | return std_scan_complete(di, devices); | |
85 | } | |
86 | ||
87 | static int dev_open(struct sr_dev_inst *sdi) | |
88 | { | |
89 | struct sr_dev_driver *di = sdi->driver; | |
90 | struct drv_context *drvc = di->context; | |
91 | struct sr_usb_dev_inst *usb; | |
92 | struct dev_context *devc; | |
93 | int ret; | |
94 | ||
95 | usb = sdi->conn; | |
96 | devc = sdi->priv; | |
97 | ||
98 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) | |
99 | sdi->status = SR_ST_ACTIVE; | |
100 | else | |
101 | return SR_ERR; | |
102 | ||
103 | /* Detach kernel drivers which grabbed this device (if any). */ | |
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; | |
112 | sr_dbg("Successfully detached kernel driver."); | |
113 | } else { | |
114 | sr_dbg("No need to detach a kernel driver."); | |
115 | } | |
116 | ||
117 | /* Claim interface 0. */ | |
118 | if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) { | |
119 | sr_err("Failed to claim interface 0: %s.", | |
120 | libusb_error_name(ret)); | |
121 | return SR_ERR; | |
122 | } | |
123 | sr_dbg("Successfully claimed interface 0."); | |
124 | ||
125 | return ret; | |
126 | } | |
127 | ||
128 | static int dev_close(struct sr_dev_inst *sdi) | |
129 | { | |
130 | struct sr_usb_dev_inst *usb; | |
131 | struct dev_context *devc; | |
132 | int ret; | |
133 | ||
134 | if (sdi->status != SR_ST_ACTIVE) | |
135 | return SR_ERR_DEV_CLOSED; | |
136 | ||
137 | usb = sdi->conn; | |
138 | devc = sdi->priv; | |
139 | ||
140 | if ((ret = libusb_release_interface(usb->devhdl, 0))) | |
141 | sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret)); | |
142 | else | |
143 | sr_dbg("Successfully released interface 0.\n"); | |
144 | ||
145 | if (!ret && devc->detached_kernel_driver) { | |
146 | if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) { | |
147 | sr_err("Failed to attach kernel driver: %s.\n", | |
148 | libusb_error_name(ret)); | |
149 | } else { | |
150 | devc->detached_kernel_driver = 0; | |
151 | sr_dbg("Successfully attached kernel driver.\n"); | |
152 | } | |
153 | } | |
154 | ||
155 | libusb_close(usb->devhdl); | |
156 | ||
157 | sdi->status = SR_ST_INACTIVE; | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
162 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
163 | const struct sr_channel_group *cg) | |
164 | { | |
165 | struct dev_context *devc = sdi->priv; | |
166 | ||
167 | (void)cg; | |
168 | ||
169 | return sr_sw_limits_config_get(&devc->sw_limits, key, data); | |
170 | } | |
171 | ||
172 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
173 | const struct sr_channel_group *cg) | |
174 | { | |
175 | struct dev_context *devc; | |
176 | ||
177 | (void)cg; | |
178 | ||
179 | if (sdi->status != SR_ST_ACTIVE) | |
180 | return SR_ERR_DEV_CLOSED; | |
181 | ||
182 | devc = sdi->priv; | |
183 | ||
184 | return sr_sw_limits_config_set(&devc->sw_limits, key, data); | |
185 | } | |
186 | ||
187 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
188 | const struct sr_channel_group *cg) | |
189 | { | |
190 | (void)sdi; | |
191 | (void)cg; | |
192 | ||
193 | switch (key) { | |
194 | case SR_CONF_SCAN_OPTIONS: | |
195 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
196 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
197 | break; | |
198 | case SR_CONF_DEVICE_OPTIONS: | |
199 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
200 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
201 | break; | |
202 | default: | |
203 | return SR_ERR_NA; | |
204 | } | |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
209 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
210 | { | |
211 | struct dev_context *devc; | |
212 | ||
213 | if (sdi->status != SR_ST_ACTIVE) | |
214 | return SR_ERR_DEV_CLOSED; | |
215 | ||
216 | devc = sdi->priv; | |
217 | ||
218 | sr_sw_limits_acquisition_start(&devc->sw_limits); | |
219 | ||
220 | std_session_send_df_header(sdi); | |
221 | ||
222 | sr_session_source_add(sdi->session, -1, 0, 10, | |
223 | brymen_bm86x_receive_data, (void *)sdi); | |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
228 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
229 | { | |
230 | if (sdi->status != SR_ST_ACTIVE) | |
231 | return SR_ERR_DEV_CLOSED; | |
232 | ||
233 | std_session_send_df_end(sdi); | |
234 | ||
235 | sr_session_source_remove(sdi->session, -1); | |
236 | ||
237 | return SR_OK; | |
238 | } | |
239 | ||
240 | static struct sr_dev_driver brymen_bm86x_driver_info = { | |
241 | .name = "brymen-bm86x", | |
242 | .longname = "Brymen BM86X", | |
243 | .api_version = 1, | |
244 | .init = std_init, | |
245 | .cleanup = std_cleanup, | |
246 | .scan = scan, | |
247 | .dev_list = std_dev_list, | |
248 | .dev_clear = NULL, | |
249 | .config_get = config_get, | |
250 | .config_set = config_set, | |
251 | .config_list = config_list, | |
252 | .dev_open = dev_open, | |
253 | .dev_close = dev_close, | |
254 | .dev_acquisition_start = dev_acquisition_start, | |
255 | .dev_acquisition_stop = dev_acquisition_stop, | |
256 | .context = NULL, | |
257 | }; | |
258 | SR_REGISTER_DEV_DRIVER(brymen_bm86x_driver_info); |