]>
Commit | Line | Data |
---|---|---|
01cf8814 DR |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
a2936073 | 5 | * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt> |
01cf8814 DR |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
8a839354 UH |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
01cf8814 DR |
19 | */ |
20 | ||
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <sys/time.h> | |
27 | #include <inttypes.h> | |
28 | #include <glib.h> | |
29 | #include <libudev.h> | |
01cf8814 | 30 | #include <arpa/inet.h> |
b7f09cf8 UH |
31 | #include "sigrok.h" |
32 | #include "sigrok-internal.h" | |
01cf8814 DR |
33 | #include "link-mso19.h" |
34 | ||
35 | #define USB_VENDOR "3195" | |
36 | #define USB_PRODUCT "f190" | |
37 | ||
464d12c7 KS |
38 | #define NUM_PROBES 8 |
39 | ||
01cf8814 | 40 | static int capabilities[] = { |
5a2326a7 UH |
41 | SR_HWCAP_LOGIC_ANALYZER, |
42 | // SR_HWCAP_OSCILLOSCOPE, | |
43 | // SR_HWCAP_PAT_GENERATOR, | |
01cf8814 | 44 | |
5a2326a7 UH |
45 | SR_HWCAP_SAMPLERATE, |
46 | // SR_HWCAP_CAPTURE_RATIO, | |
47 | SR_HWCAP_LIMIT_SAMPLES, | |
01cf8814 DR |
48 | 0, |
49 | }; | |
50 | ||
464d12c7 KS |
51 | static const char *probe_names[NUM_PROBES + 1] = { |
52 | "0", | |
53 | "1", | |
54 | "2", | |
55 | "3", | |
56 | "4", | |
57 | "5", | |
58 | "6", | |
59 | "7", | |
60 | NULL, | |
61 | }; | |
62 | ||
01cf8814 | 63 | static uint64_t supported_samplerates[] = { |
c9140419 UH |
64 | SR_HZ(100), |
65 | SR_HZ(200), | |
66 | SR_HZ(500), | |
59df0c77 UH |
67 | SR_KHZ(1), |
68 | SR_KHZ(2), | |
69 | SR_KHZ(5), | |
70 | SR_KHZ(10), | |
71 | SR_KHZ(20), | |
72 | SR_KHZ(50), | |
73 | SR_KHZ(100), | |
74 | SR_KHZ(200), | |
75 | SR_KHZ(500), | |
76 | SR_MHZ(1), | |
77 | SR_MHZ(2), | |
78 | SR_MHZ(5), | |
79 | SR_MHZ(10), | |
80 | SR_MHZ(20), | |
81 | SR_MHZ(50), | |
82 | SR_MHZ(100), | |
83 | SR_MHZ(200), | |
84 | 0, | |
01cf8814 DR |
85 | }; |
86 | ||
60679b18 | 87 | static struct sr_samplerates samplerates = { |
c9140419 | 88 | SR_HZ(100), |
59df0c77 | 89 | SR_MHZ(200), |
c9140419 | 90 | SR_HZ(0), |
59df0c77 | 91 | supported_samplerates, |
01cf8814 DR |
92 | }; |
93 | ||
94 | static GSList *device_instances = NULL; | |
95 | ||
a00ba012 | 96 | static int mso_send_control_message(struct sr_device_instance *sdi, |
01cf8814 DR |
97 | uint16_t payload[], int n) |
98 | { | |
99 | int fd = sdi->serial->fd; | |
100 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
101 | char *p, *buf; | |
102 | ||
ecad043f UH |
103 | ret = SR_ERR; |
104 | ||
01cf8814 DR |
105 | if (fd < 0) |
106 | goto ret; | |
107 | ||
ecad043f UH |
108 | if (!(buf = g_try_malloc(s))) { |
109 | sr_err("mso19: %s: buf malloc failed", __func__); | |
110 | ret = SR_ERR_MALLOC; | |
01cf8814 | 111 | goto ret; |
ecad043f | 112 | } |
01cf8814 DR |
113 | |
114 | p = buf; | |
115 | memcpy(p, mso_head, sizeof(mso_head)); | |
116 | p += sizeof(mso_head); | |
117 | ||
118 | for (i = 0; i < n; i++) { | |
119 | *(uint16_t *) p = htons(payload[i]); | |
120 | p += 2; | |
121 | } | |
122 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
123 | ||
124 | w = 0; | |
125 | while (w < s) { | |
2119ab03 | 126 | ret = serial_write(fd, buf + w, s - w); |
01cf8814 | 127 | if (ret < 0) { |
e46b8fb1 | 128 | ret = SR_ERR; |
01cf8814 DR |
129 | goto free; |
130 | } | |
131 | w += ret; | |
132 | } | |
e46b8fb1 | 133 | ret = SR_OK; |
01cf8814 | 134 | free: |
ecad043f | 135 | g_free(buf); |
01cf8814 DR |
136 | ret: |
137 | return ret; | |
138 | } | |
139 | ||
a00ba012 | 140 | static int mso_reset_adc(struct sr_device_instance *sdi) |
01cf8814 DR |
141 | { |
142 | struct mso *mso = sdi->priv; | |
143 | uint16_t ops[2]; | |
144 | ||
a8467191 RC |
145 | ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC)); |
146 | ops[1] = mso_trans(REG_CTL1, mso->ctlbase1); | |
147 | mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; | |
01cf8814 | 148 | |
7b48d6e1 | 149 | sr_dbg("mso19: Requesting ADC reset"); |
01cf8814 DR |
150 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
151 | } | |
152 | ||
a00ba012 | 153 | static int mso_reset_fsm(struct sr_device_instance *sdi) |
01cf8814 DR |
154 | { |
155 | struct mso *mso = sdi->priv; | |
156 | uint16_t ops[1]; | |
157 | ||
a8467191 RC |
158 | mso->ctlbase1 |= BIT_CTL1_RESETFSM; |
159 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
01cf8814 | 160 | |
7b48d6e1 | 161 | sr_dbg("mso19: Requesting ADC reset"); |
01cf8814 DR |
162 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
163 | } | |
164 | ||
a00ba012 | 165 | static int mso_toggle_led(struct sr_device_instance *sdi, int state) |
01cf8814 DR |
166 | { |
167 | struct mso *mso = sdi->priv; | |
168 | uint16_t ops[1]; | |
169 | ||
f020a997 | 170 | mso->ctlbase1 &= ~BIT_CTL1_LED; |
01cf8814 | 171 | if (state) |
a8467191 RC |
172 | mso->ctlbase1 |= BIT_CTL1_LED; |
173 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
01cf8814 | 174 | |
7b48d6e1 | 175 | sr_dbg("mso19: Requesting LED toggle"); |
01cf8814 DR |
176 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
177 | } | |
178 | ||
a00ba012 | 179 | static int mso_check_trigger(struct sr_device_instance *sdi, |
01cf8814 DR |
180 | uint8_t *info) |
181 | { | |
182 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
183 | char buf[1]; | |
184 | int ret; | |
185 | ||
7b48d6e1 | 186 | sr_dbg("mso19: Requesting trigger state"); |
01cf8814 | 187 | ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
e46b8fb1 | 188 | if (info == NULL || ret != SR_OK) |
01cf8814 DR |
189 | return ret; |
190 | ||
191 | buf[0] = 0; | |
2119ab03 | 192 | if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */ |
e46b8fb1 | 193 | ret = SR_ERR; |
01cf8814 DR |
194 | *info = buf[0]; |
195 | ||
7b48d6e1 | 196 | sr_dbg("mso19: Trigger state is: 0x%x", *info); |
01cf8814 DR |
197 | return ret; |
198 | } | |
199 | ||
a00ba012 | 200 | static int mso_read_buffer(struct sr_device_instance *sdi) |
01cf8814 DR |
201 | { |
202 | uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; | |
203 | ||
7b48d6e1 | 204 | sr_dbg("mso19: Requesting buffer dump"); |
01cf8814 DR |
205 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
206 | } | |
207 | ||
a00ba012 | 208 | static int mso_arm(struct sr_device_instance *sdi) |
01cf8814 DR |
209 | { |
210 | struct mso *mso = sdi->priv; | |
211 | uint16_t ops[] = { | |
a8467191 RC |
212 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM), |
213 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM), | |
214 | mso_trans(REG_CTL1, mso->ctlbase1), | |
01cf8814 DR |
215 | }; |
216 | ||
7b48d6e1 | 217 | sr_dbg("mso19: Requesting trigger arm"); |
01cf8814 DR |
218 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
219 | } | |
220 | ||
a00ba012 | 221 | static int mso_force_capture(struct sr_device_instance *sdi) |
01cf8814 DR |
222 | { |
223 | struct mso *mso = sdi->priv; | |
224 | uint16_t ops[] = { | |
a8467191 RC |
225 | mso_trans(REG_CTL1, mso->ctlbase1 | 8), |
226 | mso_trans(REG_CTL1, mso->ctlbase1), | |
01cf8814 DR |
227 | }; |
228 | ||
7b48d6e1 | 229 | sr_dbg("mso19: Requesting forced capture"); |
01cf8814 DR |
230 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
231 | } | |
232 | ||
a00ba012 | 233 | static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) |
01cf8814 DR |
234 | { |
235 | struct mso *mso = sdi->priv; | |
236 | uint16_t ops[] = { | |
237 | mso_trans(REG_DAC1, (val >> 8) & 0xff), | |
238 | mso_trans(REG_DAC2, val & 0xff), | |
a8467191 | 239 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC), |
01cf8814 DR |
240 | }; |
241 | ||
7b48d6e1 | 242 | sr_dbg("mso19: Setting dac word to 0x%x", val); |
01cf8814 DR |
243 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
244 | } | |
245 | ||
a00ba012 | 246 | static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) |
01cf8814 DR |
247 | { |
248 | uint16_t ops[] = { | |
249 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
250 | mso_trans(REG_CLKRATE2, val & 0xff), | |
251 | }; | |
252 | ||
7b48d6e1 | 253 | sr_dbg("mso19: Setting clkrate word to 0x%x", val); |
01cf8814 DR |
254 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); |
255 | } | |
256 | ||
a00ba012 | 257 | static int mso_configure_rate(struct sr_device_instance *sdi, |
01cf8814 DR |
258 | uint32_t rate) |
259 | { | |
260 | struct mso *mso = sdi->priv; | |
261 | unsigned int i; | |
e46b8fb1 | 262 | int ret = SR_ERR; |
01cf8814 DR |
263 | |
264 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
265 | if (rate_map[i].rate == rate) { | |
a8467191 | 266 | mso->ctlbase2 = rate_map[i].slowmode; |
01cf8814 | 267 | ret = mso_clkrate_out(sdi, rate_map[i].val); |
e46b8fb1 | 268 | if (ret == SR_OK) |
01cf8814 DR |
269 | mso->cur_rate = rate; |
270 | return ret; | |
271 | } | |
272 | } | |
273 | return ret; | |
274 | } | |
275 | ||
01cf8814 DR |
276 | static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) |
277 | { | |
278 | return (uint16_t) (0x200 - | |
279 | ((mso->dso_trigger_voltage / mso->dso_probe_attn) / | |
280 | mso->vbit)); | |
281 | } | |
282 | ||
a00ba012 | 283 | static int mso_configure_trigger(struct sr_device_instance *sdi) |
01cf8814 DR |
284 | { |
285 | struct mso *mso = sdi->priv; | |
286 | uint16_t ops[16]; | |
287 | uint16_t dso_trigger = mso_calc_raw_from_mv(mso); | |
288 | ||
289 | dso_trigger &= 0x3ff; | |
290 | if ((!mso->trigger_slope && mso->trigger_chan == 1) || | |
291 | (mso->trigger_slope && | |
292 | (mso->trigger_chan == 0 || | |
293 | mso->trigger_chan == 2 || | |
294 | mso->trigger_chan == 3))) | |
295 | dso_trigger |= 0x400; | |
296 | ||
297 | switch (mso->trigger_chan) { | |
298 | case 1: | |
299 | dso_trigger |= 0xe000; | |
300 | case 2: | |
301 | dso_trigger |= 0x4000; | |
302 | break; | |
303 | case 3: | |
304 | dso_trigger |= 0x2000; | |
305 | break; | |
306 | case 4: | |
307 | dso_trigger |= 0xa000; | |
308 | break; | |
309 | case 5: | |
310 | dso_trigger |= 0x8000; | |
311 | break; | |
312 | default: | |
313 | case 0: | |
314 | break; | |
315 | } | |
316 | ||
317 | switch (mso->trigger_outsrc) { | |
318 | case 1: | |
319 | dso_trigger |= 0x800; | |
320 | break; | |
321 | case 2: | |
322 | dso_trigger |= 0x1000; | |
323 | break; | |
324 | case 3: | |
325 | dso_trigger |= 0x1800; | |
326 | break; | |
327 | ||
328 | } | |
329 | ||
330 | ops[0] = mso_trans(5, mso->la_trigger); | |
331 | ops[1] = mso_trans(6, mso->la_trigger_mask); | |
332 | ops[2] = mso_trans(3, dso_trigger & 0xff); | |
333 | ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); | |
334 | ops[4] = mso_trans(11, | |
59df0c77 | 335 | mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate)); |
01cf8814 | 336 | |
a2936073 RC |
337 | /* Select the SPI/I2C trigger config bank */ |
338 | ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2))); | |
339 | /* Configure the SPI/I2C protocol trigger */ | |
340 | ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]); | |
341 | ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]); | |
342 | ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]); | |
343 | ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]); | |
344 | ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]); | |
345 | ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]); | |
346 | ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]); | |
347 | ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]); | |
348 | ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode); | |
349 | /* Select the default config bank */ | |
a8467191 | 350 | ops[15] = mso_trans(REG_CTL2, mso->ctlbase2); |
01cf8814 DR |
351 | |
352 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
353 | } | |
354 | ||
a00ba012 | 355 | static int mso_configure_threshold_level(struct sr_device_instance *sdi) |
01cf8814 DR |
356 | { |
357 | struct mso *mso = sdi->priv; | |
358 | ||
359 | return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]); | |
360 | } | |
361 | ||
362 | static int mso_parse_serial(const char *iSerial, const char *iProduct, | |
363 | struct mso *mso) | |
364 | { | |
365 | unsigned int u1, u2, u3, u4, u5, u6; | |
366 | ||
367 | iProduct = iProduct; | |
368 | /* FIXME: This code is in the original app, but I think its | |
369 | * used only for the GUI */ | |
370 | /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) | |
371 | mso->num_sample_rates = 0x16; | |
372 | else | |
373 | mso->num_sample_rates = 0x10; */ | |
374 | ||
375 | /* parse iSerial */ | |
376 | if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", | |
377 | &u1, &u2, &u3, &u4, &u5, &u6) != 6) | |
e46b8fb1 | 378 | return SR_ERR; |
01cf8814 DR |
379 | mso->hwmodel = u4; |
380 | mso->hwrev = u5; | |
381 | mso->serial = u6; | |
382 | mso->vbit = u1 / 10000; | |
383 | if (mso->vbit == 0) | |
384 | mso->vbit = 4.19195; | |
385 | mso->dac_offset = u2; | |
386 | if (mso->dac_offset == 0) | |
387 | mso->dac_offset = 0x1ff; | |
388 | mso->offset_range = u3; | |
389 | if (mso->offset_range == 0) | |
390 | mso->offset_range = 0x17d; | |
391 | ||
392 | /* | |
393 | * FIXME: There is more code on the original software to handle | |
394 | * bigger iSerial strings, but as I can't test on my device | |
395 | * I will not implement it yet | |
396 | */ | |
397 | ||
e46b8fb1 | 398 | return SR_OK; |
01cf8814 DR |
399 | } |
400 | ||
54ac5277 | 401 | static int hw_init(const char *deviceinfo) |
01cf8814 | 402 | { |
a00ba012 | 403 | struct sr_device_instance *sdi; |
01cf8814 DR |
404 | int devcnt = 0; |
405 | struct udev *udev; | |
406 | struct udev_enumerate *enumerate; | |
407 | struct udev_list_entry *devices, *dev_list_entry; | |
408 | struct mso *mso; | |
409 | ||
410 | deviceinfo = deviceinfo; | |
411 | ||
412 | /* It's easier to map usb<->serial using udev */ | |
413 | /* | |
414 | * FIXME: On windows we can get the same information from the | |
415 | * registry, add an #ifdef here later | |
416 | */ | |
417 | udev = udev_new(); | |
418 | if (!udev) { | |
7b48d6e1 | 419 | sr_err("mso19: Failed to initialize udev."); |
01cf8814 DR |
420 | goto ret; |
421 | } | |
422 | enumerate = udev_enumerate_new(udev); | |
423 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
424 | udev_enumerate_scan_devices(enumerate); | |
425 | devices = udev_enumerate_get_list_entry(enumerate); | |
426 | udev_list_entry_foreach(dev_list_entry, devices) { | |
427 | const char *syspath, *sysname, *idVendor, *idProduct, | |
428 | *iSerial, *iProduct; | |
429 | char path[32], manufacturer[32], product[32], hwrev[32]; | |
430 | struct udev_device *dev, *parent; | |
431 | size_t s; | |
432 | ||
433 | syspath = udev_list_entry_get_name(dev_list_entry); | |
434 | dev = udev_device_new_from_syspath(udev, syspath); | |
435 | sysname = udev_device_get_sysname(dev); | |
436 | parent = udev_device_get_parent_with_subsystem_devtype( | |
437 | dev, "usb", "usb_device"); | |
438 | if (!parent) { | |
7b48d6e1 | 439 | sr_err("mso19: Unable to find parent usb device for %s", |
133a37bf | 440 | sysname); |
01cf8814 DR |
441 | continue; |
442 | } | |
443 | ||
444 | idVendor = udev_device_get_sysattr_value(parent, "idVendor"); | |
445 | idProduct = udev_device_get_sysattr_value(parent, "idProduct"); | |
446 | if (strcmp(USB_VENDOR, idVendor) | |
447 | || strcmp(USB_PRODUCT, idProduct)) | |
448 | continue; | |
449 | ||
450 | iSerial = udev_device_get_sysattr_value(parent, "serial"); | |
451 | iProduct = udev_device_get_sysattr_value(parent, "product"); | |
452 | ||
453 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
454 | ||
455 | s = strcspn(iProduct, " "); | |
456 | if (s > sizeof(product) || | |
457 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
7b48d6e1 | 458 | sr_err("mso19: Could not parse iProduct: %s", iProduct); |
01cf8814 DR |
459 | continue; |
460 | } | |
461 | strncpy(product, iProduct, s); | |
462 | product[s] = 0; | |
463 | strcpy(manufacturer, iProduct + s); | |
01cf8814 | 464 | |
ecad043f UH |
465 | if (!(mso = g_try_malloc0(sizeof(struct mso)))) { |
466 | sr_err("mso19: %s: mso malloc failed", __func__); | |
467 | continue; /* TODO: Errors handled correctly? */ | |
468 | } | |
01cf8814 | 469 | |
e46b8fb1 | 470 | if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { |
7b48d6e1 | 471 | sr_err("mso19: Invalid iSerial: %s", iSerial); |
01cf8814 DR |
472 | goto err_free_mso; |
473 | } | |
d88b9393 | 474 | sprintf(hwrev, "r%d", mso->hwrev); |
a2936073 | 475 | |
01cf8814 | 476 | /* hardware initial state */ |
a8467191 | 477 | mso->ctlbase1 = 0; |
a2936073 RC |
478 | { |
479 | /* Initialize the protocol trigger configuration */ | |
480 | int i; | |
7b48d6e1 | 481 | for (i = 0; i < 4; i++) { |
a2936073 RC |
482 | mso->protocol_trigger.word[i] = 0; |
483 | mso->protocol_trigger.mask[i] = 0xff; | |
484 | } | |
485 | mso->protocol_trigger.spimode = 0; | |
486 | } | |
01cf8814 | 487 | |
d3683c42 | 488 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, |
7b48d6e1 | 489 | manufacturer, product, hwrev); |
01cf8814 | 490 | if (!sdi) { |
7b48d6e1 | 491 | sr_err("mso19: Unable to create device instance for %s", |
133a37bf | 492 | sysname); |
01cf8814 DR |
493 | goto err_free_mso; |
494 | } | |
495 | ||
496 | /* save a pointer to our private instance data */ | |
497 | sdi->priv = mso; | |
498 | ||
d3683c42 | 499 | sdi->serial = sr_serial_dev_inst_new(path, -1); |
01cf8814 DR |
500 | if (!sdi->serial) |
501 | goto err_device_instance_free; | |
502 | ||
503 | device_instances = g_slist_append(device_instances, sdi); | |
504 | devcnt++; | |
505 | continue; | |
506 | ||
507 | err_device_instance_free: | |
d3683c42 | 508 | sr_dev_inst_free(sdi); |
01cf8814 | 509 | err_free_mso: |
133a37bf | 510 | g_free(mso); |
01cf8814 DR |
511 | } |
512 | ||
513 | udev_enumerate_unref(enumerate); | |
514 | udev_unref(udev); | |
515 | ||
516 | ret: | |
517 | return devcnt; | |
518 | } | |
519 | ||
57ab7d9f | 520 | static int hw_cleanup(void) |
01cf8814 DR |
521 | { |
522 | GSList *l; | |
a00ba012 | 523 | struct sr_device_instance *sdi; |
341ce415 | 524 | int ret; |
01cf8814 | 525 | |
341ce415 | 526 | ret = SR_OK; |
01cf8814 DR |
527 | /* Properly close all devices. */ |
528 | for (l = device_instances; l; l = l->next) { | |
57ab7d9f UH |
529 | if (!(sdi = l->data)) { |
530 | /* Log error, but continue cleaning up the rest. */ | |
531 | sr_err("mso19: %s: sdi was NULL, continuing", __func__); | |
532 | ret = SR_ERR_BUG; | |
533 | continue; | |
534 | } | |
01cf8814 DR |
535 | if (sdi->serial->fd != -1) |
536 | serial_close(sdi->serial->fd); | |
d3683c42 | 537 | sr_dev_inst_free(sdi); |
01cf8814 DR |
538 | } |
539 | g_slist_free(device_instances); | |
540 | device_instances = NULL; | |
57ab7d9f | 541 | |
341ce415 | 542 | return ret; |
01cf8814 DR |
543 | } |
544 | ||
545 | static int hw_opendev(int device_index) | |
546 | { | |
a00ba012 | 547 | struct sr_device_instance *sdi; |
01cf8814 | 548 | struct mso *mso; |
e46b8fb1 | 549 | int ret = SR_ERR; |
01cf8814 | 550 | |
da1466d6 | 551 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) |
01cf8814 DR |
552 | return ret; |
553 | ||
554 | mso = sdi->priv; | |
555 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
556 | if (sdi->serial->fd == -1) | |
557 | return ret; | |
558 | ||
559 | ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2); | |
e46b8fb1 | 560 | if (ret != SR_OK) |
01cf8814 DR |
561 | return ret; |
562 | ||
5a2326a7 | 563 | sdi->status = SR_ST_ACTIVE; |
01cf8814 DR |
564 | |
565 | /* FIXME: discard serial buffer */ | |
566 | ||
567 | mso_check_trigger(sdi, &mso->trigger_state); | |
7b48d6e1 | 568 | sr_dbg("mso19: trigger state: 0x%x", mso->trigger_state); |
01cf8814 DR |
569 | |
570 | ret = mso_reset_adc(sdi); | |
e46b8fb1 | 571 | if (ret != SR_OK) |
01cf8814 DR |
572 | return ret; |
573 | ||
574 | mso_check_trigger(sdi, &mso->trigger_state); | |
7b48d6e1 | 575 | sr_dbg("mso19: trigger state: 0x%x", mso->trigger_state); |
01cf8814 DR |
576 | |
577 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 578 | // if (ret != SR_OK) |
01cf8814 DR |
579 | // return ret; |
580 | ||
7b48d6e1 | 581 | sr_dbg("mso19: Finished %s", __func__); |
357285a9 | 582 | |
e46b8fb1 UH |
583 | // return SR_ERR; |
584 | return SR_OK; | |
01cf8814 DR |
585 | } |
586 | ||
697785d1 | 587 | static int hw_closedev(int device_index) |
01cf8814 | 588 | { |
a00ba012 | 589 | struct sr_device_instance *sdi; |
01cf8814 | 590 | |
da1466d6 | 591 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) { |
697785d1 UH |
592 | sr_err("mso19: %s: sdi was NULL", __func__); |
593 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
594 | } | |
01cf8814 | 595 | |
697785d1 | 596 | /* TODO */ |
01cf8814 DR |
597 | if (sdi->serial->fd != -1) { |
598 | serial_close(sdi->serial->fd); | |
599 | sdi->serial->fd = -1; | |
5a2326a7 | 600 | sdi->status = SR_ST_INACTIVE; |
01cf8814 | 601 | } |
697785d1 | 602 | |
7b48d6e1 | 603 | sr_dbg("mso19: finished %s", __func__); |
697785d1 | 604 | return SR_OK; |
01cf8814 DR |
605 | } |
606 | ||
607 | static void *hw_get_device_info(int device_index, int device_info_id) | |
608 | { | |
a00ba012 | 609 | struct sr_device_instance *sdi; |
01cf8814 DR |
610 | struct mso *mso; |
611 | void *info = NULL; | |
612 | ||
da1466d6 | 613 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) |
01cf8814 DR |
614 | return NULL; |
615 | mso = sdi->priv; | |
616 | ||
617 | switch (device_info_id) { | |
5a2326a7 | 618 | case SR_DI_INSTANCE: |
01cf8814 DR |
619 | info = sdi; |
620 | break; | |
5a2326a7 | 621 | case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ |
464d12c7 KS |
622 | info = GINT_TO_POINTER(NUM_PROBES); |
623 | break; | |
624 | case SR_DI_PROBE_NAMES: | |
625 | info = probe_names; | |
01cf8814 | 626 | break; |
5a2326a7 | 627 | case SR_DI_SAMPLERATES: |
01cf8814 DR |
628 | info = &samplerates; |
629 | break; | |
5a2326a7 | 630 | case SR_DI_TRIGGER_TYPES: |
01cf8814 DR |
631 | info = "01"; /* FIXME */ |
632 | break; | |
5a2326a7 | 633 | case SR_DI_CUR_SAMPLERATE: |
01cf8814 DR |
634 | info = &mso->cur_rate; |
635 | break; | |
636 | } | |
637 | return info; | |
638 | } | |
639 | ||
640 | static int hw_get_status(int device_index) | |
641 | { | |
a00ba012 | 642 | struct sr_device_instance *sdi; |
01cf8814 | 643 | |
da1466d6 | 644 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) |
5a2326a7 | 645 | return SR_ST_NOT_FOUND; |
01cf8814 DR |
646 | |
647 | return sdi->status; | |
648 | } | |
649 | ||
650 | static int *hw_get_capabilities(void) | |
651 | { | |
652 | return capabilities; | |
653 | } | |
654 | ||
655 | static int hw_set_configuration(int device_index, int capability, void *value) | |
656 | { | |
a00ba012 | 657 | struct sr_device_instance *sdi; |
01cf8814 | 658 | |
da1466d6 | 659 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) |
e46b8fb1 | 660 | return SR_ERR; |
01cf8814 DR |
661 | |
662 | switch (capability) { | |
5a2326a7 | 663 | case SR_HWCAP_SAMPLERATE: |
01cf8814 | 664 | return mso_configure_rate(sdi, *(uint64_t *) value); |
5a2326a7 UH |
665 | case SR_HWCAP_PROBECONFIG: |
666 | case SR_HWCAP_LIMIT_SAMPLES: | |
01cf8814 | 667 | default: |
e46b8fb1 | 668 | return SR_OK; /* FIXME */ |
01cf8814 | 669 | } |
01cf8814 DR |
670 | } |
671 | ||
672 | #define MSO_TRIGGER_UNKNOWN '!' | |
673 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
674 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
675 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
676 | #define MSO_TRIGGER_WAIT '4' | |
677 | #define MSO_TRIGGER_FIRED '5' | |
678 | #define MSO_TRIGGER_DATAREADY '6' | |
679 | ||
680 | /* FIXME: Pass errors? */ | |
681 | static int receive_data(int fd, int revents, void *user_data) | |
682 | { | |
a00ba012 | 683 | struct sr_device_instance *sdi = user_data; |
01cf8814 | 684 | struct mso *mso = sdi->priv; |
b9c735a2 | 685 | struct sr_datafeed_packet packet; |
e42ef08d | 686 | struct sr_datafeed_logic logic; |
01cf8814 DR |
687 | uint8_t in[1024], logic_out[1024]; |
688 | double analog_out[1024]; | |
689 | size_t i, s; | |
690 | ||
691 | revents = revents; | |
692 | ||
2119ab03 | 693 | s = serial_read(fd, in, sizeof(in)); |
01cf8814 DR |
694 | if (s <= 0) |
695 | return FALSE; | |
696 | ||
697 | /* No samples */ | |
698 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
699 | mso->trigger_state = in[0]; | |
700 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
701 | mso_read_buffer(sdi); | |
702 | mso->buffer_n = 0; | |
703 | } else { | |
704 | mso_check_trigger(sdi, NULL); | |
705 | } | |
706 | return FALSE; | |
707 | } | |
708 | ||
709 | /* the hardware always dumps 1024 samples, 24bits each */ | |
710 | if (mso->buffer_n < 3072) { | |
711 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
712 | mso->buffer_n += s; | |
713 | } | |
714 | if (mso->buffer_n < 3072) | |
715 | return FALSE; | |
716 | ||
717 | /* do the conversion */ | |
718 | for (i = 0; i < 1024; i++) { | |
719 | /* FIXME: Need to do conversion to mV */ | |
720 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
721 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
722 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
723 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
724 | } | |
725 | ||
5a2326a7 | 726 | packet.type = SR_DF_LOGIC; |
42eb54fb | 727 | packet.payload = &logic; |
e42ef08d RC |
728 | logic.length = 1024; |
729 | logic.unitsize = 1; | |
730 | logic.data = logic_out; | |
8a2efef2 | 731 | sr_session_bus(mso->session_id, &packet); |
01cf8814 | 732 | |
42eb54fb UH |
733 | // Dont bother fixing this yet, keep it "old style" |
734 | /* | |
5a2326a7 | 735 | packet.type = SR_DF_ANALOG; |
01cf8814 DR |
736 | packet.length = 1024; |
737 | packet.unitsize = sizeof(double); | |
738 | packet.payload = analog_out; | |
42eb54fb UH |
739 | sr_session_bus(mso->session_id, &packet); |
740 | */ | |
01cf8814 | 741 | |
5a2326a7 | 742 | packet.type = SR_DF_END; |
8a2efef2 | 743 | sr_session_bus(mso->session_id, &packet); |
01cf8814 DR |
744 | |
745 | return TRUE; | |
746 | } | |
747 | ||
748 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
749 | { | |
a00ba012 | 750 | struct sr_device_instance *sdi; |
01cf8814 | 751 | struct mso *mso; |
b9c735a2 UH |
752 | struct sr_datafeed_packet packet; |
753 | struct sr_datafeed_header header; | |
e46b8fb1 | 754 | int ret = SR_ERR; |
01cf8814 | 755 | |
da1466d6 | 756 | if (!(sdi = sr_dev_inst_get(device_instances, device_index))) |
01cf8814 DR |
757 | return ret; |
758 | mso = sdi->priv; | |
759 | ||
760 | /* FIXME: No need to do full reconfigure every time */ | |
761 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 762 | // if (ret != SR_OK) |
01cf8814 DR |
763 | // return ret; |
764 | ||
765 | /* FIXME: ACDC Mode */ | |
a8467191 RC |
766 | mso->ctlbase1 &= 0x7f; |
767 | // mso->ctlbase1 |= mso->acdcmode; | |
01cf8814 DR |
768 | |
769 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
e46b8fb1 | 770 | if (ret != SR_OK) |
01cf8814 DR |
771 | return ret; |
772 | ||
773 | /* set dac offset */ | |
774 | ret = mso_dac_out(sdi, mso->dac_offset); | |
e46b8fb1 | 775 | if (ret != SR_OK) |
01cf8814 DR |
776 | return ret; |
777 | ||
778 | ret = mso_configure_threshold_level(sdi); | |
e46b8fb1 | 779 | if (ret != SR_OK) |
01cf8814 DR |
780 | return ret; |
781 | ||
782 | ret = mso_configure_trigger(sdi); | |
e46b8fb1 | 783 | if (ret != SR_OK) |
01cf8814 DR |
784 | return ret; |
785 | ||
786 | /* FIXME: trigger_position */ | |
787 | ||
788 | ||
789 | /* END of config hardware part */ | |
790 | ||
791 | /* with trigger */ | |
792 | ret = mso_arm(sdi); | |
e46b8fb1 | 793 | if (ret != SR_OK) |
01cf8814 DR |
794 | return ret; |
795 | ||
796 | /* without trigger */ | |
797 | // ret = mso_force_capture(sdi); | |
e46b8fb1 | 798 | // if (ret != SR_OK) |
01cf8814 DR |
799 | // return ret; |
800 | ||
801 | mso_check_trigger(sdi, &mso->trigger_state); | |
802 | ret = mso_check_trigger(sdi, NULL); | |
e46b8fb1 | 803 | if (ret != SR_OK) |
01cf8814 DR |
804 | return ret; |
805 | ||
806 | mso->session_id = session_device_id; | |
6f1be0a2 | 807 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); |
01cf8814 | 808 | |
5a2326a7 | 809 | packet.type = SR_DF_HEADER; |
01cf8814 DR |
810 | packet.payload = (unsigned char *) &header; |
811 | header.feed_version = 1; | |
812 | gettimeofday(&header.starttime, NULL); | |
813 | header.samplerate = mso->cur_rate; | |
5c64390e | 814 | // header.num_analog_probes = 1; |
01cf8814 | 815 | header.num_logic_probes = 8; |
8a2efef2 | 816 | sr_session_bus(session_device_id, &packet); |
01cf8814 DR |
817 | |
818 | return ret; | |
819 | } | |
820 | ||
821 | /* FIXME */ | |
3010f21c | 822 | static int hw_stop_acquisition(int device_index, gpointer session_device_id) |
01cf8814 | 823 | { |
b9c735a2 | 824 | struct sr_datafeed_packet packet; |
01cf8814 DR |
825 | |
826 | device_index = device_index; | |
827 | ||
5a2326a7 | 828 | packet.type = SR_DF_END; |
8a2efef2 | 829 | sr_session_bus(session_device_id, &packet); |
3010f21c UH |
830 | |
831 | return SR_OK; | |
01cf8814 DR |
832 | } |
833 | ||
ca070ed9 | 834 | SR_PRIV struct sr_device_plugin link_mso19_plugin_info = { |
01cf8814 | 835 | .name = "link-mso19", |
9f8274a5 | 836 | .longname = "Link Instruments MSO-19", |
01cf8814 DR |
837 | .api_version = 1, |
838 | .init = hw_init, | |
839 | .cleanup = hw_cleanup, | |
86f5e3d8 UH |
840 | .opendev = hw_opendev, |
841 | .closedev = hw_closedev, | |
01cf8814 DR |
842 | .get_device_info = hw_get_device_info, |
843 | .get_status = hw_get_status, | |
844 | .get_capabilities = hw_get_capabilities, | |
845 | .set_configuration = hw_set_configuration, | |
846 | .start_acquisition = hw_start_acquisition, | |
847 | .stop_acquisition = hw_stop_acquisition, | |
848 | }; |