From 4e8e5ccf11e7c0750d5ddbe24ebb0f1cdbbc2738 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Thu, 6 Oct 2016 21:26:31 +0100 Subject: colorhug: Add v2 commands for reading and writing the SRAM --- lib/colorhug/ch-device.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++ lib/colorhug/ch-device.h | 10 ++++ 2 files changed, 153 insertions(+) diff --git a/lib/colorhug/ch-device.c b/lib/colorhug/ch-device.c index 3aa1410..80d6498 100644 --- a/lib/colorhug/ch-device.c +++ b/lib/colorhug/ch-device.c @@ -2555,3 +2555,146 @@ ch_device_load_sram (GUsbDevice *device, /* success */ return TRUE; } + +/** + * ch_device_write_sram: + * @device: A #GUsbDevice + * @data: A #GBytes + * @cancellable: a #GCancellable, or %NULL + * @error: a #GError, or %NULL + * + * Loads the entire SRAM from the device EEPROM. + * + * Returns: %TRUE for success + * + * Since: 1.3.4 + **/ +gboolean +ch_device_write_sram (GUsbDevice *device, + guint16 addr, + GBytes *data, + GCancellable *cancellable, + GError **error) +{ + const guint8 *dataptr; + gboolean ret; + gsize len; + + if (ch_device_get_protocol_ver (device) != 2) { + g_set_error_literal (error, + CH_DEVICE_ERROR, + CH_ERROR_NOT_IMPLEMENTED, + "writing SRAM not supported"); + return FALSE; + } + + /* write */ + dataptr = g_bytes_get_data (data, &len); + if (len > CH_EP0_TRANSFER_SIZE_V2) { + g_set_error_literal (error, + CH_DEVICE_ERROR, + CH_ERROR_NOT_IMPLEMENTED, + "data blob too large"); + return FALSE; + } + ret = g_usb_device_control_transfer (device, + G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE, + G_USB_DEVICE_REQUEST_TYPE_CLASS, + G_USB_DEVICE_RECIPIENT_INTERFACE, + CH_CMD_WRITE_SRAM, + addr, /* wValue */ + CH_USB_INTERFACE, /* idx */ + dataptr, /* data */ + len, /* length */ + NULL, /* actual_length */ + CH_DEVICE_USB_TIMEOUT, + cancellable, + error); + if (!ret) + return FALSE; + + /* check status */ + if (!ch_device_check_status (device, cancellable, error)) + return FALSE; + + /* success */ + return TRUE; +} + +/** + * ch_device_read_sram: + * @device: A #GUsbDevice + * @cancellable: a #GCancellable, or %NULL + * @error: a #GError, or %NULL + * + * Reads a value from the SRAM. + * + * Returns: %TRUE for success + * + * Since: 1.3.4 + **/ +GBytes * +ch_device_read_sram (GUsbDevice *device, + guint16 addr, + guint16 len, + GCancellable *cancellable, + GError **error) +{ + gboolean ret; + guint8 buf[CH_EP0_TRANSFER_SIZE_V2]; + gsize actual_length; + + if (ch_device_get_protocol_ver (device) != 2) { + g_set_error_literal (error, + CH_DEVICE_ERROR, + CH_ERROR_NOT_IMPLEMENTED, + "reading SRAM not supported"); + return NULL; + } + + /* too big */ + if (len > CH_EP0_TRANSFER_SIZE_V2) { + g_set_error_literal (error, + CH_DEVICE_ERROR, + CH_ERROR_NOT_IMPLEMENTED, + "length too large"); + return NULL; + } + + /* clear the buffer */ + memset(buf, 0xbe, sizeof(buf)); + + /* read SRAM */ + ret = g_usb_device_control_transfer (device, + G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST, + G_USB_DEVICE_REQUEST_TYPE_CLASS, + G_USB_DEVICE_RECIPIENT_INTERFACE, + CH_CMD_READ_SRAM, + addr, /* wValue */ + CH_USB_INTERFACE, /* idx */ + buf, /* data */ + len, /* length */ + &actual_length, /* actual_length */ + CH_DEVICE_USB_TIMEOUT, + cancellable, + error); + if (!ret) + return NULL; + + /* check size returned */ + if (actual_length != len) { + g_set_error (error, + G_USB_DEVICE_ERROR, + G_USB_DEVICE_ERROR_IO, + "failed to get SRAM data, got %" G_GSIZE_FORMAT, + actual_length); + return NULL; + } + + /* check status */ + if (!ch_device_check_status (device, cancellable, error)) + return NULL; + + /* success */ + return g_bytes_new (buf, len); +} diff --git a/lib/colorhug/ch-device.h b/lib/colorhug/ch-device.h index 2acabbb..3d0ffbb 100644 --- a/lib/colorhug/ch-device.h +++ b/lib/colorhug/ch-device.h @@ -182,6 +182,16 @@ gboolean ch_device_load_sram (GUsbDevice *device, gboolean ch_device_save_sram (GUsbDevice *device, GCancellable *cancellable, GError **error); +GBytes *ch_device_read_sram (GUsbDevice *device, + guint16 addr, + guint16 len, + GCancellable *cancellable, + GError **error); +gboolean ch_device_write_sram (GUsbDevice *device, + guint16 addr, + GBytes *data, + GCancellable *cancellable, + GError **error); G_END_DECLS -- cgit v1.2.1