summaryrefslogtreecommitdiff
path: root/sys/dev/usb
diff options
context:
space:
mode:
authorYojiro Uo <yuo@cvs.openbsd.org>2009-12-18 04:16:15 +0000
committerYojiro Uo <yuo@cvs.openbsd.org>2009-12-18 04:16:15 +0000
commit3e608abda35c5eb9fa8e920abe29120467fbd933 (patch)
tree23a60703f12a20d1fab67555abca00fca7afd418 /sys/dev/usb
parent2e96db5908826467ab8b980003e4c281aa1c3928 (diff)
add utrh(4): USBRH temperature / humidity sensor driver
ok deraadt@
Diffstat (limited to 'sys/dev/usb')
-rw-r--r--sys/dev/usb/files.usb7
-rw-r--r--sys/dev/usb/usbdevs6
-rw-r--r--sys/dev/usb/utrh.c284
3 files changed, 295 insertions, 2 deletions
diff --git a/sys/dev/usb/files.usb b/sys/dev/usb/files.usb
index 31638f380b0..baa4dccad25 100644
--- a/sys/dev/usb/files.usb
+++ b/sys/dev/usb/files.usb
@@ -1,4 +1,4 @@
-# $OpenBSD: files.usb,v 1.86 2009/12/09 21:27:19 matthieu Exp $
+# $OpenBSD: files.usb,v 1.87 2009/12/18 04:16:14 yuo Exp $
# $NetBSD: files.usb,v 1.16 2000/02/14 20:29:54 augustss Exp $
#
# Config file and device description for machine-independent USB code.
@@ -154,6 +154,11 @@ device uthum: hid
attach uthum at uhidbus
file dev/usb/uthum.c uthum
+# Strawberry Linux USBRH
+device utrh: hid
+attach utrh at uhidbus
+file dev/usb/utrh.c utrh
+
# Ethernet adapters
# ADMtek AN986 Pegasus
device aue: ether, ifnet, mii, ifmedia
diff --git a/sys/dev/usb/usbdevs b/sys/dev/usb/usbdevs
index de8397244fc..2ddacf5fd08 100644
--- a/sys/dev/usb/usbdevs
+++ b/sys/dev/usb/usbdevs
@@ -1,4 +1,4 @@
-$OpenBSD: usbdevs,v 1.464 2009/12/17 09:12:06 mpf Exp $
+$OpenBSD: usbdevs,v 1.465 2009/12/18 04:16:14 yuo Exp $
/* $NetBSD: usbdevs,v 1.322 2003/05/10 17:47:14 hamajima Exp $ */
/*
@@ -523,6 +523,7 @@ vendor AXESSTEL 0x1726 Axesstel
vendor LINKSYS4 0x1737 Linksys
vendor SENAO 0x1740 Senao
vendor ASUS2 0x1761 ASUS
+vendor STRAWBERRYLINUX 0x1774 Strawberry linux Co., Ltd.
vendor SWEEX2 0x177f Sweex
vendor MECANIQUE 0x1781 Mecanique
vendor DISPLAYLINK 0x17e9 DisplayLink
@@ -2971,6 +2972,9 @@ product STARTECH ICUSB232X 0x3410 ICUSB2321X/2X/4X
product STMICRO BIOMETRIC_COPR 0x2016 Biometric Coprocessor
product STMICRO COMMUNICATOR 0x7554 Communicator
+/* Strawberry Linux products */
+product STRAWBERRYLINUX USBRH 0x1001 USBRH sensor
+
/* STSN products */
product STSN STSN0001 0x0001 Internet Access Device
diff --git a/sys/dev/usb/utrh.c b/sys/dev/usb/utrh.c
new file mode 100644
index 00000000000..234f589ea6c
--- /dev/null
+++ b/sys/dev/usb/utrh.c
@@ -0,0 +1,284 @@
+/* $OpenBSD: utrh.c,v 1.1 2009/12/18 04:16:14 yuo Exp $ */
+
+/*
+ * Copyright (c) 2009 Yojiro UO <yuo@nui.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* Driver for Strawberry linux USBRH Temerature/Humidity sensor */
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+#include <sys/sensors.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbhid.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/uhidev.h>
+#include <dev/usb/hid.h>
+
+#ifdef USB_DEBUG
+#define UTRH_DEBUG
+#endif
+
+#ifdef UTRH_DEBUG
+int utrhdebug = 0;
+#define DPRINTFN(n, x) do { if (utrhdebug > (n)) printf x; } while (0)
+#else
+#define DPRINTFN(n, x)
+#endif
+
+#define DPRINTF(x) DPRINTFN(0, x)
+
+/* sensors */
+#define UTRH_TEMP 0
+#define UTRH_HUMIDITY 1
+#define UTRH_MAX_SENSORS 2
+
+struct utrh_softc {
+ struct uhidev sc_hdev;
+ usbd_device_handle sc_udev;
+ u_char sc_dying;
+ uint16_t sc_flag;
+
+ /* uhidev parameters */
+ size_t sc_flen; /* feature report length */
+ size_t sc_ilen; /* input report length */
+ size_t sc_olen; /* output report length */
+
+ uint8_t *sc_ibuf;
+
+ /* sensor framework */
+ struct ksensor sc_sensor[UTRH_MAX_SENSORS];
+ struct ksensordev sc_sensordev;
+ struct sensor_task *sc_sensortask;
+
+ uint8_t sc_num_sensors;
+};
+
+const struct usb_devno utrh_devs[] = {
+ { USB_VENDOR_STRAWBERRYLINUX, USB_PRODUCT_STRAWBERRYLINUX_USBRH},
+};
+#define utrh_lookup(v, p) usb_lookup(utrh_devs, v, p)
+
+int utrh_match(struct device *, void *, void *);
+void utrh_attach(struct device *, struct device *, void *);
+int utrh_detach(struct device *, int);
+int utrh_activate(struct device *, int);
+
+int utrh_sht1x_temp(unsigned int);
+int utrh_sht1x_rh(unsigned int, int);
+
+void utrh_intr(struct uhidev *, void *, u_int);
+void utrh_refresh(void *);
+
+struct cfdriver utrh_cd = {
+ NULL, "utrh", DV_DULL
+};
+
+const struct cfattach utrh_ca = {
+ sizeof(struct utrh_softc),
+ utrh_match,
+ utrh_attach,
+ utrh_detach,
+ utrh_activate,
+};
+
+int
+utrh_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 (utrh_lookup(uha->uaa->vendor, uha->uaa->product) == NULL)
+ return UMATCH_NONE;
+
+ return (UMATCH_VENDOR_PRODUCT);
+}
+
+void
+utrh_attach(struct device *parent, struct device *self, void *aux)
+{
+ struct utrh_softc *sc = (struct utrh_softc *)self;
+ struct usb_attach_arg *uaa = aux;
+ struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
+ usbd_device_handle dev = uha->parent->sc_udev;
+ int size, repid, err;
+ void *desc;
+
+ sc->sc_udev = dev;
+ sc->sc_hdev.sc_intr = utrh_intr;
+ sc->sc_hdev.sc_parent = uha->parent;
+ sc->sc_hdev.sc_report_id = uha->reportid;
+ sc->sc_num_sensors = 0;
+
+ uhidev_get_report_desc(uha->parent, &desc, &size);
+ repid = uha->reportid;
+ sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
+ sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
+ sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
+
+ err = uhidev_open(&sc->sc_hdev);
+ if (err) {
+ printf("utrh_open: uhidev_open %d\n", err);
+ return;
+ }
+ sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
+
+ usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
+ &sc->sc_hdev.sc_dev);
+ printf("\n");
+
+ /* attach sensor */
+ strlcpy(sc->sc_sensordev.xname, sc->sc_hdev.sc_dev.dv_xname,
+ sizeof(sc->sc_sensordev.xname));
+
+ strlcpy(sc->sc_sensor[UTRH_TEMP].desc, "temp",
+ sizeof(sc->sc_sensor[UTRH_TEMP].desc));
+ sc->sc_sensor[UTRH_TEMP].type = SENSOR_TEMP;
+ sc->sc_sensor[UTRH_TEMP].status = SENSOR_S_UNSPEC;
+ sc->sc_sensor[UTRH_TEMP].flags = SENSOR_FINVALID;
+
+ strlcpy(sc->sc_sensor[UTRH_HUMIDITY].desc, "humidity",
+ sizeof(sc->sc_sensor[UTRH_HUMIDITY].desc));
+ sc->sc_sensor[UTRH_HUMIDITY].type = SENSOR_PERCENT;
+ sc->sc_sensor[UTRH_HUMIDITY].value = 0;
+ sc->sc_sensor[UTRH_HUMIDITY].status = SENSOR_S_UNSPEC;
+ sc->sc_sensor[UTRH_HUMIDITY].flags = SENSOR_FINVALID;
+
+ sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[UTRH_TEMP]);
+ sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[UTRH_HUMIDITY]);
+ sc->sc_num_sensors = 2;
+
+ if (sc->sc_num_sensors > 0) {
+ sc->sc_sensortask = sensor_task_register(sc, utrh_refresh, 6);
+ if (sc->sc_sensortask == NULL) {
+ printf(", unable to register update task\n");
+ return;
+ }
+ sensordev_install(&sc->sc_sensordev);
+ }
+
+ DPRINTF(("utrh_attach: complete\n"));
+}
+
+int
+utrh_detach(struct device *self, int flags)
+{
+ struct utrh_softc *sc = (struct utrh_softc *)self;
+ int i, rv = 0;
+
+ sc->sc_dying = 1;
+
+ if (sc->sc_num_sensors > 0) {
+ wakeup(&sc->sc_sensortask);
+ sensordev_deinstall(&sc->sc_sensordev);
+ 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);
+ }
+
+ if (sc->sc_ibuf != NULL) {
+ free(sc->sc_ibuf, M_USBDEV);
+ sc->sc_ibuf = NULL;
+ }
+
+ usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
+ &sc->sc_hdev.sc_dev);
+
+ return (rv);
+}
+
+int
+utrh_activate(struct device *self, int act)
+{
+ struct utrh_softc *sc = (struct utrh_softc *)self;
+
+ switch (act) {
+ case DVACT_ACTIVATE:
+ break;
+
+ case DVACT_DEACTIVATE:
+ sc->sc_dying = 1;
+ break;
+ }
+ return (0);
+}
+
+void
+utrh_intr(struct uhidev *addr, void *ibuf, u_int len)
+{
+ struct utrh_softc *sc = (struct utrh_softc *)addr;
+
+ if (sc->sc_ibuf == NULL)
+ return;
+
+ /* receive sensor data */
+ memcpy(sc->sc_ibuf, ibuf, len);
+ return;
+}
+
+void
+utrh_refresh(void *arg)
+{
+ struct utrh_softc *sc = arg;
+ unsigned int temp_tick, humidity_tick;
+ int temp, rh;
+
+ /* issue query */
+ uint8_t cmdbuf[] = {0x31, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00};
+ if (uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
+ cmdbuf, sc->sc_olen))
+ return;
+
+ /* wait till sensor data are updated, 1000ms will be enough */
+ tsleep(&sc->sc_sensortask, 0, "utrh", 1000);
+
+ temp_tick = (sc->sc_ibuf[2] * 256 + sc->sc_ibuf[3]) & 0x3fff;
+ humidity_tick = (sc->sc_ibuf[0] * 256 + sc->sc_ibuf[1]) & 0x0fff;
+
+ temp = utrh_sht1x_temp(temp_tick);
+ rh = utrh_sht1x_rh(humidity_tick, temp);
+
+ sc->sc_sensor[UTRH_TEMP].value = (temp * 10000) + 273150000;
+ sc->sc_sensor[UTRH_TEMP].flags &= ~SENSOR_FINVALID;
+ sc->sc_sensor[UTRH_HUMIDITY].value = rh;
+ sc->sc_sensor[UTRH_HUMIDITY].flags &= ~SENSOR_FINVALID;
+}
+
+/* return C-degree * 100 value */
+int
+utrh_sht1x_temp(unsigned int ticks)
+{
+ return (ticks - 4010);
+}
+
+/* return %RH * 1000 */
+int
+utrh_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;
+ return rh;
+}