We need to keep track of the number of slots, so add a new field for
that.

ok?

Index: usb_subr.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/usb_subr.c,v
retrieving revision 1.141
diff -u -p -r1.141 usb_subr.c
--- usb_subr.c  16 Nov 2018 11:56:42 -0000      1.141
+++ usb_subr.c  16 Nov 2018 19:48:01 -0000
@@ -894,6 +894,7 @@ usbd_probe_and_attach(struct device *par
                        err = USBD_NOMEM;
                        goto fail;
                }
+               dev->nsubdev = 2;
                dev->subdevs[dev->ndevs++] = dv;
                dev->subdevs[dev->ndevs] = 0;
                err = USBD_NORMAL_COMPLETION;
@@ -936,6 +937,7 @@ usbd_probe_and_attach(struct device *par
                /* add 1 for possible ugen and 1 for NULL terminator */
                dev->subdevs = mallocarray(nifaces + 2, sizeof(dv), M_USB,
                    M_NOWAIT | M_ZERO);
+               dev->nsubdev = nifaces + 2;
                if (dev->subdevs == NULL) {
                        free(ifaces, M_USB, nifaces * sizeof(*ifaces));
                        err = USBD_NOMEM;
@@ -967,8 +969,9 @@ usbd_probe_and_attach(struct device *par
                                goto fail;
                }
 
-               free(dev->subdevs, M_USB, (nifaces + 2) * sizeof(dv));
+               free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs));
                dev->subdevs = NULL;
+               dev->nsubdev = 0;
        }
        /* No interfaces were attached in any of the configurations. */
 
@@ -993,6 +996,7 @@ generic:
                                goto fail;
                        }
                }
+               dev->nsubdev = 2;
                dev->subdevs[dev->ndevs++] = dv;
                dev->subdevs[dev->ndevs] = 0;
                err = USBD_NORMAL_COMPLETION;
@@ -1410,8 +1414,7 @@ usb_free_device(struct usbd_device *dev)
        }
        if (dev->cdesc != NULL)
                free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength));
-       if (dev->subdevs != NULL)
-               free(dev->subdevs, M_USB, 0);
+       free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs));
        dev->bus->devices[dev->address] = NULL;
 
        if (dev->vendor != NULL)
Index: usbdivar.h
===================================================================
RCS file: /cvs/src/sys/dev/usb/usbdivar.h,v
retrieving revision 1.76
diff -u -p -r1.76 usbdivar.h
--- usbdivar.h  10 Nov 2018 15:34:25 -0000      1.76
+++ usbdivar.h  16 Nov 2018 19:48:26 -0000
@@ -158,6 +158,7 @@ struct usbd_device {
        const struct usbd_quirks     *quirks;  /* device quirks, always set */
        struct usbd_hub        *hub;           /* only if this is a hub */
        struct device         **subdevs;       /* sub-devices, 0 terminated */
+       int                     nsubdev;       /* size of the `subdevs' array */
        int                     ndevs;         /* # of subdevs */
 
        char                   *serial;        /* serial number, can be NULL */

Reply via email to