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