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