]>
Commit | Line | Data |
---|---|---|
df92e5cf | 1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
f48cef78 UH |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> |
5 | * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt> | |
6 | * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com> | |
df92e5cf | 7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include "protocol.h" | |
23 | ||
df92e5cf | 24 | static const int hwcaps[] = { |
1953564a BV |
25 | SR_CONF_LOGIC_ANALYZER, |
26 | SR_CONF_SAMPLERATE, | |
27 | SR_CONF_TRIGGER_SLOPE, | |
28 | SR_CONF_HORIZ_TRIGGERPOS, | |
29 | // SR_CONF_CAPTURE_RATIO, | |
30 | SR_CONF_LIMIT_SAMPLES, | |
31 | // SR_CONF_RLE, | |
df92e5cf | 32 | 0, |
33 | }; | |
34 | ||
35 | /* | |
36 | * Probes are numbered 0 to 7. | |
37 | * | |
38 | * See also: http://www.linkinstruments.com/images/mso19_1113.gif | |
39 | */ | |
40 | SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = { | |
41 | "0", "1", "2", "3", "4", "5", "6", "7", NULL | |
42 | }; | |
43 | ||
44 | /*supported samplerates */ | |
45 | static const struct sr_samplerates samplerates = { | |
46 | SR_HZ(100), | |
df92e5cf | 47 | SR_MHZ(200), |
5a24e89c | 48 | SR_HZ(100), |
df92e5cf | 49 | NULL, |
50 | }; | |
51 | ||
52 | SR_PRIV struct sr_dev_driver link_mso19_driver_info; | |
53 | static struct sr_dev_driver *di = &link_mso19_driver_info; | |
54 | ||
55 | static int hw_init(struct sr_context *sr_ctx) | |
56 | { | |
063e7aef | 57 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); |
df92e5cf | 58 | } |
59 | ||
60 | static GSList *hw_scan(GSList *options) | |
61 | { | |
00b44ccb UH |
62 | int i; |
63 | GSList *devices = NULL; | |
64 | const char *conn = NULL; | |
65 | const char *serialcomm = NULL; | |
66 | GSList *l; | |
1987b8d6 | 67 | struct sr_config *src; |
00b44ccb | 68 | struct udev *udev; |
df92e5cf | 69 | |
70 | (void)options; | |
df92e5cf | 71 | |
df92e5cf | 72 | for (l = options; l; l = l->next) { |
1987b8d6 BV |
73 | src = l->data; |
74 | switch (src->key) { | |
1953564a | 75 | case SR_CONF_CONN: |
1987b8d6 | 76 | conn = src->value; |
df92e5cf | 77 | break; |
1953564a | 78 | case SR_CONF_SERIALCOMM: |
1987b8d6 | 79 | serialcomm = src->value; |
df92e5cf | 80 | break; |
81 | } | |
82 | } | |
83 | if (!conn) | |
00b44ccb | 84 | conn = SERIALCONN; |
df92e5cf | 85 | if (serialcomm == NULL) |
86 | serialcomm = SERIALCOMM; | |
87 | ||
00b44ccb | 88 | udev = udev_new(); |
df92e5cf | 89 | if (!udev) { |
90 | sr_err("Failed to initialize udev."); | |
91 | } | |
00b44ccb | 92 | |
df92e5cf | 93 | struct udev_enumerate *enumerate = udev_enumerate_new(udev); |
94 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
95 | udev_enumerate_scan_devices(enumerate); | |
96 | struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate); | |
97 | struct udev_list_entry *dev_list_entry; | |
00b44ccb UH |
98 | for (dev_list_entry = devs; |
99 | dev_list_entry != NULL; | |
100 | dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { | |
df92e5cf | 101 | const char *syspath = udev_list_entry_get_name(dev_list_entry); |
00b44ccb UH |
102 | struct udev_device *dev = |
103 | udev_device_new_from_syspath(udev, syspath); | |
df92e5cf | 104 | const char *sysname = udev_device_get_sysname(dev); |
00b44ccb UH |
105 | struct udev_device *parent = |
106 | udev_device_get_parent_with_subsystem_devtype(dev, "usb", | |
107 | "usb_device"); | |
df92e5cf | 108 | |
109 | if (!parent) { | |
110 | sr_err("Unable to find parent usb device for %s", | |
111 | sysname); | |
112 | continue; | |
113 | } | |
114 | ||
00b44ccb UH |
115 | const char *idVendor = |
116 | udev_device_get_sysattr_value(parent, "idVendor"); | |
117 | const char *idProduct = | |
118 | udev_device_get_sysattr_value(parent, "idProduct"); | |
df92e5cf | 119 | if (strcmp(USB_VENDOR, idVendor) |
00b44ccb | 120 | || strcmp(USB_PRODUCT, idProduct)) |
df92e5cf | 121 | continue; |
122 | ||
00b44ccb UH |
123 | const char *iSerial = |
124 | udev_device_get_sysattr_value(parent, "serial"); | |
125 | const char *iProduct = | |
126 | udev_device_get_sysattr_value(parent, "product"); | |
df92e5cf | 127 | |
00b44ccb | 128 | char path[32]; |
df92e5cf | 129 | snprintf(path, sizeof(path), "/dev/%s", sysname); |
00b44ccb | 130 | conn = path; |
df92e5cf | 131 | |
132 | size_t s = strcspn(iProduct, " "); | |
00b44ccb UH |
133 | char product[32]; |
134 | char manufacturer[32]; | |
df92e5cf | 135 | if (s > sizeof(product) || |
00b44ccb UH |
136 | strlen(iProduct) - s > sizeof(manufacturer)) { |
137 | sr_err("Could not parse iProduct: %s.", iProduct); | |
df92e5cf | 138 | continue; |
139 | } | |
140 | strncpy(product, iProduct, s); | |
141 | product[s] = 0; | |
4db2aaff | 142 | strcpy(manufacturer, iProduct + s + 1); |
00b44ccb UH |
143 | |
144 | //Create the device context and set its params | |
145 | struct dev_context *devc; | |
146 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
147 | sr_err("Device context malloc failed."); | |
148 | return devices; | |
149 | } | |
150 | ||
151 | if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) { | |
152 | sr_err("Invalid iSerial: %s.", iSerial); | |
153 | g_free(devc); | |
154 | return devices; | |
155 | } | |
156 | ||
157 | char hwrev[32]; | |
158 | sprintf(hwrev, "r%d", devc->hwrev); | |
159 | devc->ctlbase1 = 0; | |
160 | devc->protocol_trigger.spimode = 0; | |
161 | for (i = 0; i < 4; i++) { | |
162 | devc->protocol_trigger.word[i] = 0; | |
163 | devc->protocol_trigger.mask[i] = 0xff; | |
164 | } | |
165 | ||
166 | if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) { | |
167 | g_free(devc); | |
168 | return devices; | |
169 | } | |
170 | ||
171 | struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, | |
172 | manufacturer, product, hwrev); | |
173 | ||
174 | if (!sdi) { | |
175 | sr_err("Unable to create device instance for %s", | |
176 | sysname); | |
177 | sr_dev_inst_free(sdi); | |
178 | g_free(devc); | |
179 | return devices; | |
180 | } | |
181 | ||
182 | sdi->driver = di; | |
183 | sdi->priv = devc; | |
184 | ||
185 | for (i = 0; i < NUM_PROBES; i++) { | |
186 | struct sr_probe *probe; | |
187 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
188 | mso19_probe_names[i]))) | |
189 | return 0; | |
190 | sdi->probes = g_slist_append(sdi->probes, probe); | |
191 | } | |
192 | ||
193 | //Add the driver | |
194 | struct drv_context *drvc = di->priv; | |
195 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
196 | devices = g_slist_append(devices, sdi); | |
197 | } | |
5a24e89c | 198 | |
df92e5cf | 199 | return devices; |
200 | } | |
201 | ||
202 | static GSList *hw_dev_list(void) | |
203 | { | |
df92e5cf | 204 | struct drv_context *drvc; |
205 | ||
206 | drvc = di->priv; | |
207 | ||
208 | return drvc->instances; | |
209 | } | |
210 | ||
211 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
212 | { | |
00b44ccb | 213 | int ret; |
df92e5cf | 214 | struct dev_context *devc; |
215 | ||
216 | devc = sdi->priv; | |
217 | ||
218 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
219 | return SR_ERR; | |
220 | ||
221 | sdi->status = SR_ST_ACTIVE; | |
222 | ||
223 | /* FIXME: discard serial buffer */ | |
224 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
225 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
226 | ||
00b44ccb | 227 | ret = mso_reset_adc(sdi); |
df92e5cf | 228 | if (ret != SR_OK) |
229 | return ret; | |
230 | ||
231 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
232 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
233 | ||
00b44ccb UH |
234 | // ret = mso_reset_fsm(sdi); |
235 | // if (ret != SR_OK) | |
236 | // return ret; | |
237 | // return SR_ERR; | |
df92e5cf | 238 | |
239 | return SR_OK; | |
240 | } | |
241 | ||
242 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
243 | { | |
df92e5cf | 244 | struct dev_context *devc; |
245 | ||
246 | devc = sdi->priv; | |
247 | ||
248 | if (devc->serial && devc->serial->fd != -1) { | |
249 | serial_close(devc->serial); | |
250 | sdi->status = SR_ST_INACTIVE; | |
251 | } | |
252 | ||
253 | return SR_OK; | |
254 | } | |
255 | ||
256 | static int hw_cleanup(void) | |
257 | { | |
df92e5cf | 258 | GSList *l; |
259 | struct sr_dev_inst *sdi; | |
260 | struct drv_context *drvc; | |
261 | struct dev_context *devc; | |
262 | int ret = SR_OK; | |
263 | ||
264 | if (!(drvc = di->priv)) | |
265 | return SR_OK; | |
266 | ||
267 | /* Properly close and free all devices. */ | |
268 | for (l = drvc->instances; l; l = l->next) { | |
269 | if (!(sdi = l->data)) { | |
270 | /* Log error, but continue cleaning up the rest. */ | |
271 | sr_err("%s: sdi was NULL, continuing", __func__); | |
272 | ret = SR_ERR_BUG; | |
273 | continue; | |
274 | } | |
275 | if (!(devc = sdi->priv)) { | |
276 | /* Log error, but continue cleaning up the rest. */ | |
277 | sr_err("%s: sdi->priv was NULL, continuing", __func__); | |
278 | ret = SR_ERR_BUG; | |
279 | continue; | |
280 | } | |
281 | hw_dev_close(sdi); | |
282 | sr_serial_dev_inst_free(devc->serial); | |
283 | sr_dev_inst_free(sdi); | |
284 | } | |
285 | g_slist_free(drvc->instances); | |
286 | drvc->instances = NULL; | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
035a1078 | 291 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) |
df92e5cf | 292 | { |
293 | struct dev_context *devc; | |
294 | ||
035a1078 | 295 | switch (id) { |
123e1313 | 296 | case SR_CONF_SAMPLERATE: |
df92e5cf | 297 | if (sdi) { |
298 | devc = sdi->priv; | |
299 | *data = &devc->cur_rate; | |
300 | } else | |
301 | return SR_ERR; | |
302 | break; | |
303 | default: | |
304 | return SR_ERR_ARG; | |
305 | } | |
306 | ||
307 | return SR_OK; | |
308 | } | |
309 | ||
035a1078 | 310 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
df92e5cf | 311 | { |
df92e5cf | 312 | int ret; |
087a9161 | 313 | struct dev_context *devc; |
00b44ccb UH |
314 | uint64_t num_samples, slope; |
315 | int trigger_pos; | |
316 | float pos; | |
317 | ||
318 | devc = sdi->priv; | |
087a9161 | 319 | |
df92e5cf | 320 | if (sdi->status != SR_ST_ACTIVE) |
321 | return SR_ERR; | |
322 | ||
035a1078 | 323 | switch (id) { |
1953564a | 324 | case SR_CONF_SAMPLERATE: |
00b44ccb UH |
325 | // FIXME |
326 | return mso_configure_rate(sdi, *(const uint64_t *)value); | |
df92e5cf | 327 | ret = SR_OK; |
328 | break; | |
1953564a | 329 | case SR_CONF_LIMIT_SAMPLES: |
00b44ccb UH |
330 | num_samples = *(uint64_t *)value; |
331 | if (num_samples < 1024) { | |
332 | sr_err("minimum of 1024 samples required"); | |
333 | ret = SR_ERR_ARG; | |
334 | } else { | |
335 | devc->limit_samples = num_samples; | |
336 | sr_dbg("setting limit_samples to %i\n", | |
337 | num_samples); | |
338 | ret = SR_OK; | |
339 | } | |
df92e5cf | 340 | break; |
1953564a | 341 | case SR_CONF_CAPTURE_RATIO: |
00b44ccb UH |
342 | ret = SR_OK; |
343 | break; | |
1953564a | 344 | case SR_CONF_TRIGGER_SLOPE: |
00b44ccb UH |
345 | slope = *(uint64_t *)value; |
346 | if (slope != SLOPE_NEGATIVE && slope != SLOPE_POSITIVE) { | |
347 | sr_err("Invalid trigger slope"); | |
348 | ret = SR_ERR_ARG; | |
349 | } else { | |
350 | devc->trigger_slope = slope; | |
351 | ret = SR_OK; | |
352 | } | |
353 | break; | |
1953564a | 354 | case SR_CONF_HORIZ_TRIGGERPOS: |
00b44ccb UH |
355 | pos = *(float *)value; |
356 | if (pos < 0 || pos > 255) { | |
357 | sr_err("Trigger position (%f) should be between 0 and 255.", pos); | |
358 | ret = SR_ERR_ARG; | |
359 | } else { | |
360 | trigger_pos = (int)pos; | |
361 | devc->trigger_holdoff[0] = trigger_pos & 0xff; | |
362 | ret = SR_OK; | |
363 | } | |
df92e5cf | 364 | break; |
1953564a | 365 | case SR_CONF_RLE: |
00b44ccb | 366 | ret = SR_OK; |
df92e5cf | 367 | break; |
368 | default: | |
369 | ret = SR_ERR; | |
00b44ccb | 370 | break; |
df92e5cf | 371 | } |
372 | ||
373 | return ret; | |
374 | } | |
375 | ||
a1c743fc BV |
376 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
377 | { | |
378 | ||
379 | (void)sdi; | |
380 | ||
381 | switch (key) { | |
9a6517d1 BV |
382 | case SR_CONF_DEVICE_OPTIONS: |
383 | *data = hwcaps; | |
384 | break; | |
a1c743fc BV |
385 | case SR_CONF_SAMPLERATE: |
386 | *data = &samplerates; | |
387 | break; | |
c50277a6 BV |
388 | case SR_CONF_TRIGGER_TYPE: |
389 | *data = (char *)TRIGGER_TYPE; | |
390 | break; | |
a1c743fc BV |
391 | default: |
392 | return SR_ERR_ARG; | |
393 | } | |
394 | ||
395 | return SR_OK; | |
396 | } | |
397 | ||
df92e5cf | 398 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
00b44ccb | 399 | void *cb_data) |
df92e5cf | 400 | { |
401 | struct sr_datafeed_packet *packet; | |
402 | struct sr_datafeed_header *header; | |
df92e5cf | 403 | struct dev_context *devc; |
df92e5cf | 404 | int ret = SR_ERR; |
00b44ccb | 405 | |
df92e5cf | 406 | devc = sdi->priv; |
407 | ||
408 | if (sdi->status != SR_ST_ACTIVE) | |
409 | return SR_ERR; | |
410 | ||
5a24e89c | 411 | if (mso_configure_probes(sdi) != SR_OK) { |
412 | sr_err("Failed to configure probes."); | |
413 | return SR_ERR; | |
414 | } | |
df92e5cf | 415 | |
df92e5cf | 416 | /* FIXME: No need to do full reconfigure every time */ |
00b44ccb UH |
417 | // ret = mso_reset_fsm(sdi); |
418 | // if (ret != SR_OK) | |
419 | // return ret; | |
df92e5cf | 420 | |
421 | /* FIXME: ACDC Mode */ | |
422 | devc->ctlbase1 &= 0x7f; | |
00b44ccb | 423 | // devc->ctlbase1 |= devc->acdcmode; |
df92e5cf | 424 | |
425 | ret = mso_configure_rate(sdi, devc->cur_rate); | |
426 | if (ret != SR_OK) | |
427 | return ret; | |
428 | ||
429 | /* set dac offset */ | |
430 | ret = mso_dac_out(sdi, devc->dac_offset); | |
431 | if (ret != SR_OK) | |
432 | return ret; | |
433 | ||
434 | ret = mso_configure_threshold_level(sdi); | |
435 | if (ret != SR_OK) | |
436 | return ret; | |
437 | ||
438 | ret = mso_configure_trigger(sdi); | |
439 | if (ret != SR_OK) | |
440 | return ret; | |
441 | ||
df92e5cf | 442 | /* END of config hardware part */ |
00b44ccb UH |
443 | ret = mso_arm(sdi); |
444 | if (ret != SR_OK) | |
445 | return ret; | |
df92e5cf | 446 | |
447 | /* Start acquisition on the device. */ | |
4db2aaff | 448 | mso_check_trigger(devc->serial, &devc->trigger_state); |
449 | ret = mso_check_trigger(devc->serial, NULL); | |
df92e5cf | 450 | if (ret != SR_OK) |
451 | return ret; | |
452 | ||
00b44ccb | 453 | sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data); |
df92e5cf | 454 | |
455 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
456 | sr_err("Datafeed packet malloc failed."); | |
457 | return SR_ERR_MALLOC; | |
458 | } | |
459 | ||
460 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
461 | sr_err("Datafeed header malloc failed."); | |
462 | g_free(packet); | |
463 | return SR_ERR_MALLOC; | |
464 | } | |
465 | ||
466 | packet->type = SR_DF_HEADER; | |
467 | packet->payload = (unsigned char *)header; | |
468 | header->feed_version = 1; | |
469 | gettimeofday(&header->starttime, NULL); | |
470 | sr_session_send(cb_data, packet); | |
471 | ||
df92e5cf | 472 | g_free(header); |
473 | g_free(packet); | |
474 | ||
475 | return SR_OK; | |
476 | } | |
477 | ||
087a9161 | 478 | /* This stops acquisition on ALL devices, ignoring dev_index. */ |
df92e5cf | 479 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
480 | { | |
df92e5cf | 481 | (void)cb_data; |
482 | ||
483 | stop_acquisition(sdi); | |
484 | ||
485 | return SR_OK; | |
486 | } | |
487 | ||
488 | SR_PRIV struct sr_dev_driver link_mso19_driver_info = { | |
489 | .name = "link-mso19", | |
490 | .longname = "Link Instruments MSO-19", | |
491 | .api_version = 1, | |
492 | .init = hw_init, | |
493 | .cleanup = hw_cleanup, | |
494 | .scan = hw_scan, | |
495 | .dev_list = hw_dev_list, | |
496 | .dev_clear = hw_cleanup, | |
035a1078 BV |
497 | .config_get = config_get, |
498 | .config_set = config_set, | |
a1c743fc | 499 | .config_list = config_list, |
df92e5cf | 500 | .dev_open = hw_dev_open, |
501 | .dev_close = hw_dev_close, | |
df92e5cf | 502 | .dev_acquisition_start = hw_dev_acquisition_start, |
503 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
504 | .priv = NULL, | |
505 | }; |