diff options
Diffstat (limited to 'sys/dev/usb')
-rw-r--r-- | sys/dev/usb/uthum.c | 111 |
1 files changed, 50 insertions, 61 deletions
diff --git a/sys/dev/usb/uthum.c b/sys/dev/usb/uthum.c index d77fdaee4dc..e344196fe3a 100644 --- a/sys/dev/usb/uthum.c +++ b/sys/dev/usb/uthum.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthum.c,v 1.3 2009/11/29 17:32:06 deraadt Exp $ */ +/* $OpenBSD: uthum.c,v 1.4 2009/12/10 04:44:27 deraadt Exp $ */ /* * Copyright (c) 2009 Yojiro UO <yuo@nui.org> @@ -40,8 +40,8 @@ #endif #ifdef UTHUM_DEBUG -int uthumdebug = 0; -#define DPRINTFN(n, x) do { if (uthumdebug > (n)) printf x; } while (0) +int uthumdebug = 0; +#define DPRINTFN(n, x) do { if (uthumdebug > (n)) printf x; } while (0) #else #define DPRINTFN(n, x) #endif @@ -52,10 +52,10 @@ int uthumdebug = 0; /* TEMPerHUM */ #define CMD_DEVTYPE 0x52 /* XXX */ #define CMD_GETDATA 0x48 /* XXX */ -static uint8_t cmd_start[8] = - {0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00}; +static uint8_t cmd_start[8] = + { 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x02, 0x00 }; static uint8_t cmd_end[8] = - {0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x01, 0x00}; + { 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x01, 0x00 }; /* sensors */ #define UTHUM_TEMP 0 @@ -82,7 +82,7 @@ struct uthum_softc { /* sensor framework */ struct ksensor sc_sensor[UTHUM_MAX_SENSORS]; - struct ksensordev sc_sensordev; + struct ksensordev sc_sensordev; struct sensor_task *sc_sensortask; uint8_t sc_num_sensors; @@ -91,14 +91,14 @@ struct uthum_softc { const struct usb_devno uthum_devs[] = { /* XXX: various TEMPer variants using same VID/PID */ - { USB_VENDOR_TENX, USB_PRODUCT_TENX_TEMPER}, + { USB_VENDOR_TENX, USB_PRODUCT_TENX_TEMPER}, }; #define uthum_lookup(v, p) usb_lookup(uthum_devs, v, p) -int uthum_match(struct device *, void *, void *); -void uthum_attach(struct device *, struct device *, void *); -int uthum_detach(struct device *, int); -int uthum_activate(struct device *, int); +int uthum_match(struct device *, void *, void *); +void uthum_attach(struct device *, struct device *, void *); +int uthum_detach(struct device *, int); +int uthum_activate(struct device *, int); int uthum_read_data(struct uthum_softc *, uint8_t, uint8_t *, size_t, int); int uthum_check_sensortype(struct uthum_softc *); @@ -108,17 +108,16 @@ int uthum_sht1x_rh(unsigned int, int); void uthum_intr(struct uhidev *, void *, u_int); void uthum_refresh(void *); +struct cfdriver uthum_cd = { + NULL, "uthum", DV_DULL +}; -struct cfdriver uthum_cd = { - NULL, "uthum", DV_DULL -}; - -const struct cfattach uthum_ca = { - sizeof(struct uthum_softc), - uthum_match, - uthum_attach, - uthum_detach, - uthum_activate, +const struct cfattach uthum_ca = { + sizeof(struct uthum_softc), + uthum_match, + uthum_attach, + uthum_detach, + uthum_activate, }; int @@ -127,17 +126,16 @@ uthum_match(struct device *parent, void *match, void *aux) struct usb_attach_arg *uaa = aux; struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; - if (uthum_lookup(uha->uaa->vendor, uha->uaa->product) == NULL) { + if (uthum_lookup(uha->uaa->vendor, uha->uaa->product) == NULL) return UMATCH_NONE; - } -#if 0 /* attach only sensor part of HID as uthum* */ +#if 0 /* attach only sensor part of HID as uthum* */ #define HUG_UNKNOWN_3 0x0003 void *desc; int size; uhidev_get_report_desc(uha->parent, &desc, &size); if (!hid_is_collection(desc, size, uha->reportid, - HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_UNKNOWN_3))) + HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_UNKNOWN_3))) return (UMATCH_NONE); #undef HUG_UNKNOWN_3 #endif @@ -168,7 +166,7 @@ uthum_attach(struct device *parent, struct device *self, void *aux) sc->sc_flen = hid_report_size(desc, size, hid_feature, repid); usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, - &sc->sc_hdev.sc_dev); + &sc->sc_hdev.sc_dev); printf("\n"); if (sc->sc_flen < 32) { @@ -180,18 +178,18 @@ uthum_attach(struct device *parent, struct device *self, void *aux) /* attach sensor */ strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, - sizeof(sc->sc_sensordev.xname)); + sizeof(sc->sc_sensordev.xname)); switch (sc->sc_sensortype) { case UTHUM_TYPE_SHT1x: strlcpy(sc->sc_sensor[UTHUM_TEMP].desc, "temp", - sizeof(sc->sc_sensor[UTHUM_TEMP].desc)); + sizeof(sc->sc_sensor[UTHUM_TEMP].desc)); sc->sc_sensor[UTHUM_TEMP].type = SENSOR_TEMP; sc->sc_sensor[UTHUM_TEMP].status = SENSOR_S_UNSPEC; sc->sc_sensor[UTHUM_TEMP].flags = SENSOR_FINVALID; strlcpy(sc->sc_sensor[UTHUM_HUMIDITY].desc, "humidity", - sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc)); + sizeof(sc->sc_sensor[UTHUM_HUMIDITY].desc)); sc->sc_sensor[UTHUM_HUMIDITY].type = SENSOR_PERCENT; sc->sc_sensor[UTHUM_HUMIDITY].value = 0; sc->sc_sensor[UTHUM_HUMIDITY].status = SENSOR_S_UNSPEC; @@ -231,15 +229,14 @@ uthum_detach(struct device *self, int flags) if (sc->sc_num_sensors > 0) { wakeup(&sc->sc_sensortask); sensordev_deinstall(&sc->sc_sensordev); - for (i = 0; i < sc->sc_num_sensors; i++) { + for (i = 0; i < sc->sc_num_sensors; i++) sensor_detach(&sc->sc_sensordev, &sc->sc_sensor[i]); - } if (sc->sc_sensortask != NULL) sensor_task_unregister(sc->sc_sensortask); } usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, - &sc->sc_hdev.sc_dev); + &sc->sc_hdev.sc_dev); return (rv); } @@ -272,56 +269,49 @@ uthum_intr(struct uhidev *addr, void *ibuf, u_int len) } int -uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf, +uthum_read_data(struct uthum_softc *sc, uint8_t target_cmd, uint8_t *buf, size_t len, int delay) { int i; uint8_t cmdbuf[32], report[256]; /* if return buffer is null, do nothing */ - if ((buf == NULL) || len == 0) { + if ((buf == NULL) || len == 0) return 0; - } /* issue query */ bzero(cmdbuf, sizeof(cmdbuf)); memcpy(cmdbuf, cmd_start, sizeof(cmd_start)); - if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, - cmdbuf, sc->sc_olen)) { + if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, + cmdbuf, sc->sc_olen)) return EIO; - } + bzero(cmdbuf, sizeof(cmdbuf)); cmdbuf[0] = target_cmd; - if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, - cmdbuf, sc->sc_olen)) { + if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, + cmdbuf, sc->sc_olen)) return EIO; - } + bzero(cmdbuf, sizeof(cmdbuf)); for (i = 0; i < 7; i++) { - if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, - cmdbuf, sc->sc_olen)) { + if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, + cmdbuf, sc->sc_olen)) return EIO; - } } memcpy(cmdbuf, cmd_end, sizeof(cmd_end)); - if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, - cmdbuf, sc->sc_olen)) { + if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, + cmdbuf, sc->sc_olen)) return EIO; - } /* wait if required */ - if (delay > 0) { + if (delay > 0) tsleep(&sc->sc_sensortask, 0, "uthum", (delay*hz+999)/1000 + 1); - } /* get answer */ if (uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT, - report, sc->sc_flen)) { + report, sc->sc_flen)) return EIO; - } - memcpy(buf, report, len); - return 0; } @@ -329,23 +319,22 @@ int uthum_check_sensortype(struct uthum_softc *sc) { uint8_t buf[8]; - static uint8_t sht1x_sig[] = - {0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00}; + static uint8_t sht1x_sig[] = + { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 }; if (uthum_read_data(sc, CMD_DEVTYPE, buf, sizeof(buf), 0) != 0) { DPRINTF(("uthum: read fail\n")); return UTHUM_TYPE_UNKNOWN; } - /* + /* * currently we have not enough information about the return value, * therefore, compare full bytes. * TEMPerHUM HID (SHT1x version) will return: - * {0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00} + * { 0x57, 0x5a, 0x13, 0x00, 0x14, 0x00, 0x53, 0x00 } */ - if (memcmp(buf, sht1x_sig, sizeof(sht1x_sig))) { + if (memcmp(buf, sht1x_sig, sizeof(sht1x_sig))) return UTHUM_TYPE_SHT1x; - } return UTHUM_TYPE_UNKNOWN; } @@ -399,6 +388,6 @@ uthum_sht1x_rh(unsigned int ticks, int temp) int rh_l, rh; rh_l = (-40000 + 405 * ticks) - ((7 * ticks * ticks) / 250); - rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10; + rh = ((temp - 2500) * (1 + (ticks >> 7)) + rh_l) / 10; return rh; } |