]>
Commit | Line | Data |
---|---|---|
16e76bae MS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de> | |
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> |
16e76bae MS |
21 | #include "protocol.h" |
22 | ||
05199c0a | 23 | static const uint32_t drvopts[] = { |
e52e712d | 24 | SR_CONF_LOGIC_ANALYZER, |
05199c0a UH |
25 | }; |
26 | ||
27 | static const uint32_t devopts[] = { | |
5827f61b BV |
28 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST, |
29 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
30 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
31 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
e52e712d MS |
32 | }; |
33 | ||
02d5c0d8 BV |
34 | static const int32_t trigger_matches[] = { |
35 | SR_TRIGGER_RISING, | |
36 | SR_TRIGGER_FALLING, | |
37 | SR_TRIGGER_EDGE, | |
38 | }; | |
39 | ||
79914b3a | 40 | SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = { |
e52e712d MS |
41 | SR_KHZ(1.25), |
42 | SR_KHZ(10), | |
43 | SR_KHZ(50), | |
44 | SR_KHZ(100), | |
45 | SR_KHZ(250), | |
46 | SR_KHZ(500), | |
47 | SR_MHZ(1), | |
48 | SR_MHZ(2.5), | |
49 | SR_MHZ(5), | |
50 | SR_MHZ(10), | |
c824eb63 | 51 | SR_MHZ(20), |
e52e712d MS |
52 | }; |
53 | ||
53cda65a | 54 | static const char *channel_names[] = { |
e52e712d | 55 | "0", "1", "2", "3", |
e52e712d MS |
56 | }; |
57 | ||
4f840ce9 | 58 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
16e76bae | 59 | { |
e52e712d | 60 | GSList *usb_devices, *devices, *l; |
16e76bae | 61 | struct drv_context *drvc; |
e52e712d | 62 | struct sr_dev_inst *sdi; |
e52e712d MS |
63 | struct dev_context *devc; |
64 | struct sr_usb_dev_inst *usb; | |
65 | struct device_info dev_info; | |
dcd438ee UH |
66 | unsigned int i; |
67 | int ret; | |
16e76bae MS |
68 | |
69 | (void)options; | |
70 | ||
71 | devices = NULL; | |
41812aca | 72 | drvc = di->context; |
e52e712d MS |
73 | |
74 | usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID); | |
75 | ||
98fec29e | 76 | if (!usb_devices) |
e52e712d MS |
77 | return NULL; |
78 | ||
c824eb63 | 79 | for (l = usb_devices; l; l = l->next) { |
e52e712d MS |
80 | usb = l->data; |
81 | ||
e32862eb | 82 | if ((ret = sl2_get_device_info(di, *usb, &dev_info)) < 0) { |
79914b3a | 83 | sr_warn("Failed to get device information: %d.", ret); |
e52e712d MS |
84 | sr_usb_dev_inst_free(usb); |
85 | continue; | |
86 | } | |
87 | ||
f57d8ffe | 88 | devc = g_malloc0(sizeof(struct dev_context)); |
e52e712d MS |
89 | |
90 | if (!(devc->xfer_in = libusb_alloc_transfer(0))) { | |
91 | sr_err("Transfer malloc failed."); | |
92 | sr_usb_dev_inst_free(usb); | |
93 | g_free(devc); | |
94 | continue; | |
95 | } | |
96 | ||
97 | if (!(devc->xfer_out = libusb_alloc_transfer(0))) { | |
98 | sr_err("Transfer malloc failed."); | |
99 | sr_usb_dev_inst_free(usb); | |
100 | libusb_free_transfer(devc->xfer_in); | |
101 | g_free(devc); | |
102 | continue; | |
103 | } | |
104 | ||
aac29cc1 | 105 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
106 | sdi->status = SR_ST_INACTIVE; |
107 | sdi->vendor = g_strdup(VENDOR_NAME); | |
108 | sdi->model = g_strdup(MODEL_NAME); | |
109 | sdi->version = g_strdup_printf("%u.%u", dev_info.fw_ver_major, dev_info.fw_ver_minor); | |
110 | sdi->serial_num = g_strdup_printf("%d", dev_info.serial); | |
e52e712d | 111 | sdi->priv = devc; |
e52e712d MS |
112 | sdi->inst_type = SR_INST_USB; |
113 | sdi->conn = usb; | |
114 | ||
0f34cb47 UH |
115 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) |
116 | devc->channels[i] = sr_channel_new(sdi, i, | |
117 | SR_CHANNEL_LOGIC, TRUE, channel_names[i]); | |
e52e712d MS |
118 | |
119 | devc->state = STATE_IDLE; | |
120 | devc->next_state = STATE_IDLE; | |
121 | ||
122 | /* Set default samplerate. */ | |
79914b3a | 123 | sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE); |
e52e712d MS |
124 | |
125 | /* Set default capture ratio. */ | |
126 | devc->capture_ratio = 0; | |
127 | ||
128 | /* Set default after trigger delay. */ | |
129 | devc->after_trigger_delay = 0; | |
130 | ||
131 | memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE + | |
132 | PACKET_LENGTH); | |
133 | memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE + | |
134 | PACKET_LENGTH); | |
135 | ||
136 | libusb_fill_control_setup(devc->xfer_buf_in, | |
fb8d593c | 137 | USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT, |
e52e712d MS |
138 | USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, |
139 | PACKET_LENGTH); | |
140 | libusb_fill_control_setup(devc->xfer_buf_out, | |
141 | USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT, | |
142 | USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
143 | PACKET_LENGTH); | |
144 | ||
145 | devc->xfer_data_in = devc->xfer_buf_in + | |
146 | LIBUSB_CONTROL_SETUP_SIZE; | |
147 | devc->xfer_data_out = devc->xfer_buf_out + | |
148 | LIBUSB_CONTROL_SETUP_SIZE; | |
149 | ||
e52e712d | 150 | devices = g_slist_append(devices, sdi); |
e52e712d | 151 | } |
16e76bae | 152 | |
e52e712d | 153 | g_slist_free(usb_devices); |
16e76bae | 154 | |
15a5bfe4 | 155 | return std_scan_complete(di, devices); |
16e76bae MS |
156 | } |
157 | ||
3553451f | 158 | static void clear_helper(struct dev_context *devc) |
e52e712d | 159 | { |
e52e712d MS |
160 | libusb_free_transfer(devc->xfer_in); |
161 | libusb_free_transfer(devc->xfer_out); | |
e52e712d MS |
162 | } |
163 | ||
4f840ce9 | 164 | static int dev_clear(const struct sr_dev_driver *di) |
16e76bae | 165 | { |
3553451f | 166 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); |
16e76bae MS |
167 | } |
168 | ||
169 | static int dev_open(struct sr_dev_inst *sdi) | |
170 | { | |
4f840ce9 | 171 | struct sr_dev_driver *di = sdi->driver; |
efa98402 | 172 | struct drv_context *drvc = di->context; |
e52e712d MS |
173 | struct dev_context *devc; |
174 | struct sr_usb_dev_inst *usb; | |
175 | uint8_t buffer[PACKET_LENGTH]; | |
176 | int ret; | |
177 | ||
e52e712d MS |
178 | usb = sdi->conn; |
179 | devc = sdi->priv; | |
16e76bae | 180 | |
e52e712d MS |
181 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) |
182 | return SR_ERR; | |
183 | ||
184 | /* | |
185 | * Determine if a kernel driver is active on this interface and, if so, | |
186 | * detach it. | |
187 | */ | |
188 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { | |
189 | ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE); | |
e52e712d | 190 | if (ret < 0) { |
9526f2d4 | 191 | sr_err("Failed to detach kernel driver: %s.", |
e52e712d MS |
192 | libusb_error_name(ret)); |
193 | return SR_ERR; | |
194 | } | |
195 | } | |
196 | ||
79914b3a | 197 | if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) { |
e52e712d MS |
198 | sr_err("Failed to claim interface: %s.", |
199 | libusb_error_name(ret)); | |
200 | return SR_ERR; | |
201 | } | |
202 | ||
203 | libusb_fill_control_transfer(devc->xfer_in, usb->devhdl, | |
79914b3a | 204 | devc->xfer_buf_in, sl2_receive_transfer_in, |
1a46cc62 | 205 | sdi, USB_TIMEOUT_MS); |
e52e712d MS |
206 | |
207 | libusb_fill_control_transfer(devc->xfer_out, usb->devhdl, | |
79914b3a | 208 | devc->xfer_buf_out, sl2_receive_transfer_out, |
1a46cc62 | 209 | sdi, USB_TIMEOUT_MS); |
e52e712d MS |
210 | |
211 | memset(buffer, 0, sizeof(buffer)); | |
212 | ||
213 | buffer[0] = CMD_RESET; | |
79914b3a | 214 | if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { |
e52e712d MS |
215 | sr_err("Device reset failed: %s.", libusb_error_name(ret)); |
216 | return SR_ERR; | |
217 | } | |
218 | ||
219 | /* | |
220 | * Set the device to idle state. If the device is not in idle state it | |
c824eb63 UH |
221 | * possibly will reset itself after a few seconds without being used |
222 | * and thereby close the connection. | |
e52e712d MS |
223 | */ |
224 | buffer[0] = CMD_IDLE; | |
79914b3a | 225 | if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { |
e52e712d MS |
226 | sr_err("Failed to set device in idle state: %s.", |
227 | libusb_error_name(ret)); | |
228 | return SR_ERR; | |
229 | } | |
16e76bae | 230 | |
16e76bae MS |
231 | return SR_OK; |
232 | } | |
233 | ||
234 | static int dev_close(struct sr_dev_inst *sdi) | |
235 | { | |
e52e712d MS |
236 | struct sr_usb_dev_inst *usb; |
237 | ||
e52e712d MS |
238 | usb = sdi->conn; |
239 | ||
240 | if (!usb->devhdl) | |
f1ba6b4b | 241 | return SR_ERR_BUG; |
16e76bae | 242 | |
e52e712d MS |
243 | libusb_release_interface(usb->devhdl, USB_INTERFACE); |
244 | libusb_close(usb->devhdl); | |
16e76bae | 245 | |
e52e712d | 246 | usb->devhdl = NULL; |
16e76bae MS |
247 | |
248 | return SR_OK; | |
249 | } | |
250 | ||
584560f1 | 251 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 252 | const struct sr_channel_group *cg) |
16e76bae | 253 | { |
e52e712d | 254 | struct dev_context *devc; |
16e76bae MS |
255 | int ret; |
256 | ||
53b4680f | 257 | (void)cg; |
d3c74a6f | 258 | |
16e76bae | 259 | ret = SR_OK; |
e52e712d MS |
260 | devc = sdi->priv; |
261 | ||
16e76bae | 262 | switch (key) { |
e52e712d MS |
263 | case SR_CONF_SAMPLERATE: |
264 | *data = g_variant_new_uint64(devc->samplerate); | |
265 | break; | |
266 | case SR_CONF_CAPTURE_RATIO: | |
267 | *data = g_variant_new_uint64(devc->capture_ratio); | |
268 | break; | |
16e76bae MS |
269 | default: |
270 | return SR_ERR_NA; | |
271 | } | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
584560f1 | 276 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 277 | const struct sr_channel_group *cg) |
16e76bae | 278 | { |
e52e712d | 279 | uint64_t samplerate, limit_samples, capture_ratio; |
16e76bae MS |
280 | int ret; |
281 | ||
53b4680f | 282 | (void)cg; |
d3c74a6f | 283 | |
16e76bae | 284 | switch (key) { |
e52e712d MS |
285 | case SR_CONF_LIMIT_SAMPLES: |
286 | limit_samples = g_variant_get_uint64(data); | |
79914b3a | 287 | ret = sl2_set_limit_samples(sdi, limit_samples); |
e52e712d MS |
288 | break; |
289 | case SR_CONF_SAMPLERATE: | |
290 | samplerate = g_variant_get_uint64(data); | |
79914b3a | 291 | ret = sl2_set_samplerate(sdi, samplerate); |
e52e712d MS |
292 | break; |
293 | case SR_CONF_CAPTURE_RATIO: | |
294 | capture_ratio = g_variant_get_uint64(data); | |
79914b3a | 295 | ret = sl2_set_capture_ratio(sdi, capture_ratio); |
e52e712d | 296 | break; |
16e76bae | 297 | default: |
e52e712d | 298 | return SR_ERR_NA; |
16e76bae MS |
299 | } |
300 | ||
301 | return ret; | |
302 | } | |
303 | ||
584560f1 | 304 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 305 | const struct sr_channel_group *cg) |
16e76bae | 306 | { |
f0de2dd0 | 307 | GVariant *gvar, *grange[2]; |
e52e712d | 308 | GVariantBuilder gvb; |
16e76bae | 309 | |
16e76bae | 310 | switch (key) { |
e52e712d | 311 | case SR_CONF_DEVICE_OPTIONS: |
05199c0a | 312 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); |
e52e712d MS |
313 | case SR_CONF_SAMPLERATE: |
314 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
315 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
79914b3a | 316 | sl2_samplerates, ARRAY_SIZE(sl2_samplerates), |
e52e712d MS |
317 | sizeof(uint64_t)); |
318 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
319 | *data = g_variant_builder_end(&gvb); | |
320 | break; | |
02d5c0d8 BV |
321 | case SR_CONF_TRIGGER_MATCH: |
322 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
323 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
324 | sizeof(int32_t)); | |
e52e712d | 325 | break; |
f0de2dd0 BV |
326 | case SR_CONF_LIMIT_SAMPLES: |
327 | grange[0] = g_variant_new_uint64(0); | |
328 | grange[1] = g_variant_new_uint64(MAX_SAMPLES); | |
329 | *data = g_variant_new_tuple(grange, 2); | |
330 | break; | |
16e76bae MS |
331 | default: |
332 | return SR_ERR_NA; | |
333 | } | |
334 | ||
a9010323 | 335 | return SR_OK; |
16e76bae MS |
336 | } |
337 | ||
695dc859 | 338 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
16e76bae | 339 | { |
4f840ce9 | 340 | struct sr_dev_driver *di = sdi->driver; |
e52e712d MS |
341 | struct drv_context *drvc; |
342 | struct dev_context *devc; | |
343 | uint16_t trigger_bytes, tmp; | |
344 | unsigned int i, j; | |
345 | int ret; | |
16e76bae | 346 | |
e52e712d | 347 | devc = sdi->priv; |
41812aca | 348 | drvc = di->context; |
e52e712d | 349 | |
e52e712d MS |
350 | devc->wait_data_ready_locked = TRUE; |
351 | devc->stopping_in_progress = FALSE; | |
352 | devc->transfer_error = FALSE; | |
353 | devc->samples_processed = 0; | |
354 | devc->channel = 0; | |
355 | devc->sample_packet = 0; | |
356 | ||
357 | /* | |
358 | * The trigger must be configured first because the calculation of the | |
359 | * pre and post trigger samples depends on a configured trigger. | |
360 | */ | |
02d5c0d8 | 361 | sl2_convert_trigger(sdi); |
79914b3a | 362 | sl2_calculate_trigger_samples(sdi); |
e52e712d MS |
363 | |
364 | trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes; | |
365 | ||
366 | /* Calculate the number of expected sample packets. */ | |
367 | devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES; | |
368 | ||
369 | /* Round up the number of expected sample packets. */ | |
370 | if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0) | |
371 | devc->num_sample_packets++; | |
372 | ||
ba7dd8bb | 373 | devc->num_enabled_channels = 0; |
e52e712d MS |
374 | |
375 | /* | |
ba7dd8bb | 376 | * Count the number of enabled channels and number them for a sequential |
e52e712d MS |
377 | * access. |
378 | */ | |
3f239f08 | 379 | for (i = 0, j = 0; i < NUM_CHANNELS; i++) { |
ba7dd8bb UH |
380 | if (devc->channels[i]->enabled) { |
381 | devc->num_enabled_channels++; | |
382 | devc->channel_map[j] = i; | |
e52e712d MS |
383 | j++; |
384 | } | |
385 | } | |
386 | ||
ba7dd8bb | 387 | sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels); |
e52e712d MS |
388 | |
389 | /* Set up the transfer buffer for the acquisition. */ | |
390 | devc->xfer_data_out[0] = CMD_SAMPLE; | |
391 | devc->xfer_data_out[1] = 0x00; | |
392 | ||
393 | tmp = GUINT16_TO_LE(devc->pre_trigger_bytes); | |
394 | memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp)); | |
395 | ||
396 | tmp = GUINT16_TO_LE(devc->post_trigger_bytes); | |
397 | memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp)); | |
398 | ||
399 | devc->xfer_data_out[6] = devc->samplerate_id; | |
400 | devc->xfer_data_out[7] = devc->trigger_type; | |
401 | devc->xfer_data_out[8] = devc->trigger_channel; | |
402 | devc->xfer_data_out[9] = 0x00; | |
403 | ||
404 | tmp = GUINT16_TO_LE(devc->after_trigger_delay); | |
405 | memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp)); | |
406 | ||
e52e712d | 407 | if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) { |
c824eb63 | 408 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); |
e52e712d MS |
409 | return SR_ERR; |
410 | } | |
411 | ||
102f1239 BV |
412 | usb_source_add(sdi->session, drvc->sr_ctx, 100, |
413 | ikalogic_scanalogic2_receive_data, (void *)sdi); | |
e52e712d | 414 | |
bee2b016 | 415 | std_session_send_df_header(sdi); |
e52e712d MS |
416 | |
417 | devc->next_state = STATE_SAMPLE; | |
16e76bae MS |
418 | |
419 | return SR_OK; | |
420 | } | |
421 | ||
695dc859 | 422 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
16e76bae | 423 | { |
e52e712d | 424 | sdi->status = SR_ST_STOPPING; |
16e76bae MS |
425 | |
426 | return SR_OK; | |
427 | } | |
428 | ||
dd5c48a6 | 429 | static struct sr_dev_driver ikalogic_scanalogic2_driver_info = { |
16e76bae | 430 | .name = "ikalogic-scanalogic2", |
e52e712d | 431 | .longname = "IKALOGIC Scanalogic-2", |
16e76bae | 432 | .api_version = 1, |
c2fdcc25 | 433 | .init = std_init, |
700d6b64 | 434 | .cleanup = std_cleanup, |
16e76bae | 435 | .scan = scan, |
c01bf34c | 436 | .dev_list = std_dev_list, |
16e76bae MS |
437 | .dev_clear = dev_clear, |
438 | .config_get = config_get, | |
439 | .config_set = config_set, | |
440 | .config_list = config_list, | |
441 | .dev_open = dev_open, | |
442 | .dev_close = dev_close, | |
443 | .dev_acquisition_start = dev_acquisition_start, | |
444 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 445 | .context = NULL, |
16e76bae | 446 | }; |
dd5c48a6 | 447 | SR_REGISTER_DEV_DRIVER(ikalogic_scanalogic2_driver_info); |