]>
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 | ||
20 | #include "protocol.h" | |
21 | ||
e52e712d MS |
22 | static const int hwcaps[] = { |
23 | SR_CONF_LOGIC_ANALYZER, | |
24 | SR_CONF_SAMPLERATE, | |
25 | SR_CONF_LIMIT_SAMPLES, | |
26 | SR_CONF_TRIGGER_TYPE, | |
c824eb63 | 27 | SR_CONF_CAPTURE_RATIO, |
e52e712d MS |
28 | }; |
29 | ||
79914b3a | 30 | SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = { |
e52e712d MS |
31 | SR_KHZ(1.25), |
32 | SR_KHZ(10), | |
33 | SR_KHZ(50), | |
34 | SR_KHZ(100), | |
35 | SR_KHZ(250), | |
36 | SR_KHZ(500), | |
37 | SR_MHZ(1), | |
38 | SR_MHZ(2.5), | |
39 | SR_MHZ(5), | |
40 | SR_MHZ(10), | |
c824eb63 | 41 | SR_MHZ(20), |
e52e712d MS |
42 | }; |
43 | ||
44 | static const char *probe_names[NUM_PROBES + 1] = { | |
45 | "0", "1", "2", "3", | |
c824eb63 | 46 | NULL, |
e52e712d MS |
47 | }; |
48 | ||
16e76bae MS |
49 | SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info; |
50 | static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info; | |
51 | ||
16e76bae MS |
52 | static int init(struct sr_context *sr_ctx) |
53 | { | |
e52e712d | 54 | return std_init(sr_ctx, di, LOG_PREFIX); |
16e76bae MS |
55 | } |
56 | ||
57 | static GSList *scan(GSList *options) | |
58 | { | |
e52e712d | 59 | GSList *usb_devices, *devices, *l; |
16e76bae | 60 | struct drv_context *drvc; |
e52e712d MS |
61 | struct sr_dev_inst *sdi; |
62 | struct sr_probe *probe; | |
63 | struct dev_context *devc; | |
64 | struct sr_usb_dev_inst *usb; | |
65 | struct device_info dev_info; | |
66 | int ret, device_index, i; | |
67 | char *fw_ver_str; | |
16e76bae MS |
68 | |
69 | (void)options; | |
70 | ||
71 | devices = NULL; | |
72 | drvc = di->priv; | |
73 | drvc->instances = NULL; | |
e52e712d MS |
74 | device_index = 0; |
75 | ||
76 | usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID); | |
77 | ||
78 | if (usb_devices == NULL) | |
79 | return NULL; | |
80 | ||
c824eb63 | 81 | for (l = usb_devices; l; l = l->next) { |
e52e712d MS |
82 | usb = l->data; |
83 | ||
79914b3a UH |
84 | if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) { |
85 | sr_warn("Failed to get device information: %d.", ret); | |
e52e712d MS |
86 | sr_usb_dev_inst_free(usb); |
87 | continue; | |
88 | } | |
89 | ||
c824eb63 | 90 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { |
e52e712d MS |
91 | sr_err("Device instance malloc failed."); |
92 | sr_usb_dev_inst_free(usb); | |
93 | continue; | |
94 | } | |
95 | ||
96 | if (!(devc->xfer_in = libusb_alloc_transfer(0))) { | |
97 | sr_err("Transfer malloc failed."); | |
98 | sr_usb_dev_inst_free(usb); | |
99 | g_free(devc); | |
100 | continue; | |
101 | } | |
102 | ||
103 | if (!(devc->xfer_out = libusb_alloc_transfer(0))) { | |
104 | sr_err("Transfer malloc failed."); | |
105 | sr_usb_dev_inst_free(usb); | |
106 | libusb_free_transfer(devc->xfer_in); | |
107 | g_free(devc); | |
108 | continue; | |
109 | } | |
110 | ||
111 | fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major, | |
112 | dev_info.fw_ver_minor); | |
e52e712d MS |
113 | if (!fw_ver_str) { |
114 | sr_err("Firmware string malloc failed."); | |
115 | sr_usb_dev_inst_free(usb); | |
116 | libusb_free_transfer(devc->xfer_in); | |
117 | libusb_free_transfer(devc->xfer_out); | |
118 | g_free(devc); | |
119 | continue; | |
120 | } | |
121 | ||
122 | sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE, VENDOR_NAME, | |
123 | MODEL_NAME, fw_ver_str); | |
e52e712d | 124 | g_free(fw_ver_str); |
e52e712d MS |
125 | if (!sdi) { |
126 | sr_err("sr_dev_inst_new failed."); | |
127 | sr_usb_dev_inst_free(usb); | |
128 | libusb_free_transfer(devc->xfer_in); | |
129 | libusb_free_transfer(devc->xfer_out); | |
130 | g_free(devc); | |
131 | continue; | |
132 | } | |
133 | ||
134 | sdi->priv = devc; | |
135 | sdi->driver = di; | |
136 | sdi->inst_type = SR_INST_USB; | |
137 | sdi->conn = usb; | |
138 | ||
139 | for (i = 0; probe_names[i]; i++) { | |
140 | probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
141 | probe_names[i]); | |
142 | sdi->probes = g_slist_append(sdi->probes, probe); | |
143 | devc->probes[i] = probe; | |
144 | } | |
145 | ||
146 | devc->state = STATE_IDLE; | |
147 | devc->next_state = STATE_IDLE; | |
148 | ||
149 | /* Set default samplerate. */ | |
79914b3a | 150 | sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE); |
e52e712d MS |
151 | |
152 | /* Set default capture ratio. */ | |
153 | devc->capture_ratio = 0; | |
154 | ||
155 | /* Set default after trigger delay. */ | |
156 | devc->after_trigger_delay = 0; | |
157 | ||
158 | memset(devc->xfer_buf_in, 0, LIBUSB_CONTROL_SETUP_SIZE + | |
159 | PACKET_LENGTH); | |
160 | memset(devc->xfer_buf_out, 0, LIBUSB_CONTROL_SETUP_SIZE + | |
161 | PACKET_LENGTH); | |
162 | ||
163 | libusb_fill_control_setup(devc->xfer_buf_in, | |
fb8d593c | 164 | USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT, |
e52e712d MS |
165 | USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, |
166 | PACKET_LENGTH); | |
167 | libusb_fill_control_setup(devc->xfer_buf_out, | |
168 | USB_REQUEST_TYPE_OUT, USB_HID_SET_REPORT, | |
169 | USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
170 | PACKET_LENGTH); | |
171 | ||
172 | devc->xfer_data_in = devc->xfer_buf_in + | |
173 | LIBUSB_CONTROL_SETUP_SIZE; | |
174 | devc->xfer_data_out = devc->xfer_buf_out + | |
175 | LIBUSB_CONTROL_SETUP_SIZE; | |
176 | ||
177 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
178 | devices = g_slist_append(devices, sdi); | |
179 | ||
180 | device_index++; | |
181 | } | |
16e76bae | 182 | |
e52e712d | 183 | g_slist_free(usb_devices); |
16e76bae MS |
184 | |
185 | return devices; | |
186 | } | |
187 | ||
188 | static GSList *dev_list(void) | |
189 | { | |
c824eb63 | 190 | return ((struct drv_context *)(di->priv))->instances; |
16e76bae MS |
191 | } |
192 | ||
e52e712d MS |
193 | static void clear_dev_context(void *priv) |
194 | { | |
c824eb63 UH |
195 | struct dev_context *devc; |
196 | ||
197 | devc = priv; | |
e52e712d | 198 | |
c824eb63 | 199 | sr_dbg("Device context cleared."); |
e52e712d MS |
200 | |
201 | libusb_free_transfer(devc->xfer_in); | |
202 | libusb_free_transfer(devc->xfer_out); | |
203 | g_free(devc); | |
204 | } | |
205 | ||
16e76bae MS |
206 | static int dev_clear(void) |
207 | { | |
e52e712d | 208 | return std_dev_clear(di, &clear_dev_context); |
16e76bae MS |
209 | } |
210 | ||
211 | static int dev_open(struct sr_dev_inst *sdi) | |
212 | { | |
e52e712d MS |
213 | struct drv_context *drvc; |
214 | struct dev_context *devc; | |
215 | struct sr_usb_dev_inst *usb; | |
216 | uint8_t buffer[PACKET_LENGTH]; | |
217 | int ret; | |
218 | ||
219 | if (!(drvc = di->priv)) { | |
220 | sr_err("Driver was not initialized."); | |
221 | return SR_ERR; | |
222 | } | |
223 | ||
224 | usb = sdi->conn; | |
225 | devc = sdi->priv; | |
16e76bae | 226 | |
e52e712d MS |
227 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) |
228 | return SR_ERR; | |
229 | ||
230 | /* | |
231 | * Determine if a kernel driver is active on this interface and, if so, | |
232 | * detach it. | |
233 | */ | |
234 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { | |
235 | ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE); | |
e52e712d | 236 | if (ret < 0) { |
9526f2d4 | 237 | sr_err("Failed to detach kernel driver: %s.", |
e52e712d MS |
238 | libusb_error_name(ret)); |
239 | return SR_ERR; | |
240 | } | |
241 | } | |
242 | ||
79914b3a | 243 | if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) { |
e52e712d MS |
244 | sr_err("Failed to claim interface: %s.", |
245 | libusb_error_name(ret)); | |
246 | return SR_ERR; | |
247 | } | |
248 | ||
249 | libusb_fill_control_transfer(devc->xfer_in, usb->devhdl, | |
79914b3a | 250 | devc->xfer_buf_in, sl2_receive_transfer_in, |
e52e712d MS |
251 | sdi, USB_TIMEOUT); |
252 | ||
253 | libusb_fill_control_transfer(devc->xfer_out, usb->devhdl, | |
79914b3a | 254 | devc->xfer_buf_out, sl2_receive_transfer_out, |
e52e712d MS |
255 | sdi, USB_TIMEOUT); |
256 | ||
257 | memset(buffer, 0, sizeof(buffer)); | |
258 | ||
259 | buffer[0] = CMD_RESET; | |
79914b3a | 260 | if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { |
e52e712d MS |
261 | sr_err("Device reset failed: %s.", libusb_error_name(ret)); |
262 | return SR_ERR; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Set the device to idle state. If the device is not in idle state it | |
c824eb63 UH |
267 | * possibly will reset itself after a few seconds without being used |
268 | * and thereby close the connection. | |
e52e712d MS |
269 | */ |
270 | buffer[0] = CMD_IDLE; | |
79914b3a | 271 | if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { |
e52e712d MS |
272 | sr_err("Failed to set device in idle state: %s.", |
273 | libusb_error_name(ret)); | |
274 | return SR_ERR; | |
275 | } | |
16e76bae MS |
276 | |
277 | sdi->status = SR_ST_ACTIVE; | |
278 | ||
279 | return SR_OK; | |
280 | } | |
281 | ||
282 | static int dev_close(struct sr_dev_inst *sdi) | |
283 | { | |
e52e712d MS |
284 | struct sr_usb_dev_inst *usb; |
285 | ||
286 | if (!di->priv) { | |
287 | sr_err("Driver was not initialized."); | |
288 | return SR_ERR; | |
289 | } | |
290 | ||
291 | usb = sdi->conn; | |
292 | ||
293 | if (!usb->devhdl) | |
294 | return SR_OK; | |
16e76bae | 295 | |
e52e712d MS |
296 | libusb_release_interface(usb->devhdl, USB_INTERFACE); |
297 | libusb_close(usb->devhdl); | |
16e76bae | 298 | |
e52e712d | 299 | usb->devhdl = NULL; |
16e76bae MS |
300 | sdi->status = SR_ST_INACTIVE; |
301 | ||
302 | return SR_OK; | |
303 | } | |
304 | ||
305 | static int cleanup(void) | |
306 | { | |
c824eb63 | 307 | return dev_clear(); |
16e76bae MS |
308 | } |
309 | ||
d3c74a6f BV |
310 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, |
311 | const struct sr_probe_group *probe_group) | |
16e76bae | 312 | { |
e52e712d | 313 | struct dev_context *devc; |
16e76bae MS |
314 | int ret; |
315 | ||
d3c74a6f BV |
316 | (void)probe_group; |
317 | ||
16e76bae | 318 | ret = SR_OK; |
e52e712d MS |
319 | devc = sdi->priv; |
320 | ||
16e76bae | 321 | switch (key) { |
e52e712d MS |
322 | case SR_CONF_SAMPLERATE: |
323 | *data = g_variant_new_uint64(devc->samplerate); | |
324 | break; | |
325 | case SR_CONF_CAPTURE_RATIO: | |
326 | *data = g_variant_new_uint64(devc->capture_ratio); | |
327 | break; | |
c2b988bd BV |
328 | case SR_CONF_MAX_UNCOMPRESSED_SAMPLES: |
329 | *data = g_variant_new_uint64(MAX_SAMPLES); | |
330 | break; | |
16e76bae MS |
331 | default: |
332 | return SR_ERR_NA; | |
333 | } | |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
d3c74a6f BV |
338 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, |
339 | const struct sr_probe_group *probe_group) | |
16e76bae | 340 | { |
e52e712d | 341 | uint64_t samplerate, limit_samples, capture_ratio; |
16e76bae MS |
342 | int ret; |
343 | ||
d3c74a6f BV |
344 | (void)probe_group; |
345 | ||
16e76bae MS |
346 | if (sdi->status != SR_ST_ACTIVE) |
347 | return SR_ERR_DEV_CLOSED; | |
348 | ||
349 | ret = SR_OK; | |
e52e712d | 350 | |
16e76bae | 351 | switch (key) { |
e52e712d MS |
352 | case SR_CONF_LIMIT_SAMPLES: |
353 | limit_samples = g_variant_get_uint64(data); | |
79914b3a | 354 | ret = sl2_set_limit_samples(sdi, limit_samples); |
e52e712d MS |
355 | break; |
356 | case SR_CONF_SAMPLERATE: | |
357 | samplerate = g_variant_get_uint64(data); | |
79914b3a | 358 | ret = sl2_set_samplerate(sdi, samplerate); |
e52e712d MS |
359 | break; |
360 | case SR_CONF_CAPTURE_RATIO: | |
361 | capture_ratio = g_variant_get_uint64(data); | |
79914b3a | 362 | ret = sl2_set_capture_ratio(sdi, capture_ratio); |
e52e712d | 363 | break; |
16e76bae | 364 | default: |
e52e712d | 365 | return SR_ERR_NA; |
16e76bae MS |
366 | } |
367 | ||
368 | return ret; | |
369 | } | |
370 | ||
d3c74a6f BV |
371 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
372 | const struct sr_probe_group *probe_group) | |
16e76bae | 373 | { |
e52e712d MS |
374 | GVariant *gvar; |
375 | GVariantBuilder gvb; | |
16e76bae MS |
376 | int ret; |
377 | ||
378 | (void)sdi; | |
d3c74a6f | 379 | (void)probe_group; |
16e76bae MS |
380 | |
381 | ret = SR_OK; | |
e52e712d | 382 | |
16e76bae | 383 | switch (key) { |
e52e712d MS |
384 | case SR_CONF_DEVICE_OPTIONS: |
385 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, hwcaps, | |
386 | ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
387 | break; | |
388 | case SR_CONF_SAMPLERATE: | |
389 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
390 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
79914b3a | 391 | sl2_samplerates, ARRAY_SIZE(sl2_samplerates), |
e52e712d MS |
392 | sizeof(uint64_t)); |
393 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
394 | *data = g_variant_builder_end(&gvb); | |
395 | break; | |
396 | case SR_CONF_TRIGGER_TYPE: | |
397 | *data = g_variant_new_string(TRIGGER_TYPES); | |
398 | break; | |
16e76bae MS |
399 | default: |
400 | return SR_ERR_NA; | |
401 | } | |
402 | ||
403 | return ret; | |
404 | } | |
405 | ||
e52e712d | 406 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
16e76bae | 407 | { |
e52e712d MS |
408 | struct drv_context *drvc; |
409 | struct dev_context *devc; | |
410 | uint16_t trigger_bytes, tmp; | |
411 | unsigned int i, j; | |
412 | int ret; | |
16e76bae MS |
413 | |
414 | if (sdi->status != SR_ST_ACTIVE) | |
415 | return SR_ERR_DEV_CLOSED; | |
416 | ||
e52e712d MS |
417 | devc = sdi->priv; |
418 | drvc = di->priv; | |
419 | ||
420 | devc->cb_data = cb_data; | |
421 | devc->wait_data_ready_locked = TRUE; | |
422 | devc->stopping_in_progress = FALSE; | |
423 | devc->transfer_error = FALSE; | |
424 | devc->samples_processed = 0; | |
425 | devc->channel = 0; | |
426 | devc->sample_packet = 0; | |
427 | ||
428 | /* | |
429 | * The trigger must be configured first because the calculation of the | |
430 | * pre and post trigger samples depends on a configured trigger. | |
431 | */ | |
79914b3a UH |
432 | sl2_configure_trigger(sdi); |
433 | sl2_calculate_trigger_samples(sdi); | |
e52e712d MS |
434 | |
435 | trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes; | |
436 | ||
437 | /* Calculate the number of expected sample packets. */ | |
438 | devc->num_sample_packets = trigger_bytes / PACKET_NUM_SAMPLE_BYTES; | |
439 | ||
440 | /* Round up the number of expected sample packets. */ | |
441 | if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0) | |
442 | devc->num_sample_packets++; | |
443 | ||
444 | devc->num_enabled_probes = 0; | |
445 | ||
446 | /* | |
447 | * Count the number of enabled probes and number them for a sequential | |
448 | * access. | |
449 | */ | |
450 | for (i = 0, j = 0; i < NUM_PROBES; i++) { | |
451 | if (devc->probes[i]->enabled) { | |
452 | devc->num_enabled_probes++; | |
453 | devc->probe_map[j] = i; | |
454 | j++; | |
455 | } | |
456 | } | |
457 | ||
458 | sr_dbg("Number of enabled probes: %i.", devc->num_enabled_probes); | |
459 | ||
460 | /* Set up the transfer buffer for the acquisition. */ | |
461 | devc->xfer_data_out[0] = CMD_SAMPLE; | |
462 | devc->xfer_data_out[1] = 0x00; | |
463 | ||
464 | tmp = GUINT16_TO_LE(devc->pre_trigger_bytes); | |
465 | memcpy(devc->xfer_data_out + 2, &tmp, sizeof(tmp)); | |
466 | ||
467 | tmp = GUINT16_TO_LE(devc->post_trigger_bytes); | |
468 | memcpy(devc->xfer_data_out + 4, &tmp, sizeof(tmp)); | |
469 | ||
470 | devc->xfer_data_out[6] = devc->samplerate_id; | |
471 | devc->xfer_data_out[7] = devc->trigger_type; | |
472 | devc->xfer_data_out[8] = devc->trigger_channel; | |
473 | devc->xfer_data_out[9] = 0x00; | |
474 | ||
475 | tmp = GUINT16_TO_LE(devc->after_trigger_delay); | |
476 | memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp)); | |
477 | ||
e52e712d | 478 | if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) { |
c824eb63 | 479 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); |
e52e712d MS |
480 | return SR_ERR; |
481 | } | |
482 | ||
6c60facc | 483 | usb_source_add(drvc->sr_ctx, 100, ikalogic_scanalogic2_receive_data, (void *)sdi); |
e52e712d MS |
484 | |
485 | sr_dbg("Acquisition started successfully."); | |
486 | ||
487 | /* Send header packet to the session bus. */ | |
488 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
489 | ||
490 | devc->next_state = STATE_SAMPLE; | |
16e76bae MS |
491 | |
492 | return SR_OK; | |
493 | } | |
494 | ||
495 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
496 | { | |
497 | (void)cb_data; | |
498 | ||
499 | if (sdi->status != SR_ST_ACTIVE) | |
500 | return SR_ERR_DEV_CLOSED; | |
501 | ||
e52e712d MS |
502 | sr_dbg("Stopping acquisition."); |
503 | ||
504 | sdi->status = SR_ST_STOPPING; | |
16e76bae MS |
505 | |
506 | return SR_OK; | |
507 | } | |
508 | ||
509 | SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = { | |
510 | .name = "ikalogic-scanalogic2", | |
e52e712d | 511 | .longname = "IKALOGIC Scanalogic-2", |
16e76bae MS |
512 | .api_version = 1, |
513 | .init = init, | |
514 | .cleanup = cleanup, | |
515 | .scan = scan, | |
516 | .dev_list = dev_list, | |
517 | .dev_clear = dev_clear, | |
518 | .config_get = config_get, | |
519 | .config_set = config_set, | |
520 | .config_list = config_list, | |
521 | .dev_open = dev_open, | |
522 | .dev_close = dev_close, | |
523 | .dev_acquisition_start = dev_acquisition_start, | |
524 | .dev_acquisition_stop = dev_acquisition_stop, | |
525 | .priv = NULL, | |
526 | }; |