Root/tools/atspi-id/atspi-id.c

Source at commit 96b6a50b3a1f9f889e1a59631bc17836511aaf06 created 8 years 11 months ago.
By Werner Almesberger, The Great ATSPI Renaming, part 2: rename ATSPI_* identifiers to ATUSB_*
1/*
2 * atspi-id/atspi-id.c - Identify a ben-wpan AT86RF230 board
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
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
13
14#include <stdlib.h>
15#include <stdio.h>
16
17#ifdef HAVE_USB
18#include <usb.h>
19#endif
20
21#include "at86rf230.h"
22#include "atusb/ep0.h"
23#include "atspi.h"
24
25
26#ifdef HAVE_USB
27
28#define FROM_DEV ATUSB_FROM_DEV(0)
29
30#define BUF_SIZE 256
31
32
33static int get_id(usb_dev_handle *dev, void *data, int size)
34{
35    int res;
36
37    res = usb_control_msg(dev, FROM_DEV, ATUSB_ID, 0, 0, data, size, 1000);
38    if (res < 0)
39        fprintf(stderr, "ATUSB_ID: %s\n", usb_strerror());
40    return res;
41}
42
43
44static int get_protocol(usb_dev_handle *dev,
45    uint8_t *major, uint8_t *minor, uint8_t *target)
46{
47    uint8_t ids[3];
48
49    if (get_id(dev, ids, 3) < 0)
50        return -1;
51    if (major)
52        *major = ids[0];
53    if (minor)
54        *minor = ids[1];
55    if (target)
56        *target = ids[2];
57
58    return 0;
59}
60 
61
62static int get_build(usb_dev_handle *dev, char *buf, size_t size)
63{
64    int res;
65
66    res = usb_control_msg(dev, FROM_DEV, ATUSB_BUILD, 0, 0, buf, size,
67        1000);
68    if (res < 0)
69        fprintf(stderr, "ATUSB_BUILD: %s\n", usb_strerror());
70    return res;
71}
72
73
74static void show_usb_info(struct atspi_dsc *dsc)
75{
76    usb_dev_handle *dev;
77    const struct usb_device *device;
78    uint8_t major, minor, target;
79    char buf[BUF_SIZE+1]; /* +1 for terminating \0 */
80    int len;
81
82    dev = atspi_usb_handle(dsc);
83    if (!dev)
84        return;
85    device = usb_device(dev);
86
87    printf("%04x:%04x ",
88        device->descriptor.idVendor, device->descriptor.idProduct);
89
90    if (get_protocol(dev, &major, &minor, &target) < 0)
91        exit(1);
92    printf("protocol %u.%u hw %u\n", major, minor, target);
93
94    len = get_build(dev, buf, sizeof(buf)-1);
95    if (len < 0)
96        exit(1);
97    buf[len] = 0;
98    printf("%10s%s\n", "", buf);
99}
100
101
102#else /* HAVE_USB */
103
104
105static void show_usb_info(struct atspi_dsc *dsc)
106{
107}
108
109
110#endif /* !HAVE_USB */
111
112
113static void show_info(struct atspi_dsc *dsc)
114{
115    uint8_t part, version, man_id_0, man_id_1;
116
117    show_usb_info(dsc);
118
119    part = atspi_reg_read(dsc, REG_PART_NUM);
120    version = atspi_reg_read(dsc, REG_VERSION_NUM);
121    man_id_0 = atspi_reg_read(dsc, REG_MAN_ID_0);
122    man_id_1 = atspi_reg_read(dsc, REG_MAN_ID_1);
123    printf("%10spart 0x%02x version %u manufacturer xxxx%02x%02x\n", "",
124        part, version, man_id_1, man_id_0);
125}
126
127
128static void usage(const char *name)
129{
130    fprintf(stderr, "usage: %s\n", name);
131    exit(1);
132}
133
134
135int main(int argc, const char **argv)
136{
137    struct atspi_dsc *dsc;
138
139    if (argc != 1)
140        usage(*argv);
141    dsc = atspi_open();
142    if (!dsc)
143        return 1;
144
145    show_info(dsc);
146
147    atspi_close(dsc);
148
149    return 0;
150}
151

Archive Download this file



interactive