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