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