C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Git Source Tree

Root/lib/usb.c

1/*
2 * lib/usb.c - Common USB code for F32Xbase tools
3 *
4 * Written 2008-2010 by Werner Almesberger
5 * Copyright 2008-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
15#include <stdint.h>
16#include <stdlib.h>
17#include <stdio.h>
18#include <usb.h>
19
20
21static uint16_t vendor = 0;
22static uint16_t product = 0;
23
24
25usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
26{
27    const struct usb_bus *bus;
28    struct usb_device *dev;
29    usb_dev_handle *handle;
30#ifdef DO_FULL_USB_BUREAUCRACY
31    int res;
32#endif
33
34    usb_init();
35    usb_find_busses();
36    usb_find_devices();
37
38    for (bus = usb_get_busses(); bus; bus = bus->next)
39        for (dev = bus->devices; dev; dev = dev->next) {
40            if (dev->descriptor.idVendor !=
41                (vendor ? vendor : default_vendor))
42                continue;
43            if (dev->descriptor.idProduct !=
44                (product ? product : default_product))
45                continue;
46            handle = usb_open(dev);
47#ifdef DO_FULL_USB_BUREAUCRACY
48            if (!handle)
49                return NULL;
50            res = usb_set_configuration(handle, 1);
51            if (res < 0) {
52                fprintf(stderr, "usb_set_configuration: %d\n",
53                    res);
54                return NULL;
55            }
56            res = usb_claim_interface(handle, 0);
57            if (res < 0) {
58                fprintf(stderr, "usb_claim_interface: %d\n",
59                    res);
60                return NULL;
61            }
62            res = usb_set_altinterface(handle, 0);
63            if (res < 0) {
64                fprintf(stderr, "usb_set_altinterface: %d\n",
65                    res);
66                return NULL;
67            }
68#endif
69            return handle;
70        }
71    return NULL;
72}
73
74
75static void bad_id(const char *id)
76{
77    fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id);
78    exit(1);
79}
80
81
82void parse_usb_id(const char *id)
83{
84    unsigned long tmp;
85    char *end;
86
87    tmp = strtoul(id, &end, 16);
88    if (*end != ':')
89        bad_id(id);
90    if (tmp > 0xffff)
91        bad_id(id);
92    vendor = tmp;
93    tmp = strtoul(end+1, &end, 16);
94    if (*end)
95        bad_id(id);
96    if (tmp > 0xffff)
97        bad_id(id);
98    product = tmp;
99}
100

Archive Download this file

Branches:
master



interactive