| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2017 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <glib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "huey-device.h" |
| 28 |
|
| 29 |
/* device constants */ |
| 30 |
#define HUEY_DEVICE_TIMEOUT 30000 /* ms */ |
| 31 |
#define HUEY_EEPROM_SIZE 0x3ff /* bytes */ |
| 32 |
|
| 33 |
#define HUEY_CMD_GET_STATUS 0x00 |
| 34 |
#define HUEY_CMD_REGISTER_READ 0x08 |
| 35 |
#define HUEY_CMD_SAMPLE_BY_PULSES 0x04 |
| 36 |
#define HUEY_CMD_SAMPLE_BY_TIME 0x53 |
| 37 |
|
| 38 |
#define HUEY_RC_SUCCESS 0x00 |
| 39 |
#define HUEY_RC_ERROR 0x80 |
| 40 |
|
| 41 |
static gboolean |
| 42 |
huey_device_send_data (GUsbDevice *device, |
| 43 |
const guint8 *request, |
| 44 |
gsize request_len, |
| 45 |
guint8 *reply, |
| 46 |
gsize reply_len, |
| 47 |
gsize *reply_read, |
| 48 |
GError **error) |
| 49 |
{ |
| 50 |
gboolean ret; |
| 51 |
|
| 52 |
/* control transfer */ |
| 53 |
cd_buffer_debug (CD_BUFFER_KIND_REQUEST, |
| 54 |
request, request_len); |
| 55 |
ret = g_usb_device_control_transfer (device, |
| 56 |
G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE, |
| 57 |
G_USB_DEVICE_REQUEST_TYPE_CLASS, |
| 58 |
G_USB_DEVICE_RECIPIENT_INTERFACE, |
| 59 |
0x09, |
| 60 |
0x0200, |
| 61 |
0, |
| 62 |
(guint8 *) request, |
| 63 |
request_len, |
| 64 |
NULL, |
| 65 |
HUEY_DEVICE_TIMEOUT, |
| 66 |
NULL, |
| 67 |
error); |
| 68 |
if (!ret) |
| 69 |
return FALSE; |
| 70 |
|
| 71 |
/* get sync response */ |
| 72 |
ret = g_usb_device_interrupt_transfer (device, |
| 73 |
0x81, |
| 74 |
(guint8 *) reply, |
| 75 |
reply_len, |
| 76 |
reply_read, |
| 77 |
HUEY_DEVICE_TIMEOUT, |
| 78 |
NULL, |
| 79 |
error); |
| 80 |
if (!ret) |
| 81 |
return FALSE; |
| 82 |
|
| 83 |
/* the second byte seems to be the command again */ |
| 84 |
cd_buffer_debug (CD_BUFFER_KIND_RESPONSE, |
| 85 |
reply, *reply_read); |
| 86 |
if (reply[1] != request[0]) { |
| 87 |
g_set_error (error, |
| 88 |
G_IO_ERROR, |
| 89 |
G_IO_ERROR_FAILED, |
| 90 |
"wrong command reply, got 0x%02x, " |
| 91 |
"expected 0x%02x", |
| 92 |
reply[1], |
| 93 |
request[0]); |
| 94 |
return FALSE; |
| 95 |
} |
| 96 |
|
| 97 |
/* the first byte is status */ |
| 98 |
if (reply[0] != HUEY_RC_SUCCESS) { |
| 99 |
g_set_error (error, |
| 100 |
G_IO_ERROR, |
| 101 |
G_IO_ERROR_FAILED, |
| 102 |
"failed to issue command: %s", &reply[2]); |
| 103 |
return FALSE; |
| 104 |
} |
| 105 |
|
| 106 |
/* success */ |
| 107 |
return TRUE; |
| 108 |
} |
| 109 |
|
| 110 |
gchar * |
| 111 |
huey_device_get_status (GUsbDevice *device, GError **error) |
| 112 |
{ |
| 113 |
guint8 request[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 114 |
guint8 reply[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 115 |
gboolean ret; |
| 116 |
gsize reply_read; |
| 117 |
g_autoptr(GError) error_local = NULL; |
| 118 |
|
| 119 |
request[0] = HUEY_CMD_GET_STATUS; |
| 120 |
ret = huey_device_send_data (device, |
| 121 |
request, 8, |
| 122 |
reply, 8, |
| 123 |
&reply_read, |
| 124 |
&error_local); |
| 125 |
if (!ret) { |
| 126 |
if (!g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED)) { |
| 127 |
g_propagate_error (error, error_local); |
| 128 |
error_local = NULL; |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* for error the string is also set */ |
| 134 |
return g_strndup ((gchar *) reply + 2, 6); |
| 135 |
} |
| 136 |
|
| 137 |
GBytes * |
| 138 |
huey_device_read_eeprom (GUsbDevice *device, GError **error) |
| 139 |
{ |
| 140 |
guint8 request[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 141 |
guint8 reply[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 142 |
gboolean ret; |
| 143 |
gsize reply_read; |
| 144 |
g_autofree guint8 *eeprom = NULL; |
| 145 |
|
| 146 |
/* get entire memory space */ |
| 147 |
eeprom = g_malloc0 (HUEY_EEPROM_SIZE); |
| 148 |
request[0] = HUEY_CMD_REGISTER_READ; |
| 149 |
for (guint i = 0; i < HUEY_EEPROM_SIZE; i += 0x4) { |
| 150 |
guint16 addr_be = GUINT16_TO_BE (i); |
| 151 |
memcpy (&request[1], &addr_be, 0x2); |
| 152 |
ret = huey_device_send_data (device, |
| 153 |
request, 8, |
| 154 |
reply, 8, |
| 155 |
&reply_read, |
| 156 |
error); |
| 157 |
if (!ret) { |
| 158 |
g_prefix_error (error, "failed to read eeprom @0x%04x: ", i); |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
memcpy (eeprom + i, reply + 0x4, 0x4); |
| 162 |
} |
| 163 |
return g_bytes_new (eeprom, HUEY_EEPROM_SIZE); |
| 164 |
} |
| 165 |
|
| 166 |
gboolean |
| 167 |
huey_device_open (GUsbDevice *device, GError **error) |
| 168 |
{ |
| 169 |
if (!g_usb_device_open (device, error)) { |
| 170 |
g_prefix_error (error, "failed to open device: "); |
| 171 |
return FALSE; |
| 172 |
} |
| 173 |
if (!g_usb_device_set_configuration (device, 0x01, error)) { |
| 174 |
g_prefix_error (error, "failed to set config on device: "); |
| 175 |
return FALSE; |
| 176 |
} |
| 177 |
if (!g_usb_device_claim_interface (device, 0x00, |
| 178 |
G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER, error)) { |
| 179 |
g_prefix_error (error, "failed to claim interface for device: "); |
| 180 |
return FALSE; |
| 181 |
} |
| 182 |
return TRUE; |
| 183 |
} |
| 184 |
|
| 185 |
gboolean |
| 186 |
huey_device_take_sample (GUsbDevice *device, gdouble *val, GError **error) |
| 187 |
{ |
| 188 |
gboolean ret; |
| 189 |
gdouble val_approx; |
| 190 |
gdouble val_pulses; |
| 191 |
gsize reply_read; |
| 192 |
guint16 timeout_be = GUINT16_TO_BE (0x0062); |
| 193 |
guint32 val_be; |
| 194 |
guint8 request[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 195 |
guint8 reply[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; |
| 196 |
|
| 197 |
/* get approx reading so we know the number of pulses to count */ |
| 198 |
request[0] = HUEY_CMD_SAMPLE_BY_TIME; |
| 199 |
memcpy (&request[1], &timeout_be, 0x2); |
| 200 |
ret = huey_device_send_data (device, |
| 201 |
request, 8, |
| 202 |
reply, 8, |
| 203 |
&reply_read, |
| 204 |
error); |
| 205 |
if (!ret) |
| 206 |
return FALSE; |
| 207 |
memcpy (&val_be, reply + 2, 4); |
| 208 |
val_approx = (gdouble) GUINT32_FROM_BE (val_be); |
| 209 |
g_debug ("approximate reading=%.0f", val_approx); |
| 210 |
|
| 211 |
/* calculate the number of pulses we should look for */ |
| 212 |
val_approx *= 2.9f; |
| 213 |
timeout_be = GUINT16_TO_BE ((guint16) val_approx); |
| 214 |
|
| 215 |
/* get a precise reading by counting pulses */ |
| 216 |
request[0] = HUEY_CMD_SAMPLE_BY_PULSES; |
| 217 |
memcpy (&request[1], &timeout_be, 0x2); |
| 218 |
ret = huey_device_send_data (device, |
| 219 |
request, 8, |
| 220 |
reply, 8, |
| 221 |
&reply_read, |
| 222 |
error); |
| 223 |
if (!ret) |
| 224 |
return FALSE; |
| 225 |
|
| 226 |
memcpy (&val_be, reply + 2, 4); |
| 227 |
val_pulses = (gdouble) GUINT32_FROM_BE (val_be); |
| 228 |
g_debug ("number of pulses=%.0f", val_pulses); |
| 229 |
|
| 230 |
/* calculate luminance */ |
| 231 |
if (val != NULL) |
| 232 |
*val = (val_approx * 1000.f) / val_pulses; |
| 233 |
|
| 234 |
return TRUE; |
| 235 |
} |