]> sigrok.org Git - libsigrok.git/blob - src/hardware/motech-lps-30x/api.c
464ce7db8ae923ac5a94537bde4d941d6ed347fb
[libsigrok.git] / src / hardware / motech-lps-30x / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
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 <ctype.h>
23 #include <math.h>
24 #include <string.h>
25 #include "protocol.h"
26
27 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
28 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args);
29 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...);
30 SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...);
31 SR_PRIV int lps_query_status(struct sr_dev_inst *sdi);
32
33 #define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
34
35 #define VENDOR_MOTECH "Motech"
36
37 static const uint32_t scanopts[] = {
38         SR_CONF_CONN,
39         SR_CONF_SERIALCOMM,
40 };
41
42 static const uint32_t drvopts[] = {
43         SR_CONF_POWER_SUPPLY,
44 };
45
46 static const uint32_t devopts[] = {
47         SR_CONF_CONTINUOUS,
48         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
49         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
50         SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 };
52
53 /** Hardware capabilities channel 1, 2. */
54 static const uint32_t devopts_cg_ch12[] = {
55         SR_CONF_VOLTAGE | SR_CONF_GET,
56         SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57         SR_CONF_CURRENT | SR_CONF_GET,
58         SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59         SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
60 };
61
62 /** Hardware capabilities channel 3 (LPS-304/305 only). */
63 static const uint32_t devopts_cg_ch3[] = {
64         SR_CONF_VOLTAGE | SR_CONF_GET,
65         SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
66 };
67
68 static const char *channel_modes[] = {
69         "Independent",
70         "Track1",
71         "Track2",
72 };
73
74 static const struct lps_modelspec models[] = {
75         { LPS_UNKNOWN, "Dummy", 0,
76                 {
77
78                 }
79         },
80         { LPS_301, "LPS-301", 1,
81                 {
82                         /* Channel 1 */
83                         { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } },
84                 },
85         },
86         { LPS_302, "LPS-302", 1,
87                 {
88                         /* Channel 1 */
89                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
90                 },
91         },
92         { LPS_303, "LPS-303", 1,
93                 {
94                         /* Channel 1 */
95                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
96                 },
97         },
98         { LPS_304, "LPS-304", 3,
99                 {
100                         /* Channel 1 */
101                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
102                         /* Channel 2 */
103                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
104                         /* Channel 3 */
105                         { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } },
106                 },
107         },
108         { LPS_305, "LPS-305", 3,
109                 {
110                         /* Channel 1 */
111                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
112                         /* Channel 2 */
113                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
114                         /* Channel 3 */
115                         { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } },
116                 },
117         },
118 };
119
120 /** Send command to device with va_list. */
121 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args)
122 {
123         int retc;
124         char auxfmt[LINELEN_MAX];
125         char buf[LINELEN_MAX];
126
127         snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt);
128         vsnprintf(buf, sizeof(buf), auxfmt, args);
129
130         sr_spew("lps_send_va: \"%s\"", buf);
131
132         retc = serial_write_blocking(serial, buf, strlen(buf),
133                         serial_timeout(serial, strlen(buf)));
134
135         if (retc < 0)
136                 return SR_ERR;
137
138         return SR_OK;
139 }
140
141 /** Send command to device. */
142 SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char *fmt, ...)
143 {
144         int retc;
145         va_list args;
146
147         va_start(args, fmt);
148         retc = lps_send_va(serial, fmt, args);
149         va_end(args);
150
151         return retc;
152 }
153
154 /** Send command and consume simple OK reply. */
155 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
156 {
157         int retc;
158         va_list args;
159         char buf[LINELEN_MAX];
160         char *bufptr;
161         int buflen;
162
163         /* Send command */
164         va_start(args, fmt);
165         retc = lps_send_va(serial, fmt, args);
166         va_end(args);
167
168         if (retc != SR_OK)
169                 return SR_ERR;
170
171         /* Read reply */
172         buf[0] = '\0';
173         bufptr = buf;
174         buflen = sizeof(buf);
175         retc = lps_read_reply(serial, &bufptr, &buflen);
176         if ((retc == SR_OK) && (buflen == 0))
177                 return SR_OK;
178
179         return SR_ERR;
180 }
181
182 /**
183  * Send command and read reply string.
184  * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
185  */
186 SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...)
187 {
188         int retc;
189         va_list args;
190         char buf[LINELEN_MAX];
191         char *bufptr;
192         int buflen;
193
194         reply[0] = '\0';
195
196         /* Send command */
197         va_start(args, fmt);
198         retc = lps_send_va(serial, fmt, args);
199         va_end(args);
200
201         if (retc != SR_OK)
202                 return SR_ERR;
203
204         /* Read reply */
205         buf[0] = '\0';
206         bufptr = buf;
207         buflen = sizeof(buf);
208         retc = lps_read_reply(serial, &bufptr, &buflen);
209         if ((retc == SR_OK) && (buflen > 0)) {
210                 strcpy(reply, buf);
211                 return SR_OK;
212         }
213
214         return SR_ERR;
215 }
216
217 /** Process integer value returned by STATUS command. */
218 SR_PRIV int lps_process_status(struct sr_dev_inst *sdi, int stat)
219 {
220         struct dev_context *devc;
221         int tracking_mode;
222
223         devc = sdi->priv;
224
225         sr_spew("Status: %d", stat);
226         devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
227         sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV");
228         if (devc->model->num_channels > 1) {
229                 devc->channel_status[1].cc_mode = (stat & 0x02) != 0;
230                 sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV");
231
232                 tracking_mode = (stat & 0x0c) >> 2;
233                 switch (tracking_mode) {
234                 case 0: devc->tracking_mode = 0;
235                         break;
236                 case 2: devc->tracking_mode = 1;
237                         break;
238                 case 3: devc->tracking_mode = 2;
239                         break;
240                 default:
241                         sr_err("Illegal channel tracking mode %d!", tracking_mode);
242                         devc->tracking_mode = 0;
243                         break;
244                 }
245
246                 sr_spew("Channel tracking: %d", devc->tracking_mode);
247         }
248         devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE;
249         sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF");
250         if (devc->model->num_channels > 2) {
251                 devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE;
252                 devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5;
253                 sr_spew("Channel 3 output: %s, U=%02f V, overload=%d",
254                         devc->channel_status[2].output_enabled?"ON":"OFF",
255                         devc->channel_status[2].output_voltage_last,
256                         stat&0x080?1:0);
257         }
258         sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0);
259
260         return SR_OK;
261 }
262
263 /** Send STATUS commend and process status string. */
264 SR_PRIV int lps_query_status(struct sr_dev_inst *sdi)
265 {
266         char buf[LINELEN_MAX];
267         int stat, ret;
268         struct dev_context *devc;
269
270         devc = sdi->priv;
271
272         devc->req_sent_at = g_get_real_time();
273
274         if ((ret = lps_cmd_reply(buf, sdi->conn, "STATUS")) < 0) {
275                 sr_err("%s: Failed to read status: %s.", __func__,
276                         sr_strerror(ret));
277                 return SR_ERR;
278         }
279
280         if (sr_atoi(buf, &stat) != SR_OK)
281                 return SR_ERR;
282
283         return lps_process_status(sdi, stat);
284 }
285
286 static gint64 calc_timeout_ms(gint64 start_us)
287 {
288         gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000);
289
290         if (result < 0)
291                 return 0;
292
293         return result;
294 }
295
296 /**
297  * Read message into buf until "OK" received.
298  *
299  * @retval SR_OK Msg received; buf and buflen contain result, if any except OK.
300  * @retval SR_ERR Error, including timeout.
301 */
302 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
303 {
304         int retries;
305         char buf2[LINELEN_MAX];
306         char *buf2ptr;
307         int buf2len;
308         gint64 timeout_start;
309
310         *buf[0] = '\0';
311
312         /* Read one line. It is either a data message or "OK". */
313         timeout_start = g_get_real_time();
314         buf2len = *buflen;
315         /* Up to 5 tries because serial_readline() will consume only one CR or LF per
316          * call, but device sends up to 4 in a row. */
317         for (retries = 0; retries < 5; retries++) {
318                 *buflen = buf2len;
319                 if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK)
320                         return SR_ERR;
321                 if (!strcmp(*buf, "OK")) { /* We got an OK! */
322                         *buf[0] = '\0';
323                         *buflen = 0;
324                         return SR_OK;
325                 }
326                 if (*buflen > 0) /* We got a msg! */
327                         break;
328         }
329
330         /* A data msg is in buf (possibly ERROR), need to consume "OK". */
331         buf2[0] = '\0';
332         buf2ptr = buf2;
333         for (retries = 0; retries < 5; retries++) {
334                 buf2len = sizeof(buf2);
335                 if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK)
336                         return SR_ERR;
337
338                 if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */
339                         if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */
340                                 sr_spew("ERROR found!");
341                                 *buf[0] = '\0';
342                                 *buflen = 0;
343                                 return SR_ERR;
344                         }
345                         return SR_OK;
346                 }
347         }
348
349         return SR_ERR; /* Timeout! */
350 }
351
352 /** Scan for LPS-300 series device. */
353 static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options)
354 {
355         struct sr_dev_inst *sdi;
356         struct dev_context *devc;
357         struct sr_serial_dev_inst *serial;
358         struct sr_channel *ch;
359         struct sr_channel_group *cg;
360         const char *conn, *serialcomm;
361         int cnt, ret;
362         gchar buf[LINELEN_MAX];
363         gchar channel[10];
364         char *verstr;
365
366         sdi = NULL;
367         devc = NULL;
368         conn = serialcomm = NULL;
369
370         /* Process and check options. */
371         if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
372                 return NULL;
373         if (!serialcomm)
374                 serialcomm = SERIALCOMM;
375
376         /* Init serial port. */
377         serial = sr_serial_dev_inst_new(conn, serialcomm);
378
379         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
380                 goto exit_err;
381
382         /* Query and verify model string. */
383         serial_flush(serial);
384         if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
385                 return NULL;
386
387         /* Check model string. */
388         if (strncmp(buf, "LPS-", 4)) {
389                 sr_spew("Unknown model code \"%s\"!", buf);
390                 return NULL;
391         }
392
393         /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */
394         if (modelid == LPS_UNKNOWN) {
395                 g_strstrip(buf);
396                 for (cnt = LPS_301; cnt <= LPS_305; cnt++) {
397                         if (!strcmp(buf, models[cnt].modelstr)) {
398                                 modelid = cnt;
399                                 break;
400                         }
401                 }
402                 if (modelid == LPS_UNKNOWN) {
403                         sr_err("Unable to detect model from model string '%s'!", buf);
404                         return NULL;
405                 }
406         }
407
408         /* Query version */
409         verstr = NULL;
410         if ((ret = lps_cmd_reply(buf, serial, "VERSION")) == SR_OK) {
411                 if (strncmp(buf, "Ver-", 4)) {
412                         sr_spew("Version string %s not recognized.", buf);
413                         goto exit_err;
414                 }
415
416                 g_strstrip(buf);
417                 verstr = buf + 4;
418         }
419         else /* Bug in device FW 1.17: Querying version string fails while output is active.
420                 Therefore just print an error message, but do not exit with error. */
421                 sr_err("Failed to query for hardware version: %s.",
422                         sr_strerror(ret));
423
424         sdi = g_malloc0(sizeof(struct sr_dev_inst));
425         sdi->status = SR_ST_INACTIVE;
426         sdi->vendor = g_strdup(VENDOR_MOTECH);
427         sdi->model = g_strdup(models[modelid].modelstr);
428         sdi->version = g_strdup(verstr);
429         sdi->inst_type = SR_INST_SERIAL;
430         sdi->conn = serial;
431
432         devc = g_malloc0(sizeof(struct dev_context));
433         sr_sw_limits_init(&devc->limits);
434         devc->model = &models[modelid];
435
436         sdi->priv = devc;
437
438         /* Setup channels and channel groups. */
439         for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
440                 snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
441                 ch = sr_channel_new(sdi, cnt, SR_CHANNEL_ANALOG, TRUE, channel);
442
443                 devc->channel_status[cnt].info = g_slist_append(NULL, ch);
444
445                 cg = g_malloc(sizeof(struct sr_channel_group));
446                 snprintf(channel, sizeof(channel), "CG%d", cnt+1);
447                 cg->name = g_strdup(channel);
448                 cg->priv = NULL;
449                 cg->channels = g_slist_append(NULL, ch);
450
451                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
452         }
453
454         /* Query status */
455         if (lps_query_status(sdi) != SR_OK)
456                 goto exit_err;
457
458         serial_close(serial);
459
460         return std_scan_complete(drv, g_slist_append(NULL, sdi));
461
462 exit_err:
463         sr_info("%s: Error!", __func__);
464
465         if (serial)
466                 serial_close(serial);
467         sr_serial_dev_inst_free(serial);
468         g_free(devc);
469         sr_dev_inst_free(sdi);
470
471         return NULL;
472 }
473
474 /** Scan for LPS-301 device. */
475 static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
476 {
477         return do_scan(LPS_301, di, options);
478 }
479
480 static void clear_helper(struct dev_context *devc)
481 {
482         int ch_idx;
483
484         /* Free channel_status.info (list only, data owned by sdi). */
485         for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
486                 g_slist_free(devc->channel_status[ch_idx].info);
487 }
488
489 static int dev_clear(const struct sr_dev_driver *di)
490 {
491         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
492 }
493
494 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
495                 const struct sr_channel_group *cg)
496 {
497         struct dev_context *devc;
498         struct sr_channel *ch;
499         int ch_idx;
500
501         if (!sdi)
502                 return SR_ERR_ARG;
503
504         devc = sdi->priv;
505
506         if (!cg) {
507                 switch (key) {
508                 case SR_CONF_LIMIT_SAMPLES:
509                 case SR_CONF_LIMIT_MSEC:
510                         return sr_sw_limits_config_get(&devc->limits, key, data);
511                 case SR_CONF_CHANNEL_CONFIG:
512                         *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
513                         break;
514                 default:
515                         return SR_ERR_NA;
516                 }
517         } else {
518                 /* We only ever have one channel per channel group in this driver. */
519                 ch = cg->channels->data;
520                 ch_idx = ch->index;
521
522                 switch (key) {
523                 case SR_CONF_VOLTAGE:
524                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
525                         break;
526                 case SR_CONF_VOLTAGE_TARGET:
527                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
528                         break;
529                 case SR_CONF_CURRENT:
530                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
531                         break;
532                 case SR_CONF_CURRENT_LIMIT:
533                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
534                         break;
535                 case SR_CONF_ENABLED:
536                         *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled);
537                         break;
538                 default:
539                         return SR_ERR_NA;
540                 }
541         }
542
543         return SR_OK;
544 }
545
546 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
547                 const struct sr_channel_group *cg)
548 {
549         struct dev_context *devc;
550         struct sr_channel *ch;
551         gdouble dval;
552         int ch_idx;
553         const char *sval;
554         gboolean bval;
555         int idx;
556         gboolean found;
557
558         devc = sdi->priv;
559
560         /* Cannot change settings while acquisition active, would cause a mess with commands.
561          * Changing this would be possible, but tricky. */
562         if (devc->acq_running)
563                 return SR_ERR_NA;
564
565         if (!cg) {
566                 switch (key) {
567                 case SR_CONF_LIMIT_MSEC:
568                 case SR_CONF_LIMIT_SAMPLES:
569                         return sr_sw_limits_config_set(&devc->limits, key, data);
570                 case SR_CONF_CHANNEL_CONFIG:
571                         sval = g_variant_get_string(data, NULL);
572                         found = FALSE;
573                         for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++) {
574                                 if (!strcmp(sval, channel_modes[idx])) {
575                                         found = TRUE;
576                                         if (devc->tracking_mode == idx)
577                                                 break;  /* Nothing to do! */
578                                         devc->tracking_mode = idx;
579                                         if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */
580                                                 return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode);
581                                 }
582                                 if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */
583                                         break;
584                         }
585                         if (!found)
586                                 return SR_ERR_ARG;
587                         break;
588                 default:
589                         return SR_ERR_NA;
590                 }
591         } else {
592                 /* We only ever have one channel per channel group in this driver. */
593                 ch = cg->channels->data;
594                 ch_idx = ch->index;
595
596                 switch (key) {
597                 case SR_CONF_VOLTAGE_TARGET:
598                         dval = g_variant_get_double(data);
599                         if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
600                                 return SR_ERR_ARG;
601                         if (ch_idx == 2) {
602                                 if (devc->model->modelid < LPS_304)
603                                         return SR_ERR_ARG;
604
605                                 if (fabs(dval - 5.000) <= 0.001)
606                                         dval = 5.0;
607                                 else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001))
608                                         dval = 3.3;
609                                 else return SR_ERR_ARG;
610                         }
611
612                         devc->channel_status[ch_idx].output_voltage_max = dval;
613                         if (ch_idx == 2)
614                                 return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval));
615                         else
616                                 return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
617                         break;
618                 case SR_CONF_CURRENT_LIMIT:
619                         dval = g_variant_get_double(data);
620                         if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
621                                 return SR_ERR_ARG;
622                         if (ch_idx == 2) /* No current setting for CH3. */
623                                 return SR_ERR_NA;
624                         devc->channel_status[ch_idx].output_current_max = dval;
625                         return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval);
626                         break;
627                 case SR_CONF_ENABLED:
628                         bval = g_variant_get_boolean(data);
629                         if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */
630                                 break;
631                         devc->channel_status[ch_idx].output_enabled = bval;
632                         if (ch_idx != 2) { /* Channels 1,2 can be set only together. */
633                                 devc->channel_status[ch_idx^1].output_enabled = bval;
634                                 return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval);
635                         } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */
636                                 if (bval)
637                                         return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max);
638                                 else
639                                         return lps_cmd_ok(sdi->conn, "VDD0");
640                         }
641                         break;
642                 default:
643                         return SR_ERR_NA;
644                 }
645         }
646
647         return SR_OK;
648 }
649
650 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
651                 const struct sr_channel_group *cg)
652 {
653         struct dev_context *devc;
654         struct sr_channel *ch;
655         int ch_idx;
656
657         devc = (sdi) ? sdi->priv : NULL;
658
659         if (!cg) {
660                 switch (key) {
661                 case SR_CONF_SCAN_OPTIONS:
662                 case SR_CONF_DEVICE_OPTIONS:
663                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
664                 case SR_CONF_CHANNEL_CONFIG:
665                         if (devc->model->modelid <= LPS_303) {
666                                 /* The 1-channel models. */
667                                 *data = g_variant_new_strv(channel_modes, 1);
668                         } else {
669                                 /* The other models support all modes. */
670                                 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
671                         }
672                         return SR_OK;
673                 default:
674                         return SR_ERR_NA;
675                 }
676         }
677
678         /* We only ever have one channel per channel group in this driver. */
679         ch = cg->channels->data;
680         ch_idx = ch->index;
681
682         switch (key) {
683         case SR_CONF_DEVICE_OPTIONS:
684                 if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
685                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
686                                 devopts_cg_ch12, ARRAY_SIZE(devopts_cg_ch12), sizeof(uint32_t));
687                 else /* Must be CH3 */
688                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
689                                 devopts_cg_ch3, ARRAY_SIZE(devopts_cg_ch3), sizeof(uint32_t));
690                 break;
691         case SR_CONF_VOLTAGE_TARGET:
692                 *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].voltage);
693                 break;
694         case SR_CONF_CURRENT_LIMIT:
695                 *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].current);
696                 break;
697         default:
698                 return SR_ERR_NA;
699         }
700
701         return SR_OK;
702 }
703
704 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
705 {
706         struct dev_context *devc;
707         struct sr_serial_dev_inst *serial;
708
709         devc = sdi->priv;
710
711         devc->acq_running = TRUE;
712
713         serial = sdi->conn;
714         serial_source_add(sdi->session, serial, G_IO_IN, 50,
715                         motech_lps_30x_receive_data, (void *)sdi);
716         std_session_send_df_header(sdi);
717
718         sr_sw_limits_acquisition_start(&devc->limits);
719
720         devc->acq_req = AQ_NONE;
721         /* Do not start polling device here, the read function will do it in 50 ms. */
722
723         return SR_OK;
724 }
725
726 static struct sr_dev_driver motech_lps_301_driver_info = {
727         .name = "motech-lps-301",
728         .longname = "Motech LPS-301",
729         .api_version = 1,
730         .init = std_init,
731         .cleanup = std_cleanup,
732         .scan = scan_lps301,
733         .dev_list = std_dev_list,
734         .dev_clear = dev_clear,
735         .config_get = config_get,
736         .config_set = config_set,
737         .config_list = config_list,
738         .dev_open = std_serial_dev_open,
739         .dev_close = std_serial_dev_close,
740         .dev_acquisition_start = dev_acquisition_start,
741         .dev_acquisition_stop = std_serial_dev_acquisition_stop,
742         .context = NULL,
743 };
744 SR_REGISTER_DEV_DRIVER(motech_lps_301_driver_info);