ed9fbd9db20661a0c7b472e2741c39a89f17f560
[libsigrok.git] / src / hardware / scpi-pps / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2017,2019 Frank Stettner <frank-stettner@gmx.net>
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.
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/>.
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include <strings.h>
24 #include "scpi.h"
25 #include "protocol.h"
26
27 static struct sr_dev_driver scpi_pps_driver_info;
28 static struct sr_dev_driver hp_ib_pps_driver_info;
29
30 static const uint32_t scanopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33 };
34
35 static const uint32_t drvopts[] = {
36         SR_CONF_POWER_SUPPLY,
37 };
38
39 static const struct pps_channel_instance pci[] = {
40         { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
41         { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
42         { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
43         { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
44 };
45
46 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
47                 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
48                 struct sr_scpi_hw_info **scpi_response))
49 {
50         struct dev_context *devc;
51         struct sr_dev_inst *sdi;
52         struct sr_scpi_hw_info *hw_info;
53         struct sr_channel_group *cg;
54         struct sr_channel *ch;
55         const struct scpi_pps *device;
56         struct pps_channel *pch;
57         struct channel_spec *channels;
58         struct channel_group_spec *channel_groups, *cgs;
59         struct pps_channel_group *pcg;
60         GRegex *model_re;
61         GMatchInfo *model_mi;
62         GSList *l;
63         uint64_t mask;
64         unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
65         int ret;
66         const char *vendor;
67         char ch_name[16];
68
69         if (get_hw_id(scpi, &hw_info) != SR_OK) {
70                 sr_info("Couldn't get IDN response.");
71                 return NULL;
72         }
73
74         device = NULL;
75         for (i = 0; i < num_pps_profiles; i++) {
76                 vendor = sr_vendor_alias(hw_info->manufacturer);
77                 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
78                         continue;
79                 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
80                 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
81                         device = &pps_profiles[i];
82                 g_match_info_unref(model_mi);
83                 g_regex_unref(model_re);
84                 if (device)
85                         break;
86         }
87         if (!device) {
88                 sr_scpi_hw_info_free(hw_info);
89                 return NULL;
90         }
91
92         sdi = g_malloc0(sizeof(struct sr_dev_inst));
93         sdi->vendor = g_strdup(vendor);
94         sdi->model = g_strdup(hw_info->model);
95         sdi->version = g_strdup(hw_info->firmware_version);
96         sdi->conn = scpi;
97         sdi->driver = &scpi_pps_driver_info;
98         sdi->inst_type = SR_INST_SCPI;
99         sdi->serial_num = g_strdup(hw_info->serial_number);
100
101         devc = g_malloc0(sizeof(struct dev_context));
102         devc->device = device;
103         sr_sw_limits_init(&devc->limits);
104         sdi->priv = devc;
105
106         if (device->num_channels) {
107                 /* Static channels and groups. */
108                 channels = (struct channel_spec *)device->channels;
109                 num_channels = device->num_channels;
110                 channel_groups = (struct channel_group_spec *)device->channel_groups;
111                 num_channel_groups = device->num_channel_groups;
112         } else {
113                 /* Channels and groups need to be probed. */
114                 ret = device->probe_channels(sdi, hw_info, &channels, &num_channels,
115                                 &channel_groups, &num_channel_groups);
116                 if (ret != SR_OK) {
117                         sr_err("Failed to probe for channels.");
118                         return NULL;
119                 }
120                 /*
121                  * Since these were dynamically allocated, we'll need to free them
122                  * later.
123                  */
124                 devc->channels = channels;
125                 devc->channel_groups = channel_groups;
126         }
127
128         ch_idx = 0;
129         for (ch_num = 0; ch_num < num_channels; ch_num++) {
130                 /* Create one channel per measurable output unit. */
131                 for (i = 0; i < ARRAY_SIZE(pci); i++) {
132                         if (!sr_scpi_cmd_get(devc->device->commands, pci[i].command))
133                                 continue;
134                         g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
135                                         channels[ch_num].name);
136                         ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
137                                         ch_name);
138                         pch = g_malloc0(sizeof(struct pps_channel));
139                         pch->hw_output_idx = ch_num;
140                         pch->hwname = channels[ch_num].name;
141                         pch->mq = pci[i].mq;
142                         ch->priv = pch;
143                 }
144         }
145
146         for (i = 0; i < num_channel_groups; i++) {
147                 cgs = &channel_groups[i];
148                 cg = g_malloc0(sizeof(struct sr_channel_group));
149                 cg->name = g_strdup(cgs->name);
150                 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
151                         if (cgs->channel_index_mask & mask) {
152                                 for (l = sdi->channels; l; l = l->next) {
153                                         ch = l->data;
154                                         pch = ch->priv;
155                                         /* Add mqflags from channel_group_spec only to voltage
156                                          * and current channels.
157                                          */
158                                         if (pch->mq == SR_MQ_VOLTAGE || pch->mq == SR_MQ_CURRENT)
159                                                 pch->mqflags = cgs->mqflags;
160                                         else
161                                                 pch->mqflags = 0;
162                                         if (pch->hw_output_idx == j)
163                                                 cg->channels = g_slist_append(cg->channels, ch);
164                                 }
165                         }
166                 }
167                 pcg = g_malloc0(sizeof(struct pps_channel_group));
168                 pcg->features = cgs->features;
169                 cg->priv = pcg;
170                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
171         }
172
173         sr_scpi_hw_info_free(hw_info);
174         hw_info = NULL;
175
176         /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
177         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
178                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
179                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
180
181         return sdi;
182 }
183
184 static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
185 {
186         int ret;
187         gboolean matches;
188         char *response;
189         GRegex *version_regex;
190
191         ret = sr_scpi_get_string(scpi, "ROM?", &response);
192         if (ret != SR_OK && !response)
193                 return NULL;
194
195         /* Example version string: "B01 B01" */
196         version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL);
197         matches = g_regex_match(version_regex, response, 0, NULL);
198         g_regex_unref(version_regex);
199
200         if (!matches) {
201                 /* Not a valid version string. Ignore it. */
202                 g_free(response);
203                 response = NULL;
204         } else {
205                 /* Replace space with dot. */
206                 response[3] = '.';
207         }
208
209         return response;
210 }
211
212 /*
213  * This function assumes the response is in the form "HP<model_number>"
214  *
215  * HP made many GPIB (then called HP-IB) instruments before the SCPI command
216  * set was introduced into the standard. We haven't seen any non-HP instruments
217  * which respond to the "ID?" query, so assume all are HP for now.
218  */
219 static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
220                           struct sr_scpi_hw_info **scpi_response)
221 {
222         int ret;
223         char *response;
224         struct sr_scpi_hw_info *hw_info;
225
226         ret = sr_scpi_get_string(scpi, "ID?", &response);
227         if ((ret != SR_OK) || !response)
228                 return SR_ERR;
229
230         hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
231
232         *scpi_response = hw_info;
233         hw_info->model = response;
234         hw_info->firmware_version = hpib_get_revision(scpi);
235         hw_info->manufacturer = g_strdup("HP");
236
237         return SR_OK;
238 }
239
240 static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
241 {
242         return probe_device(scpi, sr_scpi_get_hw_id);
243 }
244
245 static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
246 {
247         return probe_device(scpi, hpib_get_hw_id);
248 }
249
250 static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
251 {
252         return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
253 }
254
255 static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
256 {
257         return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
258 }
259
260 static int dev_open(struct sr_dev_inst *sdi)
261 {
262         struct dev_context *devc;
263         struct sr_scpi_dev_inst *scpi;
264         GVariant *beeper;
265
266         scpi = sdi->conn;
267         if (sr_scpi_open(scpi) < 0)
268                 return SR_ERR;
269
270         devc = sdi->priv;
271
272         /* Don't send SCPI_CMD_REMOTE for HP 66xxB using SCPI over GPIB. */
273         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
274                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
275                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE);
276
277         devc->beeper_was_set = FALSE;
278         if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL,
279                         &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
280                 if (g_variant_get_boolean(beeper)) {
281                         devc->beeper_was_set = TRUE;
282                         sr_scpi_cmd(sdi, devc->device->commands,
283                                 0, NULL, SCPI_CMD_BEEPER_DISABLE);
284                 }
285                 g_variant_unref(beeper);
286         }
287
288         return SR_OK;
289 }
290
291 static int dev_close(struct sr_dev_inst *sdi)
292 {
293         struct sr_scpi_dev_inst *scpi;
294         struct dev_context *devc;
295
296         devc = sdi->priv;
297         scpi = sdi->conn;
298
299         if (!scpi)
300                 return SR_ERR_BUG;
301
302         if (devc->beeper_was_set)
303                 sr_scpi_cmd(sdi, devc->device->commands,
304                         0, NULL, SCPI_CMD_BEEPER_ENABLE);
305
306         /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
307         if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB &&
308                         scpi->transport == SCPI_TRANSPORT_LIBGPIB))
309                 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
310
311         return sr_scpi_close(scpi);
312 }
313
314 static void clear_helper(struct dev_context *devc)
315 {
316         g_free(devc->channels);
317         g_free(devc->channel_groups);
318 }
319
320 static int dev_clear(const struct sr_dev_driver *di)
321 {
322         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
323 }
324
325 static int config_get(uint32_t key, GVariant **data,
326         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
327 {
328         struct dev_context *devc;
329         const GVariantType *gvtype;
330         unsigned int i;
331         int channel_group_cmd;
332         char *channel_group_name;
333         int cmd, ret;
334         const char *s;
335         int reg;
336         gboolean is_hmp_sqii;
337
338         if (!sdi)
339                 return SR_ERR_ARG;
340
341         devc = sdi->priv;
342
343         if (cg) {
344                 /*
345                  * These options only apply to channel groups with a single
346                  * channel -- they're per-channel settings for the device.
347                  */
348
349                 /*
350                  * Config keys are handled below depending on whether a channel
351                  * group was provided by the frontend. However some of these
352                  * take a CG on one PPS but not on others. Check the device's
353                  * profile for that here, and NULL out the channel group as needed.
354                  */
355                 for (i = 0; i < devc->device->num_devopts; i++) {
356                         if (devc->device->devopts[i] == key) {
357                                 cg = NULL;
358                                 break;
359                         }
360                 }
361         }
362
363         gvtype = NULL;
364         cmd = -1;
365         switch (key) {
366         case SR_CONF_ENABLED:
367                 gvtype = G_VARIANT_TYPE_BOOLEAN;
368                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
369                 break;
370         case SR_CONF_VOLTAGE:
371                 gvtype = G_VARIANT_TYPE_DOUBLE;
372                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
373                 break;
374         case SR_CONF_VOLTAGE_TARGET:
375                 gvtype = G_VARIANT_TYPE_DOUBLE;
376                 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
377                 break;
378         case SR_CONF_OUTPUT_FREQUENCY:
379                 gvtype = G_VARIANT_TYPE_DOUBLE;
380                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
381                 break;
382         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
383                 gvtype = G_VARIANT_TYPE_DOUBLE;
384                 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
385                 break;
386         case SR_CONF_CURRENT:
387                 gvtype = G_VARIANT_TYPE_DOUBLE;
388                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
389                 break;
390         case SR_CONF_CURRENT_LIMIT:
391                 gvtype = G_VARIANT_TYPE_DOUBLE;
392                 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
393                 break;
394         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
395                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
396                         /* OVP is always enabled. */
397                         *data = g_variant_new_boolean(TRUE);
398                         return 0;
399                 }
400                 gvtype = G_VARIANT_TYPE_BOOLEAN;
401                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
402                 break;
403         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
404                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
405                         devc->device->dialect == SCPI_DIALECT_HP_COMP)
406                         gvtype = G_VARIANT_TYPE_STRING;
407                 else
408                         gvtype = G_VARIANT_TYPE_BOOLEAN;
409                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
410                 break;
411         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
412                 gvtype = G_VARIANT_TYPE_DOUBLE;
413                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
414                 break;
415         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
416                 gvtype = G_VARIANT_TYPE_BOOLEAN;
417                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
418                 break;
419         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
420                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
421                         devc->device->dialect == SCPI_DIALECT_HP_COMP)
422                         gvtype = G_VARIANT_TYPE_STRING;
423                 else
424                         gvtype = G_VARIANT_TYPE_BOOLEAN;
425                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
426                 break;
427         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
428                 gvtype = G_VARIANT_TYPE_DOUBLE;
429                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
430                 break;
431         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
432                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
433                         /* OTP is always enabled. */
434                         *data = g_variant_new_boolean(TRUE);
435                         return 0;
436                 }
437                 gvtype = G_VARIANT_TYPE_BOOLEAN;
438                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
439                 break;
440         case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
441                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
442                         devc->device->dialect == SCPI_DIALECT_HP_COMP ||
443                         devc->device->dialect == SCPI_DIALECT_HMP)
444                         gvtype = G_VARIANT_TYPE_STRING;
445                 else
446                         gvtype = G_VARIANT_TYPE_BOOLEAN;
447                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
448                 break;
449         case SR_CONF_REGULATION:
450                 gvtype = G_VARIANT_TYPE_STRING;
451                 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
452                 break;
453         default:
454                 return sr_sw_limits_config_get(&devc->limits, key, data);
455         }
456         if (!gvtype)
457                 return SR_ERR_NA;
458
459         channel_group_cmd = 0;
460         channel_group_name = NULL;
461         if (cg) {
462                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
463                 channel_group_name = g_strdup(cg->name);
464         }
465
466         is_hmp_sqii = FALSE;
467         is_hmp_sqii |= cmd == SCPI_CMD_GET_OUTPUT_REGULATION;
468         is_hmp_sqii |= cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
469         is_hmp_sqii &= devc->device->dialect == SCPI_DIALECT_HMP;
470         if (is_hmp_sqii) {
471                 if (!cg) {
472                         /* STAT:QUES:INST:ISUMx query requires channel spec. */
473                         sr_err("Need a channel group for regulation or OTP-active query.");
474                         return SR_ERR_NA;
475                 }
476                 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
477                         0, NULL, data, gvtype, cmd, channel_group_name);
478         } else {
479                 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
480                         channel_group_cmd, channel_group_name, data, gvtype, cmd);
481         }
482         g_free(channel_group_name);
483
484         /*
485          * Handle special cases
486          */
487
488         if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
489                 if (devc->device->dialect == SCPI_DIALECT_PHILIPS) {
490                         /*
491                         * The Philips PM2800 series returns VOLT/CURR. We always return
492                         * a GVariant string in the Rigol notation (CV/CC/UR).
493                         */
494                         s = g_variant_get_string(*data, NULL);
495                         if (!g_strcmp0(s, "VOLT")) {
496                                 g_variant_unref(*data);
497                                 *data = g_variant_new_string("CV");
498                         } else if (!g_strcmp0(s, "CURR")) {
499                                 g_variant_unref(*data);
500                                 *data = g_variant_new_string("CC");
501                         }
502                 }
503                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
504                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
505                         s = g_variant_get_string(*data, NULL);
506                         sr_atoi(s, &reg);
507                         g_variant_unref(*data);
508                         if (reg & (1 << 0))
509                                 *data = g_variant_new_string("CV");
510                         else if (reg & (1 << 1))
511                                 *data = g_variant_new_string("CC");
512                         else if (reg & (1 << 2))
513                                 *data = g_variant_new_string("UR");
514                         else if (reg & (1 << 9))
515                                 *data = g_variant_new_string("CC-");
516                         else
517                                 *data = g_variant_new_string("");
518                 }
519                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
520                         /* Evaluate Operational Status Register from a HP 66xxB. */
521                         s = g_variant_get_string(*data, NULL);
522                         sr_atoi(s, &reg);
523                         g_variant_unref(*data);
524                         if (reg & (1 << 8))
525                                 *data = g_variant_new_string("CV");
526                         else if (reg & (1 << 10))
527                                 *data = g_variant_new_string("CC");
528                         else if (reg & (1 << 11))
529                                 *data = g_variant_new_string("CC-");
530                         else
531                                 *data = g_variant_new_string("UR");
532                 }
533                 if (devc->device->dialect == SCPI_DIALECT_HMP) {
534                         /* Evaluate Condition Status Register from a HMP series device. */
535                         s = g_variant_get_string(*data, NULL);
536                         sr_atoi(s, &reg);
537                         g_variant_unref(*data);
538                         if (reg & (1 << 0))
539                                 *data = g_variant_new_string("CC");
540                         else if (reg & (1 << 1))
541                                 *data = g_variant_new_string("CV");
542                         else
543                                 *data = g_variant_new_string("UR");
544                 }
545
546                 s = g_variant_get_string(*data, NULL);
547                 if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") &&
548                         g_strcmp0(s, "UR") && g_strcmp0(s, "")) {
549
550                         sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
551                         ret = SR_ERR_DATA;
552                 }
553         }
554
555         if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) {
556                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
557                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
558                         s = g_variant_get_string(*data, NULL);
559                         sr_atoi(s, &reg);
560                         g_variant_unref(*data);
561                         *data = g_variant_new_boolean(reg & (1 << 3));
562                 }
563                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
564                         /* Evaluate Questionable Status Register bit 0 from a HP 66xxB. */
565                         s = g_variant_get_string(*data, NULL);
566                         sr_atoi(s, &reg);
567                         g_variant_unref(*data);
568                         *data = g_variant_new_boolean(reg & (1 << 0));
569                 }
570         }
571
572         if (cmd == SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE) {
573                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
574                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
575                         s = g_variant_get_string(*data, NULL);
576                         sr_atoi(s, &reg);
577                         g_variant_unref(*data);
578                         *data = g_variant_new_boolean(reg & (1 << 6));
579                 }
580                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
581                         /* Evaluate Questionable Status Register bit 1 from a HP 66xxB. */
582                         s = g_variant_get_string(*data, NULL);
583                         sr_atoi(s, &reg);
584                         g_variant_unref(*data);
585                         *data = g_variant_new_boolean(reg & (1 << 1));
586                 }
587         }
588
589         if (cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE) {
590                 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
591                         /* Evaluate Status Register from a HP 66xx in COMP mode. */
592                         s = g_variant_get_string(*data, NULL);
593                         sr_atoi(s, &reg);
594                         g_variant_unref(*data);
595                         *data = g_variant_new_boolean(reg & (1 << 4));
596                 }
597                 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
598                     devc->device->dialect == SCPI_DIALECT_HMP) {
599                         /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */
600                         s = g_variant_get_string(*data, NULL);
601                         sr_atoi(s, &reg);
602                         g_variant_unref(*data);
603                         *data = g_variant_new_boolean(reg & (1 << 4));
604                 }
605         }
606
607         return ret;
608 }
609
610 static int config_set(uint32_t key, GVariant *data,
611         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
612 {
613         struct dev_context *devc;
614         double d;
615         int channel_group_cmd;
616         char *channel_group_name;
617         int ret;
618
619         if (!sdi)
620                 return SR_ERR_ARG;
621
622         channel_group_cmd = 0;
623         channel_group_name = NULL;
624         if (cg) {
625                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
626                 channel_group_name = g_strdup(cg->name);
627         }
628
629         devc = sdi->priv;
630
631         switch (key) {
632         case SR_CONF_ENABLED:
633                 if (g_variant_get_boolean(data))
634                         ret = sr_scpi_cmd(sdi, devc->device->commands,
635                                         channel_group_cmd, channel_group_name,
636                                         SCPI_CMD_SET_OUTPUT_ENABLE);
637                 else
638                         ret = sr_scpi_cmd(sdi, devc->device->commands,
639                                         channel_group_cmd, channel_group_name,
640                                         SCPI_CMD_SET_OUTPUT_DISABLE);
641                 break;
642         case SR_CONF_VOLTAGE_TARGET:
643                 d = g_variant_get_double(data);
644                 ret = sr_scpi_cmd(sdi, devc->device->commands,
645                                 channel_group_cmd, channel_group_name,
646                                 SCPI_CMD_SET_VOLTAGE_TARGET, d);
647                 break;
648         case SR_CONF_OUTPUT_FREQUENCY_TARGET:
649                 d = g_variant_get_double(data);
650                 ret = sr_scpi_cmd(sdi, devc->device->commands,
651                                 channel_group_cmd, channel_group_name,
652                                 SCPI_CMD_SET_FREQUENCY_TARGET, d);
653                 break;
654         case SR_CONF_CURRENT_LIMIT:
655                 d = g_variant_get_double(data);
656                 ret = sr_scpi_cmd(sdi, devc->device->commands,
657                                 channel_group_cmd, channel_group_name,
658                                 SCPI_CMD_SET_CURRENT_LIMIT, d);
659                 break;
660         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
661                 if (g_variant_get_boolean(data))
662                         ret = sr_scpi_cmd(sdi, devc->device->commands,
663                                         channel_group_cmd, channel_group_name,
664                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
665                 else
666                         ret = sr_scpi_cmd(sdi, devc->device->commands,
667                                         channel_group_cmd, channel_group_name,
668                                         SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
669                 break;
670         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
671                 d = g_variant_get_double(data);
672                 ret = sr_scpi_cmd(sdi, devc->device->commands,
673                                 channel_group_cmd, channel_group_name,
674                                 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
675                 break;
676         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
677                 if (g_variant_get_boolean(data))
678                         ret = sr_scpi_cmd(sdi, devc->device->commands,
679                                         channel_group_cmd, channel_group_name,
680                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
681                 else
682                         ret = sr_scpi_cmd(sdi, devc->device->commands,
683                                         channel_group_cmd, channel_group_name,
684                                         SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
685                 break;
686         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
687                 d = g_variant_get_double(data);
688                 ret = sr_scpi_cmd(sdi, devc->device->commands,
689                                 channel_group_cmd, channel_group_name,
690                                 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
691                 break;
692         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
693                 if (g_variant_get_boolean(data))
694                         ret = sr_scpi_cmd(sdi, devc->device->commands,
695                                         channel_group_cmd, channel_group_name,
696                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
697                 else
698                         ret = sr_scpi_cmd(sdi, devc->device->commands,
699                                         channel_group_cmd, channel_group_name,
700                                         SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
701                 break;
702         default:
703                 ret = sr_sw_limits_config_set(&devc->limits, key, data);
704         }
705
706         g_free(channel_group_name);
707
708         return ret;
709 }
710
711 static int config_list(uint32_t key, GVariant **data,
712         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
713 {
714         struct dev_context *devc;
715         struct sr_channel *ch;
716         struct pps_channel *pch;
717         const struct channel_spec *ch_spec;
718         int i;
719         const char *s[16];
720
721         devc = (sdi) ? sdi->priv : NULL;
722
723         if (!cg) {
724                 switch (key) {
725                 case SR_CONF_SCAN_OPTIONS:
726                 case SR_CONF_DEVICE_OPTIONS:
727                         return std_opts_config_list(key, data, sdi, cg,
728                                 ARRAY_AND_SIZE(scanopts),
729                                 ARRAY_AND_SIZE(drvopts),
730                                 (devc && devc->device) ? devc->device->devopts : NULL,
731                                 (devc && devc->device) ? devc->device->num_devopts : 0);
732                         break;
733                 case SR_CONF_CHANNEL_CONFIG:
734                         if (!devc || !devc->device)
735                                 return SR_ERR_ARG;
736                         /* Not used. */
737                         i = 0;
738                         if (devc->device->features & PPS_INDEPENDENT)
739                                 s[i++] = "Independent";
740                         if (devc->device->features & PPS_SERIES)
741                                 s[i++] = "Series";
742                         if (devc->device->features & PPS_PARALLEL)
743                                 s[i++] = "Parallel";
744                         if (i == 0) {
745                                 /*
746                                  * Shouldn't happen: independent-only devices
747                                  * shouldn't advertise this option at all.
748                                  */
749                                 return SR_ERR_NA;
750                         }
751                         *data = g_variant_new_strv(s, i);
752                         break;
753                 default:
754                         return SR_ERR_NA;
755                 }
756         } else {
757                 /*
758                  * Per-channel-group options depending on a channel are actually
759                  * done with the first channel. Channel groups in PPS can have
760                  * more than one channel, but they will typically be of equal
761                  * specification for use in series or parallel mode.
762                  */
763                 ch = cg->channels->data;
764                 pch = ch->priv;
765                 if (!devc || !devc->device)
766                         return SR_ERR_ARG;
767                 ch_spec = &(devc->device->channels[pch->hw_output_idx]);
768
769                 switch (key) {
770                 case SR_CONF_DEVICE_OPTIONS:
771                         *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
772                         break;
773                 case SR_CONF_VOLTAGE_TARGET:
774                         *data = std_gvar_min_max_step_array(ch_spec->voltage);
775                         break;
776                 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
777                         *data = std_gvar_min_max_step_array(ch_spec->frequency);
778                         break;
779                 case SR_CONF_CURRENT_LIMIT:
780                         *data = std_gvar_min_max_step_array(ch_spec->current);
781                         break;
782                 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
783                         *data = std_gvar_min_max_step_array(ch_spec->ovp);
784                         break;
785                 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
786                         *data = std_gvar_min_max_step_array(ch_spec->ocp);
787                         break;
788                 default:
789                         return SR_ERR_NA;
790                 }
791         }
792
793         return SR_OK;
794 }
795
796 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
797 {
798         struct dev_context *devc;
799         struct sr_scpi_dev_inst *scpi;
800         int ret;
801
802         devc = sdi->priv;
803         scpi = sdi->conn;
804
805         /* Prime the pipe with the first channel. */
806         devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
807
808         /* Device specific initialization before acquisition starts. */
809         if (devc->device->init_acquisition)
810                 devc->device->init_acquisition(sdi);
811
812         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
813                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
814                 return ret;
815         std_session_send_df_header(sdi);
816         sr_sw_limits_acquisition_start(&devc->limits);
817
818         return SR_OK;
819 }
820
821 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
822 {
823         struct sr_scpi_dev_inst *scpi;
824
825         scpi = sdi->conn;
826
827         sr_scpi_source_remove(sdi->session, scpi);
828
829         std_session_send_df_end(sdi);
830
831         return SR_OK;
832 }
833
834 static struct sr_dev_driver scpi_pps_driver_info = {
835         .name = "scpi-pps",
836         .longname = "SCPI PPS",
837         .api_version = 1,
838         .init = std_init,
839         .cleanup = std_cleanup,
840         .scan = scan_scpi_pps,
841         .dev_list = std_dev_list,
842         .dev_clear = dev_clear,
843         .config_get = config_get,
844         .config_set = config_set,
845         .config_list = config_list,
846         .dev_open = dev_open,
847         .dev_close = dev_close,
848         .dev_acquisition_start = dev_acquisition_start,
849         .dev_acquisition_stop = dev_acquisition_stop,
850         .context = NULL,
851 };
852
853 static struct sr_dev_driver hp_ib_pps_driver_info = {
854         .name = "hpib-pps",
855         .longname = "HP-IB PPS",
856         .api_version = 1,
857         .init = std_init,
858         .cleanup = std_cleanup,
859         .scan = scan_hpib_pps,
860         .dev_list = std_dev_list,
861         .dev_clear = dev_clear,
862         .config_get = config_get,
863         .config_set = config_set,
864         .config_list = config_list,
865         .dev_open = dev_open,
866         .dev_close = dev_close,
867         .dev_acquisition_start = dev_acquisition_start,
868         .dev_acquisition_stop = dev_acquisition_stop,
869         .context = NULL,
870 };
871 SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
872 SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);