1 /* $OpenBSD: uoak.h,v 1.5 2022/04/09 20:09:03 naddy Exp $ */ 2 3 /* 4 * Copyright (c) 2012 Yojiro UO <yuo@nui.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* TORADEX OAK series sensors */ 20 /* http://developer.toradex.com/files/toradex-dev/uploads/media/Oak/Oak_ProgrammingGuide.pdf */ 21 22 /* feature request direction */ 23 #define OAK_SET 0x0 24 #define OAK_GET 0x1 25 26 /* specification */ 27 #define OAK_V_MAXSENSORS 8 28 29 /* OAK sensor command */ 30 /* 1 byte commands */ 31 #define OAK_CMD_REPORTMODE 0x0000 32 #define OAK_REPORTMODE_AFTERSAMPLING 0x0 /* default */ 33 #define OAK_REPORTMODE_AFTERCHANGE 0x1 34 #define OAK_REPORTMODE_FIXEDRATE 0x2 35 #define OAK_CMD_LEDMODE 0x0001 36 #define OAK_CMD_SENSORSETTING 0x0002 37 /* RH */ 38 #define OAK_RH_SENSOR_HEATER_MASK (0x1 << 3) 39 #define OAK_RH_SENSOR_RES_MASK (0x1 << 0) 40 #define OAK_RH_SENSOR_HEATER_OFF 0x0 /* default */ 41 #define OAK_RH_SENSOR_HEATER_ON 0x1 42 #define OAK_RH_SENSOR_HIGHRES 0x0 /* default */ 43 #define OAK_RH_SENSOR_LOWRES 0x1 44 /* LUX */ 45 #define OAK_LUX_SENSOR_GAIN_MASK (0x1 << 4) 46 #define OAK_LUX_SENSOR_LOWGAIN 0x0 /* default */ 47 #define OAK_LUX_SENSOR_HIGHGAIN 0x1 48 #define OAK_LUX_SENSOR_INTTIME_MASK 0x3 49 #define OAK_LUX_SENSOR_INTTIME_13_7ms 0x0 /* 13.7ms */ 50 #define OAK_LUX_SENSOR_INTTIME_101ms 0x1 /* 101 ms */ 51 #define OAK_LUX_SENSOR_INTTIME_402ms 0x2 /* 402 ms (default) */ 52 /* 10V */ 53 #define OAK_V_SENSOR_INPUTMODEMASK (0x1 << 0) 54 #define OAK_V_SENSOR_SINGLEENDED 0x0 /* default */ 55 #define OAK_V_SENSOR_DIFFERENTIAL 0x1 56 57 /* 2 bytes commands */ 58 #define OAK_CMD_REPORTRATE 0x0000 59 #define OAK_CMD_SAMPLERATE 0x0001 60 61 /* 21 bytes (0x15) commands */ 62 #define OAK_CMD_DEVNAME 0x0000 63 #define OAK_CMD_CHANNAME0 0x0001 64 #define OAK_CMD_CHANNAME1 0x0002 65 #define OAK_CMD_CHANNAME2 0x0003 66 #define OAK_CMD_CHANNAME3 0x0004 67 #define OAK_CMD_CHANNAME4 0x0005 68 #define OAK_CMD_CHANNAME5 0x0006 69 #define OAK_CMD_CHANNAME6 0x0007 70 #define OAK_CMD_CHANNAME7 0x0008 71 #define OAK_CMD_CHANNAME8 0x0009 72 73 /* OAK LED command */ 74 #define OAK_LED_OFF 0x0 75 #define OAK_LED_ON 0x1 76 #define OAK_LED_BLINK_SLOW 0x2 77 #define OAK_LED_BLINK_FAST 0x3 78 #define OAK_LED_BLINK_PULSES 0x4 79 80 /* OAK config storage targets */ 81 enum uoak_target { 82 OAK_TARGET_RAM, 83 OAK_TARGET_FLASH, 84 OAK_TARGET_CPU, 85 OAK_TARGET_SENSOR, 86 OAK_TARGET_OTHER, 87 OAK_TARGET_MAXTYPES 88 }; 89 90 #define OAK_RH_TARGET_MAX 2 91 #define OAK_V_TARGET_MAX 2 92 #define OAK_LUX_TARGET_MAX 2 93 94 struct uoak_rcmd { 95 uint8_t dir; 96 uint8_t target; 97 uint8_t datasize; 98 uint16_t cmd; 99 uint8_t val[26]; 100 } __packed; 101 102 struct uoak_config { 103 char devname[24]; 104 int report_mode; 105 int report_rate; 106 int sample_rate; 107 }; 108 109 struct uoak_methods { 110 void (*dev_print)(void *parent, enum uoak_target target); 111 void (*dev_setting)(void *parent, enum uoak_target target); 112 }; 113 114 struct uoak_softc { 115 struct uhidev *sc_hdev; 116 void *sc_parent; 117 struct ksensordev *sc_sensordev; 118 struct usbd_device *sc_udev; 119 uint16_t sc_flag; 120 struct usb_device_info sc_udi; 121 122 /* uhidev parameters */ 123 size_t sc_flen; /* feature report length */ 124 size_t sc_ilen; /* input report length */ 125 size_t sc_olen; /* output report length */ 126 uint8_t *sc_ibuf; 127 128 /* buffers */ 129 struct uoak_rcmd sc_rcmd; 130 uint8_t sc_buf[32]; 131 132 /* configurations */ 133 struct uoak_config sc_config[OAK_TARGET_MAXTYPES]; 134 135 /* device specific methods */ 136 const struct uoak_methods *sc_methods; 137 }; 138 139 140 struct uoak_sensor { 141 struct ksensor avg; 142 struct ksensor max; 143 struct ksensor min; 144 int64_t vavg, vmax, vmin; 145 unsigned int count; 146 }; 147 148 149 int uoak_check_device_ready(struct uoak_softc *); 150 int uoak_set_cmd(struct uoak_softc *); 151 int uoak_get_cmd(struct uoak_softc *); 152 153 int uoak_get_device_name(struct uoak_softc *, enum uoak_target); 154 int uoak_get_report_mode(struct uoak_softc *, enum uoak_target); 155 int uoak_get_report_rate(struct uoak_softc *, enum uoak_target); 156 int uoak_get_sample_rate(struct uoak_softc *, enum uoak_target); 157 int uoak_set_sample_rate(struct uoak_softc *, enum uoak_target, int); 158 159 int uoak_led_ctrl(struct uoak_softc *, enum uoak_target, uint8_t); 160 int uoak_led_status(struct uoak_softc *, enum uoak_target, uint8_t *); 161 162 void uoak_get_devinfo(struct uoak_softc *); 163 void uoak_get_setting(struct uoak_softc *, enum uoak_target); 164 void uoak_print_devinfo(struct uoak_softc *); 165 void uoak_print_setting(struct uoak_softc *, enum uoak_target); 166 167 void uoak_sensor_attach(struct uoak_softc *, struct uoak_sensor *, 168 enum sensor_type); 169 void uoak_sensor_detach(struct uoak_softc *, struct uoak_sensor *); 170 void uoak_sensor_update(struct uoak_sensor *, int); 171 void uoak_sensor_refresh(struct uoak_sensor *, int, int); 172