Skip to contentSkip to conversation

usb-gotemp: USB thermometer emulation

PATCH In review Scott Tsai 2 participants 5 messages Nov 11, 2009 Kbuild

lore
Share & tools
lore

Scott Tsai

author
#0 lore raw

v3 cover usb-gotemp: USB thermometer emulation

I reworked the second patch in this series to add generic monitor commands to change the temperature reported from thermometers.
Thermometer devices can now include "sensor.h" and call 'qemu_add_therm_temp_handler' to register themselves.

I went with separate 'therm_set DEVICE_INDEX' and 'therm_temp TEMPERATURE' commands since a 'therm_temp' command that only requires one argument seems easier to use on the monitor command line and doesn't require searching the list of thermometers repeatedly.

To cater to my original "driver tutorial" use case,
by default the temperature would still automatically increment unless the 'controlled_by_monitor' qdev property is set.
Even when auto incrementing the temperature is now always bounded between 25C ~ 40C. (previously the temperature would increment until int16_t overflows)

author
#1 lore raw

PATCH v3 1/3 usb: move HID request defines to hw/usb.h

Move USB HID request constants from hw/usb-hid.c to hw/usb.h to allow other modules to use them.

---
 hw/usb-hid.c |   20 ++++++--------------
 hw/usb.h     |    8 ++++++++
 2 files changed, 14 insertions(+), 14 deletions(-)
hw/usb-hid.c +6 -14
index f4a2a48..e263fc0 100644
@@ -26,14 +26,6 @@
 #include "console.h"
 #include "usb.h"
 
-/* HID interface requests */
-#define GET_REPORT   0xa101
-#define GET_IDLE     0xa102
-#define GET_PROTOCOL 0xa103
-#define SET_REPORT   0x2109
-#define SET_IDLE     0x210a
-#define SET_PROTOCOL 0x210b
-
 /* HID descriptor types */
 #define USB_DT_HID    0x21
 #define USB_DT_REPORT 0x22
@@ -763,7 +755,7 @@ static int usb_hid_handle_control(USBDevice *dev, int request, int value,
             goto fail;
         }
         break;
-    case GET_REPORT:
+    case USB_REQ_HID_GET_REPORT:
 	if (s->kind == USB_MOUSE)
             ret = usb_mouse_poll(s, data, length);
 	else if (s->kind == USB_TABLET)
@@ -771,29 +763,29 @@ static int usb_hid_handle_control(USBDevice *dev, int request, int value,
         else if (s->kind == USB_KEYBOARD)
             ret = usb_keyboard_poll(&s->kbd, data, length);
         break;
-    case SET_REPORT:
+    case USB_REQ_HID_SET_REPORT:
         if (s->kind == USB_KEYBOARD)
             ret = usb_keyboard_write(&s->kbd, data, length);
         else
             goto fail;
         break;
-    case GET_PROTOCOL:
+    case USB_REQ_HID_GET_PROTOCOL:
         if (s->kind != USB_KEYBOARD)
             goto fail;
         ret = 1;
         data[0] = s->protocol;
         break;
-    case SET_PROTOCOL:
+    case USB_REQ_HID_SET_PROTOCOL:
         if (s->kind != USB_KEYBOARD)
             goto fail;
         ret = 0;
         s->protocol = value;
         break;
-    case GET_IDLE:
+    case USB_REQ_HID_GET_IDLE:
         ret = 1;
         data[0] = s->idle;
         break;
-    case SET_IDLE:
+    case USB_REQ_HID_SET_IDLE:
         s->idle = (uint8_t) (value >> 8);
         ret = 0;
         break;
hw/usb.h +8 -0
index 351c466..7d46931 100644
@@ -104,6 +104,14 @@
 #define USB_REQ_SET_INTERFACE		0x0B
 #define USB_REQ_SYNCH_FRAME		0x0C
 
+/* HID interface requests */
+#define USB_REQ_HID_GET_REPORT   0xa101
+#define USB_REQ_HID_GET_IDLE     0xa102
+#define USB_REQ_HID_GET_PROTOCOL 0xa103
+#define USB_REQ_HID_SET_REPORT   0x2109
+#define USB_REQ_HID_SET_IDLE     0x210a
+#define USB_REQ_HID_SET_PROTOCOL 0x210b
+
 #define USB_DEVICE_SELF_POWERED		0
 #define USB_DEVICE_REMOTE_WAKEUP	1
 
Signature · 1 line
1.6.5.2
author
#2 lore raw

PATCH v3 2/3 usb-gotemp: new module emulating a USB thermometer

Emulate the Vernier Go!Temp USB thermometer
(see: http://www.vernier.com/go/gotemp.html)
used in Greg Kroah-Hartman's "Write a Real, Working, Linux Driver" talk.

The emulation is complete enough for gregkh's sample driver and using the vendor supplied SDK through the in-kernel 'ldusb' module under Linux. No testing have yet been done with the vendor's fancier Windows software.

If the qdev property 'controlled_by_monitor' is _NOT_ set on the thermometer, such as through the command line option '-usbdevice thermometer:controlled_by_monitor', the temperature would increment on each report but is bound between 25C
~ 40C.

Added new monitor commands:
info thermometers
therm_set INDEX
therm_temp TEMPERATURE
modeled after 'info mice', 'mouse_set' and 'mouse_move'.

---
 Makefile        |    2 +-
 hw/usb-gotemp.c |  762 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 monitor.c       |   98 +++++++
 qemu-monitor.hx |   35 +++
 sensor.h        |   20 ++
 5 files changed, 916 insertions(+), 1 deletions(-)
 create mode 100644 hw/usb-gotemp.c
 create mode 100644 sensor.h
Makefile +1 -1
index 30f1c9d..54b8968 100644
@@ -124,7 +124,7 @@ obj-y += i2c.o smbus.o smbus_eeprom.o
 obj-y += eeprom93xx.o
 obj-y += scsi-disk.o cdrom.o
 obj-y += scsi-generic.o scsi-bus.o
-obj-y += usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o
+obj-y += usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o usb-gotemp.o
 obj-y += usb-serial.o usb-net.o usb-bus.o
 obj-$(CONFIG_SSI) += ssi.o
 obj-$(CONFIG_SSI_SD) += ssi-sd.o
hw/usb-gotemp.c new file +762 -0

762 lines not on this page · raw message

monitor.c +98 -0

116 lines not on this page · raw message

qemu-monitor.hx +35 -0
index bb01c14..2f987f7 100644
@@ -111,6 +111,8 @@ show migration status
 show balloon information
 @item info qtree
 show device tree
+@item info thermometers
+show available thermometers
 @end table
 ETEXI
 
@@ -1040,6 +1042,39 @@ Close the file descriptor previously assigned to @var{fdname} using the
 used by another monitor command.
 ETEXI
 
+    {
+        .name       = "therm_set",
+        .args_type  = "index:i",
+        .params     = "index",
+        .help       = "set which thermometer device is affected by therm_temp",
+        .mhandler.cmd_new = do_therm_set,
+    },
+
+STEXI
+@item therm_set @var{index}
+Set which thermometer device is affected by @code{therm_temp}, index
+can be obtained with
+@example
+info thermometer
+@end example
+ETEXI
+
+    {
+        .name       = "therm_temp",
+        .args_type  = "temperature_str:s",
+        .params     = "temperature",
+        .help       = "set thermometer temperature",
+        .mhandler.cmd_new = do_therm_temp,
+    },
+
+STEXI
+@item therm_temp @var{temperature}
+Set the temperature of the thermometer selected with @code{therm_set}.
+@example
+therm_temp 0F
+therm_temp -17.8c
+@end example
+ETEXI
 STEXI
 @end table
 ETEXI
sensor.h new file +20 -0
new file mode 100644
index 0000000..45024c2
@@ -0,0 +1,20 @@
+#ifndef SENSOR_H
+#define SENSOR_H
+
+#include "qemu-queue.h"
+
+/* thermometers */
+typedef void (ThermTempHandler)(void *opaque, double temperature_celsius);
+
+struct ThermTempEntry {
+    ThermTempHandler *cb;
+    void *opaque;
+    char *name;
+    QCIRCLEQ_ENTRY(ThermTempEntry) entries;
+};
+typedef struct ThermTempEntry ThermTempEntry;
+
+ThermTempEntry *qemu_add_therm_temp_handler(ThermTempHandler *cb, void *opaque, const char *name);
+void qemu_del_therm_temp_handler(ThermTempEntry *e);
+
+#endif
Signature · 1 line
1.6.5.2
author
#3 lore raw

PATCH v3 3/3 Documentation: -usbdevice thermometer option

Adds documentation for the '-usbdevice thermometer' option.

---
 qemu-options.hx |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
qemu-options.hx +3 -0
index b65fd74..c2e3b6c 100644
@@ -396,6 +396,9 @@ or fake device.
 @item net:@var{options}
 Network adapter that supports CDC ethernet and RNDIS protocols.
 
+@item thermometer
+Vernier Go!Temp compatible thermometer.
+
 @end table
 ETEXI
 
Signature · 1 line
1.6.5.2
#4 lore raw
In reply to Scott Tsai
Quoted 4 lines · Scott Tsai

I reworked the second patch in this series to add generic monitor commands to change the temperature reported from thermometers. Thermometer devices can now include "sensor.h" and call
'qemu_add_therm_temp_handler' to register themselves.

This is just another HID device. As discussed previously in the context of the wacom tablet, duplicating this code is bad (IMO usb-wacom.c should ever have been committed). Instead you should be sharing the existing code.

Paul