#include <stdio.h>
+#include <string.h>
#include <usb.h>
/*****************************************************************************/
-struct {
+#define CMD_ATM_READY 0x22
+
+#define CTL_DATA_BUS 0x55 /* refers to data bus controller */
+#define CTL_ADDR_BUS 0xAA /* .. address bus .. */
+
+typedef struct {
u8 magic[4];
u8 reserved[11];
u8 magic2;
union {
struct {
u8 which_device;
- u8 boot_cmd;
} dev_info;
struct {
u8 addrb2; /* most significant */
} times_write;
};
u8 pad[8];
-} dev_cmd;
+} dev_cmd_t;
+
+typedef struct {
+ u8 firmware_ver[4];
+ u8 bootloader_ver[4];
+ char names[56];
+} dev_info_t;
+
+static void prepare_cmd(dev_cmd_t *dev_cmd, u8 cmd)
+{
+ memset(dev_cmd, 0, sizeof(*dev_cmd));
+
+ memcpy(dev_cmd->magic, "USBC", 4);
+ dev_cmd->magic2 = 0x67; /* "MySCSICommand" */
+ dev_cmd->mx_cmd = cmd;
+}
+
+static int write_cmd(struct usb_dev_handle *dev, dev_cmd_t *cmd)
+{
+ int ret = usb_bulk_write(dev, 0x03, (char *)cmd, sizeof(*cmd), 2000);
+ if (ret < 0) {
+ fprintf(stderr, "failed to write:\n");
+ fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
+ } else if (ret != sizeof(*cmd))
+ printf("write_cmd: wrote only %d of %d bytes\n", ret, sizeof(*cmd));
+
+ return ret;
+}
+
+static int read_response(struct usb_dev_handle *dev, void *buff, int size)
+{
+ int ret = usb_bulk_read(dev, 0x82, buff, size, 2000);
+ if (ret < 0) {
+ fprintf(stderr, "failed to read:\n");
+ fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
+ } else if (ret != size)
+ printf("read_response: read only %d of %d bytes\n", ret, size);
+ return ret;
+}
static usb_dev_handle *get_device(void)
{
if (ret != 0) {
fprintf(stderr, "couldn't set configuration for /*/bus/usb/%s/%s:\n",
bus->dirname, dev->filename);
- fprintf(stderr, "%s\n", usb_strerror());
+ fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
return NULL;
}
return handle;
}
+static int read_info(struct usb_dev_handle *device, u8 ctl_id)
+{
+ dev_cmd_t cmd;
+ dev_info_t info;
+ int ret;
+
+ prepare_cmd(&cmd, CMD_ATM_READY);
+ cmd.dev_info.which_device = ctl_id;
+
+ ret = write_cmd(device, &cmd);
+ if (ret < 0)
+ return ret;
+
+ ret = read_response(device, &info, sizeof(info));
+ if (ret < 0)
+ return ret;
+
+ printf("firmware version: %X.%X.%X%c\n", info.firmware_ver[0],
+ info.firmware_ver[1], info.firmware_ver[2], info.firmware_ver[3]);
+ printf("bootloader version: %X.%X.%X%c\n", info.bootloader_ver[0],
+ info.bootloader_ver[1], info.bootloader_ver[2], info.bootloader_ver[3]);
+ info.names[sizeof(info.names) - 1] = 0;
+ printf("device name: %s\n", info.names);
+
+ return 0;
+}
+
static void release_device(struct usb_dev_handle *device)
{
usb_release_interface(device, 0);
int main(int argc, char *argv[])
{
struct usb_dev_handle *device;
+ int ret;
usb_init();
if (device == NULL)
return 1;
+ printf("data bus controller:\n");
+ ret = read_info(device, CTL_DATA_BUS);
+ if (ret < 0)
+ goto end;
+
+ printf("address bus controller:\n");
+ ret = read_info(device, CTL_ADDR_BUS);
+ if (ret < 0)
+ goto end;
+end:
release_device(device);
- return 0;
+ return ret;
}