]> sigrok.org Git - libsigrok.git/blobdiff - hardware/link-mso19/link-mso19.c
MSO19: Always use glib's memory allocation functions.
[libsigrok.git] / hardware / link-mso19 / link-mso19.c
index b3ad93041e2022c2f7040f5cca25b98cf571f113..efafb0a314a876ca8cf02ce7b2dcb39363c9722a 100644 (file)
@@ -47,13 +47,34 @@ static int capabilities[] = {
 };
 
 static uint64_t supported_samplerates[] = {
-       100, 200, 500, KHZ(1), KHZ(2), KHZ(5), KHZ(10), KHZ(20),
-       KHZ(50), KHZ(100), KHZ(200), KHZ(500), MHZ(1), MHZ(2), MHZ(5),
-       MHZ(10), MHZ(20), MHZ(50), MHZ(100), MHZ(200), 0
+       SR_HZ(100),
+       SR_HZ(200),
+       SR_HZ(500),
+       SR_KHZ(1),
+       SR_KHZ(2),
+       SR_KHZ(5),
+       SR_KHZ(10),
+       SR_KHZ(20),
+       SR_KHZ(50),
+       SR_KHZ(100),
+       SR_KHZ(200),
+       SR_KHZ(500),
+       SR_MHZ(1),
+       SR_MHZ(2),
+       SR_MHZ(5),
+       SR_MHZ(10),
+       SR_MHZ(20),
+       SR_MHZ(50),
+       SR_MHZ(100),
+       SR_MHZ(200),
+       0,
 };
 
 static struct sr_samplerates samplerates = {
-       100, MHZ(200), 0, supported_samplerates,
+       SR_HZ(100),
+       SR_MHZ(200),
+       SR_HZ(0),
+       supported_samplerates,
 };
 
 static GSList *device_instances = NULL;
@@ -65,12 +86,16 @@ static int mso_send_control_message(struct sr_device_instance *sdi,
        int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
        char *p, *buf;
 
+       ret = SR_ERR;
+
        if (fd < 0)
                goto ret;
 
-       buf = malloc(s);
-       if (!buf)
+       if (!(buf = g_try_malloc(s))) {
+               sr_err("mso19: %s: buf malloc failed", __func__);
+               ret = SR_ERR_MALLOC;
                goto ret;
+       }
 
        p = buf;
        memcpy(p, mso_head, sizeof(mso_head));
@@ -93,7 +118,7 @@ static int mso_send_control_message(struct sr_device_instance *sdi,
        }
        ret = SR_OK;
 free:
-       free(buf);
+       g_free(buf);
 ret:
        return ret;
 }
@@ -283,7 +308,7 @@ static int mso_configure_trigger(struct sr_device_instance *sdi)
        ops[2] = mso_trans(3, dso_trigger & 0xff);
        ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
        ops[4] = mso_trans(11,
-                       mso->dso_trigger_width / HZ_TO_NS(mso->cur_rate));
+                       mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
        ops[5] = mso_trans(15, (2 | mso->slowmode));
 
        /* FIXME SPI/I2C Triggers */
@@ -347,7 +372,7 @@ static int mso_parse_serial(const char *iSerial, const char *iProduct,
        return SR_OK;
 }
 
-static int hw_init(char *deviceinfo)
+static int hw_init(const char *deviceinfo)
 {
        struct sr_device_instance *sdi;
        int devcnt = 0;
@@ -365,7 +390,7 @@ static int hw_init(char *deviceinfo)
         */
        udev = udev_new();
        if (!udev) {
-               g_warning("Failed to initialize udev.");
+               sr_warn("Failed to initialize udev.");
                goto ret;
        }
        enumerate = udev_enumerate_new(udev);
@@ -385,8 +410,8 @@ static int hw_init(char *deviceinfo)
                parent = udev_device_get_parent_with_subsystem_devtype(
                                dev, "usb", "usb_device");
                if (!parent) {
-                       g_warning("Unable to find parent usb device for %s",
-                                       sysname);
+                       sr_warn("Unable to find parent usb device for %s",
+                               sysname);
                        continue;
                }
 
@@ -404,7 +429,7 @@ static int hw_init(char *deviceinfo)
                s = strcspn(iProduct, " ");
                if (s > sizeof(product) ||
                                strlen(iProduct) - s > sizeof(manufacturer)) {
-                       g_warning("Could not parse iProduct: %s", iProduct);
+                       sr_warn("Could not parse iProduct: %s", iProduct);
                        continue;
                }
                strncpy(product, iProduct, s);
@@ -412,13 +437,13 @@ static int hw_init(char *deviceinfo)
                strcpy(manufacturer, iProduct + s);
                sprintf(hwrev, "r%d", mso->hwrev);
 
-               mso = malloc(sizeof(struct mso));
-               if (!mso)
-                       continue;
-               memset(mso, 0, sizeof(struct mso));
+               if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
+                       sr_err("mso19: %s: mso malloc failed", __func__);
+                       continue; /* TODO: Errors handled correctly? */
+               }
 
                if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
-                       g_warning("Invalid iSerial: %s", iSerial);
+                       sr_warn("Invalid iSerial: %s", iSerial);
                        goto err_free_mso;
                }
                /* hardware initial state */
@@ -427,8 +452,8 @@ static int hw_init(char *deviceinfo)
                sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
                        manufacturer, product, hwrev);
                if (!sdi) {
-                       g_warning("Unable to create device instance for %s",
-                                       sysname);
+                       sr_warn("Unable to create device instance for %s",
+                               sysname);
                        goto err_free_mso;
                }
 
@@ -497,14 +522,14 @@ static int hw_opendev(int device_index)
        /* FIXME: discard serial buffer */
 
        mso_check_trigger(sdi, &mso->trigger_state);
-//     g_warning("trigger state: %c", mso->trigger_state);
+//     sr_warn("trigger state: %c", mso->trigger_state);
 
        ret = mso_reset_adc(sdi);
        if (ret != SR_OK)
                return ret;
 
        mso_check_trigger(sdi, &mso->trigger_state);
-//     g_warning("trigger state: %c", mso->trigger_state);
+//     sr_warn("trigger state: %c", mso->trigger_state);
 
 //     ret = mso_reset_fsm(sdi);
 //     if (ret != SR_OK)
@@ -721,7 +746,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
                return ret;
 
        mso->session_id = session_device_id;
-       source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
+       sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
 
        packet.type = SR_DF_HEADER;
        packet.length = sizeof(struct sr_datafeed_header);