From ae538d85339bc2f3a9193c6b732d6c627a1de430 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Thu, 2 Sep 2010 04:39:45 +0000 Subject: [PATCH] Reduce the need to accesss struct usb_device by providing functions to access the product, manufacturer and serial strings. Submitted by: Hans Petter Selasky --- sys/dev/usb/net/uhso.c | 13 ++++++------- sys/dev/usb/serial/ubser.c | 3 +-- sys/dev/usb/storage/umass.c | 19 +++++++++---------- sys/dev/usb/usb_device.c | 35 ++++++++++++++++++++++++++++------- sys/dev/usb/usb_generic.c | 6 +++--- sys/dev/usb/usb_hub.c | 3 ++- sys/dev/usb/usbdi_util.h | 7 +++++++ 7 files changed, 56 insertions(+), 30 deletions(-) diff --git a/sys/dev/usb/net/uhso.c b/sys/dev/usb/net/uhso.c index e0a9bb36ae24..c25917722d2e 100644 --- a/sys/dev/usb/net/uhso.c +++ b/sys/dev/usb/net/uhso.c @@ -62,13 +62,11 @@ __FBSDID("$FreeBSD$"); #define USB_DEBUG_VAR uhso_debug #include #include -#include #include -#include -#include -#include #include +#include + struct uhso_tty { struct uhso_softc *ht_sc; struct usb_xfer *ht_xfer[3]; @@ -513,7 +511,7 @@ uhso_probe(device_t self) return (ENXIO); if (uaa->info.bConfigIndex != 0) return (ENXIO); - if (uaa->device->ddesc.bDeviceClass != 0xff) + if (uaa->info.bDeviceClass != 0xff) return (ENXIO); error = usbd_lookup_id_by_uaa(uhso_devs, sizeof(uhso_devs), uaa); @@ -603,8 +601,9 @@ uhso_attach(device_t self) /* Announce device */ device_printf(self, "<%s port> at <%s %s> on %s\n", uhso_port_type[UHSO_IFACE_PORT_TYPE(sc->sc_type)], - uaa->device->manufacturer, uaa->device->product, - device_get_nameunit(uaa->device->bus->bdev)); + usb_get_manufacturer(uaa->device), + usb_get_product(uaa->device), + device_get_nameunit(device_get_parent(self))); if (sc->sc_ttys > 0) { SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "ports", diff --git a/sys/dev/usb/serial/ubser.c b/sys/dev/usb/serial/ubser.c index be332c3df5fb..7119177e9901 100644 --- a/sys/dev/usb/serial/ubser.c +++ b/sys/dev/usb/serial/ubser.c @@ -104,7 +104,6 @@ __FBSDID("$FreeBSD$"); #define USB_DEBUG_VAR ubser_debug #include #include -#include #include @@ -226,7 +225,7 @@ ubser_probe(device_t dev) return (ENXIO); } /* check if this is a BWCT vendor specific ubser interface */ - if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) && + if ((strcmp(usb_get_manufacturer(uaa->device), "BWCT") == 0) && (uaa->info.bInterfaceClass == 0xff) && (uaa->info.bInterfaceSubClass == 0x00)) return (0); diff --git a/sys/dev/usb/storage/umass.c b/sys/dev/usb/storage/umass.c index 567584a5b923..5455917987c9 100644 --- a/sys/dev/usb/storage/umass.c +++ b/sys/dev/usb/storage/umass.c @@ -124,7 +124,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include +#include #include "usbdevs.h" #include @@ -2304,23 +2304,24 @@ umass_cam_action(struct cam_sim *sim, union ccb *ccb) if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) { if (sc->sc_transfer.cmd_data[0] == INQUIRY) { + const char *pserial; + + pserial = usb_get_serial(sc->sc_udev); /* * Umass devices don't generally report their serial numbers * in the usual SCSI way. Emulate it here. */ if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) && - sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER && - sc->sc_udev != NULL && - sc->sc_udev->serial != NULL && - sc->sc_udev->serial[0] != '\0') { + (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) && + (pserial[0] != '\0')) { struct scsi_vpd_unit_serial_number *vpd_serial; vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr; - vpd_serial->length = strlen(sc->sc_udev->serial); + vpd_serial->length = strlen(pserial); if (vpd_serial->length > sizeof(vpd_serial->serial_num)) vpd_serial->length = sizeof(vpd_serial->serial_num); - memcpy(vpd_serial->serial_num, sc->sc_udev->serial, vpd_serial->length); + memcpy(vpd_serial->serial_num, pserial, vpd_serial->length); ccb->csio.scsi_status = SCSI_STATUS_OK; ccb->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); @@ -2553,9 +2554,7 @@ umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue, sc->sc_transfer.cmd_data[0] == INQUIRY && (sc->sc_transfer.cmd_data[1] & SI_EVPD) && sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST && - sc->sc_udev != NULL && - sc->sc_udev->serial != NULL && - sc->sc_udev->serial[0] != '\0') { + (usb_get_serial(sc->sc_udev)[0] != '\0')) { struct ccb_scsiio *csio; struct scsi_vpd_supported_page_list *page_list; diff --git a/sys/dev/usb/usb_device.c b/sys/dev/usb/usb_device.c index 41ed16bc2827..fcbd2c026230 100644 --- a/sys/dev/usb/usb_device.c +++ b/sys/dev/usb/usb_device.c @@ -136,6 +136,24 @@ usb_statestr(enum usb_dev_state state) return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); } +const char * +usb_get_manufacturer(struct usb_device *udev) +{ + return (udev->manufacturer ? udev->manufacturer : "Unknown"); +} + +const char * +usb_get_product(struct usb_device *udev) +{ + return (udev->product ? udev->product : ""); +} + +const char * +usb_get_serial(struct usb_device *udev) +{ + return (udev->serial ? udev->serial : ""); +} + /*------------------------------------------------------------------------* * usbd_get_ep_by_addr * @@ -1833,7 +1851,8 @@ usb_alloc_device(device_t parent_dev, struct usb_bus *bus, udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); /* Announce device */ - printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer, + printf("%s: <%s> at %s\n", udev->ugen_name, + usb_get_manufacturer(udev), device_get_nameunit(udev->bus->bdev)); usb_notify_addq("ATTACH", udev); @@ -1985,7 +2004,7 @@ usb_free_device(struct usb_device *udev, uint8_t flag) usb_notify_addq("DETACH", udev); printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, - udev->manufacturer, device_get_nameunit(bus->bdev)); + usb_get_manufacturer(udev), device_get_nameunit(bus->bdev)); /* Destroy UGEN symlink, if any */ if (udev->ugen_symlink) { @@ -2150,7 +2169,8 @@ usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) if (udd->bDeviceClass != 0xFF) { snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" "%x.%02x, addr %d", - udev->manufacturer, udev->product, + usb_get_manufacturer(udev), + usb_get_product(udev), udd->bDeviceClass, udd->bDeviceSubClass, (bcdUSB >> 8), bcdUSB & 0xFF, (bcdDevice >> 8), bcdDevice & 0xFF, @@ -2158,7 +2178,8 @@ usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) } else { snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" "%x.%02x, addr %d", - udev->manufacturer, udev->product, + usb_get_manufacturer(udev), + usb_get_product(udev), (bcdUSB >> 8), bcdUSB & 0xFF, (bcdDevice >> 8), bcdDevice & 0xFF, udev->address); @@ -2397,7 +2418,7 @@ usb_notify_addq_compat(const char *type, struct usb_device *udev) UGETW(udev->ddesc.idProduct), udev->ddesc.bDeviceClass, udev->ddesc.bDeviceSubClass, - udev->serial, + usb_get_serial(udev), UGETW(udev->ddesc.bcdDevice), udev->port_no, udev->parent_hub != NULL ? @@ -2437,7 +2458,7 @@ usb_notify_addq(const char *type, struct usb_device *udev) UGETW(udev->ddesc.idProduct), udev->ddesc.bDeviceClass, udev->ddesc.bDeviceSubClass, - udev->serial, + usb_get_serial(udev), UGETW(udev->ddesc.bcdDevice), (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", udev->port_no, @@ -2476,7 +2497,7 @@ usb_notify_addq(const char *type, struct usb_device *udev) UGETW(udev->ddesc.idProduct), udev->ddesc.bDeviceClass, udev->ddesc.bDeviceSubClass, - udev->serial, + usb_get_serial(udev), UGETW(udev->ddesc.bcdDevice), (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", iface->idesc->bInterfaceNumber, diff --git a/sys/dev/usb/usb_generic.c b/sys/dev/usb/usb_generic.c index d98aba4c0825..0cac19bd70df 100644 --- a/sys/dev/usb/usb_generic.c +++ b/sys/dev/usb/usb_generic.c @@ -825,9 +825,9 @@ usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di) di->udi_bus = device_get_unit(udev->bus->bdev); di->udi_addr = udev->address; di->udi_index = udev->device_index; - strlcpy(di->udi_serial, udev->serial, sizeof(di->udi_serial)); - strlcpy(di->udi_vendor, udev->manufacturer, sizeof(di->udi_vendor)); - strlcpy(di->udi_product, udev->product, sizeof(di->udi_product)); + strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial)); + strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor)); + strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product)); usb_printbcd(di->udi_release, sizeof(di->udi_release), UGETW(udev->ddesc.bcdDevice)); di->udi_vendorNo = UGETW(udev->ddesc.idVendor); diff --git a/sys/dev/usb/usb_hub.c b/sys/dev/usb/usb_hub.c index d42b5a4c7f9b..87c86d29e5a5 100644 --- a/sys/dev/usb/usb_hub.c +++ b/sys/dev/usb/usb_hub.c @@ -53,6 +53,7 @@ #include #include #include +#include #define USB_DEBUG_VAR uhub_debug @@ -1059,7 +1060,7 @@ uhub_child_pnpinfo_string(device_t parent, device_t child, UGETW(res.udev->ddesc.idProduct), res.udev->ddesc.bDeviceClass, res.udev->ddesc.bDeviceSubClass, - res.udev->serial, + usb_get_serial(res.udev), UGETW(res.udev->ddesc.bcdDevice), iface->idesc->bInterfaceClass, iface->idesc->bInterfaceSubClass); diff --git a/sys/dev/usb/usbdi_util.h b/sys/dev/usb/usbdi_util.h index bb6e324ab97e..352f3ff1c716 100644 --- a/sys/dev/usb/usbdi_util.h +++ b/sys/dev/usb/usbdi_util.h @@ -78,4 +78,11 @@ usb_error_t usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx, usb_error_t usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id); + +/* The following functions will not return NULL strings. */ + +const char *usb_get_manufacturer(struct usb_device *); +const char *usb_get_product(struct usb_device *); +const char *usb_get_serial(struct usb_device *); + #endif /* _USB_USBDI_UTIL_H_ */